Skip to content

Commit 05bc6e6

Browse files
committed
timekeeping: Provide time getters for auxiliary clocks
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> Acked-by: John Stultz <jstultz@google.com> Link: https://lore.kernel.org/all/20250625183757.868342628@linutronix.de
1 parent 9f77294 commit 05bc6e6

3 files changed

Lines changed: 81 additions & 0 deletions

File tree

include/linux/posix-timers.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ static inline int clockid_to_fd(const clockid_t clk)
3737
return ~(clk >> 3);
3838
}
3939

40+
static inline bool clockid_aux_valid(clockid_t id)
41+
{
42+
return IS_ENABLED(CONFIG_POSIX_AUX_CLOCKS) && id >= CLOCK_AUX && id <= CLOCK_AUX_LAST;
43+
}
44+
4045
#ifdef CONFIG_POSIX_TIMERS
4146

4247
#include <linux/signal_types.h>

include/linux/timekeeping.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,17 @@ extern bool timekeeping_rtc_skipresume(void);
263263

264264
extern void timekeeping_inject_sleeptime64(const struct timespec64 *delta);
265265

266+
/*
267+
* Auxiliary clock interfaces
268+
*/
269+
#ifdef CONFIG_POSIX_AUX_CLOCKS
270+
extern bool ktime_get_aux(clockid_t id, ktime_t *kt);
271+
extern bool ktime_get_aux_ts64(clockid_t id, struct timespec64 *kt);
272+
#else
273+
static inline bool ktime_get_aux(clockid_t id, ktime_t *kt) { return false; }
274+
static inline bool ktime_get_aux_ts64(clockid_t id, struct timespec64 *kt) { return false; }
275+
#endif
276+
266277
/**
267278
* struct system_time_snapshot - simultaneous raw/real time capture with
268279
* counter value

kernel/time/timekeeping.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2664,6 +2664,18 @@ EXPORT_SYMBOL(hardpps);
26642664
*/
26652665
static unsigned long aux_timekeepers;
26662666

2667+
static inline unsigned int clockid_to_tkid(unsigned int id)
2668+
{
2669+
return TIMEKEEPER_AUX_FIRST + id - CLOCK_AUX;
2670+
}
2671+
2672+
static inline struct tk_data *aux_get_tk_data(clockid_t id)
2673+
{
2674+
if (!clockid_aux_valid(id))
2675+
return NULL;
2676+
return &timekeeper_data[clockid_to_tkid(id)];
2677+
}
2678+
26672679
/* Invoked from timekeeping after a clocksource change */
26682680
static void tk_aux_update_clocksource(void)
26692681
{
@@ -2684,6 +2696,59 @@ static void tk_aux_update_clocksource(void)
26842696
}
26852697
}
26862698

2699+
/**
2700+
* ktime_get_aux - Get time for a AUX clock
2701+
* @id: ID of the clock to read (CLOCK_AUX...)
2702+
* @kt: Pointer to ktime_t to store the time stamp
2703+
*
2704+
* Returns: True if the timestamp is valid, false otherwise
2705+
*/
2706+
bool ktime_get_aux(clockid_t id, ktime_t *kt)
2707+
{
2708+
struct tk_data *aux_tkd = aux_get_tk_data(id);
2709+
struct timekeeper *aux_tk;
2710+
unsigned int seq;
2711+
ktime_t base;
2712+
u64 nsecs;
2713+
2714+
WARN_ON(timekeeping_suspended);
2715+
2716+
if (!aux_tkd)
2717+
return false;
2718+
2719+
aux_tk = &aux_tkd->timekeeper;
2720+
do {
2721+
seq = read_seqcount_begin(&aux_tkd->seq);
2722+
if (!aux_tk->clock_valid)
2723+
return false;
2724+
2725+
base = ktime_add(aux_tk->tkr_mono.base, aux_tk->offs_aux);
2726+
nsecs = timekeeping_get_ns(&aux_tk->tkr_mono);
2727+
} while (read_seqcount_retry(&aux_tkd->seq, seq));
2728+
2729+
*kt = ktime_add_ns(base, nsecs);
2730+
return true;
2731+
}
2732+
EXPORT_SYMBOL_GPL(ktime_get_aux);
2733+
2734+
/**
2735+
* ktime_get_aux_ts64 - Get time for a AUX clock
2736+
* @id: ID of the clock to read (CLOCK_AUX...)
2737+
* @ts: Pointer to timespec64 to store the time stamp
2738+
*
2739+
* Returns: True if the timestamp is valid, false otherwise
2740+
*/
2741+
bool ktime_get_aux_ts64(clockid_t id, struct timespec64 *ts)
2742+
{
2743+
ktime_t now;
2744+
2745+
if (!ktime_get_aux(id, &now))
2746+
return false;
2747+
*ts = ktime_to_timespec64(now);
2748+
return true;
2749+
}
2750+
EXPORT_SYMBOL_GPL(ktime_get_aux_ts64);
2751+
26872752
static __init void tk_aux_setup(void)
26882753
{
26892754
for (int i = TIMEKEEPER_AUX_FIRST; i <= TIMEKEEPER_AUX_LAST; i++)

0 commit comments

Comments
 (0)