Skip to contentSkip to conversation

udp: restore hlist_nulls for primary and secondary hash tables

PATCH In review Eric Dumazet 7 participants 8 messages Oct 30 – Nov 3

Subsystems (6)
lore
Share & tools
lore

Eric Dumazet

author maintainer
#0 lore raw

PATCH v2 1/1 udp: restore hlist_nulls for primary and secondary hash tables

Commit ca065d0cf80f ("udp: no longer use SLAB_DESTROY_BY_RCU") switched UDP sockets to SOCK_RCU_FREE and converted udptable->hash and udptable->hash2 from hlist_nulls_head to hlist_head.

While SOCK_RCU_FREE guarantees that a socket is not freed before an RCU grace period elapses, a live UDP socket can still be unhashed or moved to a different hash bucket without an RCU grace period:

1. udp_lib_rehash() moves sk->skc_portaddr_node from hslot2 to a different nhslot2 when inet_rcv_saddr changes (for instance via connect() or disconnect() on an wildcard-bound socket).
2. __udp_disconnect() calls udp_lib_unhash() when an implicitly bound port was used, and a subsequent connect() or bind() can immediately re-insert sk->sk_nulls_node and sk->skc_portaddr_node into different hash and hash2 buckets.

Because hlist_add_head_rcu() overwrites node->next with the new bucket chain without waiting for an RCU grace period, a concurrent lockless reader in udp4_lib_lookup1/2() or udp6_lib_lookup1/2() traversing the old bucket can silently jump to the new bucket chain, terminate early, and miss a matching socket that was located later in the original bucket.

Restore hlist_nulls for udptable->hash and udptable->hash2 while keeping SOCK_RCU_FREE (so RCU lookups remain refcount-free), and restart the bucket traversal if get_nulls_value(node) does not match the expected bucket index.

Fixes ca065d0cf80f ("udp: no longer use SLAB_DESTROY_BY_RCU")
Cc Willem de Bruijn <[email protected]>

---
v2: Keep unconditional head insertion in udp_lib_rehash(), the IPv6
SO_REUSEPORT tail insert defeated the nulls check (Sashiko). Add comments in __udp4_lib_mcast_deliver() and __udp6_lib_mcast_deliver() explaining why no restart is possible. Rename sk_for_each_entry_offset_rcu in .clang-format (Sashiko). Add Reported-by/Closes tags (David Laight).
v1: https://lore.kernel.org/netdev/[email protected]/

---
 .clang-format       |   2 +-
 include/linux/udp.h |  11 +++--
 include/net/sock.h  |  23 +++++----
 include/net/udp.h   |   5 +-
 net/ipv4/udp.c      | 116 +++++++++++++++++++++++++++++++-------------
 net/ipv4/udp_diag.c |   5 +-
 net/ipv6/udp.c      |  46 ++++++++++++++----
 7 files changed, 143 insertions(+), 65 deletions(-)
.clang-format +1 -1
index 395292ab2ac634e01d0c8aae2313c96b5fb8de3d..5d9dc5d19b9a4044b499e319e8030dba7a90f1cc 100644
@@ -689,11 +689,11 @@ ForEachMacros:
   - 'sk_for_each'
   - 'sk_for_each_bound'
   - 'sk_for_each_bound_safe'
-  - 'sk_for_each_entry_offset_rcu'
   - 'sk_for_each_from'
   - 'sk_for_each_rcu'
   - 'sk_for_each_safe'
   - 'sk_nulls_for_each'
+  - 'sk_nulls_for_each_entry_offset_rcu'
   - 'sk_nulls_for_each_from'
   - 'sk_nulls_for_each_rcu'
   - 'snd_array_for_each'
include/linux/udp.h +6 -5
index 998906ec3b32add4652bde3ee9c28e8b2b98ce51..a1990350fd22f3418724c0e06d4b7405779f7c2b 100644
@@ -243,14 +243,15 @@ static inline void udp_allow_gso(struct sock *sk)
 	udp_set_bit(ACCEPT_FRAGLIST, sk);
 }
 
-#define udp_portaddr_for_each_entry(__sk, list) \
-	hlist_for_each_entry(__sk, list, __sk_common.skc_portaddr_node)
+#define udp_portaddr_for_each_entry(__sk, node, list) \
+	hlist_nulls_for_each_entry(__sk, node, list, __sk_common.skc_portaddr_node)
 
 #define udp_portaddr_for_each_entry_from(__sk) \
-	hlist_for_each_entry_from(__sk, __sk_common.skc_portaddr_node)
+	for (; __sk; __sk = hlist_nulls_entry_safe((__sk)->__sk_common.skc_portaddr_node.next, \
+						   typeof(*(__sk)), __sk_common.skc_portaddr_node))
 
-#define udp_portaddr_for_each_entry_rcu(__sk, list) \
-	hlist_for_each_entry_rcu(__sk, list, __sk_common.skc_portaddr_node)
+#define udp_portaddr_for_each_entry_rcu(__sk, node, list) \
+	hlist_nulls_for_each_entry_rcu(__sk, node, list, __sk_common.skc_portaddr_node)
 
 #if !IS_ENABLED(CONFIG_BASE_SMALL)
 #define udp_lrpa_for_each_entry_rcu(__up, node, list) \
include/net/sock.h +14 -9
index 173b37b2b8bb09852fc2a83468eaf1577484f2f7..6140244735b55d1bf0ea993b94b7a03eb1fbef9f 100644
@@ -184,7 +184,7 @@ struct sock_common {
 	int			skc_bound_dev_if;
 	union {
 		struct hlist_node	skc_bind_node;
-		struct hlist_node	skc_portaddr_node;
+		struct hlist_nulls_node	skc_portaddr_node;
 	};
 	struct proto		*skc_prot;
 	possible_net_t		skc_net;
@@ -932,7 +932,11 @@ static inline void __sk_nulls_add_node_tail_rcu(struct sock *sk, struct hlist_nu
 static inline void sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
 {
 	sock_hold(sk);
-	__sk_nulls_add_node_rcu(sk, list);
+	if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
+	    sk->sk_family == AF_INET6)
+		__sk_nulls_add_node_tail_rcu(sk, list);
+	else
+		__sk_nulls_add_node_rcu(sk, list);
 }
 
 static inline void __sk_del_bind_node(struct sock *sk)
@@ -967,18 +971,19 @@ static inline void sk_add_bind_node(struct sock *sk,
 	hlist_for_each_entry_safe(__sk, tmp, list, sk_bind_node)
 
 /**
- * sk_for_each_entry_offset_rcu - iterate over a list at a given struct offset
+ * sk_nulls_for_each_entry_offset_rcu - iterate over a list at a given struct offset
  * @tpos:	the type * to use as a loop cursor.
- * @pos:	the &struct hlist_node to use as a loop cursor.
+ * @pos:	the &struct hlist_nulls_node to use as a loop cursor.
  * @head:	the head for your list.
- * @offset:	offset of hlist_node within the struct.
+ * @offset:	offset of hlist_nulls_node within the struct.
  *
  */
-#define sk_for_each_entry_offset_rcu(tpos, pos, head, offset)		       \
-	for (pos = rcu_dereference(hlist_first_rcu(head));		       \
-	     pos != NULL &&						       \
+#define sk_nulls_for_each_entry_offset_rcu(tpos, pos, head, offset)	       \
+	for (({ barrier(); }),						       \
+	     pos = rcu_dereference_raw(hlist_nulls_first_rcu(head));	       \
+	     (!is_a_nulls(pos)) &&					       \
 		({ tpos = (typeof(*tpos) *)((void *)pos - offset); 1;});       \
-	     pos = rcu_dereference(hlist_next_rcu(pos)))
+	     pos = rcu_dereference_raw(hlist_nulls_next_rcu(pos)))
 
 static inline struct user_namespace *sk_user_ns(const struct sock *sk)
 {
include/net/udp.h +1 -4
index 5bc25dcf25fbaabbab9a38ae048480e581ddcde5..eb08a507a7dc9c1a6bf80a20267791552ee8e678 100644
@@ -56,10 +56,7 @@ struct udp_skb_cb {
  */
 struct udp_hslot {
 	union {
-		struct hlist_head	head;
-		/* hash4 uses hlist_nulls to avoid moving wrongly onto another
-		 * hlist, because rehash() can happen with lookup().
-		 */
+		struct hlist_nulls_head	head;
 		struct hlist_nulls_head	nulls_head;
 	};
 	int			count;
net/ipv4/udp.c +81 -35

325 lines not on this page · raw message

net/ipv4/udp_diag.c +3 -2
index f4b24e628cf8ded821d0c1887dd6ca5b83c4c8e2..5e0b4e07d9c1d80a727647273c9793ca11626827 100644
@@ -100,15 +100,16 @@ static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
 
 	for (slot = s_slot; slot <= table->mask; s_num = 0, slot++) {
 		struct udp_hslot *hslot = &table->hash[slot];
+		struct hlist_nulls_node *node;
 		struct sock *sk;
 
 		num = 0;
 
-		if (hlist_empty(&hslot->head))
+		if (hlist_nulls_empty(&hslot->head))
 			continue;
 
 		spin_lock_bh(&hslot->lock);
-		sk_for_each(sk, &hslot->head) {
+		sk_nulls_for_each(sk, node, &hslot->head) {
 			struct inet_sock *inet = inet_sk(sk);
 
 			if (!net_eq(sock_net(sk), net))
net/ipv6/udp.c +37 -9

115 lines not on this page · raw message

Signature · 1 line
2.53.0
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- Whether the issue was actually triggered, or is only theoretical (e.g. found by code inspection). If it was triggered please include the symptoms, like the stack trace or error messages.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.

The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.

Acked-by on v2 1/1
#2 lore raw
In reply to Eric Dumazet

net/ipv4/udp.c static struct sock *bpf_iter_udp_batch(struct seq_file *seq) · line 3485

Quoted 13 lines · Eric Dumazet
net/ipv4/udp.c +3 -3
@@ -3431,12 +3477,12 @@ static struct sock *bpf_iter_udp_batch(struct seq_file *seq)
 	for (; state->bucket <= udptable->mask; state->bucket++) {
 		struct udp_hslot *hslot2 = &udptable->hash2[state->bucket].hslot;
 
-		if (hlist_empty(&hslot2->head))
+		if (hlist_nulls_empty(&hslot2->head))
 			goto next_bucket;
 
 		spin_lock_bh(&hslot2->lock);
-		sk = hlist_entry_safe(hslot2->head.first, struct sock,
-				      __sk_common.skc_portaddr_node);
+		sk = hlist_nulls_entry_safe(hslot2->head.first, struct sock,
+					    __sk_common.skc_portaddr_node);

For bpf bits:

#3 lore raw
In reply to Eric Dumazet
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] udp: tail insertion in udp_lib_get_port() defeats hlist_nulls restart check --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] udp: tail insertion in udp_lib_get_port() defeats hlist_nulls restart check --

commit f98bb6c6e4827227f9083bb5452ae79a7eeaa175
Author: Eric Dumazet <[email protected]>

udp: restore hlist_nulls for primary and secondary hash tables

This commit restores hlist_nulls for the primary (udptable->hash) and secondary (udptable->hash2) UDP socket hash tables so lockless RCU readers can detect when a socket is concurrently unhashed or moved to another bucket and restart the lookup.

Quoted 4 lines
include/net/sock.h +0 -0
index 173b37b2b8bb0..6140244735b55 100644

[ ... ]

Quoted 13 lines
include/net/sock.h +5 -1
@@ -932,7 +932,11 @@ static inline void __sk_nulls_add_node_tail_rcu(struct sock *sk, struct hlist_nu
 static inline void sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
 {
 	sock_hold(sk);
-	__sk_nulls_add_node_rcu(sk, list);
+	if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
+	    sk->sk_family == AF_INET6)
+		__sk_nulls_add_node_tail_rcu(sk, list);
+	else
+		__sk_nulls_add_node_rcu(sk, list);
 }
 
 static inline void __sk_del_bind_node(struct sock *sk)

[ ... ]

Quoted 4 lines
net/ipv4/udp.c +0 -0
index 303d0102519ac..7d3790550241f 100644

[ ... ]

Quoted 15 lines
net/ipv4/udp.c +1 -3
@@ -324,7 +327,7 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum,
 
 		sock_set_flag(sk, SOCK_RCU_FREE);
 
-		sk_add_node_rcu(sk, &hslot->head);
+		sk_nulls_add_node_rcu(sk, &hslot->head);
 		hslot->count++;
 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
 
@@ -332,11 +335,11 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum,
 		spin_lock(&hslot2->lock);
 		if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
 		    sk->sk_family == AF_INET6)
-			hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node,
-					   &hslot2->head);

net/ipv4/udp.c int udp_lib_get_port(struct sock *sk, unsigned short snum, · line 345

Quoted 10 lines
net/ipv4/udp.c +4 -2
+			hlist_nulls_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node,
+						 &hslot2->head);
 		else
-			hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
-					   &hslot2->head);
+			hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
+						 &hslot2->head);
 		hslot2->count++;
 		spin_unlock(&hslot2->lock);
 	}

[Severity: Medium]
Can the tail insertions in sk_nulls_add_node_rcu() and udp_lib_get_port() still defeat the get_nulls_value() restart check when a live AF_INET6 SO_REUSEPORT socket is unhashed and re-hashed?

In udp_lib_rehash(), head insertion is used unconditionally with the comment:

	/* Note: head insertion is required here, so
	 * that a lockless reader parked on this socket
	 * restarts from the head of the new chain
	 * instead of hitting a nulls marker early.
	 */
	hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
				 &nhslot2->head);

However, a live AF_INET6 SO_REUSEPORT socket bound without SOCK_BINDPORT_LOCK (for example via autobind in connect() or sendmsg(), or bind() with port 0, or when BPF_CGROUP_RUN_PROG_INET6_POST_BIND fails in __inet6_bind()) can also be unhashed without an RCU grace period via __udp_disconnect() -> udp_lib_unhash(), and then immediately re-inserted via bind() or connect() -> udp_lib_get_port().

If bucket S contains:

	head -> sk1 -> sk2 -> NULLS_MARKER(S)

and a lockless reader in udp4_lib_lookup1(), udp4_lib_lookup2(), udp6_lib_lookup1(), udp6_lib_lookup2(), or __udp4_lib_mcast_demux_lookup() is parked on sk1 while sk1 is unhashed and re-inserted into the same bucket S (or if a reader in bucket S jumps via a moved socket into another bucket and lands on a socket that is concurrently unhashed and tail-inserted into bucket S), __sk_nulls_add_node_tail_rcu() and hlist_nulls_add_tail_rcu() place sk1 at the end of bucket S:

	head -> sk2 -> sk1 -> NULLS_MARKER(S)

and overwrite sk1's next pointer with NULLS_MARKER(S). When the reader resumes and advances past sk1, it immediately hits NULLS_MARKER(S) with get_nulls_value(node) == S, so the restart check passes and the lookup terminates early without ever visiting sk2.

Also, in the primary hash table (udptable->hash), udp4_lib_lookup1() and udp6_lib_lookup1() always score every socket in hslot->head and do not use inet_lookup_reuseport() or inet6_lookup_reuseport(), so tail insertion in sk_nulls_add_node_rcu() does not appear to help reuseport selection on hslot->head.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw
In reply to Eric Dumazet
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The hlist_nulls restart does not work when an IPv6 SO_REUSEPORT socket is tail-inserted back into the bucket it was just removed from.

--

udp: restore hlist_nulls for primary and secondary hash tables

This patch converts the UDP primary (hash) and secondary (hash2) tables back to hlist_nulls and keeps SOCK_RCU_FREE. The lockless lookups now restart when the nulls value at the end of a walk does not match the expected slot. The goal is to stop readers from missing sockets that were moved to another chain by rehash, or by unhash followed by re-insert, without an RCU grace period.

Quoted 4 lines
include/net/sock.h +0 -0
index 173b37b2b8bb0..6140244735b55 100644

[ ... ]

Quoted 11 lines
include/net/sock.h +5 -1
@@ -932,7 +932,11 @@ static inline void __sk_nulls_add_node_tail_rcu(struct sock *sk, struct hlist_nu
 static inline void sk_nulls_add_node_rcu(struct sock *sk, struct hlist_nulls_head *list)
 {
 	sock_hold(sk);
-	__sk_nulls_add_node_rcu(sk, list);
+	if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
+	    sk->sk_family == AF_INET6)
+		__sk_nulls_add_node_tail_rcu(sk, list);
+	else
+		__sk_nulls_add_node_rcu(sk, list);
 }

[ ... ]

Quoted 4 lines
net/ipv4/udp.c +0 -0
index 303d0102519ac..7d3790550241f 100644

[ ... ]

Quoted 15 lines
net/ipv4/udp.c +1 -3
@@ -324,7 +327,7 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum,
 
 		sock_set_flag(sk, SOCK_RCU_FREE);
 
-		sk_add_node_rcu(sk, &hslot->head);
+		sk_nulls_add_node_rcu(sk, &hslot->head);
 		hslot->count++;
 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
 
@@ -332,11 +335,11 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum,
 		spin_lock(&hslot2->lock);
 		if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
 		    sk->sk_family == AF_INET6)
-			hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node,
-					   &hslot2->head);

net/ipv4/udp.c int udp_lib_get_port(struct sock *sk, unsigned short snum, · line 345

Quoted 10 lines
net/ipv4/udp.c +4 -2
+			hlist_nulls_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node,
+						 &hslot2->head);
 		else
-			hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
-					   &hslot2->head);
+			hlist_nulls_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
+						 &hslot2->head);
 		hslot2->count++;
 		spin_unlock(&hslot2->lock);
 	}

[Severity: High]
Does the nulls restart still catch the second scenario from the commit message when the socket is an AF_INET6 socket with SO_REUSEPORT set?

The commit message says:

2. __udp_disconnect() calls udp_lib_unhash() when an implicitly bound port was used, and a subsequent connect() or bind() can immediately re-insert sk->sk_nulls_node and sk->skc_portaddr_node into different hash and hash2 buckets.

In that case udp_lib_get_port() puts the socket back at the tail of both chains:

- the primary hash, through sk_nulls_add_node_rcu(), which now tail-inserts in this case
- hash2, through hlist_nulls_add_tail_rcu()

hlist_nulls_add_tail_rcu() sets n->next = last->next, which is the nulls marker of the target slot. hlist_nulls_del_init_rcu() in udp_lib_unhash() leaves X->next pointing at its old successor. No grace period separates the unhash from the re-insert.

If the socket goes back into the same slot it was just removed from, a reader parked on it gets the expected nulls value and does not restart. Here is the sequence for hash2, with X at the head after the connect() rehash:

hash2 slot S2: X -> Y -> nulls(S2)

CPU1 CPU2 udp6_lib_lookup2()
sk = X
connect(X, AF_UNSPEC)

    __udp_disconnect()
      sk->sk_prot->unhash(sk)           (SOCK_BINDPORT_LOCK clear)
        hlist_nulls_del_init_rcu()      S2: Y -> nulls(S2)
  bind(X, saddr:p)
    udp_lib_get_port()
      hlist_nulls_add_tail_rcu()        S2: Y -> X -> nulls(S2)
                                        node = X->next == nulls(S2)
                                        get_nulls_value(node) == slot2

Y is never scored.

If a wildcard socket is bound to [::]:p, the wildcard hash2 lookup returns it. The lookup1 fallback in __udp6_lib_lookup() is then skipped, so the packet goes to the wildcard socket instead of Y. If there is no wildcard socket, the packet is dropped instead.

The primary hash has the same problem. Re-binding to the same port always lands in the same primary slot, and that chain is now tail-inserted too. That exposes udp6_lib_lookup1(), udp4_lib_lookup1() and __udp4_lib_mcast_demux_lookup() in the same way.

The v2 changelog says the IPv6 SO_REUSEPORT tail insert defeated the nulls check. The new comment in udp_lib_rehash() says head insertion is required for the restart to work. Only the rehash path was changed, though. Can the re-insert path in udp_lib_get_port() still hit the same problem?

Separately, the commit message doesn't mention that the shared helper sk_nulls_add_node_rcu() in include/net/sock.h now tail-inserts for IPv6 reuseport sockets. Before this patch it only did head insertion. Could this be noted in the commit message, or kept local to udp instead?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261030122045.3035709-1-edumazet%40kernel.org
maintainer
#5 lore raw
Quoted 5 lines · On 11/2/26 13:22, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

Quoted 8 lines

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The hlist_nulls restart does not work when an IPv6 SO_REUSEPORT socket is tail-inserted back into the bucket it was just removed from.

This look real to me. On the flip side the most straight forward fix - avoid tail insertion in udp_lib_get_port() would re-introduce the issue addressed by commit d894ba18d4e449b3a7f6eb491f16c9e02933736e. But rehash already avoids tail insertion...

/P

author maintainer
#6 lore raw
Quoted 13 lines · Paolo Abeni
Quoted 11 lines · On 11/2/26 13:22, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

Quoted 7 lines
Quoted 2 lines

- [High] The hlist_nulls restart does not work when an IPv6 SO_REUSEPORT socket is tail-inserted back into the bucket it was just removed from.

