[PATCH v3 bpf/net 4/6] sockmap: Pass gfp_t flag to sk_psock_skb_ingress().
From: Kuniyuki Iwashima <kuniyu@google.com>
Date: 2026-02-19 17:38:06
Also in:
bpf
Subsystem:
bpf [l7 framework] (sockmap), networking [general], the rest · Maintainers:
John Fastabend, Jakub Sitnicki, Jiayuan Chen, "David S. Miller", Eric Dumazet, Jakub Kicinski, Paolo Abeni, Linus Torvalds
SOCKMAP memory accounting for UDP is broken for two reasons:
1. sk->sk_forward_alloc must be changed under
spin_lock_bh(&sk->sk_receive_queue.lock)
2. sk_psock_skb_ingress_self() should not be used for UDP
since UDP may reclaim sk->sk_forward_alloc partially
before passing skb to sockmap, resulting in a negative
sk->sk_forward_alloc
This is a prep commit to consolidate sk_psock_skb_ingress_self()
and centralise the fix to sk_psock_skb_ingress().
Let's pass gfp_t flag to sk_psock_skb_ingress() and inline
sk_psock_create_ingress_msg().
Note that now alloc_sk_msg() is called first.
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
net/core/skmsg.c | 35 +++++++++++++++++------------------
1 file changed, 17 insertions(+), 18 deletions(-)
diff --git a/net/core/skmsg.c b/net/core/skmsg.c
index 3d7eb2f4ac98..57845b0d8a71 100644
--- a/net/core/skmsg.c
+++ b/net/core/skmsg.c@@ -529,18 +529,6 @@ static struct sk_msg *alloc_sk_msg(gfp_t gfp) return msg; } -static struct sk_msg *sk_psock_create_ingress_msg(struct sock *sk, - struct sk_buff *skb) -{ - if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf) - return NULL; - - if (!sk_rmem_schedule(sk, skb, skb->truesize)) - return NULL; - - return alloc_sk_msg(GFP_KERNEL); -} - static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb, u32 off, u32 len, struct sk_psock *psock,
@@ -588,11 +576,11 @@ static int sk_psock_skb_ingress_self(struct sk_psock *psock, struct sk_buff *skb u32 off, u32 len, bool take_ref); static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, - u32 off, u32 len) + u32 off, u32 len, gfp_t gfp_flags) { struct sock *sk = psock->sk; struct sk_msg *msg; - int err; + int err = -EAGAIN; /* If we are receiving on the same sock skb->sk is already assigned, * skip memory accounting and owner transition seeing it already set
@@ -600,9 +588,16 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, */ if (unlikely(skb->sk == sk)) return sk_psock_skb_ingress_self(psock, skb, off, len, true); - msg = sk_psock_create_ingress_msg(sk, skb); + + msg = alloc_sk_msg(gfp_flags); if (!msg) - return -EAGAIN; + goto out; + + if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf) + goto free; + + if (!sk_rmem_schedule(sk, skb, skb->truesize)) + goto free; /* This will transition ownership of the data from the socket where * the BPF program was run initiating the redirect to the socket
@@ -613,8 +608,12 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb, skb_set_owner_r(skb, sk); err = sk_psock_skb_ingress_enqueue(skb, off, len, psock, sk, msg, true); if (err < 0) - kfree(msg); + goto free; +out: return err; +free: + kfree(msg); + goto out; } /* Puts an skb on the ingress queue of the socket already assigned to the
@@ -652,7 +651,7 @@ static int sk_psock_handle_skb(struct sk_psock *psock, struct sk_buff *skb, return skb_send_sock(psock->sk, skb, off, len); } - return sk_psock_skb_ingress(psock, skb, off, len); + return sk_psock_skb_ingress(psock, skb, off, len, GFP_KERNEL); } static void sk_psock_skb_state(struct sk_psock *psock,
--
2.53.0.345.g96ddfc5eaa-goog