Skip to contentSkip to conversation

tcp: bound snd_cwnd_clamp to TCP_MAX_SND_CWND

PATCH Superseded Eric Dumazet 3 participants 4 messages Oct 27 – Oct 30

Subsystems (5)
Networking Network drivers BPF TCP +1 more subsystems

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Eric Dumazet

author maintainer
#0 lore raw

PATCH 1/1 tcp: bound snd_cwnd_clamp to TCP_MAX_SND_CWND

snd_cwnd has no practical upper bound: snd_cwnd_clamp defaults to ~0, and RTAX_INITCWND, a locked RTAX_CWND metric, TCP_BPF_IW and TCP_BPF_SNDCWND_CLAMP accept any positive value.

Congestion control modules do u32 arithmetic on cwnd that assumes much smaller values, and can misbehave if cwnd gets too big.

The paths that can grow cwnd by large amounts (tcp_slow_start(), tcp_cong_avoid_ai(), bbr_set_cwnd() and tcp_init_cwnd()) already bound it by snd_cwnd_clamp, so give snd_cwnd_clamp a finite default of 2^24 packets, and cap the metric and BPF paths that set it or set cwnd directly.

2^24 packets is more than 24GB in flight with a 1448 byte MSS, well above what any sk_sndbuf allows.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 include/net/tcp.h      | 6 ++++++
 net/core/filter.c      | 3 ++-
 net/ipv4/tcp.c         | 2 +-
 net/ipv4/tcp_metrics.c | 3 ++-
 4 files changed, 11 insertions(+), 3 deletions(-)
include/net/tcp.h +6 -0
index 0265c27a5fd77df8a86991e364ec7caff3a7ea0d..be1609cd78840cfb1da75517273be0d64f4c3981 100644
@@ -1553,6 +1553,12 @@ static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
 
 #define TCP_INFINITE_SSTHRESH	0x7fffffff
 
+/* Default and maximum snd_cwnd_clamp, in packets.
+ * Far above any usable BDP, but low enough that congestion control
+ * arithmetic such as 100 * cwnd in CUBIC cannot overflow a u32.
+ */
+#define TCP_MAX_SND_CWND	(1U << 24)
+
 static inline u32 tcp_snd_cwnd(const struct tcp_sock *tp)
 {
 	return tp->snd_cwnd;
net/core/filter.c +2 -1
index 70dc621672f2e9fccd9cf54808e002ff0f82da62..63bb314e8697db81ecef3a0b3dcf4fed85ac79fa 100644
@@ -5562,11 +5562,12 @@ static int bpf_sol_tcp_setsockopt(struct sock *sk, int optname,
 	case TCP_BPF_IW:
 		if (val <= 0 || tp->data_segs_out > tp->syn_data)
 			return -EINVAL;
-		tcp_snd_cwnd_set(tp, val);
+		tcp_snd_cwnd_set(tp, min_t(u32, val, tp->snd_cwnd_clamp));
 		break;
 	case TCP_BPF_SNDCWND_CLAMP:
 		if (val <= 0)
 			return -EINVAL;
+		val = min_t(u32, val, TCP_MAX_SND_CWND);
 		tp->snd_cwnd_clamp = val;
 		WRITE_ONCE(tp->snd_ssthresh, val);
 		break;
net/ipv4/tcp.c +1 -1
index 76df69aa01154cddd4c7940f3c3749d04487ae7b..a11c1b06ed5e5591c47f585a3f9e4fba4de30153 100644
@@ -456,7 +456,7 @@ void tcp_init_sock(struct sock *sk)
 	 * initialization of these values.
 	 */
 	tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
-	tp->snd_cwnd_clamp = ~0;
+	tp->snd_cwnd_clamp = TCP_MAX_SND_CWND;
 	tp->mss_cache = TCP_MSS_DEFAULT;
 
 	tp->reordering = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_reordering);
net/ipv4/tcp_metrics.c +2 -1
index d58f99c67d4dc0877d0e853c9e9faddd80343c22..ae551546422fe7c38b2ac3224f9ea9adbd3679d0 100644
@@ -495,7 +495,8 @@ void tcp_init_metrics(struct sock *sk)
 	}
 
 	if (tcp_metric_locked(tm, TCP_METRIC_CWND))
-		tp->snd_cwnd_clamp = tcp_metric_get(tm, TCP_METRIC_CWND);
+		tp->snd_cwnd_clamp = min(tcp_metric_get(tm, TCP_METRIC_CWND),
+					 TCP_MAX_SND_CWND);
 
 	val = READ_ONCE(net->ipv4.sysctl_tcp_no_ssthresh_metrics_save) ?
 	      0 : tcp_metric_get(tm, TCP_METRIC_SSTHRESH);
Signature · 1 line
2.53.0
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

- Whether the issue was actually triggered, or is only theoretical (e.g. found by code inspection). If it was triggered please include the symptoms, like the stack trace or error messages.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.

The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.

#2 lore raw
In reply to Eric Dumazet
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

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 patch sets TCP_MAX_SND_CWND to 1U << 24 (16,777,216).

--

tcp: bound snd_cwnd_clamp to TCP_MAX_SND_CWND

This patch changes the default snd_cwnd_clamp from ~0 to a finite TCP_MAX_SND_CWND of 2^24 packets. It also caps the locked RTAX_CWND metric, TCP_BPF_SNDCWND_CLAMP and TCP_BPF_IW at that value. The stated goal is to stop congestion control u32 arithmetic from misbehaving when cwnd gets very large.

include/net/tcp.h static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp) · line 1560

Quoted 13 lines
include/net/tcp.h +5 -0
index 0265c27a5fd77..be1609cd78840 100644
@@ -1553,6 +1553,12 @@ static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
 
 #define TCP_INFINITE_SSTHRESH	0x7fffffff
 
+/* Default and maximum snd_cwnd_clamp, in packets.
+ * Far above any usable BDP, but low enough that congestion control
+ * arithmetic such as 100 * cwnd in CUBIC cannot overflow a u32.
+ */
+#define TCP_MAX_SND_CWND	(1U << 24)

[Severity: High]
Is 2^24 small enough for the claim in this comment? 100 * cwnd fits, but CUBIC has larger u32 products on cwnd:

net/ipv4/tcp_cubic.c:cubictcp_recalc_ssthresh() {
    ...
	if (tcp_snd_cwnd(tp) < ca->last_max_cwnd && fast_convergence)
		ca->last_max_cwnd = (tcp_snd_cwnd(tp) * (BICTCP_BETA_SCALE + beta))
			/ (2 * BICTCP_BETA_SCALE);
    ...
	return max((tcp_snd_cwnd(tp) * beta) / BICTCP_BETA_SCALE, 2U);
}

With beta = 717:

cwnd * 1741 wraps once cwnd is above about 2.47M. cwnd * 717 wraps once cwnd is above about 5.99M.

Both points are below 2^24.

At cwnd = 2^24, ssthresh comes out as 3,358,720 instead of 11,747,328. last_max_cwnd comes out as 1,679,360 instead of about 14.3M.

Just above each wrap point the product is close to zero, so ssthresh would drop to a few thousand or to the 2U floor. The wrong Wmax then reaches bictcp_update(), and the comment there says that code is only overflow-free for cwnd below 1 million packets.

Other in-tree modules have the same pattern:

dctcp_ssthresh(): cwnd * dctcp_alpha (up to 1024, also the

                             default), wraps at 2^22
  bictcp_recalc_ssthresh():  cwnd * 1843 and cwnd * 819
  tcp_cdg_ssthresh():        cwnd * min(1024U, backoff_beta)
  tcpnv_recalc_ssthresh():   cwnd * nv_loss_dec_factor (default 819)
  tcp_roccet.c:              cwnd * beta and cwnd * (1024 + beta)

DCTCP at cwnd = 2^24 is the clearest case:

	return max(tcp_snd_cwnd(tp) - ((tcp_snd_cwnd(tp) * ca->dctcp_alpha) >> 11U), 2U);

Here 2^24 * 1024 wraps to 0. ssthresh then equals cwnd, so the ECN window reduction never happens.

The inputs named in the commit message can still put cwnd at the clamp directly. That skips any real growth in flight:

tcp_init_cwnd() returns min_t(__u32, cwnd, tp->snd_cwnd_clamp) for RTAX_INITCWND
TCP_BPF_IW now sets min_t(u32, val, tp->snd_cwnd_clamp)

Either path can set cwnd to 2^24. The next RTO or recovery then calls the CC ssthresh callback with that cwnd:

tcp_enter_loss()->icsk_ca_ops->ssthresh() tcp_init_cwnd_reduction()->icsk_ca_ops->ssthresh()

The sk_sndbuf argument in the commit message limits packets_out. These callbacks read snd_cwnd, not packets_out.

The generic stack has a similar product as well:

net/ipv4/tcp_output.c:tcp_tso_should_defer() {
    ...
	u32 chunk = min(tp->snd_wnd, tcp_snd_cwnd(tp) * tp->mss_cache);
    ...
}

With a 1448 byte MSS, this wraps above about 2.97M packets.

Would a lower bound work here, around 2^21 or less to cover multipliers up to 2048? The other option is widening these products to u64.

Quoted 4 lines
include/net/tcp.h +1 -0
+
 static inline u32 tcp_snd_cwnd(const struct tcp_sock *tp)
 {
 	return tp->snd_cwnd;

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261027130617.1550253-1-edumazet%40kernel.org
author maintainer
#3 lore raw
Quoted 106 lines · Le mer. 28 oct. 2026 à 20:08, <[email protected]> a écrit :

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

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 patch sets TCP_MAX_SND_CWND to 1U << 24 (16,777,216).

--

tcp: bound snd_cwnd_clamp to TCP_MAX_SND_CWND

This patch changes the default snd_cwnd_clamp from ~0 to a finite TCP_MAX_SND_CWND of 2^24 packets. It also caps the locked RTAX_CWND metric, TCP_BPF_SNDCWND_CLAMP and TCP_BPF_IW at that value. The stated goal is to stop congestion control u32 arithmetic from misbehaving when cwnd gets very large.

include/net/tcp.h static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp) · line 1560

