Skip to contentSkip to conversation

leapsec01: Keep STA_UNSYNC set to avoid corrupting the RTC

PATCH New Wake Liu via ltp 1 participant 1 message Nov 16

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 leapsec01: Keep STA_UNSYNC set to avoid corrupting the RTC

The test sets CLOCK_REALTIME to two seconds before the next UTC midnight and then calls adjtimex(ADJ_STATUS) with STA_PLL, 0 and STA_INS. None of these include STA_UNSYNC, so the kernel treats the clock as synchronized by NTP and its "11 minute mode" (sync_hw_clock(), CONFIG_RTC_SYSTOHC=y by default) copies the system time into the RTC shortly afterwards. The RTC thus ends up with the fake time around 23:59:59.

The test restores CLOCK_REALTIME at the end, but nothing restores the RTC, which is left up to a day ahead of the system time. Tests that run later and compare the RTC with the system time, such as sys_rtc01, fail, and the machine boots with a wrong time until the clock is synced again.

Set STA_UNSYNC in every ADJ_STATUS call. The kernel inserts the leap second regardless of STA_UNSYNC, as second_overflow() only checks STA_INS, so the test still exercises the hrtimer bug. adjtimex() now returns TIME_ERROR instead of the leap second state, which only changes the informational messages printed by the test.

---
Tested on QEMU (x86_64, 6.12 kernel with CONFIG_RTC_SYSTOHC=y) by running leapsec01 followed by sys_rtc01:

without the patch: rtc0 - system time = 75612s, sys_rtc01 TFAIL
with the patch: rtc0 - system time = 2s, sys_rtc01 TPASS, and the
kernel still logs "Clock: inserting leap second 23:59:60 UTC"

The remaining 2s come from the existing CLOCK_REALTIME restore in run_leapsec(), which accounts neither for the time spent before the clock is set nor for the inserted leap second.

We hit this on Android devices, where leapsec01 runs about ten minutes before sys_rtc01.

 testcases/kernel/syscalls/clock_gettime/leapsec01.c | 9 +++++++++
 1 file changed, 9 insertions(+)
testcases/kernel/syscalls/clock_gettime/leapsec01.c +9 -0
index a71e2adc6..2d175468b 100644
@@ -79,6 +79,15 @@ static void adjtimex_status(struct timex *tx, int status)
 	int ret;
 	struct timespec now;
 
+	/*
+	 * Keep STA_UNSYNC set, otherwise the kernel considers the clock
+	 * synchronized and copies the system time, i.e. the fake time set by
+	 * this test, into the RTC (CONFIG_RTC_SYSTOHC). Nothing restores the
+	 * RTC afterwards. The leap second is inserted regardless of
+	 * STA_UNSYNC.
+	 */
+	status |= STA_UNSYNC;
+
 	tx->modes = ADJ_STATUS;
 	tx->status = status;
 	ret = adjtimex(tx);
Signature · 5 lines
2.56.0.611.g1030f0b940-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp