mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 2/2] selftests: timers: Add a test for CLOCK_TAI timerfds
Date: Sat, 28 Nov 2026 01:37:02 -0800	[thread overview]
Message-ID: <bf93ffe454e1992c583e0c5823688f7ed0f80304.1795855037.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1795855037.git.chris@chrisdown.name>

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


      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] timerfd: Support CLOCK_TAI Chris Down
2026-11-28  9:36 ` [PATCH v2 1/2] " Chris Down
2026-11-28  9:37 ` Chris Down [this message]

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=bf93ffe454e1992c583e0c5823688f7ed0f80304.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®