Skip to contentSkip to conversation

tcp/dccp: bypass empty buckets in inet_twsk_purge()

PATCH Applied Eric Dumazet 4 participants 5 messages Mar 27, 2024 – Mar 29, 2024

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

PATCH 1/1 tcp/dccp: bypass empty buckets in inet_twsk_purge()

TCP ehash table is often sparsely populated.

inet_twsk_purge() spends too much time calling cond_resched().

This patch can reduce time spent in inet_twsk_purge() by 20x.

---
 net/ipv4/inet_timewait_sock.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
net/ipv4/inet_timewait_sock.c +7 -2
index e8de45d34d56a507a4bdcceaccbd5707692b6c0a..b0cc07d9a568c5dc52bd29729862bcb03e5d595d 100644
@@ -266,12 +266,17 @@ EXPORT_SYMBOL_GPL(__inet_twsk_schedule);
 /* Remove all non full sockets (TIME_WAIT and NEW_SYN_RECV) for dead netns */
 void inet_twsk_purge(struct inet_hashinfo *hashinfo, int family)
 {
+	struct inet_ehash_bucket *head = &hashinfo->ehash[0];
+	unsigned int ehash_mask = hashinfo->ehash_mask;
 	struct hlist_nulls_node *node;
 	unsigned int slot;
 	struct sock *sk;
 
-	for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
-		struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
+	for (slot = 0; slot <= ehash_mask; slot++, head++) {
+
+		if (hlist_nulls_empty(&head->chain))
+			continue;
+
 restart_rcu:
 		cond_resched();
 		rcu_read_lock();
Signature · 1 line
2.44.0.396.g6e790dbe36-goog
Reviewed-by on v1 1/1
#1 lore raw

From: Eric Dumazet <[email protected]>
Date: Wed, 27 Mar 2024 19:12:06 +0000

Quoted 7 lines

TCP ehash table is often sparsely populated.

inet_twsk_purge() spends too much time calling cond_resched().

This patch can reduce time spent in inet_twsk_purge() by 20x.

Sounds good!

1 nit below.

Quoted 12 lines
---
 net/ipv4/inet_timewait_sock.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
net/ipv4/inet_timewait_sock.c +0 -0
index e8de45d34d56a507a4bdcceaccbd5707692b6c0a..b0cc07d9a568c5dc52bd29729862bcb03e5d595d 100644
@@ -266,12 +266,17 @@ EXPORT_SYMBOL_GPL(__inet_twsk_schedule);
 /* Remove all non full sockets (TIME_WAIT and NEW_SYN_RECV) for dead netns */
 void inet_twsk_purge(struct inet_hashinfo *hashinfo, int family)
 {

net/ipv4/inet_timewait_sock.c EXPORT_SYMBOL_GPL(__inet_twsk_schedule); · line 276

Quoted 10 lines
net/ipv4/inet_timewait_sock.c +4 -2
+	struct inet_ehash_bucket *head = &hashinfo->ehash[0];
+	unsigned int ehash_mask = hashinfo->ehash_mask;
 	struct hlist_nulls_node *node;
 	unsigned int slot;
 	struct sock *sk;
 
-	for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
-		struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
+	for (slot = 0; slot <= ehash_mask; slot++, head++) {
+

unnecessary blank link here.

Thanks!

Quoted 9 lines
net/ipv4/inet_timewait_sock.c +3 -0
+		if (hlist_nulls_empty(&head->chain))
+			continue;
+
 restart_rcu:
 		cond_resched();
 		rcu_read_lock();
Signature · 1 line
2.44.0.396.g6e790dbe36-goog
maintainer
#2 lore raw
Quoted 6 lines · On Wed, 27 Mar 2024 12:29:34 -0700 Kuniyuki Iwashima wrote:

net/ipv4/inet_timewait_sock.c EXPORT_SYMBOL_GPL(__inet_twsk_schedule); · line 276

Quoted 4 lines
net/ipv4/inet_timewait_sock.c +2 -2
-	for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
-		struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
+	for (slot = 0; slot <= ehash_mask; slot++, head++) {
+  

unnecessary blank link here.

I've seen some other core kernel contributors add empty lines after for or if statements, so I think this is intentional. Not sure why the empty line helps, either, TBH, but we have been letting it slide so far.

#3 lore raw
Quoted 7 lines · Jakub Kicinski
Quoted 5 lines · On Wed, 27 Mar 2024 12:29:34 -0700 Kuniyuki Iwashima wrote:
Quoted 4 lines
net/ipv4/inet_timewait_sock.c +2 -2
-   for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
-           struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
+   for (slot = 0; slot <= ehash_mask; slot++, head++) {
+
Quoted 6 lines
Quoted 1 line

unnecessary blank link here.

I've seen some other core kernel contributors add empty lines after for or if statements, so I think this is intentional. Not sure why the empty line helps, either, TBH, but we have been letting it slide so far.

I will delete this extra line in the following patch, thanks !

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

Hello:

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

Quoted 9 lines · On Wed, 27 Mar 2024 19:12:06 +0000 you wrote:

TCP ehash table is often sparsely populated.

inet_twsk_purge() spends too much time calling cond_resched().

This patch can reduce time spent in inet_twsk_purge() by 20x.

[...]

Here is the summary with links:
- [net-next] tcp/dccp: bypass empty buckets in inet_twsk_purge()

    https://git.kernel.org/netdev/net-next/c/50e2907ef8bb

You are awesome, thank you!

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