• Luiz Capitulino's avatar
    kvm: x86: make lapic hrtimer pinned · 61abdbe0
    Luiz Capitulino authored
    When a vCPU runs on a nohz_full core, the hrtimer used by
    the lapic emulation code can be migrated to another core.
    When this happens, it's possible to observe milisecond
    latency when delivering timer IRQs to KVM guests.
    
    The huge latency is mainly due to the fact that
    apic_timer_fn() expects to run during a kvm exit. It
    sets KVM_REQ_PENDING_TIMER and let it be handled on kvm
    entry. However, if the timer fires on a different core,
    we have to wait until the next kvm exit for the guest
    to see KVM_REQ_PENDING_TIMER set.
    
    This problem became visible after commit 9642d18e
    
    . This
    commit changed the timer migration code to always attempt
    to migrate timers away from nohz_full cores. While it's
    discussable if this is correct/desirable (I don't think
    it is), it's clear that the lapic emulation code has
    a requirement on firing the hrtimer in the same core
    where it was started. This is achieved by making the
    hrtimer pinned.
    
    Lastly, note that KVM has code to migrate timers when a
    vCPU is scheduled to run in different core. However, this
    forced migration may fail. When this happens, we can have
    the same problem. If we want 100% correctness, we'll have
    to modify apic_timer_fn() to cause a kvm exit when it runs
    on a different core than the vCPU. Not sure if this is
    possible.
    
    Here's a reproducer for the issue being fixed:
    
     1. Set all cores but core0 to be nohz_full cores
     2. Start a guest with a single vCPU
     3. Trace apic_timer_fn() and kvm_inject_apic_timer_irqs()
    
    You'll see that apic_timer_fn() will run in core0 while
    kvm_inject_apic_timer_irqs() runs in a different core. If
    you get both on core0, try running a program that takes 100%
    of the CPU and pin it to core0 to force the vCPU out.
    
    Signed-off-by: default avatarLuiz Capitulino <lcapitulino@redhat.com>
    Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
    61abdbe0