[BACK]Return to uipc_socket.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.

Diff for /src/sys/kern/uipc_socket.c between version 1.204 and 1.209.2.1

version 1.204, 2011/06/26 16:42:42 version 1.209.2.1, 2012/07/12 17:11:17
Line 92  __KERNEL_RCSID(0, "$NetBSD$");
Line 92  __KERNEL_RCSID(0, "$NetBSD$");
 #include <sys/kauth.h>  #include <sys/kauth.h>
 #include <sys/mutex.h>  #include <sys/mutex.h>
 #include <sys/condvar.h>  #include <sys/condvar.h>
   #include <sys/kthread.h>
   
 #ifdef COMPAT_50  #ifdef COMPAT_50
 #include <compat/sys/time.h>  #include <compat/sys/time.h>
Line 135  EVCNT_ATTACH_STATIC(sosend_kvalimit);
Line 136  EVCNT_ATTACH_STATIC(sosend_kvalimit);
   
 #endif /* SOSEND_COUNTERS */  #endif /* SOSEND_COUNTERS */
   
 static struct callback_entry sokva_reclaimerentry;  
   
 #if defined(SOSEND_NO_LOAN) || defined(MULTIPROCESSOR)  #if defined(SOSEND_NO_LOAN) || defined(MULTIPROCESSOR)
 int sock_loan_thresh = -1;  int sock_loan_thresh = -1;
 #else  #else
Line 144  int sock_loan_thresh = 4096;
Line 143  int sock_loan_thresh = 4096;
 #endif  #endif
   
 static kmutex_t so_pendfree_lock;  static kmutex_t so_pendfree_lock;
 static struct mbuf *so_pendfree;  static struct mbuf *so_pendfree = NULL;
   
 #ifndef SOMAXKVA  #ifndef SOMAXKVA
 #define SOMAXKVA (16 * 1024 * 1024)  #define SOMAXKVA (16 * 1024 * 1024)
Line 157  static kauth_listener_t socket_listener;
Line 156  static kauth_listener_t socket_listener;
   
 #define SOCK_LOAN_CHUNK         65536  #define SOCK_LOAN_CHUNK         65536
   
 static size_t sodopendfree(void);  static void sopendfree_thread(void *);
 static size_t sodopendfreel(void);  static kcondvar_t pendfree_thread_cv;
   static lwp_t *sopendfree_lwp;
   
 static void sysctl_kern_somaxkva_setup(void);  static void sysctl_kern_somaxkva_setup(void);
 static struct sysctllog *socket_sysctllog;  static struct sysctllog *socket_sysctllog;
