Netdev List
 help / color / mirror / Atom feed
* [PATCH net 0/3] tcp: avoid integer wraps in the RTT estimator and the RTO
@ 2026-11-18  0:50 Chris Mason
  2026-11-18  0:50 ` [PATCH net 1/3] tcp: don't let __tcp_set_rto() return 0 Chris Mason
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Chris Mason @ 2026-11-18  0:50 UTC (permalink / raw)
  To: mason, edumazet, ncardwell, kuniyu, davem, kuba, pabeni, horms,
	willemb, josef, netdev

Hi everyone,

These three patches keep large RTT samples from wrapping the u32 fields
of the TCP RTT estimator.

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.
This covers the ACK, SACK and TSecr samples.

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

Thanks to Eric for suggesting patches 1 and 2.  This is on top of
v7.4-rc3.

Chris Mason (3):
  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

 include/net/tcp.h    | 11 ++++++++++-
 net/ipv4/tcp_input.c | 12 +++++++-----
 2 files changed, 17 insertions(+), 6 deletions(-)

-- 
2.55.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH net 1/3] tcp: don't let __tcp_set_rto() return 0
  2026-11-18  0:50 [PATCH net 0/3] tcp: avoid integer wraps in the RTT estimator and the RTO Chris Mason
@ 2026-11-18  0:50 ` Chris Mason
  2026-11-18  0:50 ` [PATCH net 2/3] tcp: clamp RTT samples in tcp_rtt_estimator() Chris Mason
  2026-11-18  0:50 ` [PATCH net 3/3] tcp: reject RTT samples from TSecr above 120 s Chris Mason
  2 siblings, 0 replies; 4+ messages in thread
From: Chris Mason @ 2026-11-18  0:50 UTC (permalink / raw)
  To: mason, edumazet, ncardwell, kuniyu, davem, kuba, pabeni, horms,
	willemb, josef, netdev

__tcp_set_rto() computes the RTO as (srtt_us >> 3) + rttvar_us, in u32
arithmetic and with no lower bound. A comment in tcp_set_rto() says that
the algorithm guarantees an RTO above TCP_RTO_MIN. But tcp_rto_min() is
only applied to rttvar_us, so that guarantee is lost if the sum wraps.

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

Of the three callers, only the thin-stream branch of
tcp_retransmit_timer() clamps the result. tcp_set_rto() and
tcp_ld_RTO_revert() store it in icsk_rto with an upper bound only.

The fix used here is to keep the result of __tcp_set_rto() from 0, for
all of its callers, so that the backoff recovers from any state of the
RTT estimator.

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")
Signed-off-by: Chris Mason <mason@kernel.org>
---
 include/net/tcp.h    | 4 +++-
 net/ipv4/tcp_input.c | 5 +++--
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/include/net/tcp.h b/include/net/tcp.h
index 5c9203aae834..6a714189bcc7 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -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)
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index a2074a1af422..f65697bc96c9 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -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);
 }
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH net 2/3] tcp: clamp RTT samples in tcp_rtt_estimator()
  2026-11-18  0:50 [PATCH net 0/3] tcp: avoid integer wraps in the RTT estimator and the RTO Chris Mason
  2026-11-18  0:50 ` [PATCH net 1/3] tcp: don't let __tcp_set_rto() return 0 Chris Mason
@ 2026-11-18  0:50 ` Chris Mason
  2026-11-18  0:50 ` [PATCH net 3/3] tcp: reject RTT samples from TSecr above 120 s Chris Mason
  2 siblings, 0 replies; 4+ messages in thread
From: Chris Mason @ 2026-11-18  0:50 UTC (permalink / raw)
  To: mason, edumazet, ncardwell, kuniyu, davem, kuba, pabeni, horms,
	willemb, josef, netdev

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.

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")
Signed-off-by: Chris Mason <mason@kernel.org>
---
 include/net/tcp.h    | 7 +++++++
 net/ipv4/tcp_input.c | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/include/net/tcp.h b/include/net/tcp.h
index 6a714189bcc7..a617a72ee2c6 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -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
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index f65697bc96c9..10a23ee20e61 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -1098,7 +1098,7 @@ 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 */
+	long m = min(mrtt_us, TCP_RTT_MAX_US); /* RTT */
 	u32 srtt = tp->srtt_us;
 
 	/*	The following amusing code comes from Jacobson's
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH net 3/3] tcp: reject RTT samples from TSecr above 120 s
  2026-11-18  0:50 [PATCH net 0/3] tcp: avoid integer wraps in the RTT estimator and the RTO Chris Mason
  2026-11-18  0:50 ` [PATCH net 1/3] tcp: don't let __tcp_set_rto() return 0 Chris Mason
  2026-11-18  0:50 ` [PATCH net 2/3] tcp: clamp RTT samples in tcp_rtt_estimator() Chris Mason
@ 2026-11-18  0:50 ` Chris Mason
  2 siblings, 0 replies; 4+ messages in thread
From: Chris Mason @ 2026-11-18  0:50 UTC (permalink / raw)
  To: mason, edumazet, ncardwell, kuniyu, davem, kuba, pabeni, horms,
	willemb, josef, netdev

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() passes the sample to tcp_rcv_rtt_update(),
which computes sample << 3 in u32 arithmetic. That wraps from 536.9 s
on.

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")
Signed-off-by: Chris Mason <mason@kernel.org>
---
 net/ipv4/tcp_input.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index 10a23ee20e61..8de54d9804e1 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -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);
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-18  0:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18  0:50 [PATCH net 0/3] tcp: avoid integer wraps in the RTT estimator and the RTO Chris Mason
2026-11-18  0:50 ` [PATCH net 1/3] tcp: don't let __tcp_set_rto() return 0 Chris Mason
2026-11-18  0:50 ` [PATCH net 2/3] tcp: clamp RTT samples in tcp_rtt_estimator() Chris Mason
2026-11-18  0:50 ` [PATCH net 3/3] tcp: reject RTT samples from TSecr above 120 s Chris Mason

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox