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.141.2.4 retrieving revision 1.142 diff -u -p -r1.141.2.4 -r1.142 --- src/sys/kern/uipc_socket.c 2008/03/23 02:05:01 1.141.2.4 +++ src/sys/kern/uipc_socket.c 2007/09/19 04:33:43 1.142 @@ -1,7 +1,7 @@ -/* uipc_socket.c,v 1.141.2.3 2008/01/09 01:56:28 matt Exp */ +/* $NetBSD: uipc_socket.c,v 1.142 2007/09/19 04:33:43 dyoung Exp $ */ /*- - * Copyright (c) 2002, 2007, 2008 The NetBSD Foundation, Inc. + * Copyright (c) 2002, 2007 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, "uipc_socket.c,v 1.141.2.3 2008/01/09 01:56:28 matt Exp"); +__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.142 2007/09/19 04:33:43 dyoung Exp $"); #include "opt_sock_counters.h" #include "opt_sosend_loan.h" @@ -280,7 +280,7 @@ sodoloanfree(struct vm_page **pgs, void } static size_t -sodopendfree(void) +sodopendfree() { size_t rv; @@ -299,10 +299,11 @@ sodopendfree(void) */ static size_t -sodopendfreel(void) +sodopendfreel() { struct mbuf *m, *next; size_t rv = 0; + int s; KASSERT(mutex_owned(&so_pendfree_lock)); @@ -318,7 +319,9 @@ sodopendfreel(void) sodoloanfree((m->m_flags & M_EXT_PAGES) ? m->m_ext.ext_pgs : NULL, m->m_ext.ext_buf, m->m_ext.ext_size); - pool_cache_put(mb_cache, m); + s = splvm(); + pool_cache_put(&mbpool_cache, m); + splx(s); } mutex_enter(&so_pendfree_lock); @@ -428,11 +431,11 @@ sokva_reclaim_callback(struct callback_e } struct mbuf * -getsombuf(struct socket *so, int type) +getsombuf(struct socket *so) { struct mbuf *m; - m = m_get(M_WAIT, type); + m = m_get(M_WAIT, MT_SONAME); MCLAIM(m, so->so_mowner); return m; } @@ -442,7 +445,7 @@ m_intopt(struct socket *so, int val) { struct mbuf *m; - m = getsombuf(so, MT_SOOPTS); + m = getsombuf(so); m->m_len = sizeof(int); *mtod(m, int *) = val; return m; @@ -452,7 +455,7 @@ void soinit(void) { - mutex_init(&so_pendfree_lock, MUTEX_DEFAULT, IPL_VM); + mutex_init(&so_pendfree_lock, MUTEX_DRIVER, IPL_VM); cv_init(&socurkva_cv, "sokva"); /* Set the initial adjusted socket buffer size. */ @@ -516,8 +519,6 @@ 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 - selinit(&so->so_rcv.sb_sel); - selinit(&so->so_snd.sb_sel); uid = kauth_cred_geteuid(l->l_cred); so->so_uidinfo = uid_find(uid); error = (*prp->pr_usrreq)(so, PRU_ATTACH, NULL, @@ -580,13 +581,13 @@ sobind(struct socket *so, struct mbuf *n } int -solisten(struct socket *so, int backlog, struct lwp *l) +solisten(struct socket *so, int backlog) { int s, error; s = splsoftnet(); error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, - NULL, NULL, l); + NULL, NULL, NULL); if (error != 0) { splx(s); return error; @@ -623,8 +624,6 @@ sofree(struct socket *so) RLIM_INFINITY); sbrelease(&so->so_snd, so); sorflush(so); - seldestroy(&so->so_rcv.sb_sel); - seldestroy(&so->so_snd.sb_sel); pool_put(&socket_pool, so); } @@ -660,7 +659,8 @@ soclose(struct socket *so) goto drop; } if (so->so_options & SO_LINGER) { - if ((so->so_state & SS_ISDISCONNECTING) && so->so_nbio) + if ((so->so_state & SS_ISDISCONNECTING) && + (so->so_state & SS_NBIO)) goto drop; while (so->so_state & SS_ISCONNECTED) { error = tsleep((void *)&so->so_timeo, @@ -869,7 +869,7 @@ sosend(struct socket *so, struct mbuf *a snderr(EMSGSIZE); if (space < resid + clen && (atomic || space < so->so_snd.sb_lowat || space < clen)) { - if (so->so_nbio) + if (so->so_state & SS_NBIO) snderr(EWOULDBLOCK); sbunlock(&so->so_snd); error = sbwait(&so->so_snd); @@ -890,7 +890,7 @@ sosend(struct socket *so, struct mbuf *a if (flags & MSG_EOR) top->m_flags |= M_EOR; } else do { - if (top == NULL) { + if (top == 0) { m = m_gethdr(M_WAIT, MT_DATA); mlen = MHLEN; m->m_pkthdr.len = 0; @@ -934,13 +934,14 @@ sosend(struct socket *so, struct mbuf *a if (atomic && top == 0 && len < mlen) MH_ALIGN(m, len); } - error = uiomove(mtod(m, void *), (int)len, uio); + error = uiomove(mtod(m, void *), (int)len, + uio); have_data: resid = uio->uio_resid; m->m_len = len; *mp = m; top->m_pkthdr.len += len; - if (error != 0) + if (error) goto release; mp = &m->m_next; if (resid <= 0) { @@ -969,10 +970,10 @@ sosend(struct socket *so, struct mbuf *a splx(s); clen = 0; - control = NULL; - top = NULL; + control = 0; + top = 0; mp = ⊤ - if (error != 0) + if (error) goto release; } while (resid && space > 0); } while (resid); @@ -1009,24 +1010,21 @@ soreceive(struct socket *so, struct mbuf { struct lwp *l = curlwp; struct mbuf *m, **mp; - int atomic, flags, len, error, s, offset, moff, type, orig_resid; + int flags, len, error, s, offset, moff, type, orig_resid; const struct protosw *pr; struct mbuf *nextrecord; int mbuf_removed = 0; - const struct domain *dom; pr = so->so_proto; - atomic = pr->pr_flags & PR_ATOMIC; - dom = pr->pr_domain; mp = mp0; type = 0; orig_resid = uio->uio_resid; - if (paddr != NULL) - *paddr = NULL; - if (controlp != NULL) - *controlp = NULL; - if (flagsp != NULL) + if (paddr) + *paddr = 0; + if (controlp) + *controlp = 0; + if (flagsp) flags = *flagsp &~ MSG_EOR; else flags = 0; @@ -1044,20 +1042,20 @@ soreceive(struct socket *so, struct mbuf error = uiomove(mtod(m, void *), (int) min(uio->uio_resid, m->m_len), uio); m = m_free(m); - } while (uio->uio_resid > 0 && error == 0 && m); + } while (uio->uio_resid && error == 0 && m); bad: - if (m != NULL) + if (m) m_freem(m); - return error; + return (error); } - if (mp != NULL) + if (mp) *mp = NULL; if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) (*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, l); restart: if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) - return error; + return (error); s = splsoftnet(); m = so->so_rcv.sb_mb; @@ -1072,19 +1070,17 @@ soreceive(struct socket *so, struct mbuf * we have to do the receive in sections, and thus risk returning * a short count if a timeout or signal occurs after we start. */ - if (m == NULL || - ((flags & MSG_DONTWAIT) == 0 && - so->so_rcv.sb_cc < uio->uio_resid && - (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || - ((flags & MSG_WAITALL) && - uio->uio_resid <= so->so_rcv.sb_hiwat)) && - m->m_nextpkt == NULL && !atomic)) { + if (m == 0 || (((flags & MSG_DONTWAIT) == 0 && + so->so_rcv.sb_cc < uio->uio_resid) && + (so->so_rcv.sb_cc < so->so_rcv.sb_lowat || + ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) && + m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) { #ifdef DIAGNOSTIC - if (m == NULL && so->so_rcv.sb_cc) + if (m == 0 && so->so_rcv.sb_cc) panic("receive 1"); #endif if (so->so_error) { - if (m != NULL) + if (m) goto dontblock; error = so->so_error; if ((flags & MSG_PEEK) == 0) @@ -1092,12 +1088,12 @@ soreceive(struct socket *so, struct mbuf goto release; } if (so->so_state & SS_CANTRCVMORE) { - if (m != NULL) + if (m) goto dontblock; else goto release; } - for (; m != NULL; m = m->m_next) + for (; m; m = m->m_next) if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) { m = so->so_rcv.sb_mb; goto dontblock; @@ -1109,7 +1105,7 @@ soreceive(struct socket *so, struct mbuf } if (uio->uio_resid == 0) goto release; - if (so->so_nbio || (flags & MSG_DONTWAIT)) { + if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) { error = EWOULDBLOCK; goto release; } @@ -1118,8 +1114,8 @@ soreceive(struct socket *so, struct mbuf sbunlock(&so->so_rcv); error = sbwait(&so->so_rcv); splx(s); - if (error != 0) - return error; + if (error) + return (error); goto restart; } dontblock: @@ -1128,7 +1124,7 @@ soreceive(struct socket *so, struct mbuf * While we process the initial mbufs containing address and control * info, we save a copy of m->m_nextpkt into nextrecord. */ - if (l != NULL) + if (l) l->l_proc->p_stats->p_ru.ru_msgrcv++; KASSERT(m == so->so_rcv.sb_mb); SBLASTRECORDCHK(&so->so_rcv, "soreceive 1"); @@ -1147,10 +1143,10 @@ soreceive(struct socket *so, struct mbuf } else { sbfree(&so->so_rcv, m); mbuf_removed = 1; - if (paddr != NULL) { + if (paddr) { *paddr = m; so->so_rcv.sb_mb = m->m_next; - m->m_next = NULL; + m->m_next = 0; m = so->so_rcv.sb_mb; } else { MFREE(m, so->so_rcv.sb_mb); @@ -1158,36 +1154,37 @@ soreceive(struct socket *so, struct mbuf } } } - while (m != NULL && m->m_type == MT_CONTROL && error == 0) { + while (m && m->m_type == MT_CONTROL && error == 0) { if (flags & MSG_PEEK) { - if (controlp != NULL) + if (controlp) *controlp = m_copy(m, 0, m->m_len); m = m->m_next; } else { sbfree(&so->so_rcv, m); mbuf_removed = 1; - if (controlp != NULL) { + if (controlp) { + struct domain *dom = pr->pr_domain; if (dom->dom_externalize && l && mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS) error = (*dom->dom_externalize)(m, l); *controlp = m; so->so_rcv.sb_mb = m->m_next; - m->m_next = NULL; + m->m_next = 0; m = so->so_rcv.sb_mb; } else { /* * Dispose of any SCM_RIGHTS message that went * through the read path rather than recv. */ - if (dom->dom_dispose && + if (pr->pr_domain->dom_dispose && mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS) - (*dom->dom_dispose)(m); + (*pr->pr_domain->dom_dispose)(m); MFREE(m, so->so_rcv.sb_mb); m = so->so_rcv.sb_mb; } } - if (controlp != NULL) { + if (controlp) { orig_resid = 0; controlp = &(*controlp)->m_next; } @@ -1199,7 +1196,7 @@ soreceive(struct socket *so, struct mbuf * the last packet on the chain (nextrecord == NULL) and we * change m->m_nextpkt. */ - if (m != NULL) { + if (m) { if ((flags & MSG_PEEK) == 0) { m->m_nextpkt = nextrecord; /* @@ -1227,7 +1224,7 @@ soreceive(struct socket *so, struct mbuf moff = 0; offset = 0; - while (m != NULL && uio->uio_resid > 0 && error == 0) { + while (m && uio->uio_resid > 0 && error == 0) { if (m->m_type == MT_OOBDATA) { if (type != MT_OOBDATA) break; @@ -1251,13 +1248,13 @@ soreceive(struct socket *so, struct mbuf * we must note any additions to the sockbuf when we * block interrupts again. */ - if (mp == NULL) { + if (mp == 0) { SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove"); SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove"); splx(s); error = uiomove(mtod(m, char *) + moff, (int)len, uio); s = splsoftnet(); - if (error != 0) { + if (error) { /* * If any part of the record has been removed * (such as the MT_SONAME mbuf, which will @@ -1269,7 +1266,8 @@ soreceive(struct socket *so, struct mbuf * This avoids a later panic("receive 1a") * when compiled with DIAGNOSTIC. */ - if (m && mbuf_removed && atomic) + if (m && mbuf_removed + && (pr->pr_flags & PR_ATOMIC)) (void) sbdroprecord(&so->so_rcv); goto release; @@ -1310,14 +1308,16 @@ soreceive(struct socket *so, struct mbuf SBLASTRECORDCHK(&so->so_rcv, "soreceive 3"); SBLASTMBUFCHK(&so->so_rcv, "soreceive 3"); } - } else if (flags & MSG_PEEK) - moff += len; - else { - if (mp != NULL) - *mp = m_copym(m, 0, len, M_WAIT); - m->m_data += len; - m->m_len -= len; - so->so_rcv.sb_cc -= len; + } else { + if (flags & MSG_PEEK) + moff += len; + else { + if (mp) + *mp = m_copym(m, 0, len, M_WAIT); + m->m_data += len; + m->m_len -= len; + so->so_rcv.sb_cc -= len; + } } if (so->so_oobmark) { if ((flags & MSG_PEEK) == 0) { @@ -1341,7 +1341,7 @@ soreceive(struct socket *so, struct mbuf * with a short count but without error. * Keep sockbuf locked against other readers. */ - while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 && + while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 && !sosendallatonce(so) && !nextrecord) { if (so->so_error || so->so_state & SS_CANTRCVMORE) break; @@ -1362,23 +1362,23 @@ soreceive(struct socket *so, struct mbuf SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2"); SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2"); error = sbwait(&so->so_rcv); - if (error != 0) { + if (error) { sbunlock(&so->so_rcv); splx(s); - return 0; + return (0); } if ((m = so->so_rcv.sb_mb) != NULL) nextrecord = m->m_nextpkt; } } - if (m && atomic) { + if (m && pr->pr_flags & PR_ATOMIC) { flags |= MSG_TRUNC; if ((flags & MSG_PEEK) == 0) (void) sbdroprecord(&so->so_rcv); } if ((flags & MSG_PEEK) == 0) { - if (m == NULL) { + if (m == 0) { /* * First part is an inline SB_EMPTY_FIXUP(). Second * part makes sure sb_lastrecord is up-to-date if @@ -1404,12 +1404,12 @@ soreceive(struct socket *so, struct mbuf goto restart; } - if (flagsp != NULL) + if (flagsp) *flagsp |= flags; release: sbunlock(&so->so_rcv); splx(s); - return error; + return (error); } int @@ -1426,7 +1426,7 @@ soshutdown(struct socket *so, int how) if (how == SHUT_WR || how == SHUT_RDWR) return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL, NULL, NULL, NULL); - return 0; + return (0); } void @@ -1682,9 +1682,8 @@ sogetopt(struct socket *so, int level, i void sohasoutofband(struct socket *so) { - fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so); - selnotify(&so->so_rcv.sb_sel, POLLPRI | POLLRDBAND, 0); + selwakeup(&so->so_rcv.sb_sel); } static void @@ -1795,66 +1794,13 @@ soo_kqfilter(struct file *fp, struct kno sb = &so->so_snd; break; default: - return (EINVAL); + return (1); } SLIST_INSERT_HEAD(&sb->sb_sel.sel_klist, kn, kn_selnext); sb->sb_flags |= SB_KNOTE; return (0); } -static int -sodopoll(struct socket *so, int events) -{ - int revents; - - revents = 0; - - if (events & (POLLIN | POLLRDNORM)) - if (soreadable(so)) - revents |= events & (POLLIN | POLLRDNORM); - - if (events & (POLLOUT | POLLWRNORM)) - if (sowritable(so)) - revents |= events & (POLLOUT | POLLWRNORM); - - if (events & (POLLPRI | POLLRDBAND)) - if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) - revents |= events & (POLLPRI | POLLRDBAND); - - return revents; -} - -int -sopoll(struct socket *so, int events) -{ - int revents = 0; - int s; - - if ((revents = sodopoll(so, events)) != 0) - return revents; - - KERNEL_LOCK(1, curlwp); - s = splsoftnet(); - - if ((revents = sodopoll(so, events)) == 0) { - if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) { - selrecord(curlwp, &so->so_rcv.sb_sel); - so->so_rcv.sb_flags |= SB_SEL; - } - - if (events & (POLLOUT | POLLWRNORM)) { - selrecord(curlwp, &so->so_snd.sb_sel); - so->so_snd.sb_flags |= SB_SEL; - } - } - - splx(s); - KERNEL_UNLOCK_ONE(curlwp); - - return revents; -} - - #include static int sysctl_kern_somaxkva(SYSCTLFN_PROTO);