[LTP] [PATCH] sys_rtc01: Report TCONF on system time mismatch when STA_UNSYNC is set
Wake Liu
wakel@google.com
Wed Nov 18 00:58:48 CET 2026
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")
Signed-off-by: Wake Liu <wakel@google.com>
---
testcases/kernel/sysfs/class/rtc/sys_rtc01.c | 25 +++++++++++++++++---
1 file changed, 22 insertions(+), 3 deletions(-)
diff --git a/testcases/kernel/sysfs/class/rtc/sys_rtc01.c b/testcases/kernel/sysfs/class/rtc/sys_rtc01.c
index 89141f2fa..0e1c69f95 100644
--- a/testcases/kernel/sysfs/class/rtc/sys_rtc01.c
+++ b/testcases/kernel/sysfs/class/rtc/sys_rtc01.c
@@ -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",
--
2.56.0.679.gc527c65306-goog
More information about the ltp
mailing list