Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys - Split off PRU_ATTACH and PRU_DETACH logic into separat...



details:   https://anonhg.NetBSD.org/src/rev/fab54558daab
branches:  trunk
changeset: 796066:fab54558daab
user:      rmind <rmind%NetBSD.org@localhost>
date:      Mon May 19 02:51:24 2014 +0000

description:
- Split off PRU_ATTACH and PRU_DETACH logic into separate functions.
- Replace malloc with kmem and eliminate M_PCB while here.
- Sprinkle more asserts.

diffstat:

 sys/kern/kern_malloc.c              |    5 +-
 sys/kern/uipc_socket.c              |   32 +++---
 sys/kern/uipc_socket2.c             |   34 ++++---
 sys/kern/uipc_usrreq.c              |  127 +++++++++++----------------
 sys/net/link_proto.c                |   32 +++++-
 sys/net/raw_cb.c                    |   73 ++++++++-------
 sys/net/raw_cb.h                    |    4 +-
 sys/net/raw_usrreq.c                |   65 +++----------
 sys/net/rtsock.c                    |   98 ++++++++++++---------
 sys/netatalk/ddp_usrreq.c           |   66 ++++++-------
 sys/netbt/bt_proto.c                |    6 +-
 sys/netbt/hci_socket.c              |  114 ++++++++++++++----------
 sys/netbt/l2cap.h                   |    6 +-
 sys/netbt/l2cap_socket.c            |   70 +++++++++-----
 sys/netbt/l2cap_upper.c             |    7 +-
 sys/netbt/rfcomm.h                  |    6 +-
 sys/netbt/rfcomm_socket.c           |   90 +++++++++++--------
 sys/netbt/rfcomm_upper.c            |   16 +--
 sys/netbt/sco.h                     |    4 +-
 sys/netbt/sco_socket.c              |   61 ++++++++----
 sys/netbt/sco_upper.c               |   10 +-
 sys/netinet/ip_icmp.c               |   11 +-
 sys/netinet/raw_ip.c                |  130 +++++++++++++++------------
 sys/netinet/tcp_usrreq.c            |  165 ++++++++++++++++++++++-------------
 sys/netinet/tcp_var.h               |    3 +-
 sys/netinet/udp_usrreq.c            |  123 +++++++++++++++-----------
 sys/netinet6/icmp6.c                |   11 +-
 sys/netinet6/ip6_var.h              |    3 +-
 sys/netinet6/raw_ip6.c              |  121 ++++++++++++++-----------
 sys/netinet6/udp6_usrreq.c          |   92 ++++++++++++-------
 sys/netipsec/key.c                  |   14 +-
 sys/netipsec/keysock.c              |  107 +++++++++++++---------
 sys/netmpls/mpls_proto.c            |   31 ++++--
 sys/netnatm/natm.c                  |   87 ++++++++++--------
 sys/netnatm/natm.h                  |    4 +-
 sys/netnatm/natm_pcb.c              |   32 ++----
 sys/netsmb/smb_trantcp.c            |   12 +-
 sys/rump/net/lib/libsockin/sockin.c |   89 ++++++++++--------
 sys/sys/malloc.h                    |    3 +-
 sys/sys/protosw.h                   |    4 +-
 sys/sys/un.h                        |    5 +-
 41 files changed, 1073 insertions(+), 900 deletions(-)

diffs (truncated from 3762 to 300 lines):

diff -r 3e3b6dd01f5c -r fab54558daab sys/kern/kern_malloc.c
--- a/sys/kern/kern_malloc.c    Sun May 18 21:25:44 2014 +0000
+++ b/sys/kern/kern_malloc.c    Mon May 19 02:51:24 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_malloc.c,v 1.142 2012/04/30 22:51:27 rmind Exp $  */
+/*     $NetBSD: kern_malloc.c,v 1.143 2014/05/19 02:51:24 rmind Exp $  */
 
 /*
  * Copyright (c) 1987, 1991, 1993
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.142 2012/04/30 22:51:27 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.143 2014/05/19 02:51:24 rmind Exp $");
 
 #include <sys/param.h>
 #include <sys/malloc.h>
@@ -84,7 +84,6 @@
 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
 MALLOC_DEFINE(M_DMAMAP, "DMA map", "bus_dma(9) structures");
 MALLOC_DEFINE(M_FREE, "free", "should be on free list");
-MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
 MALLOC_DEFINE(M_FTABLE, "fragtbl", "fragment reassembly header");
diff -r 3e3b6dd01f5c -r fab54558daab sys/kern/uipc_socket.c
--- a/sys/kern/uipc_socket.c    Sun May 18 21:25:44 2014 +0000
+++ b/sys/kern/uipc_socket.c    Mon May 19 02:51:24 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: uipc_socket.c,v 1.223 2014/05/18 14:46:15 rmind Exp $  */
+/*     $NetBSD: uipc_socket.c,v 1.224 2014/05/19 02:51:24 rmind Exp $  */
 
 /*-
  * Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -71,7 +71,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.223 2014/05/18 14:46:15 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.224 2014/05/19 02:51:24 rmind Exp $");
 
 #include "opt_compat_netbsd.h"
 #include "opt_sock_counters.h"
@@ -490,7 +490,7 @@
  *
  * => Caller may specify another socket for lock sharing (must not be held).
  * => Returns the new socket without lock held.
-*/
+ */
 int
 socreate(int dom, struct socket **aso, int type, int proto, struct lwp *l,
         struct socket *lockso)
