timekeeping: Avoid some runtime conditionals around auxiliary clocks
v2 cover timekeeping: Avoid some runtime conditionals around auxiliary clocks
Some of the conditionals around auxiliary timekeepers are known at compile time but are currently checked at runtime. Introduce and use some helpers to avoid the runtime overhead.
---
Changes in v2:
- Rebase on v7.3-rc1.
- Link to v1: https://patch.msgid.link/20260812-timekeeping-conditionals-v1-0-75ebe366677d@linutronix.de
---
Thomas Weißschuh (Schneider Electric) (4):
timekeeping: Move timekeeper_is_core_tk() up in the file
timekeeping: Use timekeeper_is_core_tk() in timekeeping_update_on_shadow()
timekeeping: Remove tk_is_aux() and its usage
timekeeping: Introduce and use timekeeper_is_clock_valid()
kernel/time/timekeeping.c | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-)
---
base-commit: cee9395acd8043be0644b25c34bfa86623f2b935
change-id: 20260701-timekeeping-conditionals-25e2d45d86d9
Best regards,
Signature · 1 line
Thomas Weißschuh <[email protected]>
PATCH v2 1/4 timekeeping: Move timekeeper_is_core_tk() up in the file
New users are about to be introduced which require the definition earlier in the file.
Move it.
--- kernel/time/timekeeping.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/time/timekeeping.c +5 -5
@@ -90,6 +90,11 @@ static inline void tk_update_aux_offs(struct timekeeper *tk, ktime_t offs) tk->monotonic_to_aux =ktime_to_timespec64 (offs); } +static inline bool timekeeper_is_core_tk (struct timekeeper *tk) +{ +return !IS_ENABLED (CONFIG_POSIX_AUX_CLOCKS ) || tk->id ==TIMEKEEPER_CORE ; +} +/* flag for if timekeeping is suspended */ int __read_mostly timekeeping_suspended;
@@ -1735,11 +1740,6 @@ int do_settimeofday64(const struct timespec64 *ts) }EXPORT_SYMBOL (do_settimeofday64); -static inline bool timekeeper_is_core_tk (struct timekeeper *tk) -{ -return !IS_ENABLED (CONFIG_POSIX_AUX_CLOCKS ) || tk->id ==TIMEKEEPER_CORE ; -} -/** * __timekeeping_inject_offset - Adds or subtracts from the current time. * @tkd: Pointer to the timekeeper to modify
Signature · 1 line
2.55.0
PATCH v2 2/4 timekeeping: Use timekeeper_is_core_tk() in timekeeping_update_on_shadow()
In reply to Thomas Weißschuh (Schneider Electric)If CONFIG_POSIX_AUX_CLOCKS is disabled, the timekeeper is known to be the core timekeeper.
Use the helper to avoid the conditional at runtime in this case.
--- kernel/time/timekeeping.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/time/timekeeping.c +1 -1
@@ -842,7 +842,7 @@ static void timekeeping_update_from_shadow(struct tk_data *tkd, unsigned int acttk_update_ktime_data (tk); tk->tkr_mono.base_real = tk->tkr_mono.base + tk->offs_real; -if (tk->id ==TIMEKEEPER_CORE ) { +if (timekeeper_is_core_tk (tk)) {tk_update_ns_to_cyc (tk, &tkd->timekeeper);update_vsyscall (tk);update_pvclock_gtod (tk, action &TK_CLOCK_WAS_SET );
Signature · 1 line
2.55.0
PATCH v2 3/4 timekeeping: Remove tk_is_aux() and its usage
In reply to Thomas Weißschuh (Schneider Electric)Any timekeeper which is not the core timekeeper is already known to be an auxiliary one. There is no need for an additional conditional.
Remove the 'else if' and the now unused tk_is_aux().
--- kernel/time/timekeeping.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-)
kernel/time/timekeeping.c +1 -11
@@ -62,22 +62,12 @@ static inline bool tk_get_aux_ts64(unsigned int tkid, struct timespec64 *ts) {return ktime_get_aux_ts64 (CLOCK_AUX + tkid -TIMEKEEPER_AUX_FIRST , ts); } - -static inline bool tk_is_aux (const struct timekeeper *tk) -{ -return tk->id >=TIMEKEEPER_AUX_FIRST && tk->id <=TIMEKEEPER_AUX_LAST ; -}static inline struct tk_data *aux_get_tk_data (clockid_t id);#else static inline bool tk_get_aux_ts64 (unsigned int tkid,struct timespec64 *ts) {return false; } - -static inline bool tk_is_aux (const struct timekeeper *tk) -{ -return false; -}static inline struct tk_data *aux_get_tk_data (clockid_t id) {return NULL ;
@@ -849,7 +839,7 @@ static void timekeeping_update_from_shadow(struct tk_data *tkd, unsigned int actupdate_fast_timekeeper (&tk->tkr_mono, &tk_fast_mono);update_fast_timekeeper (&tk->tkr_raw, &tk_fast_raw); - }else if (tk_is_aux (tk)) { + }else {vdso_time_update_aux (tk); }
Signature · 1 line
2.55.0
PATCH v2 4/4 timekeeping: Introduce and use timekeeper_is_clock_valid()
In reply to Thomas Weißschuh (Schneider Electric)If CONFIG_POSIX_AUX_CLOCKS is disabled only the core timekeeper exists which is always valid.
Introduce and use a helper function to avoid the runtime conditional in this case.
--- kernel/time/timekeeping.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
kernel/time/timekeeping.c +11 -6
@@ -85,6 +85,11 @@ static inline bool timekeeper_is_core_tk(struct timekeeper *tk)return !IS_ENABLED (CONFIG_POSIX_AUX_CLOCKS ) || tk->id ==TIMEKEEPER_CORE ; } +static inline bool timekeeper_is_clock_valid (const struct timekeeper *tk) +{ +return !IS_ENABLED (CONFIG_POSIX_AUX_CLOCKS ) || tk->clock_valid; +} +/* flag for if timekeeping is suspended */ int __read_mostly timekeeping_suspended;
@@ -1292,7 +1297,7 @@ void ktime_get_snapshot_id(clockid_t clock_id, struct system_time_snapshot *syst seq =read_seqcount_begin (&tkd->seq);/* Aux clocks can be invalid */ -if (!tk->clock_valid) +if (!timekeeper_is_clock_valid (tk))return ; now =tk_clock_read_snapshot (&tk->tkr_mono, &chs);
@@ -2993,7 +2998,7 @@ static int __do_adjtimex(struct tk_data *tkd, struct __kernel_timex *txc,guard (raw_spinlock_irqsave)(&tkd->lock); -if (!tks->clock_valid) +if (!timekeeper_is_clock_valid (tks))return -ENODEV ;if (txc->modes &ADJ_SETOFFSET ) {
@@ -3108,7 +3113,7 @@ static void tk_aux_update_clocksource(void)struct timekeeper *tks = &tkd->shadow_timekeeper;guard (raw_spinlock_irqsave)(&tkd->lock); -if (!tks->clock_valid) +if (!timekeeper_is_clock_valid (tks))continue ;timekeeping_forward_now (tks);
@@ -3127,7 +3132,7 @@ static void tk_aux_advance(void)struct tk_data *aux_tkd = &timekeeper_data[id +TIMEKEEPER_AUX_FIRST ];guard (raw_spinlock)(&aux_tkd->lock); -if (aux_tkd->shadow_timekeeper.clock_valid) +if (timekeeper_is_clock_valid (&aux_tkd->shadow_timekeeper))__timekeeping_advance (aux_tkd,TK_ADV_TICK ); } }
@@ -3155,7 +3160,7 @@ bool ktime_get_aux(clockid_t id, ktime_t *kt) aux_tk = &aux_tkd->timekeeper; do { seq = read_seqcount_begin(&aux_tkd->seq); - if (!aux_tk->clock_valid) + if (!timekeeper_is_clock_valid(aux_tk)) return false; base = ktime_add(aux_tk->tkr_mono.base, aux_tk->offs_aux);
@@ -3214,7 +3219,7 @@ static int aux_clock_set(const clockid_t id, const struct timespec64 *tnew) aux_tks = &aux_tkd->shadow_timekeeper;guard (raw_spinlock_irq)(&aux_tkd->lock); -if (!aux_tks->clock_valid) +if (!timekeeper_is_clock_valid (aux_tks))return -ENODEV ;/* Forward the timekeeper base time */
Signature · 1 line
2.55.0