From patchwork Wed Feb 8 13:31:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 9562575 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 89EF460216 for ; Wed, 8 Feb 2017 13:43:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7CF2F284BA for ; Wed, 8 Feb 2017 13:43:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 71CFD284D0; Wed, 8 Feb 2017 13:43:17 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6533C284C9 for ; Wed, 8 Feb 2017 13:43:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753903AbdBHNnN (ORCPT ); Wed, 8 Feb 2017 08:43:13 -0500 Received: from mx1.redhat.com ([209.132.183.28]:46256 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753785AbdBHNnM (ORCPT ); Wed, 8 Feb 2017 08:43:12 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9429C7E9EC for ; Wed, 8 Feb 2017 13:32:12 +0000 (UTC) Received: from amt.cnet (vpn1-4-215.gru2.redhat.com [10.97.4.215]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v18DWB0l010587; Wed, 8 Feb 2017 08:32:12 -0500 Received: from amt.cnet (localhost [127.0.0.1]) by amt.cnet (Postfix) with ESMTP id 73758100F5C; Wed, 8 Feb 2017 11:31:48 -0200 (BRST) Received: (from marcelo@localhost) by amt.cnet (8.14.7/8.14.7/Submit) id v18DVhAD001153; Wed, 8 Feb 2017 11:31:43 -0200 Date: Wed, 8 Feb 2017 11:31:39 -0200 From: Marcelo Tosatti To: kvm-devel Cc: Paolo Bonzini , Radim =?utf-8?B?S3LEjW3DocWZ?= Subject: [PATCH] kvm PTP: drop TSC correction from ptp_kvm_gettime Message-ID: <20170208133138.GA1126@amt.cnet> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Wed, 08 Feb 2017 13:32:12 +0000 (UTC) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The TSC correction in ptp_kvm_gettime was an attempt to handle high load situations. Unfortunately it is incorrect because userspace expects PTP clock reads to happen in the middle point between realtime clock reads: |* | | | | | | | | | | | |* | | | | | | | | | | | |* | rt-read-1 ptp-clock-read-1 rt-read-2 The TSC correction causes the PTP clock reads to read the clock at: |* | | | | | | | | | | | | | | | | |* | | | | | | |* | rt-read-1 ptp-clock-read-1 rt-read-2 So drop the TSC offset correction, increasing precision of synchronization of an idle guest to <= 10ns. Signed-off-by: Marcelo Tosatti Reviewed-by: Radim Krčmář diff --git a/drivers/ptp/ptp_kvm.c b/drivers/ptp/ptp_kvm.c index 3ea41b8..994aa04 100644 --- a/drivers/ptp/ptp_kvm.c +++ b/drivers/ptp/ptp_kvm.c @@ -123,61 +123,21 @@ static int ptp_kvm_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts) { unsigned long ret; struct timespec64 tspec; - u64 delta, offset; - unsigned version; - int cpu; - struct pvclock_vcpu_time_info *src; spin_lock(&kvm_ptp_lock); - preempt_disable_notrace(); - cpu = smp_processor_id(); - src = &hv_clock[cpu].pvti; - - do { - /* - * We are measuring the delay between - * kvm_hypercall and rdtsc using TSC, - * and converting that delta through - * tsc_to_system_mul and tsc_shift - * So any changes to tsc_to_system_mul - * and tsc_shift in this region - * invalidate the measurement. - */ - version = pvclock_read_begin(src); - - ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING, - clock_pair_gpa, - KVM_CLOCK_PAIRING_WALLCLOCK); - if (ret != 0) { - pr_err_ratelimited("clock pairing hypercall ret %lu\n", ret); - spin_unlock(&kvm_ptp_lock); - preempt_enable_notrace(); - return -EOPNOTSUPP; - } - - tspec.tv_sec = clock_pair.sec; - tspec.tv_nsec = clock_pair.nsec; - - delta = rdtsc_ordered() - clock_pair.tsc; - offset = pvclock_scale_delta(delta, src->tsc_to_system_mul, - src->tsc_shift); - } while (pvclock_read_retry(src, version)); - - preempt_enable_notrace(); - - spin_unlock(&kvm_ptp_lock); - - while (unlikely(offset >= NSEC_PER_SEC)) { - tspec.tv_sec++; - offset -= NSEC_PER_SEC; + ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING, + clock_pair_gpa, + KVM_CLOCK_PAIRING_WALLCLOCK); + if (ret != 0) { + pr_err("clock offset hypercall ret %lu\n", ret); + spin_unlock(&kvm_ptp_lock); + return -EOPNOTSUPP; } - tspec.tv_nsec = tspec.tv_nsec + offset; - if (tspec.tv_nsec >= NSEC_PER_SEC) { - tspec.tv_sec++; - tspec.tv_nsec -= NSEC_PER_SEC; - } + tspec.tv_sec = clock_pair.sec; + tspec.tv_nsec = clock_pair.nsec; + spin_unlock(&kvm_ptp_lock); memcpy(ts, &tspec, sizeof(struct timespec64));