Skip to contentSkip to conversation

tcp: better memory control for not-yet-accepted sockets

PATCH Applied Eric Dumazet 2 participants 4 messages Jul 7, 2025 – Jul 10, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover tcp: better memory control for not-yet-accepted sockets

Address a possible OOM condition caused by a recent change.

Add a new packetdrill test checking the expected behavior.

Eric Dumazet (2):
tcp: refine sk_rcvbuf increase for ooo packets selftests/net: packetdrill: add tcp_ooo-before-and-after-accept.pkt

 net/ipv4/tcp_input.c                          |  4 +-
 .../tcp_ooo-before-and-after-accept.pkt       | 53 +++++++++++++++++++
 2 files changed, 56 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/net/packetdrill/tcp_ooo-before-and-after-accept.pkt
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
#1 lore raw

PATCH 1/2 tcp: refine sk_rcvbuf increase for ooo packets

When a passive flow has not been accepted yet, it is not wise to increase sk_rcvbuf when receiving ooo packets.

A very busy server might tune down tcp_rmem[1] to better control how much memory can be used by sockets waiting in its listeners accept queues.

Fixes 63ad7dfedfae ("tcp: adjust rcvbuf in presence of reorders")
---
 net/ipv4/tcp_input.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/ipv4/tcp_input.c +3 -1
index 12c2e6fc85c62304565e4f2f8d22a916cf73db0c..68bc79eb90197b1bf3e8f5ddfc8762cf331ca1c7 100644
@@ -5181,7 +5181,9 @@ static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
 		skb_condense(skb);
 		skb_set_owner_r(skb, sk);
 	}
-	tcp_rcvbuf_grow(sk);
+	/* do not grow rcvbuf for not-yet-accepted or orphaned sockets. */
+	if (sk->sk_socket)
+		tcp_rcvbuf_grow(sk);
 }
 
 static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb,
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
#2 lore raw

PATCH 2/2 selftests/net: packetdrill: add tcp_ooo-before-and-after-accept.pkt

In reply to Eric Dumazet

Test how new passive flows react to ooo incoming packets.

Their sk_rcvbuf can increase only after accept().

---
 .../tcp_ooo-before-and-after-accept.pkt       | 53 +++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100644 tools/testing/selftests/net/packetdrill/tcp_ooo-before-and-after-accept.pkt
tools/testing/selftests/net/packetdrill/tcp_ooo-before-and-after-accept.pkt new file +53 -0
new file mode 100644
index 0000000000000000000000000000000000000000..09aabc775e80a2457543e6fc69b16d7981dc777e
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-2.0
+
+--mss=1000
+
+`./defaults.sh
+sysctl -q net.ipv4.tcp_rmem="4096 131072 $((32*1024*1024))"`
+
+// Test that a not-yet-accepted socket does not change
+// its initial sk_rcvbuf (tcp_rmem[1]) when receiving ooo packets.
+
+   +0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
+   +0 setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
+   +0 bind(3, ..., ...) = 0
+   +0 listen(3, 1) = 0
+
+   +0 < S 0:0(0) win 65535 <mss 1000,nop,nop,sackOK,nop,wscale 7>
+   +0 > S. 0:0(0) ack 1 <mss 1460,nop,nop,sackOK,nop,wscale 10>
+  +.1 < . 1:1(0) ack 1 win 257
+  +0  < . 2001:41001(39000) ack 1 win 257
+  +0  > . 1:1(0) ack 1 <nop,nop,sack 2001:41001>
+  +0  < . 41001:101001(60000) ack 1 win 257
+  +0  > . 1:1(0) ack 1 <nop,nop,sack 2001:101001>
+  +0  < . 1:1001(1000) ack 1 win 257
+  +0  > . 1:1(0) ack 1001 <nop,nop,sack 2001:101001>
+  +0  < . 1001:2001(1000) ack 1 win 257
+  +0  > . 1:1(0) ack 101001
+
+  +0 accept(3, ..., ...) = 4
+
+  +0 %{ assert SK_MEMINFO_RCVBUF == 131072, SK_MEMINFO_RCVBUF }%
+
+  +0 close(4) = 0
+  +0 close(3) = 0
+
+// Test that ooo packets for accepted sockets do increase sk_rcvbuf
+   +0 socket(..., SOCK_STREAM, IPPROTO_TCP) = 3
+   +0 setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
+   +0 bind(3, ..., ...) = 0
+   +0 listen(3, 1) = 0
+
+   +0 < S 0:0(0) win 65535 <mss 1000,nop,nop,sackOK,nop,wscale 7>
+   +0 > S. 0:0(0) ack 1 <mss 1460,nop,nop,sackOK,nop,wscale 10>
+  +.1 < . 1:1(0) ack 1 win 257
+
+   +0 accept(3, ..., ...) = 4
+
+  +0  < . 2001:41001(39000) ack 1 win 257
+  +0  > . 1:1(0) ack 1 <nop,nop,sack 2001:41001>
+  +0  < . 41001:101001(60000) ack 1 win 257
+  +0  > . 1:1(0) ack 1 <nop,nop,sack 2001:101001>
+
+  +0 %{ assert SK_MEMINFO_RCVBUF > 131072, SK_MEMINFO_RCVBUF }%
+
Signature · 1 line
2.50.0.727.gbf7dc18ff4-goog
#3 lore raw
In reply to Eric Dumazet
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Mon, 7 Jul 2025 21:38:58 +0000 you wrote:

Address a possible OOM condition caused by a recent change.

Add a new packetdrill test checking the expected behavior.

Eric Dumazet (2):
tcp: refine sk_rcvbuf increase for ooo packets selftests/net: packetdrill: add tcp_ooo-before-and-after-accept.pkt

[...]

Here is the summary with links:
- [net,1/2] tcp: refine sk_rcvbuf increase for ooo packets

    https://git.kernel.org/netdev/net/c/1a03edeb84e6
  - [net,2/2] selftests/net: packetdrill: add tcp_ooo-before-and-after-accept.pkt
    https://git.kernel.org/netdev/net/c/b939c074efc1

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html