Skip to contentSkip to conversation

tcp, mptcp: fix receive buffer sizing and peek accounting

PATCH In review lei lei 7 participants 20 messages Nov 17 – Nov 29

Subsystems (4)
lore
Share & tools
lore

lei lei

author
#0 lore raw

cover tcp, mptcp: fix receive buffer sizing and peek accounting

This series fixes receive-buffer sizing and accounting errors in TCP and MPTCP.

The first two patches prevent integer truncation from producing an undersized receive-memory budget. __tcp_space_from_win() can return a truncated int, while tcp_rcvbuf_grow() and mptcp_rcvbuf_grow() can wrap a u32 window prediction. The fixes saturate the conversion result at INT_MAX and keep growth predictions in u64 until they are bounded for the existing helper interface. Patch 2 depends on patch 1.

The third patch restores the MSG_PEEK exclusion from MPTCP receive autotuning. Repeated peeks currently count the same queued bytes as application consumption, which can inflate the receive-memory budget without the application draining the queue.

Validation used targeted IPv4 loopback socket tests on unpatched and patched kernels in x86_64 QEMU/KVM guests. GCC 13.3 builds completed without warnings. The tests confirmed that, with the fixes applied:

- Large SO_RCVLOWAT settings on TCP and MPTCP correctly increase the receive-memory budget to INT_MAX when tcp_rmem[2] is INT_MAX and the window-to-memory conversion exceeds the representable range.
- For queued bursts whose predicted window exceeds 32 bits, TCP and MPTCP receive budgets now reach the configured 64 MiB limit.
- Repeated MSG_PEEK calls leave both a fixed 32 KiB MPTCP receive queue and its memory budget unchanged. A subsequent consuming read returns the original data intact.

The investigation began with sk_rcvbuf failing to grow despite application reads during normal use, constraining the advertised receive window.

OpenAI Codex assisted with designing trigger conditions and socket tests for reproduction and validation.

lllei (3):
tcp: saturate window-to-memory conversions at INT_MAX
net: bound receive window predictions before narrowing
mptcp: exclude MSG_PEEK from receive buffer autotuning

 include/net/tcp.h    |  2 +-
 net/ipv4/tcp_input.c | 11 ++++++-----
 net/mptcp/protocol.c | 12 +++++++-----
 3 files changed, 14 insertions(+), 11 deletions(-)

base-commit: aa40afa831ebfe45e40a23721711059f2ac584d4

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/3 tcp: saturate window-to-memory conversions at INT_MAX

__tcp_space_from_win() computes a receive-memory requirement in u64 but returns int. A result above INT_MAX can therefore become negative or wrap to a smaller positive value before the caller compares or limits it.

TCP and MPTCP SO_RCVLOWAT handling compare this result with sk_rcvbuf. They can accept a large low watermark without increasing the receive budget because the truncated requirement appears smaller than the existing buffer. Receive autotuning uses the same conversion helper.

Saturate the u64 result at INT_MAX before returning it. This matches the int type of sk_rcvbuf and preserves the value and rounding of every representable result.

TCP and MPTCP loopback tests with tcp_rmem[2]=INT_MAX and SO_RCVLOWAT=INT_MAX/2 reproduce the missed update after learning a scaling ratio below 128. With this change, both budgets reach INT_MAX.

Fixes dfa2f0483360 ("tcp: get rid of sysctl_tcp_adv_win_scale")
---
 include/net/tcp.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/net/tcp.h +1 -1
index 5c9203aae..09827f1ae 100644
@@ -1799,7 +1799,7 @@ static inline int __tcp_space_from_win(u8 scaling_ratio, int win)
 	u64 val = (u64)win << TCP_RMEM_TO_WIN_SCALE;
 
 	do_div(val, scaling_ratio);
-	return val;
+	return min_t(u64, val, INT_MAX);
 }
 
 static inline int tcp_space_from_win(const struct sock *sk, int win)
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH net 1/3] tcp: saturate window-to-memory conversions at INT_MAX

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] tcp: invalid Signed-off-by trailer using handle instead of real name --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] tcp: invalid Signed-off-by trailer using handle instead of real name
--

commit 7ae76d33251cff52d3abaf1bc0db22dbfc70f04d
Author: lei lei <[email protected]>

Quoted 15 lines

tcp: saturate window-to-memory conversions at INT_MAX

__tcp_space_from_win() computes a receive-memory requirement in u64 but returns int. A result above INT_MAX can therefore become negative or wrap to a smaller positive value before the caller compares or limits it.

TCP and MPTCP SO_RCVLOWAT handling compare this result with sk_rcvbuf. They can accept a large low watermark without increasing the receive budget because the truncated requirement appears smaller than the existing buffer. Receive autotuning uses the same conversion helper.

Saturate the u64 result at INT_MAX before returning it. This matches the int type of sk_rcvbuf and preserves the value and rounding of every representable result.

Quoted 6 lines

TCP and MPTCP loopback tests with tcp_rmem[2]=INT_MAX and SO_RCVLOWAT=INT_MAX/2 reproduce the missed update after learning a scaling ratio below 128. With this change, both budgets reach INT_MAX.

Fixes dfa2f0483360 ("tcp: get rid of sysctl_tcp_adv_win_scale")

[Severity: High]
The Signed-off-by trailer uses the single-word handle "lllei" instead of a real human name matching the Author header, which violates Developer's Certificate of Origin requirements in Documentation/process/submitting-patches.rst.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#3 lore raw

Re: [PATCH net 1/3] tcp: saturate window-to-memory conversions at INT_MAX

In reply to lei lei
Quoted 10 lines · lei lei

__tcp_space_from_win() computes a receive-memory requirement in u64 but returns int. A result above INT_MAX can therefore become negative or wrap to a smaller positive value before the caller compares or limits it.

TCP and MPTCP SO_RCVLOWAT handling compare this result with sk_rcvbuf. They can accept a large low watermark without increasing the receive budget because the truncated requirement appears smaller than the existing buffer. Receive autotuning uses the same conversion helper.

Quoted 7 lines

Saturate the u64 result at INT_MAX before returning it. This matches the int type of sk_rcvbuf and preserves the value and rounding of every representable result.

TCP and MPTCP loopback tests with tcp_rmem[2]=INT_MAX and SO_RCVLOWAT=INT_MAX/2 reproduce the missed update after learning a scaling ratio below 128. With this change, both budgets reach INT_MAX.

