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: 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 <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-19-adrian.hunter@intel.com

authored by

Adrian Hunter and committed by
Thomas Gleixner
135225a3 fcf190c3

+13 -18
+13 -18
kernel/time/timekeeping.c
··· 266 266 * Try to catch underflows by checking if we are seeing small 267 267 * mask-relative negative values. 268 268 */ 269 - if (unlikely((~delta & mask) < (mask >> 3))) { 269 + if (unlikely((~delta & mask) < (mask >> 3))) 270 270 tk->underflow_seen = 1; 271 - now = last; 272 - } 273 271 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)) 276 274 tk->overflow_seen = 1; 277 - now = last + max; 278 - } 279 275 276 + /* timekeeping_cycles_to_ns() handles both under and overflow */ 280 277 return timekeeping_cycles_to_ns(tkr, now); 281 278 } 282 279 #else ··· 372 375 u64 mask = tkr->mask, delta = (cycles - tkr->cycle_last) & mask; 373 376 374 377 /* 375 - * This detects the case where the delta overflows the multiplication 376 - * with tkr->mult. 378 + * This detects both negative motion and the case where the delta 379 + * overflows the multiplication with tkr->mult. 377 380 */ 378 381 if (unlikely(delta > tkr->clock->max_cycles)) { 379 - if (IS_ENABLED(CONFIG_CLOCKSOURCE_VALIDATE_LAST_CYCLE)) { 380 - /* 381 - * Handle clocksource inconsistency between CPUs to prevent 382 - * time from going backwards by checking for the MSB of the 383 - * mask being set in the delta. 384 - */ 385 - if (unlikely(delta & ~(mask >> 1))) 386 - return tkr->xtime_nsec >> tkr->shift; 387 - } 382 + /* 383 + * Handle clocksource inconsistency between CPUs to prevent 384 + * time from going backwards by checking for the MSB of the 385 + * mask being set in the delta. 386 + */ 387 + if (delta & ~(mask >> 1)) 388 + return tkr->xtime_nsec >> tkr->shift; 388 389 389 390 return delta_to_ns_safe(tkr, delta); 390 391 }