From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.46 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796323574; b=QqnVlGK6ufF1MrDX5zv7RaxhInABGFc1GDj6af+EN1bfqgzngho11j1U5qBmARtcU/y1 lQVM0K0J65CSUIJYNdQJxSwk4PKmD3y290sOzSE4NadEm4xOGXMy7G6EA2jdK1SP2kybZ NKDa6QQwBMXme/fl6AtsWeE/acibndLojNpwIeV3LdTHtzGOP2Ec51J7K4qTdrZS2lxB9 Q5VfIZY/T9duewLE6rDfmFpZ3ixJnnEF2v1KzlllaX3hhc4IjnccsxjKwHreTtzKpFUhE reSYvuGIi2VtteFNdGAkhH/9b7AYMNwd3w6gTuZQsXU0mrpXSk8BY3VDX6fDcgyRrng== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796323574; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=sM0KvXi2EpTqxxS+zbYevIm1ETJLbxJtzme16oiWGNI=; b=wY3oWNfhMcDxDlXgJCQmPHxlGlaFihIbkoK8zDpmo63qMIBRBvZm94RRVAfnhVma7LVX aGxbEJpdJOxbtiQ2PSmGBJCv+F9bm1cJkuVwrBlDBpMRvvl3EzihvwmoKM1Hi2gyFjj3B UQFvqSfWAJDwyMB56zgGAZFGBrg/1SSRwvPPdC9Ae7mFpDoD6cvb0ADIvL+zsF9Z52Uoo Kn+UsfCBNlT02rfOsCTnPtfttmU3qygwx2hNslLe01CcBVswMblni7Y8tJSosjhEv4HNB SMUsyShZImSVec97HobSt1v2IJcE0EkHXwNVQGfLJmTl4KkM7FKg7nZQbwb4rlu0Ymg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=UFzPaBlU; arc=none smtp.remote-ip=209.85.128.46 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 53B82606A32D for ; Thu, 3 Dec 2026 18:46:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 53B82606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=UFzPaBlU Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-4bba5e17cbeso1066605e9.1 for ; Thu, 03 Dec 2026 10:46:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796323572; x=1796928372; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:message-id:date :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=sM0KvXi2EpTqxxS+zbYevIm1ETJLbxJtzme16oiWGNI=; b=UFzPaBlUeZKY68+8jcRfD29Ev3RFOtYy4X7GDTKUp8U41Y9HwwA/Sxr5+6rHw4vmXI A03+uWS0I8mDv/KpKp29JCFy2Jh15xIhghZzUGFEjvK7qMHUa5yBUGy1A3bM21Uol8TQ mhF9XvpE9AsxCtBHB/4GjOaUBdoAWr0S7mvZpzt5aGgBTvrjMf2VRWUQivtlhB+gDfOz g81VBZT9zTpLJYbfFfRNS4fsDDXvII0LQNYXxxDIfy6IuXFpbqnDwsC6MnshDtSq0sAs rC/eJKKj+pEhFsqspgUXeTQTv4v/36yo/TptzLY3orof/Ni/FDGbn2M4RTyJPNFeKOzE 07Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796323572; x=1796928372; h=content-transfer-encoding:content-type:mime-version:message-id:date :subject:cc:to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to:content-type; bh=sM0KvXi2EpTqxxS+zbYevIm1ETJLbxJtzme16oiWGNI=; b=ii+nucZ9McDAB7fsrlsCRRz7KE8YUV/UKSvsiD1RXqqibNUKrn5acfZKLiEWgTepWu GfikMbNy8EF65sPS5To2KIsoD+WIN6BYf7EdNxeOdCAOOhFjD0daMYIo9vgw1gezj4x7 JgXbT+Yp5qLt+M0EQ/LjL6d4ih8xu2hyCttyQedskzw0g+2osnrK+TeNDBcRkcLe88bu 5GMKLENnngc2BR0mNzNKh8serHaVmvLuFEPn2p3hkWV6H3VBuR81wftNk3PieP44LAU5 7zRuqZh+u0gTjm2FS+Bavm1eZpXt19iSKBjmww4UnD2lnyg3onSTivS6/hW4oCYx4yoJ ynhQ== X-Forwarded-Encrypted: i=1; AL+hErHI+zd+iNKzGkhRIxvtWCte0Kw6Csu2Gqgynvcbzi3ZFg4/Bx9QMJuWpkN4TdLMy288jpKpWqItue5Bsa8=@vger.kernel.org X-Gm-Message-State: APuU4Z+36/tkr1+tm5oEJKbYgVy3J9eO82TcmlMalGh9adjC4RnSugua 7Jc/CgxKv+dgcVE7VdxlkQcoghl0vtlPHNEajvCyz7ix9tsKJQK4ENnw X-Gm-Gg: AZFdTZfExJ8iTASP8GnaAb8eNWPflXkL2v/y+/ppcPQiHRSjF1hyil0kveT660CIyjt E+EYy/9OcnGP7xr/9E4pfnvh/bbux9ojAS8PMgXV0TiW/RwwZnTxJ58Zzr6JFK+GxipoHczHUYK I1VrsIO3MaadtQ1gPFoXczG+pxOJwdu1vQA+ZQM9yvHNpx2E7Wjy60jgfff/1AmoikkrzBtPjsO EE3aAcxxXVZ/se6QdxESZ2pPsQw/TPseiwvsknCxT6I6EfBdERxGPP+VSlvpjqMgn2TplFgGPqz flMJ7PU3qk+4pd5EMS+g5IWWDACdjwdY+7blX6cXgfu27dpnAvfKkfEsgdWjeJVUg0O/lHZNF/w pPVeRFuM/AY+83ASvPYAS6eCbHD+Qeb8TvjQ1UyKcu12WekErSsOKltRQuS5vD0HM5rHJpuKoTS WzKmDfu40RiavcRc2Hc7DeoR1CK8wZa+eweEYzlpdvDxKjFJe6HFgRIzKMLFzScVKwT42WLszRQ H2/wlg45jEPfP/SEtcRNwR1mKh6kguFXj/G3deMmOCCdrGuKz6BOAVu7438aJuga4jzDYbkGUPB AuQ+fHpKEmyUaJ2pqdklU7t8f9v0BKzCjAduQJr/Aw== X-Received: by 2002:a05:600c:4fc7:b0:4bb:e8b5:7c8e with SMTP id 5b1f17b1804b1-4bbe8b57cbfmr9384685e9.35.1796323571221; Thu, 03 Dec 2026 10:46:11 -0800 (PST) Received: from fedora.emu.edu.tr ([194.27.73.86]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb906e4ce6sm798225025e9.4.2026.12.03.10.46.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 10:46:10 -0800 (PST) From: =?UTF-8?q?Alperen=20K=C4=B1l=C4=B1=C3=A7?= To: Alexandre Belloni , Thomas Gleixner Cc: linux-rtc@vger.kernel.org, John Stultz , Stephen Boyd , Miroslav Lichvar , linux-kernel@vger.kernel.org, Ivaylo Truhchev , Florimond Husquinet , =?UTF-8?q?Alperen=20K=C4=B1l=C4=B1=C3=A7?= , stable@vger.kernel.org Subject: [PATCH v2] rtc: class: Do not inject sleep time without a suspend snapshot Date: Thu, 3 Dec 2026 20:45:49 +0200 Message-ID: <20261203184549.32164-1-sabri.alperen03@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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