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

Merge tag 'timers-urgent-2026-03-01' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull timer fix from Ingo Molnar:
"Improve the inlining of jiffies_to_msecs() and jiffies_to_usecs(), for
the common HZ=100, 250 or 1000 cases. Only use a function call for odd
HZ values like HZ=300 that generate more code.

The function call overhead showed up in performance tests of the TCP
code"

* tag 'timers-urgent-2026-03-01' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
time/jiffies: Inline jiffies_to_msecs() and jiffies_to_usecs()

+45 -14
+38 -2
include/linux/jiffies.h
··· 434 434 /* 435 435 * Convert various time units to each other: 436 436 */ 437 - extern unsigned int jiffies_to_msecs(const unsigned long j); 438 - extern unsigned int jiffies_to_usecs(const unsigned long j); 437 + 438 + #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) 439 + /** 440 + * jiffies_to_msecs - Convert jiffies to milliseconds 441 + * @j: jiffies value 442 + * 443 + * This inline version takes care of HZ in {100,250,1000}. 444 + * 445 + * Return: milliseconds value 446 + */ 447 + static inline unsigned int jiffies_to_msecs(const unsigned long j) 448 + { 449 + return (MSEC_PER_SEC / HZ) * j; 450 + } 451 + #else 452 + unsigned int jiffies_to_msecs(const unsigned long j); 453 + #endif 454 + 455 + #if !(USEC_PER_SEC % HZ) 456 + /** 457 + * jiffies_to_usecs - Convert jiffies to microseconds 458 + * @j: jiffies value 459 + * 460 + * Return: microseconds value 461 + */ 462 + static inline unsigned int jiffies_to_usecs(const unsigned long j) 463 + { 464 + /* 465 + * Hz usually doesn't go much further MSEC_PER_SEC. 466 + * jiffies_to_usecs() and usecs_to_jiffies() depend on that. 467 + */ 468 + BUILD_BUG_ON(HZ > USEC_PER_SEC); 469 + 470 + return (USEC_PER_SEC / HZ) * j; 471 + } 472 + #else 473 + unsigned int jiffies_to_usecs(const unsigned long j); 474 + #endif 439 475 440 476 /** 441 477 * jiffies_to_nsecs - Convert jiffies to nanoseconds
+7 -12
kernel/time/time.c
··· 365 365 } 366 366 #endif 367 367 368 + #if HZ > MSEC_PER_SEC || (MSEC_PER_SEC % HZ) 368 369 /** 369 370 * jiffies_to_msecs - Convert jiffies to milliseconds 370 371 * @j: jiffies value 371 - * 372 - * Avoid unnecessary multiplications/divisions in the 373 - * two most common HZ cases. 374 372 * 375 373 * Return: milliseconds value 376 374 */ 377 375 unsigned int jiffies_to_msecs(const unsigned long j) 378 376 { 379 - #if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ) 380 - return (MSEC_PER_SEC / HZ) * j; 381 - #elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) 377 + #if HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC) 382 378 return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC); 383 379 #else 384 380 # if BITS_PER_LONG == 32 ··· 386 390 #endif 387 391 } 388 392 EXPORT_SYMBOL(jiffies_to_msecs); 393 + #endif 389 394 395 + #if (USEC_PER_SEC % HZ) 390 396 /** 391 397 * jiffies_to_usecs - Convert jiffies to microseconds 392 398 * @j: jiffies value ··· 403 405 */ 404 406 BUILD_BUG_ON(HZ > USEC_PER_SEC); 405 407 406 - #if !(USEC_PER_SEC % HZ) 407 - return (USEC_PER_SEC / HZ) * j; 408 - #else 409 - # if BITS_PER_LONG == 32 408 + #if BITS_PER_LONG == 32 410 409 return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32; 411 - # else 410 + #else 412 411 return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN; 413 - # endif 414 412 #endif 415 413 } 416 414 EXPORT_SYMBOL(jiffies_to_usecs); 415 + #endif 417 416 418 417 /** 419 418 * mktime64 - Converts date to seconds.