netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: LKML <linux-kernel@vger.kernel.org>
Cc: netdev@vger.kernel.org,
	"Richard Cochran" <richardcochran@gmail.com>,
	"Christopher Hall" <christopher.s.hall@intel.com>,
	"John Stultz" <jstultz@google.com>,
	"Frederic Weisbecker" <frederic@kernel.org>,
	"Anna-Maria Behnsen" <anna-maria@linutronix.de>,
	"Miroslav Lichvar" <mlichvar@redhat.com>,
	"Werner Abt" <werner.abt@meinberg-usa.com>,
	"David Woodhouse" <dwmw2@infradead.org>,
	"Stephen Boyd" <sboyd@kernel.org>,
	"Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
	"Kurt Kanzenbach" <kurt@linutronix.de>,
	"Nam Cao" <namcao@linutronix.de>,
	"Antoine Tenart" <atenart@kernel.org>
Subject: [patch V3 02/11] timekeeping: Provide time getters for auxiliary clocks
Date: Wed, 25 Jun 2025 20:38:31 +0200 (CEST)	[thread overview]
Message-ID: <20250625183757.868342628@linutronix.de> (raw)
In-Reply-To: 20250625182951.587377878@linutronix.de

Provide interfaces similar to the ktime_get*() family which provide access
to the auxiliary clocks.

These interfaces have a boolean return value, which indicates whether the
accessed clock is valid or not.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
V3: Remove misleading TAI comment and use aux_tkd* for clarity - John
---
 include/linux/posix-timers.h |    5 +++
 include/linux/timekeeping.h  |   11 +++++++
 kernel/time/timekeeping.c    |   65 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 81 insertions(+)
---

--- a/include/linux/posix-timers.h
+++ b/include/linux/posix-timers.h
@@ -37,6 +37,11 @@ static inline int clockid_to_fd(const cl
 	return ~(clk >> 3);
 }
 
+static inline bool clockid_aux_valid(clockid_t id)
+{
+	return IS_ENABLED(CONFIG_POSIX_AUX_CLOCKS) && id >= CLOCK_AUX && id <= CLOCK_AUX_LAST;
+}
+
 #ifdef CONFIG_POSIX_TIMERS
 
 #include <linux/signal_types.h>
--- a/include/linux/timekeeping.h
+++ b/include/linux/timekeeping.h
@@ -263,6 +263,17 @@ extern bool timekeeping_rtc_skipresume(v
 
 extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta);
 
+/*
+ * Auxiliary clock interfaces
+ */
+#ifdef CONFIG_POSIX_AUX_CLOCKS
+extern bool ktime_get_aux(clockid_t id, ktime_t *kt);
+extern bool ktime_get_aux_ts64(clockid_t id, struct timespec64 *kt);
+#else
+static inline bool ktime_get_aux(clockid_t id, ktime_t *kt) { return false; }
+static inline bool ktime_get_aux_ts64(clockid_t id, struct timespec64 *kt) { return false; }
+#endif
+
 /**
  * struct system_time_snapshot - simultaneous raw/real time capture with
  *				 counter value
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -2659,6 +2659,18 @@ EXPORT_SYMBOL(hardpps);
 /* Bitmap for the activated auxiliary timekeepers */
 static unsigned long aux_timekeepers;
 
+static inline unsigned int clockid_to_tkid(unsigned int id)
+{
+	return TIMEKEEPER_AUX_FIRST + id - CLOCK_AUX;
+}
+
+static inline struct tk_data *aux_get_tk_data(clockid_t id)
+{
+	if (!clockid_aux_valid(id))
+		return NULL;
+	return &timekeeper_data[clockid_to_tkid(id)];
+}
+
 /* Invoked from timekeeping after a clocksource change */
 static void tk_aux_update_clocksource(void)
 {
@@ -2679,6 +2691,59 @@ static void tk_aux_update_clocksource(vo
 	}
 }
 
+/**
+ * ktime_get_aux - Get time for a AUX clock
+ * @id:	ID of the clock to read (CLOCK_AUX...)
+ * @kt:	Pointer to ktime_t to store the time stamp
+ *
+ * Returns: True if the timestamp is valid, false otherwise
+ */
+bool ktime_get_aux(clockid_t id, ktime_t *kt)
+{
+	struct tk_data *aux_tkd = aux_get_tk_data(id);
+	struct timekeeper *aux_tk;
+	unsigned int seq;
+	ktime_t base;
+	u64 nsecs;
+
+	WARN_ON(timekeeping_suspended);
+
+	if (!aux_tkd)
+		return false;
+
+	aux_tk = &aux_tkd->timekeeper;
+	do {
+		seq = read_seqcount_begin(&aux_tkd->seq);
+		if (!aux_tk->clock_valid)
+			return false;
+
+		base = ktime_add(aux_tk->tkr_mono.base, aux_tk->offs_aux);
+		nsecs = timekeeping_get_ns(&aux_tk->tkr_mono);
+	} while (read_seqcount_retry(&aux_tkd->seq, seq));
+
+	*kt = ktime_add_ns(base, nsecs);
+	return true;
+}
+EXPORT_SYMBOL_GPL(ktime_get_aux);
+
+/**
+ * ktime_get_aux_ts64 - Get time for a AUX clock
+ * @id:	ID of the clock to read (CLOCK_AUX...)
+ * @ts:	Pointer to timespec64 to store the time stamp
+ *
+ * Returns: True if the timestamp is valid, false otherwise
+ */
+bool ktime_get_aux_ts64(clockid_t id, struct timespec64 *ts)
+{
+	ktime_t now;
+
+	if (!ktime_get_aux(id, &now))
+		return false;
+	*ts = ktime_to_timespec64(now);
+	return true;
+}
+EXPORT_SYMBOL_GPL(ktime_get_aux_ts64);
+
 static __init void tk_aux_setup(void)
 {
 	for (int i = TIMEKEEPER_AUX_FIRST; i <= TIMEKEEPER_AUX_LAST; i++)


  parent reply	other threads:[~2025-06-25 18:38 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-25 18:38 [patch V3 00/11] timekeeping: Provide support for auxiliary clocks - Remaining series Thomas Gleixner
2025-06-25 18:38 ` [patch V3 01/11] timekeeping: Update auxiliary timekeepers on clocksource change Thomas Gleixner
2025-06-27  4:43   ` John Stultz
2025-06-27 14:04     ` Thomas Gleixner
2025-06-25 18:38 ` Thomas Gleixner [this message]
2025-06-27  4:18   ` [patch V3 02/11] timekeeping: Provide time getters for auxiliary clocks John Stultz
2025-06-25 18:38 ` [patch V3 03/11] timekeeping: Add minimal posix-timers support " Thomas Gleixner
2025-06-27  4:19   ` John Stultz
2025-06-25 18:38 ` [patch V3 04/11] timekeeping: Provide time setter " Thomas Gleixner
2025-06-27  4:23   ` John Stultz
2025-06-27 14:18     ` Thomas Gleixner
2025-06-27 14:57       ` Thomas Gleixner
2025-06-25 18:38 ` [patch V3 05/11] timekeeping: Make timekeeping_inject_offset() reusable Thomas Gleixner
2025-06-27  4:26   ` John Stultz
2025-06-25 18:38 ` [patch V3 06/11] timekeeping: Add auxiliary clock support to __timekeeping_inject_offset() Thomas Gleixner
2025-06-27  4:54   ` John Stultz
2025-06-25 18:38 ` [patch V3 07/11] timekeeping: Make do_adjtimex() reusable Thomas Gleixner
2025-06-27  4:56   ` John Stultz
2025-06-25 18:38 ` [patch V3 08/11] timekeeping: Prepare do_adtimex() for auxiliary clocks Thomas Gleixner
2025-06-27  5:00   ` John Stultz
2025-06-25 18:38 ` [patch V3 09/11] timekeeping: Provide adjtimex() " Thomas Gleixner
2025-06-27  5:01   ` John Stultz
2025-06-25 18:38 ` [patch V3 10/11] timekeeping: Provide update for auxiliary timekeepers Thomas Gleixner
2025-06-27  5:05   ` John Stultz
2025-06-25 18:38 ` [patch V3 11/11] timekeeping: Provide interface to control auxiliary clocks Thomas Gleixner
2025-06-27  5:07   ` John Stultz

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=20250625183757.868342628@linutronix.de \
    --to=tglx@linutronix.de \
    --cc=anna-maria@linutronix.de \
    --cc=atenart@kernel.org \
    --cc=christopher.s.hall@intel.com \
    --cc=dwmw2@infradead.org \
    --cc=frederic@kernel.org \
    --cc=jstultz@google.com \
    --cc=kurt@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mlichvar@redhat.com \
    --cc=namcao@linutronix.de \
    --cc=netdev@vger.kernel.org \
    --cc=richardcochran@gmail.com \
    --cc=sboyd@kernel.org \
    --cc=thomas.weissschuh@linutronix.de \
    --cc=werner.abt@meinberg-usa.com \
    /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;
as well as URLs for NNTP newsgroup(s).