@@ -538,20 +538,23 @@
        uid = kauth_cred_geteuid(l->l_cred);
        so->so_uidinfo = uid_find(uid);
        so->so_cpid = l->l_proc->p_pid;
-       if (lockso != NULL) {
-               /* Caller wants us to share a lock. */
+
+       /*
+        * Lock assigned and taken during PCB attach, unless we share
+        * the lock with another socket, e.g. socketpair(2) case.
+        */
+       if (lockso) {
                lock = lockso->so_lock;
                so->so_lock = lock;
                mutex_obj_hold(lock);
-               /* XXX Why is this not solock, to match sounlock? */
                mutex_enter(lock);
-       } else {
-               /* Lock assigned and taken during PRU_ATTACH. */
        }
-       error = (*prp->pr_usrreqs->pr_generic)(so, PRU_ATTACH, NULL,
-           (struct mbuf *)(long)proto, NULL, l);
+
+       /* Attach the PCB (returns with the socket lock held). */
+       error = (*prp->pr_usrreqs->pr_attach)(so, proto);
        KASSERT(solocked(so));
-       if (error != 0) {
+
+       if (error) {
                KASSERT(so->so_pcb == NULL);
                so->so_state |= SS_NOFDREF;
                sofree(so);
@@ -559,6 +562,7 @@
        }
        so->so_cred = kauth_cred_dup(l->l_cred);
        sounlock(so);
+
        *aso = so;
        return 0;
 }
@@ -752,10 +756,8 @@
        }
  drop:
        if (so->so_pcb) {
-               int error2 = (*so->so_proto->pr_usrreqs->pr_generic)(so,
-                   PRU_DETACH, NULL, NULL, NULL, NULL);
-               if (error == 0)
-                       error = error2;
+               KASSERT(solocked(so));
+               (*so->so_proto->pr_usrreqs->pr_detach)(so);
        }
  discard:
        KASSERT((so->so_state & SS_NOFDREF) == 0);
diff -r 3e3b6dd01f5c -r fab54558daab sys/kern/uipc_socket2.c
--- a/sys/kern/uipc_socket2.c   Sun May 18 21:25:44 2014 +0000
+++ b/sys/kern/uipc_socket2.c   Mon May 19 02:51:24 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: uipc_socket2.c,v 1.118 2014/05/18 14:46:15 rmind Exp $ */
+/*     $NetBSD: uipc_socket2.c,v 1.119 2014/05/19 02:51:24 rmind Exp $ */
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -58,7 +58,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uipc_socket2.c,v 1.118 2014/05/18 14:46:15 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_socket2.c,v 1.119 2014/05/19 02:51:24 rmind Exp $");
 
 #include "opt_mbuftrace.h"
 #include "opt_sb_max.h"
@@ -282,13 +282,25 @@
        so->so_receive = head->so_receive;
        so->so_uidinfo = head->so_uidinfo;
        so->so_cpid = head->so_cpid;
+
+       /*
+        * Share the lock with the listening-socket, it may get unshared
+        * once the connection is complete.
+        */
+       mutex_obj_hold(head->so_lock);
+       so->so_lock = head->so_lock;
+
+       /*
+        * Reserve the space for socket buffers.
+        */
 #ifdef MBUFTRACE
        so->so_mowner = head->so_mowner;
        so->so_rcv.sb_mowner = head->so_rcv.sb_mowner;
        so->so_snd.sb_mowner = head->so_snd.sb_mowner;
 #endif
-       if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) != 0)
+       if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) {
                goto out;
+       }
        so->so_snd.sb_lowat = head->so_snd.sb_lowat;
        so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
        so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
@@ -297,17 +309,13 @@
        so->so_snd.sb_flags |= head->so_snd.sb_flags & (SB_AUTOSIZE | SB_ASYNC);
 
        /*
-        * Share the lock with the listening-socket, it may get unshared
-        * once the connection is complete.
+        * Finally, perform the protocol attach.  Note: a new socket
+        * lock may be assigned at this point (if so, it will be held).
         */
