git: 371392bc10f2 - main - sockbuf: remove sbflush_internal() and sbrelease_internal() shims

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Mon, 24 Mar 2025 06:41:18 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=371392bc10f262a71c197a64d3815d930f8527fe

commit 371392bc10f262a71c197a64d3815d930f8527fe
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2025-03-24 06:40:19 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2025-03-24 06:40:19 +0000

    sockbuf: remove sbflush_internal() and sbrelease_internal() shims
    
    This functions serve just one purpose - allow to call sbdestroy() from
    sofree() without triggering unlocked mutex assertions.  Let's just don't
    save on locking with INVARIANTS kernel and this will allow to clean up all
    these shims.  Should be no functional changes.
    
    Reviewed by:            markj
    Differential Revision:  https://reviews.freebsd.org/D49363
---
 sys/kern/uipc_sockbuf.c | 34 ++++++++++------------------------
 sys/kern/uipc_socket.c  | 18 +++++++++++++-----
 2 files changed, 23 insertions(+), 29 deletions(-)

diff --git a/sys/kern/uipc_sockbuf.c b/sys/kern/uipc_sockbuf.c
index e8d410b00c15..48984046ea8a 100644
--- a/sys/kern/uipc_sockbuf.c
+++ b/sys/kern/uipc_sockbuf.c
@@ -75,7 +75,6 @@ static void	sbcompress_ktls_rx(struct sockbuf *sb, struct mbuf *m,
     struct mbuf *n);
 #endif
 static struct mbuf	*sbcut_internal(struct sockbuf *sb, int len);
-static void	sbflush_internal(struct sockbuf *sb);
 
 /*
  * Our own version of m_clrprotoflags(), that can preserve M_NOTREADY.
@@ -779,24 +778,17 @@ sbsetopt(struct socket *so, struct sockopt *sopt)
 /*
  * Free mbufs held by a socket, and reserved mbuf space.
  */
-static void
-sbrelease_internal(struct socket *so, sb_which which)
-{
-	struct sockbuf *sb = sobuf(so, which);
-
-	sbflush_internal(sb);
-	(void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
-	    RLIM_INFINITY);
-	sb->sb_mbmax = 0;
-}
-
 void
 sbrelease_locked(struct socket *so, sb_which which)
 {
+	struct sockbuf *sb = sobuf(so, which);
 
 	SOCK_BUF_LOCK_ASSERT(so, which);
 
-	sbrelease_internal(so, which);
+	sbflush_locked(sb);
+	(void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
+	    RLIM_INFINITY);
+	sb->sb_mbmax = 0;
 }
 
 void
@@ -818,7 +810,7 @@ sbdestroy(struct socket *so, sb_which which)
 		ktls_free(sb->sb_tls_info);
 	sb->sb_tls_info = NULL;
 #endif
-	sbrelease_internal(so, which);
+	sbrelease_locked(so, which);
 }
 
 /*
@@ -1530,10 +1522,12 @@ sbcompress_ktls_rx(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
 /*
  * Free all mbufs in a sockbuf.  Check that all resources are reclaimed.
  */
-static void
-sbflush_internal(struct sockbuf *sb)
+void
+sbflush_locked(struct sockbuf *sb)
 {
 
+	SOCKBUF_LOCK_ASSERT(sb);
+
 	while (sb->sb_mbcnt || sb->sb_tlsdcc) {
 		/*
 		 * Don't call sbcut(sb, 0) if the leading mbuf is non-empty:
@@ -1548,14 +1542,6 @@ sbflush_internal(struct sockbuf *sb)
 	    sb->sb_ccc, (void *)sb->sb_mb, sb->sb_mbcnt));
 }
 
-void
-sbflush_locked(struct sockbuf *sb)
-{
-
-	SOCKBUF_LOCK_ASSERT(sb);
-	sbflush_internal(sb);
-}
-
 void
 sbflush(struct sockbuf *sb)
 {
diff --git a/sys/kern/uipc_socket.c b/sys/kern/uipc_socket.c
index c27b007cafc6..63d30f04c8e0 100644
--- a/sys/kern/uipc_socket.c
+++ b/sys/kern/uipc_socket.c
@@ -1862,14 +1862,22 @@ sofree(struct socket *so)
 	if (pr->pr_detach != NULL)
 		pr->pr_detach(so);
 
-	/*
-	 * From this point on, we assume that no other references to this
-	 * socket exist anywhere else in the stack.  Therefore, no locks need
-	 * to be acquired or held.
-	 */
 	if (!(pr->pr_flags & PR_SOCKBUF) && !SOLISTENING(so)) {
+		/*
+		 * From this point on, we assume that no other references to
+		 * this socket exist anywhere else in the stack.  Therefore,
+		 * no locks need to be acquired or held.
+		 */
+#ifdef INVARIANTS
+		SOCK_SENDBUF_LOCK(so);
+		SOCK_RECVBUF_LOCK(so);
+#endif
 		sbdestroy(so, SO_SND);
 		sbdestroy(so, SO_RCV);
+#ifdef INVARIANTS
+		SOCK_SENDBUF_UNLOCK(so);
+		SOCK_RECVBUF_UNLOCK(so);
+#endif
 	}
 	seldrain(&so->so_rdsel);
 	seldrain(&so->so_wrsel);