I do not think this is a fix, and I do not think it belongs to net tree.

The conversion overflows only if

val * 256 / scaling_ratio > INT_MAX

which is the same thing as saying that @val is bigger than the window an INT_MAX sk_rcvbuf can offer at this scaling_ratio.

sk_rcvbuf being an int, the requested SO_RCVLOWAT can not be honored in this case, with or without your patch. With scaling_ratio == 64, the biggest possible window is ~512MB, a 1GB lowat is never reached.

So the changelog saying the budget is now "correct" is misleading:
sk_rcvbuf is set to INT_MAX and the application is still woken up before sk_rcvlowat bytes are queued, from tcp_epollin_ready(), when the receive window is about to close.

Note that scaling_ratio is not a constant, it is updated in tcp_measure_rcv_mss() for the lifetime of the flow. A sk_rcvbuf sized at setsockopt() time is too small as soon as the ratio decreases, far below the overflow point. Huge SO_RCVLOWAT values are best effort.

To hit this, the admin has to set tcp_rmem[2] above 2GB, and the application has to call setsockopt(SO_RCVLOWAT) with a ~1GB value after a low scaling_ratio was learned. (Before any data is received, the ratio is 128 and @val is capped to tcp_rmem[2]/2, no overflow is possible.)

Also, with your patch, sk_rcvbuf can be set to INT_MAX while tcp_rmem[2] is smaller than that (say 3GB/2, scaling_ratio 64), because __tcp_set_rcvlowat() and mptcp_set_rcvlowat() do not clamp @space to tcp_rmem[2]. This is not new, but saturating makes it more visible.

Your cover letter mentions that this started with sk_rcvbuf failing to grow "during normal use". What was the workload, and what were the tcp_rmem values ? I would prefer we look at the real problem.

If you still want the saturation as a cleanup, please send it to net-next, without a Fixes: tag, and with a changelog describing what is really changed for the user.

Quoted 3 lines
Fixes dfa2f0483360 ("tcp: get rid of sysctl_tcp_adv_win_scale")

Sashiko already pointed this: your From: is "lei lei", this
Signed-off-by: is "lllei". Please use your real name in both, see Documentation/process/submitting-patches.rst.

Thanks.

pw-bot: cr

#4 lore raw

Re: [PATCH net 1/3] tcp: saturate window-to-memory conversions at INT_MAX

In reply to lei lei
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…

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 author and DCO sign-off names do not match.
- [Low] (Baseline decision: not a valid regression; kept only because requires_validation=true.) The concern: when scaling_ratio < 128 and…

--

tcp: saturate window-to-memory conversions at INT_MAX

This commit caps the u64 result of __tcp_space_from_win() at INT_MAX before it is returned as an int. Without the cap, large results can wrap when narrowed. That lets the TCP and MPTCP SO_RCVLOWAT handlers accept a large low watermark without raising sk_rcvbuf.

Quoted 2 lines
Fixes dfa2f0483360 ("tcp: get rid of sysctl_tcp_adv_win_scale")

[Severity: Low]
This isn't a bug, but the Signed-off-by name does not match the From name. The author is "lei lei <[email protected]>" and the sign-off is "lllei <[email protected]>". The email is the same but the names differ, and checkpatch.pl reports this as FROM_SIGN_OFF_MISMATCH.

The other two patches in the series have the same mismatch:

net: bound receive window predictions before narrowing
mptcp: exclude MSG_PEEK from receive buffer autotuning

Could the series be resent with one consistent identity?

include/net/tcp.h static inline int __tcp_space_from_win(u8 scaling_ratio, int win) · line 1803

Quoted 11 lines
include/net/tcp.h +1 -1
index 5c9203aae8341..09827f1ae0a72 100644
@@ -1799,7 +1799,7 @@ static inline int __tcp_space_from_win(u8 scaling_ratio, int win)
 	u64 val = (u64)win << TCP_RMEM_TO_WIN_SCALE;
 
 	do_div(val, scaling_ratio);
-	return val;
+	return min_t(u64, val, INT_MAX);
 }

[Severity: Low]
This probably isn't a regression, but one side effect of the saturation is worth checking.

When scaling_ratio is below 128 and val is around tcp_rmem[2] >> 1, the result saturates to INT_MAX. Then __tcp_win_from_space(ratio, INT_MAX) is smaller than val, so sk_rcvbuf no longer covers sk_rcvlowat.

__tcp_set_rcvlowat() in net/ipv4/tcp.c still stores the full watermark and raises window_clamp to val:

	WRITE_ONCE(sk->sk_rcvlowat, val ? : 1);
	...
	space = tcp_space_from_win(sk, val);
	if (space > sk->sk_rcvbuf) {
		WRITE_ONCE(sk->sk_rcvbuf, space);

		if (tp->window_clamp && tp->window_clamp < val)
			WRITE_ONCE(tp->window_clamp, val);
	}

mptcp_set_rcvlowat() in net/mptcp/sockopt.c does the same for the msk and each subflow.

This seems harmless:

- __tcp_select_window() takes min_t(int, tp->window_clamp, allowed_space), so the larger clamp is never advertised.

- For TCP, tcp_epollin_ready() falls back to tcp_rmem_pressure(), so a watermark the buffer cannot reach does not stall the reader.

- The SOCK_RCVBUF_LOCK branch already accepts a watermark that the buffer cannot back.

- Before this patch the budget was not raised at all.

mptcp_epollin_ready() has no rmem-pressure fallback. That limitation predates this patch and can also be reached through the SOCK_RCVBUF_LOCK path.

Should the commit message say that sk_rcvbuf can still be too small for the requested SO_RCVLOWAT once it saturates?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117052200.1507847-1-llleigoing%40outlook.com
author
#5 lore raw

PATCH 2/3 net: bound receive window predictions before narrowing

In reply to lei lei

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")
---
 net/ipv4/tcp_input.c | 11 ++++++-----
 net/mptcp/protocol.c |  9 +++++----
 2 files changed, 11 insertions(+), 9 deletions(-)
net/ipv4/tcp_input.c +6 -5
index a2074a1af..6d5950dbe 100644
@@ -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);
net/mptcp/protocol.c +5 -4
index 8968db580..b3b979403 100644
@@ -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);
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH net 2/3] net: bound receive window predictions before narrowing

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] net: invalid handle and author mismatch in Signed-off-by trailer --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] net: invalid handle and author mismatch in Signed-off-by trailer
--

commit 0e17123a2c1e28d81f7aa6b0170c8d97f7b356e5
Author: lei lei <[email protected]>

Quoted 18 lines

net: bound receive window predictions before narrowing

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.

Quoted 6 lines

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")

[Severity: High]
The Signed-off-by trailer uses the single-word handle "lllei" instead of a real human name and does not match the commit Author header ("lei lei"). Documentation/process/submitting-patches.rst requires a full real name in Signed-off-by that matches the commit author.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer
#7 lore raw

Re: [PATCH net 2/3] net: bound receive window predictions before narrowing

In reply to lei lei
Quoted 8 lines · lei lei

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.

The arithmetic is right, but I do not see a bug worth a net patch and stable backports.

This needs @newval to be ~256 times @oldval between two consecutive measurements. The "allow the sender to double its rate" logic was never meant to be meaningful for such a sample: a 4GB prediction out of 4MB copied to user space is garbage, wrapped or not.

What happens today: this round gets a smaller sk_rcvbuf increase than the formula asked for (or none). sk_rcvbuf never shrinks here, and the next measurement has a sane @oldval. So we lose one step.

What happens with your patch: a single sample like this sends sk_rcvbuf straight to tcp_rmem[2].

This is the kind of sk_rcvbuf inflation we have been trying to avoid lately, see commit aa251c84636c and the tcp_rcvbuf_low_rtt logic. I am not convinced this is an improvement.

Quoted 9 lines

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.

Quoted 3 lines

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.

For TCP, the slow start branch is only taken if the RTT is above tcp_rcvbuf_low_rtt (1ms by default). I guess you had to change this sysctl to get this result over loopback ? Please describe the full setup, including tcp_rmem[1].

More importantly, reaching tcp_rmem[2] after a single 4MB burst is not a goal. What is the user visible problem here ? Do you have numbers (throughput, drops, rcv window) for a real flow ?

If there is a real case where growth is too slow, I would rather discuss the heuristic than the type of @rcvwin.

If you send this again:

- net-next, no Fixes: tag, unless you can show a real regression.

- Please split TCP and MPTCP changes. One patch with a "net:" prefix touching both, with a single Fixes: tag pointing to a TCP commit is not right for mptcp_rcvbuf_grow().

Quoted 3 lines
Fixes aa251c84636c ("tcp: fix too slow tcp_rcvbuf_grow() action")

Same remark about the Signed-off-by: than for patch 1.

Thanks.

#8 lore raw

Re: [PATCH net 2/3] net: bound receive window predictions before narrowing

Hi Eric,

Sorry for the late reply -- it took me some time to put together clean WAN reproducers and check the tracepoints carefully before getting back to you.

Reading your comments on patches 1 and 2, I realize my v1 changelogs did not explain the failure mode clearly and conflated two separate issues:

1) an implementation bug where 32-bit wrap coupled with the unconditional rcvq_space.space update can leave sk_rcvbuf stuck at its initial value across subsequent steady in-order streaming; and
2) the pre-existing overshoot of the slow-start heuristic itself on burst or backlog samples.

I think it is clearer to look at them separately. Replies inline below.

Quoted 3 lines · Eric Dumazet

What happens today: this round gets a smaller sk_rcvbuf increase than the formula asked for (or none). sk_rcvbuf never shrinks here, and the next measurement has a sane @oldval. So we lose one step.

Looking closer at tcp_rcvbuf_grow(), it seems this can lose more than one step -- once @rcvwin wraps, sk_rcvbuf can stay stuck at its initial value across subsequent steady in-order RTTs (barring an OOO packet hitting tcp_data_queue_ofo() or another multi-RTT stall).

In tcp_rcvbuf_grow(), tp->rcvq_space.space is overwritten with @newval at the top of the function, even when u32 @rcvwin wraps and sk->sk_rcvbuf (and therefore tp->window_clamp) is not updated:

  void tcp_rcvbuf_grow(struct sock *sk, u32 newval)
  {
          ...
          oldval = tp->rcvq_space.space;
          tp->rcvq_space.space = newval;   /* updated unconditionally */
          ...
          rcvwin = newval << 1;
          ...
          grow = div_u64(((u64)rcvwin << 1) * (newval - oldval), oldval);
          rcvwin += grow;                  /* wraps in u32 */
          ...
          rcvbuf = min_t(u32, tcp_space_from_win(sk, rcvwin), cap);
          if (rcvbuf > sk->sk_rcvbuf) {    /* false after wrap */
                  WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
                  WRITE_ONCE(tp->window_clamp,
                             tcp_win_from_space(sk, rcvbuf));
          }
  }

Concretely, taking the values from the TCP trace below (rtt_us = 100000, ooo = 0, scaling_ratio = 158):

1. Initial sk_rcvbuf = 6,382,000 gives tp->window_clamp = 3,938,890,

     with initial oldval = 14,480.

2. After an initial receiver stall, a measurement sees copied = 3,987,000 and inq = 39,096, so net @newval = 3,947,904:
- tp->rcvq_space.space becomes 3,947,904;
- the unnarrowed prediction is 4,297,614,134 (2^32 + 2,646,838), which wraps in u32 @rcvwin to 2,646,838 -> rcvbuf = 4,288,547;
- since rcvbuf < sk->sk_rcvbuf, neither sk->sk_rcvbuf nor tp->window_clamp changes.

3. Now tp->rcvq_space.space (3,947,904) exceeds tp->window_clamp (3,938,890) by 9,014 bytes. (tcp_measure_rcv_mss() clamps rcvq_space.space down to window_clamp only when scaling_ratio changes.)

4. Across the rest of the 64 MiB steady in-order transfer with continuous reads, each ~1-RTT sample stays below 3,947,904, so tcp_rcv_space_adjust() keeps taking:

if (copied <= tp->rcvq_space.space)
goto new_measure;

no further tcp_rcvbuf_grow tracepoint fires, and sk_rcvbuf stays at 6,382,000.

A similar lock-in happens in mptcp_rcvbuf_grow(): once msk->rcvq_space.space is set to 3,963,800 (with oldval = 14,600, wrapping 4,296,650,602 to 1,683,306 -> rcvbuf = 3,215,868 at scaling_ratio = 134), sk_rcvbuf stays at 6,553,600 (whose full-budget window at that ratio is ~3.43 MB) throughout steady streaming.

Quoted 8 lines

For TCP, the slow start branch is only taken if the RTT is above tcp_rcvbuf_low_rtt (1ms by default). I guess you had to change this sysctl to get this result over loopback ? Please describe the full setup, including tcp_rmem[1].

More importantly, reaching tcp_rmem[2] after a single 4MB burst is not a goal. What is the user visible problem here ? Do you have numbers (throughput, drops, rcv window) for a real flow ?

Yes -- my v1 loopback test for TCP had lowered tcp_rcvbuf_low_rtt (MPTCP has no low_rtt check). To test without touching that sysctl, I reran over a simulated WAN on 7.4.0-rc2 (mptcp_net-next commit 800508bf1ccd, keeping tcp_rcvbuf_low_rtt = 1000 us and tcp_timestamps = 1 at defaults, with tcp_wmem raised so the sender is not send-buffer-bound):

1) WAN reproducer setup and results:

I tuned tcp_rmem[1] and the initial write size here specifically to land inside the narrow (~1.8 KB on TCP, ~1.6 KB on MPTCP) non-growing wrap window and verify whether the flow recovers on subsequent RTTs; I do not have data suggesting this narrow window is hit frequently on mainline.

Trigger conditions:
- Path: two netns over veth + tc netem with 100 ms RTT and 1 Gbit/s (BDP = 12.5 MB), MTU 1500, TSO/GSO/GRO disabled.
- Budgets: tcp_rmem[1] ~ 6.1-6.25 MiB, tcp_rmem[2] = 64 MiB, tcp_wmem[1] = 16 MiB on TCP / 8 MiB on MPTCP, tcp_wmem[2] = 64 MiB.
- Traffic: Phase 1 queues an initial burst before draining so that the resulting sample lands in the first 4 GiB wrap window (on Phase 1's drain for MPTCP, and as Phase 2 starts for TCP); Phase 2 streams 64 MiB with normal continuous reads.

Results (Phase 2 application goodput over 64 MiB):
- Plain TCP (tcp_rmem = "4096 6382000 67108864"):
* unpatched: SO_RCVBUF stays 6382000 -> 6382000 -> 6382000; trace_tcp_rcvbuf_grow fires once (time=113917 rtt_us=100000 copied=3987000 inq=39096 space=14480 ooo=0 scaling_ratio=158 window_clamp=3938890); 64 MiB takes 1.823s (294.4 Mbit/s).
* with v1 patches 1+2: SO_RCVBUF reaches 67108864 and 64 MiB takes 0.767s (699.8 Mbit/s).

- MPTCP (tcp_rmem = "4096 6553600 67108864"):
* unpatched: SO_RCVBUF stays 6553600 -> 6553600 -> 6553600; trace_mptcp_rcvbuf_grow fires once; 64 MiB takes 2.019s (265.9 Mbit/s).
* with v1 patches 1+2: SO_RCVBUF reaches 67108864 and 64 MiB takes 0.815s (658.8 Mbit/s).

Standalone reproducer scripts for TCP and MPTCP:

     https://gist.githubusercontent.com/llleixx/5201855b015831b5377a02478f843949/raw/repro_tcp_lockin.sh
     https://gist.githubusercontent.com/llleixx/5201855b015831b5377a02478f843949/raw/repro_mptcp_lockin.sh

2) Original workload (your question on patch 1): I first ran into a stuck receive buffer on an Ubuntu 6.8 kernel with default tcp_rmem ("4096 131072 6291456") on MPTCP over a high-jitter path; in a local 6.8 reproduction of that setup, sk_rcvbuf stayed at 131,072 alongside msk->rcvq_space.space = 16,282,074. (On 6.8, mptcp_rcv_space_adjust() kept rcvwin in u64 but narrowed at int win in __tcp_space_from_win(), and its older msk rtt_us caching could allow a longer sampling interval.) While auditing current mainline from there, I noticed the u32 @rcvwin wrap above as well.

Quoted 8 lines

This needs @newval to be ~256 times @oldval between two consecutive measurements. The "allow the sender to double its rate" logic was never meant to be meaningful for such a sample: a 4GB prediction out of 4MB copied to user space is garbage, wrapped or not. [...]
What happens with your patch: a single sample like this sends sk_rcvbuf straight to tcp_rmem[2].

Quoted 6 lines

This is the kind of sk_rcvbuf inflation we have been trying to avoid lately, see commit aa251c84636c and the tcp_rcvbuf_low_rtt logic. I am not convinced this is an improvement.
[...]
If there is a real case where growth is too slow, I would rather discuss the heuristic than the type of @rcvwin.

Agreed -- a 4 GB prediction from a 4 MB read makes no sense, and jumping straight to tcp_rmem[2] on a single burst is undesirable.

It seems this overshoot is inherent to the current formula rather than caused by widening @rcvwin: even on unpatched mainline, with oldval = 14,480 and scaling_ratio = 158, a burst or OOO release of ~382 KiB already yields rcvwin > 39.5 MiB and sends sk_rcvbuf straight to a 64 MiB tcp_rmem[2] (~342 KiB already reaches ~51 MiB). Around ~3.95 MB, the u32 wrap turns that overshoot into the lock-in above.

Following the comment above the formula ("slow start: allow the sender to double its rate"), one option (with u64 @rcvwin) might be to cap the growth ratio (newval - oldval) / oldval at 1:

  grow = rcvwin << 1;
  if (newval - oldval < oldval)
          grow = div_u64(grow * (newval - oldval), oldval);