-       mutex_obj_hold(head->so_lock);
-       so->so_lock = head->so_lock;
-
-       error = (*so->so_proto->pr_usrreqs->pr_generic)(so,
-           PRU_ATTACH, NULL, NULL, NULL, NULL);
-       KASSERT(solocked(so));
+       error = (*so->so_proto->pr_usrreqs->pr_attach)(so, 0);
        if (error) {
 out:
+               KASSERT(solocked(so));
                KASSERT(so->so_accf == NULL);
                soput(so);
 
@@ -315,6 +323,7 @@
                KASSERT(solocked(head));
                return NULL;
        }
+       KASSERT(solocked2(head, so));
 
        /*
         * Insert into the queue.  If ready, update the connection status
@@ -326,7 +335,6 @@
                sorwakeup(head);
                cv_broadcast(&head->so_cv);
        }
-       KASSERT(solocked2(head, so));
        return so;
 }
 
@@ -1420,8 +1428,6 @@
                mutex_obj_hold(lock);
                mutex_enter(lock);
        }
-
-       /* In all cases, lock must be held on return from PRU_ATTACH. */
        KASSERT(solocked(so));
 }
 
diff -r 3e3b6dd01f5c -r fab54558daab sys/kern/uipc_usrreq.c
--- a/sys/kern/uipc_usrreq.c    Sun May 18 21:25:44 2014 +0000
+++ b/sys/kern/uipc_usrreq.c    Mon May 19 02:51:24 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: uipc_usrreq.c,v 1.151 2014/05/18 14:46:15 rmind Exp $  */
+/*     $NetBSD: uipc_usrreq.c,v 1.152 2014/05/19 02:51:24 rmind Exp $  */
 
 /*-
  * Copyright (c) 1998, 2000, 2004, 2008, 2009 The NetBSD Foundation, Inc.
@@ -96,7 +96,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.151 2014/05/18 14:46:15 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.152 2014/05/19 02:51:24 rmind Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -170,6 +170,7 @@
 };
 ino_t  unp_ino;                        /* prototype for fake inode numbers */
 
+static void unp_detach(struct socket *);
 struct mbuf *unp_addsockcred(struct lwp *, struct mbuf *);
 static void unp_mark(file_t *);
 static void unp_scan(struct mbuf *, void (*)(file_t *), int);
@@ -288,12 +289,11 @@
 static void
 unp_free(struct unpcb *unp)
 {
-
        if (unp->unp_addr)
                free(unp->unp_addr, M_SONAME);
        if (unp->unp_streamlock != NULL)
                mutex_obj_free(unp->unp_streamlock);
-       free(unp, M_PCB);
+       kmem_free(unp, sizeof(*unp));
 }
 
 int
@@ -366,40 +366,27 @@
 unp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
     struct mbuf *control, struct lwp *l)
 {
-       struct unpcb *unp = sotounpcb(so);
+       struct unpcb *unp;
        struct socket *so2;
        u_int newhiwat;
        int error = 0;
 
-       if (req == PRU_CONTROL)
-               return (EOPNOTSUPP);
+       KASSERT(req != PRU_ATTACH);
+       KASSERT(req != PRU_DETACH);
 
-#ifdef DIAGNOSTIC
-       if (req != PRU_SEND && req != PRU_SENDOOB && control)
-               panic("uipc_usrreq: unexpected control mbuf");
-#endif
-       if (req != PRU_ATTACH) {
-               if (unp == NULL) {
-                       error = EINVAL;
-                       goto release;
-               }
-               KASSERT(solocked(so));
+       if (req == PRU_CONTROL) {
+               return EOPNOTSUPP;
+       }
+       KASSERT(solocked(so));
+       unp = sotounpcb(so);
+
+       KASSERT(!control || (req == PRU_SEND || req == PRU_SENDOOB));
+       if (unp == NULL) {
+               error = EINVAL;
+               goto release;
        }
 
        switch (req) {
-
-       case PRU_ATTACH:
-               if (unp != NULL) {
-                       error = EISCONN;
-                       break;
-               }
-               error = unp_attach(so);
-               break;
-
-       case PRU_DETACH:
-               unp_detach(unp);
-               break;
-
        case PRU_BIND:
                KASSERT(l != NULL);
                error = unp_bind(so, nam, l);
@@ -452,7 +439,7 @@
                /*
                 * If the connection is fully established, break the
                 * association with uipc_lock and give the connected
-                * pair a seperate lock to share.
+                * pair a separate lock to share.
                 * There is a race here: sotounpcb(so2)->unp_streamlock
                 * is not locked, so when changing so2->so_lock



Home | Main Index | Thread Index | Old Index