Skip to content

Commit 36cd0e6

Browse files
jrfastabborkmann
authored andcommitted
bpf, sockmap: Ensure SO_RCVBUF memory is observed on ingress redirect
Fix sockmap sk_skb programs so that they observe sk_rcvbuf limits. This allows users to tune SO_RCVBUF and sockmap will honor them. We can refactor the if(charge) case out in later patches. But, keep this fix to the point. Fixes: 5119940 ("bpf: skb_verdict, support SK_PASS on RX BPF path") Suggested-by: Jakub Sitnicki <[email protected]> Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: Jakub Sitnicki <[email protected]> Link: https://lore.kernel.org/bpf/160556568657.73229.8404601585878439060.stgit@john-XPS-13-9370
1 parent c9c89dc commit 36cd0e6

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

net/core/skmsg.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,12 @@ static int sk_msg_free_elem(struct sock *sk, struct sk_msg *msg, u32 i,
170170
struct scatterlist *sge = sk_msg_elem(msg, i);
171171
u32 len = sge->length;
172172

173-
if (charge)
174-
sk_mem_uncharge(sk, len);
175-
if (!msg->skb)
173+
/* When the skb owns the memory we free it from consume_skb path. */
174+
if (!msg->skb) {
175+
if (charge)
176+
sk_mem_uncharge(sk, len);
176177
put_page(sg_page(sge));
178+
}
177179
memset(sge, 0, sizeof(*sge));
178180
return len;
179181
}
@@ -403,6 +405,9 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb)
403405
int copied = 0, num_sge;
404406
struct sk_msg *msg;
405407

408+
if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
409+
return -EAGAIN;
410+
406411
msg = kzalloc(sizeof(*msg), __GFP_NOWARN | GFP_ATOMIC);
407412
if (unlikely(!msg))
408413
return -EAGAIN;
@@ -418,7 +423,14 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb)
418423
return num_sge;
419424
}
420425

421-
sk_mem_charge(sk, skb->len);
426+
/* This will transition ownership of the data from the socket where
427+
* the BPF program was run initiating the redirect to the socket
428+
* we will eventually receive this data on. The data will be released
429+
* from skb_consume found in __tcp_bpf_recvmsg() after its been copied
430+
* into user buffers.
431+
*/
432+
skb_set_owner_r(skb, sk);
433+
422434
copied = skb->len;
423435
msg->sg.start = 0;
424436
msg->sg.size = copied;

net/ipv4/tcp_bpf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ int __tcp_bpf_recvmsg(struct sock *sk, struct sk_psock *psock,
4545
if (likely(!peek)) {
4646
sge->offset += copy;
4747
sge->length -= copy;
48-
sk_mem_uncharge(sk, copy);
48+
if (!msg_rx->skb)
49+
sk_mem_uncharge(sk, copy);
4950
msg_rx->sg.size -= copy;
5051

5152
if (!sge->length) {

0 commit comments

Comments
 (0)