Skip to content

Commit 5b605db

Browse files
committed
timekeeping: Provide ktime_get_clock_ts64()
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. Due to merge logistics this temporarily defines CLOCK_AUX[_LAST] if undefined, so that the plain branch, which does not contain any of the core timekeeper changes, can be pulled into the networking tree as prerequisite for the PTP side changes. These temporary defines are removed after that branch is merged into the tip::timers/ptp branch. That way the result in -next or upstream in the next merge window has zero dependencies. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Vadim Fedorenko <vadim.fedorenko@linux.dev> Acked-by: John Stultz <jstultz@google.com> Link: https://lore.kernel.org/all/20250701132628.357686408@linutronix.de
1 parent 19272b3 commit 5b605db

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

include/linux/timekeeping.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extern void ktime_get_ts64(struct timespec64 *ts);
4444
extern void ktime_get_real_ts64(struct timespec64 *tv);
4545
extern void ktime_get_coarse_ts64(struct timespec64 *ts);
4646
extern void ktime_get_coarse_real_ts64(struct timespec64 *ts);
47+
extern void ktime_get_clock_ts64(clockid_t id, struct timespec64 *ts);
4748

4849
/* Multigrain timestamp interfaces */
4950
extern void ktime_get_coarse_real_ts64_mg(struct timespec64 *ts);
@@ -345,4 +346,13 @@ void read_persistent_wall_and_boot_offset(struct timespec64 *wall_clock,
345346
extern int update_persistent_clock64(struct timespec64 now);
346347
#endif
347348

349+
/* Temporary workaround to avoid merge dependencies and cross tree messes */
350+
#ifndef CLOCK_AUX
351+
#define CLOCK_AUX MAX_CLOCKS
352+
#define MAX_AUX_CLOCKS 8
353+
#define CLOCK_AUX_LAST (CLOCK_AUX + MAX_AUX_CLOCKS - 1)
354+
355+
static inline bool ktime_get_aux_ts64(clockid_t id, struct timespec64 *kt) { return false; }
356+
#endif
357+
348358
#endif

kernel/time/timekeeping.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,39 @@ void ktime_get_raw_ts64(struct timespec64 *ts)
15731573
}
15741574
EXPORT_SYMBOL(ktime_get_raw_ts64);
15751575

1576+
/**
1577+
* ktime_get_clock_ts64 - Returns time of a clock in a timespec
1578+
* @id: POSIX clock ID of the clock to read
1579+
* @ts: Pointer to the timespec64 to be set
1580+
*
1581+
* The timestamp is invalidated (@ts->sec is set to -1) if the
1582+
* clock @id is not available.
1583+
*/
1584+
void ktime_get_clock_ts64(clockid_t id, struct timespec64 *ts)
1585+
{
1586+
/* Invalidate time stamp */
1587+
ts->tv_sec = -1;
1588+
ts->tv_nsec = 0;
1589+
1590+
switch (id) {
1591+
case CLOCK_REALTIME:
1592+
ktime_get_real_ts64(ts);
1593+
return;
1594+
case CLOCK_MONOTONIC:
1595+
ktime_get_ts64(ts);
1596+
return;
1597+
case CLOCK_MONOTONIC_RAW:
1598+
ktime_get_raw_ts64(ts);
1599+
return;
1600+
case CLOCK_AUX ... CLOCK_AUX_LAST:
1601+
if (IS_ENABLED(CONFIG_POSIX_AUX_CLOCKS))
1602+
ktime_get_aux_ts64(id, ts);
1603+
return;
1604+
default:
1605+
WARN_ON_ONCE(1);
1606+
}
1607+
}
1608+
EXPORT_SYMBOL_GPL(ktime_get_clock_ts64);
15761609

15771610
/**
15781611
* timekeeping_valid_for_hres - Check if timekeeping is suitable for hres

0 commit comments

Comments
 (0)