udp: no longer touch sk->sk_refcnt in early demux
Subsystems (3)
PATCH 1/1 udp: no longer touch sk->sk_refcnt in early demux
After commits ca065d0cf80f ("udp: no longer use SLAB_DESTROY_BY_RCU") and 7ae215d23c12 ("bpf: Don't refcount LISTEN sockets in sk_assign()") UDP early demux no longer need to grab a refcount on the UDP socket.
This save two atomic operations per incoming packet for connected sockets.
--- net/ipv4/udp.c | 5 +++-- net/ipv6/udp.c | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-)
net/ipv4/udp.c +3 -2
@@ -2570,11 +2570,12 @@ int udp_v4_early_demux(struct sk_buff *skb) uh->source, iph->saddr, dif, sdif); } -if (!sk || !refcount_inc_not_zero (&sk->sk_refcnt)) +if (!sk)return 0 ; skb->sk = sk; - skb->destructor = sock_efree; +DEBUG_NET_WARN_ON_ONCE (sk_is_refcounted (sk)); + skb->destructor = sock_pfree; dst =rcu_dereference (sk->sk_rx_dst);if (dst)
net/ipv6/udp.c +3 -2
@@ -1101,11 +1101,12 @@ void udp_v6_early_demux(struct sk_buff *skb)else return ; -if (!sk || !refcount_inc_not_zero (&sk->sk_refcnt)) +if (!sk)return ; skb->sk = sk; - skb->destructor = sock_efree; +DEBUG_NET_WARN_ON_ONCE (sk_is_refcounted (sk)); + skb->destructor = sock_pfree; dst =rcu_dereference (sk->sk_rx_dst);if (dst)
Signature · 1 line
2.44.0.278.ge034bb2e1d-goog
Paolo Abeni
maintainerQuoted 6 lines · Eric Dumazet
After commits
ca065d0cf80f("udp: no longer useSLAB_DESTROY_BY_RCU") and7ae215d23c12("bpf: Don't refcount LISTEN sockets insk_assign()") UDP early demux no longer need to grab a refcount on the UDP socket.This save two atomic operations per incoming packet for connected sockets.
This reminds me of a old series:
https://lore.kernel.org/netdev/[email protected]/
and I'm wondering if we could reconsider such option.
Quoted 17 lines
Signed-off-by Eric Dumazet <[email protected]>Cc Martin KaFai Lau <[email protected]>Cc Joe Stringer <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Willem de Bruijn <[email protected]>Cc Kuniyuki Iwashima <[email protected]>--- net/ipv4/udp.c | 5 +++-- net/ipv6/udp.c | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-)net/ipv4/udp.c +0
-0@@ -2570,11 +2570,12 @@ int udp_v4_early_demux(struct sk_buff *skb) uh->source, iph->saddr, dif, sdif);
net/ipv4/udp.c int udp_v4_early_demux(struct sk_buff *skb) · line 2578
Quoted 10 lines
net/ipv4/udp.c +3
-2} -if (!sk || !refcount_inc_not_zero (&sk->sk_refcnt)) +if (!sk)return 0 ; skb->sk = sk; - skb->destructor = sock_efree; +DEBUG_NET_WARN_ON_ONCE (sk_is_refcounted (sk)); + skb->destructor = sock_pfree;
I *think* that the skb may escape the current rcu section if e.g. if matches a nf dup target in the input tables.
Back then I tried to implement some debug infra to track such accesses:
https://lore.kernel.org/lkml/[email protected]/
which was buggy (prone to false negative). I think it can be improved to something more reliable, perhaps I should revamp it?
I'm also wondering if the DEBUG_NET_WARN_ON_ONCE is worthy?!? the sk is an hashed UDP socket so is a full sock and has the bit SOCK_RCU_FREE set.
Perhaps we could use a simple 'noop' destructor as in:
Thanks!
Paolo
Paolo Abeni
maintainerQuoted 52 lines · Paolo Abeni
Quoted 6 lines · Eric Dumazet
After commits
ca065d0cf80f("udp: no longer useSLAB_DESTROY_BY_RCU") and7ae215d23c12("bpf: Don't refcount LISTEN sockets insk_assign()") UDP early demux no longer need to grab a refcount on the UDP socket.This save two atomic operations per incoming packet for connected sockets.
This reminds me of a old series:
https://lore.kernel.org/netdev/[email protected]/
and I'm wondering if we could reconsider such option.
Quoted 17 lines
Signed-off-by Eric Dumazet <[email protected]>Cc Martin KaFai Lau <[email protected]>Cc Joe Stringer <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Willem de Bruijn <[email protected]>Cc Kuniyuki Iwashima <[email protected]>--- net/ipv4/udp.c | 5 +++-- net/ipv6/udp.c | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-)net/ipv4/udp.c +0
-0@@ -2570,11 +2570,12 @@ int udp_v4_early_demux(struct sk_buff *skb) uh->source, iph->saddr, dif, sdif);net/ipv4/udp.c int udp_v4_early_demux(struct sk_buff *skb) · line 2578
Quoted 10 lines
net/ipv4/udp.c +3
-2} -if (!sk || !refcount_inc_not_zero (&sk->sk_refcnt)) +if (!sk)return 0 ; skb->sk = sk; - skb->destructor = sock_efree; +DEBUG_NET_WARN_ON_ONCE (sk_is_refcounted (sk)); + skb->destructor = sock_pfree;I *think* that the skb may escape the current rcu section if e.g. if matches a nf dup target in the input tables.
Back then I tried to implement some debug infra to track such accesses:
https://lore.kernel.org/lkml/[email protected]/
which was buggy (prone to false negative). I think it can be improved to something more reliable, perhaps I should revamp it?
Quoted 7 lines
I'm also wondering if the
DEBUG_NET_WARN_ON_ONCEis worthy?!? the sk is an hashed UDP socket so is a full sock and has the bitSOCK_RCU_FREEset.Perhaps we could use a simple 'noop' destructor as in:
Please ignore this last part, too late I noticed we need 'sock_pfree' to let inet_steal_sock() work as expected.
Paolo
Quoted 4 lines · Paolo Abeni
Please ignore this last part, too late I noticed we need '
sock_pfree' to letinet_steal_sock()work as expected.
Yes, this is skb_sk_is_prefetched() part.
Quoted 38 lines · Paolo Abeni
Quoted 6 lines · Eric Dumazet
After commits
ca065d0cf80f("udp: no longer useSLAB_DESTROY_BY_RCU") and7ae215d23c12("bpf: Don't refcount LISTEN sockets insk_assign()") UDP early demux no longer need to grab a refcount on the UDP socket.This save two atomic operations per incoming packet for connected sockets.
This reminds me of a old series:
https://lore.kernel.org/netdev/[email protected]/
and I'm wondering if we could reconsider such option.
Quoted 23 lines
Signed-off-by Eric Dumazet <[email protected]>Cc Martin KaFai Lau <[email protected]>Cc Joe Stringer <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Willem de Bruijn <[email protected]>Cc Kuniyuki Iwashima <[email protected]>--- net/ipv4/udp.c | 5 +++-- net/ipv6/udp.c | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-)net/ipv4/udp.c +1
-1@@ -2570,11 +2570,12 @@ int udp_v4_early_demux(struct sk_buff *skb) uh->source, iph->saddr, dif, sdif); } -if (!sk || !refcount_inc_not_zero (&sk->sk_refcnt)) +if (!sk)return 0 ;
Quoted 7 lines
net/ipv4/udp.c int udp_v4_early_demux(struct sk_buff *skb) · line 2578
Quoted 4 lines
net/ipv4/udp.c +2
-1skb->sk = sk; - skb->destructor = sock_efree; +DEBUG_NET_WARN_ON_ONCE (sk_is_refcounted (sk)); + skb->destructor = sock_pfree;I *think* that the skb may escape the current rcu section if e.g. if matches a nf dup target in the input tables.
You mean the netfilter queueing stuff perhaps ?
This is already safe, it uses a refcount_inc_not_zero(&sk->sk_refcnt):
if (skb_sk_is_prefetched(skb)) {
struct sock *sk = skb->sk;
if (!sk_is_refcounted(sk)) {
if (!refcount_inc_not_zero(&sk->sk_refcnt))
return -ENOTCONN;
/* drop refcount on skb_orphan */
skb->destructor = sock_edemux;
}
}I would think a duplicate can not duplicate skb->sk in general, or must also attempt an refcount_inc_not_zero(&sk->sk_refcnt) and use a related destructor.
Quoted 11 lines
Back then I tried to implement some debug infra to track such accesses:
https://lore.kernel.org/lkml/[email protected]/
which was buggy (prone to false negative). I think it can be improved to something more reliable, perhaps I should revamp it?
I'm also wondering if the
DEBUG_NET_WARN_ON_ONCEis worthy?!? the sk is an hashed UDP socket so is a full sock and has the bitSOCK_RCU_FREEset.
This was mostly to catch any future issues and related to my use of sock_pfree()
DEBUG_NET_WARN_ON_ONCE() is a nop, unless you compile a DEV kernel.
Quoted 5 lines
Perhaps we could use a simple 'noop' destructor as in:
I think we need sock_pfree() for inet_steal_sock(), I might be wrong.
Quoted 36 lines · Eric Dumazet
Quoted 12 lines
Quoted 12 lines
net/ipv4/udp.c +1
-1@@ -2570,11 +2570,12 @@ int udp_v4_early_demux(struct sk_buff *skb) uh->source, iph->saddr, dif, sdif); } -if (!sk || !refcount_inc_not_zero (&sk->sk_refcnt)) +if (!sk)return 0 ;Quoted 7 lines
net/ipv4/udp.c int udp_v4_early_demux(struct sk_buff *skb) · line 2578
Quoted 4 lines
net/ipv4/udp.c +2
-1skb->sk = sk; - skb->destructor = sock_efree; +DEBUG_NET_WARN_ON_ONCE (sk_is_refcounted (sk)); + skb->destructor = sock_pfree;I *think* that the skb may escape the current rcu section if e.g. if matches a nf dup target in the input tables.
You mean the netfilter queueing stuff perhaps ?
This is already safe, it uses a
refcount_inc_not_zero(&sk->sk_refcnt):if (skb_sk_is_prefetched(skb)) { struct sock *sk = skb->sk; if (!sk_is_refcounted(sk)) { if (!refcount_inc_not_zero(&sk->sk_refcnt)) return -ENOTCONN; /* drop refcount on skb_orphan */ skb->destructor = sock_edemux; } }
Quoted 2 lines
I would think a duplicate can not duplicate
skb->skin general, or must also attempt anrefcount_inc_not_zero(&sk->sk_refcnt)and use a related destructor.
Right, looks safe.
Quoted 3 lines · Paolo Abeni
Right, looks safe.
Acked-by Paolo Abeni <[email protected]>
Thanks !
BTW, I was thinking (for next cycle) to let users (or TCP stack with ad-hoc heuristics )
opt-in TCP sockets to SOCK_RCU_FREE.
This would avoid the refcnt dance for their incoming packets.
This patch was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:
Quoted 8 lines · On Thu, 7 Mar 2024 22:00:16 +0000 you wrote:
After commits
ca065d0cf80f("udp: no longer useSLAB_DESTROY_BY_RCU") and7ae215d23c12("bpf: Don't refcount LISTEN sockets insk_assign()") UDP early demux no longer need to grab a refcount on the UDP socket.This save two atomic operations per incoming packet for connected sockets.
[...]
Here is the summary with links:
- [net-next] udp: no longer touch sk->sk_refcnt in early demux
https://git.kernel.org/netdev/net-next/c/08842c43d016
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html