linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] hv_utils: return error if host timesysnc update is stale
@ 2020-08-21 15:25 Vineeth Pillai
  2020-08-21 15:44 ` Michael Kelley
  0 siblings, 1 reply; 3+ messages in thread
From: Vineeth Pillai @ 2020-08-21 15:25 UTC (permalink / raw)
  To: Haiyang Zhang, Stephen Hemminger, Wei Liu
  Cc: Vineeth Pillai, K . Y . Srinivasan, linux-hyperv, linux-kernel

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>
---

 v2:
    - Fix warnings reported by Kernel test robot <lkp@intel.com>
    - s/pr_warn/pr_warn_once/

---
 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..1f86e8d9b018 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
+ */
+static const u64 HOST_TIMESYNC_DELAY_THRESH = 600 * (u64)NSEC_PER_SEC;
+
+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_once("TIMESYNC IC: Stale time stamp, %llu nsecs old\n",
+			     (timediff_adj * 100));
+		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 = {

base-commit: 49971e6bad2da980686368baa34039907f62b516
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* RE: [PATCH v2] hv_utils: return error if host timesysnc update is stale
  2020-08-21 15:25 [PATCH v2] hv_utils: return error if host timesysnc update is stale Vineeth Pillai
@ 2020-08-21 15:44 ` Michael Kelley
  2020-08-24 14:51   ` Wei Liu
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Kelley @ 2020-08-21 15:44 UTC (permalink / raw)
  To: Vineeth Pillai, Haiyang Zhang, Stephen Hemminger, Wei Liu
  Cc: KY Srinivasan, linux-hyperv, linux-kernel

From: viremana@linux.microsoft.com Sent: Friday, August 21, 2020 8:25 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>
> ---
> 
>  v2:
>     - Fix warnings reported by Kernel test robot <lkp@intel.com>
>     - s/pr_warn/pr_warn_once/
> 
> ---
>  drivers/hv/hv_util.c | 46 +++++++++++++++++++++++++++++++++-----------
>  1 file changed, 35 insertions(+), 11 deletions(-)

Reviewed-by: Michael Kelley <mikelley@microsoft.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] hv_utils: return error if host timesysnc update is stale
  2020-08-21 15:44 ` Michael Kelley
@ 2020-08-24 14:51   ` Wei Liu
  0 siblings, 0 replies; 3+ messages in thread
From: Wei Liu @ 2020-08-24 14:51 UTC (permalink / raw)
  To: Michael Kelley
  Cc: Vineeth Pillai, Haiyang Zhang, Stephen Hemminger, Wei Liu,
	KY Srinivasan, linux-hyperv, linux-kernel

On Fri, Aug 21, 2020 at 03:44:24PM +0000, Michael Kelley wrote:
> From: viremana@linux.microsoft.com Sent: Friday, August 21, 2020 8:25 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>
> > ---
> > 
> >  v2:
> >     - Fix warnings reported by Kernel test robot <lkp@intel.com>
> >     - s/pr_warn/pr_warn_once/
> > 
> > ---
> >  drivers/hv/hv_util.c | 46 +++++++++++++++++++++++++++++++++-----------
> >  1 file changed, 35 insertions(+), 11 deletions(-)
> 
> Reviewed-by: Michael Kelley <mikelley@microsoft.com>

Applied to hyperv-fixes. Thanks.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-08-24 14:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-21 15:25 [PATCH v2] hv_utils: return error if host timesysnc update is stale Vineeth Pillai
2020-08-21 15:44 ` Michael Kelley
2020-08-24 14:51   ` Wei Liu

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).