Skip to contentSkip to conversation

time/timecounter: inline timecounter_cyc2time()

PATCH Applied Eric Dumazet 7 participants 9 messages Nov 29, 2025 – Jan 21

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 time/timecounter: inline timecounter_cyc2time()

New network transport protocols want NIC drivers to get hwtstamps of all incoming packets, and possibly all outgoing packets.

Swift congestion control is used by good old TCP transport and is our primary need for timecounter_cyc2time(). This will be upstreamed soon.

This means timecounter_cyc2time() can be called more than 100 million times per second on a busy server.

Inlining timecounter_cyc2time() brings a 12 % improvement on a UDP receive stress test on a 100Gbit NIC.

Note that FDO, LTO, PGO are unable to magically help for this case, presumably because NIC drivers are almost exclusively shipped as modules.

Add an unlikely() around the cc_cyc2ns_backwards() case, even if FDO (when used) is able to take care of this optimization.

---
 include/linux/timecounter.h | 33 +++++++++++++++++++++++++++++++--
 kernel/time/timecounter.c   | 35 -----------------------------------
 2 files changed, 31 insertions(+), 37 deletions(-)
include/linux/timecounter.h +31 -2
index dce03a5cafb7cfe61bd83304ef49f7721735da64..de21312ebed0f41946304f95f05cc625ea7f2a68 100644
@@ -115,6 +115,16 @@ extern void timecounter_init(struct timecounter *tc,
  */
 extern u64 timecounter_read(struct timecounter *tc);
 
+/*
+ * This is like cyclecounter_cyc2ns(), but it is used for computing a
+ * time previous to the time stored in the cycle counter.
+ */
+static inline u64 cc_cyc2ns_backwards(const struct cyclecounter *cc,
+				      u64 cycles, u64 frac)
+{
+	return ((cycles * cc->mult) - frac) >> cc->shift;
+}
+
 /**
  * timecounter_cyc2time - convert a cycle counter to same
  *                        time base as values returned by
@@ -131,7 +141,26 @@ extern u64 timecounter_read(struct timecounter *tc);
  *
  * Returns: cycle counter converted to nanoseconds since the initial time stamp
  */
-extern u64 timecounter_cyc2time(const struct timecounter *tc,
-				u64 cycle_tstamp);
+static inline u64 timecounter_cyc2time(const struct timecounter *tc,
+				       u64 cycle_tstamp)
+{
+	const struct cyclecounter *cc = tc->cc;
+	u64 delta = (cycle_tstamp - tc->cycle_last) & cc->mask;
+	u64 nsec = tc->nsec, frac = tc->frac;
+
+	/*
+	 * Instead of always treating cycle_tstamp as more recent
+	 * than tc->cycle_last, detect when it is too far in the
+	 * future and treat it as old time stamp instead.
+	 */
+	if (unlikely(delta > cc->mask / 2)) {
+		delta = (tc->cycle_last - cycle_tstamp) & cc->mask;
+		nsec -= cc_cyc2ns_backwards(cc, delta, frac);
+	} else {
+		nsec += cyclecounter_cyc2ns(cc, delta, tc->mask, &frac);
+	}
+
+	return nsec;
+}
 
 #endif
kernel/time/timecounter.c +0 -35
index 3d2a354cfe1c1b205aa960a748a76f8dd94335e2..2e64dbb6302d71d8b092d92dda0b71c99cdc053d 100644
@@ -62,38 +62,3 @@ u64 timecounter_read(struct timecounter *tc)
 }
 EXPORT_SYMBOL_GPL(timecounter_read);
 
-/*
- * This is like cyclecounter_cyc2ns(), but it is used for computing a
- * time previous to the time stored in the cycle counter.
- */
-static u64 cc_cyc2ns_backwards(const struct cyclecounter *cc,
-			       u64 cycles, u64 mask, u64 frac)
-{
-	u64 ns = (u64) cycles;
-
-	ns = ((ns * cc->mult) - frac) >> cc->shift;
-
-	return ns;
-}
-
-u64 timecounter_cyc2time(const struct timecounter *tc,
-			 u64 cycle_tstamp)
-{
-	u64 delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
-	u64 nsec = tc->nsec, frac = tc->frac;
-
-	/*
-	 * Instead of always treating cycle_tstamp as more recent
-	 * than tc->cycle_last, detect when it is too far in the
-	 * future and treat it as old time stamp instead.
-	 */
-	if (delta > tc->cc->mask / 2) {
-		delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
-		nsec -= cc_cyc2ns_backwards(tc->cc, delta, tc->mask, frac);
-	} else {
-		nsec += cyclecounter_cyc2ns(tc->cc, delta, tc->mask, &frac);
-	}
-
-	return nsec;
-}
-EXPORT_SYMBOL_GPL(timecounter_cyc2time);
Signature · 1 line
2.52.0.107.ga0afd4fd5b-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Eric Dumazet

New network transport protocols want NIC drivers to get hwtstamps of all incoming packets, and possibly all outgoing packets.

Swift congestion control is used by good old TCP transport and is our primary need for timecounter_cyc2time(). This will be upstreamed soon.

This means timecounter_cyc2time() can be called more than 100 million times per second on a busy server.

Inlining timecounter_cyc2time() brings a 12 % improvement on a UDP receive stress test on a 100Gbit NIC.

Note that FDO, LTO, PGO are unable to magically help for this case, presumably because NIC drivers are almost exclusively shipped as modules.

Add an unlikely() around the cc_cyc2ns_backwards() case, even if FDO (when used) is able to take care of this optimization.

Quoted 6 lines
Reviewed-by
#2 lore raw
Quoted 29 lines · Willem de Bruijn
Quoted 19 lines · Eric Dumazet

New network transport protocols want NIC drivers to get hwtstamps of all incoming packets, and possibly all outgoing packets.

Swift congestion control is used by good old TCP transport and is our primary need for timecounter_cyc2time(). This will be upstreamed soon.

This means timecounter_cyc2time() can be called more than 100 million times per second on a busy server.

Inlining timecounter_cyc2time() brings a 12 % improvement on a UDP receive stress test on a 100Gbit NIC.

Note that FDO, LTO, PGO are unable to magically help for this case, presumably because NIC drivers are almost exclusively shipped as modules.

Add an unlikely() around the cc_cyc2ns_backwards() case, even if FDO (when used) is able to take care of this optimization.

Quoted 6 lines
merge bot Applied
#4 lore raw

[tip: timers/core] time/timecounter: Inline timecounter_cyc2time()

In reply to Eric Dumazet
The following commit has been merged into the timers/core branch of tip:

The following commit has been merged into the timers/core branch of tip:

Commit-ID:     4725344462362e2ce2645f354737a8ea4280fa57
Gitweb:        https://git.kernel.org/tip/4725344462362e2ce2645f354737a8ea4280fa57
Author:        Eric Dumazet <[email protected]>
AuthorDate:    Sat, 29 Nov 2025 09:57:40 
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Mon, 15 Dec 2025 20:16:49 +01:00

time/timecounter: Inline timecounter_cyc2time()

New network transport protocols want NIC drivers to get hardware timestamps of all incoming packets, and possibly all outgoing packets.

One example is the upcoming 'Swift congestion control' which is used by TCP transport and is the primary need for timecounter_cyc2time(). This means timecounter_cyc2time() can be called more than 100 million times per second on a busy server.

Inlining timecounter_cyc2time() brings a 12% improvement on a UDP receive stress test on a 100Gbit NIC.

Note that FDO, LTO, PGO are unable to magically help for this case, presumably because NIC drivers are almost exclusively shipped as modules.

Add an unlikely() around the cc_cyc2ns_backwards() case, even if FDO (when used) is able to take care of this optimization.

---
 include/linux/timecounter.h | 31 +++++++++++++++++++++++++++++--
 kernel/time/timecounter.c   | 35 -----------------------------------
 2 files changed, 29 insertions(+), 37 deletions(-)
include/linux/timecounter.h +29 -2
index dce03a5..7de6b35 100644
@@ -115,6 +115,15 @@ extern void timecounter_init(struct timecounter *tc,
  */
 extern u64 timecounter_read(struct timecounter *tc);
 
+/*
+ * This is like cyclecounter_cyc2ns(), but it is used for computing a
+ * time previous to the time stored in the cycle counter.
+ */
+static inline u64 cc_cyc2ns_backwards(const struct cyclecounter *cc, u64 cycles, u64 frac)
+{
+	return ((cycles * cc->mult) - frac) >> cc->shift;
+}
+
 /**
  * timecounter_cyc2time - convert a cycle counter to same
  *                        time base as values returned by
@@ -131,7 +140,25 @@ extern u64 timecounter_read(struct timecounter *tc);
  *
  * Returns: cycle counter converted to nanoseconds since the initial time stamp
  */
-extern u64 timecounter_cyc2time(const struct timecounter *tc,
-				u64 cycle_tstamp);
+static inline u64 timecounter_cyc2time(const struct timecounter *tc, u64 cycle_tstamp)
+{
+	const struct cyclecounter *cc = tc->cc;
+	u64 delta = (cycle_tstamp - tc->cycle_last) & cc->mask;
+	u64 nsec = tc->nsec, frac = tc->frac;
+
+	/*
+	 * Instead of always treating cycle_tstamp as more recent than
+	 * tc->cycle_last, detect when it is too far in the future and
+	 * treat it as old time stamp instead.
+	 */
+	if (unlikely(delta > cc->mask / 2)) {
+		delta = (tc->cycle_last - cycle_tstamp) & cc->mask;
+		nsec -= cc_cyc2ns_backwards(cc, delta, frac);
+	} else {
+		nsec += cyclecounter_cyc2ns(cc, delta, tc->mask, &frac);
+	}
+
+	return nsec;
+}
 
 #endif
kernel/time/timecounter.c +0 -35
index 3d2a354..2e64dbb 100644
@@ -62,38 +62,3 @@ u64 timecounter_read(struct timecounter *tc)
 }
 EXPORT_SYMBOL_GPL(timecounter_read);
 
-/*
- * This is like cyclecounter_cyc2ns(), but it is used for computing a
- * time previous to the time stored in the cycle counter.
- */
-static u64 cc_cyc2ns_backwards(const struct cyclecounter *cc,
-			       u64 cycles, u64 mask, u64 frac)
-{
-	u64 ns = (u64) cycles;
-
-	ns = ((ns * cc->mult) - frac) >> cc->shift;
-
-	return ns;
-}
-
-u64 timecounter_cyc2time(const struct timecounter *tc,
-			 u64 cycle_tstamp)
-{
-	u64 delta = (cycle_tstamp - tc->cycle_last) & tc->cc->mask;
-	u64 nsec = tc->nsec, frac = tc->frac;
-
-	/*
-	 * Instead of always treating cycle_tstamp as more recent
-	 * than tc->cycle_last, detect when it is too far in the
-	 * future and treat it as old time stamp instead.
-	 */
-	if (delta > tc->cc->mask / 2) {
-		delta = (tc->cycle_last - cycle_tstamp) & tc->cc->mask;
-		nsec -= cc_cyc2ns_backwards(tc->cc, delta, tc->mask, frac);
-	} else {
-		nsec += cyclecounter_cyc2ns(tc->cc, delta, tc->mask, &frac);
-	}
-
-	return nsec;
-}
-EXPORT_SYMBOL_GPL(timecounter_cyc2time);
#5 lore raw
In reply to Eric Dumazet
Quoted 20 lines · Eric Dumazet

New network transport protocols want NIC drivers to get hwtstamps of all incoming packets, and possibly all outgoing packets.

Swift congestion control is used by good old TCP transport and is our primary need for timecounter_cyc2time(). This will be upstreamed soon.

This means timecounter_cyc2time() can be called more than 100 million times per second on a busy server.

Inlining timecounter_cyc2time() brings a 12 % improvement on a UDP receive stress test on a 100Gbit NIC.

Note that FDO, LTO, PGO are unable to magically help for this case, presumably because NIC drivers are almost exclusively shipped as modules.

Add an unlikely() around the cc_cyc2ns_backwards() case, even if FDO (when used) is able to take care of this optimization.

Quoted 6 lines

When I'm browsing the modification related to SWIFT, I noticed this patch seems to have not been merged yet?

Thanks,
Jason

maintainer
#6 lore raw
Quoted 4 lines · Jason Xing

On Sat, Nov 29, 2025 at 5:57 PM Eric Dumazet <[email protected]> wrote:

When I'm browsing the modification related to SWIFT, I noticed this patch seems to have not been merged yet?

It's in the tip tree and scheduled for the next merge window.

#7 lore raw
Quoted 8 lines · Thomas Gleixner
Quoted 4 lines · Jason Xing

On Sat, Nov 29, 2025 at 5:57 PM Eric Dumazet <[email protected]> wrote:

When I'm browsing the modification related to SWIFT, I noticed this patch seems to have not been merged yet?

It's in the tip tree and scheduled for the next merge window.

You beat me to it, thank you

tip tree :

commit 4725344462362e2ce2645f354737a8ea4280fa57

Author:     Eric Dumazet <[email protected]>
AuthorDate: Sat Nov 29 09:57:40 2025 +0000
Commit:     Thomas Gleixner <[email protected]>
CommitDate: Mon Dec 15 20:16:49 2025 +0100

time/timecounter: Inline timecounter_cyc2time()

#8 lore raw
Quoted 15 lines · Eric Dumazet
Quoted 8 lines · Thomas Gleixner
Quoted 4 lines · Jason Xing

On Sat, Nov 29, 2025 at 5:57 PM Eric Dumazet <[email protected]> wrote:

When I'm browsing the modification related to SWIFT, I noticed this patch seems to have not been merged yet?

It's in the tip tree and scheduled for the next merge window.

You beat me to it, thank you

tip tree :

Quoted 7 lines

commit 4725344462362e2ce2645f354737a8ea4280fa57

Author:     Eric Dumazet <[email protected]>
AuthorDate: Sat Nov 29 09:57:40 2025 +0000
Commit:     Thomas Gleixner <[email protected]>
CommitDate: Mon Dec 15 20:16:49 2025 +0100

time/timecounter: Inline timecounter_cyc2time()

Thanks, I see now :)

Thanks,
Jason