public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 net] udp: Unhash auto-bound connected sk from 4-tuple hash table when disconnected.
@ 2026-02-27  3:55 Kuniyuki Iwashima
  2026-02-28 14:37 ` Eric Dumazet
  2026-02-28 16:10 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 3+ messages in thread
From: Kuniyuki Iwashima @ 2026-02-27  3:55 UTC (permalink / raw)
  To: Willem de Bruijn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni
  Cc: Simon Horman, Philo Lu, Kuniyuki Iwashima, Kuniyuki Iwashima,
	netdev

Let's say we bind() an UDP socket to the wildcard address with a
non-zero port, connect() it to an address, and disconnect it from
the address.

bind() sets SOCK_BINDPORT_LOCK on sk->sk_userlocks (but not
SOCK_BINDADDR_LOCK), and connect() calls udp_lib_hash4() to put
the socket into the 4-tuple hash table.

Then, __udp_disconnect() calls sk->sk_prot->rehash(sk).

It computes a new hash based on the wildcard address and moves
the socket to a new slot in the 4-tuple hash table, leaving a
garbage in the chain that no packet hits.

Let's remove such a socket from 4-tuple hash table when disconnected.

Note that udp_sk(sk)->udp_portaddr_hash needs to be udpated after
udp_hash4_dec(hslot2) in udp_unhash4().

Fixes: 78c91ae2c6de ("ipv4/udp: Add 4-tuple hash for connected socket")
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
v2: Moved udp_portaddr_hash update later
v1: https://lore.kernel.org/netdev/20260225002709.156882-1-kuniyu@google.com/
---
 net/ipv4/udp.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 014fdfdd331b..b60fad393e18 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -2287,7 +2287,6 @@ void udp_lib_rehash(struct sock *sk, u16 newhash, u16 newhash4)
 				     udp_sk(sk)->udp_port_hash);
 		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
 		nhslot2 = udp_hashslot2(udptable, newhash);
-		udp_sk(sk)->udp_portaddr_hash = newhash;
 
 		if (hslot2 != nhslot2 ||
 		    rcu_access_pointer(sk->sk_reuseport_cb)) {
@@ -2321,19 +2320,25 @@ void udp_lib_rehash(struct sock *sk, u16 newhash, u16 newhash4)
 		if (udp_hashed4(sk)) {
 			spin_lock_bh(&hslot->lock);
 
-			udp_rehash4(udptable, sk, newhash4);
-			if (hslot2 != nhslot2) {
-				spin_lock(&hslot2->lock);
-				udp_hash4_dec(hslot2);
-				spin_unlock(&hslot2->lock);
-
-				spin_lock(&nhslot2->lock);
-				udp_hash4_inc(nhslot2);
-				spin_unlock(&nhslot2->lock);
+			if (inet_rcv_saddr_any(sk)) {
+				udp_unhash4(udptable, sk);
+			} else {
+				udp_rehash4(udptable, sk, newhash4);
+				if (hslot2 != nhslot2) {
+					spin_lock(&hslot2->lock);
+					udp_hash4_dec(hslot2);
+					spin_unlock(&hslot2->lock);
+
+					spin_lock(&nhslot2->lock);
+					udp_hash4_inc(nhslot2);
+					spin_unlock(&nhslot2->lock);
+				}
 			}
 
 			spin_unlock_bh(&hslot->lock);
 		}
+
+		udp_sk(sk)->udp_portaddr_hash = newhash;
 	}
 }
 EXPORT_IPV6_MOD(udp_lib_rehash);
-- 
2.53.0.473.g4a7958ca14-goog


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

* Re: [PATCH v2 net] udp: Unhash auto-bound connected sk from 4-tuple hash table when disconnected.
  2026-02-27  3:55 [PATCH v2 net] udp: Unhash auto-bound connected sk from 4-tuple hash table when disconnected Kuniyuki Iwashima
@ 2026-02-28 14:37 ` Eric Dumazet
  2026-02-28 16:10 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Dumazet @ 2026-02-28 14:37 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: Willem de Bruijn, David S. Miller, Jakub Kicinski, Paolo Abeni,
	Simon Horman, Philo Lu, Kuniyuki Iwashima, netdev

On Fri, Feb 27, 2026 at 4:59 AM Kuniyuki Iwashima <kuniyu@google.com> wrote:
>
> Let's say we bind() an UDP socket to the wildcard address with a
> non-zero port, connect() it to an address, and disconnect it from
> the address.
>
> bind() sets SOCK_BINDPORT_LOCK on sk->sk_userlocks (but not
> SOCK_BINDADDR_LOCK), and connect() calls udp_lib_hash4() to put
> the socket into the 4-tuple hash table.
>
> Then, __udp_disconnect() calls sk->sk_prot->rehash(sk).
>
> It computes a new hash based on the wildcard address and moves
> the socket to a new slot in the 4-tuple hash table, leaving a
> garbage in the chain that no packet hits.
>
> Let's remove such a socket from 4-tuple hash table when disconnected.
>
> Note that udp_sk(sk)->udp_portaddr_hash needs to be udpated after
> udp_hash4_dec(hslot2) in udp_unhash4().
>
> Fixes: 78c91ae2c6de ("ipv4/udp: Add 4-tuple hash for connected socket")
> Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
> ---
> v2: Moved udp_portaddr_hash update later
> v1: https://lore.kernel.org/netdev/20260225002709.156882-1-kuniyu@google.com/

Reviewed-by: Eric Dumazet <edumazet@google.com>

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

* Re: [PATCH v2 net] udp: Unhash auto-bound connected sk from 4-tuple hash table when disconnected.
  2026-02-27  3:55 [PATCH v2 net] udp: Unhash auto-bound connected sk from 4-tuple hash table when disconnected Kuniyuki Iwashima
  2026-02-28 14:37 ` Eric Dumazet
@ 2026-02-28 16:10 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-02-28 16:10 UTC (permalink / raw)
  To: Kuniyuki Iwashima
  Cc: willemdebruijn.kernel, davem, edumazet, kuba, pabeni, horms,
	lulie, kuni1840, netdev

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Fri, 27 Feb 2026 03:55:35 +0000 you wrote:
> Let's say we bind() an UDP socket to the wildcard address with a
> non-zero port, connect() it to an address, and disconnect it from
> the address.
> 
> bind() sets SOCK_BINDPORT_LOCK on sk->sk_userlocks (but not
> SOCK_BINDADDR_LOCK), and connect() calls udp_lib_hash4() to put
> the socket into the 4-tuple hash table.
> 
> [...]

Here is the summary with links:
  - [v2,net] udp: Unhash auto-bound connected sk from 4-tuple hash table when disconnected.
    https://git.kernel.org/netdev/net/c/6996a2d2d0a6

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] 3+ messages in thread

end of thread, other threads:[~2026-02-28 16:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-27  3:55 [PATCH v2 net] udp: Unhash auto-bound connected sk from 4-tuple hash table when disconnected Kuniyuki Iwashima
2026-02-28 14:37 ` Eric Dumazet
2026-02-28 16:10 ` 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