Skip to contentSkip to conversation

tcp: add tcp_release_cb_cond() helper

PATCH Applied Eric Dumazet 4 participants 5 messages Mar 10 – Mar 12

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

PATCH 1/1 tcp: add tcp_release_cb_cond() helper

Majority of tcp_release_cb() calls do nothing at all.

Provide tcp_release_cb_cond() helper so that release_sock() can avoid these calls.

Also hint the compiler thar __release_sock() and wake_up() are rarely called.

$ scripts/bloat-o-meter -t vmlinux.old vmlinux.new
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-77 (-77)
Function                                     old     new   delta
release_sock                                 258     181     -77
Total: Before=25235790, After=25235713, chg -0.00%
---
 include/linux/tcp.h   |  7 +++++++
 include/net/tcp.h     | 14 ++++++++++++++
 net/core/sock.c       | 14 ++++++++------
 net/ipv4/tcp_output.c |  5 -----
 4 files changed, 29 insertions(+), 11 deletions(-)
include/linux/tcp.h +7 -0
index c44cf9ae8d16f5cc7808a6ca78c1dad03354dccf..bcebc4f07532f0e099d9b0751db7d3eedff2ee5d 100644
@@ -548,6 +548,13 @@ enum tsq_flags {
 	TCPF_ACK_DEFERRED		= BIT(TCP_ACK_DEFERRED),
 };
 
