[PATCH v2 2/2] selftests: timers: Add a test for CLOCK_TAI timerfds
From: Chris Down
Date: Sat Nov 28 2026 - 04:37:12 EST
Add timerfd-tai. It checks that an absolute CLOCK_TAI timer expires once
CLOCK_TAI reaches its expiry time, and what setting the clock does to
timers which were armed with TFD_TIMER_CANCEL_ON_SET:
- A step of CLOCK_REALTIME has to cancel CLOCK_REALTIME and CLOCK_TAI
timers.
- A change of the TAI offset has to cancel CLOCK_TAI timers only.
- Injecting an offset of zero has to cancel neither.
The test steps CLOCK_REALTIME by a second and changes the TAI offset by
one. It puts both back afterwards, but that still makes it a destructive
test. It is skipped where timerfd_create() rejects CLOCK_TAI.
Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>
---
tools/testing/selftests/timers/.gitignore | 1 +
tools/testing/selftests/timers/Makefile | 2 +-
tools/testing/selftests/timers/timerfd-tai.c | 183 +++++++++++++++++++
3 files changed, 185 insertions(+), 1 deletion(-)
create mode 100644 tools/testing/selftests/timers/timerfd-tai.c
diff --git a/tools/testing/selftests/timers/.gitignore b/tools/testing/selftests/timers/.gitignore
index bb5326ff900b..ef56c8bbcc35 100644
--- a/tools/testing/selftests/timers/.gitignore
+++ b/tools/testing/selftests/timers/.gitignore
@@ -20,3 +20,4 @@ valid-adjtimex
adjtick
set-tz
freq-step
+timerfd-tai
diff --git a/tools/testing/selftests/timers/Makefile b/tools/testing/selftests/timers/Makefile
index 0e73a16874c4..7a978064e3fb 100644
--- a/tools/testing/selftests/timers/Makefile
+++ b/tools/testing/selftests/timers/Makefile
@@ -9,7 +9,7 @@ TEST_GEN_PROGS = posix_timers nanosleep nsleep-lat set-timer-lat mqueue-lat \
DESTRUCTIVE_TESTS = alarmtimer-suspend valid-adjtimex adjtick change_skew \
skew_consistency clocksource-switch freq-step leap-a-day \
- leapcrash set-tai set-2038 set-tz
+ leapcrash set-tai set-2038 set-tz timerfd-tai
TEST_GEN_PROGS_EXTENDED = $(DESTRUCTIVE_TESTS)
diff --git a/tools/testing/selftests/timers/timerfd-tai.c b/tools/testing/selftests/timers/timerfd-tai.c
new file mode 100644
index 000000000000..803937d9e354
--- /dev/null
+++ b/tools/testing/selftests/timers/timerfd-tai.c
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Tests for timerfd timers on CLOCK_TAI.
+ *
+ * The TFD_TIMER_CANCEL_ON_SET tests run on CLOCK_REALTIME too, because a
+ * change of the TAI offset has to cancel the timers on one clock but not on
+ * the other. They step CLOCK_REALTIME and change the TAI offset, and put both
+ * back afterwards.
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <poll.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/timerfd.h>
+#include <sys/timex.h>
+
+#include "clock-helpers.h"
+#include "kselftest.h"
+
+static int tai_offset;
+
+static void set_tai_offset(int offset)
+{
+ struct timex tx = { .modes = ADJ_TAI, .constant = offset };
+
+ if (adjtimex(&tx) < 0)
+ ksft_exit_fail_perror("adjtimex(ADJ_TAI)");
+}
+
+static void step_realtime(int sec)
+{
+ struct timex tx = { .modes = ADJ_SETOFFSET, .time.tv_sec = sec };
+
+ if (adjtimex(&tx) < 0)
+ ksft_exit_fail_perror("adjtimex(ADJ_SETOFFSET)");
+}
+
+static long long timespec_to_ns(const struct timespec *ts)
+{
+ return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec;
+}
+
+static void check_expiry(void)
+{
+ struct pollfd pfd = { .events = POLLIN };
+ struct itimerspec its = {};
+ struct timespec now;
+ uint64_t ticks;
+ bool pass;
+
+ pfd.fd = timerfd_create(CLOCK_TAI, 0);
+ if (pfd.fd < 0)
+ ksft_exit_fail_perror("timerfd_create()");
+
+ if (clock_gettime(CLOCK_TAI, &its.it_value))
+ ksft_exit_fail_perror("clock_gettime()");
+ its.it_value.tv_sec++;
+
+ if (timerfd_settime(pfd.fd, TFD_TIMER_ABSTIME, &its, NULL))
+ ksft_exit_fail_perror("timerfd_settime()");
+
+ pass = poll(&pfd, 1, 5 * MSEC_PER_SEC) == 1 &&
+ read(pfd.fd, &ticks, sizeof(ticks)) == sizeof(ticks) && ticks == 1;
+
+ if (clock_gettime(CLOCK_TAI, &now))
+ ksft_exit_fail_perror("clock_gettime()");
+ pass &= timespec_to_ns(&now) >= timespec_to_ns(&its.it_value);
+
+ close(pfd.fd);
+ ksft_test_result(pass, "%s %s\n", __func__, clock_name(CLOCK_TAI));
+}
+
+/* Arm a timer which is too far away to expire while the test runs */
+static int arm_timer(clockid_t clockid)
+{
+ struct itimerspec its = {};
+ int fd;
+
+ fd = timerfd_create(clockid, TFD_NONBLOCK);
+ if (fd < 0)
+ ksft_exit_fail_perror("timerfd_create()");
+
+ if (clock_gettime(clockid, &its.it_value))
+ ksft_exit_fail_perror("clock_gettime()");
+ its.it_value.tv_sec += 3600;
+
+ if (timerfd_settime(fd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &its, NULL))
+ ksft_exit_fail_perror("timerfd_settime()");
+
+ return fd;
+}
+
+/*
+ * A cancelled timer is readable and the read fails with ECANCELED. One which
+ * was left alone is not readable.
+ */
+static bool check_timer(int fd, bool cancelled)
+{
+ struct pollfd pfd = { .fd = fd, .events = POLLIN };
+ bool readable = poll(&pfd, 1, 0) == 1;
+ uint64_t ticks;
+
+ if (readable != cancelled)
+ return false;
+
+ if (read(fd, &ticks, sizeof(ticks)) >= 0)
+ return false;
+
+ return errno == (cancelled ? ECANCELED : EAGAIN);
+}
+
+static void check_clock_step(clockid_t clockid)
+{
+ int fd = arm_timer(clockid);
+ bool pass;
+
+ /* CLOCK_TAI is stepped along with CLOCK_REALTIME */
+ step_realtime(1);
+ pass = check_timer(fd, true);
+ step_realtime(-1);
+ pass &= check_timer(fd, true);
+
+ /* Injecting no offset counts as setting the clock, but it moves nothing */
+ step_realtime(0);
+ pass &= check_timer(fd, false);
+
+ close(fd);
+ ksft_test_result(pass, "%s %s\n", __func__, clock_name(clockid));
+}
+
+static void check_tai_offset(clockid_t clockid)
+{
+ /* The TAI offset only moves CLOCK_TAI */
+ bool cancelled = clockid == CLOCK_TAI;
+ int fd = arm_timer(clockid);
+ bool pass;
+
+ set_tai_offset(tai_offset + 1);
+ pass = check_timer(fd, cancelled);
+ set_tai_offset(tai_offset);
+ pass &= check_timer(fd, cancelled);
+
+ close(fd);
+ ksft_test_result(pass, "%s %s\n", __func__, clock_name(clockid));
+}
+
+int main(void)
+{
+ struct timex tx = {};
+ int fd;
+
+ ksft_print_header();
+
+ fd = timerfd_create(CLOCK_TAI, 0);
+ if (fd < 0 && errno == EINVAL)
+ ksft_exit_skip("timerfd does not support CLOCK_TAI\n");
+ if (fd < 0)
+ ksft_exit_fail_perror("timerfd_create()");
+ close(fd);
+
+ if (adjtimex(&tx) < 0)
+ ksft_exit_fail_perror("adjtimex()");
+ tai_offset = tx.tai;
+
+ /* Setting the TAI offset to what it already is only checks permissions */
+ tx.modes = ADJ_TAI;
+ tx.constant = tai_offset;
+ if (adjtimex(&tx) < 0 && errno == EPERM)
+ ksft_exit_skip("Changing the clocks needs CAP_SYS_TIME\n");
+
+ ksft_set_plan(5);
+
+ check_expiry();
+ check_clock_step(CLOCK_REALTIME);
+ check_clock_step(CLOCK_TAI);
+ check_tai_offset(CLOCK_REALTIME);
+ check_tai_offset(CLOCK_TAI);
+
+ ksft_finished();
+}
--
2.56.1