That leaves rising samples up to 2x unchanged while bounding larger jumps to a 6 * newval window (~3.6 MiB rcvbuf instead of 64 MiB for a 382 KiB burst, though a 3.95 MB burst still yields ~36.6 MiB). Since genuine slow-start samples can exceed 2x slightly (e.g. ~2.2x in commit aa251c84636c's trace), this would still need to be evaluated against the slow-start workloads from aa251c84636c to check the ramp-up vs. peak-budget trade-off.

Quoted 9 lines

If you send this again:

- net-next, no Fixes: tag, unless you can show a real regression.

- Please split TCP and MPTCP changes. One patch with a "net:" prefix touching both, with a single Fixes: tag pointing to a TCP commit is not right for mptcp_rcvbuf_grow().
[...]
Same remark about the Signed-off-by: than for patch 1.

Understood -- for any follow-up patches, I will target net-next without Fixes:, split TCP and MPTCP, and fix my From:/Signed-off-by:
consistency. On patch 1, I also agree with your point about huge best-effort SO_RCVLOWAT values.

To summarize:

1) The 32-bit @rcvwin wrap coupled with the unconditional rcvq_space.space update is an implementation bug that can leave sk_rcvbuf stuck across steady in-order streaming on both TCP and MPTCP, rather than only losing one step.
2) Jumping straight to tcp_rmem[2] on a burst is a separate limitation of the current unbounded slow-start extrapolation, which could potentially be tamed by bounding (newval - oldval) / oldval (e.g. at 1, subject to slow-start benchmarking).

The above is just my personal take on these issues -- I would really appreciate your thoughts and advice.

Thanks,
Lin

#9 lore raw

Re: [PATCH net 2/3] net: bound receive window predictions before narrowing

In reply to lei lei
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

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

Critical: 0 · High: 0 · Medium: 1 · 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).

- [Medium] In tcp_rcvbuf_grow() (net/ipv4/tcp_input.c), the slow-start growth term multiplies by (newval - oldval).
- [Low] The From:/Signed-off-by: names do not match.

--

net: bound receive window predictions before narrowing

This patch changes rcvwin in tcp_rcvbuf_grow() and mptcp_rcvbuf_grow() from u32 to u64, so the predicted receive window no longer wraps before it is converted to a memory budget. The prediction is then clamped at INT_MAX before it is passed to tcp_space_from_win() or mptcp_space_from_win().

Quoted 2 lines
Fixes aa251c84636c ("tcp: fix too slow tcp_rcvbuf_grow() action")

[Severity: Low]
The Author line is "lei lei <[email protected]>", but the sign-off is "lllei <[email protected]>". The email addresses match and the names don't. Could the names be made consistent? checkpatch will flag the mismatch. The other patches in the series have the same mismatch.

Quoted 4 lines
net/ipv4/tcp_input.c +0 -0
index a2074a1af4222..6d5950dbe6638 100644

[ ... ]

Quoted 14 lines
net/ipv4/tcp_input.c +2 -2
@@ -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;
 

net/ipv4/tcp_input.c void tcp_rcvbuf_grow(struct sock *sk, u32 newval) · line 979

Quoted 7 lines
net/ipv4/tcp_input.c +1 -0
@@ -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);

[Severity: Medium]
Can this clamp turn an underflowed prediction into a fully saturated subflow receive buffer?

The slow-start branch above computes (newval - oldval) in u32, and tcp_rcvbuf_grow() never checks that newval > oldval. For MPTCP subflows, mptcp_rcv_space_adjust() only compares copied against the msk-level space. It then calls tcp_rcvbuf_grow() on every subflow:

net/mptcp/protocol.c:mptcp_rcv_space_adjust() {
    ...
	if (copied <= msk->rcvq_space.space)
		goto new_measure;
    ...
	if (mptcp_rcvbuf_grow(sk, copied)) {
		...
		mptcp_for_each_subflow(msk, subflow) {
			...
			if (tcp_sk(ssk)->rcvq_space.space)
				tcp_rcvbuf_grow(ssk, copied);
    ...
}

The msk space comes from a single subflow. mptcp_rcv_space_init() sets it to min(rcv_wnd, TCP_INIT_CWND * advmss) of msk->first or the accepted ssk. A join subflow sets its own space in tcp_init_buffer_space(), from its own advmss and window.

For example, a first subflow with advmss 1460 gives an msk space of 14600. A join subflow on a 9000 MTU path starts at around 62720. Both values only grow after that, so the gap stays.

Suppose copied lands between those two values, e.g. copied=30000. That already gives an msk prediction of about 186 KB, above the default 128 KiB sk_rcvbuf, so mptcp_rcvbuf_grow() returns true. For the join subflow, newval - oldval then wraps to about 2^32. If that subflow's rtt_us >> 3 is at least tcp_rcvbuf_low_rtt, the slow-start grow term is around 1e10.

Before this patch, the u32 truncation gave an arbitrary result. With newval == oldval / 2 the sum was exactly 2^33, so nothing changed.

Now that rcvwin is u64 and clamped at INT_MAX, every such underflow sets sk_rcvbuf to tcp_rmem[2] on that subflow. window_clamp then becomes tcp_win_from_space(sk, tcp_rmem[2]).

The commit message says:

Predictions above INT_MAX therefore already require a saturated receive budget.

That assumes the prediction is meaningful, which isn't true when newval < oldval.

The subtraction was already there before this patch. Would it make sense to skip the slow-start growth when newval <= oldval?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117052200.1507847-1-llleigoing%40outlook.com
#10 lore raw

Re: [PATCH net 2/3] net: bound receive window predictions before narrowing

Hi Matthieu, Paolo, Eric,

Following up on the MPTCP subflow issue Sashiko flagged on patch 2/3:

Quoted 2 lines · On Fri, 20 Nov 2026 08:22:53 +0000, [email protected] wrote:

Can this clamp turn an underflowed prediction into a fully saturated subflow receive buffer?

[...]

Quoted 2 lines

Before this patch, the u32 truncation gave an arbitrary result. With newval == oldval / 2 the sum was exactly 2^33, so nothing changed.

[...]

Quoted 2 lines

The subtraction was already there before this patch. Would it make sense to skip the slow-start growth when newval <= oldval?

Yes, this heterogeneous-subflow case (e.g. an MTU 1500 initial subflow and an MTU 9000 MP_JOIN subflow, with @copied landing between their rcvq_space.space values) can indeed happen: (newval - oldval) underflows and produces a bogus slow-start prediction.

Cases like newval == oldval / 2 leaving sk_rcvbuf unchanged before v1 only happened by coincidence because 2^33 wrapped to 0 in u32 (in a local test of this MTU 1500 + 9000 setup on unpatched 7.4.0-rc2, a 30 KB read already sends the join subflow's sk_rcvbuf straight to tcp_rmem[2] and moves its rcvq_space.space backwards). Relying on 32-bit wraparound is not valid behavior; avoiding bogus growth here should be handled explicitly by the algorithm.

If this seems worth addressing, I see two main options:

1) Option 1: Skip slow-start growth when newval <= oldval in tcp_rcvbuf_grow(), as Sashiko suggested.

This avoids the underflow without affecting tcp_data_queue_ofo() (which passes newval == oldval), but pure TCP callers already guarantee newval >= oldval, and tp->rcvq_space.space = newval at the top of tcp_rcvbuf_grow() would still move the larger-MTU subflow's space estimate backwards.

2) Option 2: Keep tcp_rcvbuf_grow() unchanged and clamp the sample to at least the subflow's existing rcvq_space.space in MPTCP:

net/mptcp/protocol.c +4 -2
@@ -2321,12 +2321,14 @@ static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
 		mptcp_for_each_subflow(msk, subflow) {
 			struct sock *ssk;
 			bool slow;
+			int space;
 
 			ssk = mptcp_subflow_tcp_sock(subflow);
 			slow = lock_sock_fast(ssk);
+			space = tcp_sk(ssk)->rcvq_space.space;
 			/* subflows can be added before tcp_init_transfer() */
-			if (tcp_sk(ssk)->rcvq_space.space)
-				tcp_rcvbuf_grow(ssk, copied);
+			if (space)
+				tcp_rcvbuf_grow(ssk, max(copied, space));
 			unlock_sock_fast(ssk, slow);
 		}
 	}

