rtc_suspend() returns early when timekeeping_rtc_skipsuspend() is true, which is the case on every system with a persistent clock. old_rtc and old_system are then never written and stay zero. rtc_resume() is skipped under a different condition, timekeeping_rtc_skipresume(), which is only true once timekeeping_resume() has injected sleep time. timekeeping_resume() does that if a nonstop clocksource reports elapsed time or if the persistent clock is ahead of timekeeping_suspend_time. If neither holds, it injects nothing and rtc_resume() runs. With old_rtc and old_system being zero, rtc_resume() computes sleep_time = new_rtc - new_system and injects it when it is not negative. For an RTC kept in UTC this is about zero. For an RTC kept in local time east of UTC it is the UTC offset, so CLOCK_REALTIME and CLOCK_BOOTTIME jump ahead by that amount. This happens with suspend-to-idle on x86. The persistent clock is the CMOS RTC with a resolution of one second, and the TSC is only flagged CLOCK_SOURCE_SUSPEND_NONSTOP on CPUs with X86_FEATURE_NONSTOP_TSC_S3. timekeeping_suspend() and timekeeping_resume() are called for every pass through the s2idle loop, and when the last pass freezes timekeeping for less than a second, the persistent clock may not have advanced past timekeeping_suspend_time. Seen on a Lenovo ThinkBook 16p G5 (Core i9-14900HX, s2idle only) with the RTC in local time at UTC+3: on some resumes the system clock jumped three hours ahead and chronyd then slewed it back for more than a day. A kprobe on timekeeping_inject_sleeptime64() shows the call coming from rtc_resume() with a delta of 10798 seconds. The same jump has since been reported independently on a Lenovo ThinkPad T14s Gen 4 (UTC+1), a Samsung Galaxy Book4 (UTC+2) and an ASUS Vivobook K3402ZA (UTC+1), all of them s2idle only with the RTC in local time. The Samsung report includes pm_debug_messages output with a short last freeze followed by rtc_resume() injecting the two hour offset. Record in rtc_suspend() whether the snapshot was taken and return early from rtc_resume() when it was not. This also covers the case where rtc_suspend() failed to read the RTC. The device name check moves in front of timekeeping_rtc_skipsuspend() so that the flag is cleared on every suspend of the hctosys device. A freeze too short to be seen by the persistent clock is not accounted after this change. With the RTC in UTC the injected value used to be the sub-second difference between the RTC and the system time, not the time spent suspended. Tested on v7.3-rc5 with a script that shifts the system clock by less than a second relative to the RTC and then suspends to idle with a wake alarm a few seconds ahead, 30 times. Unpatched, 4 cycles jumped by the UTC offset, each of them with timekeeping frozen for less than 0.9 seconds. Patched, no cycle jumped although 6 of them met that condition. Fixes: 0fa88cb4b82b ("time, drivers/rtc: Don't bother with rtc_resume() for the nonstop clocksource") Link: https://bugzilla.redhat.com/show_bug.cgi?id=2543517 Link: https://lore.kernel.org/all/CAKUb7EBUeY=2zHTu5g1ZOoo0JAFiE_MAvTDNQta+LitQnVELoA@mail.gmail.com/ Tested-by: Florimond Husquinet Cc: stable@vger.kernel.org Signed-off-by: Alperen Kılıç --- Notes: v2: - Rebased onto v7.4-rc5, no code change. - Changelog mentions the independent reports from other laptops and links the Samsung one. - Thomas Gleixner added to To, as the bug sits between the RTC class and the timekeeping core. - Added the Tested-by from Florimond, who tested v1 on Fedora's 7.3.6 sources (unpatched 7 of 27 cycles jumped, patched 0 of 27): https://lore.kernel.org/all/20261203175428.7248-1-florimond@husquinet.net/ v1: https://lore.kernel.org/all/20261003163138.14221-1-sabri.alperen03@gmail.com/ The jump was found and measured on my own laptop. I used an LLM to help me read through the RTC and timekeeping resume code and to write the reproducer and this patch. I went through the change myself and ran all of the tests on my own machine. drivers/rtc/class.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c index 18ea141ab..d4550bae9 100644 --- a/drivers/rtc/class.c +++ b/drivers/rtc/class.c @@ -98,6 +98,8 @@ static void rtc_hctosys(struct rtc_device *rtc) */ static struct timespec64 old_rtc, old_system, old_delta; +/* Set when rtc_suspend() took the snapshot rtc_resume() relies on */ +static bool old_rtc_valid; static int rtc_suspend(struct device *dev) { @@ -106,10 +108,12 @@ static int rtc_suspend(struct device *dev) struct timespec64 delta, delta_delta; int err; - if (timekeeping_rtc_skipsuspend()) + if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) return 0; - if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) + old_rtc_valid = false; + + if (timekeeping_rtc_skipsuspend()) return 0; /* snapshot the current RTC and system time at suspend*/ @@ -121,6 +125,7 @@ static int rtc_suspend(struct device *dev) ktime_get_real_ts64(&old_system); old_rtc.tv_sec = rtc_tm_to_time64(&tm); + old_rtc_valid = true; /* * To avoid drift caused by repeated suspend/resumes, @@ -155,6 +160,18 @@ static int rtc_resume(struct device *dev) if (timekeeping_rtc_skipresume()) return 0; + /* + * rtc_suspend() takes no snapshot when the persistent clock is in + * charge of the sleep time or when the RTC could not be read. The + * timekeeping core still asks for sleep time if the persistent clock + * did not advance, which happens when the system was suspended for + * less than a second. Without a snapshot there is nothing to compute + * the sleep time from. Using the stale old_rtc and old_system would + * inject the offset between the RTC and system time. + */ + if (!old_rtc_valid) + return 0; + rtc_hctosys_ret = -ENODEV; if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) return 0; base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244 -- 2.56.1