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 740b0f1841 ("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: 1da177e4c3 ("Linux-2.6.12-rc2")
Fixes: 740b0f1841 ("tcp: switch rtt estimations to usec resolution")
Signed-off-by: Chris Mason <[email protected]>
Reviewed-by: Eric Dumazet <[email protected]>
Link: https://patch.msgid.link/[email protected]
Signed-off-by: Paolo Abeni <[email protected]>
This commit is contained in:
Chris Mason
2026-11-19 14:47:59 +01:00
committed by Paolo Abeni
parent 5d2a5ad036
commit fa472d53d9
2 changed files with 11 additions and 1 deletions
+7
View File
@@ -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
+4 -1
View File
@@ -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