Personally, Option 2 seems cleaner to me: it fixes the issue at the only caller that can pass a smaller sample, preserves the subflow's existing space estimate, zeros the slow-start delta when copied < space, and still lets TCP account for its own OOO queue (per commit 6b329393502e) and low_rtt policy.

What do you think? If this looks reasonable to you, I plan to submit a standalone patch.

Thanks,
Lin

maintainer
#11 lore raw

Re: [PATCH net 2/3] net: bound receive window predictions before narrowing

Hi Lin Lei,

Thank you for looking at this issue.

29 Nov 2026 07:38:02 Lin Lei <[email protected]>:

Quoted 62 lines

Hi Matthieu, Paolo, Eric,

Following up on the MPTCP subflow issue Sashiko flagged on patch 2/3:

Quoted 2 lines · On Fri, 20 Nov 2026 08:22:53 +0000, [email protected] wrote:

Can this clamp turn an underflowed prediction into a fully saturated subflow receive buffer?

[...]

Quoted 2 lines

Before this patch, the u32 truncation gave an arbitrary result. With newval == oldval / 2 the sum was exactly 2^33, so nothing changed.

[...]

Quoted 2 lines

The subtraction was already there before this patch. Would it make sense to skip the slow-start growth when newval <= oldval?

Yes, this heterogeneous-subflow case (e.g. an MTU 1500 initial subflow and an MTU 9000 MP_JOIN subflow, with @copied landing between their rcvq_space.space values) can indeed happen: (newval - oldval) underflows and produces a bogus slow-start prediction.

Cases like newval == oldval / 2 leaving sk_rcvbuf unchanged before v1 only happened by coincidence because 2^33 wrapped to 0 in u32 (in a local test of this MTU 1500 + 9000 setup on unpatched 7.4.0-rc2, a 30 KB read already sends the join subflow's sk_rcvbuf straight to tcp_rmem[2] and moves its rcvq_space.space backwards). Relying on 32-bit wraparound is not valid behavior; avoiding bogus growth here should be handled explicitly by the algorithm.

If this seems worth addressing, I see two main options:

1) Option 1: Skip slow-start growth when newval <= oldval in tcp_rcvbuf_grow(), as Sashiko suggested.

   This avoids the underflow without affecting tcp_data_queue_ofo() (which passes newval == oldval), but pure TCP callers already guarantee newval >= oldval, and tp->rcvq_space.space = newval at the top of tcp_rcvbuf_grow() would still move the larger-MTU subflow's space estimate backwards.

2) Option 2: Keep tcp_rcvbuf_grow() unchanged and clamp the sample to at least the subflow's existing rcvq_space.space in MPTCP:

net/mptcp/protocol.c +4 -2
@@ -2321,12 +2321,14 @@ static void mptcp_rcv_space_adjust(struct mptcp_sock *msk, int copied)
         mptcp_for_each_subflow(msk, subflow) {
             struct sock *ssk;
             bool slow;
+           int space;
 
             ssk = mptcp_subflow_tcp_sock(subflow);
             slow = lock_sock_fast(ssk);
+           space = tcp_sk(ssk)->rcvq_space.space;
             /* subflows can be added before tcp_init_transfer() */
-           if (tcp_sk(ssk)->rcvq_space.space)
-               tcp_rcvbuf_grow(ssk, copied);
+           if (space)
+               tcp_rcvbuf_grow(ssk, max(copied, space));
             unlock_sock_fast(ssk, slow);
         }
     }
Quoted 8 lines

   Personally, Option 2 seems cleaner to me: it fixes the issue at the only caller that can pass a smaller sample, preserves the subflow's existing space estimate, zeros the slow-start delta when copied < space, and still lets TCP account for its own OOO queue (per commit 6b329393502e) and low_rtt policy.

What do you think? If this looks reasonable to you, I plan to submit a standalone patch.

I didn't check in details, but if this behaviour is specific to MPTCP, and if it can be easily fixed on MPTCP side, then better to do so. So option 2.

