2021-01-11 10:43:25

by Geert Uytterhoeven

[permalink] [raw]
Subject: [PATCH] ntp: Fix RTC synchronization on 32-bit platforms

Due to an integer overflow, RTC synchronization now happens every 2s
instead of the intended 11 minutes. Fix this by forcing 64-bit
arithmetic for the sync period calculation.

Fixes: c9e6189fb03123a7 ("ntp: Make the RTC synchronization more reliable")
Signed-off-by: Geert Uytterhoeven <[email protected]>
---
kernel/time/ntp.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
index 7404d38315276a96..54d52fab201d283e 100644
--- a/kernel/time/ntp.c
+++ b/kernel/time/ntp.c
@@ -498,7 +498,7 @@ int second_overflow(time64_t secs)
static void sync_hw_clock(struct work_struct *work);
static DECLARE_WORK(sync_work, sync_hw_clock);
static struct hrtimer sync_hrtimer;
-#define SYNC_PERIOD_NS (11UL * 60 * NSEC_PER_SEC)
+#define SYNC_PERIOD_NS (11ULL * 60 * NSEC_PER_SEC)

static enum hrtimer_restart sync_timer_callback(struct hrtimer *timer)
{
--
2.25.1


2021-01-11 10:49:47

by Geert Uytterhoeven

[permalink] [raw]
Subject: Re: [PATCH] ntp: Fix RTC synchronization on 32-bit platforms

On Mon, Jan 11, 2021 at 11:40 AM Geert Uytterhoeven
<[email protected]> wrote:
> Due to an integer overflow, RTC synchronization now happens every 2s
> instead of the intended 11 minutes. Fix this by forcing 64-bit
> arithmetic for the sync period calculation.
>
> Fixes: c9e6189fb03123a7 ("ntp: Make the RTC synchronization more reliable")
> Signed-off-by: Geert Uytterhoeven <[email protected]>
> ---
> kernel/time/ntp.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
> index 7404d38315276a96..54d52fab201d283e 100644
> --- a/kernel/time/ntp.c
> +++ b/kernel/time/ntp.c
> @@ -498,7 +498,7 @@ int second_overflow(time64_t secs)
> static void sync_hw_clock(struct work_struct *work);
> static DECLARE_WORK(sync_work, sync_hw_clock);
> static struct hrtimer sync_hrtimer;
> -#define SYNC_PERIOD_NS (11UL * 60 * NSEC_PER_SEC)
> +#define SYNC_PERIOD_NS (11ULL * 60 * NSEC_PER_SEC)
>
> static enum hrtimer_restart sync_timer_callback(struct hrtimer *timer)
> {

While the line

exp = ktime_add_ns(exp, 2 * NSEC_PER_SEC - offset_nsec);

is currently not an issue, it may be wise to change it to

exp = ktime_add_ns(exp, 2ULL * NSEC_PER_SEC - offset_nsec);

in case someone wants to experiment with values larger then 4s?

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

Subject: [tip: timers/urgent] ntp: Fix RTC synchronization on 32-bit platforms

The following commit has been merged into the timers/urgent branch of tip:

Commit-ID: e3fab2f3de081e98c50b7b4ace1b040161d95310
Gitweb: https://git.kernel.org/tip/e3fab2f3de081e98c50b7b4ace1b040161d95310
Author: Geert Uytterhoeven <[email protected]>
AuthorDate: Mon, 11 Jan 2021 11:39:56 +01:00
Committer: Thomas Gleixner <[email protected]>
CommitterDate: Tue, 12 Jan 2021 21:13:01 +01:00

ntp: Fix RTC synchronization on 32-bit platforms

Due to an integer overflow, RTC synchronization now happens every 2s
instead of the intended 11 minutes. Fix this by forcing 64-bit
arithmetic for the sync period calculation.

Annotate the other place which multiplies seconds for consistency as well.

Fixes: c9e6189fb03123a7 ("ntp: Make the RTC synchronization more reliable")
Signed-off-by: Geert Uytterhoeven <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>
Link: https://lore.kernel.org/r/[email protected]

---
kernel/time/ntp.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
index 7404d38..87389b9 100644
--- a/kernel/time/ntp.c
+++ b/kernel/time/ntp.c
@@ -498,7 +498,7 @@ out:
static void sync_hw_clock(struct work_struct *work);
static DECLARE_WORK(sync_work, sync_hw_clock);
static struct hrtimer sync_hrtimer;
-#define SYNC_PERIOD_NS (11UL * 60 * NSEC_PER_SEC)
+#define SYNC_PERIOD_NS (11ULL * 60 * NSEC_PER_SEC)

static enum hrtimer_restart sync_timer_callback(struct hrtimer *timer)
{
@@ -512,7 +512,7 @@ static void sched_sync_hw_clock(unsigned long offset_nsec, bool retry)
ktime_t exp = ktime_set(ktime_get_real_seconds(), 0);

if (retry)
- exp = ktime_add_ns(exp, 2 * NSEC_PER_SEC - offset_nsec);
+ exp = ktime_add_ns(exp, 2ULL * NSEC_PER_SEC - offset_nsec);
else
exp = ktime_add_ns(exp, SYNC_PERIOD_NS - offset_nsec);