Skip to content

Commit e809a80

Browse files
ahunter6KAGA-KOKO
authored andcommitted
timekeeping: Prepare timekeeping_cycles_to_ns() for overflow safety
Open code clocksource_delta() in timekeeping_cycles_to_ns() so that overflow safety can be added efficiently. 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 3094c6d commit e809a80

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

kernel/time/timekeeping.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,17 @@ static void tk_setup_internals(struct timekeeper *tk, struct clocksource *clock)
367367
static inline u64 timekeeping_cycles_to_ns(const struct tk_read_base *tkr, u64 cycles)
368368
{
369369
/* Calculate the delta since the last update_wall_time() */
370-
u64 delta = clocksource_delta(cycles, tkr->cycle_last, tkr->mask);
370+
u64 mask = tkr->mask, delta = (cycles - tkr->cycle_last) & mask;
371+
372+
if (IS_ENABLED(CONFIG_CLOCKSOURCE_VALIDATE_LAST_CYCLE)) {
373+
/*
374+
* Handle clocksource inconsistency between CPUs to prevent
375+
* time from going backwards by checking for the MSB of the
376+
* mask being set in the delta.
377+
*/
378+
if (unlikely(delta & ~(mask >> 1)))
379+
return tkr->xtime_nsec >> tkr->shift;
380+
}
371381

372382
return ((delta * tkr->mult) + tkr->xtime_nsec) >> tkr->shift;
373383
}

0 commit comments

Comments
 (0)