public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH net-next] tcp: add tcp_release_cb_cond() helper
@ 2026-03-10 12:44 Eric Dumazet
  2026-03-11  4:20 ` Kuniyuki Iwashima
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Eric Dumazet @ 2026-03-10 12:44 UTC (permalink / raw)
  To: David S . Miller, Jakub Kicinski, Paolo Abeni
  Cc: Simon Horman, Neal Cardwell, Kuniyuki Iwashima, netdev,
	eric.dumazet, 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.

$ 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%

Signed-off-by: Eric Dumazet <edumazet@google.com>
---
 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(-)

diff --git a/include/linux/tcp.h b/include/linux/tcp.h
index c44cf9ae8d16f5cc7808a6ca78c1dad03354dccf..bcebc4f07532f0e099d9b0751db7d3eedff2ee5d 100644
--- a/include/linux/tcp.h
+++ b/include/linux/tcp.h
@@ -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.
diff --git a/include/net/tcp.h b/include/net/tcp.h
index 9f0aee9e5d76d3ef5be75586bd289583e48c8baf..48dffcca0a71b70d0c0fd49d89a66c4f6ae72a58 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -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);
diff --git a/net/core/sock.c b/net/core/sock.c
index f4e2ff23d60eb5103a972d1eee7cc8372de86c5d..fdaf66e6dc18c691f3d8df26ec6769b193d43668 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -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);
diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index a53802f28dd1788eda6c10654e91e918a42b1380..34a25ef610060988c0c0350ca4b97a112f04ddcb 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -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
-- 
2.53.0.473.g4a7958ca14-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH net-next] tcp: add tcp_release_cb_cond() helper
  2026-03-10 12:44 [PATCH net-next] tcp: add tcp_release_cb_cond() helper Eric Dumazet
@ 2026-03-11  4:20 ` Kuniyuki Iwashima
  2026-03-12 12:26 ` Paolo Abeni
  2026-03-12 12:30 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 5+ messages in thread
From: Kuniyuki Iwashima @ 2026-03-11  4:20 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: David S . Miller, Jakub Kicinski, Paolo Abeni, Simon Horman,
	Neal Cardwell, netdev, eric.dumazet

On Tue, Mar 10, 2026 at 5:44 AM Eric Dumazet <edumazet@google.com> 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.
>
> $ 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%
>
> Signed-off-by: Eric Dumazet <edumazet@google.com>

Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net-next] tcp: add tcp_release_cb_cond() helper
  2026-03-10 12:44 [PATCH net-next] tcp: add tcp_release_cb_cond() helper Eric Dumazet
  2026-03-11  4:20 ` Kuniyuki Iwashima
@ 2026-03-12 12:26 ` Paolo Abeni
  2026-03-12 12:33   ` Eric Dumazet
  2026-03-12 12:30 ` patchwork-bot+netdevbpf
  2 siblings, 1 reply; 5+ messages in thread
From: Paolo Abeni @ 2026-03-12 12:26 UTC (permalink / raw)
  To: Eric Dumazet, David S . Miller, Jakub Kicinski
  Cc: Simon Horman, Neal Cardwell, Kuniyuki Iwashima, netdev,
	eric.dumazet

On 3/10/26 1:44 PM, Eric Dumazet 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()

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

> 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net-next] tcp: add tcp_release_cb_cond() helper
  2026-03-10 12:44 [PATCH net-next] tcp: add tcp_release_cb_cond() helper Eric Dumazet
  2026-03-11  4:20 ` Kuniyuki Iwashima
  2026-03-12 12:26 ` Paolo Abeni
@ 2026-03-12 12:30 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-03-12 12:30 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: davem, kuba, pabeni, horms, ncardwell, kuniyu, netdev,
	eric.dumazet

Hello:

This patch was applied to netdev/net-next.git (main)
by Paolo Abeni <pabeni@redhat.com>:

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!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net-next] tcp: add tcp_release_cb_cond() helper
  2026-03-12 12:26 ` Paolo Abeni
@ 2026-03-12 12:33   ` Eric Dumazet
  0 siblings, 0 replies; 5+ messages in thread
From: Eric Dumazet @ 2026-03-12 12:33 UTC (permalink / raw)
  To: Paolo Abeni
  Cc: David S . Miller, Jakub Kicinski, Simon Horman, Neal Cardwell,
	Kuniyuki Iwashima, netdev, eric.dumazet

On Thu, Mar 12, 2026 at 1:27 PM Paolo Abeni <pabeni@redhat.com> wrote:
>
> On 3/10/26 1:44 PM, Eric Dumazet 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()
>
> I took the liberty of fixing the above typo while applying the patch.
>
> > 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 !

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-03-12 12:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-10 12:44 [PATCH net-next] tcp: add tcp_release_cb_cond() helper Eric Dumazet
2026-03-11  4:20 ` Kuniyuki Iwashima
2026-03-12 12:26 ` Paolo Abeni
2026-03-12 12:33   ` Eric Dumazet
2026-03-12 12:30 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox