* [PATCH] time/jiffies: inline jiffies_to_msecs() and jiffies_to_usecs()
@ 2026-02-10 17:02 Eric Dumazet
2026-02-11 8:00 ` [tip: timers/urgent] time/jiffies: Inline " tip-bot2 for Eric Dumazet
2026-09-17 9:58 ` [PATCH] time/jiffies: inline " Thomas Huth
0 siblings, 2 replies; 3+ messages in thread
From: Eric Dumazet @ 2026-02-10 17:02 UTC (permalink / raw)
To: Andrew Morton, John Stultz, Thomas Gleixner, Stephen Boyd
Cc: linux-kernel, Eric Dumazet, Eric Dumazet
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@google.com>
---
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
+ */
+static inline unsigned int jiffies_to_usecs(const unsigned long j)
+{
+ /*
+ * Hz usually doesn't go much further MSEC_PER_SEC.
+ * jiffies_to_usecs() and usecs_to_jiffies() depend on that.
+ */
+ BUILD_BUG_ON(HZ > USEC_PER_SEC);
+
+ return (USEC_PER_SEC / HZ) * j;
+}
+#else
+unsigned int jiffies_to_usecs(const unsigned long j);
+#endif
/**
* jiffies_to_nsecs - Convert jiffies to nanoseconds
diff --git a/kernel/time/time.c b/kernel/time/time.c
index 0ba8e3c50d62570d7248dd02a80395cb0c51c42e..36fd2313ae7eef5686722687008750981646a9e0 100644
--- a/kernel/time/time.c
+++ b/kernel/time/time.c
@@ -365,20 +365,16 @@ SYSCALL_DEFINE1(adjtimex_time32, struct old_timex32 __user *, utp)
}
#endif
+#if HZ > MSEC_PER_SEC || (MSEC_PER_SEC % HZ)
/**
* jiffies_to_msecs - Convert jiffies to milliseconds
* @j: jiffies value
*
- * Avoid unnecessary multiplications/divisions in the
- * two most common HZ cases.
- *
* Return: milliseconds value
*/
unsigned int jiffies_to_msecs(const unsigned long j)
{
-#if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
- return (MSEC_PER_SEC / HZ) * j;
-#elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
+#if HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
#else
# if BITS_PER_LONG == 32
@@ -390,7 +386,9 @@ unsigned int jiffies_to_msecs(const unsigned long j)
#endif
}
EXPORT_SYMBOL(jiffies_to_msecs);
+#endif
+#if (USEC_PER_SEC % HZ)
/**
* jiffies_to_usecs - Convert jiffies to microseconds
* @j: jiffies value
@@ -405,17 +403,14 @@ unsigned int jiffies_to_usecs(const unsigned long j)
*/
BUILD_BUG_ON(HZ > USEC_PER_SEC);
-#if !(USEC_PER_SEC % HZ)
- return (USEC_PER_SEC / HZ) * j;
-#else
-# if BITS_PER_LONG == 32
+#if BITS_PER_LONG == 32
return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
-# else
+#else
return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
-# endif
#endif
}
EXPORT_SYMBOL(jiffies_to_usecs);
+#endif
/**
* mktime64 - Converts date to seconds.
--
2.53.0.rc2.204.g2597b5adb4-goog
^ permalink raw reply [flat|nested] 3+ messages in thread* [tip: timers/urgent] time/jiffies: Inline jiffies_to_msecs() and jiffies_to_usecs()
2026-02-10 17:02 [PATCH] time/jiffies: inline jiffies_to_msecs() and jiffies_to_usecs() Eric Dumazet
@ 2026-02-11 8:00 ` tip-bot2 for Eric Dumazet
2026-09-17 9:58 ` [PATCH] time/jiffies: inline " Thomas Huth
1 sibling, 0 replies; 3+ messages in thread
From: tip-bot2 for Eric Dumazet @ 2026-02-11 8:00 UTC (permalink / raw)
To: linux-tip-commits; +Cc: Eric Dumazet, Thomas Gleixner, x86, linux-kernel
The following commit has been merged into the timers/urgent branch of tip:
Commit-ID: b777b5e09eabeefc6ba80f4296366a4742701103
Gitweb: https://git.kernel.org/tip/b777b5e09eabeefc6ba80f4296366a4742701103
Author: Eric Dumazet <edumazet@google.com>
AuthorDate: Tue, 10 Feb 2026 17:02:25
Committer: Thomas Gleixner <tglx@kernel.org>
CommitterDate: Wed, 11 Feb 2026 08:55:52 +01:00
time/jiffies: Inline jiffies_to_msecs() and jiffies_to_usecs()
For common cases (HZ=100, 250 or 1000), these helpers are at most one
multiply, so 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@google.com>
Signed-off-by: Thomas Gleixner <tglx@kernel.org>
Link: https://patch.msgid.link/20260210170226.57209-1-edumazet@google.com
---
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 fdef2c1..d1c3d49 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
+ */
+static inline unsigned int jiffies_to_usecs(const unsigned long j)
+{
+ /*
+ * Hz usually doesn't go much further MSEC_PER_SEC.
+ * jiffies_to_usecs() and usecs_to_jiffies() depend on that.
+ */
+ BUILD_BUG_ON(HZ > USEC_PER_SEC);
+
+ return (USEC_PER_SEC / HZ) * j;
+}
+#else
+unsigned int jiffies_to_usecs(const unsigned long j);
+#endif
/**
* jiffies_to_nsecs - Convert jiffies to nanoseconds
diff --git a/kernel/time/time.c b/kernel/time/time.c
index 0ba8e3c..36fd231 100644
--- a/kernel/time/time.c
+++ b/kernel/time/time.c
@@ -365,20 +365,16 @@ SYSCALL_DEFINE1(adjtimex_time32, struct old_timex32 __user *, utp)
}
#endif
+#if HZ > MSEC_PER_SEC || (MSEC_PER_SEC % HZ)
/**
* jiffies_to_msecs - Convert jiffies to milliseconds
* @j: jiffies value
*
- * Avoid unnecessary multiplications/divisions in the
- * two most common HZ cases.
- *
* Return: milliseconds value
*/
unsigned int jiffies_to_msecs(const unsigned long j)
{
-#if HZ <= MSEC_PER_SEC && !(MSEC_PER_SEC % HZ)
- return (MSEC_PER_SEC / HZ) * j;
-#elif HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
+#if HZ > MSEC_PER_SEC && !(HZ % MSEC_PER_SEC)
return (j + (HZ / MSEC_PER_SEC) - 1)/(HZ / MSEC_PER_SEC);
#else
# if BITS_PER_LONG == 32
@@ -390,7 +386,9 @@ unsigned int jiffies_to_msecs(const unsigned long j)
#endif
}
EXPORT_SYMBOL(jiffies_to_msecs);
+#endif
+#if (USEC_PER_SEC % HZ)
/**
* jiffies_to_usecs - Convert jiffies to microseconds
* @j: jiffies value
@@ -405,17 +403,14 @@ unsigned int jiffies_to_usecs(const unsigned long j)
*/
BUILD_BUG_ON(HZ > USEC_PER_SEC);
-#if !(USEC_PER_SEC % HZ)
- return (USEC_PER_SEC / HZ) * j;
-#else
-# if BITS_PER_LONG == 32
+#if BITS_PER_LONG == 32
return (HZ_TO_USEC_MUL32 * j) >> HZ_TO_USEC_SHR32;
-# else
+#else
return (j * HZ_TO_USEC_NUM) / HZ_TO_USEC_DEN;
-# endif
#endif
}
EXPORT_SYMBOL(jiffies_to_usecs);
+#endif
/**
* mktime64 - Converts date to seconds.
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] time/jiffies: inline jiffies_to_msecs() and jiffies_to_usecs()
2026-02-10 17:02 [PATCH] time/jiffies: inline jiffies_to_msecs() and jiffies_to_usecs() Eric Dumazet
2026-02-11 8:00 ` [tip: timers/urgent] time/jiffies: Inline " tip-bot2 for Eric Dumazet
@ 2026-09-17 9:58 ` Thomas Huth
1 sibling, 0 replies; 3+ messages in thread
From: Thomas Huth @ 2026-09-17 9:58 UTC (permalink / raw)
To: Eric Dumazet
Cc: linux-kernel, Eric Dumazet, Andrew Morton, Thomas Gleixner,
John Stultz, Stephen Boyd
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@google.com>
> ---
> 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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-09-17 9:58 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-02-10 17:02 [PATCH] time/jiffies: inline jiffies_to_msecs() and jiffies_to_usecs() Eric Dumazet
2026-02-11 8:00 ` [tip: timers/urgent] time/jiffies: Inline " tip-bot2 for Eric Dumazet
2026-09-17 9:58 ` [PATCH] time/jiffies: inline " Thomas Huth
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®