From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753364AbdF2Q6t (ORCPT ); Thu, 29 Jun 2017 12:58:49 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:33144 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753310AbdF2Q6e (ORCPT ); Thu, 29 Jun 2017 12:58:34 -0400 From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: rkrcmar@redhat.com, wanpeng.li@hotmail.com Subject: [PATCH 3/3] KVM: LAPIC: Fix lapic timer injection delay Date: Thu, 29 Jun 2017 18:58:21 +0200 Message-Id: <1498755501-39602-4-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1498755501-39602-1-git-send-email-pbonzini@redhat.com> References: <1498755501-39602-1-git-send-email-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wanpeng Li 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. The next vmentry results in an immediate vmx preemption timer vmexit and 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 ~1200 cycles on the tscdeadline_immed test of vmexit.flat. Cc: Paolo Bonzini Cc: Radim Krčmář Signed-off-by: Wanpeng Li [Rebased on top of previous patch. - Paolo] Signed-off-by: Paolo Bonzini --- arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/vmx.c | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index a80e5a5d6f2f..2819d4c123eb 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1525,8 +1525,11 @@ static bool start_hv_timer(struct kvm_lapic *apic) * the window. For periodic timer, leave the hv timer running for * 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; + } trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, true); return true; diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index e8b61ad84a8e..92ddea08f999 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -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; vmcs_set_bits(PIN_BASED_VM_EXEC_CONTROL, PIN_BASED_VMX_PREEMPTION_TIMER); - return 0; + + return delta_tsc == 0; } static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu) -- 1.8.3.1