linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Kelley <mikelley@microsoft.com>
To: Vineeth Pillai <viremana@linux.microsoft.com>,
	Haiyang Zhang <haiyangz@microsoft.com>,
	Stephen Hemminger <sthemmin@microsoft.com>,
	Wei Liu <wei.liu@kernel.org>
Cc: KY Srinivasan <kys@microsoft.com>,
	"linux-hyperv@vger.kernel.org" <linux-hyperv@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: RE: [PATCH] hv_utils: return error if host timesysnc update is stale
Date: Thu, 20 Aug 2020 18:21:09 +0000	[thread overview]
Message-ID: <MW2PR2101MB10527EAC115BF49715BF4722D75A0@MW2PR2101MB1052.namprd21.prod.outlook.com> (raw)
In-Reply-To: <20200819174527.47156-1-viremana@linux.microsoft.com>

From: Vineeth Pillai <viremana@linux.microsoft.com> Sent: Wednesday, August 19, 2020 10:45 AM
> 
> If for any reason, host timesync messages were not processed by
> the guest, hv_ptp_gettime() returns a stale value and the
> caller (clock_gettime, PTP ioctl etc) has no means to know this
> now. Return an error so that the caller knows about this.
> 
> Signed-off-by: Vineeth Pillai <viremana@linux.microsoft.com>
> ---
>  drivers/hv/hv_util.c | 46 +++++++++++++++++++++++++++++++++-----------
>  1 file changed, 35 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/hv/hv_util.c b/drivers/hv/hv_util.c
> index 92ee0fe4c919..1357861fd8ae 100644
> --- a/drivers/hv/hv_util.c
> +++ b/drivers/hv/hv_util.c
> @@ -282,26 +282,52 @@ static struct {
>  	spinlock_t			lock;
>  } host_ts;
> 
> -static struct timespec64 hv_get_adj_host_time(void)
> +static inline u64 reftime_to_ns(u64 reftime)
>  {
> -	struct timespec64 ts;
> -	u64 newtime, reftime;
> +	return (reftime - WLTIMEDELTA) * 100;
> +}
> +
> +/*
> + * Hard coded threshold for host timesync delay: 600 seconds
> + */
> +const u64 HOST_TIMESYNC_DELAY_THRESH = 600 * NSEC_PER_SEC;

Kernel test robot has already complained that this should be static,
and about the potential overflow based on the types of the constants in
the right side expression.  I didn't check the details, but I suspect the
complaint is when building in 32-bit mode.  This code does get built in
32-bit mode and it's possible for run 32-bit Linux guests on Hyper-V.

> +
> +static int hv_get_adj_host_time(struct timespec64 *ts)
> +{
> +	u64 newtime, reftime, timediff_adj;
>  	unsigned long flags;
> +	int ret = 0;
> 
>  	spin_lock_irqsave(&host_ts.lock, flags);
>  	reftime = hv_read_reference_counter();
> -	newtime = host_ts.host_time + (reftime - host_ts.ref_time);
> -	ts = ns_to_timespec64((newtime - WLTIMEDELTA) * 100);
> +
> +	/*
> +	 * We need to let the caller know that last update from host
> +	 * is older than the max allowable threshold. clock_gettime()
> +	 * and PTP ioctl do not have a documented error that we could
> +	 * return for this specific case. Use ESTALE to report this.
> +	 */
> +	timediff_adj = reftime - host_ts.ref_time;
> +	if (timediff_adj * 100 > HOST_TIMESYNC_DELAY_THRESH) {
> +		pr_warn("TIMESYNC IC: Stale time stamp, %llu nsecs old\n",
> +			HOST_TIMESYNC_DELAY_THRESH);

Let's provide the timediff_adj in the message instead of the constant
threshold value so we know the degree of staleness. :-)

Also, I'm wondering if this should be pr_warn_once().  Presumably
chronyd or whoever is reading /dev/ptp0 will give up after getting
this error, but if not, it would be nice to avoid filling up the console
with these error messages.

> +		ret = -ESTALE;
> +	}
> +
> +	newtime = host_ts.host_time + timediff_adj;
> +	*ts = ns_to_timespec64(reftime_to_ns(newtime));
>  	spin_unlock_irqrestore(&host_ts.lock, flags);
> 
> -	return ts;
> +	return ret;
>  }
> 
>  static void hv_set_host_time(struct work_struct *work)
>  {
> -	struct timespec64 ts = hv_get_adj_host_time();
> 
> -	do_settimeofday64(&ts);
> +	struct timespec64 ts;
> +
> +	if (!hv_get_adj_host_time(&ts))
> +		do_settimeofday64(&ts);
>  }
> 
>  /*
> @@ -622,9 +648,7 @@ static int hv_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
> 
>  static int hv_ptp_gettime(struct ptp_clock_info *info, struct timespec64 *ts)
>  {
> -	*ts = hv_get_adj_host_time();
> -
> -	return 0;
> +	return hv_get_adj_host_time(ts);
>  }
> 
>  static struct ptp_clock_info ptp_hyperv_info = {
> --
> 2.17.1


  parent reply	other threads:[~2020-08-20 18:21 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-19 17:45 [PATCH] hv_utils: return error if host timesysnc update is stale Vineeth Pillai
2020-08-19 22:19 ` kernel test robot
2020-08-19 22:19 ` [RFC PATCH] hv_utils: HOST_TIMESYNC_DELAY_THRESH can be static kernel test robot
2020-08-19 22:54 ` [PATCH] hv_utils: return error if host timesysnc update is stale kernel test robot
2020-08-20 18:21 ` Michael Kelley [this message]
2020-08-20 21:39   ` Vineeth Pillai

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=MW2PR2101MB10527EAC115BF49715BF4722D75A0@MW2PR2101MB1052.namprd21.prod.outlook.com \
    --to=mikelley@microsoft.com \
    --cc=haiyangz@microsoft.com \
    --cc=kys@microsoft.com \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sthemmin@microsoft.com \
    --cc=viremana@linux.microsoft.com \
    --cc=wei.liu@kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).