public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Benjamin ROBIN <dev@benjarobin.fr>
To: jstultz@google.com, tglx@linutronix.de
Cc: sboyd@kernel.org, linux-kernel@vger.kernel.org,
	Benjamin ROBIN <dev@benjarobin.fr>
Subject: [PATCH] ntp: Make sure RTC is synchronized when time goes backwards
Date: Sat,  7 Sep 2024 21:09:00 +0200	[thread overview]
Message-ID: <20240907190900.55421-1-dev@benjarobin.fr> (raw)

The "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 next call to "sync_hw_clock" could be realized after a very long
period.

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

This patch cancels the periodic timer on a time jump (ADJ_SETOFFSET).
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>
---
 kernel/time/ntp.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
index 8d2dd214ec68..5c8dd92cf012 100644
--- a/kernel/time/ntp.c
+++ b/kernel/time/ntp.c
@@ -751,6 +751,9 @@ static inline void process_adjtimex_modes(const struct __kernel_timex *txc,
 
 	if (txc->modes & (ADJ_TICK|ADJ_FREQUENCY|ADJ_OFFSET))
 		ntp_update_frequency();
+
+	if (txc->modes & ADJ_SETOFFSET)
+		hrtimer_cancel(&sync_hrtimer);
 }
 
 
-- 
2.45.2


             reply	other threads:[~2024-09-07 19:26 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-07 19:09 Benjamin ROBIN [this message]
2024-09-07 21:42 ` [PATCH] ntp: Make sure RTC is synchronized when time goes backwards Thomas Gleixner
2024-09-08 11:45   ` Benjamin ROBIN
2024-09-08 11:45   ` [PATCH v2] " Benjamin ROBIN
2024-09-08 12:39 ` [PATCH] " kernel test robot
2024-09-08 12:39 ` kernel test robot
2024-09-08 14:08 ` [PATCH v3] " Benjamin ROBIN
2024-09-10 11:59   ` [tip: timers/core] " tip-bot2 for Benjamin ROBIN

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=20240907190900.55421-1-dev@benjarobin.fr \
    --to=dev@benjarobin.fr \
    --cc=jstultz@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sboyd@kernel.org \
    --cc=tglx@linutronix.de \
    /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