diff --git a/drivers/rtc/rtc-macsmc.c b/drivers/rtc/rtc-macsmc.c index 690a4a84ff90e..c40c7e20d6a3b 100644 --- a/drivers/rtc/rtc-macsmc.c +++ b/drivers/rtc/rtc-macsmc.c @@ -444,6 +444,14 @@ 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"); + /* + * 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 = NSEC_PER_SEC / 2; + 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..579814b97b866 100644 --- a/kernel/time/ntp.c +++ b/kernel/time/ntp.c @@ -747,12 +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_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); + 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); }