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