tcp: Clean up inet_hash() and inet_unhash().
Subsystems (3)
Kuniyuki Iwashima
reviewercover tcp: Clean up inet_hash() and inet_unhash().
While reviewing the ehash fix series from Xuanqiang Luo [0], I noticed that inet_twsk_hashdance_schedule() checks the retval of __sk_nulls_del_node_init_rcu(), which looks confusing.
The test exists from the pre-git era:
$ git blame -L:tcp_tw_hashdance net/ipv4/tcp_minisocks.c e48c414ee61f4~
Patch 3 is to clarify that the retval check is unnecessary in inet_twsk_hashdance_schedule(), but I'll delegate its removal to the Xuanqiang's series.
Patch 1 & 2 are minor cleanups.
[0]: https://lore.kernel.org/netdev/[email protected]/
Kuniyuki Iwashima (3):
tcp: Remove osk from __inet_hash() arg.
tcp: Remove inet6_hash().
tcp: Remove redundant sk_unhashed() in inet_unhash().
include/net/inet6_hashtables.h | 2 -- include/net/inet_hashtables.h | 1 - net/ipv4/inet_hashtables.c | 28 ++++++---------------------- net/ipv6/inet6_hashtables.c | 11 ----------- net/ipv6/tcp_ipv6.c | 2 +- 5 files changed, 7 insertions(+), 37 deletions(-)
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Kuniyuki Iwashima
reviewerPATCH 1/3 tcp: Remove osk from __inet_hash() arg.
__inet_hash() is called from inet_hash() and inet6_hash with osk NULL.
Let's remove the 2nd arg from __inet_hash().
--- include/net/inet_hashtables.h | 2 +- net/ipv4/inet_hashtables.c | 6 +++--- net/ipv6/inet6_hashtables.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-)
include/net/inet_hashtables.h +1 -1
@@ -289,7 +289,7 @@ int inet_hashinfo2_init_mod(struct inet_hashinfo *h);bool inet_ehash_insert (struct sock *sk,struct sock *osk,bool *found_dup_sk);bool inet_ehash_nolisten (struct sock *sk,struct sock *osk,bool *found_dup_sk); -int __inet_hash (struct sock *sk,struct sock *osk); +int __inet_hash (struct sock *sk);int inet_hash (struct sock *sk);void inet_unhash (struct sock *sk);
net/ipv4/inet_hashtables.c +3 -3
@@ -739,7 +739,7 @@ static int inet_reuseport_add_sock(struct sock *sk,return reuseport_alloc (sk,inet_rcv_saddr_any (sk)); } -int __inet_hash (struct sock *sk,struct sock *osk) +int __inet_hash (struct sock *sk) {struct inet_hashinfo *hashinfo =tcp_get_hashinfo (sk);struct inet_listen_hashbucket *ilb2;
@@ -747,7 +747,7 @@ int __inet_hash(struct sock *sk, struct sock *osk)if (sk->sk_state !=TCP_LISTEN ) {local_bh_disable (); -inet_ehash_nolisten (sk, osk,NULL ); +inet_ehash_nolisten (sk,NULL ,NULL );local_bh_enable ();return 0 ; }
@@ -779,7 +779,7 @@ int inet_hash(struct sock *sk)int err =0 ;if (sk->sk_state !=TCP_CLOSE ) - err =__inet_hash (sk,NULL ); + err =__inet_hash (sk);return err; }
net/ipv6/inet6_hashtables.c +1 -1
@@ -374,7 +374,7 @@ int inet6_hash(struct sock *sk)int err =0 ;if (sk->sk_state !=TCP_CLOSE ) - err =__inet_hash (sk,NULL ); + err =__inet_hash (sk);return err; }
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Re: [PATCH v1 net-next 1/3] tcp: Remove osk from __inet_hash() arg.
Quoted 7 lines · Kuniyuki Iwashima
__inet_hash()is called frominet_hash()andinet6_hashwith osk NULL.Let's remove the 2nd arg from
__inet_hash().Signed-off-by Kuniyuki Iwashima <[email protected]>---
Kuniyuki Iwashima
reviewerPATCH 2/3 tcp: Remove inet6_hash().
In reply to Kuniyuki Iwashimainet_hash() and inet6_hash() are exactly the same.
Also, we do not need to export inet6_hash().
Let's consolidate the two into __inet_hash() and rename it to inet_hash().
--- include/net/inet6_hashtables.h | 2 -- include/net/inet_hashtables.h | 1 - net/ipv4/inet_hashtables.c | 17 +++++------------ net/ipv6/inet6_hashtables.c | 11 ----------- net/ipv6/tcp_ipv6.c | 2 +- 5 files changed, 6 insertions(+), 27 deletions(-)
include/net/inet6_hashtables.h +0 -2
@@ -167,8 +167,6 @@ struct sock *inet6_lookup(const struct net *net, struct sk_buff *skb, int doff, const struct in6_addr *daddr, const __be16 dport, const int dif); -int inet6_hash (struct sock *sk); - static inline bool inet6_match(const struct net *net, const struct sock *sk, const struct in6_addr *saddr, const struct in6_addr *daddr,
include/net/inet_hashtables.h +0 -1
@@ -289,7 +289,6 @@ int inet_hashinfo2_init_mod(struct inet_hashinfo *h);bool inet_ehash_insert (struct sock *sk,struct sock *osk,bool *found_dup_sk);bool inet_ehash_nolisten (struct sock *sk,struct sock *osk,bool *found_dup_sk); -int __inet_hash (struct sock *sk);int inet_hash (struct sock *sk);void inet_unhash (struct sock *sk);
net/ipv4/inet_hashtables.c +5 -12
@@ -739,12 +739,15 @@ static int inet_reuseport_add_sock(struct sock *sk,return reuseport_alloc (sk,inet_rcv_saddr_any (sk)); } -int __inet_hash (struct sock *sk) +int inet_hash (struct sock *sk) {struct inet_hashinfo *hashinfo =tcp_get_hashinfo (sk);struct inet_listen_hashbucket *ilb2;int err =0 ; +if (sk->sk_state ==TCP_CLOSE ) +return 0 ; +if (sk->sk_state !=TCP_LISTEN ) {local_bh_disable ();inet_ehash_nolisten (sk,NULL ,NULL );
@@ -772,17 +775,7 @@ int __inet_hash(struct sock *sk)return err; } -EXPORT_IPV6_MOD (__inet_hash); - -int inet_hash (struct sock *sk) -{ -int err =0 ; - -if (sk->sk_state !=TCP_CLOSE ) - err =__inet_hash (sk); - -return err; -} +EXPORT_IPV6_MOD (inet_hash);void inet_unhash (struct sock *sk) {
net/ipv6/inet6_hashtables.c +0 -11
@@ -368,14 +368,3 @@ int inet6_hash_connect(struct inet_timewait_death_row *death_row, __inet6_check_established); }EXPORT_SYMBOL_GPL (inet6_hash_connect); - -int inet6_hash (struct sock *sk) -{ -int err =0 ; - -if (sk->sk_state !=TCP_CLOSE ) - err =__inet_hash (sk); - -return err; -} -EXPORT_SYMBOL_GPL (inet6_hash);
net/ipv6/tcp_ipv6.c +1 -1
@@ -2355,7 +2355,7 @@ struct proto tcpv6_prot = { .splice_eof = tcp_splice_eof, .backlog_rcv = tcp_v6_do_rcv, .release_cb = tcp_release_cb, - .hash = inet6_hash, + .hash = inet_hash, .unhash = inet_unhash, .get_port = inet_csk_get_port, .put_port = inet_put_port,
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Re: [PATCH v1 net-next 2/3] tcp: Remove inet6_hash().
Quoted 8 lines · Kuniyuki Iwashima
inet_hash()andinet6_hash()are exactly the same.Also, we do not need to export
inet6_hash().Let's consolidate the two into
__inet_hash()and rename it toinet_hash().Signed-off-by Kuniyuki Iwashima <[email protected]>
Kuniyuki Iwashima
reviewerPATCH 3/3 tcp: Remove redundant sk_unhashed() in inet_unhash().
In reply to Kuniyuki Iwashimainet_unhash() checks sk_unhashed() twice at the entry and after locking ehash/lhash bucket.
The former was somehow added redundantly by commit 4f9bf2a2f5aa ("tcp:
Don't acquire inet_listen_hashbucket::lock with disabled BH.").
inet_unhash() is called for the full socket from 4 places, and it is always under lock_sock() or the socket is not yet published to other threads:
1. __sk_prot_rehash()
-> called from inet_sk_reselect_saddr(), which has
lockdep_sock_is_held() 2. sk_common_release()
-> called when inet_create() or inet6_create() fail, then the
socket is not yet published
3. tcp_set_state()
-> calls tcp_call_bpf_2arg(), and tcp_call_bpf() has
sock_owned_by_me() 4. inet_ctl_sock_create()
-> creates a kernel socket and unhashes it immediately, but TCP
socket is not hashed in sock_create_kern() (only SOCK_RAW is)
So we do not need to check sk_unhashed() twice before/after ehash/lhash lock in inet_unhash().
Let's remove the 2nd one.
--- net/ipv4/inet_hashtables.c | 9 --------- 1 file changed, 9 deletions(-)
net/ipv4/inet_hashtables.c +0 -9
@@ -793,11 +793,6 @@ void inet_unhash(struct sock *sk)* avoid circular locking dependency on PREEMPT_RT. */ spin_lock (&ilb2->lock); -if (sk_unhashed (sk)) { -spin_unlock (&ilb2->lock); -return ; - } -if (rcu_access_pointer (sk->sk_reuseport_cb))reuseport_stop_listen_sock (sk);
@@ -808,10 +803,6 @@ void inet_unhash(struct sock *sk)spinlock_t *lock =inet_ehash_lockp (hashinfo, sk->sk_hash);spin_lock_bh (lock); -if (sk_unhashed (sk)) { -spin_unlock_bh (lock); -return ; - }__sk_nulls_del_node_init_rcu (sk);sock_prot_inuse_add (sock_net (sk), sk->sk_prot,-1 );spin_unlock_bh (lock);
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Re: [PATCH v1 net-next 3/3] tcp: Remove redundant sk_unhashed() in inet_unhash().
Quoted 27 lines · Kuniyuki Iwashima
inet_unhash()checkssk_unhashed()twice at the entry and after locking ehash/lhash bucket.The former was somehow added redundantly by commit
4f9bf2a2f5aa("tcp:
Don't acquireinet_listen_hashbucket::lockwith disabled BH.").
inet_unhash()is called for the full socket from 4 places, and it is always underlock_sock()or the socket is not yet published to other threads:1.
__sk_prot_rehash()-> called from inet_sk_reselect_saddr(), which has lockdep_sock_is_held()2.
sk_common_release()
-> called wheninet_create()orinet6_create()fail, then the
socket is not yet published3.
tcp_set_state()-> calls tcp_call_bpf_2arg(), and tcp_call_bpf() has sock_owned_by_me()4.
inet_ctl_sock_create()
-> creates a kernel socket and unhashes it immediately, but TCP
socket is not hashed insock_create_kern()(onlySOCK_RAWis)
Quoted 6 lines
So we do not need to check
sk_unhashed()twice before/after ehash/lhash lock ininet_unhash().Let's remove the 2nd one.
Signed-off-by Kuniyuki Iwashima <[email protected]>
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Fri, 19 Sep 2025 08:35:27 +0000 you wrote:
While reviewing the ehash fix series from Xuanqiang Luo [0], I noticed that
inet_twsk_hashdance_schedule()checks the retval of__sk_nulls_del_node_init_rcu(), which looks confusing.The test exists from the pre-git era:
$ git blame -L:tcp_tw_hashdance net/ipv4/tcp_minisocks.c e48c414ee61f4~[...]
Here is the summary with links:
- [v1,net-next,1/3] tcp: Remove osk from __inet_hash() arg.
https://git.kernel.org/netdev/net-next/c/6445bb832dc0 - [v1,net-next,2/3] tcp: Remove inet6_hash(). https://git.kernel.org/netdev/net-next/c/0ac44301e3bf - [v1,net-next,3/3] tcp: Remove redundant sk_unhashed() in inet_unhash(). https://git.kernel.org/netdev/net-next/c/bb6f9445666e
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html