Skip to contentSkip to conversation

sys_rtc01: Report TCONF on system time mismatch when STA_UNSYNC is set

PATCH In review Wake Liu via ltp 3 participants 6 messages Nov 17 – Nov 19

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sys_rtc01: Report TCONF on system time mismatch when STA_UNSYNC is set

For the primary RTC (where hctosys == 1, or rtc0 as fallback), sys_rtc01 asserts that since_epoch is within slack of the current system time(2).

When a system boots with an RTC that is off the current time and userspace later steps CLOCK_REALTIME via clock_settime(2) or settimeofday(2) (for example via date(1)) without updating /dev/rtcN, do_settimeofday64() sets STA_UNSYNC in the kernel NTP status. While STA_UNSYNC is set, the kernel's CONFIG_RTC_SYSTOHC (sync_hw_clock()) does not write CLOCK_REALTIME back to the RTC, yet /sys/class/rtc/rtc0 still reports hctosys == 1 from boot. As noted in the test comment, since_epoch and system time legitimately diverge in this state.

Query adjtimex(2) when since_epoch differs from system time by more than slack and report TCONF instead of TFAIL if STA_UNSYNC is set.

Fixes 79d62f4cf62f ("testcases: sysfs: Add sys_rtc01")
---
 testcases/kernel/sysfs/class/rtc/sys_rtc01.c | 25 +++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)
testcases/kernel/sysfs/class/rtc/sys_rtc01.c +22 -3
index 89141f2fa..0e1c69f95 100644
@@ -43,9 +43,12 @@
  * test performs, so on a long-running system a purely fixed slack would
  * eventually produce false failures even on perfectly healthy hardware.
  *
- * The check may still fail if the system was booted with RTC that is fairly
- * off the current time and the system was later synced with NTP. This is out
- * of scope of this test.
+ * If the system was booted with an RTC that was off the current time and the
+ * system clock was later stepped in userspace via :manpage:`clock_settime(2)`
+ * or :manpage:`settimeofday(2)` without updating the RTC, the kernel sets
+ * STA_UNSYNC and CONFIG_RTC_SYSTOHC does not write the system clock back to
+ * the RTC while hctosys remains 1. Report TCONF instead of TFAIL when
+ * since_epoch diverges from system time while STA_UNSYNC is set.
  *
  * Under virtualization the slack is additionally multiplied, since a VM's
  * clock (and thus its emulated RTC) commonly lags real time by tens of
@@ -61,6 +64,7 @@
 #include <limits.h>
 #include <time.h>
 #include <sys/sysinfo.h>
+#include <sys/timex.h>
 #include "tst_test.h"
 #include "tst_sysfs_assert.h"
 #include "tst_path_defs.h"
@@ -108,6 +112,16 @@ static long read_hctosys(const char *name)
 	return TST_SYSFS_READ_LI(PATH_CLASS_RTC "/%s/hctosys", name);
 }
 
+static int is_clock_unsynced(void)
+{
+	struct timex tx = { .modes = 0 };
+
+	if (adjtimex(&tx) == -1)
+		tst_brk(TBROK | TERRNO, "adjtimex() failed");
+
+	return !!(tx.status & STA_UNSYNC);
+}
+
 static void read_rtc_snapshot(const char *name, char *date, size_t date_size,
 			      char *rtc_time, size_t time_size,
 			      time_t *since_epoch)
@@ -203,6 +217,11 @@ static void check_rtc(const char *name, int any_hctosys)
 			"%s: since_epoch (%lld), system time (%lld), diff %llds <= slack %lds",
 			name, (long long)since_epoch, (long long)sys_epoch,
 			diff, slack);
