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 rcsdiff: /ftp/cvs/cvsroot/src/sys/kern/uipc_socket.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.78 retrieving revision 1.104 diff -u -p -r1.78 -r1.104 --- src/sys/kern/uipc_socket.c 2003/02/26 06:31:11 1.78 +++ src/sys/kern/uipc_socket.c 2004/07/01 12:42:57 1.104 @@ -1,4 +1,4 @@ -/* $NetBSD: uipc_socket.c,v 1.78 2003/02/26 06:31:11 matt Exp $ */ +/* $NetBSD: uipc_socket.c,v 1.104 2004/07/01 12:42:57 yamt Exp $ */ /*- * Copyright (c) 2002 The NetBSD Foundation, Inc. @@ -48,11 +48,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -72,10 +68,12 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.78 2003/02/26 06:31:11 matt Exp $"); +__KERNEL_RCSID(0, "$NetBSD: uipc_socket.c,v 1.104 2004/07/01 12:42:57 yamt Exp $"); #include "opt_sock_counters.h" #include "opt_sosend_loan.h" +#include "opt_mbuftrace.h" +#include "opt_somaxkva.h" #include #include @@ -92,10 +90,11 @@ __KERNEL_RCSID(0, "$NetBSD: uipc_socket. #include #include #include +#include #include -struct pool socket_pool; +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"); @@ -117,6 +116,10 @@ struct evcnt sosend_kvalimit = EVCNT_INI #define SOSEND_COUNTER_INCR(ev) (ev)->ev_count++ +EVCNT_ATTACH_STATIC(sosend_loan_big); +EVCNT_ATTACH_STATIC(sosend_copy_big); +EVCNT_ATTACH_STATIC(sosend_copy_small); +EVCNT_ATTACH_STATIC(sosend_kvalimit); #else #define SOSEND_COUNTER_INCR(ev) /* nothing */ @@ -127,15 +130,10 @@ void soinit(void) { - pool_init(&socket_pool, sizeof(struct socket), 0, 0, 0, - "sockpl", NULL); + /* Set the initial adjusted socket buffer size. */ + if (sb_max_set(sb_max)) + panic("bad initial sb_max value: %lu\n", sb_max); -#ifdef SOSEND_COUNTERS - evcnt_attach_static(&sosend_loan_big); - evcnt_attach_static(&sosend_copy_big); - evcnt_attach_static(&sosend_copy_small); - evcnt_attach_static(&sosend_kvalimit); -#endif /* SOSEND_COUNTERS */ } #ifdef SOSEND_NO_LOAN @@ -144,19 +142,131 @@ int use_sosend_loan = 0; int use_sosend_loan = 1; #endif +struct simplelock so_pendfree_slock = SIMPLELOCK_INITIALIZER; struct mbuf *so_pendfree; -int somaxkva = 16 * 1024 * 1024; +#ifndef SOMAXKVA +#define SOMAXKVA (16 * 1024 * 1024) +#endif +int somaxkva = SOMAXKVA; int socurkva; int sokvawaiters; #define SOCK_LOAN_THRESH 4096 #define SOCK_LOAN_CHUNK 65536 +static size_t sodopendfree(struct socket *); +static size_t sodopendfreel(struct socket *); +static __inline vsize_t sokvareserve(struct socket *, vsize_t); +static __inline void sokvaunreserve(vsize_t); + +static __inline vsize_t +sokvareserve(struct socket *so, vsize_t len) +{ + int s; + int error; + + s = splvm(); + simple_lock(&so_pendfree_slock); + while (socurkva + len > somaxkva) { + size_t freed; + + /* + * try to do pendfree. + */ + + freed = sodopendfreel(so); + + /* + * if some kva was freed, try again. + */ + + if (freed) + continue; + + SOSEND_COUNTER_INCR(&sosend_kvalimit); + sokvawaiters++; + error = ltsleep(&socurkva, PVM | PCATCH, "sokva", 0, + &so_pendfree_slock); + sokvawaiters--; + if (error) { + len = 0; + break; + } + } + socurkva += len; + simple_unlock(&so_pendfree_slock); + splx(s); + return len; +} + +static __inline void +sokvaunreserve(vsize_t len) +{ + int s; + + s = splvm(); + simple_lock(&so_pendfree_slock); + socurkva -= len; + if (sokvawaiters) + wakeup(&socurkva); + simple_unlock(&so_pendfree_slock); + splx(s); +} + +/* + * sokvaalloc: allocate kva for loan. + */ + +vaddr_t +sokvaalloc(vsize_t len, struct socket *so) +{ + vaddr_t lva; + + /* + * reserve kva. + */ + + if (sokvareserve(so, len) == 0) + return 0; + + /* + * allocate kva. + */ + + lva = uvm_km_valloc_wait(kernel_map, len); + if (lva == 0) { + sokvaunreserve(len); + return (0); + } + + return lva; +} + +/* + * sokvafree: free kva for loan. + */ + +void +sokvafree(vaddr_t sva, vsize_t len) +{ + + /* + * free kva. + */ + + uvm_km_free(kernel_map, sva, len); + + /* + * unreserve kva. + */ + + sokvaunreserve(len); +} + static void -sodoloanfree(caddr_t buf, size_t size) +sodoloanfree(struct vm_page **pgs, caddr_t buf, size_t size) { - struct vm_page **pgs; vaddr_t va, sva, eva; vsize_t len; paddr_t pa; @@ -167,79 +277,110 @@ sodoloanfree(caddr_t buf, size_t size) len = eva - sva; npgs = len >> PAGE_SHIFT; - pgs = alloca(npgs * sizeof(*pgs)); + if (__predict_false(pgs == NULL)) { + pgs = alloca(npgs * sizeof(*pgs)); - for (i = 0, va = sva; va < eva; i++, va += PAGE_SIZE) { - if (pmap_extract(pmap_kernel(), va, &pa) == FALSE) - panic("sodoloanfree: va 0x%lx not mapped", va); - pgs[i] = PHYS_TO_VM_PAGE(pa); + for (i = 0, va = sva; va < eva; i++, va += PAGE_SIZE) { + if (pmap_extract(pmap_kernel(), va, &pa) == FALSE) + panic("sodoloanfree: va 0x%lx not mapped", va); + pgs[i] = PHYS_TO_VM_PAGE(pa); + } } pmap_kremove(sva, len); pmap_update(pmap_kernel()); uvm_unloan(pgs, npgs, UVM_LOAN_TOPAGE); - uvm_km_free(kernel_map, sva, len); - socurkva -= len; - if (sokvawaiters) - wakeup(&socurkva); + sokvafree(sva, len); } static size_t sodopendfree(struct socket *so) { - struct mbuf *m; - size_t rv = 0; int s; + size_t rv; s = splvm(); + simple_lock(&so_pendfree_slock); + rv = sodopendfreel(so); + simple_unlock(&so_pendfree_slock); + splx(s); - for (;;) { - m = so_pendfree; - if (m == NULL) - break; - so_pendfree = m->m_next; - splx(s); + return rv; +} - rv += m->m_ext.ext_size; - sodoloanfree(m->m_ext.ext_buf, m->m_ext.ext_size); - s = splvm(); - pool_cache_put(&mbpool_cache, m); - } +/* + * sodopendfreel: free mbufs on "pendfree" list. + * unlock and relock so_pendfree_slock when freeing mbufs. + * + * => called with so_pendfree_slock held. + * => called at splvm. + */ + +static size_t +sodopendfreel(struct socket *so) +{ + size_t rv = 0; + + LOCK_ASSERT(simple_lock_held(&so_pendfree_slock)); for (;;) { - m = so->so_pendfree; + struct mbuf *m; + struct mbuf *next; + + m = so_pendfree; if (m == NULL) break; - so->so_pendfree = m->m_next; - splx(s); + so_pendfree = NULL; + simple_unlock(&so_pendfree_slock); + /* XXX splx */ + + for (; m != NULL; m = next) { + next = m->m_next; + + rv += m->m_ext.ext_size; + 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(&mbpool_cache, m); + } - rv += m->m_ext.ext_size; - sodoloanfree(m->m_ext.ext_buf, m->m_ext.ext_size); - s = splvm(); - pool_cache_put(&mbpool_cache, m); + /* XXX splvm */ + simple_lock(&so_pendfree_slock); } - splx(s); return (rv); } -static void +void soloanfree(struct mbuf *m, caddr_t buf, size_t size, void *arg) { - struct socket *so = arg; int s; if (m == NULL) { - sodoloanfree(buf, size); + + /* + * called from MEXTREMOVE. + */ + + sodoloanfree(NULL, buf, size); return; } + /* + * postpone freeing mbuf. + * + * we can't do it in interrupt context + * because we need to put kva back to kernel_map. + */ + s = splvm(); - m->m_next = so->so_pendfree; - so->so_pendfree = m; - splx(s); + simple_lock(&so_pendfree_slock); + m->m_next = so_pendfree; + so_pendfree = m; if (sokvawaiters) wakeup(&socurkva); + simple_unlock(&so_pendfree_slock); + splx(s); } static long @@ -248,9 +389,8 @@ sosend_loan(struct socket *so, struct ui struct iovec *iov = uio->uio_iov; vaddr_t sva, eva; vsize_t len; - struct vm_page **pgs; vaddr_t lva, va; - int npgs, s, i, error; + int npgs, i, error; if (uio->uio_segflg != UIO_USERSPACE) return (0); @@ -265,39 +405,30 @@ sosend_loan(struct socket *so, struct ui len = eva - sva; npgs = len >> PAGE_SHIFT; - while (socurkva + len > somaxkva) { - if (sodopendfree(so)) - continue; - SOSEND_COUNTER_INCR(&sosend_kvalimit); - s = splvm(); - sokvawaiters++; - (void) tsleep(&socurkva, PVM, "sokva", 0); - sokvawaiters--; - splx(s); - } + /* XXX KDASSERT */ + KASSERT(npgs <= M_EXT_MAXPAGES); + KASSERT(uio->uio_procp != NULL); - lva = uvm_km_valloc_wait(kernel_map, len); + lva = sokvaalloc(len, so); if (lva == 0) - return (0); - socurkva += len; - - pgs = alloca(npgs * sizeof(*pgs)); + return 0; error = uvm_loan(&uio->uio_procp->p_vmspace->vm_map, sva, len, - pgs, UVM_LOAN_TOPAGE); + m->m_ext.ext_pgs, UVM_LOAN_TOPAGE); if (error) { - uvm_km_free(kernel_map, lva, len); - socurkva -= len; + sokvafree(lva, len); return (0); } for (i = 0, va = lva; i < npgs; i++, va += PAGE_SIZE) - pmap_kenter_pa(va, VM_PAGE_TO_PHYS(pgs[i]), VM_PROT_READ); + pmap_kenter_pa(va, VM_PAGE_TO_PHYS(m->m_ext.ext_pgs[i]), + VM_PROT_READ); pmap_update(pmap_kernel()); lva += (vaddr_t) iov->iov_base & PAGE_MASK; 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) */ @@ -320,14 +451,12 @@ sosend_loan(struct socket *so, struct ui */ /*ARGSUSED*/ int -socreate(int dom, struct socket **aso, int type, int proto) +socreate(int dom, struct socket **aso, int type, int proto, struct proc *p) { - struct proc *p; - struct protosw *prp; + const struct protosw *prp; struct socket *so; int error, s; - p = curproc; /* XXX */ if (proto) prp = pffindproto(dom, proto, type); else @@ -352,6 +481,8 @@ socreate(int dom, struct socket **aso, i #endif if (p != 0) so->so_uid = p->p_ucred->cr_uid; + else + so->so_uid = UID_MAX; error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0, (struct mbuf *)(long)proto, (struct mbuf *)0, p); if (error) { @@ -401,7 +532,6 @@ solisten(struct socket *so, int backlog) void sofree(struct socket *so) { - struct mbuf *m; if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0) return; @@ -414,13 +544,14 @@ sofree(struct socket *so) if (!soqremque(so, 0)) return; } - sbrelease(&so->so_snd); + if (so->so_rcv.sb_hiwat) + (void)chgsbsize(so->so_uid, &so->so_rcv.sb_hiwat, 0, + RLIM_INFINITY); + if (so->so_snd.sb_hiwat) + (void)chgsbsize(so->so_uid, &so->so_snd.sb_hiwat, 0, + RLIM_INFINITY); + sbrelease(&so->so_snd, so); sorflush(so); - while ((m = so->so_pendfree) != NULL) { - so->so_pendfree = m->m_next; - m->m_next = so_pendfree; - so_pendfree = m; - } pool_put(&socket_pool, so); } @@ -518,12 +649,10 @@ soaccept(struct socket *so, struct mbuf } int -soconnect(struct socket *so, struct mbuf *nam) +soconnect(struct socket *so, struct mbuf *nam, struct proc *p) { - struct proc *p; int s, error; - p = curproc; /* XXX */ if (so->so_options & SO_ACCEPTCONN) return (EOPNOTSUPP); s = splsoftnet(); @@ -600,16 +729,14 @@ sodisconnect(struct socket *so) */ int sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top, - struct mbuf *control, int flags) + struct mbuf *control, int flags, struct proc *p) { - struct proc *p; struct mbuf **mp, *m; long space, len, resid, clen, mlen; int error, s, dontroute, atomic; sodopendfree(so); - p = curproc; /* XXX */ clen = 0; atomic = sosendallatonce(so) || top; if (uio) @@ -630,7 +757,8 @@ sosend(struct socket *so, struct mbuf *a dontroute = (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 && (so->so_proto->pr_flags & PR_ATOMIC); - p->p_stats->p_ru.ru_msgsnd++; + if (p) + p->p_stats->p_ru.ru_msgsnd++; if (control) clen = control->m_len; #define snderr(errno) { error = errno; splx(s); goto release; } @@ -662,7 +790,7 @@ sosend(struct socket *so, struct mbuf *a if ((atomic && resid > so->so_snd.sb_hiwat) || clen > so->so_snd.sb_hiwat) snderr(EMSGSIZE); - if (space < resid + clen && uio && + if (space < resid + clen && (atomic || space < so->so_snd.sb_lowat || space < clen)) { if (so->so_state & SS_NBIO) snderr(EWOULDBLOCK); @@ -803,9 +931,10 @@ int soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp) { + struct proc * p; struct mbuf *m, **mp; int flags, len, error, s, offset, moff, type, orig_resid; - struct protosw *pr; + const struct protosw *pr; struct mbuf *nextrecord; int mbuf_removed = 0; @@ -813,6 +942,8 @@ soreceive(struct socket *so, struct mbuf mp = mp0; type = 0; orig_resid = uio->uio_resid; + p = uio->uio_procp; + if (paddr) *paddr = 0; if (controlp) @@ -828,8 +959,8 @@ 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), (struct mbuf *)0, - (struct proc *)0); + (struct mbuf *)(long)(flags & MSG_PEEK), + (struct mbuf *)0, p); if (error) goto bad; do { @@ -846,7 +977,7 @@ soreceive(struct socket *so, struct mbuf *mp = (struct mbuf *)0; if (so->so_state & SS_ISCONFIRMING && uio->uio_resid) (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, - (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0); + (struct mbuf *)0, (struct mbuf *)0, p); restart: if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) @@ -920,8 +1051,8 @@ soreceive(struct socket *so, struct mbuf * info, we save a copy of m->m_nextpkt into nextrecord. */ #ifdef notyet /* XXXX */ - if (uio->uio_procp) - uio->uio_procp->p_stats->p_ru.ru_msgrcv++; + if (p) + p->p_stats->p_ru.ru_msgrcv++; #endif KASSERT(m == so->so_rcv.sb_mb); SBLASTRECORDCHK(&so->so_rcv, "soreceive 1"); @@ -960,10 +1091,11 @@ soreceive(struct socket *so, struct mbuf sbfree(&so->so_rcv, m); mbuf_removed = 1; if (controlp) { - if (pr->pr_domain->dom_externalize && + struct domain *dom = pr->pr_domain; + if (dom->dom_externalize && p && mtod(m, struct cmsghdr *)->cmsg_type == SCM_RIGHTS) - error = (*pr->pr_domain->dom_externalize)(m); + error = (*dom->dom_externalize)(m, p); *controlp = m; so->so_rcv.sb_mb = m->m_next; m->m_next = 0; @@ -1149,8 +1281,7 @@ soreceive(struct socket *so, struct mbuf (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, (struct mbuf *)(long)flags, - (struct mbuf *)0, - (struct proc *)0); + (struct mbuf *)0, p); SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2"); SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2"); error = sbwait(&so->so_rcv); @@ -1187,8 +1318,7 @@ soreceive(struct socket *so, struct mbuf SBLASTMBUFCHK(&so->so_rcv, "soreceive 4"); if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0, - (struct mbuf *)(long)flags, (struct mbuf *)0, - (struct proc *)0); + (struct mbuf *)(long)flags, (struct mbuf *)0, p); } if (orig_resid == uio->uio_resid && orig_resid && (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { @@ -1208,7 +1338,7 @@ soreceive(struct socket *so, struct mbuf int soshutdown(struct socket *so, int how) { - struct protosw *pr; + const struct protosw *pr; pr = so->so_proto; if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) @@ -1226,7 +1356,7 @@ void sorflush(struct socket *so) { struct sockbuf *sb, asb; - struct protosw *pr; + const struct protosw *pr; int s; sb = &so->so_rcv; @@ -1237,11 +1367,16 @@ sorflush(struct socket *so) socantrcvmore(so); sbunlock(sb); asb = *sb; - memset((caddr_t)sb, 0, sizeof(*sb)); + /* + * Clear most of the sockbuf structure, but leave some of the + * fields valid. + */ + memset(&sb->sb_startzero, 0, + sizeof(*sb) - offsetof(struct sockbuf, sb_startzero)); splx(s); if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose) (*pr->pr_domain->dom_dispose)(asb.sb_mb); - sbrelease(&asb); + sbrelease(&asb, so); } int @@ -1315,7 +1450,7 @@ sosetopt(struct socket *so, int level, i case SO_RCVBUF: if (sbreserve(optname == SO_SNDBUF ? &so->so_snd : &so->so_rcv, - (u_long) optval) == 0) { + (u_long) optval, so) == 0) { error = ENOBUFS; goto bad; } @@ -1343,14 +1478,14 @@ sosetopt(struct socket *so, int level, i case SO_RCVTIMEO: { struct timeval *tv; - short val; + int val; if (m == NULL || m->m_len < sizeof(*tv)) { error = EINVAL; goto bad; } tv = mtod(m, struct timeval *); - if (tv->tv_sec > (SHRT_MAX - tv->tv_usec / tick) / hz) { + if (tv->tv_sec > (INT_MAX - tv->tv_usec / tick) / hz) { error = EDOM; goto bad; } @@ -1472,12 +1607,7 @@ sogetopt(struct socket *so, int level, i void sohasoutofband(struct socket *so) { - struct proc *p; - - if (so->so_pgid < 0) - gsignal(-so->so_pgid, SIGURG); - else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0) - psignal(p, SIGURG); + fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so); selwakeup(&so->so_rcv.sb_sel); } @@ -1596,3 +1726,56 @@ soo_kqfilter(struct file *fp, struct kno return (0); } +#include + +static int sysctl_kern_somaxkva(SYSCTLFN_PROTO); + +/* + * sysctl helper routine for kern.somaxkva. ensures that the given + * value is not too small. + * (XXX should we maybe make sure it's not too large as well?) + */ +static int +sysctl_kern_somaxkva(SYSCTLFN_ARGS) +{ + int error, new_somaxkva; + struct sysctlnode node; + int s; + + new_somaxkva = somaxkva; + node = *rnode; + node.sysctl_data = &new_somaxkva; + error = sysctl_lookup(SYSCTLFN_CALL(&node)); + if (error || newp == NULL) + return (error); + + if (new_somaxkva < (16 * 1024 * 1024)) /* sanity */ + return (EINVAL); + + s = splvm(); + simple_lock(&so_pendfree_slock); + somaxkva = new_somaxkva; + wakeup(&socurkva); + simple_unlock(&so_pendfree_slock); + splx(s); + + return (error); +} + +SYSCTL_SETUP(sysctl_kern_somaxkva_setup, "sysctl kern.somaxkva setup") +{ + + sysctl_createv(clog, 0, NULL, NULL, + CTLFLAG_PERMANENT, + CTLTYPE_NODE, "kern", NULL, + NULL, 0, NULL, 0, + CTL_KERN, CTL_EOL); + + sysctl_createv(clog, 0, NULL, NULL, + CTLFLAG_PERMANENT|CTLFLAG_READWRITE, + CTLTYPE_INT, "somaxkva", + SYSCTL_DESCR("Maximum amount of kernel memory to be " + "used for socket buffers"), + sysctl_kern_somaxkva, 0, NULL, 0, + CTL_KERN, KERN_SOMAXKVA, CTL_EOL); +}