This look real to me. On the flip side the most straight forward fix - avoid tail insertion in udp_lib_get_port() would re-introduce the issue addressed by commit d894ba18d4e449b3a7f6eb491f16c9e02933736e. But rehash already avoids tail insertion...

Agreed, it is real, although not a regression: before this patch, hlist_add_tail_rcu() set n->next = NULL and a reader parked on the socket stopped there as well. v2 simply did not fix this case.

We still need the tail insertion: udp4_lib_lookup2() returns as soon as the first matching socket has a reuseport group without connections, so a dual-stack IPv6 group placed first would win over AF_INET sockets, despite their better score.

The problem only exists while a reader can still be parked on the socket, that is less than an RCU grace period after udp_lib_unhash().

v3 records get_state_synchronize_rcu() in udp_lib_unhash(), and udp_lib_get_port() only uses tail insertion if poll_state_synchronize_rcu() says the grace period has elapsed, otherwise it uses head insertion, as udp_lib_rehash() already does. The cookie fits in an existing hole of struct udp_sock.

The cost is that an IPv6 SO_REUSEPORT socket rebound within a grace period of its disconnect keeps its head position, so the v4/v6 ordering is not enforced for it, the same as after a rehash today. This needs disconnect() then bind() on the same port, with AF_INET and dual-stack AF_INET6 reuseport sockets sharing that port.

I also kept sk_nulls_add_node_rcu() unchanged, the tail insertion logic is now local to UDP.

Thanks !

pw-bot: cr

#7 lore raw
Quoted 40 lines · On Tue, 3 Nov 2026 14:34:29 +0100 Eric Dumazet <[email protected]> wrote:
Quoted 13 lines · Paolo Abeni
Quoted 11 lines · On 11/2/26 13:22, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

Quoted 7 lines
Quoted 2 lines

- [High] The hlist_nulls restart does not work when an IPv6 SO_REUSEPORT socket is tail-inserted back into the bucket it was just removed from.

This look real to me. On the flip side the most straight forward fix - avoid tail insertion in udp_lib_get_port() would re-introduce the issue addressed by commit d894ba18d4e449b3a7f6eb491f16c9e02933736e. But rehash already avoids tail insertion...

Agreed, it is real, although not a regression: before this patch, hlist_add_tail_rcu() set n->next = NULL and a reader parked on the socket stopped there as well. v2 simply did not fix this case.

We still need the tail insertion: udp4_lib_lookup2() returns as soon as the first matching socket has a reuseport group without connections, so a dual-stack IPv6 group placed first would win over AF_INET sockets, despite their better score.

The problem only exists while a reader can still be parked on the socket, that is less than an RCU grace period after udp_lib_unhash().

v3 records get_state_synchronize_rcu() in udp_lib_unhash(), and udp_lib_get_port() only uses tail insertion if poll_state_synchronize_rcu() says the grace period has elapsed, otherwise it uses head insertion, as udp_lib_rehash() already does. The cookie fits in an existing hole of struct udp_sock.

Quoted 8 lines

The cost is that an IPv6 SO_REUSEPORT socket rebound within a grace period of its disconnect keeps its head position, so the v4/v6 ordering is not enforced for it, the same as after a rehash today. This needs disconnect() then bind() on the same port, with AF_INET and dual-stack AF_INET6 reuseport sockets sharing that port.

I also kept sk_nulls_add_node_rcu() unchanged, the tail insertion logic is now local to UDP.

Is there a different issue that is a socket is rehashed there is a chance that it ends up back on the same list.
If that happens you need it to stay in the same place (if possible), certainly moving it to the end would mean that a reader could miss out some items but not see a different nulls value.

There is an even more unlikely possibility of a reader starting on chain A, being moved to chain B, and then back to A - so missing some items on A. Probably just worth noting that it is extremely unlikely.

David

Quoted 4 lines

Thanks !

pw-bot: cr