From 5d2a5ad0367725f2833bbfcec25aae1e69d71ba1 Mon Sep 17 00:00:00 2001 From: Chris Mason Date: Wed, 18 Nov 2026 10:51:45 -0500 Subject: [PATCH 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.") Suggested-by: Eric Dumazet Signed-off-by: Chris Mason Link: https://patch.msgid.link/20261118155144.18393-2-mason@kernel.org Signed-off-by: Paolo Abeni --- 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); } From fa472d53d9b7aaff384d25f4ca86540d63815113 Mon Sep 17 00:00:00 2001 From: Chris Mason Date: Wed, 18 Nov 2026 10:51:46 -0500 Subject: [PATCH 2/5] tcp: clamp RTT samples in tcp_rtt_estimator() 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") Signed-off-by: Chris Mason Reviewed-by: Eric Dumazet Link: https://patch.msgid.link/20261118155144.18393-3-mason@kernel.org Signed-off-by: Paolo Abeni --- include/net/tcp.h | 7 +++++++ net/ipv4/tcp_input.c | 5 ++++- 2 files changed, 11 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..e796497b84be 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -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 From 1b91d9c09aa703d19995412ad41f23b8886bc608 Mon Sep 17 00:00:00 2001 From: Chris Mason Date: Wed, 18 Nov 2026 10:51:47 -0500 Subject: [PATCH 3/5] tcp: reject RTT samples from TSecr above 120 s 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") Signed-off-by: Chris Mason Reviewed-by: Eric Dumazet Link: https://patch.msgid.link/20261118155144.18393-4-mason@kernel.org Signed-off-by: Paolo Abeni --- 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 e796497b84be..69e038b9389a 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -901,15 +901,16 @@ new_measure: 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); From f22d5042faa6b03df9586074e0322db60973933b Mon Sep 17 00:00:00 2001 From: Chris Mason Date: Wed, 18 Nov 2026 10:51:48 -0500 Subject: [PATCH 4/5] tcp: clamp the samples in tcp_rcv_rtt_update() 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") Suggested-by: Eric Dumazet Signed-off-by: Chris Mason Reviewed-by: Eric Dumazet Link: https://patch.msgid.link/20261118155144.18393-5-mason@kernel.org Signed-off-by: Paolo Abeni --- net/ipv4/tcp_input.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 69e038b9389a..d9a66a1342ef 100644 --- a/net/ipv4/tcp_input.c +++ b/net/ipv4/tcp_input.c @@ -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; From 314b35c8173cb5726ae4c3dd401d982e16de9482 Mon Sep 17 00:00:00 2001 From: Chris Mason Date: Wed, 18 Nov 2026 10:51:49 -0500 Subject: [PATCH 5/5] tcp: bound the rto_min of a route in tcp_rto_min() 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.") Signed-off-by: Chris Mason Reviewed-by: Eric Dumazet Link: https://patch.msgid.link/20261118155144.18393-6-mason@kernel.org Signed-off-by: Paolo Abeni --- include/net/tcp.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/net/tcp.h b/include/net/tcp.h index a617a72ee2c6..27de546dc802 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -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; }