• Haitao Shan's avatar
    KVM: x86: Fix lapic timer interrupt lost after loading a snapshot. · 9cfec6d0
    Haitao Shan authored
    When running android emulator (which is based on QEMU 2.12) on
    certain Intel hosts with kernel version 6.3-rc1 or above, guest
    will freeze after loading a snapshot. This is almost 100%
    reproducible. By default, the android emulator will use snapshot
    to speed up the next launching of the same android guest. So
    this breaks the android emulator badly.
    
    I tested QEMU 8.0.4 from Debian 12 with an Ubuntu 22.04 guest by
    running command "loadvm" after "savevm". The same issue is
    observed. At the same time, none of our AMD platforms is impacted.
    More experiments show that loading the KVM module with
    "enable_apicv=false" can workaround it.
    
    The issue started to show up after commit 8e6ed96c ("KVM: x86:
    fire timer when it is migrated and expired, and in oneshot mode").
    However, as is pointed out by Sean Christopherson, it is introduced
    by commit 967235d3 ("KVM: vmx: clear pending interrupts on
    KVM_SET_LAPIC"). commit 8e6ed96c ("KVM: x86: fire timer when
    it is migrated and expired, and in oneshot mode") just makes it
    easier to hit the issue.
    
    Having both commits, the oneshot lapic timer gets fired immediately
    inside the KVM_SET_LAPIC call when loading the snapshot. On Intel
    platforms with APIC virtualization and posted interrupt processing,
    this eventually leads to setting the corresponding PIR bit. However,
    the whole PIR bits get cleared later in the same KVM_SET_LAPIC call
    by apicv_post_state_restore. This leads to timer interrupt lost.
    
    The fix is to move vmx_apicv_post_state_restore to the beginning of
    the KVM_SET_LAPIC call and rename to vmx_apicv_pre_state_restore.
    What vmx_apicv_post_state_restore does is actually clearing any
    former apicv state and this behavior is more suitable to carry out
    in the beginning.
    
    Fixes: 967235d3 ("KVM: vmx: clear pending interrupts on KVM_SET_LAPIC")
    Cc: stable@vger.kernel.org
    Suggested-by: default avatarSean Christopherson <seanjc@google.com>
    Signed-off-by: default avatarHaitao Shan <hshan@google.com>
    Link: https://lore.kernel.org/r/20230913000215.478387-1-hshan@google.comSigned-off-by: default avatarSean Christopherson <seanjc@google.com>
    9cfec6d0
vmx.c 247 KB