[PATCH] ntp: Make sure RTC is synchronized for any time jump
From: Benjamin ROBIN
Date: Sun Oct 06 2024 - 16:35:24 EST
Follow-up of commit 35b603f8a78b ("ntp: Make sure RTC is synchronized
when time goes backwards").
sync_hw_clock() is normally called every 11 minutes when time is
synchronized. This issue is that this periodic timer uses the REALTIME
clock, so when time moves backwards, the timer expires late.
If the timer expires late, which can be days later, the RTC will no longer
be updated, which is an issue if the device is abruptly powered OFF during
this period. When the device will restart (when powered ON), it will have
the date prior to the time jump.
This follow-up handles all kernel API (syscall) that can trigger a time
jump. Cancel periodic timer on any time jump, if and only if STA_UNSYNC
flag was previously set (net_clear() was called).
The timer will be relaunched at the end of ntp_notify_cmos_timer() if
NTP is synced again somehow: This is possible since stopping the timer is
done outside of a locked section. Otherwise the timer will be relaunched
later when NTP is synced. This way, when the time is synchronized again,
the RTC is updated after less than 2 seconds.
Signed-off-by: Benjamin ROBIN <dev@xxxxxxxxxxxxx>
---
kernel/time/ntp.c | 10 +++++-----
kernel/time/ntp_internal.h | 4 ++--
kernel/time/timekeeping.c | 7 ++++---
3 files changed, 11 insertions(+), 10 deletions(-)
diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
index 802b336f4b8c..b296c71af09b 100644
--- a/kernel/time/ntp.c
+++ b/kernel/time/ntp.c
@@ -660,14 +660,14 @@ static void sync_hw_clock(struct work_struct *work)
sched_sync_hw_clock(offset_nsec, res != 0);
}
-void ntp_notify_cmos_timer(bool offset_set)
+void ntp_notify_cmos_timer(bool ntp_was_cleared)
{
/*
- * If the time jumped (using ADJ_SETOFFSET) cancels sync timer,
- * which may have been running if the time was synchronized
- * prior to the ADJ_SETOFFSET call.
+ * If time jumped (clock set), and if ntp_clear() was called,
+ * cancels sync timer, which may have been running if time was
+ * previously synchronized.
*/
- if (offset_set)
+ if (ntp_was_cleared)
hrtimer_cancel(&sync_hrtimer);
/*
diff --git a/kernel/time/ntp_internal.h b/kernel/time/ntp_internal.h
index 5a633dce9057..0615ed904119 100644
--- a/kernel/time/ntp_internal.h
+++ b/kernel/time/ntp_internal.h
@@ -14,9 +14,9 @@ extern int __do_adjtimex(struct __kernel_timex *txc,
extern void __hardpps(const struct timespec64 *phase_ts, const struct timespec64 *raw_ts);
#if defined(CONFIG_GENERIC_CMOS_UPDATE) || defined(CONFIG_RTC_SYSTOHC)
-extern void ntp_notify_cmos_timer(bool offset_set);
+extern void ntp_notify_cmos_timer(bool ntp_was_cleared);
#else
-static inline void ntp_notify_cmos_timer(bool offset_set) { }
+static inline void ntp_notify_cmos_timer(bool ntp_was_cleared) { }
#endif
#endif /* _LINUX_NTP_INTERNAL_H */
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 7e6f409bf311..602775aa24c7 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -1472,6 +1472,7 @@ int do_settimeofday64(const struct timespec64 *ts)
/* Signal hrtimers about time change */
clock_was_set(CLOCK_SET_WALL);
+ ntp_notify_cmos_timer(true);
if (!ret) {
audit_tk_injoffset(ts_delta);
@@ -1522,6 +1523,7 @@ static int timekeeping_inject_offset(const struct timespec64 *ts)
/* Signal hrtimers about time change */
clock_was_set(CLOCK_SET_WALL);
+ ntp_notify_cmos_timer(true);
return ret;
}
@@ -1897,6 +1899,7 @@ void timekeeping_inject_sleeptime64(const struct timespec64 *delta)
/* Signal hrtimers about time change */
clock_was_set(CLOCK_SET_WALL | CLOCK_SET_BOOT);
+ ntp_notify_cmos_timer(true);
}
#endif
@@ -2553,7 +2556,6 @@ int do_adjtimex(struct __kernel_timex *txc)
{
struct timekeeper *tk = &tk_core.timekeeper;
struct audit_ntp_data ad;
- bool offset_set = false;
bool clock_set = false;
struct timespec64 ts;
unsigned long flags;
@@ -2576,7 +2578,6 @@ int do_adjtimex(struct __kernel_timex *txc)
if (ret)
return ret;
- offset_set = delta.tv_sec != 0;
audit_tk_injoffset(delta);
}
@@ -2610,7 +2611,7 @@ int do_adjtimex(struct __kernel_timex *txc)
if (clock_set)
clock_was_set(CLOCK_SET_WALL);
- ntp_notify_cmos_timer(offset_set);
+ ntp_notify_cmos_timer(false);
return ret;
}
--
2.46.1