* [PATCH v2 1/2] timerfd: Support CLOCK_TAI
2026-11-28 9:36 [PATCH v2 0/2] timerfd: Support CLOCK_TAI Chris Down
@ 2026-11-28 9:36 ` Chris Down
2026-11-28 9:37 ` [PATCH v2 2/2] selftests: timers: Add a test for CLOCK_TAI timerfds Chris Down
1 sibling, 0 replies; 3+ messages in thread
From: Chris Down @ 2026-11-28 9:36 UTC (permalink / raw)
To: linux-fsdevel
Cc: linux-kernel, Alexander Viro, Christian Brauner, Jan Kara,
Anna-Maria Behnsen, Frederic Weisbecker, Thomas Gleixner,
kernel-team
Despite hrtimer and POSIX timers both accepting CLOCK_TAI,
timerfd_create() rejects it. This usually corners userspace into one of
two bad options:
1. Convert TAI deadlines through CLOCK_REALTIME, which reintroduces the
leap second exposure the application was trying to avoid in the first
place.
2. Abandon timerfd entirely and use POSIX timers or busy wait approaches
that do not compose well with epoll based event loops.
Avoid these bad outcomes by allowing timerfd to support CLOCK_TAI.
timerfd arms hrtimers directly and hrtimers already have a CLOCK_TAI
base, so nothing is needed to queue and expire the timers.
TFD_TIMER_CANCEL_ON_SET has to work for them as well. CLOCK_TAI moves
along when CLOCK_REALTIME is set, and it also moves when
adjtimex(ADJ_TAI) changes the TAI offset. timerfd_clock_was_set()
cancels a timer when the offset between CLOCK_MONOTONIC and
CLOCK_REALTIME differs from the one recorded for the timer. That is the
wrong offset for a CLOCK_TAI timer in two cases:
1. adjtimex(ADJ_TAI) moves CLOCK_TAI, but not CLOCK_REALTIME.
2. A leap second moves CLOCK_REALTIME, but not CLOCK_TAI.
Compare against the offset of the clock which the timer is on instead,
and put CLOCK_TAI timers on the cancel list. clock_was_set() already
notifies timerfd when the TAI offset changes, so the timekeeping side
needs no change.
With this, epoll based applications can arm TAI based file descriptor
timers natively, and can have them cancelled when CLOCK_TAI is set.
Signed-off-by: Chris Down <chris@chrisdown.name>
---
fs/timerfd.c | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/fs/timerfd.c b/fs/timerfd.c
index fe845af0b74e..26cf804d8d0f 100644
--- a/fs/timerfd.c
+++ b/fs/timerfd.c
@@ -55,6 +55,13 @@ static inline bool isalarm(struct timerfd_ctx *ctx)
ctx->clockid == CLOCK_BOOTTIME_ALARM;
}
+static ktime_t timerfd_get_moffs(struct timerfd_ctx *ctx)
+{
+ if (ctx->clockid == CLOCK_TAI)
+ return ktime_mono_to_any(0, TK_OFFS_TAI);
+ return ktime_mono_to_real(0);
+}
+
static void __timerfd_triggered(struct timerfd_ctx *ctx)
{
lockdep_assert_held(&ctx->wqh.lock);
@@ -98,14 +105,17 @@ static void timerfd_alarmproc(struct alarm *alarm, ktime_t now)
*/
void timerfd_clock_was_set(void)
{
- ktime_t moffs = ktime_mono_to_real(0);
+ ktime_t moffs_tai = ktime_mono_to_any(0, TK_OFFS_TAI);
+ ktime_t moffs_real = ktime_mono_to_real(0);
struct timerfd_ctx *ctx;
unsigned long flags;
+ ktime_t moffs;
rcu_read_lock();
list_for_each_entry_rcu(ctx, &cancel_list, clist) {
if (!ctx->might_cancel)
continue;
+ moffs = ctx->clockid == CLOCK_TAI ? moffs_tai : moffs_real;
spin_lock_irqsave(&ctx->wqh.lock, flags);
if (ctx->moffs != moffs) {
ctx->moffs = KTIME_MAX;
@@ -154,7 +164,7 @@ static bool timerfd_canceled(struct timerfd_ctx *ctx)
{
if (!ctx->might_cancel || ctx->moffs != KTIME_MAX)
return false;
- ctx->moffs = ktime_mono_to_real(0);
+ ctx->moffs = timerfd_get_moffs(ctx);
return true;
}
@@ -162,7 +172,8 @@ static void timerfd_setup_cancel(struct timerfd_ctx *ctx, int flags)
{
spin_lock(&ctx->cancel_lock);
if ((ctx->clockid == CLOCK_REALTIME ||
- ctx->clockid == CLOCK_REALTIME_ALARM) &&
+ ctx->clockid == CLOCK_REALTIME_ALARM ||
+ ctx->clockid == CLOCK_TAI) &&
(flags & TFD_TIMER_ABSTIME) && (flags & TFD_TIMER_CANCEL_ON_SET)) {
if (!ctx->might_cancel) {
ctx->might_cancel = true;
@@ -434,6 +445,7 @@ SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
(clockid != CLOCK_MONOTONIC &&
clockid != CLOCK_REALTIME &&
clockid != CLOCK_REALTIME_ALARM &&
+ clockid != CLOCK_TAI &&
clockid != CLOCK_BOOTTIME &&
clockid != CLOCK_BOOTTIME_ALARM))
return -EINVAL;
@@ -459,7 +471,7 @@ SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags)
else
hrtimer_setup(&ctx->t.tmr, timerfd_tmrproc, clockid, HRTIMER_MODE_ABS);
- ctx->moffs = ktime_mono_to_real(0);
+ ctx->moffs = timerfd_get_moffs(ctx);
ret = FD_ADD(flags & TFD_SHARED_FCNTL_FLAGS,
anon_inode_getfile_fmode("[timerfd]", &timerfd_fops, ctx,
--
2.56.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* [PATCH v2 2/2] selftests: timers: Add a test for CLOCK_TAI timerfds
2026-11-28 9:36 [PATCH v2 0/2] timerfd: Support CLOCK_TAI Chris Down
2026-11-28 9:36 ` [PATCH v2 1/2] " Chris Down
@ 2026-11-28 9:37 ` Chris Down
1 sibling, 0 replies; 3+ messages in thread
From: Chris Down @ 2026-11-28 9:37 UTC (permalink / raw)
To: linux-fsdevel
Cc: linux-kernel, Alexander Viro, Christian Brauner, Jan Kara,
Anna-Maria Behnsen, Frederic Weisbecker, Thomas Gleixner,
kernel-team
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@chrisdown.name>
---
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
^ permalink raw reply related [flat|nested] 3+ messages in thread