Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2572541yba; Mon, 6 May 2019 08:10:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqxmXW2Bnu6rxQL3oFVjYtnxaRd82FQftWzGrryClISq58Ppv+kjfjO/tYW3Kvh4gwVfB2wi X-Received: by 2002:a63:5659:: with SMTP id g25mr32906490pgm.59.1557155454742; Mon, 06 May 2019 08:10:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557155454; cv=none; d=google.com; s=arc-20160816; b=nnfUeiMbg7g5DpPMfqLAUZCgW1QA+lwVIjtYzpXAjdOowctjZv6ayMTL9/8NRfaGIj 9sJK5iItGCc30Y0KXiV60fT9AA3Iu+rJ4zhnohoqKV8AL9o9GZeaPG+anUnxYtXqQDIy XeilDiHqn4zNP2rNV/Y0R/liTY5bnD82blcsVSEjQuZ1B/0fxhmAcSN19I9MIJyQr9Cb U2RD9ecxNtug/pUuH6DaL6ZG/tiTOOfS7UtFGnsr2z8gnBZCcOjTgQYU47Ksv87HtqtK jmLm44keOI3i13J+MnQ+dN5rRVP+QPVdeu9JYUGMz6IGSk0kzVsMqZXoi9p329A54Ozn rRSA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=0WPvb8JI+Vzkssr3pTnizmskrrSD8WT2DLXCK5bQj4U=; b=oHLGo2fiEfHsfC5j9HxfEMgax3vpTP0WQ1msLeK2Y3Wssgg/m5LdRt2iaaxAatUqo4 RhMweGSgRO5mLdq+u7etKvtEqdU0qyuP78a5SZf2F7C25s9mjul1EZpPSbfXAxnIn9TL vUgN9HB/c8rwb6Z4MqayRNwsc2PwkfpgHs/k5xO4CEfpZkxPsE18zAwHAr+lXzswO8qW YeB3wIu5UGCuQoJEgDGw1GI5kDJ/pDaQe9qA0vNwuBPtyTR6Se2QTncHOrKLCjUPbPX2 +ABaZaBAp0d9BK3K/5+kSA/nLz8dbZAZlMOt3zY3zcpHLyoNpnVAprOapZ2y4I3UNxTu oxdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=uTXK7weI; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v66si7866330pgb.542.2019.05.06.08.10.36; Mon, 06 May 2019 08:10:54 -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=@kernel.org header.s=default header.b=uTXK7weI; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727272AbfEFPJd (ORCPT + 99 others); Mon, 6 May 2019 11:09:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:55422 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726407AbfEFOfX (ORCPT ); Mon, 6 May 2019 10:35:23 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A46C8214C6; Mon, 6 May 2019 14:35:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1557153322; bh=IhMrdxYw7BZjtqmi2BAZfg1TqFu9OA85yOul+YcPvms=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uTXK7weI1jQbPYNnIwe4QriaU2uaCoa+fMSGUGavj4yq+Z3k0n2RzeI1tLMU0FmLj UKdXO1YVWXw6ObZwX5rZz9SCLhvpXfzy4/FjphEzMrc9MpqhwHNsUK/d0oYzU9FLx6 o9GvGX6FC5lHrrlrgwh9jtJkhQNT+0M5hCxpl1Cw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Liran Alon , Wanpeng Li , Sean Christopherson , Paolo Bonzini Subject: [PATCH 5.0 024/122] KVM: lapic: Convert guest TSC to host time domain if necessary Date: Mon, 6 May 2019 16:31:22 +0200 Message-Id: <20190506143056.922875731@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190506143054.670334917@linuxfoundation.org> References: <20190506143054.670334917@linuxfoundation.org> User-Agent: quilt/0.66 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: Sean Christopherson commit b6aa57c69cb26ea0160c51f7cf45f1af23542686 upstream. To minimize the latency of timer interrupts as observed by the guest, KVM adjusts the values it programs into the host timers to account for the host's overhead of programming and handling the timer event. In the event that the adjustments are too aggressive, i.e. the timer fires earlier than the guest expects, KVM busy waits immediately prior to entering the guest. Currently, KVM manually converts the delay from nanoseconds to clock cycles. But, the conversion is done in the guest's time domain, while the delay occurs in the host's time domain. This is perfectly ok when the guest and host are using the same TSC ratio, but if the guest is using a different ratio then the delay may not be accurate and could wait too little or too long. When the guest is not using the host's ratio, convert the delay from guest clock cycles to host nanoseconds and use ndelay() instead of __delay() to provide more accurate timing. Because converting to nanoseconds is relatively expensive, e.g. requires division and more multiplication ops, continue using __delay() directly when guest and host TSCs are running at the same ratio. Cc: Liran Alon Cc: Wanpeng Li Cc: stable@vger.kernel.org Fixes: 3b8a5df6c4dc6 ("KVM: LAPIC: Tune lapic_timer_advance_ns automatically") Signed-off-by: Sean Christopherson Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/lapic.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1478,6 +1478,26 @@ static bool lapic_timer_int_injected(str return false; } +static inline void __wait_lapic_expire(struct kvm_vcpu *vcpu, u64 guest_cycles) +{ + u64 timer_advance_ns = vcpu->arch.apic->lapic_timer.timer_advance_ns; + + /* + * If the guest TSC is running at a different ratio than the host, then + * convert the delay to nanoseconds to achieve an accurate delay. Note + * that __delay() uses delay_tsc whenever the hardware has TSC, thus + * always for VMX enabled hardware. + */ + if (vcpu->arch.tsc_scaling_ratio == kvm_default_tsc_scaling_ratio) { + __delay(min(guest_cycles, + nsec_to_cycles(vcpu, timer_advance_ns))); + } else { + u64 delay_ns = guest_cycles * 1000000ULL; + do_div(delay_ns, vcpu->arch.virtual_tsc_khz); + ndelay(min_t(u32, delay_ns, timer_advance_ns)); + } +} + void wait_lapic_expire(struct kvm_vcpu *vcpu) { struct kvm_lapic *apic = vcpu->arch.apic; @@ -1498,10 +1518,8 @@ void wait_lapic_expire(struct kvm_vcpu * guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline); - /* __delay is delay_tsc whenever the hardware has TSC, thus always. */ if (guest_tsc < tsc_deadline) - __delay(min(tsc_deadline - guest_tsc, - nsec_to_cycles(vcpu, timer_advance_ns))); + __wait_lapic_expire(vcpu, tsc_deadline - guest_tsc); if (!apic->lapic_timer.timer_advance_adjust_done) { /* too early */