Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/sys/kern/uipc_socket.c,v retrieving revision 1.132.2.5 retrieving revision 1.133 diff -u -p -r1.132.2.5 -r1.133 --- src/sys/kern/uipc_socket.c 2007/05/07 10:55:48 1.132.2.5 +++ src/sys/kern/uipc_socket.c 2007/02/22 06:34:45 1.133 @@ -1,7 +1,7 @@ -/* $NetBSD: uipc_socket.c,v 1.132.2.5 2007/05/07 10:55:48 yamt Exp $ */ +/* $NetBSD: uipc_socket.c,v 1.133 2007/02/22 06:34:45 thorpej Exp $ */ /*- - * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc. + * Copyright (c) 2002 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation @@ -68,7 +68,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.132.2.5 2007/05/07 10:55:48 yamt Exp $"); +__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.133 2007/02/22 06:34:45 thorpej Exp $"); #include "opt_sock_counters.h" #include "opt_sosend_loan.h" @@ -92,13 +92,10 @@ __KERNEL_RCSID(0, "$NetBSD: uipc_socket. #include #include #include -#include -#include #include -POOL_INIT(socket_pool, sizeof(struct socket), 0, 0, 0, "sockpl", NULL, - IPL_SOFTNET); +POOL_INIT(socket_pool, sizeof(struct socket), 0, 0, 0, "sockpl", NULL); MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options"); MALLOC_DEFINE(M_SONAME, "soname", "socket name"); @@ -138,7 +135,7 @@ int sock_loan_thresh = -1; int sock_loan_thresh = 4096; #endif -static kmutex_t so_pendfree_lock; +static struct simplelock so_pendfree_slock = SIMPLELOCK_INITIALIZER; static struct mbuf *so_pendfree; #ifndef SOMAXKVA @@ -146,7 +143,7 @@ static struct mbuf *so_pendfree; #endif int somaxkva = SOMAXKVA; static int socurkva; -static kcondvar_t socurkva_cv; +static int sokvawaiters; #define SOCK_LOAN_CHUNK 65536 @@ -156,9 +153,11 @@ static size_t sodopendfreel(void); static vsize_t sokvareserve(struct socket *so, vsize_t len) { + int s; int error; - mutex_enter(&so_pendfree_lock); + s = splvm(); + simple_lock(&so_pendfree_slock); while (socurkva + len > somaxkva) { size_t freed; @@ -176,25 +175,33 @@ sokvareserve(struct socket *so, vsize_t continue; SOSEND_COUNTER_INCR(&sosend_kvalimit); - error = cv_wait_sig(&socurkva_cv, &so_pendfree_lock); + sokvawaiters++; + error = ltsleep(&socurkva, PVM | PCATCH, "sokva", 0, + &so_pendfree_slock); + sokvawaiters--; if (error) { len = 0; break; } } socurkva += len; - mutex_exit(&so_pendfree_lock); + simple_unlock(&so_pendfree_slock); + splx(s); return len; } static void sokvaunreserve(vsize_t len) { + int s; - mutex_enter(&so_pendfree_lock); + s = splvm(); + simple_lock(&so_pendfree_slock); socurkva -= len; - cv_broadcast(&socurkva_cv); - mutex_exit(&so_pendfree_lock); + if (sokvawaiters) + wakeup(&socurkva); + simple_unlock(&so_pendfree_slock); + splx(s); } /* @@ -248,7 +255,7 @@ sokvafree(vaddr_t sva, vsize_t len) } static void -sodoloanfree(struct vm_page **pgs, void *buf, size_t size) +sodoloanfree(struct vm_page **pgs, caddr_t buf, size_t size) { vaddr_t va, sva, eva; vsize_t len; @@ -279,35 +286,43 @@ sodoloanfree(struct vm_page **pgs, void static size_t sodopendfree() { + int s; size_t rv; - mutex_enter(&so_pendfree_lock); + s = splvm(); + simple_lock(&so_pendfree_slock); rv = sodopendfreel(); - mutex_exit(&so_pendfree_lock); + simple_unlock(&so_pendfree_slock); + splx(s); return rv; } /* * sodopendfreel: free mbufs on "pendfree" list. - * unlock and relock so_pendfree_lock when freeing mbufs. + * unlock and relock so_pendfree_slock when freeing mbufs. * - * => called with so_pendfree_lock held. + * => called with so_pendfree_slock held. + * => called at splvm. */ static size_t sodopendfreel() { - struct mbuf *m, *next; size_t rv = 0; - int s; - KASSERT(mutex_owned(&so_pendfree_lock)); + LOCK_ASSERT(simple_lock_held(&so_pendfree_slock)); + + for (;;) { + struct mbuf *m; + struct mbuf *next; - while (so_pendfree != NULL) { m = so_pendfree; + if (m == NULL) + break; so_pendfree = NULL; - mutex_exit(&so_pendfree_lock); + simple_unlock(&so_pendfree_slock); + /* XXX splx */ for (; m != NULL; m = next) { next = m->m_next; @@ -316,20 +331,20 @@ sodopendfreel() sodoloanfree((m->m_flags & M_EXT_PAGES) ? m->m_ext.ext_pgs : NULL, m->m_ext.ext_buf, m->m_ext.ext_size); - s = splvm(); pool_cache_put(&mbpool_cache, m); - splx(s); } - mutex_enter(&so_pendfree_lock); + /* XXX splvm */ + simple_lock(&so_pendfree_slock); } return (rv); } void -soloanfree(struct mbuf *m, void *buf, size_t size, void *arg) +soloanfree(struct mbuf *m, caddr_t buf, size_t size, void *arg) { + int s; if (m == NULL) { @@ -348,11 +363,14 @@ soloanfree(struct mbuf *m, void *buf, si * because we need to put kva back to kernel_map. */ - mutex_enter(&so_pendfree_lock); + s = splvm(); + simple_lock(&so_pendfree_slock); m->m_next = so_pendfree; so_pendfree = m; - cv_broadcast(&socurkva_cv); - mutex_exit(&so_pendfree_lock); + if (sokvawaiters) + wakeup(&socurkva); + simple_unlock(&so_pendfree_slock); + splx(s); } static long @@ -398,12 +416,12 @@ sosend_loan(struct socket *so, struct ui lva += (vaddr_t) iov->iov_base & PAGE_MASK; - MEXTADD(m, (void *) lva, space, M_MBUF, soloanfree, so); + MEXTADD(m, (caddr_t) lva, space, M_MBUF, soloanfree, so); m->m_flags |= M_EXT_PAGES | M_EXT_ROMAP; uio->uio_resid -= space; /* uio_offset not updated, not set/used for write(2) */ - uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + space; + uio->uio_iov->iov_base = (caddr_t) uio->uio_iov->iov_base + space; uio->uio_iov->iov_len -= space; if (uio->uio_iov->iov_len == 0) { uio->uio_iov++; @@ -431,9 +449,6 @@ void soinit(void) { - mutex_init(&so_pendfree_lock, MUTEX_DRIVER, IPL_VM); - cv_init(&socurkva_cv, "sokva"); - /* Set the initial adjusted socket buffer size. */ if (sb_max_set(sb_max)) panic("bad initial sb_max value: %lu", sb_max); @@ -461,29 +476,29 @@ socreate(int dom, struct socket **aso, i error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_SOCKET, KAUTH_REQ_NETWORK_SOCKET_OPEN, KAUTH_ARG(dom), KAUTH_ARG(type), KAUTH_ARG(proto)); - if (error != 0) - return error; + if (error) + return (error); if (proto) prp = pffindproto(dom, proto, type); else prp = pffindtype(dom, type); - if (prp == NULL) { + if (prp == 0) { /* no support for domain */ if (pffinddomain(dom) == 0) - return EAFNOSUPPORT; + return (EAFNOSUPPORT); /* no support for socket type */ if (proto == 0 && type != 0) - return EPROTOTYPE; - return EPROTONOSUPPORT; + return (EPROTOTYPE); + return (EPROTONOSUPPORT); } - if (prp->pr_usrreq == NULL) - return EPROTONOSUPPORT; + if (prp->pr_usrreq == 0) + return (EPROTONOSUPPORT); if (prp->pr_type != type) - return EPROTOTYPE; + return (EPROTOTYPE); s = splsoftnet(); so = pool_get(&socket_pool, PR_WAITOK); - memset(so, 0, sizeof(*so)); + memset((caddr_t)so, 0, sizeof(*so)); TAILQ_INIT(&so->so_q0); TAILQ_INIT(&so->so_q); so->so_type = type; @@ -495,19 +510,23 @@ socreate(int dom, struct socket **aso, i so->so_snd.sb_mowner = &prp->pr_domain->dom_mowner; so->so_mowner = &prp->pr_domain->dom_mowner; #endif - uid = kauth_cred_geteuid(l->l_cred); + if (l != NULL) { + uid = kauth_cred_geteuid(l->l_cred); + } else { + uid = 0; + } so->so_uidinfo = uid_find(uid); - error = (*prp->pr_usrreq)(so, PRU_ATTACH, NULL, - (struct mbuf *)(long)proto, NULL, l); - if (error != 0) { + error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0, + (struct mbuf *)(long)proto, (struct mbuf *)0, l); + if (error) { so->so_state |= SS_NOFDREF; sofree(so); splx(s); - return error; + return (error); } splx(s); *aso = so; - return 0; + return (0); } int @@ -516,9 +535,10 @@ sobind(struct socket *so, struct mbuf *n int s, error; s = splsoftnet(); - error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, NULL, nam, NULL, l); + error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, (struct mbuf *)0, + nam, (struct mbuf *)0, l); splx(s); - return error; + return (error); } int @@ -527,11 +547,11 @@ solisten(struct socket *so, int backlog) int s, error; s = splsoftnet(); - error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, - NULL, NULL, NULL); - if (error != 0) { + error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, (struct mbuf *)0, + (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0); + if (error) { splx(s); - return error; + return (error); } if (TAILQ_EMPTY(&so->so_q)) so->so_options |= SO_ACCEPTCONN; @@ -539,7 +559,7 @@ solisten(struct socket *so, int backlog) backlog = 0; so->so_qlimit = min(backlog, somaxconn); splx(s); - return 0; + return (0); } void @@ -604,7 +624,7 @@ soclose(struct socket *so) (so->so_state & SS_NBIO)) goto drop; while (so->so_state & SS_ISCONNECTED) { - error = tsleep((void *)&so->so_timeo, + error = tsleep((caddr_t)&so->so_timeo, PSOCK | PCATCH, netcls, so->so_linger * hz); if (error) @@ -615,7 +635,8 @@ soclose(struct socket *so) drop: if (so->so_pcb) { int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, - NULL, NULL, NULL, NULL); + (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0, + (struct lwp *)0); if (error == 0) error = error2; } @@ -634,15 +655,9 @@ soclose(struct socket *so) int soabort(struct socket *so) { - int error; - KASSERT(so->so_head == NULL); - error = (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL, - NULL, NULL, NULL); - if (error) { - sofree(so); - } - return error; + return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, (struct mbuf *)0, + (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0); } int @@ -658,7 +673,7 @@ soaccept(struct socket *so, struct mbuf if ((so->so_state & SS_ISDISCONNECTED) == 0 || (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0) error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, - NULL, nam, NULL, NULL); + (struct mbuf *)0, nam, (struct mbuf *)0, (struct lwp *)0); else error = ECONNABORTED; @@ -686,7 +701,7 @@ soconnect(struct socket *so, struct mbuf error = EISCONN; else error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, - NULL, nam, NULL, l); + (struct mbuf *)0, nam, (struct mbuf *)0, l); splx(s); return (error); } @@ -698,7 +713,8 @@ soconnect2(struct socket *so1, struct so s = splsoftnet(); error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, - NULL, (struct mbuf *)so2, NULL, NULL); + (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0, + (struct lwp *)0); splx(s); return (error); } @@ -718,7 +734,8 @@ sodisconnect(struct socket *so) goto bad; } error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, - NULL, NULL, NULL, NULL); + (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0, + (struct lwp *)0); bad: splx(s); sodopendfree(); @@ -835,7 +852,7 @@ sosend(struct socket *so, struct mbuf *a m = m_gethdr(M_WAIT, MT_DATA); mlen = MHLEN; m->m_pkthdr.len = 0; - m->m_pkthdr.rcvif = NULL; + m->m_pkthdr.rcvif = (struct ifnet *)0; } else { m = m_get(M_WAIT, MT_DATA); mlen = MLEN; @@ -875,7 +892,7 @@ sosend(struct socket *so, struct mbuf *a if (atomic && top == 0 && len < mlen) MH_ALIGN(m, len); } - error = uiomove(mtod(m, void *), (int)len, + error = uiomove(mtod(m, caddr_t), (int)len, uio); have_data: resid = uio->uio_resid; @@ -976,11 +993,12 @@ soreceive(struct socket *so, struct mbuf if (flags & MSG_OOB) { m = m_get(M_WAIT, MT_DATA); error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m, - (struct mbuf *)(long)(flags & MSG_PEEK), NULL, l); + (struct mbuf *)(long)(flags & MSG_PEEK), + (struct mbuf *)0, l); if (error) goto bad; do { - error = uiomove(mtod(m, void *), + error = uiomove(mtod(m, caddr_t), (int) min(uio->uio_resid, m->m_len), uio); m = m_free(m); } while (uio->uio_resid && error == 0 && m); @@ -990,9 +1008,10 @@ soreceive(struct socket *so, struct mbuf return (error); } if (mp) - *mp = NULL; + *mp = (struct mbuf *)0; if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) - (*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, l); + (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, + (struct mbuf *)0, (struct mbuf *)0, l); restart: if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) @@ -1193,7 +1212,7 @@ soreceive(struct socket *so, struct mbuf SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove"); SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove"); splx(s); - error = uiomove(mtod(m, char *) + moff, (int)len, uio); + error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio); s = splsoftnet(); if (error) { /* @@ -1228,7 +1247,7 @@ soreceive(struct socket *so, struct mbuf *mp = m; mp = &m->m_next; so->so_rcv.sb_mb = m = m->m_next; - *mp = NULL; + *mp = (struct mbuf *)0; } else { MFREE(m, so->so_rcv.sb_mb); m = so->so_rcv.sb_mb; @@ -1299,7 +1318,9 @@ soreceive(struct socket *so, struct mbuf */ if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb) (*pr->pr_usrreq)(so, PRU_RCVD, - NULL, (struct mbuf *)(long)flags, NULL, l); + (struct mbuf *)0, + (struct mbuf *)(long)flags, + (struct mbuf *)0, l); SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2"); SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2"); error = sbwait(&so->so_rcv); @@ -1335,8 +1356,8 @@ soreceive(struct socket *so, struct mbuf SBLASTRECORDCHK(&so->so_rcv, "soreceive 4"); SBLASTMBUFCHK(&so->so_rcv, "soreceive 4"); if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) - (*pr->pr_usrreq)(so, PRU_RCVD, NULL, - (struct mbuf *)(long)flags, NULL, l); + (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, + (struct mbuf *)(long)flags, (struct mbuf *)0, l); } if (orig_resid == uio->uio_resid && orig_resid && (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { @@ -1365,8 +1386,8 @@ soshutdown(struct socket *so, int how) if (how == SHUT_RD || how == SHUT_RDWR) sorflush(so); if (how == SHUT_WR || how == SHUT_RDWR) - return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL, - NULL, NULL, NULL); + return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0, + (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0); return (0); } @@ -1773,6 +1794,7 @@ sysctl_kern_somaxkva(SYSCTLFN_ARGS) { int error, new_somaxkva; struct sysctlnode node; + int s; new_somaxkva = somaxkva; node = *rnode; @@ -1784,10 +1806,12 @@ sysctl_kern_somaxkva(SYSCTLFN_ARGS) if (new_somaxkva < (16 * 1024 * 1024)) /* sanity */ return (EINVAL); - mutex_enter(&so_pendfree_lock); + s = splvm(); + simple_lock(&so_pendfree_slock); somaxkva = new_somaxkva; - cv_broadcast(&socurkva_cv); - mutex_exit(&so_pendfree_lock); + wakeup(&socurkva); + simple_unlock(&so_pendfree_slock); + splx(s); return (error); }