Quoted 13 lines
include/net/tcp.h +5 -0
index 0265c27a5fd77..be1609cd78840 100644
@@ -1553,6 +1553,12 @@ static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
 
 #define TCP_INFINITE_SSTHRESH        0x7fffffff
 
+/* Default and maximum snd_cwnd_clamp, in packets.
+ * Far above any usable BDP, but low enough that congestion control
+ * arithmetic such as 100 * cwnd in CUBIC cannot overflow a u32.
+ */
+#define TCP_MAX_SND_CWND     (1U << 24)

[Severity: High]
Is 2^24 small enough for the claim in this comment? 100 * cwnd fits, but CUBIC has larger u32 products on cwnd:

net/ipv4/tcp_cubic.c:cubictcp_recalc_ssthresh() {
    ...
        if (tcp_snd_cwnd(tp) < ca->last_max_cwnd && fast_convergence)
                ca->last_max_cwnd = (tcp_snd_cwnd(tp) * (BICTCP_BETA_SCALE + beta))
                        / (2 * BICTCP_BETA_SCALE);
    ...
        return max((tcp_snd_cwnd(tp) * beta) / BICTCP_BETA_SCALE, 2U);
}

With beta = 717:

cwnd * 1741 wraps once cwnd is above about 2.47M. cwnd * 717 wraps once cwnd is above about 5.99M.

Both points are below 2^24.

At cwnd = 2^24, ssthresh comes out as 3,358,720 instead of 11,747,328. last_max_cwnd comes out as 1,679,360 instead of about 14.3M.

Just above each wrap point the product is close to zero, so ssthresh would drop to a few thousand or to the 2U floor. The wrong Wmax then reaches bictcp_update(), and the comment there says that code is only overflow-free for cwnd below 1 million packets.

Other in-tree modules have the same pattern:

dctcp_ssthresh(): cwnd * dctcp_alpha (up to 1024, also the

                             default), wraps at 2^22
  bictcp_recalc_ssthresh():  cwnd * 1843 and cwnd * 819
  tcp_cdg_ssthresh():        cwnd * min(1024U, backoff_beta)
  tcpnv_recalc_ssthresh():   cwnd * nv_loss_dec_factor (default 819)
  tcp_roccet.c:              cwnd * beta and cwnd * (1024 + beta)

DCTCP at cwnd = 2^24 is the clearest case:

        return max(tcp_snd_cwnd(tp) - ((tcp_snd_cwnd(tp) * ca->dctcp_alpha) >> 11U), 2U);

Here 2^24 * 1024 wraps to 0. ssthresh then equals cwnd, so the ECN window reduction never happens.

The inputs named in the commit message can still put cwnd at the clamp directly. That skips any real growth in flight:

tcp_init_cwnd() returns min_t(__u32, cwnd, tp->snd_cwnd_clamp) for RTAX_INITCWND
TCP_BPF_IW now sets min_t(u32, val, tp->snd_cwnd_clamp)

Either path can set cwnd to 2^24. The next RTO or recovery then calls the CC ssthresh callback with that cwnd:

tcp_enter_loss()->icsk_ca_ops->ssthresh() tcp_init_cwnd_reduction()->icsk_ca_ops->ssthresh()

The sk_sndbuf argument in the commit message limits packets_out. These callbacks read snd_cwnd, not packets_out.

The generic stack has a similar product as well:

net/ipv4/tcp_output.c:tcp_tso_should_defer() {
    ...
        u32 chunk = min(tp->snd_wnd, tcp_snd_cwnd(tp) * tp->mss_cache);
    ...
}
Quoted 4 lines

With a 1448 byte MSS, this wraps above about 2.97M packets.

Would a lower bound work here, around 2^21 or less to cover multipliers up to 2048? The other option is widening these products to u64.

Good point, 2^24 is too big: the ssthresh() handlers multiply cwnd by factors up to 2047 (CUBIC/BIC/ROCCET 1024 + beta), and DCTCP wraps to zero at 2^24 with alpha = 1024.

I will send a v2 using 2^20 instead. This covers all in-tree multipliers (< 2048), and also matches the "cwnd < 1 million packets" assumption documented in CUBIC bictcp_update().

2^20 packets does not limit real flows: a peer can not advertise more than 65535 << 14 bytes (~1GB), which is below 2^20 packets for any MSS >= 1024.

I prefer this over widening each product to u64, which would touch many CC modules and still leave bictcp_update() limits in place.

The tcp_tso_should_defer() products (cwnd * mss_cache) can overflow with large MSS regardless of this patch, but they only affect the TSO deferral heuristic. This can be addressed separately in net-next.

pw-bot: cr