]> git.dujemihanovic.xyz Git - linux.git/commitdiff
ntp: Make sure RTC is synchronized when time goes backwards
authorBenjamin ROBIN <dev@benjarobin.fr>
Sun, 8 Sep 2024 14:08:36 +0000 (16:08 +0200)
committerThomas Gleixner <tglx@linutronix.de>
Tue, 10 Sep 2024 11:50:40 +0000 (13:50 +0200)
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 NTP server jumps into the past),
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 ADJ_SETOFFSET call.

A normal NTP server should not jump in the past like that, but it is
possible... Another way of reproducing this issue is to use phc2sys to
synchronize the REALTIME clock with, for example, an IRIG timecode with
the source always starting at the same date (not synchronized).

Also, if the time jump in the future by less than 11 minutes, the RTC may
not be updated immediately (minor issue). Consider the following scenario:
 - Time is synchronized, and sync_hw_clock() was just called (the timer
   expires in 11 minutes).
 - A time jump is realized in the future by a couple of minutes.
 - The time is synchronized again.
 - Users may expect that RTC to be updated as soon as possible, and not
   after 11 minutes (for the same reason, if a power loss occurs in this
   period).

Cancel periodic timer on any time jump (ADJ_SETOFFSET) greater than or
equal to 1s. The timer will be relaunched at the end of do_adjtimex() if
NTP is still considered synced. 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@benjarobin.fr>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/all/20240908140836.203911-1-dev@benjarobin.fr
kernel/time/ntp.c
kernel/time/ntp_internal.h
kernel/time/timekeeping.c

index 8d2dd214ec68227814a880cd5e1f73c6700c0d4e..802b336f4b8c2f6e92b9b4c765182efaa0574efd 100644 (file)
@@ -660,8 +660,16 @@ rearm:
        sched_sync_hw_clock(offset_nsec, res != 0);
 }
 
-void ntp_notify_cmos_timer(void)
+void ntp_notify_cmos_timer(bool offset_set)
 {
+       /*
+        * 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 (offset_set)
+               hrtimer_cancel(&sync_hrtimer);
+
        /*
         * When the work is currently executed but has not yet the timer
         * rearmed this queues the work immediately again. No big issue,
index 23d1b74c30657002760ed59f3b4a1cf01a14097f..5a633dce9057aa866878a2b51c1f86a766140117 100644 (file)
@@ -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(void);
+extern void ntp_notify_cmos_timer(bool offset_set);
 #else
-static inline void ntp_notify_cmos_timer(void) { }
+static inline void ntp_notify_cmos_timer(bool offset_set) { }
 #endif
 
 #endif /* _LINUX_NTP_INTERNAL_H */
index 5391e4167d60226dfc48c845170e36bcbeb7b292..7e6f409bf3114af3ef237ef2b48760a73aba7b43 100644 (file)
@@ -2553,6 +2553,7 @@ 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;
@@ -2575,6 +2576,7 @@ int do_adjtimex(struct __kernel_timex *txc)
                if (ret)
                        return ret;
 
+               offset_set = delta.tv_sec != 0;
                audit_tk_injoffset(delta);
        }
 
@@ -2608,7 +2610,7 @@ int do_adjtimex(struct __kernel_timex *txc)
        if (clock_set)
                clock_was_set(CLOCK_SET_WALL);
 
-       ntp_notify_cmos_timer();
+       ntp_notify_cmos_timer(offset_set);
 
        return ret;
 }