(Note about your previous email: maintainers and reviewers are currently overloaded, and I don't think they will be able to go through long emails replies. It might be easier for them if you send patches or new versions containing short and specific questions around them.)

Cheers,
Matt

#12 lore raw

Re: [PATCH net 2/3] net: bound receive window predictions before narrowing

Hi Matt,

Quoted 8 lines · Matthieu Baerts

I didn't check in details, but if this behaviour is specific to MPTCP, and if it can be easily fixed on MPTCP side, then better to do so. So option 2.

(Note about your previous email: maintainers and reviewers are currently overloaded, and I don't think they will be able to go through long emails replies. It might be easier for them if you send patches or new versions containing short and specific questions around them.)

Thanks a lot for the feedback and the helpful advice! I will follow up with concise patches and see what the maintainers think.

Thanks,
Lin

author
#13 lore raw

PATCH 3/3 mptcp: exclude MSG_PEEK from receive buffer autotuning

In reply to lei lei

__mptcp_recvmsg_mskq() passes bytes copied by MSG_PEEK to mptcp_rcv_space_adjust(), although those bytes remain in the receive queue. Repeated peeks count the same data as new application consumption and can inflate the receive-memory budget. Subtracting the unread queue at a measurement boundary cannot undo that repeated count.

The receive path originally skipped this adjustment for MSG_PEEK. Commit 581302298524 ("mptcp: error out earlier on disconnect") moved the adjustment calls without preserving that check.

Only adjust receive space for non-peeking reads. Keep the returned byte count unchanged.

A loopback test leaves a fixed 32 KiB queue unread and peeks 4096 times. Before this change, sk_rcvbuf grows to the configured 8 MiB limit. Afterwards, both the budget and the queue remain unchanged, and a subsequent consuming read returns the original data intact.

Fixes 581302298524 ("mptcp: error out earlier on disconnect")
---
 net/mptcp/protocol.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/mptcp/protocol.c +2 -1
index b3b979403..79db8063a 100644
@@ -2215,7 +2215,8 @@ static int __mptcp_recvmsg_mskq(struct sock *sk, struct msghdr *msg,
 			break;
 	}
 
-	mptcp_rcv_space_adjust(msk, copied);
+	if (!(flags & MSG_PEEK))
+		mptcp_rcv_space_adjust(msk, copied);
 	return copied;
 }
 
Signature · 1 line
2.43.0
#14 lore raw

Re: [PATCH net 3/3] mptcp: exclude MSG_PEEK from receive buffer autotuning

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] mptcp: invalid Signed-off-by trailer using handle instead of real name --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] mptcp: invalid Signed-off-by trailer using handle instead of real name
--

commit bf35feefb3ca938f4410fd363b5311a7fad1ae8f
Author: lei lei <[email protected]>

Quoted 15 lines

mptcp: exclude MSG_PEEK from receive buffer autotuning

__mptcp_recvmsg_mskq() passes bytes copied by MSG_PEEK to mptcp_rcv_space_adjust(), although those bytes remain in the receive queue. Repeated peeks count the same data as new application consumption and can inflate the receive-memory budget. Subtracting the unread queue at a measurement boundary cannot undo that repeated count.

The receive path originally skipped this adjustment for MSG_PEEK. Commit 581302298524 ("mptcp: error out earlier on disconnect") moved the adjustment calls without preserving that check.

Only adjust receive space for non-peeking reads. Keep the returned byte count unchanged.

Quoted 7 lines

A loopback test leaves a fixed 32 KiB queue unread and peeks 4096 times. Before this change, sk_rcvbuf grows to the configured 8 MiB limit. Afterwards, both the budget and the queue remain unchanged, and a subsequent consuming read returns the original data intact.

Fixes 581302298524 ("mptcp: error out earlier on disconnect")

[Severity: High]
The Signed-off-by trailer uses the single-word handle "lllei" instead of a real full human name and does not match the commit Author header ("lei lei"), which violates the Developer's Certificate of Origin requirements in Documentation/process/submitting-patches.rst.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer Reviewed-by on v1 3/3
#15 lore raw

Re: [PATCH net 3/3] mptcp: exclude MSG_PEEK from receive buffer autotuning

In reply to lei lei
Quoted 7 lines · lei lei

__mptcp_recvmsg_mskq() passes bytes copied by MSG_PEEK to mptcp_rcv_space_adjust(), although those bytes remain in the receive queue. Repeated peeks count the same data as new application consumption and can inflate the receive-memory budget. Subtracting the unread queue at a measurement boundary cannot undo that repeated count.

Quoted 6 lines

The receive path originally skipped this adjustment for MSG_PEEK. Commit 581302298524 ("mptcp: error out earlier on disconnect") moved the adjustment calls without preserving that check.

Only adjust receive space for non-peeking reads. Keep the returned byte count unchanged.

This one looks good to me.

mptcp_rcv_space_adjust() accumulates @copied in msk->rcvq_space.copied, and mptcp_inq_hint() is subtracted once per measurement, so N peeks of the same bytes are indeed counted N times.

(TCP also calls tcp_rcv_space_adjust() for MSG_PEEK, but it uses tp->copied_seq, which is not changed by a peek, so it has no such issue.)

The Fixes: tag is correct, commit 581302298524 removed the "if (!(flags & MSG_PEEK))" test.

Quoted 5 lines

A loopback test leaves a fixed 32 KiB queue unread and peeks 4096 times. Before this change, sk_rcvbuf grows to the configured 8 MiB limit. Afterwards, both the budget and the queue remain unchanged, and a subsequent consuming read returns the original data intact.

It would be nice to have this test in tools/testing/selftests/net/mptcp/ (or as a packetdrill test), MPTCP maintainers can tell you what they prefer.

Quoted 3 lines
Fixes 581302298524 ("mptcp: error out earlier on disconnect")

Given my comments on patches 1 and 2, I suggest you send this patch alone, it does not depend on them.

With the Signed-off-by: fixed:

Thanks.

maintainer Reviewed-by
#16 lore raw

Re: [PATCH net 3/3] mptcp: exclude MSG_PEEK from receive buffer autotuning

Hi Eric, Lei,

Thank you for the patch, and for the review!

Quoted 21 lines · Eric Dumazet
Quoted 7 lines · lei lei

__mptcp_recvmsg_mskq() passes bytes copied by MSG_PEEK to mptcp_rcv_space_adjust(), although those bytes remain in the receive queue. Repeated peeks count the same data as new application consumption and can inflate the receive-memory budget. Subtracting the unread queue at a measurement boundary cannot undo that repeated count.

Quoted 6 lines

The receive path originally skipped this adjustment for MSG_PEEK. Commit 581302298524 ("mptcp: error out earlier on disconnect") moved the adjustment calls without preserving that check.

Only adjust receive space for non-peeking reads. Keep the returned byte count unchanged.

This one looks good to me.

mptcp_rcv_space_adjust() accumulates @copied in msk->rcvq_space.copied, and mptcp_inq_hint() is subtracted once per measurement, so N peeks of the same bytes are indeed counted N times.

Quoted 6 lines

(TCP also calls tcp_rcv_space_adjust() for MSG_PEEK, but it uses tp->copied_seq, which is not changed by a peek, so it has no such issue.)