Line 170  sokvareserve(struct socket *so, vsize_t 
Line 170  sokvareserve(struct socket *so, vsize_t 
   
         mutex_enter(&so_pendfree_lock);          mutex_enter(&so_pendfree_lock);
         while (socurkva + len > somaxkva) {          while (socurkva + len > somaxkva) {
                 size_t freed;  
   
                 /*  
                  * try to do pendfree.  
                  */  
   
                 freed = sodopendfreel();  
   
                 /*  
                  * if some kva was freed, try again.  
                  */  
   
                 if (freed)  
                         continue;  
   
                 SOSEND_COUNTER_INCR(&sosend_kvalimit);                  SOSEND_COUNTER_INCR(&sosend_kvalimit);
                 error = cv_wait_sig(&socurkva_cv, &so_pendfree_lock);                  error = cv_wait_sig(&socurkva_cv, &so_pendfree_lock);
                 if (error) {                  if (error) {
Line 212  sokvaunreserve(vsize_t len)
Line 197  sokvaunreserve(vsize_t len)
  */   */
   
 vaddr_t  vaddr_t
 sokvaalloc(vsize_t len, struct socket *so)  sokvaalloc(vaddr_t sva, vsize_t len, struct socket *so)
 {  {
         vaddr_t lva;          vaddr_t lva;
   
Line 227  sokvaalloc(vsize_t len, struct socket *s
Line 212  sokvaalloc(vsize_t len, struct socket *s
          * allocate kva.           * allocate kva.
          */           */
   
         lva = uvm_km_alloc(kernel_map, len, 0, UVM_KMF_VAONLY | UVM_KMF_WAITVA);          lva = uvm_km_alloc(kernel_map, len, atop(sva) & uvmexp.colormask,
               UVM_KMF_COLORMATCH | UVM_KMF_VAONLY | UVM_KMF_WAITVA);
         if (lva == 0) {          if (lva == 0) {
                 sokvaunreserve(len);                  sokvaunreserve(len);
                 return (0);                  return (0);
Line 277  sodoloanfree(struct vm_page **pgs, void 
Line 263  sodoloanfree(struct vm_page **pgs, void 
         sokvafree(sva, len);          sokvafree(sva, len);
 }  }
   
 static size_t  
 sodopendfree(void)  
 {  
         size_t rv;  
   
         if (__predict_true(so_pendfree == NULL))  
                 return 0;  
   
         mutex_enter(&so_pendfree_lock);  
         rv = sodopendfreel();  
         mutex_exit(&so_pendfree_lock);  
   
         return rv;  
 }  
   
 /*  /*
  * sodopendfreel: free mbufs on "pendfree" list.   * sopendfree_thread: free mbufs on "pendfree" list.
  * unlock and relock so_pendfree_lock when freeing mbufs.   * unlock and relock so_pendfree_lock when freeing mbufs.
  *  
  * => called with so_pendfree_lock held.  
  */   */
   
 static size_t  static void
 sodopendfreel(void)  sopendfree_thread(void *v)
 {  {
         struct mbuf *m, *next;          struct mbuf *m, *next;
         size_t rv = 0;          size_t rv;
   
         KASSERT(mutex_owned(&so_pendfree_lock));  
   
         while (so_pendfree != NULL) {          mutex_enter(&so_pendfree_lock);
                 m = so_pendfree;  
                 so_pendfree = NULL;  
                 mutex_exit(&so_pendfree_lock);  
   
                 for (; m != NULL; m = next) {          for (;;) {
                         next = m->m_next;                  rv = 0;
                         KASSERT((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0);                  while (so_pendfree != NULL) {
                         KASSERT(m->m_ext.ext_refcnt == 0);                          m = so_pendfree;
                           so_pendfree = NULL;
                           mutex_exit(&so_pendfree_lock);
   
                           for (; m != NULL; m = next) {
                                   next = m->m_next;
                                   KASSERT((~m->m_flags & (M_EXT|M_EXT_PAGES)) == 0);
                                   KASSERT(m->m_ext.ext_refcnt == 0);
   
                                   rv += m->m_ext.ext_size;
                                   sodoloanfree(m->m_ext.ext_pgs, m->m_ext.ext_buf,
                                       m->m_ext.ext_size);
                                   pool_cache_put(mb_cache, m);
                           }
   
                         rv += m->m_ext.ext_size;                          mutex_enter(&so_pendfree_lock);
                         sodoloanfree(m->m_ext.ext_pgs, m->m_ext.ext_buf,  
                             m->m_ext.ext_size);  
                         pool_cache_put(mb_cache, m);  
                 }                  }
                   if (rv)
                 mutex_enter(&so_pendfree_lock);                          cv_broadcast(&socurkva_cv);
                   cv_wait(&pendfree_thread_cv, &so_pendfree_lock);
         }          }
           panic("sopendfree_thread");
         return (rv);          /* NOTREACHED */
 }  }
   
 void  void
Line 345  soloanfree(struct mbuf *m, void *buf, si
Line 320  soloanfree(struct mbuf *m, void *buf, si
         mutex_enter(&so_pendfree_lock);          mutex_enter(&so_pendfree_lock);
         m->m_next = so_pendfree;          m->m_next = so_pendfree;
         so_pendfree = m;          so_pendfree = m;
         cv_broadcast(&socurkva_cv);          cv_signal(&pendfree_thread_cv);
         mutex_exit(&so_pendfree_lock);          mutex_exit(&so_pendfree_lock);
 }  }
   
Line 375  sosend_loan(struct socket *so, struct ui
Line 350  sosend_loan(struct socket *so, struct ui
   
         KASSERT(npgs <= M_EXT_MAXPAGES);          KASSERT(npgs <= M_EXT_MAXPAGES);
   
         lva = sokvaalloc(len, so);          lva = sokvaalloc(sva, len, so);
         if (lva == 0)          if (lva == 0)
                 return 0;                  return 0;
   
Line 408  sosend_loan(struct socket *so, struct ui
Line 383  sosend_loan(struct socket *so, struct ui
         return (space);          return (space);
 }  }
   
 static int  
 sokva_reclaim_callback(struct callback_entry *ce, void *obj, void *arg)  
 {  
   
         KASSERT(ce == &sokva_reclaimerentry);  
         KASSERT(obj == NULL);  
   
         sodopendfree();  
         if (!vm_map_starved_p(kernel_map)) {  
                 return CALLBACK_CHAIN_ABORT;  
         }  
         return CALLBACK_CHAIN_CONTINUE;  
 }  
   
 struct mbuf *  struct mbuf *
 getsombuf(struct socket *so, int type)  getsombuf(struct socket *so, int type)
 {  {
Line 497  soinit(void)
Line 458  soinit(void)
         mutex_init(&so_pendfree_lock, MUTEX_DEFAULT, IPL_VM);          mutex_init(&so_pendfree_lock, MUTEX_DEFAULT, IPL_VM);
         softnet_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);          softnet_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
         cv_init(&socurkva_cv, "sokva");          cv_init(&socurkva_cv, "sokva");
           cv_init(&pendfree_thread_cv, "sopendfr");
         soinit2();          soinit2();
   
         /* Set the initial adjusted socket buffer size. */          /* Set the initial adjusted socket buffer size. */
         if (sb_max_set(sb_max))          if (sb_max_set(sb_max))
                 panic("bad initial sb_max value: %lu", sb_max);                  panic("bad initial sb_max value: %lu", sb_max);
   
         callback_register(&vm_map_to_kernel(kernel_map)->vmk_reclaim_callback,  
             &sokva_reclaimerentry, NULL, sokva_reclaim_callback);  
   
         socket_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,          socket_listener = kauth_listen_scope(KAUTH_SCOPE_NETWORK,
             socket_listener_cb, NULL);              socket_listener_cb, NULL);
 }  }
   
   void
   soinit1(void)
   {
           int error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
               sopendfree_thread, NULL, &sopendfree_lwp, "sopendfree");
           if (error)
                   panic("soinit1 %d", error);
   }
   
 /*  /*
  * Socket operation routines.   * Socket operation routines.
  * These routines are called by the routines in   * These routines are called by the routines in
Line 604  fsocreate(int domain, struct socket **so
Line 572  fsocreate(int domain, struct socket **so
         if ((error = fd_allocfile(&fp, &fd)) != 0)          if ((error = fd_allocfile(&fp, &fd)) != 0)
                 return error;                  return error;
         fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0);          fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0);
         fp->f_flag = FREAD|FWRITE|((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0);          fp->f_flag = FREAD|FWRITE|((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|
               ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);
         fp->f_type = DTYPE_SOCKET;          fp->f_type = DTYPE_SOCKET;
         fp->f_ops = &socketops;          fp->f_ops = &socketops;
         error = socreate(domain, &so, type, protocol, l, NULL);          error = socreate(domain, &so, type, protocol, l, NULL);
Line 756  soclose(struct socket *so)
Line 725  soclose(struct socket *so)
                                 goto drop;                                  goto drop;
                 }                  }
                 if (so->so_options & SO_LINGER) {                  if (so->so_options & SO_LINGER) {
                         if ((so->so_state & SS_ISDISCONNECTING) && so->so_nbio)                          if ((so->so_state & (SS_ISDISCONNECTING|SS_NBIO)) ==
                               (SS_ISDISCONNECTING|SS_NBIO))
                                 goto drop;                                  goto drop;
                         while (so->so_state & SS_ISCONNECTED) {                          while (so->so_state & SS_ISCONNECTED) {
                                 error = sowait(so, true, so->so_linger * hz);                                  error = sowait(so, true, so->so_linger * hz);
Line 878  sodisconnect(struct socket *so)
Line 848  sodisconnect(struct socket *so)
                 error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,                  error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
                     NULL, NULL, NULL, NULL);                      NULL, NULL, NULL, NULL);
         }          }
         sodopendfree();  
         return (error);          return (error);
 }  }
   
Line 911  sosend(struct socket *so, struct mbuf *a
Line 880  sosend(struct socket *so, struct mbuf *a
         short           wakeup_state = 0;          short           wakeup_state = 0;
   
         p = l->l_proc;          p = l->l_proc;
         sodopendfree();  
         clen = 0;          clen = 0;
   
         /*          /*
Line 978  sosend(struct socket *so, struct mbuf *a
Line 946  sosend(struct socket *so, struct mbuf *a
                 }                  }
                 if (space < resid + clen &&                  if (space < resid + clen &&
                     (atomic || space < so->so_snd.sb_lowat || space < clen)) {                      (atomic || space < so->so_snd.sb_lowat || space < clen)) {
                         if (so->so_nbio) {                          if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) {
                                 error = EWOULDBLOCK;                                  error = EWOULDBLOCK;
                                 goto release;                                  goto release;
                         }                          }
Line 1164  soreceive(struct socket *so, struct mbuf
Line 1132  soreceive(struct socket *so, struct mbuf
 {  {
         struct lwp *l = curlwp;          struct lwp *l = curlwp;
         struct mbuf     *m, **mp, *mt;          struct mbuf     *m, **mp, *mt;
         int atomic, flags, len, error, s, offset, moff, type, orig_resid;          size_t len, offset, moff, orig_resid;
           int atomic, flags, error, s, type;
         const struct protosw    *pr;          const struct protosw    *pr;
         struct mbuf     *nextrecord;          struct mbuf     *nextrecord;
         int             mbuf_removed = 0;          int             mbuf_removed = 0;
Line 1187  soreceive(struct socket *so, struct mbuf
Line 1156  soreceive(struct socket *so, struct mbuf
         else          else
                 flags = 0;                  flags = 0;
   
         if ((flags & MSG_DONTWAIT) == 0)  
                 sodopendfree();  
   
         if (flags & MSG_OOB) {          if (flags & MSG_OOB) {
                 m = m_get(M_WAIT, MT_DATA);                  m = m_get(M_WAIT, MT_DATA);
                 solock(so);                  solock(so);
Line 1200  soreceive(struct socket *so, struct mbuf
Line 1166  soreceive(struct socket *so, struct mbuf
                         goto bad;                          goto bad;
                 do {                  do {
                         error = uiomove(mtod(m, void *),                          error = uiomove(mtod(m, void *),
                             (int) min(uio->uio_resid, m->m_len), uio);                              MIN(uio->uio_resid, m->m_len), uio);
                         m = m_free(m);                          m = m_free(m);
                 } while (uio->uio_resid > 0 && error == 0 && m);                  } while (uio->uio_resid > 0 && error == 0 && m);
  bad:   bad:
Line 1277  soreceive(struct socket *so, struct mbuf
Line 1243  soreceive(struct socket *so, struct mbuf
                 }                  }
                 if (uio->uio_resid == 0)                  if (uio->uio_resid == 0)
                         goto release;                          goto release;
                 if (so->so_nbio || (flags & MSG_DONTWAIT)) {                  if ((so->so_state & SS_NBIO) ||
                       (flags & (MSG_DONTWAIT|MSG_NBIO))) {
                         error = EWOULDBLOCK;                          error = EWOULDBLOCK;
                         goto release;                          goto release;
                 }                  }
Line 1453  soreceive(struct socket *so, struct mbuf
Line 1420  soreceive(struct socket *so, struct mbuf
                         SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove");                          SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove");
                         sounlock(so);                          sounlock(so);
                         splx(s);                          splx(s);
                         error = uiomove(mtod(m, char *) + moff, (int)len, uio);                          error = uiomove(mtod(m, char *) + moff, len, uio);
                         s = splsoftnet();                          s = splsoftnet();
                         solock(so);                          solock(so);
                         if (error != 0) {                          if (error != 0) {
Line 1737  sosetopt1(struct socket *so, const struc
Line 1704  sosetopt1(struct socket *so, const struc
         case SO_REUSEPORT:          case SO_REUSEPORT:
         case SO_OOBINLINE:          case SO_OOBINLINE:
         case SO_TIMESTAMP:          case SO_TIMESTAMP:
           case SO_NOSIGPIPE:
 #ifdef SO_OTIMESTAMP  #ifdef SO_OTIMESTAMP
         case SO_OTIMESTAMP:          case SO_OTIMESTAMP:
 #endif  #endif
Line 1937  sogetopt1(struct socket *so, struct sock
Line 1905  sogetopt1(struct socket *so, struct sock
         case SO_BROADCAST:          case SO_BROADCAST:
         case SO_OOBINLINE:          case SO_OOBINLINE:
         case SO_TIMESTAMP:          case SO_TIMESTAMP:
           case SO_NOSIGPIPE:
 #ifdef SO_OTIMESTAMP  #ifdef SO_OTIMESTAMP
         case SO_OTIMESTAMP:          case SO_OTIMESTAMP:
 #endif  #endif

Legend:
Removed from v.1.204  
changed lines
  Added in v.1.209.2.1

CVSweb <webmaster@jp.NetBSD.org>