Skip to content

Commit c5e183b

Browse files
almostivanNipaLocal
authored andcommitted
net: add sock_rcvbuf_has_space() helper
Let's add helper function that abstract the sk->sk_rcvbuf limits check for wraparound that Kuniyuki Iwashima introduce for udp receive path: commit 5a465a0 ("udp: Fix multiple wraparounds of sk->sk_rmem_alloc.") Note that I copied Kuniyuki Iwashima's comments into sock_rcvbuf_has_space(). Subsequent patches will use this for udp and netlink sockets. Signed-off-by: Jason Baron <[email protected]> Signed-off-by: NipaLocal <nipa@local>
1 parent 97e6d3c commit c5e183b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

include/net/sock.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,6 +2490,44 @@ static inline unsigned long sock_wspace(struct sock *sk)
24902490
return amt;
24912491
}
24922492

2493+
static inline bool __sock_rcvbuf_has_space(unsigned int rmem,
2494+
unsigned int rcvbuf,
2495+
unsigned int size)
2496+
{
2497+
/* Immediately drop when the receive queue is full. */
2498+
if (rmem + size > rcvbuf) {
2499+
if (rcvbuf > INT_MAX >> 1)
2500+
return false;
2501+
2502+
/* Always allow at least one packet for small buffer. */
2503+
if (rmem > rcvbuf)
2504+
return false;
2505+
}
2506+
return true;
2507+
}
2508+
2509+
/**
2510+
* sock_rcvbuf_has_space - check if sk->sk_rcvbuf has space
2511+
* @sk: socket
2512+
* @skb: socket buffer
2513+
*
2514+
* Can skb->truesize bytes be added to the socket receive buffer
2515+
* while respecting the sk->sk_rcvbuf limit. Note that rcvbuf and
2516+
* rmem are assigned to unsigned int to avoid wraparound.
2517+
*
2518+
* Return: true if there is space, false otherwise
2519+
*/
2520+
static inline bool sock_rcvbuf_has_space(struct sock *sk, struct sk_buff *skb)
2521+
{
2522+
unsigned int rmem, rcvbuf;
2523+
2524+
/* Cast to unsigned int performs the boundary check for INT_MAX. */
2525+
rmem = atomic_read(&sk->sk_rmem_alloc);
2526+
rcvbuf = READ_ONCE(sk->sk_rcvbuf);
2527+
2528+
return __sock_rcvbuf_has_space(rmem, rcvbuf, skb->truesize);
2529+
}
2530+
24932531
/* Note:
24942532
* We use sk->sk_wq_raw, from contexts knowing this
24952533
* pointer is not NULL and cannot disappear/change.

0 commit comments

Comments
 (0)