Author: markj
Date: Tue Sep 15 19:23:01 2020
New Revision: 365763
URL: https://svnweb.freebsd.org/changeset/base/365763

Log:
  Avoid an unnecessary malloc() when connecting dgram sockets.
  
  The allocated memory is only required for SOCK_STREAM and SOCK_SEQPACKET
  sockets.
  
  Reviewed by:  kevans
  Tested by:    pho
  Sponsored by: The FreeBSD Foundation
  Differential Revision:        https://reviews.freebsd.org/D26298

Modified:
  head/sys/kern/uipc_usrreq.c

Modified: head/sys/kern/uipc_usrreq.c
==============================================================================
--- head/sys/kern/uipc_usrreq.c Tue Sep 15 19:22:37 2020        (r365762)
+++ head/sys/kern/uipc_usrreq.c Tue Sep 15 19:23:01 2020        (r365763)
@@ -1557,7 +1557,8 @@ static int
 unp_connectat(int fd, struct socket *so, struct sockaddr *nam,
     struct thread *td)
 {
-       struct sockaddr_un *soun = (struct sockaddr_un *)nam;
+       struct mtx *vplock;
+       struct sockaddr_un *soun;
        struct vnode *vp;
        struct socket *so2;
        struct unpcb *unp, *unp2, *unp3;
@@ -1566,7 +1567,7 @@ unp_connectat(int fd, struct socket *so, struct sockad
        struct sockaddr *sa;
        cap_rights_t rights;
        int error, len, freed;
-       struct mtx *vplock;
+       bool connreq;
 
        if (nam->sa_family != AF_UNIX)
                return (EAFNOSUPPORT);
@@ -1575,6 +1576,7 @@ unp_connectat(int fd, struct socket *so, struct sockad
        len = nam->sa_len - offsetof(struct sockaddr_un, sun_path);
        if (len <= 0)
                return (EINVAL);
+       soun = (struct sockaddr_un *)nam;
        bcopy(soun->sun_path, buf, len);
        buf[len] = 0;
 
@@ -1587,7 +1589,11 @@ unp_connectat(int fd, struct socket *so, struct sockad
        unp->unp_flags |= UNP_CONNECTING;
        UNP_PCB_UNLOCK(unp);
 
-       sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
+       connreq = (so->so_proto->pr_flags & PR_CONNREQUIRED) != 0;
+       if (connreq)
+               sa = malloc(sizeof(struct sockaddr_un), M_SONAME, M_WAITOK);
+       else
+               sa = NULL;
        NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF,
            UIO_SYSSPACE, buf, fd, cap_rights_init(&rights, CAP_CONNECTAT), td);
        error = namei(&nd);
@@ -1628,7 +1634,7 @@ unp_connectat(int fd, struct socket *so, struct sockad
                error = EPROTOTYPE;
                goto bad2;
        }
-       if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
+       if (connreq) {
                if (so2->so_options & SO_ACCEPTCONN) {
                        CURVNET_SET(so2->so_vnet);
                        so2 = sonewconn(so2, 0);
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to