Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <tglx@linutronix.de>
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20240325064023.2997-17-adrian.hunter@intel.com

authored by

Adrian Hunter and committed by
Thomas Gleixner
e809a80a 3094c6db

+11 -1
+11 -1
kernel/time/timekeeping.c
··· 367 367 static inline u64 timekeeping_cycles_to_ns(const struct tk_read_base *tkr, u64 cycles) 368 368 { 369 369 /* 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 + } 371 381 372 382 return ((delta * tkr->mult) + tkr->xtime_nsec) >> tkr->shift; 373 383 }