From: Radim Krcmar <rkrcmar@redhat.com>
To: Marcelo Tosatti <mtosatti@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
Paolo Bonzini <pbonzini@redhat.com>,
Richard Cochran <richardcochran@gmail.com>,
Miroslav Lichvar <mlichvar@redhat.com>
Subject: Re: [patch 5/5] PTP: add kvm PTP driver
Date: Fri, 20 Jan 2017 15:12:56 +0100 [thread overview]
Message-ID: <20170120141255.GA1365@potion> (raw)
In-Reply-To: <20170120122503.842086637@redhat.com>
2017-01-20 10:20-0200, Marcelo Tosatti:
> Add a driver with gettime method returning hosts realtime clock.
> This allows Chrony to synchronize host and guest clocks with
> high precision (see results below).
>
> chronyc> sources
> MS Name/IP address Stratum Poll Reach LastRx Last sample
> ===============================================================================
> #* PHC0 0 3 377 4 +162ns[ -683ns] +/- 11ns
>
> To configure Chronyd to use PHC refclock, add the
> following line to its configuration file:
>
> refclock PHC /dev/ptpX poll 3 dpoll -2 offset 0
>
> Where /dev/ptpX is the kvmclock PTP clock.
>
> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
>
> ---
> drivers/ptp/Kconfig | 12 ++
> drivers/ptp/Makefile | 1
> drivers/ptp/ptp_kvm.c | 213 ++++++++++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 226 insertions(+)
>
> v2: check for kvmclock (Radim)
> initialize global variables before device registration (Radim)
> v3: use cross timestamps callback (Paolo, Miroslav, Radim)
>
> Index: kvm-ptpdriver/drivers/ptp/ptp_kvm.c
> ===================================================================
> --- /dev/null 1970-01-01 00:00:00.000000000 +0000
> +++ kvm-ptpdriver/drivers/ptp/ptp_kvm.c 2017-01-20 10:19:20.555311672 -0200
> @@ -0,0 +1,213 @@
> +/*
> + * Virtual PTP 1588 clock for use with KVM guests
> + *
> + * Copyright (C) 2017 Red Hat Inc.
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU General Public License for more details.
> + *
> + */
> +#include <linux/device.h>
> +#include <linux/err.h>
> +#include <linux/init.h>
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <uapi/linux/kvm_para.h>
> +#include <asm/kvm_para.h>
> +#include <asm/pvclock.h>
> +#include <asm/kvmclock.h>
> +#include <uapi/asm/kvm_para.h>
> +
> +#include <linux/ptp_clock_kernel.h>
> +
> +struct kvm_ptp_clock {
> + struct ptp_clock *ptp_clock;
> + struct ptp_clock_info caps;
> +};
> +
> +DEFINE_SPINLOCK(kvm_ptp_lock);
> +
> +static struct pvclock_vsyscall_time_info *hv_clock;
> +
> +static struct kvm_clock_offset clock_off;
> +static phys_addr_t clock_off_gpa;
> +
> +/*
> + * system_counterval.cycles: kvmclock value com TSC do host.
> + * system_counterval.cs: kvmclock clocksource.
> + * device_time: host realtime clock.
> + *
> + */
> +static int ptp_kvm_get_time_fn(ktime_t *device_time,
> + struct system_counterval_t *system_counter,
> + void *ctx)
> +{
> + unsigned long ret;
> + struct timespec64 tspec;
> + unsigned version;
> + u8 flags;
> + int cpu;
> + struct pvclock_vcpu_time_info *src;
> +
> + preempt_disable_notrace();
> + cpu = smp_processor_id();
> + src = &hv_clock[cpu].pvti;
> +
> + spin_lock(&kvm_ptp_lock);
What does the lock prevent?
> +
> + do {
> + /*
> + * We are measuring the delay between
> + * kvm_hypercall and rdtsc using TSC,
> + * and converting that delta to
> + * 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_off_gpa,
> + KVM_CLOCK_PAIRING_WALLCLOCK);
> + if (ret != 0) {
> + pr_err("clock offset hypercall ret %lu\n", ret);
> + spin_unlock(&kvm_ptp_lock);
> + preempt_enable_notrace();
> + return -EOPNOTSUPP;
> + }
> +
> + tspec.tv_sec = clock_off.sec;
> + tspec.tv_nsec = clock_off.nsec;
> + ret = __pvclock_read_cycles(src, clock_off.tsc);
> + flags = src->flags;
> + } while (pvclock_read_retry(src, version));
> +
> + preempt_enable_notrace();
> +
> + system_counter->cycles = ret;
> + system_counter->cs = get_kvmclock_cs();
Can't we use clocksource_tsc and just pass the tsc without kvmclock in
the middle?
> + tspec.tv_nsec = tspec.tv_nsec;
(This looks extraneous.)
Thanks.
next prev parent reply other threads:[~2017-01-20 14:12 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-01-20 12:20 [patch 0/5] KVM virtual PTP driver (v3) Marcelo Tosatti
2017-01-20 12:20 ` [patch 1/5] KVM: x86: provide realtime host clock via vsyscall notifiers Marcelo Tosatti
2017-01-20 12:20 ` [patch 2/5] KVM: x86: add KVM_HC_CLOCK_OFFSET hypercall Marcelo Tosatti
2017-01-20 12:20 ` [patch 3/5] kvmclock: export kvmclock clocksource pointer Marcelo Tosatti
2017-01-20 12:55 ` Paolo Bonzini
2017-01-20 12:20 ` [patch 4/5] PTP: add PTP_SYS_OFFSET emulation via cross timestamps infrastructure Marcelo Tosatti
2017-01-20 12:55 ` Paolo Bonzini
2017-01-20 13:07 ` Marcelo Tosatti
2017-01-20 13:36 ` Paolo Bonzini
2017-01-20 13:52 ` Marcelo Tosatti
2017-01-20 14:02 ` Radim Krcmar
2017-01-20 14:23 ` Paolo Bonzini
2017-01-20 14:31 ` Miroslav Lichvar
2017-01-20 18:30 ` Radim Krcmar
2017-01-20 20:25 ` Richard Cochran
2017-01-23 13:19 ` Marcelo Tosatti
2017-01-23 18:44 ` Richard Cochran
2017-01-23 19:44 ` Paolo Bonzini
2017-01-24 5:43 ` Richard Cochran
2017-01-24 11:23 ` Marcelo Tosatti
2017-01-24 11:35 ` Richard Cochran
2017-01-23 23:06 ` Marcelo Tosatti
2017-01-24 5:32 ` Richard Cochran
2017-01-24 8:15 ` Miroslav Lichvar
2017-01-20 12:20 ` [patch 5/5] PTP: add kvm PTP driver Marcelo Tosatti
2017-01-20 12:58 ` Paolo Bonzini
2017-01-20 13:11 ` Marcelo Tosatti
2017-01-20 14:12 ` Radim Krcmar [this message]
2017-01-20 14:20 ` Radim Krcmar
2017-01-20 15:00 ` Marcelo Tosatti
2017-01-20 17:11 ` Paolo Bonzini
2017-01-20 18:08 ` Radim Krcmar
2017-01-20 19:10 ` Marcelo Tosatti
2017-01-21 8:02 ` Paolo Bonzini
2017-01-20 13:10 ` [patch 0/5] KVM virtual PTP driver (v3) Paolo Bonzini
-- strict thread matches above, loose matches on Subject: below --
2017-01-20 14:51 [patch 0/5] KVM virtual PTP driver (v4) Marcelo Tosatti
2017-01-20 14:51 ` [patch 5/5] PTP: add kvm PTP driver Marcelo Tosatti
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170120141255.GA1365@potion \
--to=rkrcmar@redhat.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mlichvar@redhat.com \
--cc=mtosatti@redhat.com \
--cc=pbonzini@redhat.com \
--cc=richardcochran@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.