Netdev List
 help / color / mirror / Atom feed
From: lei lei <llleigoing@outlook.com>
To: "netdev@vger.kernel.org" <netdev@vger.kernel.org>
Cc: "mptcp@lists.linux.dev" <mptcp@lists.linux.dev>,
	"edumazet@kernel.org" <edumazet@kernel.org>,
	"ncardwell@google.com" <ncardwell@google.com>,
	"kuniyu@google.com" <kuniyu@google.com>,
	"davem@davemloft.net" <davem@davemloft.net>,
	"kuba@kernel.org" <kuba@kernel.org>,
	"pabeni@redhat.com" <pabeni@redhat.com>,
	"horms@kernel.org" <horms@kernel.org>,
	"matttbe@kernel.org" <matttbe@kernel.org>,
	"martineau@kernel.org" <martineau@kernel.org>,
	"geliang@kernel.org" <geliang@kernel.org>
Subject: [PATCH net 2/3] net: bound receive window predictions before narrowing
Date: Tue, 17 Nov 2026 05:22:02 +0000	[thread overview]
Message-ID: <20261117052200.1507847-3-llleigoing@outlook.com> (raw)
In-Reply-To: <20261117052200.1507847-1-llleigoing@outlook.com>

tcp_rcvbuf_grow() and mptcp_rcvbuf_grow() compute growth terms in u64 but
accumulate the predicted receive window in u32. The prediction can wrap
before conversion to a memory budget and application of tcp_rmem[2].

For example, oldval=16 KiB and newval=4104 KiB produce a slow-start
window prediction of 4 GiB + 8 MiB. Storing it in u32 leaves only 8 MiB,
so the memory limit is applied to an already truncated prediction.

Keep rcvwin in u64 and promote newval before the initial shift. Clamp
the completed prediction at INT_MAX before passing it to the existing
int-input window-to-memory helpers. Since scaling_ratio is a nonzero
u8, the required memory is at least as large as the window. Predictions
above INT_MAX therefore already require a saturated receive budget.
The helpers' return saturation and the existing tcp_rmem[2] limit then
provide the appropriate memory bound.

TCP and MPTCP loopback tests using small warm-up reads followed by a
queued burst of about 4 MiB previously left the budget below the
configured 64 MiB limit. With this change, both reach that limit.

Fixes: aa251c84636c ("tcp: fix too slow tcp_rcvbuf_grow() action")
Signed-off-by: lllei <llleigoing@outlook.com>
---
 net/ipv4/tcp_input.c | 11 ++++++-----
 net/mptcp/protocol.c |  9 +++++----
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index a2074a1af..6d5950dbe 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -940,9 +940,9 @@ void tcp_rcvbuf_grow(struct sock *sk, u32 newval)
 {
 	const struct net *net = sock_net(sk);
 	struct tcp_sock *tp = tcp_sk(sk);
-	u32 rcvwin, rcvbuf, cap, oldval;
+	u32 rcvbuf, cap, oldval;
 	u32 rtt_threshold, rtt_us;
-	u64 grow;
+	u64 rcvwin, grow;
 
 	oldval = tp->rcvq_space.space;
 	tp->rcvq_space.space = newval;
@@ -952,7 +952,7 @@ void tcp_rcvbuf_grow(struct sock *sk, u32 newval)
 		return;
 
 	/* DRS is always one RTT late. */
-	rcvwin = newval << 1;
+	rcvwin = (u64)newval << 1;
 
 	rtt_us = tp->rcv_rtt_est.rtt_us >> 3;
 	rtt_threshold = READ_ONCE(net->ipv4.sysctl_tcp_rcvbuf_low_rtt);
@@ -961,10 +961,10 @@ void tcp_rcvbuf_grow(struct sock *sk, u32 newval)
 		 * It might take few additional ms to reach 'line rate',
 		 * but will avoid sk_rcvbuf inflation and poor cache use.
 		 */
-		grow = div_u64((u64)rcvwin * rtt_us, rtt_threshold);
+		grow = div_u64(rcvwin * rtt_us, rtt_threshold);
 	} else {
 		/* slow start: allow the sender to double its rate. */
-		grow = div_u64(((u64)rcvwin << 1) * (newval - oldval), oldval);
+		grow = div_u64((rcvwin << 1) * (newval - oldval), oldval);
 	}
 	rcvwin += grow;
 
@@ -973,6 +973,7 @@ void tcp_rcvbuf_grow(struct sock *sk, u32 newval)
 
 	cap = READ_ONCE(net->ipv4.sysctl_tcp_rmem[2]);
 
+	rcvwin = min_t(u64, rcvwin, INT_MAX);
 	rcvbuf = min_t(u32, tcp_space_from_win(sk, rcvwin), cap);
 	if (rcvbuf > sk->sk_rcvbuf) {
 		WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c
index 8968db580..b3b979403 100644
--- a/net/mptcp/protocol.c
+++ b/net/mptcp/protocol.c
@@ -216,8 +216,8 @@ static bool mptcp_rcvbuf_grow(struct sock *sk, u32 newval)
 {
 	struct mptcp_sock *msk = mptcp_sk(sk);
 	const struct net *net = sock_net(sk);
-	u32 rcvwin, rcvbuf, cap, oldval;
-	u64 grow;
+	u32 rcvbuf, cap, oldval;
+	u64 rcvwin, grow;
 
 	oldval = msk->rcvq_space.space;
 	msk->rcvq_space.space = newval;
@@ -226,15 +226,16 @@ static bool mptcp_rcvbuf_grow(struct sock *sk, u32 newval)
 		return false;
 
 	/* DRS is always one RTT late. */
-	rcvwin = newval << 1;
+	rcvwin = (u64)newval << 1;
 
 	/* slow start: allow the sender to double its rate. */
-	grow = (u64)rcvwin * (newval - oldval);
+	grow = rcvwin * (newval - oldval);
 	do_div(grow, oldval);
 	rcvwin += grow << 1;
 
 	cap = READ_ONCE(net->ipv4.sysctl_tcp_rmem[2]);
 
+	rcvwin = min_t(u64, rcvwin, INT_MAX);
 	rcvbuf = min_t(u32, mptcp_space_from_win(sk, rcvwin), cap);
 	if (rcvbuf > sk->sk_rcvbuf) {
 		WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
-- 
2.43.0


  parent reply	other threads:[~2026-11-17  5:22 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  5:22 [PATCH net 0/3] tcp, mptcp: fix receive buffer sizing and peek accounting lei lei
2026-11-17  5:22 ` [PATCH net 1/3] tcp: saturate window-to-memory conversions at INT_MAX lei lei
2026-11-19  7:54   ` Eric Dumazet
2026-11-17  5:22 ` lei lei [this message]
2026-11-19  7:56   ` [PATCH net 2/3] net: bound receive window predictions before narrowing Eric Dumazet
2026-11-17  5:22 ` [PATCH net 3/3] mptcp: exclude MSG_PEEK from receive buffer autotuning lei lei
2026-11-19  7:59   ` Eric Dumazet
2026-11-19 18:33     ` Matthieu Baerts

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117052200.1507847-3-llleigoing@outlook.com \
    --to=llleigoing@outlook.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=geliang@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=martineau@kernel.org \
    --cc=matttbe@kernel.org \
    --cc=mptcp@lists.linux.dev \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox