git: 5dc8dd5f3ac5 - main - unix/dgram: inline sbappendaddr_locked() into uipc_sosend_dgram()

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Fri, 24 Jun 2022 16:10:33 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=5dc8dd5f3ac564518b391ecd3e8ad9f4f3c786df

commit 5dc8dd5f3ac564518b391ecd3e8ad9f4f3c786df
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2022-06-24 16:09:10 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2022-06-24 16:09:10 +0000

    unix/dgram: inline sbappendaddr_locked() into uipc_sosend_dgram()
    
    This allows to remove one M_NOWAIT allocation and also makes it
    more clear what's going on.
    
    Reviewed by:            markj
    Differential revision:  https://reviews.freebsd.org/D35297
---
 sys/kern/uipc_usrreq.c | 48 ++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 44 insertions(+), 4 deletions(-)

diff --git a/sys/kern/uipc_usrreq.c b/sys/kern/uipc_usrreq.c
index 51f60d1299b9..2872e28aef3a 100644
--- a/sys/kern/uipc_usrreq.c
+++ b/sys/kern/uipc_usrreq.c
@@ -1118,6 +1118,9 @@ release:
 
 /*
  * PF_UNIX/SOCK_DGRAM send
+ *
+ * Allocate a record consisting of 3 mbufs in the sequence of
+ * from -> control -> data and append it to the socket buffer.
  */
 static int
 uipc_sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
@@ -1126,11 +1129,14 @@ uipc_sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
 	struct unpcb *unp, *unp2;
 	const struct sockaddr *from;
 	struct socket *so2;
-	int error;
+	struct sockbuf *sb;
+	struct mbuf *f, *clast;
+	int cc, error;
 
 	MPASS((uio != NULL && m == NULL) || (m != NULL && uio == NULL));
 
 	error = 0;
+	f = NULL;
 
 	if (__predict_false(flags & MSG_OOB)) {
 		error = EOPNOTSUPP;
@@ -1146,6 +1152,7 @@ uipc_sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
 			error = EFAULT;
 			goto out;
 		}
+		f = m_get(M_WAITOK, MT_SONAME);
 		if (c != NULL && (error = unp_internalize(&c, td)))
 			goto out;
 	} else {
@@ -1159,6 +1166,10 @@ uipc_sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
 			error = EMSGSIZE;
 			goto out;
 		}
+		if ((f = m_get(M_NOWAIT, MT_SONAME)) == NULL) {
+			error = ENOBUFS;
+			goto out;
+		}
 		/* Condition the foreign mbuf to our standards. */
 		m_clrprotoflags(m);
 		m_tag_delete_chain(m, NULL);
@@ -1225,11 +1236,38 @@ uipc_sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
 		from = (struct sockaddr *)unp->unp_addr;
 	else
 		from = &sun_noname;
+	f->m_len = from->sa_len;
+	MPASS(from->sa_len <= MLEN);
+	bcopy(from, mtod(f, void *), from->sa_len);
+	cc = f->m_len + m->m_pkthdr.len;
+	if (c != NULL)
+		cc += m_length(c, &clast);
 	so2 = unp2->unp_socket;
-	SOCKBUF_LOCK(&so2->so_rcv);
-	if (sbappendaddr_locked(&so2->so_rcv, from, m, c)) {
+	sb = &so2->so_rcv;
+	SOCKBUF_LOCK(sb);
+	if (cc <= sbspace(sb)) {
+		/* Concatenate: from -> control -> data. */
+		if (c != NULL) {
+			f->m_next = c;
+			clast->m_next = m;
+		} else
+			f->m_next = m;
+		m = f;
+		/* Reusing f as iterator. */
+		for (f = m; f->m_next != NULL; f = f->m_next)
+			sballoc(sb, f);
+		sballoc(sb, f);
+		sb->sb_mbtail = f;
+		/* SBLINKRECORD */
+		if (sb->sb_lastrecord != NULL)
+			sb->sb_lastrecord->m_nextpkt = m;
+		else
+			sb->sb_mb = m;
+		sb->sb_lastrecord = m;
+		SBLASTMBUFCHK(sb);
+		SBLASTRECORDCHK(sb);
 		sorwakeup_locked(so2);
-		m = c = NULL;
+		f = m = c = NULL;
 	} else {
 		soroverflow_locked(so2);
 		error = (so->so_state & SS_NBIO) ? EAGAIN : ENOBUFS;
@@ -1248,6 +1286,8 @@ out2:
 	if (c)
 		unp_scan(c, unp_freerights);
 out:
+	if (f)
+		m_free(f);
 	if (c)
 		m_freem(c);
 	if (m)