Skip to content

Commit 135225a

Browse files
ahunter6KAGA-KOKO
authored andcommitted
timekeeping: Let timekeeping_cycles_to_ns() handle both under and overflow
For the case !CONFIG_CLOCKSOURCE_VALIDATE_LAST_CYCLE, forego overflow protection in the range (mask << 1) < delta <= mask, and interpret it always as an inconsistency between CPU clock values. That allows slightly neater code, and it is on a slow path so has no effect on performance. Suggested-by: Thomas Gleixner <[email protected]> Signed-off-by: Adrian Hunter <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent fcf190c commit 135225a

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

kernel/time/timekeeping.c

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,14 @@ static inline u64 timekeeping_debug_get_ns(const struct tk_read_base *tkr)
266266
* Try to catch underflows by checking if we are seeing small
267267
* mask-relative negative values.
268268
*/
269-
if (unlikely((~delta & mask) < (mask >> 3))) {
269+
if (unlikely((~delta & mask) < (mask >> 3)))
270270
tk->underflow_seen = 1;
271-
now = last;
272-
}
273271

274-
/* Cap delta value to the max_cycles values to avoid mult overflows */
275-
if (unlikely(delta > max)) {
272+
/* Check for multiplication overflows */
273+
if (unlikely(delta > max))
276274
tk->overflow_seen = 1;
277-
now = last + max;
278-
}
279275

276+
/* timekeeping_cycles_to_ns() handles both under and overflow */
280277
return timekeeping_cycles_to_ns(tkr, now);
281278
}
282279
#else
@@ -375,19 +372,17 @@ static inline u64 timekeeping_cycles_to_ns(const struct tk_read_base *tkr, u64 c
375372
u64 mask = tkr->mask, delta = (cycles - tkr->cycle_last) & mask;
376373

377374
/*
378-
* This detects the case where the delta overflows the multiplication
379-
* with tkr->mult.
375+
* This detects both negative motion and the case where the delta
376+
* overflows the multiplication with tkr->mult.
380377
*/
381378
if (unlikely(delta > tkr->clock->max_cycles)) {
382-
if (IS_ENABLED(CONFIG_CLOCKSOURCE_VALIDATE_LAST_CYCLE)) {
383-
/*
384-
* Handle clocksource inconsistency between CPUs to prevent
385-
* time from going backwards by checking for the MSB of the
386-
* mask being set in the delta.
387-
*/
388-
if (unlikely(delta & ~(mask >> 1)))
389-
return tkr->xtime_nsec >> tkr->shift;
390-
}
379+
/*
380+
* Handle clocksource inconsistency between CPUs to prevent
381+
* time from going backwards by checking for the MSB of the
382+
* mask being set in the delta.
383+
*/
384+
if (delta & ~(mask >> 1))
385+
return tkr->xtime_nsec >> tkr->shift;
391386

392387
return delta_to_ns_safe(tkr, delta);
393388
}

0 commit comments

Comments
 (0)