From: Chris Down <chris@chrisdown.name>
To: linux-fsdevel@vger.kernel.org
Cc: linux-kernel@vger.kernel.org,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
Anna-Maria Behnsen <anna-maria@linutronix.de>,
Frederic Weisbecker <frederic@kernel.org>,
Thomas Gleixner <tglx@kernel.org>,
kernel-team@meta.com
Subject: [PATCH v2 1/2] timerfd: Support CLOCK_TAI
Date: Sat, 28 Nov 2026 01:36:59 -0800 [thread overview]
Message-ID: <f7d42197b65bd98166440c75fff0d42d366fc377.1795855037.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1795855037.git.chris@chrisdown.name>
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
next prev parent reply other threads:[~2026-11-28 9:37 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-28 9:36 [PATCH v2 0/2] " Chris Down
2026-11-28 9:36 ` Chris Down [this message]
2026-11-28 9:37 ` [PATCH v2 2/2] selftests: timers: Add a test for CLOCK_TAI timerfds Chris Down
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=f7d42197b65bd98166440c75fff0d42d366fc377.1795855037.git.chris@chrisdown.name \
--to=chris@chrisdown.name \
--cc=anna-maria@linutronix.de \
--cc=brauner@kernel.org \
--cc=frederic@kernel.org \
--cc=jack@suse.cz \
--cc=kernel-team@meta.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=tglx@kernel.org \
--cc=viro@zeniv.linux.org.uk \
/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
all inboxes | Powered by JetHome®