All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH] sys_rtc01: Report TCONF on system time mismatch when STA_UNSYNC is set
@ 2026-11-17 23:58 Wake Liu via ltp
  2026-11-18  4:49 ` [LTP] " linuxtestproject.agent
  2026-11-18  9:23 ` [LTP] [PATCH] " Cyril Hrubis
  0 siblings, 2 replies; 4+ messages in thread
From: Wake Liu via ltp @ 2026-11-17 23:58 UTC (permalink / raw)
  To: ltp; +Cc: Wake Liu

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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [LTP] sys_rtc01: Report TCONF on system time mismatch when STA_UNSYNC is set
  2026-11-17 23:58 [LTP] [PATCH] sys_rtc01: Report TCONF on system time mismatch when STA_UNSYNC is set Wake Liu via ltp
@ 2026-11-18  4:49 ` linuxtestproject.agent
  2026-11-18 10:12   ` Cyril Hrubis
  2026-11-18  9:23 ` [LTP] [PATCH] " Cyril Hrubis
  1 sibling, 1 reply; 4+ messages in thread
From: linuxtestproject.agent @ 2026-11-18  4:49 UTC (permalink / raw)
  To: Wake Liu; +Cc: ltp

Hi Wake,

On Tue, Nov 17, 2026, Wake Liu <wakel@google.com> wrote:
> sys_rtc01: Report TCONF on system time mismatch when STA_UNSYNC is set

> +	} 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

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [LTP] [PATCH] sys_rtc01: Report TCONF on system time mismatch when STA_UNSYNC is set
  2026-11-17 23:58 [LTP] [PATCH] sys_rtc01: Report TCONF on system time mismatch when STA_UNSYNC is set Wake Liu via ltp
  2026-11-18  4:49 ` [LTP] " linuxtestproject.agent
@ 2026-11-18  9:23 ` Cyril Hrubis
  1 sibling, 0 replies; 4+ messages in thread
From: Cyril Hrubis @ 2026-11-18  9:23 UTC (permalink / raw)
  To: Wake Liu; +Cc: ltp

Hi
Applied, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [LTP] sys_rtc01: Report TCONF on system time mismatch when STA_UNSYNC is set
  2026-11-18  4:49 ` [LTP] " linuxtestproject.agent
@ 2026-11-18 10:12   ` Cyril Hrubis
  0 siblings, 0 replies; 4+ messages in thread
From: Cyril Hrubis @ 2026-11-18 10:12 UTC (permalink / raw)
  To: linuxtestproject.agent; +Cc: Wake Liu, ltp

Hi!
> > +	} 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.

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:

diff --git a/testcases/kernel/sysfs/class/rtc/sys_rtc01.c b/testcases/kernel/sysfs/class/rtc/sys_rtc01.c
index 0e1c69f95..a4ad37088 100644
--- a/testcases/kernel/sysfs/class/rtc/sys_rtc01.c
+++ b/testcases/kernel/sysfs/class/rtc/sys_rtc01.c
@@ -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 {


-- 
Cyril Hrubis
chrubis@suse.cz

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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-18 10:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 23:58 [LTP] [PATCH] sys_rtc01: Report TCONF on system time mismatch when STA_UNSYNC is set Wake Liu via ltp
2026-11-18  4:49 ` [LTP] " linuxtestproject.agent
2026-11-18 10:12   ` Cyril Hrubis
2026-11-18  9:23 ` [LTP] [PATCH] " Cyril Hrubis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.