+/* Flags of interest for tcp_release_cb() */
+#define TCP_DEFERRED_ALL (TCPF_TSQ_DEFERRED |		\
+			  TCPF_WRITE_TIMER_DEFERRED |	\
+			  TCPF_DELACK_TIMER_DEFERRED |	\
+			  TCPF_MTU_REDUCED_DEFERRED |	\
+			  TCPF_ACK_DEFERRED)
+
 #define tcp_sk(ptr) container_of_const(ptr, struct tcp_sock, inet_conn.icsk_inet.sk)
 
 /* Variant of tcp_sk() upgrading a const sock to a read/write tcp socket.
include/net/tcp.h +14 -0
index 9f0aee9e5d76d3ef5be75586bd289583e48c8baf..48dffcca0a71b70d0c0fd49d89a66c4f6ae72a58 100644
@@ -375,7 +375,21 @@ int tcp_send_mss(struct sock *sk, int *size_goal, int flags);
 int tcp_wmem_schedule(struct sock *sk, int copy);
 void tcp_push(struct sock *sk, int flags, int mss_now, int nonagle,
 	      int size_goal);
+
 void tcp_release_cb(struct sock *sk);
+
+static inline bool tcp_release_cb_cond(struct sock *sk)
+{
+#ifdef CONFIG_INET
+	if (likely(sk->sk_prot->release_cb == tcp_release_cb)) {
+		if (unlikely(smp_load_acquire(&sk->sk_tsq_flags) & TCP_DEFERRED_ALL))
+			tcp_release_cb(sk);
+		return true;
+	}
+#endif
+	return false;
+}
+
 void tcp_wfree(struct sk_buff *skb);
 void tcp_write_timer_handler(struct sock *sk);
 void tcp_delack_timer_handler(struct sock *sk);
net/core/sock.c +8 -6
index f4e2ff23d60eb5103a972d1eee7cc8372de86c5d..fdaf66e6dc18c691f3d8df26ec6769b193d43668 100644
@@ -3807,16 +3807,18 @@ EXPORT_SYMBOL(lock_sock_nested);
 void release_sock(struct sock *sk)
 {
 	spin_lock_bh(&sk->sk_lock.slock);
-	if (sk->sk_backlog.tail)
-		__release_sock(sk);
 
-	if (sk->sk_prot->release_cb)
-		INDIRECT_CALL_INET_1(sk->sk_prot->release_cb,
-				     tcp_release_cb, sk);
+	if (unlikely(sk->sk_backlog.tail))
+		__release_sock(sk);
 
+	if (sk->sk_prot->release_cb) {
+		if (!tcp_release_cb_cond(sk))
+			sk->sk_prot->release_cb(sk);
+	}
 	sock_release_ownership(sk);
-	if (waitqueue_active(&sk->sk_lock.wq))
+	if (unlikely(waitqueue_active(&sk->sk_lock.wq)))
 		wake_up(&sk->sk_lock.wq);
+
 	spin_unlock_bh(&sk->sk_lock.slock);
 }
 EXPORT_SYMBOL(release_sock);
net/ipv4/tcp_output.c +0 -5
index a53802f28dd1788eda6c10654e91e918a42b1380..34a25ef610060988c0c0350ca4b97a112f04ddcb 100644
@@ -1320,11 +1320,6 @@ static void tcp_tsq_workfn(struct work_struct *work)
 	}
 }
 
-#define TCP_DEFERRED_ALL (TCPF_TSQ_DEFERRED |		\
-			  TCPF_WRITE_TIMER_DEFERRED |	\
-			  TCPF_DELACK_TIMER_DEFERRED |	\
-			  TCPF_MTU_REDUCED_DEFERRED |	\
-			  TCPF_ACK_DEFERRED)
 /**
  * tcp_release_cb - tcp release_sock() callback
  * @sk: socket
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Eric Dumazet

Majority of tcp_release_cb() calls do nothing at all.

Provide tcp_release_cb_cond() helper so that release_sock() can avoid these calls.

Also hint the compiler thar __release_sock() and wake_up() are rarely called.

Quoted 7 lines
$ scripts/bloat-o-meter -t vmlinux.old vmlinux.new
add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-77 (-77)
Function                                     old     new   delta
release_sock                                 258     181     -77
Total: Before=25235790, After=25235713, chg -0.00%
maintainer
#2 lore raw
In reply to Eric Dumazet
Quoted 6 lines · Eric Dumazet

Majority of tcp_release_cb() calls do nothing at all.

Provide tcp_release_cb_cond() helper so that release_sock() can avoid these calls.

Also hint the compiler thar __release_sock() and wake_up()

I took the liberty of fixing the above typo while applying the patch.

Quoted 1 line

are rarely called.

For my education: out of sheer ignorance and lack of relevant H/W to actually measure the thing, I thought that on very high speed link with big TCP enabled, user-space process and BH processing running on different CPUs backlog usage would be quite probable?!?

/P

#3 lore raw
Quoted 9 lines · Paolo Abeni
Quoted 6 lines · Eric Dumazet

Majority of tcp_release_cb() calls do nothing at all.

Provide tcp_release_cb_cond() helper so that release_sock() can avoid these calls.

Also hint the compiler thar __release_sock() and wake_up()

Quoted 8 lines

I took the liberty of fixing the above typo while applying the patch.

Quoted 1 line

are rarely called.

For my education: out of sheer ignorance and lack of relevant H/W to actually measure the thing, I thought that on very high speed link with big TCP enabled, user-space process and BH processing running on different CPUs backlog usage would be quite probable?!?

Yes, but in this case each system call pumps in or out a lot of data.

Majority of data center traffic is RPC, overall probability of calling
__release_sock()
is less than 5 %.

I mostly see that by looking at the difference between non FDO compilations and FDO kernels.

Thanks !

#4 lore raw
In reply to Eric Dumazet
This patch was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Paolo Abeni <[email protected]>:

Quoted 9 lines · On Tue, 10 Mar 2026 12:44:51 +0000 you wrote:

Majority of tcp_release_cb() calls do nothing at all.

Provide tcp_release_cb_cond() helper so that release_sock() can avoid these calls.

Also hint the compiler thar __release_sock() and wake_up() are rarely called.

[...]

Here is the summary with links:
- [net-next] tcp: add tcp_release_cb_cond() helper

    https://git.kernel.org/netdev/net-next/c/6f459eda8b60

You are awesome, thank you!

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