git: bb35a4e11d5e - main - unix: microoptimize unp_connectat() - one less lock on success

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Thu, 12 May 2022 20:22:53 UTC
The branch main has been updated by glebius:

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

commit bb35a4e11d5ec70f99cd36caf5290a33afc9a515
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2022-05-12 20:22:39 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2022-05-12 20:22:39 +0000

    unix: microoptimize unp_connectat() - one less lock on success
    
    This change is also a preparation for further optimization to
    allow locked return on success.
    
    Reviewed by:            markj
    Differential revision:  https://reviews.freebsd.org/D35182
---
 sys/kern/uipc_usrreq.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/sys/kern/uipc_usrreq.c b/sys/kern/uipc_usrreq.c
index 13fa7acfcdf3..d4e3b25c5479 100644
--- a/sys/kern/uipc_usrreq.c
+++ b/sys/kern/uipc_usrreq.c
@@ -1634,6 +1634,9 @@ unp_connectat(int fd, struct socket *so, struct sockaddr *nam,
 	    sotounpcb(so2) == unp2,
 	    ("%s: unp2 %p so2 %p", __func__, unp2, so2));
 	unp_connect2(so, so2, PRU_CONNECT);
+	KASSERT((unp->unp_flags & UNP_CONNECTING) != 0,
+	    ("%s: unp %p has UNP_CONNECTING clear", __func__, unp));
+	unp->unp_flags &= ~UNP_CONNECTING;
 	unp_pcb_unlock_pair(unp, unp2);
 bad2:
 	mtx_unlock(vplock);
@@ -1642,11 +1645,13 @@ bad:
 		vput(vp);
 	}
 	free(sa, M_SONAME);
-	UNP_PCB_LOCK(unp);
-	KASSERT((unp->unp_flags & UNP_CONNECTING) != 0,
-	    ("%s: unp %p has UNP_CONNECTING clear", __func__, unp));
-	unp->unp_flags &= ~UNP_CONNECTING;
-	UNP_PCB_UNLOCK(unp);
+	if (__predict_false(error)) {
+		UNP_PCB_LOCK(unp);
+		KASSERT((unp->unp_flags & UNP_CONNECTING) != 0,
+		    ("%s: unp %p has UNP_CONNECTING clear", __func__, unp));
+		unp->unp_flags &= ~UNP_CONNECTING;
+		UNP_PCB_UNLOCK(unp);
+	}
 	return (error);
 }