Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp3069559imm; Mon, 28 May 2018 23:54:04 -0700 (PDT) X-Google-Smtp-Source: AB8JxZp5Jx2uBCbaWVkYoa32LyaLatPHTTkiBhNvKZRAh7SjOXz6JZgphpl7ckktbRuIIUrF80ot X-Received: by 2002:a17:902:42a3:: with SMTP id h32-v6mr16603393pld.72.1527576844495; Mon, 28 May 2018 23:54:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527576844; cv=none; d=google.com; s=arc-20160816; b=pIgKRkLd3idKT2JkLd/gqQWfqlys+lWLnQ+CWJVmvPxLR0wn4th5TtCxf0YzWSLTrV M2P2i/US6P6i6LrvqHS794qaZUco2UqvV1X5R1B46z4GWSuZULNslh5BwHMWKN2HO7kd eVLxi+uHjP0jkKJFCi4xzhcOSXki5a+9PnwKKjGXyN/j5rWOgLzg8KjF2xh7zvzOD9nk 2f59u9RwBj+jjhfhNJaS2dohBxuz8gHfm2BR+Dhg0d60vFefvnai/z0GlV57eUxx7Cab DUrmcjOoo3AVaaZNjA/n7wE0kjkgVayqvU/jye9+w1cNUkOdUfozWC8stPFal1DrC9PX pCQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature :arc-authentication-results; bh=vCPP2KwOaOUZanjoYUiaCYjfoAyj6F80yTbQsvbnJEY=; b=yiPCNPIKZ6eqvpwa8ce0KvSiWDeeclFmcoKpPAAuMXkA/6PVVrG28vSAKX8pFiEsRE dDYJoouMqSZxdc9Fa7DcIYpPjODyZ9WgjAW6F9SrRPKWzykNlDBRJypFOKYk/+SDZP2A jPWoWnWyvbQ2njuW55kX4+JBWFUIVuBS1MJLY78TbSlWPtcQknjZ13DRz+qOfAGEWWU1 Tys8kslHpJumhjS48yGDEtBGdH50Fks6XOjOSfx5EnUQ3bSBTmuAplkdzv7MPit8v3a/ Yjns3jvx10k+D5P1E+5chpfX6wRu1rbi6PSZaucR4GThOhs72tarsLs8ISEzo2ILUs4O 7MbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Ys+Ct2Q1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r12-v6si22380993pgf.653.2018.05.28.23.53.49; Mon, 28 May 2018 23:54:04 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Ys+Ct2Q1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754671AbeE2GxZ (ORCPT + 99 others); Tue, 29 May 2018 02:53:25 -0400 Received: from mail-pl0-f68.google.com ([209.85.160.68]:36525 "EHLO mail-pl0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751444AbeE2GxW (ORCPT ); Tue, 29 May 2018 02:53:22 -0400 Received: by mail-pl0-f68.google.com with SMTP id v24-v6so8389970plo.3; Mon, 28 May 2018 23:53:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=vCPP2KwOaOUZanjoYUiaCYjfoAyj6F80yTbQsvbnJEY=; b=Ys+Ct2Q1ijLKu/hIsGtMNZ8DFlfR4gOrhXOs9diIO47iomR/PdKJl4OCZaW69ViJJd SKU3wxIsP5bOEzD68khRXvP8PRDV7Wipg69DhRpb0Z0v7mOAO3Zx07BGiO8fwJO2TM+n XmLHLY255rfpO+ejD0pl2SmTEqxQ9fTl+1nK/ueJWiW63FrE3yYjAEXndZJxq7Bp99Zt PasD+S+pymEJjE+u/RmmUzlQ0DuVUHSvkROIeGheWCf6kyDGhiCnzP4FuR3wjtFLb3Fw tpx4Ac+YBwWPGAIPDNrfgRDRWRFVf/PFiF7fghywM898fxn1EZf7NklsRubPmQILk2V0 lgSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=vCPP2KwOaOUZanjoYUiaCYjfoAyj6F80yTbQsvbnJEY=; b=WzmdNh618buPK/g8WD0hH1/ESly3il5Upz/DGL8FjBQurjmsYnfiiwl6MZnx7+L+Ke lBH5CE86m57CtcNTrSrtCiWFtMjJzHX4VffJjhJaQGmHA5gbkQUw8ShC5Fnykn2JgiNa fzAEzkkPcFskNQ2g1I0ghbJ5rRWj6lXuVxDIjSuI18p1twMQWU51YCgyU2e/HuvEGAcq p+7f3pYtbrlXvKi5xdkXfjLH6fYzVL8J4g2rUvoM7Qz20NyOuBS+t0NnuPZGfCHxXXl1 D/Ty5cPp00dMdYIq6HsjUVNy8UjJAFT5fYfkbLbA8jMugXX2/3S/LH+pSIVwPuZPjtjD LrPw== X-Gm-Message-State: ALKqPwdaq8WwtIT+GeOOfaahftFPiGXzcNmm2U0MgudC8PWevZxgrxm2 8xl8bsF/Dsjp62ifADhBylcFKA== X-Received: by 2002:a17:902:7896:: with SMTP id q22-v6mr16593554pll.243.1527576801723; Mon, 28 May 2018 23:53:21 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id x4-v6sm54394672pfb.99.2018.05.28.23.53.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 28 May 2018 23:53:21 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= Subject: [PATCH] KVM: VMX: Optimize tscdeadline timer latency Date: Tue, 29 May 2018 14:53:17 +0800 Message-Id: <1527576797-5738-1-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wanpeng Li 'Commit d0659d946be0 ("KVM: x86: add option to advance tscdeadline hrtimer expiration")' advances the tscdeadline (the timer is emulated by hrtimer) expiration in order that the latency which is incurred by hypervisor (apic_timer_fn -> vmentry) can be avoided. This patch adds the advance tscdeadline expiration support to which the tscdeadline timer is emulated by VMX preemption timer to reduce the hypervisor lantency (handle_preemption_timer -> vmentry). clockevents infrastruture can program minimum delay if hrtimer feeds a expiration in the past, we set delta_tsc to 1(which will be converted to 0 before vmentry) which can lead to an immediately vmexit when delta_tsc is not bigger than advance ns. This patch can reduce ~63% latency (~4450 cycles to ~1660 cycles on a haswell desktop) for kvm-unit-tests/tscdeadline_latency when testing busy waits. Cc: Paolo Bonzini Cc: Radim Krčmář Signed-off-by: Wanpeng Li --- arch/x86/kvm/vmx.c | 8 +++++++- arch/x86/kvm/x86.c | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index e50beb7..d8b5110 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -12435,7 +12435,7 @@ static inline int u64_shl_div_u64(u64 a, unsigned int shift, static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) { struct vcpu_vmx *vmx; - u64 tscl, guest_tscl, delta_tsc; + u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles; if (kvm_mwait_in_guest(vcpu->kvm)) return -EOPNOTSUPP; @@ -12444,6 +12444,12 @@ static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc) tscl = rdtsc(); guest_tscl = kvm_read_l1_tsc(vcpu, tscl); delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl; + lapic_timer_advance_cycles = nsec_to_cycles(vcpu, lapic_timer_advance_ns); + + if (delta_tsc > lapic_timer_advance_cycles) + delta_tsc -= lapic_timer_advance_cycles; + else + delta_tsc = 1; /* Convert to host delta tsc if tsc scaling is enabled */ if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio && diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 22bd20f..d8abd96 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -138,6 +138,7 @@ module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR); /* lapic timer advance (tscdeadline mode only) in nanoseconds */ unsigned int __read_mostly lapic_timer_advance_ns = 0; module_param(lapic_timer_advance_ns, uint, S_IRUGO | S_IWUSR); +EXPORT_SYMBOL_GPL(lapic_timer_advance_ns); static bool __read_mostly vector_hashing = true; module_param(vector_hashing, bool, S_IRUGO); -- 2.7.4