mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/4] timekeeping: Avoid some runtime conditionals around auxiliary clocks
@ 2026-09-08  6:17 Thomas Weißschuh (Schneider Electric)
  2026-09-08  6:17 ` [PATCH v2 1/4] timekeeping: Move timekeeper_is_core_tk() up in the file Thomas Weißschuh (Schneider Electric)
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Thomas Weißschuh (Schneider Electric) @ 2026-09-08  6:17 UTC (permalink / raw)
  To: John Stultz, Thomas Gleixner, Stephen Boyd, Miroslav Lichvar
  Cc: linux-kernel, Thomas Weißschuh (Schneider Electric)

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.

Signed-off-by: Thomas Weißschuh (Schneider Electric) <thomas.weissschuh@linutronix.de>
---
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,
--  
Thomas Weißschuh <thomas.weissschuh@linutronix.de>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/4] timekeeping: Move timekeeper_is_core_tk() up in the file
  2026-09-08  6:17 [PATCH v2 0/4] timekeeping: Avoid some runtime conditionals around auxiliary clocks Thomas Weißschuh (Schneider Electric)
@ 2026-09-08  6:17 ` Thomas Weißschuh (Schneider Electric)
  2026-09-08  6:17 ` [PATCH v2 2/4] timekeeping: Use timekeeper_is_core_tk() in timekeeping_update_on_shadow() Thomas Weißschuh (Schneider Electric)
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Thomas Weißschuh (Schneider Electric) @ 2026-09-08  6:17 UTC (permalink / raw)
  To: John Stultz, Thomas Gleixner, Stephen Boyd, Miroslav Lichvar
  Cc: linux-kernel, Thomas Weißschuh (Schneider Electric)

New users are about to be introduced which require the definition
earlier in the file.

Move it.

Signed-off-by: Thomas Weißschuh (Schneider Electric) <thomas.weissschuh@linutronix.de>
---
 kernel/time/timekeeping.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index ea2e6e55f37b..2249d7fb3cb6 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -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

-- 
2.55.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 2/4] timekeeping: Use timekeeper_is_core_tk() in timekeeping_update_on_shadow()
  2026-09-08  6:17 [PATCH v2 0/4] timekeeping: Avoid some runtime conditionals around auxiliary clocks Thomas Weißschuh (Schneider Electric)
  2026-09-08  6:17 ` [PATCH v2 1/4] timekeeping: Move timekeeper_is_core_tk() up in the file Thomas Weißschuh (Schneider Electric)
@ 2026-09-08  6:17 ` Thomas Weißschuh (Schneider Electric)
  2026-09-08  6:17 ` [PATCH v2 3/4] timekeeping: Remove tk_is_aux() and its usage Thomas Weißschuh (Schneider Electric)
  2026-09-08  6:17 ` [PATCH v2 4/4] timekeeping: Introduce and use timekeeper_is_clock_valid() Thomas Weißschuh (Schneider Electric)
  3 siblings, 0 replies; 5+ messages in thread
From: Thomas Weißschuh (Schneider Electric) @ 2026-09-08  6:17 UTC (permalink / raw)
  To: John Stultz, Thomas Gleixner, Stephen Boyd, Miroslav Lichvar
  Cc: linux-kernel, 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.

Signed-off-by: Thomas Weißschuh (Schneider Electric) <thomas.weissschuh@linutronix.de>
---
 kernel/time/timekeeping.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 2249d7fb3cb6..12bd61e9b5de 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -842,7 +842,7 @@ static void timekeeping_update_from_shadow(struct tk_data *tkd, unsigned int act
 	tk_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);

-- 
2.55.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 3/4] timekeeping: Remove tk_is_aux() and its usage
  2026-09-08  6:17 [PATCH v2 0/4] timekeeping: Avoid some runtime conditionals around auxiliary clocks Thomas Weißschuh (Schneider Electric)
  2026-09-08  6:17 ` [PATCH v2 1/4] timekeeping: Move timekeeper_is_core_tk() up in the file Thomas Weißschuh (Schneider Electric)
  2026-09-08  6:17 ` [PATCH v2 2/4] timekeeping: Use timekeeper_is_core_tk() in timekeeping_update_on_shadow() Thomas Weißschuh (Schneider Electric)
@ 2026-09-08  6:17 ` Thomas Weißschuh (Schneider Electric)
  2026-09-08  6:17 ` [PATCH v2 4/4] timekeeping: Introduce and use timekeeper_is_clock_valid() Thomas Weißschuh (Schneider Electric)
  3 siblings, 0 replies; 5+ messages in thread
From: Thomas Weißschuh (Schneider Electric) @ 2026-09-08  6:17 UTC (permalink / raw)
  To: John Stultz, Thomas Gleixner, Stephen Boyd, Miroslav Lichvar
  Cc: linux-kernel, 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().

Signed-off-by: Thomas Weißschuh (Schneider Electric) <thomas.weissschuh@linutronix.de>
---
 kernel/time/timekeeping.c | 12 +-----------
 1 file changed, 1 insertion(+), 11 deletions(-)

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 12bd61e9b5de..5cdf6795c5a6 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -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 act
 
 		update_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);
 	}
 

-- 
2.55.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 4/4] timekeeping: Introduce and use timekeeper_is_clock_valid()
  2026-09-08  6:17 [PATCH v2 0/4] timekeeping: Avoid some runtime conditionals around auxiliary clocks Thomas Weißschuh (Schneider Electric)
                   ` (2 preceding siblings ...)
  2026-09-08  6:17 ` [PATCH v2 3/4] timekeeping: Remove tk_is_aux() and its usage Thomas Weißschuh (Schneider Electric)
@ 2026-09-08  6:17 ` Thomas Weißschuh (Schneider Electric)
  3 siblings, 0 replies; 5+ messages in thread
From: Thomas Weißschuh (Schneider Electric) @ 2026-09-08  6:17 UTC (permalink / raw)
  To: John Stultz, Thomas Gleixner, Stephen Boyd, Miroslav Lichvar
  Cc: linux-kernel, 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.

Signed-off-by: Thomas Weißschuh (Schneider Electric) <thomas.weissschuh@linutronix.de>
---
 kernel/time/timekeeping.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 5cdf6795c5a6..8a8cb9f9f333 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -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 */

-- 
2.55.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-09-08  6:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-08  6:17 [PATCH v2 0/4] timekeeping: Avoid some runtime conditionals around auxiliary clocks Thomas Weißschuh (Schneider Electric)
2026-09-08  6:17 ` [PATCH v2 1/4] timekeeping: Move timekeeper_is_core_tk() up in the file Thomas Weißschuh (Schneider Electric)
2026-09-08  6:17 ` [PATCH v2 2/4] timekeeping: Use timekeeper_is_core_tk() in timekeeping_update_on_shadow() Thomas Weißschuh (Schneider Electric)
2026-09-08  6:17 ` [PATCH v2 3/4] timekeeping: Remove tk_is_aux() and its usage Thomas Weißschuh (Schneider Electric)
2026-09-08  6:17 ` [PATCH v2 4/4] timekeeping: Introduce and use timekeeper_is_clock_valid() Thomas Weißschuh (Schneider Electric)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®