From a95794f8661bf2924773d52e39e002f9420fefdc Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Wed, 18 Nov 2026 04:21:51 -0600 Subject: [PATCH 1/2] rtc: macsmc: fix ntp sync_hw_clock hrtimer retry storm The RTC class default set_offset_nsec is one second plus five milliseconds, which makes rtc_tv_nsec_ok fail outside a narrow window. sync_hw_clock then re-arms its retry hrtimer at second + 2s - offset, which is already in the past, so the timer and workqueue spin at hundreds per second with no adjtimex calls (measured: 0 adjtimex vs ~488/s sync work in 5s). Set macsmc-rtc to the five millisecond transport offset the class documents, and never arm the sync hrtimer in the past when offset_nsec is mis-sized. --- drivers/rtc/rtc-macsmc.c | 9 +++++++++ kernel/time/ntp.c | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/drivers/rtc/rtc-macsmc.c b/drivers/rtc/rtc-macsmc.c index 690a4a84ff90e..5e8adb55ad9fd 100644 --- a/drivers/rtc/rtc-macsmc.c +++ b/drivers/rtc/rtc-macsmc.c @@ -444,6 +444,15 @@ static int macsmc_rtc_probe(struct platform_device *pdev) rtc->offset_in_seconds = of_property_read_bool(pdev->dev.of_node, "apple,rtc-offset-seconds"); + /* + * The class default is NSEC_PER_SEC + 5 ms. ntp sync_hw_clock passes that + * to rtc_tv_nsec_ok, so the write window is only at the second edge; on + * retry the hrtimer re-arms at second + 2 s - offset, which lands in the + * past when offset is about one second and spins at ~500 Hz with no + * userspace adjtimex. + */ + rtc->rtc_dev->set_offset_nsec = 5 * NSEC_PER_MSEC; + rtc->rtc_dev->ops = &macsmc_rtc_ops; ret = macsmc_rtc_init_alarm(pdev, rtc); if (ret == -EPROBE_DEFER) diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c index d22b532ec5363..f29d369c47bea 100644 --- a/kernel/time/ntp.c +++ b/kernel/time/ntp.c @@ -748,12 +748,16 @@ static enum hrtimer_restart sync_timer_callback(struct hrtimer *timer) static void sched_sync_hw_clock(unsigned long offset_nsec, bool retry) { ktime_t exp = ktime_set(ktime_get_real_seconds(), 0); + ktime_t now = ktime_get(); if (retry) exp = ktime_add_ns(exp, 2ULL * NSEC_PER_SEC - offset_nsec); else exp = ktime_add_ns(exp, SYNC_PERIOD_NS - offset_nsec); + if (ktime_compare(exp, now) <= 0) + exp = ktime_add_ns(now, NSEC_PER_MSEC); + hrtimer_start(&sync_hrtimer, exp, HRTIMER_MODE_ABS); } From 33958ff95c0847134bbecddf794e306951e7ec77 Mon Sep 17 00:00:00 2001 From: Joshua Warren <816217+joshuaswarren@users.noreply.github.com> Date: Wed, 18 Nov 2026 05:18:19 -0600 Subject: [PATCH 2/2] rtc/ntp: fix systohc hrtimer retry (wAX 2197) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit wAX: past-arm guard used ktime_get() (MONOTONIC) against CLOCK_REALTIME expiry — guard never fired. Compare and slip forward on ktime_get_real(); on retry roll to the next second slot instead of a 1 ms tight loop. wAX: 5 ms set_offset_nsec is wrong for macsmc free-running CLKM+nvmem. Drop transport offset; use NSEC_PER_SEC/2 systohc phase window like CMOS. M5 pre-fix (daily): adjtimex 1/5s (checkpoint), sync_hw_clock+timer 2158/5s (~432/s) — kernel sched_sync_hw_clock re-arm, not userspace adjtimex. --- drivers/rtc/rtc-macsmc.c | 11 +++++------ kernel/time/ntp.c | 24 +++++++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/drivers/rtc/rtc-macsmc.c b/drivers/rtc/rtc-macsmc.c index 5e8adb55ad9fd..c40c7e20d6a3b 100644 --- a/drivers/rtc/rtc-macsmc.c +++ b/drivers/rtc/rtc-macsmc.c @@ -445,13 +445,12 @@ static int macsmc_rtc_probe(struct platform_device *pdev) "apple,rtc-offset-seconds"); /* - * The class default is NSEC_PER_SEC + 5 ms. ntp sync_hw_clock passes that - * to rtc_tv_nsec_ok, so the write window is only at the second edge; on - * retry the hrtimer re-arms at second + 2 s - offset, which lands in the - * past when offset is about one second and spins at ~500 Hz with no - * userspace adjtimex. + * Free-running counter + nvmem offset, not a post-write tick-up RTC. + * The class default (1s + 5 ms) shrinks rtc_tv_nsec_ok to the edge fuzz + * and makes sync_hw_clock retry constantly. Use a half-second systohc + * phase window like CMOS, not a transport-time offset. */ - rtc->rtc_dev->set_offset_nsec = 5 * NSEC_PER_MSEC; + rtc->rtc_dev->set_offset_nsec = NSEC_PER_SEC / 2; rtc->rtc_dev->ops = &macsmc_rtc_ops; ret = macsmc_rtc_init_alarm(pdev, rtc); diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c index f29d369c47bea..579814b97b866 100644 --- a/kernel/time/ntp.c +++ b/kernel/time/ntp.c @@ -747,16 +747,26 @@ static enum hrtimer_restart sync_timer_callback(struct hrtimer *timer) static void sched_sync_hw_clock(unsigned long offset_nsec, bool retry) { - ktime_t exp = ktime_set(ktime_get_real_seconds(), 0); - ktime_t now = ktime_get(); + ktime_t now = ktime_get_real(); + ktime_t exp; if (retry) - exp = ktime_add_ns(exp, 2ULL * NSEC_PER_SEC - offset_nsec); + exp = ktime_add_ns(ktime_set(ktime_get_real_seconds(), 0), + 2ULL * NSEC_PER_SEC - offset_nsec); else - exp = ktime_add_ns(exp, SYNC_PERIOD_NS - offset_nsec); - - if (ktime_compare(exp, now) <= 0) - exp = ktime_add_ns(now, NSEC_PER_MSEC); + exp = ktime_add_ns(now, SYNC_PERIOD_NS - offset_nsec); + + /* sync_hrtimer is CLOCK_REALTIME; compare and arm in the same base. */ + if (ktime_compare(exp, now) <= 0) { + if (retry) { + exp = ktime_add_ns(ktime_set(ktime_get_real_seconds() + 1, 0), + 2ULL * NSEC_PER_SEC - offset_nsec); + if (ktime_compare(exp, now) <= 0) + exp = ktime_add_ns(now, NSEC_PER_MSEC); + } else { + exp = ktime_add_ns(now, NSEC_PER_MSEC); + } + } hrtimer_start(&sync_hrtimer, exp, HRTIMER_MODE_ABS); }