+	} else if (is_clock_unsynced()) {
+		tst_res(TCONF,
+			"%s: since_epoch (%lld), system time (%lld), diff %llds > slack %lds (STA_UNSYNC is set)",
+			name, (long long)since_epoch, (long long)sys_epoch,
+			diff, slack);
 	} else {
 		tst_res(TFAIL,
 			"%s: since_epoch (%lld), system time (%lld), diff %llds > slack %lds",
Signature · 5 lines
2.56.0.679.gc527c65306-goog


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi Wake,

testcases/kernel/sysfs/class/rtc/sys_rtc01.c static void check_rtc(const char *name, int any_hctosys) · line 224

Quoted 7 lines · Wake Liu

sys_rtc01: Report TCONF on system time mismatch when STA_UNSYNC is set

testcases/kernel/sysfs/class/rtc/sys_rtc01.c +5 -0
+	} else if (is_clock_unsynced()) {
+		tst_res(TCONF,
+			"%s: since_epoch (%lld), system time (%lld), diff %llds > slack %lds (STA_UNSYNC is set)",
+			name, (long long)since_epoch, (long long)sys_epoch,
+			diff, slack);

STA_UNSYNC is already set at boot by ntp_init() and rtc_hctosys() (via do_settimeofday64() -> ntp_clear()), and it is only ever cleared when a userspace NTP daemon calls adjtimex(2) with ADJ_STATUS. On any system without an active NTP daemon, is_clock_unsynced() always returns 1 even if the clock was never stepped after boot, making the TFAIL branch unreachable and masking real RTC vs system time mismatches. Conversely, when an NTP daemon is active and clears STA_UNSYNC after syncing a system booted with a wrong RTC, the RTC can still diverge if CONFIG_RTC_SYSTOHC is disabled or unsupported by the driver.

Verdict - Needs revision

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Hi!

Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+	} else if (is_clock_unsynced()) {
+		tst_res(TCONF,
+			"%s: since_epoch (%lld), system time (%lld), diff %llds > slack %lds (STA_UNSYNC is set)",
+			name, (long long)since_epoch, (long long)sys_epoch,
+			diff, slack);

STA_UNSYNC is already set at boot by ntp_init() and rtc_hctosys() (via

Quoted 8 lines

do_settimeofday64() -> ntp_clear()), and it is only ever cleared when a userspace NTP daemon calls adjtimex(2) with ADJ_STATUS. On any system without an active NTP daemon, is_clock_unsynced() always returns 1 even if the clock was never stepped after boot, making the TFAIL branch unreachable and masking real RTC vs system time mismatches. Conversely, when an NTP daemon is active and clears STA_UNSYNC after syncing a system booted with a wrong RTC, the RTC can still diverge if CONFIG_RTC_SYSTOHC is disabled or unsupported by the driver.

Hmm, I guess that the logic should be the other way, if STA_UNSYNC is not set the clock was modified by NTP and the test should be skipped:

testcases/kernel/sysfs/class/rtc/sys_rtc01.c +9 -6
index 0e1c69f95..a4ad37088 100644
@@ -47,8 +47,11 @@
  * system clock was later stepped in userspace via :manpage:`clock_settime(2)`
  * or :manpage:`settimeofday(2)` without updating the RTC, the kernel sets
  * STA_UNSYNC and CONFIG_RTC_SYSTOHC does not write the system clock back to
- * the RTC while hctosys remains 1. Report TCONF instead of TFAIL when
- * since_epoch diverges from system time while STA_UNSYNC is set.
+ * the RTC while hctosys remains 1.
+ *
+ * If system time was corrected by NTP the kernel clears STA_UNSYNC flag.
+ * Report TCONF instead of TFAIL when since_epoch diverges from system time
+ * when STA_UNSYNC is not set.
  *
  * Under virtualization the slack is additionally multiplied, since a VM's
  * clock (and thus its emulated RTC) commonly lags real time by tens of
@@ -112,14 +115,14 @@ static long read_hctosys(const char *name)
        return TST_SYSFS_READ_LI(PATH_CLASS_RTC "/%s/hctosys", name);
 }
 
-static int is_clock_unsynced(void)
+static int clock_is_synced(void)
 {
        struct timex tx = { .modes = 0 };
 
        if (adjtimex(&tx) == -1)
                tst_brk(TBROK | TERRNO, "adjtimex() failed");
 
-       return !!(tx.status & STA_UNSYNC);
+       return !(tx.status & STA_UNSYNC);
 }
 
 static void read_rtc_snapshot(const char *name, char *date, size_t date_size,
@@ -217,9 +220,9 @@ static void check_rtc(const char *name, int any_hctosys)
                        "%s: since_epoch (%lld), system time (%lld), diff %llds <= slack %lds",
                        name, (long long)since_epoch, (long long)sys_epoch,
                        diff, slack);
-       } else if (is_clock_unsynced()) {
+       } else if (clock_is_synced()) {
                tst_res(TCONF,
-                       "%s: since_epoch (%lld), system time (%lld), diff %llds > slack %lds (STA_UNSYNC is set)",
+                       "%s: since_epoch (%lld), system time (%lld), diff %llds > slack %lds (STA_UNSYNC is not set)",
                        name, (long long)since_epoch, (long long)sys_epoch,
                        diff, slack);
        } else {
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Hi Cyril,

Quoted 2 lines

Hmm, I guess that the logic should be the other way, if STA_UNSYNC is not set the clock was modified by NTP and the test should be skipped:

I think the logic in 3d11e5dd622e as applied is the right way around; inverting it would bring back the failure this patch fixes.

In kernel/time/ntp.c, the kernel writes the system time back to the RTC only while STA_UNSYNC is clear: sync_hw_clock() returns early if !ntp_synced(), and do_adjtimex() queues sync_work via ntp_notify_cmos_timer() once an NTP daemon clears STA_UNSYNC. With CONFIG_RTC_SYSTOHC=y on a writable RTC, the NTP-corrected time is written to the RTC within a few seconds and then every 11 minutes, so since_epoch is expected to match system time. (That is also how leapsec01 ended up writing its fake 23:59:58 into rtc0 when it cleared STA_UNSYNC [1].)

Conversely, STA_UNSYNC set is the state where nothing keeps the RTC in
sync: it is set at boot, and every clock_settime(), settimeofday(), or ADJ_SETOFFSET sets it again via ntp_clear(). On Android test devices, for example, the harness sets the clock with date(1) after boot and no NTP daemon clears STA_UNSYNC, so rtc0 stays at its boot-time offset ("diff 24441s > slack 10s"). chronyd without rtcsync behaves the same
way: it disciplines the system clock while leaving STA_UNSYNC set. With the inverted check, all of these would hit TFAIL again, while systems actively syncing the RTC via SYSTOHC would report TCONF.

The reviewer is right on two limitations:
1. When STA_UNSYNC is set, userspace cannot tell a broken RTC apart from a system clock stepped after boot, though the since_epoch vs date/time check still runs on all systems.
2. When STA_UNSYNC is clear, the RTC can still diverge if CONFIG_RTC_SYSTOHC is disabled (or points to a different RTC), or if the RTC driver is read-only / does not support set_time.

Do you think it is worth sending a follow-up to also check CONFIG_RTC_SYSTOHC / CONFIG_RTC_SYSTOHC_DEVICE (when kernel config is available), or would you prefer to keep 3d11e5dd622e as-is and just note the read-only / non-SYSTOHC case in the comment?

[1] https://lore.kernel.org/ltp/[email protected]/

Best regards,
Wake

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Hi!

Quoted 5 lines
Quoted 2 lines

Hmm, I guess that the logic should be the other way, if STA_UNSYNC is not set the clock was modified by NTP and the test should be skipped:

I think the logic in 3d11e5dd622e as applied is the right way around; inverting it would bring back the failure this patch fixes.

I was thinking about this yesterday and I think that we should redesign the test to make it simpler and more robust. I will send a patch that changes the test so that it samples rtc and monotonic timer for a while and checks that they increase in the same steps. That removes the absolute offset and the test stops being dependend on time being in sync.

Signature · 5 lines
Cyril Hrubis
[email protected]

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