Merge branch 'tcp-avoid-integer-wraps-in-the-rtt-estimator-and-the-rto'

Chris Mason says:

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

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.

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

Link: https://patch.msgid.link/[email protected]
Signed-off-by: Paolo Abeni <[email protected]>
This commit is contained in:
Paolo Abeni
2026-11-19 14:48:04 +01:00
2 changed files with 27 additions and 8 deletions
+12 -2
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
@@ -910,7 +917,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)
@@ -930,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;
}
+15 -6
View File
@@ -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;
@@ -901,15 +905,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);
@@ -1098,8 +1103,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
@@ -1221,8 +1229,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);
}