[patch 1/3] timekeeping: Provide ktime_get_clock_ts64()

Thomas Gleixner posted 3 patches 3 months, 2 weeks ago
There is a newer version of this series
[patch 1/3] timekeeping: Provide ktime_get_clock_ts64()
Posted by Thomas Gleixner 3 months, 2 weeks ago
PTP implements an inline switch case for taking timestamps from various
POSIX clock IDs, which already consumes quite some text space. Expanding it
for auxiliary clocks really becomes too big for inlining.

Provide a out of line version. 

The function invalidates the timestamp in case the clock is invalid. The
invalidation allows to implement a validation check without the need to
propagate a return value through deep existing call chains.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 include/linux/timekeeping.h |    1 +
 kernel/time/timekeeping.c   |   34 ++++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

--- a/include/linux/timekeeping.h
+++ b/include/linux/timekeeping.h
@@ -44,6 +44,7 @@ extern void ktime_get_ts64(struct timesp
 extern void ktime_get_real_ts64(struct timespec64 *tv);
 extern void ktime_get_coarse_ts64(struct timespec64 *ts);
 extern void ktime_get_coarse_real_ts64(struct timespec64 *ts);
+extern void ktime_get_clock_ts64(clockid_t id, struct timespec64 *ts);
 
 /* Multigrain timestamp interfaces */
 extern void ktime_get_coarse_real_ts64_mg(struct timespec64 *ts);
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -1636,6 +1636,40 @@ void ktime_get_raw_ts64(struct timespec6
 EXPORT_SYMBOL(ktime_get_raw_ts64);
 
 /**
+ * ktime_get_clock_ts64 - Returns time of a clock in a timespec
+ * @id:		POSIX clock ID of the clock to read
+ * @ts:		Pointer to the timespec64 to be set
+ *
+ * The timestamp is invalidated (@ts->sec is set to -1) if the
+ * clock @id is not available.
+ */
+void ktime_get_clock_ts64(clockid_t id, struct timespec64 *ts)
+{
+	/* Invalidate time stamp */
+	ts->tv_sec = -1;
+	ts->tv_nsec = 0;
+
+	switch (id) {
+	case CLOCK_REALTIME:
+		ktime_get_real_ts64(ts);
+		return;
+	case CLOCK_MONOTONIC:
+		ktime_get_ts64(ts);
+		return;
+	case CLOCK_MONOTONIC_RAW:
+		ktime_get_raw_ts64(ts);
+		return;
+	case CLOCK_AUX ... CLOCK_AUX_LAST:
+		if (IS_ENABLED(CONFIG_POSIX_AUX_CLOCKS))
+			ktime_get_aux_ts64(id, ts);
+		return;
+	default:
+		WARN_ON_ONCE(1);
+	}
+}
+EXPORT_SYMBOL_GPL(ktime_get_clock_ts64);
+
+/**
  * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
  */
 int timekeeping_valid_for_hres(void)
Re: [patch 1/3] timekeeping: Provide ktime_get_clock_ts64()
Posted by Vadim Fedorenko 3 months, 1 week ago
On 26/06/2025 14:27, Thomas Gleixner wrote:
> PTP implements an inline switch case for taking timestamps from various
> POSIX clock IDs, which already consumes quite some text space. Expanding it
> for auxiliary clocks really becomes too big for inlining.
> 
> Provide a out of line version.
> 
> The function invalidates the timestamp in case the clock is invalid. The
> invalidation allows to implement a validation check without the need to
> propagate a return value through deep existing call chains.
> 
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
> ---
>   include/linux/timekeeping.h |    1 +
>   kernel/time/timekeeping.c   |   34 ++++++++++++++++++++++++++++++++++
>   2 files changed, 35 insertions(+)
> 
> --- a/include/linux/timekeeping.h
> +++ b/include/linux/timekeeping.h
> @@ -44,6 +44,7 @@ extern void ktime_get_ts64(struct timesp
>   extern void ktime_get_real_ts64(struct timespec64 *tv);
>   extern void ktime_get_coarse_ts64(struct timespec64 *ts);
>   extern void ktime_get_coarse_real_ts64(struct timespec64 *ts);
> +extern void ktime_get_clock_ts64(clockid_t id, struct timespec64 *ts);
>   
>   /* Multigrain timestamp interfaces */
>   extern void ktime_get_coarse_real_ts64_mg(struct timespec64 *ts);
> --- a/kernel/time/timekeeping.c
> +++ b/kernel/time/timekeeping.c
> @@ -1636,6 +1636,40 @@ void ktime_get_raw_ts64(struct timespec6
>   EXPORT_SYMBOL(ktime_get_raw_ts64);
>   
>   /**
> + * ktime_get_clock_ts64 - Returns time of a clock in a timespec
> + * @id:		POSIX clock ID of the clock to read
> + * @ts:		Pointer to the timespec64 to be set
> + *
> + * The timestamp is invalidated (@ts->sec is set to -1) if the
> + * clock @id is not available.
> + */
> +void ktime_get_clock_ts64(clockid_t id, struct timespec64 *ts)
> +{
> +	/* Invalidate time stamp */
> +	ts->tv_sec = -1;
> +	ts->tv_nsec = 0;
> +
> +	switch (id) {
> +	case CLOCK_REALTIME:
> +		ktime_get_real_ts64(ts);
> +		return;
> +	case CLOCK_MONOTONIC:
> +		ktime_get_ts64(ts);
> +		return;
> +	case CLOCK_MONOTONIC_RAW:
> +		ktime_get_raw_ts64(ts);
> +		return;
> +	case CLOCK_AUX ... CLOCK_AUX_LAST:
> +		if (IS_ENABLED(CONFIG_POSIX_AUX_CLOCKS))
> +			ktime_get_aux_ts64(id, ts);
> +		return;
> +	default:
> +		WARN_ON_ONCE(1);
> +	}
> +}
> +EXPORT_SYMBOL_GPL(ktime_get_clock_ts64);
> +
> +/**
>    * timekeeping_valid_for_hres - Check if timekeeping is suitable for hres
>    */
>   int timekeeping_valid_for_hres(void)
> 

Reviewed-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
Re: [patch 1/3] timekeeping: Provide ktime_get_clock_ts64()
Posted by John Stultz 3 months, 1 week ago
On Thu, Jun 26, 2025 at 6:27 AM Thomas Gleixner <tglx@linutronix.de> wrote:
>
> PTP implements an inline switch case for taking timestamps from various
> POSIX clock IDs, which already consumes quite some text space. Expanding it
> for auxiliary clocks really becomes too big for inlining.
>
> Provide a out of line version.
>
> The function invalidates the timestamp in case the clock is invalid. The
> invalidation allows to implement a validation check without the need to
> propagate a return value through deep existing call chains.
>
> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
> ---
>  include/linux/timekeeping.h |    1 +
>  kernel/time/timekeeping.c   |   34 ++++++++++++++++++++++++++++++++++
>  2 files changed, 35 insertions(+)
>
> --- a/include/linux/timekeeping.h
> +++ b/include/linux/timekeeping.h
> @@ -44,6 +44,7 @@ extern void ktime_get_ts64(struct timesp
>  extern void ktime_get_real_ts64(struct timespec64 *tv);
>  extern void ktime_get_coarse_ts64(struct timespec64 *ts);
>  extern void ktime_get_coarse_real_ts64(struct timespec64 *ts);
> +extern void ktime_get_clock_ts64(clockid_t id, struct timespec64 *ts);
>
>  /* Multigrain timestamp interfaces */
>  extern void ktime_get_coarse_real_ts64_mg(struct timespec64 *ts);
> --- a/kernel/time/timekeeping.c
> +++ b/kernel/time/timekeeping.c
> @@ -1636,6 +1636,40 @@ void ktime_get_raw_ts64(struct timespec6
>  EXPORT_SYMBOL(ktime_get_raw_ts64);
>
>  /**
> + * ktime_get_clock_ts64 - Returns time of a clock in a timespec
> + * @id:                POSIX clock ID of the clock to read
> + * @ts:                Pointer to the timespec64 to be set
> + *
> + * The timestamp is invalidated (@ts->sec is set to -1) if the
> + * clock @id is not available.
> + */
> +void ktime_get_clock_ts64(clockid_t id, struct timespec64 *ts)
> +{
> +       /* Invalidate time stamp */
> +       ts->tv_sec = -1;
> +       ts->tv_nsec = 0;
> +
> +       switch (id) {
> +       case CLOCK_REALTIME:
> +               ktime_get_real_ts64(ts);
> +               return;
> +       case CLOCK_MONOTONIC:
> +               ktime_get_ts64(ts);
> +               return;
> +       case CLOCK_MONOTONIC_RAW:
> +               ktime_get_raw_ts64(ts);
> +               return;
> +       case CLOCK_AUX ... CLOCK_AUX_LAST:
> +               if (IS_ENABLED(CONFIG_POSIX_AUX_CLOCKS))
> +                       ktime_get_aux_ts64(id, ts);
> +               return;
> +       default:
> +               WARN_ON_ONCE(1);
> +       }
> +}
> +EXPORT_SYMBOL_GPL(ktime_get_clock_ts64);

While I recognize this is mainly focused on the ptp use case, as the
interface looks generic from headers point of view, should we add the
other clockids for completeness?

Other than that,
Acked-by: John Stultz <jstultz@google.com>

thanks
-john