udp: Add 4-tuple hash for connected sockets
Subsystems (3)
v5 cover udp: Add 4-tuple hash for connected sockets
This patchset introduces 4-tuple hash for connected udp sockets, to make connected udp lookup faster. Test using udpgso_bench_tx/rx shows no obvious difference w/ and w/o this patchset for unconnected socket receiving (see v4 thread).
Patch1: Add a new counter for hslot2 named hash4_cnt, to avoid cache line
miss when lookup.
Patch2 and 3: Implement 4-tuple hash for ipv4.
(That for ipv6 is in progress.)
The detailed motivation is described in Patch 3.
The 4-tuple hash increases the size of udp_sock and udp_hslot. Thus add it with CONFIG_BASE_SMALL check, i.e., it's a no op with CONFIG_BASE_SMALL.
AFAICS the patchset can be further improved by:
(a) Better interact with hash2/reuseport. Now hash4 hardly affects other mechanisms, but maintaining sockets in both hash4 and hash2 lists seems unnecessary.
(b) Support early demux and ipv6.
changelogs:
v4 -> v5 (Paolo Abeni):
- Add CONFIG_BASE_SMALL with which udp hash4 does nothing
v3 -> v4 (Willem de Bruijn):
- fix mistakes in udp_pernet_table_alloc()
RFCv2 -> v3 (Gur Stavi):
- minor fix in udp_hashslot2() and udp_table_init()
- add rcu sync in rehash4()
RFCv1 -> RFCv2:
- add a new struct for hslot2
- remove the sockopt UDP_HASH4 because it has little side effect for unconnected sockets
- add rehash in connect()
- re-organize the patch into 3 smaller ones
- other minor fix
v4:
https://lore.kernel.org/all/[email protected]/
v3:
https://lore.kernel.org/all/[email protected]/
RFCv2:
https://lore.kernel.org/all/[email protected]/
RFCv1:
https://lore.kernel.org/all/[email protected]/
Philo Lu (3):
net/udp: Add a new struct for hash2 slot
net/udp: Add 4-tuple hash list basis
ipv4/udp: Add 4-tuple hash for connected socket
include/linux/udp.h | 11 +++ include/net/udp.h | 111 +++++++++++++++++++++- net/ipv4/udp.c | 217 +++++++++++++++++++++++++++++++++++++++----- net/ipv6/udp.c | 17 ++-- 4 files changed, 317 insertions(+), 39 deletions(-)
--
2.32.0.3.g01195cf9f
PATCH v5 1/3 net/udp: Add a new struct for hash2 slot
Preparing for udp 4-tuple hash (uhash4 for short).
To implement uhash4 without cache line missing when lookup, hslot2 is used to record the number of hashed sockets in hslot4. Thus adding a new struct udp_hslot_main with field hash4_cnt, which is used by hash2. The new struct is used to avoid doubling the size of udp_hslot.
Before uhash4 lookup, firstly checking hash4_cnt to see if there are hashed sks in hslot4. Because hslot2 is always used in lookup, there is no cache line miss.
Related helpers are updated, and use the helpers as possible.
uhash4 is implemented in following patches.
--- include/net/udp.h | 23 +++++++++++++++++++---- net/ipv4/udp.c | 44 +++++++++++++++++++++++--------------------- net/ipv6/udp.c | 15 ++++++--------- 3 files changed, 48 insertions(+), 34 deletions(-)
include/net/udp.h +19 -4
@@ -50,7 +50,7 @@ struct udp_skb_cb {#define UDP_SKB_CB (__skb) ((struct udp_skb_cb *)((__skb)->cb))/** -* struct udp_hslot - UDP hash slot +* struct udp_hslot - UDP hash slot used by udp_table.hash * * @head: head of list of sockets * @count: number of sockets in 'head' list
@@ -60,7 +60,21 @@ struct udp_hslot {struct hlist_head head;int count;spinlock_t lock; -} __attribute__((aligned(2 * sizeof(long)))); +}__aligned(2 * sizeof(long)) ; + +/** +* struct udp_hslot_main - UDP hash slot used by udp_table.hash2 +* +* @hslot: basic hash slot +* @hash4_cnt: number of sockets in hslot4 of the same (local port, local address) +*/ +struct udp_hslot_main { +struct udp_hslot hslot;/* must be the first member */ +#if !IS_ENABLED (CONFIG_BASE_SMALL ) +u32 hash4_cnt; +#endif +}__aligned(2 * sizeof(long)) ; +#define UDP_HSLOT_MAIN (__hslot) ((struct udp_hslot_main *)(__hslot))/** * struct udp_table - UDP table
@@ -72,7 +86,7 @@ struct udp_hslot {*/ struct udp_table {struct udp_hslot *hash; -struct udp_hslot *hash2; +struct udp_hslot_main *hash2;unsigned int mask;unsigned int log; };
@@ -84,6 +98,7 @@ static inline struct udp_hslot *udp_hashslot(struct udp_table *table, {return &table->hash[udp_hashfn (net, num, table->mask)]; } +/* * For secondary hash, net_hash_mix() is performed before calling * udp_hashslot2(), this explains difference with udp_hashslot()
@@ -91,7 +106,7 @@ static inline struct udp_hslot *udp_hashslot(struct udp_table *table,static inline struct udp_hslot *udp_hashslot2 (struct udp_table *table,unsigned int hash) { -return &table->hash2[hash & table->mask]; +return &table->hash2[hash & table->mask].hslot; }extern struct proto udp_prot;
net/ipv4/udp.c +23 -21
125 lines not on this page · raw message
net/ipv6/udp.c +6 -9
@@ -224,13 +224,12 @@ struct sock *__udp6_lib_lookup(const struct net *net, struct sk_buff *skb) {unsigned short hnum =ntohs (dport); -unsigned int hash2, slot2;struct udp_hslot *hslot2;struct sock *result, *sk; +unsigned int hash2; hash2 =ipv6_portaddr_hash (net, daddr, hnum); - slot2 = hash2 & udptable->mask; - hslot2 = &udptable->hash2[slot2]; + hslot2 =udp_hashslot2 (udptable, hash2);/* Lookup connected or non-wildcard sockets */ result = udp6_lib_lookup2(net, saddr, sport,
@@ -257,8 +256,7 @@ struct sock *__udp6_lib_lookup(const struct net *net,/* Lookup wildcard sockets */ hash2 =ipv6_portaddr_hash (net, &in6addr_any, hnum); - slot2 = hash2 & udptable->mask; - hslot2 = &udptable->hash2[slot2]; + hslot2 =udp_hashslot2 (udptable, hash2); result = udp6_lib_lookup2(net, saddr, sport, &in6addr_any, hnum, dif, sdif,
@@ -859,7 +857,7 @@ static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb, udptable->mask; hash2 =ipv6_portaddr_hash (net, daddr, hnum) & udptable->mask; start_lookup: - hslot = &udptable->hash2[hash2]; + hslot = &udptable->hash2[hash2].hslot; offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node); }
@@ -1065,14 +1063,13 @@ static struct sock *__udp6_lib_demux_lookup(struct net *net, {struct udp_table *udptable = net->ipv4.udp_table;unsigned short hnum =ntohs (loc_port); -unsigned int hash2, slot2;struct udp_hslot *hslot2; +unsigned int hash2;__portpair ports;struct sock *sk; hash2 =ipv6_portaddr_hash (net, loc_addr, hnum); - slot2 = hash2 & udptable->mask; - hslot2 = &udptable->hash2[slot2]; + hslot2 =udp_hashslot2 (udptable, hash2); ports =INET_COMBINED_PORTS (rmt_port, hnum);udp_portaddr_for_each_entry_rcu (sk, &hslot2->head) {
Signature · 1 line
2.32.0.3.g01195cf9f
Paolo Abeni
maintainerRe: [PATCH v5 net-next 1/3] net/udp: Add a new struct for hash2 slot
Quoted 9 lines · Philo Lu
net/ipv4/udp.c +1
-1@@ -3438,16 +3436,17 @@ void __init udp_table_init(struct udp_table *table, const char *name)UDP_HTABLE_SIZE_MIN , UDP_HTABLE_SIZE_MAX); - table->hash2 = table->hash + (table->mask +1 ); + table->hash2 = (void *)(table->hash + (table->mask +1 ));for (i =0 ; i <= table->mask; i++) {INIT_HLIST_HEAD (&table->hash[i].head); table->hash[i].count =0 ;
net/ipv4/udp.c void __init udp_table_init(struct udp_table *table, const char *name) · line 3449
Quoted 10 lines
net/ipv4/udp.c +4
-3spin_lock_init (&table->hash[i].lock); }for (i =0 ; i <= table->mask; i++) { -INIT_HLIST_HEAD (&table->hash2[i].head); - table->hash2[i].count =0 ; -spin_lock_init (&table->hash2[i].lock); +INIT_HLIST_HEAD (&table->hash2[i].hslot.head); + table->hash2[i].hslot.count =0 ; +spin_lock_init (&table->hash2[i].hslot.lock); + table->hash2[i].hash4_cnt =0 ;
Does not build for CONFIG_BASE_SMALL=y kernels. You need to put all the hash4_cnt access under compiler guards.
Cheers,
Paolo
Paolo Abeni
maintainerRe: [PATCH v5 net-next 1/3] net/udp: Add a new struct for hash2 slot
Quoted 15 lines · Paolo Abeni
Quoted 14 lines · Philo Lu
net/ipv4/udp.c +1
-3@@ -3438,16 +3436,17 @@ void __init udp_table_init(struct udp_table *table, const char *name)UDP_HTABLE_SIZE_MIN , UDP_HTABLE_SIZE_MAX); - table->hash2 = table->hash + (table->mask +1 ); + table->hash2 = (void *)(table->hash + (table->mask +1 ));for (i =0 ; i <= table->mask; i++) {INIT_HLIST_HEAD (&table->hash[i].head); table->hash[i].count =0 ;spin_lock_init (&table->hash[i].lock); }for (i =0 ; i <= table->mask; i++) { -INIT_HLIST_HEAD (&table->hash2[i].head); - table->hash2[i].count =0 ;
Quoted 8 lines
net/ipv4/udp.c void __init udp_table_init(struct udp_table *table, const char *name) · line 3449
Quoted 5 lines
net/ipv4/udp.c +4
-1-spin_lock_init (&table->hash2[i].lock); +INIT_HLIST_HEAD (&table->hash2[i].hslot.head); + table->hash2[i].hslot.count =0 ; +spin_lock_init (&table->hash2[i].hslot.lock); + table->hash2[i].hash4_cnt =0 ;Does not build for
CONFIG_BASE_SMALL=ykernels. You need to put all thehash4_cntaccess under compiler guards.
I see now it's fixed in the next patch, but it's not good enough: we want to preserve bisectability. You should introduce udp_table_hash4_init() in this patch.
Thanks,
Paolo
PATCH v5 2/3 net/udp: Add 4-tuple hash list basis
In reply to Philo LuAdd a new hash list, hash4, in udp table. It will be used to implement 4-tuple hash for connected udp sockets. This patch adds the hlist to table, and implements helpers and the initialization. 4-tuple hash is implemented in the following patch.
--- include/linux/udp.h | 11 ++++++ include/net/udp.h | 87 ++++++++++++++++++++++++++++++++++++++++++++- net/ipv4/udp.c | 10 +++--- 3 files changed, 103 insertions(+), 5 deletions(-)
include/linux/udp.h +11 -0
@@ -56,6 +56,12 @@ struct udp_sock {int pending;/* Any pending frames ? */ __u8 encap_type;/* Is this an Encapsulation socket? */ +#if !IS_ENABLED (CONFIG_BASE_SMALL ) +/* For UDP 4-tuple hash */ +__u16 udp_lrpa_hash; +struct hlist_node udp_lrpa_node; +#endif +/* * Following member retains the information to create a UDP header * when the socket is uncorked.
@@ -206,6 +212,11 @@ static inline void udp_allow_gso(struct sock *sk)#define udp_portaddr_for_each_entry_rcu (__sk, list) \ hlist_for_each_entry_rcu(__sk, list, __sk_common.skc_portaddr_node) +#if !IS_ENABLED (CONFIG_BASE_SMALL ) +#define udp_lrpa_for_each_entry_rcu (__up, list) \ + hlist_for_each_entry_rcu(__up, list, udp_lrpa_node) +#endif +#define IS_UDPLITE (__sk) (__sk->sk_protocol == IPPROTO_UDPLITE)#endif /* _LINUX_UDP_H */
include/net/udp.h +86 -1
111 lines not on this page · raw message
net/ipv4/udp.c +6 -4
@@ -3425,7 +3425,8 @@ void __init udp_table_init(struct udp_table *table, const char *name) {unsigned int i, slot_size; - slot_size =sizeof (struct udp_hslot ) +sizeof (struct udp_hslot_main ); + slot_size =sizeof (struct udp_hslot ) +sizeof (struct udp_hslot_main ) + +udp_hash4_slot_size (); table->hash = alloc_large_system_hash(name, slot_size, uhash_entries,
@@ -3446,8 +3447,8 @@ void __init udp_table_init(struct udp_table *table, const char *name)INIT_HLIST_HEAD (&table->hash2[i].hslot.head); table->hash2[i].hslot.count =0 ;spin_lock_init (&table->hash2[i].hslot.lock); - table->hash2[i].hash4_cnt =0 ; } +udp_table_hash4_init (table); }u32 udp_flow_hashrnd (void )
@@ -3480,7 +3481,8 @@ static struct udp_table __net_init *udp_pernet_table_alloc(unsigned int hash_entif (!udptable) goto out; - slot_size =sizeof (struct udp_hslot ) +sizeof (struct udp_hslot_main ); + slot_size =sizeof (struct udp_hslot ) +sizeof (struct udp_hslot_main ) + +udp_hash4_slot_size (); udptable->hash =vmalloc_huge (hash_entries * slot_size,GFP_KERNEL_ACCOUNT );if (!udptable->hash)
@@ -3498,8 +3500,8 @@ static struct udp_table __net_init *udp_pernet_table_alloc(unsigned int hash_entINIT_HLIST_HEAD (&udptable->hash2[i].hslot.head); udptable->hash2[i].hslot.count =0 ;spin_lock_init (&udptable->hash2[i].hslot.lock); - udptable->hash2[i].hash4_cnt =0 ; } +udp_table_hash4_init (udptable);return udptable;
Signature · 1 line
2.32.0.3.g01195cf9f
PATCH v5 3/3 ipv4/udp: Add 4-tuple hash for connected socket
In reply to Philo LuCurrently, the udp_table has two hash table, the port hash and portaddr hash. Usually for UDP servers, all sockets have the same local port and addr, so they are all on the same hash slot within a reuseport group.
In some applications, UDP servers use connect() to manage clients. In particular, when firstly receiving from an unseen 4 tuple, a new socket is created and connect()ed to the remote addr:port, and then the fd is used exclusively by the client.
Once there are connected sks in a reuseport group, udp has to score all sks in the same hash2 slot to find the best match. This could be inefficient with a large number of connections, resulting in high softirq overhead.
To solve the problem, this patch implement 4-tuple hash for connected udp sockets. During connect(), hash4 slot is updated, as well as a corresponding counter, hash4_cnt, in hslot2. In __udp4_lib_lookup(), hslot4 will be searched firstly if the counter is non-zero. Otherwise, hslot2 is used like before. Note that only connected sockets enter this hash4 path, while un-connected ones are not affected.
--- include/net/udp.h | 3 +- net/ipv4/udp.c | 171 +++++++++++++++++++++++++++++++++++++++++++++- net/ipv6/udp.c | 2 +- 3 files changed, 171 insertions(+), 5 deletions(-)
include/net/udp.h +2 -1
@@ -293,7 +293,7 @@ static inline int udp_lib_hash(struct sock *sk) }void udp_lib_unhash (struct sock *sk); -void udp_lib_rehash (struct sock *sk,u16 new_hash); +void udp_lib_rehash (struct sock *sk,u16 new_hash,u16 new_hash4);static inline void udp_lib_close (struct sock *sk,long timeout) {
@@ -386,6 +386,7 @@ int udp_rcv(struct sk_buff *skb);int udp_ioctl (struct sock *sk,int cmd,int *karg);int udp_init_sock (struct sock *sk);int udp_pre_connect (struct sock *sk,struct sockaddr *uaddr,int addr_len); +int udp_connect (struct sock *sk,struct sockaddr *uaddr,int addr_len);int __udp_disconnect (struct sock *sk,int flags);int udp_disconnect (struct sock *sk,int flags);__poll_t udp_poll (struct file *file,struct socket *sock,poll_table *wait);
net/ipv4/udp.c +168 -3
219 lines not on this page · raw message
net/ipv6/udp.c +1 -1
@@ -111,7 +111,7 @@ void udp_v6_rehash(struct sock *sk) &sk->sk_v6_rcv_saddr, inet_sk(sk)->inet_num); -udp_lib_rehash (sk, new_hash); +udp_lib_rehash (sk, new_hash,0 );/* 4-tuple hash not implemented */ } static int compute_score(struct sock *sk, const struct net *net,
Signature · 1 line
2.32.0.3.g01195cf9f
Paolo Abeni
maintainerRe: [PATCH v5 net-next 3/3] ipv4/udp: Add 4-tuple hash for connected socket
On 10/18/24 13:45, Philo Lu wrote:
[...]
Quoted 6 lines
net/ipv4/udp.c +6
-0+/* In hash4, rehash can also happen in connect(), where hash4_cnt keeps unchanged. */ +static void udp4_rehash4 (struct udp_table *udptable,struct sock *sk,u16 newhash4) +{ +struct udp_hslot *hslot4, *nhslot4; + + hslot4 =udp_hashslot4 (udptable,udp_sk (sk)->udp_lrpa_hash);
net/ipv4/udp.c static struct sock *udp4_lib_lookup2(const struct net *net, · line 539
Quoted 10 lines
net/ipv4/udp.c +10
-0+ nhslot4 =udp_hashslot4 (udptable, newhash4); +udp_sk (sk)->udp_lrpa_hash = newhash4; + +if (hslot4 != nhslot4) { +spin_lock_bh (&hslot4->lock); +hlist_del_init_rcu (&udp_sk (sk)->udp_lrpa_node); + hslot4->count--; +spin_unlock_bh (&hslot4->lock); + +synchronize_rcu ();
This deserve a comment explaining why it's needed. I had to dig in past revision to understand it.
Quoted 34 lines
net/ipv4/udp.c +34
-0+ +spin_lock_bh (&nhslot4->lock); +hlist_add_head_rcu (&udp_sk (sk)->udp_lrpa_node, &nhslot4->head); + nhslot4->count++; +spin_unlock_bh (&nhslot4->lock); + } +} + +static void udp4_unhash4 (struct udp_table *udptable,struct sock *sk) +{ +struct udp_hslot *hslot2, *hslot4; + +if (udp_hashed4 (sk)) { + hslot2 =udp_hashslot2 (udptable,udp_sk (sk)->udp_portaddr_hash); + hslot4 =udp_hashslot4 (udptable,udp_sk (sk)->udp_lrpa_hash); + +spin_lock (&hslot4->lock); +hlist_del_init_rcu (&udp_sk (sk)->udp_lrpa_node); + hslot4->count--; +spin_unlock (&hslot4->lock); + +spin_lock (&hslot2->lock); +udp_hash4_dec (hslot2); +spin_unlock (&hslot2->lock); + } +} + +/* call with sock lock */ +static void udp4_hash4 (struct sock *sk) +{ +struct udp_hslot *hslot, *hslot2, *hslot4; +struct net *net =sock_net (sk); +struct udp_table *udptable; +unsigned int hash;
net/ipv4/udp.c static struct sock *udp4_lib_lookup2(const struct net *net, · line 583
Quoted 10 lines
net/ipv4/udp.c +10
-0+ +if (sk_unhashed (sk) ||inet_sk (sk)->inet_rcv_saddr ==htonl (INADDR_ANY )) +return ; + + hash =udp_ehashfn (net,inet_sk (sk)->inet_rcv_saddr,inet_sk (sk)->inet_num, +inet_sk (sk)->inet_daddr,inet_sk (sk)->inet_dport); + + udptable = net->ipv4.udp_table; +if (udp_hashed4 (sk)) { +udp4_rehash4 (udptable, sk, hash);
It's unclear to me how we can enter this branch. Also it's unclear why here you don't need to call udp_hash4_inc()udp_hash4_dec, too. Why such accounting can't be placed in udp4_rehash4()?
[...]
Quoted 8 lines
net/ipv4/udp.c +4
-0@@ -2031,6 +2180,19 @@ void udp_lib_rehash(struct sock *sk, u16 newhash)spin_unlock (&nhslot2->lock); } +if (udp_hashed4 (sk)) { +udp4_rehash4 (udptable, sk, newhash4); + +if (hslot2 != nhslot2) {
net/ipv4/udp.c void udp_lib_rehash(struct sock *sk, u16 newhash) · line 2196
Quoted 10 lines
net/ipv4/udp.c +9
-0+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);
The udp4_rehash4() call above is in atomic context and could end-up calling synchronize_rcu() which is a blocking function. You must avoid that.
Cheers,
Paolo
Paolo Abeni
maintainerRe: [PATCH v5 net-next 3/3] ipv4/udp: Add 4-tuple hash for connected socket
Quoted 2 lines · Paolo Abeni
The
udp4_rehash4()call above is in atomic context and could end-up callingsynchronize_rcu()which is a blocking function. You must avoid that.
I almost forgot: please include in this commit message or in the cover letter, the performance figures for unconnected sockets before and after this series and for a stress test with a lot of connected sockets, before and after this series.
Thanks,
Paolo
Re: [PATCH v5 net-next 3/3] ipv4/udp: Add 4-tuple hash for connected socket
In reply to Paolo AbeniQuoted 13 lines · Paolo Abeni
On 10/18/24 13:45, Philo Lu wrote:
[...]Quoted 11 lines
net/ipv4/udp.c +11
-0+/* In hash4, rehash can also happen in connect(), where hash4_cnt keeps unchanged. */ +static void udp4_rehash4 (struct udp_table *udptable,struct sock *sk,u16 newhash4) +{ +struct udp_hslot *hslot4, *nhslot4; + + hslot4 =udp_hashslot4 (udptable,udp_sk (sk)->udp_lrpa_hash); + nhslot4 =udp_hashslot4 (udptable, newhash4); +udp_sk (sk)->udp_lrpa_hash = newhash4; + +if (hslot4 != nhslot4) { +spin_lock_bh (&hslot4->lock);
Quoted 9 lines
net/ipv4/udp.c static struct sock *udp4_lib_lookup2(const struct net *net, · line 539
Quoted 5 lines
net/ipv4/udp.c +5
-0+hlist_del_init_rcu (&udp_sk (sk)->udp_lrpa_node); + hslot4->count--; +spin_unlock_bh (&hslot4->lock); + +synchronize_rcu ();This deserve a comment explaining why it's needed. I had to dig in past revision to understand it.
Got it. And a short explanation here (see [1] for detail):
Here, we move a node from a hlist to another new one, i.e., update node->next from the old hlist to the new hlist. For readers traversing the old hlist, if we update node->next just when readers move onto the moved node, then the readers also move to the new hlist. This is unexpected.
Reader(lookup) Writer(rehash)
----------------- ---------------1. rcu_read_lock()
2. pos = sk;
3. hlist_del_init_rcu(sk, old_slot)
4. hlist_add_head_rcu(sk, new_slot)
5. pos = pos->next; <=
6. rcu_read_unlock()
[1]
https://lore.kernel.org/all/[email protected]/
Quoted 40 lines
Quoted 40 lines
net/ipv4/udp.c +40
-0+ +spin_lock_bh (&nhslot4->lock); +hlist_add_head_rcu (&udp_sk (sk)->udp_lrpa_node, &nhslot4->head); + nhslot4->count++; +spin_unlock_bh (&nhslot4->lock); + } +} + +static void udp4_unhash4 (struct udp_table *udptable,struct sock *sk) +{ +struct udp_hslot *hslot2, *hslot4; + +if (udp_hashed4 (sk)) { + hslot2 =udp_hashslot2 (udptable,udp_sk (sk)->udp_portaddr_hash); + hslot4 =udp_hashslot4 (udptable,udp_sk (sk)->udp_lrpa_hash); + +spin_lock (&hslot4->lock); +hlist_del_init_rcu (&udp_sk (sk)->udp_lrpa_node); + hslot4->count--; +spin_unlock (&hslot4->lock); + +spin_lock (&hslot2->lock); +udp_hash4_dec (hslot2); +spin_unlock (&hslot2->lock); + } +} + +/* call with sock lock */ +static void udp4_hash4 (struct sock *sk) +{ +struct udp_hslot *hslot, *hslot2, *hslot4; +struct net *net =sock_net (sk); +struct udp_table *udptable; +unsigned int hash; + +if (sk_unhashed (sk) ||inet_sk (sk)->inet_rcv_saddr ==htonl (INADDR_ANY )) +return ; + + hash =udp_ehashfn (net,inet_sk (sk)->inet_rcv_saddr,inet_sk (sk)->inet_num, +inet_sk (sk)->inet_daddr,inet_sk (sk)->inet_dport);
Quoted 9 lines
net/ipv4/udp.c static struct sock *udp4_lib_lookup2(const struct net *net, · line 583
Quoted 4 lines
net/ipv4/udp.c +4
-0+ + udptable = net->ipv4.udp_table; +if (udp_hashed4 (sk)) { +udp4_rehash4 (udptable, sk, hash);It's unclear to me how we can enter this branch. Also it's unclear why here you don't need to call udp_hash4_inc()udp_hash4_dec, too. Why such accounting can't be placed in
udp4_rehash4()?
It's possible that a connected udp socket _re-connect_ to another remote address. Then, because the local address is not changed, hash2 and its hash4_cnt keep unchanged. But rehash4 need to be done.
I'll also add a comment here.
Quoted 14 lines
[...]
Quoted 13 lines
net/ipv4/udp.c +9
-0@@ -2031,6 +2180,19 @@ void udp_lib_rehash(struct sock *sk, u16 newhash)spin_unlock (&nhslot2->lock); } +if (udp_hashed4 (sk)) { +udp4_rehash4 (udptable, sk, newhash4); + +if (hslot2 != nhslot2) { +spin_lock (&hslot2->lock); +udp_hash4_dec (hslot2); +spin_unlock (&hslot2->lock); + +spin_lock (&nhslot2->lock);
Quoted 9 lines
net/ipv4/udp.c void udp_lib_rehash(struct sock *sk, u16 newhash) · line 2196
Quoted 5 lines
net/ipv4/udp.c +4
-0+udp_hash4_inc (nhslot2); +spin_unlock (&nhslot2->lock); + } + }spin_unlock_bh (&hslot->lock);The
udp4_rehash4()call above is in atomic context and could end-up callingsynchronize_rcu()which is a blocking function. You must avoid that.
I see, synchronize_rcu() cannot be used with spinlock. However, I don't have a good idea to solve it by now. Do you have any thoughts or suggestions?
Quoted 3 lines
Cheers,
Paolo
Thanks for your reviewing, Paolo. I'll address all your concerns in the next version.
Signature · 1 line
Philo
Paolo Abeni
maintainerRe: [PATCH v5 net-next 3/3] ipv4/udp: Add 4-tuple hash for connected socket
Quoted 40 lines · Philo Lu
Quoted 13 lines · Paolo Abeni
On 10/18/24 13:45, Philo Lu wrote:
[...]Quoted 11 lines
net/ipv4/udp.c +11
-0+/* In hash4, rehash can also happen in connect(), where hash4_cnt keeps unchanged. */ +static void udp4_rehash4 (struct udp_table *udptable,struct sock *sk,u16 newhash4) +{ +struct udp_hslot *hslot4, *nhslot4; + + hslot4 =udp_hashslot4 (udptable,udp_sk (sk)->udp_lrpa_hash); + nhslot4 =udp_hashslot4 (udptable, newhash4); +udp_sk (sk)->udp_lrpa_hash = newhash4; + +if (hslot4 != nhslot4) { +spin_lock_bh (&hslot4->lock);Quoted 9 lines
net/ipv4/udp.c static struct sock *udp4_lib_lookup2(const struct net *net, · line 539
Quoted 5 lines
net/ipv4/udp.c +5
-0+hlist_del_init_rcu (&udp_sk (sk)->udp_lrpa_node); + hslot4->count--; +spin_unlock_bh (&hslot4->lock); + +synchronize_rcu ();This deserve a comment explaining why it's needed. I had to dig in past revision to understand it.
Got it. And a short explanation here (see [1] for detail):
Here, we move a node from a hlist to another new one, i.e., update
node->nextfrom the old hlist to the new hlist. For readers traversing the old hlist, if we updatenode->nextjust when readers move onto the moved node, then the readers also move to the new hlist. This is unexpected.Reader(lookup) Writer(rehash) ----------------- ---------------1.
rcu_read_lock()
2. pos = sk;
3.hlist_del_init_rcu(sk, old_slot)
4.hlist_add_head_rcu(sk, new_slot)
5. pos =pos->next; <=
6.rcu_read_unlock()
Quoted 2 lines
Thanks. AFAICS the problem that such thing could cause is a lookup failure for a socket positioned later in the same chain when a previous entry is moved on a different slot during a concurrent lookup.
I think that could be solved the same way TCP is handling such scenario:
using hlist_null RCU list for the hash4 bucket, checking that a failed lookup ends in the same bucket where it started and eventually reiterating from the original bucket.
Have a look at __inet_lookup_established() for a more descriptive reference, especially:
https://elixir.bootlin.com/linux/v6.12-rc4/source/net/ipv4/inet_hashtables.c#L528
Quoted 50 lines
Quoted 40 lines
Quoted 40 lines
net/ipv4/udp.c +40
-0+ +spin_lock_bh (&nhslot4->lock); +hlist_add_head_rcu (&udp_sk (sk)->udp_lrpa_node, &nhslot4->head); + nhslot4->count++; +spin_unlock_bh (&nhslot4->lock); + } +} + +static void udp4_unhash4 (struct udp_table *udptable,struct sock *sk) +{ +struct udp_hslot *hslot2, *hslot4; + +if (udp_hashed4 (sk)) { + hslot2 =udp_hashslot2 (udptable,udp_sk (sk)->udp_portaddr_hash); + hslot4 =udp_hashslot4 (udptable,udp_sk (sk)->udp_lrpa_hash); + +spin_lock (&hslot4->lock); +hlist_del_init_rcu (&udp_sk (sk)->udp_lrpa_node); + hslot4->count--; +spin_unlock (&hslot4->lock); + +spin_lock (&hslot2->lock); +udp_hash4_dec (hslot2); +spin_unlock (&hslot2->lock); + } +} + +/* call with sock lock */ +static void udp4_hash4 (struct sock *sk) +{ +struct udp_hslot *hslot, *hslot2, *hslot4; +struct net *net =sock_net (sk); +struct udp_table *udptable; +unsigned int hash; + +if (sk_unhashed (sk) ||inet_sk (sk)->inet_rcv_saddr ==htonl (INADDR_ANY )) +return ; + + hash =udp_ehashfn (net,inet_sk (sk)->inet_rcv_saddr,inet_sk (sk)->inet_num, +inet_sk (sk)->inet_daddr,inet_sk (sk)->inet_dport);Quoted 9 lines
net/ipv4/udp.c static struct sock *udp4_lib_lookup2(const struct net *net, · line 583
Quoted 4 lines
net/ipv4/udp.c +4
-0+ + udptable = net->ipv4.udp_table; +if (udp_hashed4 (sk)) { +udp4_rehash4 (udptable, sk, hash);It's unclear to me how we can enter this branch. Also it's unclear why here you don't need to call udp_hash4_inc()udp_hash4_dec, too. Why such accounting can't be placed in
udp4_rehash4()?
Quoted 4 lines
It's possible that a connected udp socket _re-connect_ to another remote address. Then, because the local address is not changed, hash2 and its
hash4_cntkeep unchanged. But rehash4 need to be done.
I'll also add a comment here.
Right, UDP socket could actually connect() successfully twice in a row without a disconnect in between...
I almost missed the point that the ipv6 implementation is planned to land afterwards.
I'm sorry, but I think that would be problematic - i.e. if ipv4 support will land in 6.13, but ipv6 will not make it - due to time constraints - we will have (at least a release with inconsistent behavior between ipv4 and ipv6. I think it will be better bundle such changes together.
Thanks,
Paolo
Re: [PATCH v5 net-next 3/3] ipv4/udp: Add 4-tuple hash for connected socket
Quoted 41 lines · Paolo Abeni
Quoted 40 lines · Philo Lu
Quoted 13 lines · Paolo Abeni
On 10/18/24 13:45, Philo Lu wrote:
[...]Quoted 11 lines
net/ipv4/udp.c +11
-0+/* In hash4, rehash can also happen in connect(), where hash4_cnt keeps unchanged. */ +static void udp4_rehash4 (struct udp_table *udptable,struct sock *sk,u16 newhash4) +{ +struct udp_hslot *hslot4, *nhslot4; + + hslot4 =udp_hashslot4 (udptable,udp_sk (sk)->udp_lrpa_hash); + nhslot4 =udp_hashslot4 (udptable, newhash4); +udp_sk (sk)->udp_lrpa_hash = newhash4; + +if (hslot4 != nhslot4) { +spin_lock_bh (&hslot4->lock);Quoted 9 lines
net/ipv4/udp.c static struct sock *udp4_lib_lookup2(const struct net *net, · line 539
Quoted 5 lines
net/ipv4/udp.c +5
-0+hlist_del_init_rcu (&udp_sk (sk)->udp_lrpa_node); + hslot4->count--; +spin_unlock_bh (&hslot4->lock); + +synchronize_rcu ();This deserve a comment explaining why it's needed. I had to dig in past revision to understand it.
Got it. And a short explanation here (see [1] for detail):
Here, we move a node from a hlist to another new one, i.e., update
node->nextfrom the old hlist to the new hlist. For readers traversing the old hlist, if we updatenode->nextjust when readers move onto the moved node, then the readers also move to the new hlist. This is unexpected.Reader(lookup) Writer(rehash) ----------------- ---------------1.
rcu_read_lock()
2. pos = sk;
3.hlist_del_init_rcu(sk, old_slot)
4.hlist_add_head_rcu(sk, new_slot)
5. pos =pos->next; <=
6.rcu_read_unlock()
Quoted 7 lines
Quoted 2 lines
Thanks. AFAICS the problem that such thing could cause is a lookup failure for a socket positioned later in the same chain when a previous entry is moved on a different slot during a concurrent lookup.
Yes, you're right.
Quoted 10 lines
I think that could be solved the same way TCP is handling such scenario:
usinghlist_nullRCU list for the hash4 bucket, checking that a failed lookup ends in the same bucket where it started and eventually reiterating from the original bucket.Have a look at
__inet_lookup_established()for a more descriptive reference, especially:https://elixir.bootlin.com/linux/v6.12-rc4/source/net/ipv4/inet_hashtables.c#L528
Thank you! I'll try it in the next version.
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
+
...
Quoted 32 lines
Quoted 24 lines
Quoted 14 lines
Quoted 14 lines
net/ipv4/udp.c +14
-0+ +/* call with sock lock */ +static void udp4_hash4 (struct sock *sk) +{ +struct udp_hslot *hslot, *hslot2, *hslot4; +struct net *net =sock_net (sk); +struct udp_table *udptable; +unsigned int hash; + +if (sk_unhashed (sk) ||inet_sk (sk)->inet_rcv_saddr ==htonl (INADDR_ANY )) +return ; + + hash =udp_ehashfn (net,inet_sk (sk)->inet_rcv_saddr,inet_sk (sk)->inet_num, +inet_sk (sk)->inet_daddr,inet_sk (sk)->inet_dport);Quoted 9 lines
net/ipv4/udp.c static struct sock *udp4_lib_lookup2(const struct net *net, · line 583
Quoted 4 lines
net/ipv4/udp.c +4
-0+ + udptable = net->ipv4.udp_table; +if (udp_hashed4 (sk)) { +udp4_rehash4 (udptable, sk, hash);It's unclear to me how we can enter this branch. Also it's unclear why here you don't need to call udp_hash4_inc()udp_hash4_dec, too. Why such accounting can't be placed in
udp4_rehash4()?Quoted 4 lines
It's possible that a connected udp socket _re-connect_ to another remote address. Then, because the local address is not changed, hash2 and its
hash4_cntkeep unchanged. But rehash4 need to be done.
I'll also add a comment here.Right, UDP socket could actually
connect()successfully twice in a row without a disconnect in between...
Quoted 8 lines
I almost missed the point that the ipv6 implementation is planned to land afterwards.
I'm sorry, but I think that would be problematic - i.e. if ipv4 support will land in 6.13, but ipv6 will not make it - due to time constraints - we will have (at least a release with inconsistent behavior between ipv4 and ipv6. I think it will be better bundle such changes together.
No problem. I can add ipv6 support in the next version too.
Thanks.
Signature · 1 line
Philo