From: Wake Liu via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it
Cc: Wake Liu <wakel@google.com>
Subject: [LTP] [PATCH] sys_rtc01: Report TCONF on system time mismatch when STA_UNSYNC is set
Date: Tue, 17 Nov 2026 23:58:48 +0000 [thread overview]
Message-ID: <20261117235848.2975548-1-wakel@google.com> (raw)
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
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next reply other threads:[~2026-11-17 23:59 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 23:58 Wake Liu via ltp [this message]
2026-11-18 4:49 ` [LTP] sys_rtc01: Report TCONF on system time mismatch when STA_UNSYNC is set linuxtestproject.agent
2026-11-18 10:12 ` Cyril Hrubis
2026-11-18 9:23 ` [LTP] [PATCH] " Cyril Hrubis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261117235848.2975548-1-wakel@google.com \
--to=ltp@lists.linux.it \
--cc=wakel@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox