* [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; 14+ 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] 14+ 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 10:08 ` Eric Dumazet ` (2 more replies) 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, 3 replies; 14+ 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] 14+ messages in thread
* Re: [PATCH net 1/3] tcp: don't let __tcp_set_rto() return 0 2026-11-18 0:50 ` [PATCH net 1/3] tcp: don't let __tcp_set_rto() return 0 Chris Mason @ 2026-11-18 10:08 ` Eric Dumazet 2026-11-18 13:33 ` David Laight 2026-11-18 15:25 ` netdev-bot+sashiko 2 siblings, 0 replies; 14+ messages in thread From: Eric Dumazet @ 2026-11-18 10:08 UTC (permalink / raw) To: Chris Mason Cc: ncardwell, kuniyu, davem, kuba, pabeni, horms, willemb, josef, netdev Le mer. 18 nov. 2026 à 01:50, Chris Mason <mason@kernel.org> a écrit : > > __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. > The wrap is what patch 2 fixes. Once samples are clamped to 120 s, the sum is bounded by ~600 s and can not wrap anymore, so this paragraph no longer justifies the patch. Also, usecs_to_jiffies() rounds up, so a wrapped sum only gives rto == 0 if it lands exactly on 0 modulo 2^32. Any other value gives at least one jiffy, and the backoff recovers from that. There is another way to get 0, with no wrap, and I think this is the one the changelog should describe: tcp_rto_min() can return 0 if the route has a locked RTAX_RTO_MIN metric of 0. ip_metrics_convert() only rejects a zero locked cwnd. All other knobs have a non zero floor (tcp_rto_min_us sysctl, TCP_RTO_MIN_US, TCP_BPF_RTO_MIN). Then a first RTT sample of 0 usec gives: srtt_us = max(1U, 0 << 3) = 1 mdev_us = 0 rttvar_us = max(mdev_us, tcp_rto_min_us(sk)) = 0 __tcp_set_rto() = usecs_to_jiffies((1 >> 3) + 0) = 0 A 0 usec sample is possible with usec TS, because tcp_rtt_tsopt_us() returns delta before min_delta is applied in that case. > 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") Linux-2.6.12-rc2 had no rto_min setting at all. rttvar was floored by the TCP_RTO_MIN constant, and tcp_rtt_estimator() had: if (m == 0) m = 1; So the comment was correct at that time, wraps aside. The configurable minimum came with: Fixes: 05bb1fad1cde ("[TCP]: Allow minimum RTO to be configurable via routing metrics.") Please use this tag and reword the changelog in v2, or drop the Fixes: tag and send this one to net-next as hardening, since it needs a zero locked rto_min route metric. Thanks. ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH net 1/3] tcp: don't let __tcp_set_rto() return 0 2026-11-18 0:50 ` [PATCH net 1/3] tcp: don't let __tcp_set_rto() return 0 Chris Mason 2026-11-18 10:08 ` Eric Dumazet @ 2026-11-18 13:33 ` David Laight 2026-11-18 15:25 ` netdev-bot+sashiko 2 siblings, 0 replies; 14+ messages in thread From: David Laight @ 2026-11-18 13:33 UTC (permalink / raw) To: Chris Mason Cc: edumazet, ncardwell, kuniyu, davem, kuba, pabeni, horms, willemb, josef, netdev On Tue, 17 Nov 2026 19:50:49 -0500 Chris Mason <mason@kernel.org> wrote: > __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); return rto ?: 1; is possibly more readable? David > } > > 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); > } ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH net 1/3] tcp: don't let __tcp_set_rto() return 0 2026-11-18 0:50 ` [PATCH net 1/3] tcp: don't let __tcp_set_rto() return 0 Chris Mason 2026-11-18 10:08 ` Eric Dumazet 2026-11-18 13:33 ` David Laight @ 2026-11-18 15:25 ` netdev-bot+sashiko 2026-11-18 15:37 ` Chris Mason 2 siblings, 1 reply; 14+ messages in thread From: netdev-bot+sashiko @ 2026-11-18 15:25 UTC (permalink / raw) To: mason Cc: edumazet, ncardwell, kuniyu, davem, kuba, pabeni, horms, willemb, josef, netdev Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider. Critical: 0 · High: 0 · Medium: 1 · Low: 2 (2) 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). - [Medium] tcp_init_metrics() (net/ipv4/tcp_metrics.c:535-538) is a sibling writer of icsk_rto that does not go through __tcp_set_rto(). - [Low] The commit message blames the zero RTO only on a u32 wrap of (srtt_us >> 3) + rttvar_us, and it understates the impact. - [Low] This concern is retained only because it requires validation; the baseline decision is false. Pre-existing issues: - [Low] This concern is retained only because it requires validation; the baseline decision is false. - [Low] This concern is retained only because it requires validation; the baseline decision is false. -- tcp: don't let __tcp_set_rto() return 0 This patch makes __tcp_set_rto() return at least 1 jiffy, so that the exponential backoff in tcp_retransmit_timer() can always move icsk_rto away from zero. It also rewrites the comment in tcp_set_rto() to say where the tcp_rto_min() floor comes from. > __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. [Severity: Low] Is the wrap really what produces the zero here? usecs_to_jiffies() rounds up, so __tcp_set_rto() returns 0 only when the u32 sum is exactly 0 mod 2^32. A wrapped sum that lands anywhere else still gives at least 1 jiffy. Another trigger needs no wrap at all. ip_metrics_convert() accepts a locked RTAX_RTO_MIN route metric of 0, and then tcp_rto_min_us(sk) returns 0: net/ipv4/tcp_input.c:tcp_rtt_estimator() { ... tp->rttvar_us = max(tp->mdev_us, tcp_rto_min_us(sk)); ... } In that case rttvar_us can be 0, and srtt_us can be below 8 after a 0 us sample, so the sum is 0. Later in this series, "tcp: clamp RTT samples in tcp_rtt_estimator()" keeps the sum below 2^32. After that, the rto_min == 0 case is the only trigger left that this patch is needed for. Could the commit message name it, so that stable backporters can see why this patch is still needed? The impact could also be described more fully. When icsk_rto is 0, tcp_retransmit_timer() does this: icsk->icsk_rto = min(icsk->icsk_rto << 1, tcp_rto_max(sk)); It then re-arms ICSK_TIME_RETRANS with a 0 timeout on every expiry. The socket retransmits on every tick until retransmits_timed_out() gives up, which takes about 924 seconds at the default tcp_retries2. > diff --git a/include/net/tcp.h b/include/net/tcp.h > index 5c9203aae8341..6a714189bcc7f 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); [Severity: Low] This isn't a bug introduced by this patch, but the sum here is still computed in wrapping u32 arithmetic. When it wraps, the result now falls somewhere between 1 jiffy and tcp_rto_min(). tcp_set_rto() and tcp_ld_RTO_revert() apply only an upper bound to it. If this commit is taken on its own, can a peer still cause the wrap? tcp_rtt_tsopt_us() accepts TSecr deltas up to about 2.1e9 us: if (likely(delta < INT_MAX / (USEC_PER_SEC / TCP_TS_HZ))) { tcp_rtt_estimator() then uses that sample without clamping it. Later patches in this series close that path: - "tcp: clamp RTT samples in tcp_rtt_estimator()" changes the sample to min(mrtt_us, TCP_RTT_MAX_US), with a 120 s bound. - "tcp: reject RTT samples from TSecr above 120 s" makes tcp_rtt_tsopt_us() return -1 for such samples. With both applied, srtt_us >> 3 stays below about 536.9e6, even with TCP_TX_DELAY. rttvar_us stays below about 2.147e9, so the sum stays under 2^32. One way to wrap is left: a locked RTAX_RTO_MIN route metric near 4294 seconds, where jiffies_to_usecs(tcp_rto_min()) is already close to 2^32. That needs an admin-only configuration. It is a gap in the bound chosen by the sample-clamping patch rather than in this one. > + > + return max(rto, 1U); [Severity: Medium] This floor only covers callers of __tcp_set_rto(). Can tcp_init_metrics() still store an icsk_rto of 0? net/ipv4/tcp_metrics.c:tcp_init_metrics() { ... if (crtt > tp->srtt_us) { /* Set RTO like tcp_rtt_estimator(), but from cached RTT. */ crtt /= 8 * USEC_PER_SEC / HZ; inet_csk(sk)->icsk_rto = crtt + max(2 * crtt, tcp_rto_min(sk)); ... } This assignment uses u32 arithmetic, has no lower bound and does not call tcp_bound_rto(). When RTAX_RTO_MIN is locked, tcp_rto_min() returns dst_metric_rtt(dst, RTAX_RTO_MIN). ip_metrics_convert() accepts any value for that metric; its only zero check is for a locked RTAX_CWND. There are two ways to reach 0: - With a locked rto_min of 0, a cached RTT below one jiffy but above the SYN RTT gives crtt = 0, so icsk_rto = 0 + max(0, 0) = 0. An example is 100-500 us on a local network or loopback at HZ=1000. - With a very large locked rto_min, crtt + rto_min can wrap to exactly 0. Suppose the peer then stops answering after the handshake. No RTT sample reaches tcp_set_rto(), and this happens: tcp_init_transfer()->tcp_init_metrics() /* icsk_rto = 0 */ tcp_retransmit_timer() icsk->icsk_rto = min(icsk->icsk_rto << 1, tcp_rto_max(sk)); /* 0 */ tcp_reset_xmit_timer(sk, ICSK_TIME_RETRANS, tcp_clamp_rto_to_user_timeout(sk), false); retransmits_timed_out() is time based, so the socket would retransmit on every tick for about 924 seconds at tcp_retries2=15. Setting the route metric needs CAP_NET_ADMIN in the netns, which an unprivileged user can get through user namespaces. The new comment in tcp_set_rto() says rto must never be 0, and the rest of the series doesn't change tcp_metrics.c. Should this assignment also be bounded? Either max(..., 1U) or a clamp to [tcp_rto_min, tcp_rto_max] would work. The floor could also go where the backoff doubles icsk_rto. > } [ ... ] > diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c > index a2074a1af4222..f65697bc96c9d 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. > */ [Severity: Low] Is this comment accurate at this commit? It says no tcp_rto_min() clamp is needed because rttvar_us carries the floor. The commit message, though, says "that guarantee is lost if the sum wraps", and __tcp_set_rto() only guarantees 1 jiffy. While the sum can wrap, the result can fall in [1, rto_min). The contradiction goes away once "tcp: clamp RTT samples in tcp_rtt_estimator()" later in the series keeps the sum within u32. From then on, rttvar_us >= tcp_rto_min_us() does imply rto >= rto_min, and the 1U floor only matters when rto_min == 0. A smaller gap remains, and it predates this patch. rttvar_us is not raised when tcp_rto_min() increases in the middle of a connection. tcp_rtt_estimator() resets mdev_max_us to the new value: tp->mdev_max_us = tcp_rto_min_us(sk); But rttvar_us only grows when mdev_us exceeds mdev_max_us. The old comment ("current algo guarantees that rto is higher") had the same gap. > tcp_bound_rto(sk); > } [Severity: Low] This is a pre-existing issue, but the three users of __tcp_set_rto() apply different lower bounds. The thin-stream branch of tcp_retransmit_timer() clamps at both ends: icsk->icsk_rto = clamp(__tcp_set_rto(tp), tcp_rto_min(sk), tcp_rto_max(sk)); tcp_set_rto() (through tcp_bound_rto()) and tcp_ld_RTO_revert() (through inet_csk_rto_backoff()) apply only tcp_rto_max(). On those two paths, the documented tcp_rto_min_us minimum is enforced only indirectly, through the rttvar_us floor in tcp_rtt_estimator(). That floor holds once the later sample-clamping patch keeps the sum from wrapping. Is it intended that raising rto_min mid-connection takes effect at once only on the thin-stream path? -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118005048.53393-1-mason%40kernel.org ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH net 1/3] tcp: don't let __tcp_set_rto() return 0 2026-11-18 15:25 ` netdev-bot+sashiko @ 2026-11-18 15:37 ` Chris Mason 0 siblings, 0 replies; 14+ messages in thread From: Chris Mason @ 2026-11-18 15:37 UTC (permalink / raw) To: netdev-bot+sashiko Cc: edumazet, ncardwell, kuniyu, davem, kuba, pabeni, horms, willemb, josef, netdev On Wed, 18 Nov 2026 15:25:23 +0000, netdev-bot+sashiko@kernel.org wrote: > [Severity: Medium] > This floor only covers callers of __tcp_set_rto(). Can > tcp_init_metrics() still store an icsk_rto of 0? Yes, both ways you describe. v2 adds a patch that clamps a locked rto_min to the range from one jiffy to TCP_RTO_MAX in tcp_rto_min(). That covers tcp_init_metrics(), and also the rto_min near 4294 s that you mention for the sum in __tcp_set_rto(). > After that, the rto_min == 0 case is the only > trigger left that this patch is needed for. Could the commit message name > it, so that stable backporters can see why this patch is still needed? Eric asked for the same thing. The v2 changelog describes that case, with the Fixes: tag he suggested. The High finding on patch 3 is right too: tcp_rcv_rtt_measure() still gets to the sample << 3. Eric caught that one as well, and v2 clamps the sample in tcp_rcv_rtt_update(). I'm leaving the rto_min that is raised in the middle of a connection alone. It has always worked that way, and it isn't what this series is about. pw-bot: cr -chris ^ permalink raw reply [flat|nested] 14+ 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 9:46 ` Eric Dumazet 2026-11-18 15:25 ` netdev-bot+sashiko 2026-11-18 0:50 ` [PATCH net 3/3] tcp: reject RTT samples from TSecr above 120 s Chris Mason 2 siblings, 2 replies; 14+ 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] 14+ messages in thread
* Re: [PATCH net 2/3] tcp: clamp RTT samples in tcp_rtt_estimator() 2026-11-18 0:50 ` [PATCH net 2/3] tcp: clamp RTT samples in tcp_rtt_estimator() Chris Mason @ 2026-11-18 9:46 ` Eric Dumazet 2026-11-18 15:25 ` netdev-bot+sashiko 1 sibling, 0 replies; 14+ messages in thread From: Eric Dumazet @ 2026-11-18 9:46 UTC (permalink / raw) To: Chris Mason Cc: ncardwell, kuniyu, davem, kuba, pabeni, horms, willemb, josef, netdev Le mer. 18 nov. 2026 à 01:50, Chris Mason <mason@kernel.org> a écrit : > > 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; > Nit: the two tcp_bpf_rtt(sk, mrtt_us, srtt) calls below still pass the unclamped sample, next to an srtt computed from the clamped one. Maybe clamp the argument instead: mrtt_us = min(mrtt_us, TCP_RTT_MAX_US); Reviewed-by: Eric Dumazet <edumazet@kernel.org> ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH net 2/3] tcp: clamp RTT samples in tcp_rtt_estimator() 2026-11-18 0:50 ` [PATCH net 2/3] tcp: clamp RTT samples in tcp_rtt_estimator() Chris Mason 2026-11-18 9:46 ` Eric Dumazet @ 2026-11-18 15:25 ` netdev-bot+sashiko 1 sibling, 0 replies; 14+ messages in thread From: netdev-bot+sashiko @ 2026-11-18 15:25 UTC (permalink / raw) To: mason Cc: edumazet, ncardwell, kuniyu, davem, kuba, pabeni, horms, willemb, josef, netdev 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 new comment above TCP_RTT_MAX_US in include/net/tcp.h says 'A larger sample could not raise the RTO, which TCP_RTO_MAX bounds.' This… Pre-existing issues: - [Low] The receive-side RTT estimator has the same u32 '8x RTT' wrap, and this patch leaves it alone. -- tcp: clamp RTT samples in tcp_rtt_estimator() This commit clamps each RTT sample in tcp_rtt_estimator() to TCP_RTT_MAX_US. That is 120 s, the largest possible RTO. The clamp keeps the u32 fields srtt_us, mdev_us and rttvar_us from wrapping, and the smoothed RTT now saturates at 120 s. > diff --git a/include/net/tcp.h b/include/net/tcp.h > index 6a714189bcc7f..a617a72ee2c6b 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. > + */ [Severity: Low] Does "A larger sample could not raise the RTO" hold beyond the update that uses the sample? For that update it does. With m >= 120 s, (srtt_us >> 3) + rttvar_us is already >= m in __tcp_set_rto(), so the RTO is capped at TCP_RTO_MAX with or without the clamp. But the clamped m is also what gets stored in srtt_us, mdev_us and rttvar_us. For example, a first sample of 500 s gives srtt/8 = 500 s and mdev = rttvar = 1000 s. The clamped sample gives srtt/8 = 120 s and mdev = rttvar = 240 s. When smaller samples follow, the clamped state falls below the RTO cap sooner. Later RTO and TLP timeouts are then lower than they would be with the unclamped sample, though still within the cap. The commit message describes the saturation in TCP_INFO, the pacing rate and the metrics cache, but this comment only talks about the RTO. Could it say that the clamp leaves the RTO of the current update unchanged, while later RTOs computed from the clamped state can be lower? > +#define TCP_RTT_MAX_US (TCP_RTO_MAX_SEC * USEC_PER_SEC) > + [ ... ] > diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c > index f65697bc96c9d..10a23ee20e61d 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 */ [Severity: Low] This is a pre-existing issue, not one introduced by this patch, but the receive-side estimator has the same 8x RTT in u32 wrap. Should tcp_rcv_rtt_update() get a similar bound? net/ipv4/tcp_input.c:tcp_rcv_rtt_update() { u32 new_sample, old_sample = tp->rcv_rtt_est.rtt_us; long m = sample << 3; if (old_sample == 0 || m < old_sample) { new_sample = m; } else { ... if (win_dep) return; ... tp->rcv_rtt_est.rtt_us = new_sample; } sample is u32, so the shift is done in 32-bit arithmetic. It wraps for samples above 2^29 us (about 536.9 s) before the result is widened to long. A wrapped m below old_sample is stored, even for win_dep=1 samples. There are two sources of large samples. The TSecr path, tcp_rcv_rtt_measure_ts(), passes tcp_rtt_tsopt_us() results of up to about 2,147 s. The later patch in this series, "tcp: reject RTT samples from TSecr above 120 s", makes tcp_rtt_tsopt_us() return -1 above TCP_RTT_MAX_US, which closes that path. The non-timestamp path is still unbounded at the end of the series: tcp_event_data_recv() tcp_rcv_rtt_measure() delta_us = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcv_rtt_est.time); tcp_rcv_rtt_update(tp, delta_us, 1); Suppose a receive window measurement spans a stall of more than 536.9 s. The wrapped value is accepted only if the stall falls in a narrow band: from 536.87 s to about 536.87 s plus the current receiver RTT, modulo 2^29 us. The win_dep check rejects most other wrapped values. When the value is accepted, tp->rcv_rtt_est.rtt_us is too small. Without timestamps this is a min filter, so the bad value stays for the life of the connection. It is read by tcp_rcv_space_adjust(), tcp_rcvbuf_grow(), the compressed ACK delay in __tcp_ack_snd_check(), tcpi_rcv_rtt and mptcp_rcv_rtt_update(). A remote peer can already push this min filter down by sending a window quickly, so the wrap doesn't give it anything new. -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118005048.53393-1-mason%40kernel.org ^ permalink raw reply [flat|nested] 14+ 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 2026-11-18 9:58 ` Eric Dumazet 2026-11-18 15:25 ` netdev-bot+sashiko 2 siblings, 2 replies; 14+ 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] 14+ messages in thread
* Re: [PATCH net 3/3] tcp: reject RTT samples from TSecr above 120 s 2026-11-18 0:50 ` [PATCH net 3/3] tcp: reject RTT samples from TSecr above 120 s Chris Mason @ 2026-11-18 9:58 ` Eric Dumazet 2026-11-18 13:36 ` David Laight 2026-11-18 15:25 ` netdev-bot+sashiko 1 sibling, 1 reply; 14+ messages in thread From: Eric Dumazet @ 2026-11-18 9:58 UTC (permalink / raw) To: Chris Mason Cc: ncardwell, kuniyu, davem, kuba, pabeni, horms, willemb, josef, netdev Le mer. 18 nov. 2026 à 01:50, Chris Mason <mason@kernel.org> a écrit : > > 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(). This only covers one of the two callers of tcp_rcv_rtt_update(). tcp_rcv_rtt_measure() is called from tcp_event_data_recv() for every data segment, with or without timestamps: 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); delta_us is the time the peer took to send one receive window. Any low rate flow can take more than 536.9 s for that, no bogus TSecr needed. Then 'sample << 3' wraps, and since win_dep is 1, the wrapped value is kept whenever it is smaller than the current estimate: if (old_sample == 0 || m < old_sample) { new_sample = m; For instance a 540 s sample ends up as rcv_rtt_est.rtt_us >> 3 == ~3.1 s. We should also clamp in tcp_rcv_rtt_update(), like patch 2 does in tcp_rtt_estimator(), so that both sources are covered [1] With that, 'old_sample - (old_sample >> 3) + sample' converges to at most 8 * 120 s, which fits in u32 too. > > 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. > This seems fine to me. We send a retransmit at least every TCP_RTO_MAX, and the retransmit filling the hole updates TS.Recent on the receiver, so a well behaved peer does not echo a TSval that is more than 120 s old in an ACK of new data. [1] Something like this on top of your patch (compile tested only): diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c index 2d739cfdae8c33d12cd34e00388a29f0f4229806..708a989e5a63bb6ad67cc777d57815ae197c451e 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_t(u32, sample, TCP_RTT_MAX_US); + m = sample << 3; if (old_sample == 0 || m < old_sample) { new_sample = m; Thanks. ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH net 3/3] tcp: reject RTT samples from TSecr above 120 s 2026-11-18 9:58 ` Eric Dumazet @ 2026-11-18 13:36 ` David Laight 2026-11-18 15:27 ` Eric Dumazet 0 siblings, 1 reply; 14+ messages in thread From: David Laight @ 2026-11-18 13:36 UTC (permalink / raw) To: Eric Dumazet Cc: Chris Mason, ncardwell, kuniyu, davem, kuba, pabeni, horms, willemb, josef, netdev On Wed, 18 Nov 2026 10:58:14 +0100 Eric Dumazet <edumazet@kernel.org> wrote: > Le mer. 18 nov. 2026 à 01:50, Chris Mason <mason@kernel.org> a écrit : > > > > 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(). > > This only covers one of the two callers of tcp_rcv_rtt_update(). > > tcp_rcv_rtt_measure() is called from tcp_event_data_recv() for every > data segment, with or without timestamps: > > 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); > > delta_us is the time the peer took to send one receive window. Any low > rate flow can take more than 536.9 s for that, no bogus TSecr needed. > > Then 'sample << 3' wraps, and since win_dep is 1, the wrapped value > is kept whenever it is smaller than the current estimate: > > if (old_sample == 0 || m < old_sample) { > new_sample = m; > > For instance a 540 s sample ends up as rcv_rtt_est.rtt_us >> 3 == ~3.1 s. > > We should also clamp in tcp_rcv_rtt_update(), like patch 2 > does in tcp_rtt_estimator(), so that both sources are covered [1] > > With that, 'old_sample - (old_sample >> 3) + sample' converges to at > most 8 * 120 s, which fits in u32 too. > > > > > 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. > > > > This seems fine to me. We send a retransmit at least every > TCP_RTO_MAX, and the retransmit filling the hole updates TS.Recent on > the receiver, so a well behaved peer does not echo a TSval that is more > than 120 s old in an ACK of new data. > > [1] > Something like this on top of your patch (compile tested only): > > diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c > index 2d739cfdae8c33d12cd34e00388a29f0f4229806..708a989e5a63bb6ad67cc777d57815ae197c451e > 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_t(u32, sample, TCP_RTT_MAX_US); You can't need min_t() here. David > + m = sample << 3; > > if (old_sample == 0 || m < old_sample) { > new_sample = m; > > Thanks. > ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH net 3/3] tcp: reject RTT samples from TSecr above 120 s 2026-11-18 13:36 ` David Laight @ 2026-11-18 15:27 ` Eric Dumazet 0 siblings, 0 replies; 14+ messages in thread From: Eric Dumazet @ 2026-11-18 15:27 UTC (permalink / raw) To: David Laight Cc: Chris Mason, ncardwell, kuniyu, davem, kuba, pabeni, horms, willemb, josef, netdev Le mer. 18 nov. 2026 à 14:36, David Laight <david.laight.linux@gmail.com> a écrit : > > On Wed, 18 Nov 2026 10:58:14 +0100 > Eric Dumazet <edumazet@kernel.org> wrote: > > > Le mer. 18 nov. 2026 à 01:50, Chris Mason <mason@kernel.org> a écrit : > > > > > > 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(). > > > > This only covers one of the two callers of tcp_rcv_rtt_update(). > > > > tcp_rcv_rtt_measure() is called from tcp_event_data_recv() for every > > data segment, with or without timestamps: > > > > 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); > > > > delta_us is the time the peer took to send one receive window. Any low > > rate flow can take more than 536.9 s for that, no bogus TSecr needed. > > > > Then 'sample << 3' wraps, and since win_dep is 1, the wrapped value > > is kept whenever it is smaller than the current estimate: > > > > if (old_sample == 0 || m < old_sample) { > > new_sample = m; > > > > For instance a 540 s sample ends up as rcv_rtt_est.rtt_us >> 3 == ~3.1 s. > > > > We should also clamp in tcp_rcv_rtt_update(), like patch 2 > > does in tcp_rtt_estimator(), so that both sources are covered [1] > > > > With that, 'old_sample - (old_sample >> 3) + sample' converges to at > > most 8 * 120 s, which fits in u32 too. > > > > > > > > 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. > > > > > > > This seems fine to me. We send a retransmit at least every > > TCP_RTO_MAX, and the retransmit filling the hole updates TS.Recent on > > the receiver, so a well behaved peer does not echo a TSval that is more > > than 120 s old in an ACK of new data. > > > > [1] > > Something like this on top of your patch (compile tested only): > > > > diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c > > index 2d739cfdae8c33d12cd34e00388a29f0f4229806..708a989e5a63bb6ad67cc777d57815ae197c451e > > 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_t(u32, sample, TCP_RTT_MAX_US); > > You can't need min_t() here. > Sure, on current kernels min() is happy with an u32 against a non negative signed constant. But this series carries Fixes: tags going back to 2.6.12, so it is going to be backported as far as stable maintainers can take it. The relaxed min()/max() checks landed in v6.7. I do not have cycles to check if they were backported to stable versions. ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH net 3/3] tcp: reject RTT samples from TSecr above 120 s 2026-11-18 0:50 ` [PATCH net 3/3] tcp: reject RTT samples from TSecr above 120 s Chris Mason 2026-11-18 9:58 ` Eric Dumazet @ 2026-11-18 15:25 ` netdev-bot+sashiko 1 sibling, 0 replies; 14+ messages in thread From: netdev-bot+sashiko @ 2026-11-18 15:25 UTC (permalink / raw) To: mason Cc: edumazet, ncardwell, kuniyu, davem, kuba, pabeni, horms, willemb, josef, netdev 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: the u32 wrap of `sample << 3` in tcp_rcv_rtt_update() (net/ipv4/tcp_input.c:860) can still be reached. - [Low] The patch gives TCP_RTT_MAX_US a second meaning but leaves its header comment as it was. -- tcp: reject RTT samples from TSecr above 120 s This commit makes tcp_rtt_tsopt_us() reject TSecr-derived RTT samples above TCP_RTT_MAX_US (120 s), for both the usec and ms timestamp clocks. Both callers are affected: tcp_ack_update_rtt() and tcp_rcv_rtt_measure_ts(). The goal is to stop feeding the age of a stale timestamp to the RTT estimator, the congestion control, and the u32 sample << 3 in tcp_rcv_rtt_update(). > diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c > index 10a23ee20e61d..8de54d9804e17 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. */ [Severity: Low] The header comment for TCP_RTT_MAX_US in include/net/tcp.h still describes the constant only as a clamp for tcp_rtt_estimator(): /* 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 matches how tcp_rtt_estimator() uses it, where a larger sample is clamped and then used: long m = min(mrtt_us, TCP_RTT_MAX_US); /* RTT */ Here the same constant becomes a threshold for rejecting a sample. The TSecr path, tcp_rcv_rtt_measure_ts()->tcp_rcv_rtt_update(), also now depends on TCP_RTT_MAX_US * 8 fitting in u32. Should the header comment mention this second use? As it stands, a later change that raises TCP_RTO_MAX_SEC above about 536 would bring the wrap back silently, and no BUILD_BUG_ON guards against that. > 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))) { [Severity: High] The commit message says: 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. Can that wrap in tcp_rcv_rtt_update() still be reached through its other caller, tcp_rcv_rtt_measure()? tcp_event_data_recv()->tcp_rcv_rtt_measure() runs on in-order data, with or without timestamps. Nothing bounds its sample: 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); ... } rcv_rtt_est.time is only refreshed once rcv_nxt reaches rcv_rtt_est.seq. Suppose the peer completes one measurement, stays idle for more than 2^29 us (about 536.9 s), and then sends a window of data. delta_us is then above 2^29, and the shift wraps: net/ipv4/tcp_input.c:tcp_rcv_rtt_update() { u32 new_sample, old_sample = tp->rcv_rtt_est.rtt_us; long m = sample << 3; if (old_sample == 0 || m < old_sample) { new_sample = m; } else { ... if (win_dep) return; ... } If delta_us lands within about one RTT past a multiple of 2^29 us, the wrapped m is below old_sample. It is accepted, and rcv_rtt_est.rtt_us ends up near zero. With win_dep=1, every later, larger and correct sample hits the early return. So on connections without timestamps, the bad value appears to stay for the life of the connection. Several things read it: receive buffer autotuning in tcp_rcv_space_adjust() and tcp_rcvbuf_grow(), tcpi_rcv_rtt, and MPTCP. Would it make sense to bound sample, or do the shift in 64 bits, inside tcp_rcv_rtt_update()? That would cover both callers, not only the TSecr source. > if (!delta) > delta = min_delta; > delta_us = delta * (USEC_PER_SEC / TCP_TS_HZ); -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118005048.53393-1-mason%40kernel.org ^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2026-11-18 15:37 UTC | newest] Thread overview: 14+ 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 10:08 ` Eric Dumazet 2026-11-18 13:33 ` David Laight 2026-11-18 15:25 ` netdev-bot+sashiko 2026-11-18 15:37 ` Chris Mason 2026-11-18 0:50 ` [PATCH net 2/3] tcp: clamp RTT samples in tcp_rtt_estimator() Chris Mason 2026-11-18 9:46 ` Eric Dumazet 2026-11-18 15:25 ` netdev-bot+sashiko 2026-11-18 0:50 ` [PATCH net 3/3] tcp: reject RTT samples from TSecr above 120 s Chris Mason 2026-11-18 9:58 ` Eric Dumazet 2026-11-18 13:36 ` David Laight 2026-11-18 15:27 ` Eric Dumazet 2026-11-18 15:25 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox