udp: remove busylock and add per NUMA queues
Subsystems (3)
PATCH v4 1/1 udp: remove busylock and add per NUMA queues
busylock was protecting UDP sockets against packet floods, but unfortunately was not protecting the host itself.
Under stress, many cpus could spin while acquiring the busylock, and NIC had to drop packets. Or packets would be dropped in cpu backlog if RPS/RFS were in place.
This patch replaces the busylock by intermediate
lockless queues. (One queue per NUMA node).
This means that fewer number of cpus have to acquire the UDP receive queue lock.
Most of the cpus can either:
- immediately drop the packet.
- or queue it in their NUMA aware lockless queue.
Then one of the cpu is chosen to process this lockless queue in a batch.
The batch only contains packets that were cooked on the same NUMA node, thus with very limited latency impact.
Tested:
DDOS targeting a victim UDP socket, on a platform with 6 NUMA nodes (Intel(R) Xeon(R) 6985P-C)
Before:
nstat -n ; sleep 1 ; nstat | grep Udp
Udp6InDatagrams 1004179 0.0 Udp6InErrors 3117 0.0 Udp6RcvbufErrors 3117 0.0
After:
nstat -n ; sleep 1 ; nstat | grep Udp
Udp6InDatagrams 1116633 0.0 Udp6InErrors 14197275 0.0 Udp6RcvbufErrors 14197275 0.0
We can see this host can now proces 14.2 M more packets per second while under attack, and the victim socket can receive 11 % more packets.
I used a small bpftrace program measuring time (in us) spent in __udp_enqueue_schedule_skb().
Before:
@udp_enqueue_us[398]:
[0] 24901 |@@@ | [1] 63512 |@@@@@@@@@ | [2, 4) 344827 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [4, 8) 244673 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [8, 16) 54022 |@@@@@@@@ | [16, 32) 222134 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [32, 64) 232042 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [64, 128) 4219 | | [128, 256) 188 | |
After:
@udp_enqueue_us[398]:
[0] 5608855 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [1] 1111277 |@@@@@@@@@@ | [2, 4) 501439 |@@@@ | [4, 8) 102921 | | [8, 16) 29895 | | [16, 32) 43500 | | [32, 64) 31552 | | [64, 128) 979 | | [128, 256) 13 | |
Note that the remaining bottleneck for this platform is in udp_drops_inc() because we limited struct numa_drop_counters to only two nodes so far.
---
v4: cache sk_drops_read(), as suggested by Willem.
This will optimize SOCK_RXQ_OVFL users.
v3: https://lore.kernel.org/netdev/[email protected]/
- Moved kfree(up->udp_prod_queue) to udp_destruct_common(), addressing reports from Jakub and syzbot.
- Perform SKB_DROP_REASON_PROTO_MEM drops after the queue spinlock is released.
v2: https://lore.kernel.org/netdev/[email protected]/
- Added a kfree(up->udp_prod_queue) in udpv6_destroy_sock() (Jakub feedback on v1)
- Added bpftrace histograms in changelog.
v1: https://lore.kernel.org/netdev/[email protected]/
include/linux/udp.h | 9 +++- include/net/udp.h | 11 ++++- net/ipv4/udp.c | 117 +++++++++++++++++++++++++++----------------- net/ipv6/udp.c | 5 +- 4 files changed, 91 insertions(+), 51 deletions(-)
include/linux/udp.h +8 -1
@@ -44,6 +44,12 @@ enum { UDP_FLAGS_UDPLITE_RECV_CC, /* set via udplite setsockopt */ }; +/* per NUMA structure for lockless producer usage. */ +struct udp_prod_queue { +struct llist_head ll_root____cacheline_aligned_in_smp ; +atomic_t rmem_alloc; +}; +struct udp_sock {/* inet_sock has to be the first member */ struct inet_sock inet;
@@ -90,6 +96,8 @@ struct udp_sock {struct sk_buff *skb, int nhoff); +struct udp_prod_queue *udp_prod_queue; +/* udp_recvmsg try to use this before splicing sk_receive_queue */ struct sk_buff_head reader_queue____cacheline_aligned_in_smp ;
@@ -109,7 +117,6 @@ struct udp_sock {*/ struct hlist_node tunnel_list;struct numa_drop_counters drop_counters; -spinlock_t busylock____cacheline_aligned_in_smp ; };#define udp_test_bit (nr, sk) \
include/net/udp.h +9 -2
@@ -284,16 +284,23 @@ INDIRECT_CALLABLE_DECLARE(int udpv6_rcv(struct sk_buff *));struct sk_buff *__udp_gso_segment (struct sk_buff *gso_skb,netdev_features_t features,bool is_ipv6); -static inline void udp_lib_init_sock (struct sock *sk) +static inline int udp_lib_init_sock (struct sock *sk) {struct udp_sock *up =udp_sk (sk); sk->sk_drop_counters = &up->drop_counters; -spin_lock_init (&up->busylock);skb_queue_head_init (&up->reader_queue);INIT_HLIST_NODE (&up->tunnel_list); up->forward_threshold = sk->sk_rcvbuf >>2 ;set_bit (SOCK_CUSTOM_SOCKOPT , &sk->sk_socket->flags); + + up->udp_prod_queue =kcalloc (nr_node_ids,sizeof (*up->udp_prod_queue), +GFP_KERNEL ); +if (!up->udp_prod_queue) +return -ENOMEM ; +for (int i =0 ; i < nr_node_ids; i++) +init_llist_head (&up->udp_prod_queue[i].ll_root); +return 0 ; }static inline void udp_drops_inc (struct sock *sk)
net/ipv4/udp.c +71 -46
171 lines not on this page · raw message
net/ipv6/udp.c +3 -2
@@ -67,10 +67,11 @@ static void udpv6_destruct_sock(struct sock *sk)int udpv6_init_sock (struct sock *sk) { -udp_lib_init_sock (sk); +int res =udp_lib_init_sock (sk); + sk->sk_destruct = udpv6_destruct_sock;set_bit (SOCK_SUPPORT_ZC , &sk->sk_socket->flags); -return 0 ; +return res; }INDIRECT_CALLABLE_SCOPE
Signature · 1 line
2.51.0.470.ga7dc726c21-goog
Quoted 74 lines · Eric Dumazet
busylock was protecting UDP sockets against packet floods, but unfortunately was not protecting the host itself.
Under stress, many cpus could spin while acquiring the busylock, and NIC had to drop packets. Or packets would be dropped in cpu backlog if RPS/RFS were in place.
This patch replaces the busylock by intermediate
lockless queues. (One queue per NUMA node).This means that fewer number of cpus have to acquire the UDP receive queue lock.
Most of the cpus can either:
- immediately drop the packet.
- or queue it in their NUMA aware lockless queue.Then one of the cpu is chosen to process this lockless queue in a batch.
The batch only contains packets that were cooked on the same NUMA node, thus with very limited latency impact.
Tested:
DDOS targeting a victim UDP socket, on a platform with 6 NUMA nodes (Intel(R) Xeon(R) 6985P-C)
Before:
nstat -n ; sleep 1 ; nstat | grep Udp
Udp6InDatagrams 1004179 0.0 Udp6InErrors 3117 0.0 Udp6RcvbufErrors 3117 0.0After:
nstat -n ; sleep 1 ; nstat | grep UdpUdp6InDatagrams 1116633 0.0 Udp6InErrors 14197275 0.0 Udp6RcvbufErrors 14197275 0.0We can see this host can now proces 14.2 M more packets per second while under attack, and the victim socket can receive 11 % more packets.
I used a small bpftrace program measuring time (in us) spent in
__udp_enqueue_schedule_skb().Before:
@udp_enqueue_us[398]:
[0] 24901 |@@@ | [1] 63512 |@@@@@@@@@ | [2, 4) 344827 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [4, 8) 244673 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [8, 16) 54022 |@@@@@@@@ | [16, 32) 222134 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [32, 64) 232042 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [64, 128) 4219 | | [128, 256) 188 | |After:
@udp_enqueue_us[398]:
[0] 5608855 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [1] 1111277 |@@@@@@@@@@ | [2, 4) 501439 |@@@@ | [4, 8) 102921 | | [8, 16) 29895 | | [16, 32) 43500 | | [32, 64) 31552 | | [64, 128) 979 | | [128, 256) 13 | |
Quoted 6 lines
Note that the remaining bottleneck for this platform is in
udp_drops_inc()because we limited structnuma_drop_countersto only two nodes so far.Signed-off-by Eric Dumazet <[email protected]>Acked-by Paolo Abeni <[email protected]>
Quoted 75 lines · Eric Dumazet
busylock was protecting UDP sockets against packet floods, but unfortunately was not protecting the host itself.
Under stress, many cpus could spin while acquiring the busylock, and NIC had to drop packets. Or packets would be dropped in cpu backlog if RPS/RFS were in place.
This patch replaces the busylock by intermediate
lockless queues. (One queue per NUMA node).This means that fewer number of cpus have to acquire the UDP receive queue lock.
Most of the cpus can either:
- immediately drop the packet.
- or queue it in their NUMA aware lockless queue.Then one of the cpu is chosen to process this lockless queue in a batch.
The batch only contains packets that were cooked on the same NUMA node, thus with very limited latency impact.
Tested:
DDOS targeting a victim UDP socket, on a platform with 6 NUMA nodes (Intel(R) Xeon(R) 6985P-C)
Before:
nstat -n ; sleep 1 ; nstat | grep Udp
Udp6InDatagrams 1004179 0.0 Udp6InErrors 3117 0.0 Udp6RcvbufErrors 3117 0.0After:
nstat -n ; sleep 1 ; nstat | grep UdpUdp6InDatagrams 1116633 0.0 Udp6InErrors 14197275 0.0 Udp6RcvbufErrors 14197275 0.0We can see this host can now proces 14.2 M more packets per second while under attack, and the victim socket can receive 11 % more packets.
I used a small bpftrace program measuring time (in us) spent in
__udp_enqueue_schedule_skb().Before:
@udp_enqueue_us[398]:
[0] 24901 |@@@ | [1] 63512 |@@@@@@@@@ | [2, 4) 344827 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [4, 8) 244673 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [8, 16) 54022 |@@@@@@@@ | [16, 32) 222134 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [32, 64) 232042 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ | [64, 128) 4219 | | [128, 256) 188 | |After:
@udp_enqueue_us[398]:
[0] 5608855 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@| [1] 1111277 |@@@@@@@@@@ | [2, 4) 501439 |@@@@ | [4, 8) 102921 | | [8, 16) 29895 | | [16, 32) 43500 | | [32, 64) 31552 | | [64, 128) 979 | | [128, 256) 13 | |
Quoted 6 lines
Note that the remaining bottleneck for this platform is in
udp_drops_inc()because we limited structnuma_drop_countersto only two nodes so far.Signed-off-by Eric Dumazet <[email protected]>Acked-by Paolo Abeni <[email protected]>
Thanks!
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 8 lines · On Mon, 22 Sep 2025 10:42:40 +0000 you wrote:
busylock was protecting UDP sockets against packet floods, but unfortunately was not protecting the host itself.
Under stress, many cpus could spin while acquiring the busylock, and NIC had to drop packets. Or packets would be dropped in cpu backlog if RPS/RFS were in place.
[...]
Here is the summary with links:
- [v4,net-next] udp: remove busylock and add per NUMA queues
https://git.kernel.org/netdev/net-next/c/b650bf0977d3
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html