Commit c8533544 authored by Wanpeng Li's avatar Wanpeng Li Committed by Paolo Bonzini

KVM: LAPIC: Fix lapic timer injection delay

If the TSC deadline timer is programmed really close to the deadline or
even in the past, the computation in vmx_set_hv_timer will program the
absolute target tsc value to vmcs preemption timer field w/ delta == 0,
then plays a vmentry and an upcoming vmx preemption timer fire vmexit
dance, the lapic timer injection is delayed due to this duration. Actually
the lapic timer which is emulated by hrtimer can handle this correctly.

This patch fixes it by firing the lapic timer and injecting a timer interrupt
immediately during the next vmentry if the TSC deadline timer is programmed
really close to the deadline or even in the past. This saves ~300 cycles on
the tsc_deadline_timer test of apic.flat.

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Signed-off-by: default avatarWanpeng Li <wanpeng.li@hotmail.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent a749e247
...@@ -1525,8 +1525,11 @@ static bool start_hv_timer(struct kvm_lapic *apic) ...@@ -1525,8 +1525,11 @@ static bool start_hv_timer(struct kvm_lapic *apic)
* the window. For periodic timer, leave the hv timer running for * the window. For periodic timer, leave the hv timer running for
* simplicity, and the deadline will be recomputed on the next vmexit. * simplicity, and the deadline will be recomputed on the next vmexit.
*/ */
if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending)) if (!apic_lvtt_period(apic) && (r || atomic_read(&ktimer->pending))) {
if (r)
apic_timer_expired(apic);
return false; return false;
}
trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, true); trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, true);
return true; return true;
......
...@@ -11147,7 +11147,8 @@ static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) ...@@ -11147,7 +11147,8 @@ static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc)
vmx->hv_deadline_tsc = tscl + delta_tsc; vmx->hv_deadline_tsc = tscl + delta_tsc;
vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL,
PIN_BASED_VMX_PREEMPTION_TIMER); PIN_BASED_VMX_PREEMPTION_TIMER);
return 0;
return delta_tsc == 0;
} }
static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment