Skip to contentSkip to conversation

tcp: avoid integer wraps in the RTT estimator and the RTO

PATCH Applied Chris Mason 5 participants 15 messages Nov 18 – Nov 19

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

v2 cover tcp: avoid integer wraps in the RTT estimator and the RTO

Hi everyone,

This is v2 of the RTT clamps, with Eric's review folded in.

Patch 1: __tcp_set_rto() never returns 0, so that the backoff recovers from any state of the RTT estimator.

Patch 2: tcp_rtt_estimator() clamps each sample to 120 s, so that srtt_us, mdev_us, rttvar_us and the sum in __tcp_set_rto() can't wrap.

Patch 3: tcp_rtt_tsopt_us() rejects a TSecr sample above 120 s.

Patch 4: new, from Eric's review. tcp_rcv_rtt_update() clamps its samples too, which covers tcp_rcv_rtt_measure().

Patch 5: new. tcp_rto_min() bounds the locked rto_min of a route.

Eric, I have a question on patch 5. You pointed at a locked rto_min of 0 for patch 1. We can also get an RTO of 0 out of tcp_init_metrics(), which doesn't go through __tcp_set_rto(), so I bounded the metric in tcp_rto_min(). For a locked cwnd of 0 you wanted ip_metrics_convert() to reject it instead.

I didn't do that here because claude points out that "rto_min lock 0" is accepted today and mostly works, but just let me know how you want this to look.

Changes since v1:

- Patch 1: no code change. New changelog and Fixes: tag (Eric). David suggested "return rto ?: 1;". I kept max(), since tcp_rtt_estimator() ends with max(1U, srtt).
- Patch 2: clamp mrtt_us itself, so that tcp_bpf_rtt() gets the clamped sample (Eric).
- Patch 3: no code change. The part on sample << 3 moved to patch 4.
- Patch 4: new (Eric), with min() instead of min_t() (David).
- Patch 5: new.

v1: https://lore.kernel.org/all/[email protected]/

This is on top of v7.4-rc3.

Chris Mason (5):
tcp: don't let __tcp_set_rto() return 0
tcp: clamp RTT samples in tcp_rtt_estimator()
tcp: reject RTT samples from TSecr above 120 s
tcp: clamp the samples in tcp_rcv_rtt_update()
tcp: bound the rto_min of a route in tcp_rto_min()

 include/net/tcp.h    | 14 ++++++++++++--
 net/ipv4/tcp_input.c | 21 +++++++++++++++------
 2 files changed, 27 insertions(+), 8 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH v2 1/5 tcp: don't let __tcp_set_rto() return 0

tcp_retransmit_timer() backs off by doubling icsk_rto, so the backoff only works if icsk_rto is nonzero.

__tcp_set_rto() adds srtt_us >> 3 and rttvar_us with no lower bound, and relies on tcp_rtt_estimator() to keep rttvar_us at tcp_rto_min() or above. But tcp_rto_min() returns 0 for a route with a locked RTAX_RTO_MIN of 0. A first RTT sample of 0 usec, which usec TS allows, then gives:

srtt_us = max(1U, 0 << 3) = 1
rttvar_us = max(mdev_us, tcp_rto_min_us(sk)) = 0
__tcp_set_rto() = usecs_to_jiffies((1 >> 3) + 0) = 0

Keep the result from 0, so that the backoff recovers from any state of the RTT estimator.

Do it in __tcp_set_rto() and not in tcp_set_rto(), since tcp_ld_RTO_revert() uses the same sum. The third caller, the linear timeouts of thin streams in tcp_retransmit_timer(), already clamps the result to tcp_rto_min().

Found by code inspection (Eric Dumazet), not reproduced. Fixed by LLM, and tested with existing kernel selftests.

Fixes 05bb1fad1cde ("[TCP]: Allow minimum RTO to be configurable via routing metrics.")

---

Notes:

    v1->v2: no code change, new Fixes: tag (Eric), changelog updates
 include/net/tcp.h    | 4 +++-
 net/ipv4/tcp_input.c | 5 +++--
 2 files changed, 6 insertions(+), 3 deletions(-)
include/net/tcp.h +3 -1
index 5c9203aae834..6a714189bcc7 100644
@@ -910,7 +910,9 @@ static inline void tcp_bound_rto(struct sock *sk)
 
 static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
 {
-	return usecs_to_jiffies((tp->srtt_us >> 3) + tp->rttvar_us);
+	u32 rto = usecs_to_jiffies((tp->srtt_us >> 3) + tp->rttvar_us);
+
+	return max(rto, 1U);
 }
 
 static inline unsigned long tcp_reqsk_timeout(struct request_sock *req)
net/ipv4/tcp_input.c +3 -2
index a2074a1af422..f65697bc96c9 100644
@@ -1221,8 +1221,9 @@ void tcp_set_rto(struct sock *sk)
 	 *    with correct one. It is exactly, which we pretend to do.
 	 */
 
-	/* NOTE: clamping at TCP_RTO_MIN is not required, current algo
-	 * guarantees that rto is higher.
+	/* NOTE: clamping at tcp_rto_min() is not required, tcp_rtt_estimator()
+	 * applies it to rttvar_us. But rto must never be 0, which exponential
+	 * backoff could not leave: __tcp_set_rto() takes care of that.
 	 */
 	tcp_bound_rto(sk);
 }
Signature · 1 line
2.55.0
#3 lore raw

PATCH v2 2/5 tcp: clamp RTT samples in tcp_rtt_estimator()

In reply to Chris Mason

tcp_rtt_estimator() keeps its state in u32 fields, in microseconds. srtt_us holds 8 times the smoothed RTT, so it wraps at an RTT of 536.9 s. Samples can be larger than that:

- For an ACK of retransmitted data, the sample is the timestamp clock minus TSecr, and tcp_rtt_tsopt_us() accepts a sample of up to 2,147 s.

- For a SACK of a segment that was never retransmitted, the sample is the age of that segment.

Samples from TSecr could wrap these fields when they still held jiffies. The age of a segment can wrap them since commit 740b0f1841f6 ("tcp:
switch rtt estimations to usec resolution"), which took the wrap of the smoothed RTT from 2^29 jiffies to 2^29 us.

The fix used here is to clamp each sample to 120 s, the largest possible RTO, in tcp_rtt_estimator(), where the samples of all sources meet.

The smoothed RTT now saturates at 120 s. A path with a larger real RTT sees that in TCP_INFO, in the pacing rate and in the metrics cache. BPF programs on the RTT callback get the clamped sample too.

Found by code inspection (Eric Dumazet), and confirmed with LLM generated reproducers on an unmodified kernel. Fixed by LLM, and tested with the reproducers and existing kernel selftests.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes 740b0f1841f6 ("tcp: switch rtt estimations to usec resolution")

---

Notes:

    v1->v2: clamp mrtt_us itself, so BPF gets the clamped sample (Eric)
 include/net/tcp.h    | 7 +++++++
 net/ipv4/tcp_input.c | 5 ++++-
 2 files changed, 11 insertions(+), 1 deletion(-)
include/net/tcp.h +7 -0
index 6a714189bcc7..a617a72ee2c6 100644
@@ -164,6 +164,13 @@ static_assert((1 << ATO_BITS) > TCP_DELACK_MAX);
 
 #define TCP_TIMEOUT_MIN_US (2*USEC_PER_MSEC) /* Min TCP timeout in microsecs */
 
+/* Max RTT sample for tcp_rtt_estimator(). srtt_us holds 8 times the RTT, and
+ * mdev_us and rttvar_us up to 4 times its deviation, all in u32, like the sum
+ * in __tcp_set_rto(). A larger sample could not raise the RTO, which
+ * TCP_RTO_MAX bounds.
+ */
+#define TCP_RTT_MAX_US	(TCP_RTO_MAX_SEC * USEC_PER_SEC)
+
 #define TCP_TIMEOUT_INIT ((unsigned)(1*HZ))	/* RFC6298 2.1 initial RTO value	*/
 #define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ))	/* RFC 1122 initial RTO value, now
 						 * used as a fallback RTO for the
net/ipv4/tcp_input.c +4 -1
index f65697bc96c9..e796497b84be 100644
@@ -1098,8 +1098,11 @@ static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)
 static void tcp_rtt_estimator(struct sock *sk, long mrtt_us)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
-	long m = mrtt_us; /* RTT */
 	u32 srtt = tp->srtt_us;
+	long m; /* RTT */
+
+	mrtt_us = min(mrtt_us, TCP_RTT_MAX_US);
+	m = mrtt_us;
 
 	/*	The following amusing code comes from Jacobson's
 	 *	article in SIGCOMM '88.  Note that rtt and mdev
Signature · 1 line
2.55.0
#4 lore raw

PATCH v2 3/5 tcp: reject RTT samples from TSecr above 120 s

In reply to Chris Mason

tcp_rtt_tsopt_us() returns the timestamp clock minus TSecr as an RTT sample, and accepts up to 2,147 s. If TSecr echoes an old timestamp, the sample is the age of that timestamp, not an RTT.

tcp_ack_update_rtt() takes this sample for an ACK of retransmitted data. tcp_rtt_estimator() clamps it to 120 s. The congestion control gets the sample unclamped, in rs->rtt_us.

tcp_rcv_rtt_measure_ts() takes the same sample for the RTT estimate of the receive side.

The fix used here is to reject a sample from TSecr above 120 s, for both callers of tcp_rtt_tsopt_us().

An ACK of retransmitted data whose TSecr is more than 120 s old now gives no RTT sample. So it no longer resets the backoff, as on a connection without timestamps.

Found by LLM code inspection, and confirmed with LLM generated reproducers on an unmodified kernel. Fixed by LLM, and tested with the reproducers and existing kernel selftests.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

---

Notes:

    v1->v2: no code change, the part on sample << 3 moved to patch 4
 net/ipv4/tcp_input.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
net/ipv4/tcp_input.c +3 -2
index e796497b84be..69e038b9389a 100644
@@ -901,15 +901,16 @@ static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)
 	tp->rcv_rtt_est.time = tp->tcp_mstamp;
 }
 
+/* A sample from TSecr above TCP_RTT_MAX_US is bogus, not large. */
 static s32 tcp_rtt_tsopt_us(const struct tcp_sock *tp, u32 min_delta)
 {
 	u32 delta, delta_us;
 
 	delta = tcp_time_stamp_ts(tp) - tp->rx_opt.rcv_tsecr;
 	if (tp->tcp_usec_ts)
-		return delta;
+		return delta <= TCP_RTT_MAX_US ? delta : -1;
 
-	if (likely(delta < INT_MAX / (USEC_PER_SEC / TCP_TS_HZ))) {
+	if (likely(delta <= TCP_RTT_MAX_US / (USEC_PER_SEC / TCP_TS_HZ))) {
 		if (!delta)
 			delta = min_delta;
 		delta_us = delta * (USEC_PER_SEC / TCP_TS_HZ);
Signature · 1 line
2.55.0
#7 lore raw

PATCH v2 4/5 tcp: clamp the samples in tcp_rcv_rtt_update()

In reply to Chris Mason

tcp_rcv_rtt_update() starts with sample << 3 in u32 arithmetic, so a sample of 536.9 s or more wraps.

tcp_rcv_rtt_measure() can hand it one. Its sample is the time the sender took to fill one receive window, and a low rate flow can take longer than that. This caller only looks for a minimum, so the wrapped value is kept whenever it is below the current estimate. A 540 s sample ends up as an estimate of about 3.1 s.

Clamp the sample to TCP_RTT_MAX_US in tcp_rcv_rtt_update(), like tcp_rtt_estimator() does on the send side. That covers both callers, and the smoothed estimate now tops out at 8 * 120 s, which fits in a u32.

Found by code inspection (Eric Dumazet), who also suggested the fix. Confirmed with an LLM generated reproducer on an unmodified kernel, and tested with the reproducer and existing kernel selftests.

Fixes 645f4c6f2ebd ("tcp: switch rcv_rtt_est and rcvq_space to high resolution timestamps")

---

Notes:

    v1->v2: new patch (Eric), with min() instead of min_t() (David)
 net/ipv4/tcp_input.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
net/ipv4/tcp_input.c +5 -1
index 69e038b9389a..d9a66a1342ef 100644
@@ -857,7 +857,11 @@ void tcp_initialize_rcv_mss(struct sock *sk)
 static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)
 {
 	u32 new_sample, old_sample = tp->rcv_rtt_est.rtt_us;
-	long m = sample << 3;
+	long m;
+
+	/* Keep sample << 3 and the smoothed estimate within u32. */
+	sample = min(sample, TCP_RTT_MAX_US);
+	m = sample << 3;
 
 	if (old_sample == 0 || m < old_sample) {
 		new_sample = m;
Signature · 1 line
2.55.0
#12 lore raw

PATCH v2 5/5 tcp: bound the rto_min of a route in tcp_rto_min()

In reply to Chris Mason

For a route with a locked RTAX_RTO_MIN, tcp_rto_min() returns the metric as it is, and that can be any 32-bit number of milliseconds. All other ways to set the minimum RTO have a nonzero floor: the tcp_rto_min_us sysctl, TCP_RTO_MIN_US and TCP_BPF_RTO_MIN.

Both ends of that range give us trouble.

With an rto_min of 0, tcp_init_metrics() can start a connection with an RTO of 0, if the cached RTT is below one jiffy:

    icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk)) = 0

It sets icsk_rto itself, so the lower bound in __tcp_set_rto() does not help here.

At the other end, tcp_rto_min_us() and the sum in __tcp_set_rto() are u32 values in usec, so an rto_min of more than about 71 minutes does not fit.

Clamp a locked rto_min to the range from one jiffy to TCP_RTO_MAX in tcp_rto_min().

ip_metrics_convert() could reject these values instead, as it does for a locked cwnd of 0. But a route with "rto_min lock 0" is accepted today and mostly works, so that would break existing setups.

A route still shows the rto_min that it was given, while TCP uses one jiffy or 120 s.

Found by LLM code inspection, and confirmed with LLM generated reproducers on an unmodified kernel. Fixed by LLM, and tested with the reproducers and existing kernel selftests.

Fixes 05bb1fad1cde ("[TCP]: Allow minimum RTO to be configurable via routing metrics.")

---

Notes:

    v1->v2: new patch
 include/net/tcp.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/net/tcp.h +2 -1
index a617a72ee2c6..27de546dc802 100644
@@ -939,7 +939,8 @@ static inline u32 tcp_rto_min(const struct sock *sk)
 	u32 rto_min = READ_ONCE(inet_csk(sk)->icsk_rto_min);
 
 	if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
-		rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
+		rto_min = clamp(dst_metric_rtt(dst, RTAX_RTO_MIN),
+				1, TCP_RTO_MAX);
 	return rto_min;
 }
 
Signature · 1 line
2.55.0
maintainer Reviewed-by on v2 5/5
#13 lore raw

Re: [PATCH net v2 5/5] tcp: bound the rto_min of a route in tcp_rto_min()

Quoted 20 lines · Chris Mason

For a route with a locked RTAX_RTO_MIN, tcp_rto_min() returns the metric as it is, and that can be any 32-bit number of milliseconds. All other ways to set the minimum RTO have a nonzero floor: the tcp_rto_min_us sysctl, TCP_RTO_MIN_US and TCP_BPF_RTO_MIN.

Both ends of that range give us trouble.

With an rto_min of 0, tcp_init_metrics() can start a connection with an RTO of 0, if the cached RTT is below one jiffy:

    icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk)) = 0

It sets icsk_rto itself, so the lower bound in __tcp_set_rto() does not help here.

At the other end, tcp_rto_min_us() and the sum in __tcp_set_rto() are u32 values in usec, so an rto_min of more than about 71 minutes does not fit.

Quoted 7 lines

Clamp a locked rto_min to the range from one jiffy to TCP_RTO_MAX in tcp_rto_min().

ip_metrics_convert() could reject these values instead, as it does for a locked cwnd of 0. But a route with "rto_min lock 0" is accepted today and mostly works, so that would break existing setups.

I agree, please keep the clamp in tcp_rto_min().

A locked cwnd of 0 makes no sense, but "rto_min lock 0" can be read as "as low as possible", and we can not start rejecting it now.

Quoted 34 lines

A route still shows the rto_min that it was given, while TCP uses one jiffy or 120 s.

Found by LLM code inspection, and confirmed with LLM generated reproducers on an unmodified kernel. Fixed by LLM, and tested with the reproducers and existing kernel selftests.

Fixes 05bb1fad1cde ("[TCP]: Allow minimum RTO to be configurable via routing metrics.")

---

Notes:

    v1->v2: new patch
 include/net/tcp.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/net/tcp.h +2 -2
index a617a72ee2c6..27de546dc802 100644
@@ -939,7 +939,8 @@ static inline u32 tcp_rto_min(const struct sock *sk)
        u32 rto_min = READ_ONCE(inet_csk(sk)->icsk_rto_min);
 
        if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
-               rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
+               rto_min = clamp(dst_metric_rtt(dst, RTAX_RTO_MIN),
+                               1, TCP_RTO_MAX);
        return rto_min;
 }
 
--

2.55.0

maintainer Reviewed-by on v2 4/5
#8 lore raw

Re: [PATCH net v2 4/5] tcp: clamp the samples in tcp_rcv_rtt_update()

In reply to Chris Mason
Quoted 18 lines · Chris Mason

tcp_rcv_rtt_update() starts with sample << 3 in u32 arithmetic, so a sample of 536.9 s or more wraps.

tcp_rcv_rtt_measure() can hand it one. Its sample is the time the sender took to fill one receive window, and a low rate flow can take longer than that. This caller only looks for a minimum, so the wrapped value is kept whenever it is below the current estimate. A 540 s sample ends up as an estimate of about 3.1 s.

Clamp the sample to TCP_RTT_MAX_US in tcp_rcv_rtt_update(), like tcp_rtt_estimator() does on the send side. That covers both callers, and the smoothed estimate now tops out at 8 * 120 s, which fits in a u32.

Found by code inspection (Eric Dumazet), who also suggested the fix. Confirmed with an LLM generated reproducer on an unmodified kernel, and tested with the reproducer and existing kernel selftests.

Quoted 7 lines
Fixes 645f4c6f2ebd ("tcp: switch rcv_rtt_est and rcvq_space to high resolution timestamps")

---

Notes:

    v1->v2: new patch (Eric), with min() instead of min_t() (David)
maintainer Reviewed-by on v2 3/5
#5 lore raw

Re: [PATCH net v2 3/5] tcp: reject RTT samples from TSecr above 120 s

In reply to Chris Mason
Quoted 19 lines · Chris Mason

tcp_rtt_tsopt_us() returns the timestamp clock minus TSecr as an RTT sample, and accepts up to 2,147 s. If TSecr echoes an old timestamp, the sample is the age of that timestamp, not an RTT.

tcp_ack_update_rtt() takes this sample for an ACK of retransmitted data. tcp_rtt_estimator() clamps it to 120 s. The congestion control gets the sample unclamped, in rs->rtt_us.

tcp_rcv_rtt_measure_ts() takes the same sample for the RTT estimate of the receive side.

The fix used here is to reject a sample from TSecr above 120 s, for both callers of tcp_rtt_tsopt_us().

An ACK of retransmitted data whose TSecr is more than 120 s old now gives no RTT sample. So it no longer resets the backoff, as on a connection without timestamps.

Quoted 6 lines

Found by LLM code inspection, and confirmed with LLM generated reproducers on an unmodified kernel. Fixed by LLM, and tested with the reproducers and existing kernel selftests.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
#2 lore raw

Re: [PATCH net v2 1/5] tcp: don't let __tcp_set_rto() return 0

In reply to Chris Mason
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message of "tcp: don't let __tcp_set_rto() return 0" gets the thin-stream caller wrong.

--

tcp: don't let __tcp_set_rto() return 0

This commit makes __tcp_set_rto() return at least 1 jiffy. Without that floor, a route with a locked RTAX_RTO_MIN of 0 and an RTT sum under one jiffy leaves icsk_rto at 0, and the exponential backoff in tcp_retransmit_timer() can never get it above 0. Because the floor is in __tcp_set_rto(), both tcp_set_rto() and tcp_ld_RTO_revert() get it.

Quoted 4 lines

Do it in __tcp_set_rto() and not in tcp_set_rto(), since tcp_ld_RTO_revert() uses the same sum. The third caller, the linear timeouts of thin streams in tcp_retransmit_timer(), already clamps the result to tcp_rto_min().

[Severity: Low]
Does this describe the thin stream caller correctly for the configuration this patch targets?

The trigger here is tcp_rto_min() returning 0 for a route with a locked RTAX_RTO_MIN of 0. At this commit, tcp_rto_min() passes the route metric through with no lower bound:

include/net/tcp.h:tcp_rto_min() {
    ...
	if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
		rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
	return rto_min;
}

Before this patch, the thin stream branch in tcp_retransmit_timer() was:

net/ipv4/tcp_timer.c:tcp_retransmit_timer() {
    ...
		icsk->icsk_rto = clamp(__tcp_set_rto(tp),
				       tcp_rto_min(sk),
				       tcp_rto_max(sk));
    ...
}

This gives clamp(0, 0, rto_max) == 0 whenever (srtt_us >> 3) + rttvar_us is below one jiffy. icsk_rto then stays at 0 for up to TCP_THIN_LINEAR_RETRIES retransmissions. After that, the exponential branch (icsk_rto << 1) keeps it at 0.

The code change fixes this path as well, because the floor is in __tcp_set_rto(). Only the rationale looks wrong.

Could the commit message say that the thin stream path is also affected and fixed by this change, instead of saying it is already protected?

The later commit "tcp: bound the rto_min of a route in tcp_rto_min()" clamps tcp_rto_min() to at least 1. That makes the statement true once the whole series is applied. However, this patch has its own Fixes: tag and may be backported on its own.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118155144.18393-1-mason%40kernel.org
#6 lore raw

Re: [PATCH net v2 3/5] tcp: reject RTT samples from TSecr above 120 s

In reply to Chris Mason
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The header comment for TCP_RTT_MAX_US is out of date after this patch.

--

tcp: reject RTT samples from TSecr above 120 s

This patch makes tcp_rtt_tsopt_us() reject any TSecr-derived RTT sample above TCP_RTT_MAX_US (120 s), in both the usec and ms timestamp branches. Before, samples of up to about 2,147 s were accepted. Both callers, tcp_ack_update_rtt() and tcp_rcv_rtt_measure_ts(), now get no sample when TSecr echoes a very old timestamp.

Quoted 10 lines
net/ipv4/tcp_input.c +1 -0
index e796497b84be8..69e038b9389ae 100644
@@ -901,15 +901,16 @@ static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)
 	tp->rcv_rtt_est.time = tp->tcp_mstamp;
 }
 
+/* A sample from TSecr above TCP_RTT_MAX_US is bogus, not large. */
 static s32 tcp_rtt_tsopt_us(const struct tcp_sock *tp, u32 min_delta)

net/ipv4/tcp_input.c static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp) · line 913

Quoted 10 lines
net/ipv4/tcp_input.c +2 -2
 {
 	u32 delta, delta_us;
 
 	delta = tcp_time_stamp_ts(tp) - tp->rx_opt.rcv_tsecr;
 	if (tp->tcp_usec_ts)
-		return delta;
+		return delta <= TCP_RTT_MAX_US ? delta : -1;
 
-	if (likely(delta < INT_MAX / (USEC_PER_SEC / TCP_TS_HZ))) {
+	if (likely(delta <= TCP_RTT_MAX_US / (USEC_PER_SEC / TCP_TS_HZ))) {

[Severity: Low]
This isn't a bug, but should the comment on TCP_RTT_MAX_US in include/net/tcp.h be updated to match its new use here? It currently reads:

/* Max RTT sample for tcp_rtt_estimator(). srtt_us holds 8 times the RTT, and
* mdev_us and rttvar_us up to 4 times its deviation, all in u32, like the sum
* in __tcp_set_rto(). A larger sample could not raise the RTO, which
* TCP_RTO_MAX bounds.
*/
#define TCP_RTT_MAX_US (TCP_RTO_MAX_SEC * USEC_PER_SEC)

That comment names tcp_rtt_estimator() as the only user. The only reason it gives for the value is u32 headroom for a saturating clamp.

With this patch the same constant is also the validity cutoff in tcp_rtt_tsopt_us(). That cutoff covers more than the estimator: it decides rs->rtt_us, the tcp_update_rtt_min() input, and whether icsk_backoff is reset in tcp_ack_update_rtt(). It also gates the receive-side estimate in tcp_rcv_rtt_measure_ts().

The later patch in the series, "tcp: clamp the samples in tcp_rcv_rtt_update()", adds another user, and the header comment is still unchanged at the end of the series.

Someone who changes TCP_RTO_MAX_SEC or TCP_RTT_MAX_US because of the u32 headroom reasoning would not see that this also moves the TSecr rejection threshold. Could the header comment mention that the constant also bounds valid TSecr samples?

Quoted 3 lines
net/ipv4/tcp_input.c +0 -0
 		if (!delta)
 			delta = min_delta;
 		delta_us = delta * (USEC_PER_SEC / TCP_TS_HZ);
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118155144.18393-1-mason%40kernel.org
#9 lore raw

Re: [PATCH net v2 4/5] tcp: clamp the samples in tcp_rcv_rtt_update()

In reply to Chris Mason
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The fix is incomplete.
- [Low] The comment on TCP_RTT_MAX_US in include/net/tcp.h (lines 167-172) still says it is the 'Max RTT sample for tcp_rtt_estimator()'…

--

tcp: clamp the samples in tcp_rcv_rtt_update()

This patch clamps the sample passed to tcp_rcv_rtt_update() to TCP_RTT_MAX_US before the shift left by 3. The goal is that a receive-window fill time of 536.9 s or more no longer wraps in u32 arithmetic. The commit message says this covers both callers of tcp_rcv_rtt_update().

Quoted 5 lines
net/ipv4/tcp_input.c +0 -0
index 69e038b9389ae..d9a66a1342efd 100644

net/ipv4/tcp_input.c void tcp_initialize_rcv_mss(struct sock *sk) · line 864

Quoted 9 lines
net/ipv4/tcp_input.c +5 -1
@@ -857,7 +857,11 @@ void tcp_initialize_rcv_mss(struct sock *sk)
 static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)
 {
 	u32 new_sample, old_sample = tp->rcv_rtt_est.rtt_us;
-	long m = sample << 3;
+	long m;
+
+	/* Keep sample << 3 and the smoothed estimate within u32. */
+	sample = min(sample, TCP_RTT_MAX_US);
+	m = sample << 3;

[Severity: High]
Can a long window-fill time still wrap before it gets to this clamp?

In tcp_rcv_rtt_measure(), the sample comes from tcp_stamp_us_delta():

net/ipv4/tcp_input.c:tcp_rcv_rtt_measure() {
    ...
	delta_us = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcv_rtt_est.time);
	if (!delta_us)
		delta_us = 1;
	tcp_rcv_rtt_update(tp, delta_us, 1);
    ...
}

That helper narrows the s64 difference to u32:

include/net/tcp.h:
static inline u32 tcp_stamp_us_delta(u64 t1, u64 t0)

{
	return max_t(s64, t1 - t0, 0);
}

So by the time min() sees an interval of 2^32 us (about 4295 s) or more, it has already been reduced modulo 2^32.

For example, a 4300 s window-fill time arrives here as 5,032,704 us (about 5.03 s). That passes the 120 s clamp unchanged. With win_dep=1 it is stored when old_sample == 0 or when m < old_sample.

The only writer of rcv_rtt_est.time is the new_measure label in tcp_rcv_rtt_measure(), and nothing resets a pending measurement when the connection goes idle. The window-completing segment could arrive after more than about 72 minutes of idle time or a slow sender. Would that still hit the case the commit message describes, where "the wrapped value is kept whenever it is below the current estimate", only with a higher threshold?

Without timestamps the win_dep path only lowers the estimate. A bogus small value would stay for the life of the connection.

Would it be better to clamp the 64-bit delta in tcp_rcv_rtt_measure() before it is narrowed to u32?

[Severity: Low]
This isn't a bug, but the comment on TCP_RTT_MAX_US in include/net/tcp.h still describes the constant only in terms of tcp_rtt_estimator() and the RTO:

/* Max RTT sample for tcp_rtt_estimator(). srtt_us holds 8 times the RTT, and
* mdev_us and rttvar_us up to 4 times its deviation, all in u32, like the sum
* in __tcp_set_rto(). A larger sample could not raise the RTO, which
* TCP_RTO_MAX bounds.
*/
#define TCP_RTT_MAX_US (TCP_RTO_MAX_SEC * USEC_PER_SEC)

After this patch, tcp_rcv_rtt_update() also relies on 8 * TCP_RTT_MAX_US fitting in u32 for rcv_rtt_est.rtt_us, which is not an input to __tcp_set_rto(). tcp_rtt_tsopt_us() also uses the constant.

Could the header comment be extended to cover the receive-side users? Alternatively, a static_assert could check that TCP_RTT_MAX_US << 3 fits in a u32.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118155144.18393-1-mason%40kernel.org
#10 lore raw

Re: [PATCH net v2 4/5] tcp: clamp the samples in tcp_rcv_rtt_update()

Quoted 2 lines · On Wed, 18 Nov 2026 18:23:03 +0000, [email protected] wrote:

[Severity: High]
Can a long window-fill time still wrap before it gets to this clamp?

tcp_stamp_us_delta() narrows the delta to u32 first, so from 4295 s on the clamp sees what is left of it. Your 4300 s example is right.

It takes a flow that needs more than 71 minutes to fill one receive window, so I don't think it's as bad as High, but the changelog says the patch covers both callers and for this one that isn't true yet.

Quoted 2 lines

Would it be better to clamp the 64-bit delta in tcp_rcv_rtt_measure() before it is narrowed to u32?

Yes, so I can spin a v3 with this change, or make a follow up, I'll defer to netdev maintainers:

- delta_us = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcv_rtt_est.time);
+ /* Clamp the delta before it is narrowed to u32. */
+ delta_us = min_t(u64, tp->tcp_mstamp - tp->rcv_rtt_est.time,
+ TCP_RTT_MAX_US);

-chris

maintainer
#11 lore raw

Re: [PATCH net v2 4/5] tcp: clamp the samples in tcp_rcv_rtt_update()

Quoted 17 lines · Chris Mason
Quoted 2 lines · On Wed, 18 Nov 2026 18:23:03 +0000, [email protected] wrote:

[Severity: High]
Can a long window-fill time still wrap before it gets to this clamp?

tcp_stamp_us_delta() narrows the delta to u32 first, so from 4295 s on the clamp sees what is left of it. Your 4300 s example is right.

It takes a flow that needs more than 71 minutes to fill one receive window, so I don't think it's as bad as High, but the changelog says the patch covers both callers and for this one that isn't true yet.

Quoted 2 lines

Would it be better to clamp the 64-bit delta in tcp_rcv_rtt_measure() before it is narrowed to u32?

Yes, so I can spin a v3 with this change, or make a follow up, I'll defer to netdev maintainers:

Review

Quoted 4 lines
(quoted diff) +3 -1
-	delta_us = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcv_rtt_est.time);
+	/* Clamp the delta before it is narrowed to u32. */
+	delta_us = min_t(u64, tp->tcp_mstamp - tp->rcv_rtt_est.time,
+			 TCP_RTT_MAX_US);

I think a follow-up is the preferred option here.

/P

#14 lore raw
In reply to Chris Mason
This series was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:

Quoted 8 lines · On Wed, 18 Nov 2026 10:51:44 -0500 you wrote:

Hi everyone,

This is v2 of the RTT clamps, with Eric's review folded in.

Patch 1: __tcp_set_rto() never returns 0, so that the backoff recovers from any state of the RTT estimator.

[...]

Here is the summary with links:
- [net,v2,1/5] tcp: don't let __tcp_set_rto() return 0

    https://git.kernel.org/netdev/net/c/5d2a5ad03677
  - [net,v2,2/5] tcp: clamp RTT samples in tcp_rtt_estimator()
    https://git.kernel.org/netdev/net/c/fa472d53d9b7
  - [net,v2,3/5] tcp: reject RTT samples from TSecr above 120 s
    https://git.kernel.org/netdev/net/c/1b91d9c09aa7
  - [net,v2,4/5] tcp: clamp the samples in tcp_rcv_rtt_update()
    https://git.kernel.org/netdev/net/c/f22d5042faa6
  - [net,v2,5/5] tcp: bound the rto_min of a route in tcp_rto_min()
    https://git.kernel.org/netdev/net/c/314b35c8173c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html