Re: [PATCH] time/jiffies: inline jiffies_to_msecs() and jiffies_to_usecs()

From: Thomas Huth

Date: Thu Sep 17 2026 - 06:02:25 EST


On 10/02/2026 18.02, Eric Dumazet wrote:
For common cases (HZ=100, 250 or 1000), these helpers are
at most one multiply, there is no point calling a tiny function.

Keep them out of line for HZ=300 and others.

This saves cycles in TCP fast path, among other things.

$ scripts/bloat-o-meter -t vmlinux.old vmlinux.new
add/remove: 0/8 grow/shrink: 25/89 up/down: 530/-3474 (-2944)
...
nla_put_msecs 193 - -193
message_stats_print 2131 920 -1211
Total: Before=25365208, After=25362264, chg -0.01%

Signed-off-by: Eric Dumazet <edumazet@xxxxxxxxxx>
---
include/linux/jiffies.h | 40 ++++++++++++++++++++++++++++++++++++++--
kernel/time/time.c | 19 +++++++------------
2 files changed, 45 insertions(+), 14 deletions(-)

diff --git a/include/linux/jiffies.h b/include/linux/jiffies.h
index fdef2c155c27202ae8ba910f170a62521294dc36..d1c3d4941854a82c4571032adb41ca57d5faa8bd 100644
--- a/include/linux/jiffies.h
+++ b/include/linux/jiffies.h
@@ -434,8 +434,44 @@ extern unsigned long preset_lpj;
/*
* Convert various time units to each other:
*/
-extern unsigned int jiffies_to_msecs(const unsigned long j);
-extern unsigned int jiffies_to_usecs(const unsigned long j);
+
+#if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
+/**
+ * jiffies_to_msecs - Convert jiffies to milliseconds
+ * @j: jiffies value
+ *
+ * This inline version takes care of HZ in {100,250,1000}.
+ *
+ * Return: milliseconds value
+ */
+static inline unsigned int jiffies_to_msecs(const unsigned long j)
+{
+ return (MSEC_PER_SEC / HZ) * j;
+}
+#else
+unsigned int jiffies_to_msecs(const unsigned long j);
+#endif
+
+#if !(USEC_PER_SEC % HZ)
+/**
+ * jiffies_to_usecs - Convert jiffies to microseconds
+ * @j: jiffies value
+ *
+ * Return: microseconds value
+ */
Hi Eric!

This now causes warnings when doing "make htmldocs":

.../Documentation/driver-api/basics:42: .../kernel/time/time.c:387: WARNING: Duplicate C declaration, also defined at driver-api/basics:436.
Declaration is '.. c:function:: unsigned int jiffies_to_msecs (const unsigned long j)'. [duplicate_declaration.c]
.../Documentation/driver-api/basics:42: .../kernel/time/time.c:410: WARNING: Duplicate C declaration, also defined at driver-api/basics:453.
Declaration is '.. c:function:: unsigned int jiffies_to_usecs (const unsigned long j)'. [duplicate_declaration.c]

Any ideas how to fix that best? Maybe the comments in the header should be just normal comments, and the kerneldoc should just reside in time.c ?

Thanks,
Thomas