• Mingwei Zhang's avatar
    KVM: x86: Service NMI requests after PMI requests in VM-Enter path · fad505b2
    Mingwei Zhang authored
    Service NMI and SMI requests after PMI requests in vcpu_enter_guest() so
    that KVM does not need to cancel and redo the VM-Enter if the guest
    configures its PMIs to be delivered as NMIs (likely) or SMIs (unlikely).
    Because APIC emulation "injects" NMIs via KVM_REQ_NMI, handling PMI
    requests after NMI requests (the likely case) means KVM won't detect the
    pending NMI request until the final check for outstanding requests.
    Detecting requests at the final stage is costly as KVM has already loaded
    guest state, potentially queued events for injection, disabled IRQs,
    dropped SRCU, etc., most of which needs to be unwound.
    
    Note that changing the order of request processing doesn't change the end
    result, as KVM's final check for outstanding requests prevents entering
    the guest until all requests are serviced.  I.e. KVM will ultimately
    coalesce events (or not) regardless of the ordering.
    
    Using SPEC2017 benchmark programs running along with Intel vtune in a VM
    demonstrates that the following code change reduces 800~1500 canceled
    VM-Enters per second.
    
    Some glory details:
    
    Probe the invocation to vmx_cancel_injection():
    
        $ perf probe -a vmx_cancel_injection
        $ perf stat -a -e probe:vmx_cancel_injection -I 10000 # per 10 seconds
    
    Partial results when SPEC2017 with Intel vtune are running in the VM:
    
    On kernel without the change:
        10.010018010              14254      probe:vmx_cancel_injection
        20.037646388              15207      probe:vmx_cancel_injection
        30.078739816              15261      probe:vmx_cancel_injection
        40.114033258              15085      probe:vmx_cancel_injection
        50.149297460              15112      probe:vmx_cancel_injection
        60.185103088              15104      probe:vmx_cancel_injection
    
    On kernel with the change:
        10.003595390                 40      probe:vmx_cancel_injection
        20.017855682                 31      probe:vmx_cancel_injection
        30.028355883                 34      probe:vmx_cancel_injection
        40.038686298                 31      probe:vmx_cancel_injection
        50.048795162                 20      probe:vmx_cancel_injection
        60.069057747                 19      probe:vmx_cancel_injection
    Suggested-by: default avatarSean Christopherson <seanjc@google.com>
    Signed-off-by: default avatarMingwei Zhang <mizhang@google.com>
    Link: https://lore.kernel.org/r/20231002040839.2630027-1-mizhang@google.com
    [sean: hoist PMU/PMI above SMI too, massage changelog]
    Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
    fad505b2
x86.c 364 KB