[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <8ceedaa19fb64010ada1db183f7a6ad5a297835b.1750285100.git.jbaron@akamai.com>
Date: Wed, 18 Jun 2025 19:13:21 -0400
From: Jason Baron <jbaron@...mai.com>
To: netdev@...r.kernel.org
Cc: davem@...emloft.net, edumazet@...gle.com, kuba@...nel.org,
pabeni@...hat.com, horms@...nel.org, kuniyu@...zon.com
Subject: [PATCH net-next v2 1/3] 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 5a465a0da13e ("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 <jbaron@...mai.com>
---
include/net/sock.h | 38 ++++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/include/net/sock.h b/include/net/sock.h
index 70c0b570a21f..5f4fdf96bba6 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -2484,6 +2484,44 @@ static inline unsigned long sock_wspace(struct sock *sk)
return amt;
}
+static inline bool __sock_rcvbuf_has_space(unsigned int rmem,
+ unsigned int rcvbuf,
+ unsigned int size)
+{
+ /* Immediately drop when the receive queue is full. */
+ if (rmem + size > rcvbuf) {
+ if (rcvbuf > INT_MAX >> 1)
+ return false;
+
+ /* Always allow at least one packet for small buffer. */
+ if (rmem > rcvbuf)
+ return false;
+ }
+ return true;
+}
+
+/**
+ * sock_rcvbuf_has_space - check if sk->sk_rcvbuf has space
+ * @sk: socket
+ * @skb: socket buffer
+ *
+ * Can skb->truesize bytes be added to the socket receive buffer
+ * while respecting the sk->sk_rcvbuf limit. Note that rcvbuf and
+ * rmem are assigned to unsigned int to avoid wraparound.
+ *
+ * Return: true if there is space, false otherwise
+ */
+static inline bool sock_rcvbuf_has_space(struct sock *sk, struct sk_buff *skb)
+{
+ unsigned int rmem, rcvbuf;
+
+ /* Cast to unsigned int performs the boundary check for INT_MAX. */
+ rmem = atomic_read(&sk->sk_rmem_alloc);
+ rcvbuf = READ_ONCE(sk->sk_rcvbuf);
+
+ return __sock_rcvbuf_has_space(rmem, rcvbuf, skb->truesize);
+}
+
/* Note:
* We use sk->sk_wq_raw, from contexts knowing this
* pointer is not NULL and cannot disappear/change.
--
2.25.1
Powered by blists - more mailing lists