• Sean Christopherson's avatar
    KVM: nVMX: Detect nested posted interrupt NV at nested VM-Exit injection · 6e0b4565
    Sean Christopherson authored
    When synthensizing a nested VM-Exit due to an external interrupt, pend a
    nested posted interrupt if the external interrupt vector matches L2's PI
    notification vector, i.e. if the interrupt is a PI notification for L2.
    This fixes a bug where KVM will incorrectly inject VM-Exit instead of
    processing nested posted interrupt when IPI virtualization is enabled.
    
    Per the SDM, detection of the notification vector doesn't occur until the
    interrupt is acknowledge and deliver to the CPU core.
    
      If the external-interrupt exiting VM-execution control is 1, any unmasked
      external interrupt causes a VM exit (see Section 26.2). If the "process
      posted interrupts" VM-execution control is also 1, this behavior is
      changed and the processor handles an external interrupt as follows:
    
        1. The local APIC is acknowledged; this provides the processor core
           with an interrupt vector, called here the physical vector.
        2. If the physical vector equals the posted-interrupt notification
           vector, the logical processor continues to the next step. Otherwise,
           a VM exit occurs as it would normally due to an external interrupt;
           the vector is saved in the VM-exit interruption-information field.
    
    For the most part, KVM has avoided problems because a PI NV for L2 that
    arrives will L2 is active will be processed by hardware, and KVM checks
    for a pending notification vector during nested VM-Enter.  Thus, to hit
    the bug, the PI NV interrupt needs to sneak its way into L1's vIRR while
    L2 is active.
    
    Without IPI virtualization, the scenario is practically impossible to hit,
    modulo L1 doing weird things (see below), as the ordering between
    vmx_deliver_posted_interrupt() and nested VM-Enter effectively guarantees
    that either the sender will see the vCPU as being in_guest_mode(), or the
    receiver will see the interrupt in its vIRR.
    
    With IPI virtualization, introduced by commit d588bb9b
    
     ("KVM: VMX:
    enable IPI virtualization"), the sending CPU effectively implements a rough
    equivalent of vmx_deliver_posted_interrupt(), sans the nested PI NV check.
    If the target vCPU has a valid PID, the CPU will send a PI NV interrupt
    based on _L1's_ PID, as the sender's because IPIv table points at L1 PIDs.
    
      PIR := 32 bytes at PID_ADDR;
      // under lock
      PIR[V] := 1;
      store PIR at PID_ADDR;
      // release lock
    
      NotifyInfo := 8 bytes at PID_ADDR + 32;
      // under lock
      IF NotifyInfo.ON = 0 AND NotifyInfo.SN = 0; THEN
        NotifyInfo.ON := 1;
        SendNotify := 1;
      ELSE
        SendNotify := 0;
      FI;
      store NotifyInfo at PID_ADDR + 32;
      // release lock
    
      IF SendNotify = 1; THEN
        send an IPI specified by NotifyInfo.NDST and NotifyInfo.NV;
      FI;
    
    As a result, the target vCPU ends up receiving an interrupt on KVM's
    POSTED_INTR_VECTOR while L2 is running, with an interrupt in L1's PIR for
    L2's nested PI NV.  The POSTED_INTR_VECTOR interrupt triggers a VM-Exit
    from L2 to L0, KVM moves the interrupt from L1's PIR to vIRR, triggers a
    KVM_REQ_EVENT prior to re-entry to L2, and calls vmx_check_nested_events(),
    effectively bypassing all of KVM's "early" checks on nested PI NV.
    
    Without IPI virtualization, the bug can likely be hit only if L1 programs
    an assigned device to _post_ an interrupt to L2's notification vector, by
    way of L1's PID.PIR.  Doing so would allow the interrupt to get into L1's
    vIRR without KVM checking vmcs12's NV.  Which is architecturally allowed,
    but unlikely behavior for a hypervisor.
    
    Cc: Zeng Guang <guang.zeng@intel.com>
    Reviewed-by: default avatarChao Gao <chao.gao@intel.com>
    Link: https://lore.kernel.org/r/20240906043413.1049633-5-seanjc@google.com
    
    Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
    6e0b4565
nested.c 224 KB