2023-11-27 21:35:43

by pmartincic

[permalink] [raw]
Subject: [PATCH v2] hv_utils: Allow implicit ICTIMESYNCFLAG_SYNC

From: Peter Martincic <[email protected]>

Hyper-V hosts can omit the _SYNC flag to due a bug on resume from modern
suspend. In such a case, the guest may fail to update its time-of-day to
account for the period when it was suspended, and could proceed with a
significantly wrong time-of-day. In such a case when the guest is
significantly behind, fix it by treating a _SAMPLE the same as if _SYNC
was received so that the guest time-of-day is updated.

This is hidden behind param hv_utils.timesync_implicit.

Signed-off-by: Peter Martincic <[email protected]>
---
drivers/hv/hv_util.c | 31 ++++++++++++++++++++++++++++++-
1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/drivers/hv/hv_util.c b/drivers/hv/hv_util.c
index 42aec2c5606a..9c97c4065fe7 100644
--- a/drivers/hv/hv_util.c
+++ b/drivers/hv/hv_util.c
@@ -296,6 +296,11 @@ static struct {
spinlock_t lock;
} host_ts;

+static bool timesync_implicit;
+
+module_param(timesync_implicit, bool, 0644);
+MODULE_PARM_DESC(timesync_implicit, "If set treat SAMPLE as SYNC when clock is behind");
+
static inline u64 reftime_to_ns(u64 reftime)
{
return (reftime - WLTIMEDELTA) * 100;
@@ -344,6 +349,29 @@ static void hv_set_host_time(struct work_struct *work)
do_settimeofday64(&ts);
}

+/*
+ * Due to a bug on Hyper-V hosts, the sync flag may not always be sent on resume.
+ * Force a sync if the guest is behind.
+ */
+static inline bool hv_implicit_sync(u64 host_time)
+{
+ struct timespec64 new_ts;
+ struct timespec64 threshold_ts;
+
+ new_ts = ns_to_timespec64(reftime_to_ns(host_time));
+ ktime_get_real_ts64(&threshold_ts);
+
+ threshold_ts.tv_sec += 5;
+
+ /*
+ * If guest behind the host by 5 or more seconds.
+ */
+ if (timespec64_compare(&new_ts, &threshold_ts) >= 0)
+ return true;
+
+ return false;
+}
+
/*
* Synchronize time with host after reboot, restore, etc.
*
@@ -384,7 +412,8 @@ static inline void adj_guesttime(u64 hosttime, u64 reftime, u8 adj_flags)
spin_unlock_irqrestore(&host_ts.lock, flags);

/* Schedule work to do do_settimeofday64() */
- if (adj_flags & ICTIMESYNCFLAG_SYNC)
+ if ((adj_flags & ICTIMESYNCFLAG_SYNC) ||
+ (timesync_implicit && hv_implicit_sync(host_ts.host_time)))
schedule_work(&adj_time_work);
}

--
2.34.1


2023-11-30 19:17:48

by Boqun Feng

[permalink] [raw]
Subject: Re: [PATCH v2] hv_utils: Allow implicit ICTIMESYNCFLAG_SYNC

On Mon, Nov 27, 2023 at 01:35:24PM -0800, [email protected] wrote:
> From: Peter Martincic <[email protected]>
>
> Hyper-V hosts can omit the _SYNC flag to due a bug on resume from modern
> suspend. In such a case, the guest may fail to update its time-of-day to
> account for the period when it was suspended, and could proceed with a
> significantly wrong time-of-day. In such a case when the guest is
> significantly behind, fix it by treating a _SAMPLE the same as if _SYNC
> was received so that the guest time-of-day is updated.
>
> This is hidden behind param hv_utils.timesync_implicit.
>
> Signed-off-by: Peter Martincic <[email protected]>

Looks good to me.

Acked-by: Boqun Feng <[email protected]>

Regards,
Boqun

> ---
> drivers/hv/hv_util.c | 31 ++++++++++++++++++++++++++++++-
> 1 file changed, 30 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/hv/hv_util.c b/drivers/hv/hv_util.c
> index 42aec2c5606a..9c97c4065fe7 100644
> --- a/drivers/hv/hv_util.c
> +++ b/drivers/hv/hv_util.c
> @@ -296,6 +296,11 @@ static struct {
> spinlock_t lock;
> } host_ts;
>
> +static bool timesync_implicit;
> +
> +module_param(timesync_implicit, bool, 0644);
> +MODULE_PARM_DESC(timesync_implicit, "If set treat SAMPLE as SYNC when clock is behind");
> +
> static inline u64 reftime_to_ns(u64 reftime)
> {
> return (reftime - WLTIMEDELTA) * 100;
> @@ -344,6 +349,29 @@ static void hv_set_host_time(struct work_struct *work)
> do_settimeofday64(&ts);
> }
>
> +/*
> + * Due to a bug on Hyper-V hosts, the sync flag may not always be sent on resume.
> + * Force a sync if the guest is behind.
> + */
> +static inline bool hv_implicit_sync(u64 host_time)
> +{
> + struct timespec64 new_ts;
> + struct timespec64 threshold_ts;
> +
> + new_ts = ns_to_timespec64(reftime_to_ns(host_time));
> + ktime_get_real_ts64(&threshold_ts);
> +
> + threshold_ts.tv_sec += 5;
> +
> + /*
> + * If guest behind the host by 5 or more seconds.
> + */
> + if (timespec64_compare(&new_ts, &threshold_ts) >= 0)
> + return true;
> +
> + return false;
> +}
> +
> /*
> * Synchronize time with host after reboot, restore, etc.
> *
> @@ -384,7 +412,8 @@ static inline void adj_guesttime(u64 hosttime, u64 reftime, u8 adj_flags)
> spin_unlock_irqrestore(&host_ts.lock, flags);
>
> /* Schedule work to do do_settimeofday64() */
> - if (adj_flags & ICTIMESYNCFLAG_SYNC)
> + if ((adj_flags & ICTIMESYNCFLAG_SYNC) ||
> + (timesync_implicit && hv_implicit_sync(host_ts.host_time)))
> schedule_work(&adj_time_work);
> }
>
> --
> 2.34.1
>

2023-12-04 05:52:09

by Wei Liu

[permalink] [raw]
Subject: Re: [PATCH v2] hv_utils: Allow implicit ICTIMESYNCFLAG_SYNC

On Thu, Nov 30, 2023 at 11:16:43AM -0800, Boqun Feng wrote:
> On Mon, Nov 27, 2023 at 01:35:24PM -0800, [email protected] wrote:
> > From: Peter Martincic <[email protected]>
> >
> > Hyper-V hosts can omit the _SYNC flag to due a bug on resume from modern
> > suspend. In such a case, the guest may fail to update its time-of-day to
> > account for the period when it was suspended, and could proceed with a
> > significantly wrong time-of-day. In such a case when the guest is
> > significantly behind, fix it by treating a _SAMPLE the same as if _SYNC
> > was received so that the guest time-of-day is updated.
> >
> > This is hidden behind param hv_utils.timesync_implicit.
> >
> > Signed-off-by: Peter Martincic <[email protected]>
>
> Looks good to me.
>
> Acked-by: Boqun Feng <[email protected]>

Applied to hyperv-fixes, thanks!