udp: fix busy polling
Subsystems (4)
PATCH v3 1/1 udp: fix busy polling
Generic sk_busy_loop_end() only looks at sk->sk_receive_queue for presence of packets.
Problem is that for UDP sockets after blamed commit, some packets could be present in another queue: udp_sk(sk)->reader_queue
In some cases, a busy poller could spin until timeout expiration, even if some packets are available in udp_sk(sk)->reader_queue.
v3: - make sk_busy_loop_end() nicer (Willem)
v2: - add a READ_ONCE(sk->sk_family) in sk_is_inet() to avoid KCSAN splats.
- add a sk_is_inet() check in sk_is_udp() (Willem feedback)
- add a sk_is_inet() check in sk_is_tcp().
--- include/linux/skmsg.h | 6 ------ include/net/inet_sock.h | 5 ----- include/net/sock.h | 18 +++++++++++++++++- net/core/sock.c | 11 +++++++++-- 4 files changed, 26 insertions(+), 14 deletions(-)
include/linux/skmsg.h +0 -6
@@ -505,12 +505,6 @@ static inline bool sk_psock_strp_enabled(struct sk_psock *psock)return !!psock->saved_data_ready; } -static inline bool sk_is_udp (const struct sock *sk) -{ -return sk->sk_type ==SOCK_DGRAM && - sk->sk_protocol ==IPPROTO_UDP ; -} -#if IS_ENABLED (CONFIG_NET_SOCK_MSG )#define BPF_F_STRPARSER (1UL << 1)
include/net/inet_sock.h +0 -5
@@ -307,11 +307,6 @@ static inline unsigned long inet_cmsg_flags(const struct inet_sock *inet)#define inet_assign_bit (nr, sk, val) \ assign_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags, val) -static inline bool sk_is_inet (struct sock *sk) -{ -return sk->sk_family ==AF_INET || sk->sk_family ==AF_INET6 ; -} -/** * sk_to_full_sk - Access to a full socket * @sk: pointer to a socket
include/net/sock.h +17 -1
@@ -2765,9 +2765,25 @@ static inline void skb_setup_tx_timestamp(struct sk_buff *skb, __u16 tsflags) &skb_shinfo(skb)->tskey); } +static inline bool sk_is_inet (const struct sock *sk) +{ +int family =READ_ONCE (sk->sk_family); + +return family ==AF_INET || family ==AF_INET6 ; +} +static inline bool sk_is_tcp (const struct sock *sk) { -return sk->sk_type ==SOCK_STREAM && sk->sk_protocol ==IPPROTO_TCP ; +return sk_is_inet (sk) && + sk->sk_type ==SOCK_STREAM && + sk->sk_protocol ==IPPROTO_TCP ; +} + +static inline bool sk_is_udp (const struct sock *sk) +{ +return sk_is_inet (sk) && + sk->sk_type ==SOCK_DGRAM && + sk->sk_protocol ==IPPROTO_UDP ; }static inline bool sk_is_stream_unix (const struct sock *sk)
net/core/sock.c +9 -2
@@ -107,6 +107,7 @@#include <linux/interrupt.h> #include <linux/poll.h> #include <linux/tcp.h> +#include <linux/udp.h> #include <linux/init.h> #include <linux/highmem.h> #include <linux/user_namespace.h>
@@ -4144,8 +4145,14 @@ bool sk_busy_loop_end(void *p, unsigned long start_time) {struct sock *sk = p; -return !skb_queue_empty_lockless (&sk->sk_receive_queue) || -sk_busy_loop_timeout (sk, start_time); +if (!skb_queue_empty_lockless (&sk->sk_receive_queue)) +return true; + +if (sk_is_udp (sk) && + !skb_queue_empty_lockless (&udp_sk (sk)->reader_queue)) +return true; + +return sk_busy_loop_timeout (sk, start_time); }EXPORT_SYMBOL (sk_busy_loop_end);#endif /* CONFIG_NET_RX_BUSY_POLL */
Signature · 1 line
2.43.0.429.g432eaa2c6b-goog
From: Eric Dumazet <[email protected]>
Date: Thu, 18 Jan 2024 20:17:49 +0000
Quoted 11 lines
Generic
sk_busy_loop_end()only looks atsk->sk_receive_queuefor presence of packets.Problem is that for UDP sockets after blamed commit, some packets could be present in another queue: udp_sk(sk)->reader_queue
In some cases, a busy poller could spin until timeout expiration, even if some packets are available in udp_sk(sk)->reader_queue.
v3: - make
sk_busy_loop_end()nicer (Willem)
Quoted 8 lines
v2: - add a
READ_ONCE(sk->sk_family)insk_is_inet()to avoid KCSAN splats.
- add ask_is_inet()check insk_is_udp()(Willem feedback)
- add ask_is_inet()check insk_is_tcp().Fixes 2276f58ac589 ("udp: use a separate rx queue for packet reception")Signed-off-by Eric Dumazet <[email protected]>Reviewed-by Paolo Abeni <[email protected]>Reviewed-by Willem de Bruijn <[email protected]>
IPPROTO_UDPLITE will have the same issue but not worth adding sk_is_udp_or_udplite() for a single place given no one has complained about duplication notice.
Quoted 103 lines
--- include/linux/skmsg.h | 6 ------ include/net/inet_sock.h | 5 ----- include/net/sock.h | 18 +++++++++++++++++- net/core/sock.c | 11 +++++++++-- 4 files changed, 26 insertions(+), 14 deletions(-)include/linux/skmsg.h +0
-6@@ -505,12 +505,6 @@ static inline bool sk_psock_strp_enabled(struct sk_psock *psock)return !!psock->saved_data_ready; } -static inline bool sk_is_udp (const struct sock *sk) -{ -return sk->sk_type ==SOCK_DGRAM && - sk->sk_protocol ==IPPROTO_UDP ; -} -#if IS_ENABLED (CONFIG_NET_SOCK_MSG )#define BPF_F_STRPARSER (1UL << 1)include/net/inet_sock.h +0
-5@@ -307,11 +307,6 @@ static inline unsigned long inet_cmsg_flags(const struct inet_sock *inet)#define inet_assign_bit (nr, sk, val) \ assign_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags, val) -static inline bool sk_is_inet (struct sock *sk) -{ -return sk->sk_family ==AF_INET || sk->sk_family ==AF_INET6 ; -} -/** * sk_to_full_sk - Access to a full socket * @sk: pointer to a socket include/net/sock.h +17
-1@@ -2765,9 +2765,25 @@ static inline void skb_setup_tx_timestamp(struct sk_buff *skb, __u16 tsflags) &skb_shinfo(skb)->tskey); } +static inline bool sk_is_inet (const struct sock *sk) +{ +int family =READ_ONCE (sk->sk_family); + +return family ==AF_INET || family ==AF_INET6 ; +} +static inline bool sk_is_tcp (const struct sock *sk) { -return sk->sk_type ==SOCK_STREAM && sk->sk_protocol ==IPPROTO_TCP ; +return sk_is_inet (sk) && + sk->sk_type ==SOCK_STREAM && + sk->sk_protocol ==IPPROTO_TCP ; +} + +static inline bool sk_is_udp (const struct sock *sk) +{ +return sk_is_inet (sk) && + sk->sk_type ==SOCK_DGRAM && + sk->sk_protocol ==IPPROTO_UDP ; }static inline bool sk_is_stream_unix (const struct sock *sk)net/core/sock.c +9
-2@@ -107,6 +107,7 @@#include <linux/interrupt.h> #include <linux/poll.h> #include <linux/tcp.h> +#include <linux/udp.h> #include <linux/init.h> #include <linux/highmem.h> #include <linux/user_namespace.h> @@ -4144,8 +4145,14 @@ bool sk_busy_loop_end(void *p, unsigned long start_time) {struct sock *sk = p; -return !skb_queue_empty_lockless (&sk->sk_receive_queue) || -sk_busy_loop_timeout (sk, start_time); +if (!skb_queue_empty_lockless (&sk->sk_receive_queue)) +return true; + +if (sk_is_udp (sk) && + !skb_queue_empty_lockless (&udp_sk (sk)->reader_queue)) +return true; + +return sk_busy_loop_timeout (sk, start_time); }EXPORT_SYMBOL (sk_busy_loop_end);#endif /* CONFIG_NET_RX_BUSY_POLL */ Signature · 1 line
2.43.0.429.g432eaa2c6b-goog
This patch was applied to netdev/net.git (main) by David S. Miller <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by David S. Miller <[email protected]>:
Quoted 10 lines · On Thu, 18 Jan 2024 20:17:49 +0000 you wrote:
Generic
sk_busy_loop_end()only looks atsk->sk_receive_queuefor presence of packets.Problem is that for UDP sockets after blamed commit, some packets could be present in another queue: udp_sk(sk)->reader_queue
In some cases, a busy poller could spin until timeout expiration, even if some packets are available in udp_sk(sk)->reader_queue.
[...]
Here is the summary with links:
- [v3,net] udp: fix busy polling
https://git.kernel.org/netdev/net/c/a54d51fb2dfb
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html