Skip to contentSkip to conversation

timerfd: Support CLOCK_TAI

PATCH New Chris Down 1 participant 3 messages Nov 28

Subsystems (5)
lore
Share & tools
lore

Chris Down

author
#0 lore raw

v2 cover timerfd: Support CLOCK_TAI

Both hrtimers and POSIX timers accept CLOCK_TAI, but timerfd_create() rejects it. An application with TAI deadlines and an epoll based event loop is left with two options. It can convert the deadlines to CLOCK_REALTIME, which brings back the exposure to wall clock jumps it was trying to avoid, or it can stop using timerfd.

Patch 1 makes timerfd_create() accept CLOCK_TAI and makes TFD_TIMER_CANCEL_ON_SET work for it. A CLOCK_TAI timer is cancelled when CLOCK_REALTIME is set and when adjtimex(ADJ_TAI) changes the TAI offset. Nothing changes for CLOCK_REALTIME timers.

Patch 2 adds a selftest for this.

There are two existing limitations I noticed which I have not fixed here:

- Relative CLOCK_TAI timers stay on the TAI base, but hrtimer_setup() moves relative CLOCK_REALTIME timers to CLOCK_MONOTONIC. That makes a relative CLOCK_TAI timerfd expire early if the clock is stepped forwards. The same happens with timer_settime() and clock_nanosleep() on CLOCK_TAI today. TFD_TIMER_CANCEL_ON_SET does not report it because it only applies to absolute timers.
- The offset which cancel on set compares against is first recorded in timerfd_create() rather than when the timer is armed. If the clock is stepped between the two, the next clock_was_set() cancels the timer even if nothing moved, and a later step back by the same amount is not reported. This happens with CLOCK_REALTIME timers today.

I can send patches for these separately if you think they are worth fixing.

v2:

- Make TFD_TIMER_CANCEL_ON_SET work for CLOCK_TAI timers (tglx)
- Add a selftest
- Rebase onto tip timers/core

v1: https://lore.kernel.org/r/[email protected]

Chris Down (2):
timerfd: Support CLOCK_TAI
selftests: timers: Add a test for CLOCK_TAI timerfds

 fs/timerfd.c                                 |  20 +-
 tools/testing/selftests/timers/.gitignore    |   1 +
 tools/testing/selftests/timers/Makefile      |   2 +-
 tools/testing/selftests/timers/timerfd-tai.c | 183 +++++++++++++++++++
 4 files changed, 201 insertions(+), 5 deletions(-)
 create mode 100644 tools/testing/selftests/timers/timerfd-tai.c

base-commit: 0359ecff58853a491dd0f09792fea8e87235ae06

Signature · 1 line
2.56.1
author
#1 lore raw

PATCH v2 1/2 timerfd: Support CLOCK_TAI

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.

---
 fs/timerfd.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
fs/timerfd.c +16 -4
index fe845af0b74e..26cf804d8d0f 100644
@@ -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,
Signature · 1 line
2.56.1
author
#2 lore raw

PATCH v2 2/2 selftests: timers: Add a test for CLOCK_TAI timerfds

In reply to Chris Down

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.

---
 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
tools/testing/selftests/timers/.gitignore +1 -0
index bb5326ff900b..ef56c8bbcc35 100644
@@ -20,3 +20,4 @@ valid-adjtimex
 adjtick
 set-tz
 freq-step
+timerfd-tai
tools/testing/selftests/timers/Makefile +1 -1
index 0e73a16874c4..7a978064e3fb 100644
@@ -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)
 
tools/testing/selftests/timers/timerfd-tai.c new file +183 -0

183 lines not on this page · raw message

Signature · 1 line
2.56.1