The Fixes: tag is correct, commit 581302298524 removed the "if (!(flags & MSG_PEEK))" test.

It looks good to me too, please send a v2 with the Signed-off-by:

Quoted 8 lines
Quoted 4 lines

A loopback test leaves a fixed 32 KiB queue unread and peeks 4096 times. Before this change, sk_rcvbuf grows to the configured 8 MiB limit. Afterwards, both the budget and the queue remain unchanged, and a subsequent consuming read returns the original data intact.

It would be nice to have this test in tools/testing/selftests/net/mptcp/ (or as a packetdrill test), MPTCP maintainers can tell you what they prefer.

Good idea! We prefer a packetdrill test here, using the MPTCP fork [1]:
such tests are usually shorter and easier to read than a new selftest, and our CI
runs them.

@Lei: could you open a PR on this separate repository with a new test, please? For example in gtests/net/mptcp/syscalls/recv_peek_rcvbuf.pkt, next to recv_peek_waitall.pkt. Feel free to use the following script if it works on your side. You can test it with:

./packetdrill/run_all.py -lv mptcp/syscalls/recv_peek_rcvbuf.pkt

------------------------- 8< -------------------------
// MSG_PEEK must not be counted as consumed data by the rcvbuf autotuning --tolerance_usecs=100000
../common/defaults.sh

// Small initial rcvbuf: without the fix, a few peeks are enough to grow it +0.0 sysctl -q net.ipv4.tcp_rmem="4096 8192 15728640"

  +0.0  socket(..., SOCK_STREAM, IPPROTO_MPTCP) = 3
  +0.0  setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
  +0.0  bind(3, ..., ...) = 0
  +0.0  listen(3, 1) = 0

// With TS, the first data gives an RTT sample (~100ms), needed by autotuning

  +0.0    <  S   0:0(0)                win 65535  <mss 1460, sackOK, TS val 100 ecr 0,   nop, wscale 8, mpcapable v1 flags[flag_h] nokey>
  +0.0    >  S.  0:0(0)        ack 1              <mss 1460, sackOK, TS val 700 ecr 100, nop, wscale 8, mpcapable v1 flags[flag_h] key[skey]>
  +0.1    <   .  1:1(0)        ack 1     win 256  <nop, nop,         TS val 200 ecr 700,                mpcapable v1 flags[flag_h] key[ckey=2, skey]>

+0.0 accept(3, ..., ...) = 4

  +0.0    <  P.  1:1001(1000)  ack 1     win 256  <nop, nop,         TS val 300 ecr 700,                mpcapable v1 flags[flag_h] key[skey, ckey] mpcdatalen 1000, nop, nop>
  +0.0    >   .  1:1(0)        ack 1001           <nop, nop,         TS val 700 ecr 300,                dss dack8=1001 dll=0 nocs>

// Peek the same data multiple times within one RTT...

  +0.0  recv(4, ..., 1000, MSG_PEEK) = 1000
  +0.0  recv(4, ..., 1000, MSG_PEEK) = 1000
  +0.0  recv(4, ..., 1000, MSG_PEEK) = 1000
  +0.0  recv(4, ..., 1000, MSG_PEEK) = 1000
  +0.0  recv(4, ..., 1000, MSG_PEEK) = 1000

// ... then once more after one RTT: nothing consumed, the rcvbuf is unchanged +0.2 recv(4, ..., 1000, MSG_PEEK) = 1000
+0.0 getsockopt(4, SOL_SOCKET, SO_RCVBUF, [8192], [4]) = 0

------------------------- 8< -------------------------

[1] https://github.com/multipath-tcp/packetdrill

Cheers,
Matt

Signature · 1 line
Sponsored by the NGI0 Core fund.
#17 lore raw

Re: [PATCH net 3/3] mptcp: exclude MSG_PEEK from receive buffer autotuning

Hi Matthieu and Eric,

Quoted 1 line

It looks good to me too, please send a v2 with the Signed-off-by:

Thanks for the reviews. I sent the fix as a standalone v2 with my corrected sign-off and both of your Reviewed-by tags:

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

Quoted 2 lines

@Lei: could you open a PR on this separate repository with a new test, please?

Thanks for the script, Matthieu. I used it as the basis for the test and opened the PR here:

https://github.com/multipath-tcp/packetdrill/pull/214

I ran the test three times each on IPv4, IPv6 and IPv4-mapped IPv6. Before the fix, SO_RCVBUF grew from 8192 to 50237 bytes in all nine runs. With the fix, it stayed at 8192 bytes in all nine.

Thanks,
Lin

#18 lore raw

Re: [PATCH net 3/3] mptcp: exclude MSG_PEEK from receive buffer autotuning

In reply to lei lei
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…

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 name in the Signed-off-by tag does not match the author name.

--

mptcp: exclude MSG_PEEK from receive buffer autotuning

This patch stops __mptcp_recvmsg_mskq() from passing bytes copied by MSG_PEEK to mptcp_rcv_space_adjust(). Repeated peeks no longer count the same queued data as new application consumption, which could inflate sk_rcvbuf. This restores the MSG_PEEK check that was dropped by commit 581302298524 ("mptcp: error out earlier on disconnect").

Quoted 1 line

Author: lei lei <[email protected]>

[ ... ]

Quoted 1 line

[Severity: Low]
This isn't a bug, but the name in the Signed-off-by tag ("lllei") does not match the author name ("lei lei"). The email address is the same in both.

checkpatch.pl reports this as a From:/Signed-off-by: name mismatch.

Could the sign-off be changed to match the author identity, using the real name, in a v2?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117052200.1507847-1-llleigoing%40outlook.com
#19 lore raw
In reply to lei lei

Hi lei,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal (except selftest_mptcp_join): Unstable: 1 failed test(s): packetdrill_dss ⚠️
- KVM Validation: normal (only selftest_mptcp_join): Success! ✅
- KVM Validation: debug (except selftest_mptcp_join): Success! ✅
- KVM Validation: debug (only selftest_mptcp_join): Success! ✅
- KVM Validation: btf-normal (only bpftest_all): Success! ✅
- KVM Validation: btf-debug (only bpftest_all): Success! ✅
- Perf: Unstable: 1 failed test(s): validation:sched-mem-pressure ⚠️
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/44024826211

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/64bd1916b1c2
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=1213436

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)