Home | History | Annotate | Line # | Download | only in kern
sys_select.c revision 1.21.4.3
      1  1.21.4.2     rmind /*	$NetBSD: sys_select.c,v 1.21.4.3 2011/04/21 01:42:09 rmind Exp $	*/
      2       1.1        ad 
      3       1.1        ad /*-
      4  1.21.4.1     rmind  * Copyright (c) 2007, 2008, 2009, 2010 The NetBSD Foundation, Inc.
      5       1.1        ad  * All rights reserved.
      6       1.1        ad  *
      7       1.1        ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.21.4.2     rmind  * by Andrew Doran and Mindaugas Rasiukevicius.
      9       1.1        ad  *
     10       1.1        ad  * Redistribution and use in source and binary forms, with or without
     11       1.1        ad  * modification, are permitted provided that the following conditions
     12       1.1        ad  * are met:
     13       1.1        ad  * 1. Redistributions of source code must retain the above copyright
     14       1.1        ad  *    notice, this list of conditions and the following disclaimer.
     15       1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     17       1.1        ad  *    documentation and/or other materials provided with the distribution.
     18       1.1        ad  *
     19       1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1        ad  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1        ad  */
     31       1.1        ad 
     32       1.1        ad /*
     33       1.1        ad  * Copyright (c) 1982, 1986, 1989, 1993
     34       1.1        ad  *	The Regents of the University of California.  All rights reserved.
     35       1.1        ad  * (c) UNIX System Laboratories, Inc.
     36       1.1        ad  * All or some portions of this file are derived from material licensed
     37       1.1        ad  * to the University of California by American Telephone and Telegraph
     38       1.1        ad  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     39       1.1        ad  * the permission of UNIX System Laboratories, Inc.
     40       1.1        ad  *
     41       1.1        ad  * Redistribution and use in source and binary forms, with or without
     42       1.1        ad  * modification, are permitted provided that the following conditions
     43       1.1        ad  * are met:
     44       1.1        ad  * 1. Redistributions of source code must retain the above copyright
     45       1.1        ad  *    notice, this list of conditions and the following disclaimer.
     46       1.1        ad  * 2. Redistributions in binary form must reproduce the above copyright
     47       1.1        ad  *    notice, this list of conditions and the following disclaimer in the
     48       1.1        ad  *    documentation and/or other materials provided with the distribution.
     49       1.1        ad  * 3. Neither the name of the University nor the names of its contributors
     50       1.1        ad  *    may be used to endorse or promote products derived from this software
     51       1.1        ad  *    without specific prior written permission.
     52       1.1        ad  *
     53       1.1        ad  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54       1.1        ad  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55       1.1        ad  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56       1.1        ad  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57       1.1        ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58       1.1        ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59       1.1        ad  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60       1.1        ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61       1.1        ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62       1.1        ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63       1.1        ad  * SUCH DAMAGE.
     64       1.1        ad  *
     65       1.1        ad  *	@(#)sys_generic.c	8.9 (Berkeley) 2/14/95
     66       1.1        ad  */
     67       1.1        ad 
     68       1.1        ad /*
     69      1.21     rmind  * System calls of synchronous I/O multiplexing subsystem.
     70      1.21     rmind  *
     71      1.21     rmind  * Locking
     72      1.21     rmind  *
     73  1.21.4.1     rmind  * Two locks are used: <object-lock> and selcluster_t::sc_lock.
     74      1.21     rmind  *
     75      1.21     rmind  * The <object-lock> might be a device driver or another subsystem, e.g.
     76      1.21     rmind  * socket or pipe.  This lock is not exported, and thus invisible to this
     77      1.21     rmind  * subsystem.  Mainly, synchronisation between selrecord() and selnotify()
     78      1.21     rmind  * routines depends on this lock, as it will be described in the comments.
     79      1.21     rmind  *
     80      1.21     rmind  * Lock order
     81      1.21     rmind  *
     82      1.21     rmind  *	<object-lock> ->
     83  1.21.4.1     rmind  *		selcluster_t::sc_lock
     84       1.1        ad  */
     85       1.1        ad 
     86       1.1        ad #include <sys/cdefs.h>
     87  1.21.4.2     rmind __KERNEL_RCSID(0, "$NetBSD: sys_select.c,v 1.21.4.3 2011/04/21 01:42:09 rmind Exp $");
     88       1.1        ad 
     89       1.1        ad #include <sys/param.h>
     90       1.1        ad #include <sys/systm.h>
     91       1.1        ad #include <sys/filedesc.h>
     92       1.1        ad #include <sys/file.h>
     93       1.1        ad #include <sys/proc.h>
     94       1.1        ad #include <sys/socketvar.h>
     95       1.1        ad #include <sys/signalvar.h>
     96       1.1        ad #include <sys/uio.h>
     97       1.1        ad #include <sys/kernel.h>
     98  1.21.4.2     rmind #include <sys/lwp.h>
     99       1.1        ad #include <sys/poll.h>
    100       1.1        ad #include <sys/mount.h>
    101       1.1        ad #include <sys/syscallargs.h>
    102       1.1        ad #include <sys/cpu.h>
    103       1.1        ad #include <sys/atomic.h>
    104       1.1        ad #include <sys/socketvar.h>
    105       1.1        ad #include <sys/sleepq.h>
    106       1.1        ad 
    107       1.1        ad /* Flags for lwp::l_selflag. */
    108       1.1        ad #define	SEL_RESET	0	/* awoken, interrupted, or not yet polling */
    109       1.1        ad #define	SEL_SCANNING	1	/* polling descriptors */
    110  1.21.4.2     rmind #define	SEL_BLOCKING	2	/* blocking and waiting for event */
    111  1.21.4.2     rmind #define	SEL_EVENT	3	/* interrupted, events set directly */
    112  1.21.4.2     rmind 
    113  1.21.4.2     rmind /* Operations: either select() or poll(). */
    114  1.21.4.2     rmind #define	SELOP_SELECT	1
    115  1.21.4.2     rmind #define	SELOP_POLL	2
    116       1.1        ad 
    117  1.21.4.1     rmind /*
    118  1.21.4.1     rmind  * Per-cluster state for select()/poll().  For a system with fewer
    119  1.21.4.1     rmind  * than 32 CPUs, this gives us per-CPU clusters.
    120  1.21.4.1     rmind  */
    121  1.21.4.1     rmind #define	SELCLUSTERS	32
    122  1.21.4.1     rmind #define	SELCLUSTERMASK	(SELCLUSTERS - 1)
    123  1.21.4.1     rmind 
    124  1.21.4.1     rmind typedef struct selcluster {
    125      1.13        ad 	kmutex_t	*sc_lock;
    126       1.1        ad 	sleepq_t	sc_sleepq;
    127       1.1        ad 	int		sc_ncoll;
    128       1.1        ad 	uint32_t	sc_mask;
    129  1.21.4.1     rmind } selcluster_t;
    130       1.1        ad 
    131  1.21.4.2     rmind static inline int	selscan(char *, const int, const size_t, register_t *);
    132  1.21.4.2     rmind static inline int	pollscan(struct pollfd *, const int, register_t *);
    133      1.19     rmind static void		selclear(void);
    134       1.1        ad 
    135  1.21.4.2     rmind static const int sel_flag[] = {
    136  1.21.4.2     rmind 	POLLRDNORM | POLLHUP | POLLERR,
    137  1.21.4.2     rmind 	POLLWRNORM | POLLHUP | POLLERR,
    138  1.21.4.2     rmind 	POLLRDBAND
    139  1.21.4.2     rmind };
    140  1.21.4.2     rmind 
    141       1.1        ad static syncobj_t select_sobj = {
    142       1.1        ad 	SOBJ_SLEEPQ_FIFO,
    143       1.1        ad 	sleepq_unsleep,
    144       1.1        ad 	sleepq_changepri,
    145       1.1        ad 	sleepq_lendpri,
    146       1.1        ad 	syncobj_noowner,
    147       1.1        ad };
    148       1.1        ad 
    149  1.21.4.2     rmind static selcluster_t	*selcluster[SELCLUSTERS] __read_mostly;
    150  1.21.4.1     rmind 
    151       1.1        ad /*
    152       1.1        ad  * Select system call.
    153       1.1        ad  */
    154       1.1        ad int
    155      1.12  christos sys___pselect50(struct lwp *l, const struct sys___pselect50_args *uap,
    156      1.12  christos     register_t *retval)
    157       1.1        ad {
    158       1.1        ad 	/* {
    159       1.1        ad 		syscallarg(int)				nd;
    160       1.1        ad 		syscallarg(fd_set *)			in;
    161       1.1        ad 		syscallarg(fd_set *)			ou;
    162       1.1        ad 		syscallarg(fd_set *)			ex;
    163       1.1        ad 		syscallarg(const struct timespec *)	ts;
    164       1.1        ad 		syscallarg(sigset_t *)			mask;
    165       1.1        ad 	} */
    166      1.14  christos 	struct timespec	ats, *ts = NULL;
    167       1.1        ad 	sigset_t	amask, *mask = NULL;
    168       1.1        ad 	int		error;
    169       1.1        ad 
    170       1.1        ad 	if (SCARG(uap, ts)) {
    171       1.1        ad 		error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
    172       1.1        ad 		if (error)
    173       1.1        ad 			return error;
    174      1.14  christos 		ts = &ats;
    175       1.1        ad 	}
    176       1.1        ad 	if (SCARG(uap, mask) != NULL) {
    177       1.1        ad 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
    178       1.1        ad 		if (error)
    179       1.1        ad 			return error;
    180       1.1        ad 		mask = &amask;
    181       1.1        ad 	}
    182       1.1        ad 
    183      1.19     rmind 	return selcommon(retval, SCARG(uap, nd), SCARG(uap, in),
    184      1.14  christos 	    SCARG(uap, ou), SCARG(uap, ex), ts, mask);
    185       1.1        ad }
    186       1.1        ad 
    187       1.1        ad int
    188      1.12  christos sys___select50(struct lwp *l, const struct sys___select50_args *uap,
    189      1.12  christos     register_t *retval)
    190       1.1        ad {
    191       1.1        ad 	/* {
    192       1.1        ad 		syscallarg(int)			nd;
    193       1.1        ad 		syscallarg(fd_set *)		in;
    194       1.1        ad 		syscallarg(fd_set *)		ou;
    195       1.1        ad 		syscallarg(fd_set *)		ex;
    196       1.1        ad 		syscallarg(struct timeval *)	tv;
    197       1.1        ad 	} */
    198      1.14  christos 	struct timeval atv;
    199      1.14  christos 	struct timespec ats, *ts = NULL;
    200       1.1        ad 	int error;
    201       1.1        ad 
    202       1.1        ad 	if (SCARG(uap, tv)) {
    203      1.14  christos 		error = copyin(SCARG(uap, tv), (void *)&atv, sizeof(atv));
    204       1.1        ad 		if (error)
    205       1.1        ad 			return error;
    206      1.14  christos 		TIMEVAL_TO_TIMESPEC(&atv, &ats);
    207      1.14  christos 		ts = &ats;
    208       1.1        ad 	}
    209       1.1        ad 
    210      1.19     rmind 	return selcommon(retval, SCARG(uap, nd), SCARG(uap, in),
    211      1.14  christos 	    SCARG(uap, ou), SCARG(uap, ex), ts, NULL);
    212       1.1        ad }
    213       1.1        ad 
    214      1.17     rmind /*
    215      1.17     rmind  * sel_do_scan: common code to perform the scan on descriptors.
    216      1.17     rmind  */
    217      1.17     rmind static int
    218  1.21.4.2     rmind sel_do_scan(const int op, void *fds, const int nf, const size_t ni,
    219  1.21.4.2     rmind     struct timespec *ts, sigset_t *mask, register_t *retval)
    220       1.1        ad {
    221      1.17     rmind 	lwp_t		* const l = curlwp;
    222       1.1        ad 	proc_t		* const p = l->l_proc;
    223  1.21.4.1     rmind 	selcluster_t	*sc;
    224      1.13        ad 	kmutex_t	*lock;
    225      1.17     rmind 	sigset_t	oldmask;
    226      1.17     rmind 	struct timespec	sleepts;
    227      1.17     rmind 	int		error, timo;
    228       1.1        ad 
    229       1.1        ad 	timo = 0;
    230      1.14  christos 	if (ts && inittimeleft(ts, &sleepts) == -1) {
    231      1.17     rmind 		return EINVAL;
    232       1.1        ad 	}
    233       1.1        ad 
    234      1.17     rmind 	if (__predict_false(mask)) {
    235       1.1        ad 		sigminusset(&sigcantmask, mask);
    236       1.5        ad 		mutex_enter(p->p_lock);
    237       1.1        ad 		oldmask = l->l_sigmask;
    238       1.1        ad 		l->l_sigmask = *mask;
    239       1.5        ad 		mutex_exit(p->p_lock);
    240      1.17     rmind 	} else {
    241      1.17     rmind 		/* XXXgcc */
    242      1.17     rmind 		oldmask = l->l_sigmask;
    243      1.17     rmind 	}
    244       1.1        ad 
    245  1.21.4.1     rmind 	sc = curcpu()->ci_data.cpu_selcluster;
    246      1.13        ad 	lock = sc->sc_lock;
    247  1.21.4.1     rmind 	l->l_selcluster = sc;
    248       1.1        ad 	SLIST_INIT(&l->l_selwait);
    249  1.21.4.2     rmind 
    250  1.21.4.2     rmind 	l->l_selret = 0;
    251  1.21.4.2     rmind 	if (op == SELOP_SELECT) {
    252  1.21.4.3     rmind 		l->l_selbits = fds;
    253  1.21.4.2     rmind 		l->l_selni = ni;
    254  1.21.4.2     rmind 	} else {
    255  1.21.4.2     rmind 		l->l_selbits = NULL;
    256  1.21.4.2     rmind 	}
    257       1.1        ad 	for (;;) {
    258      1.17     rmind 		int ncoll;
    259      1.17     rmind 
    260       1.1        ad 		/*
    261      1.17     rmind 		 * No need to lock.  If this is overwritten by another value
    262      1.17     rmind 		 * while scanning, we will retry below.  We only need to see
    263      1.17     rmind 		 * exact state from the descriptors that we are about to poll,
    264      1.17     rmind 		 * and lock activity resulting from fo_poll is enough to
    265      1.17     rmind 		 * provide an up to date value for new polling activity.
    266       1.1        ad 		 */
    267      1.17     rmind 		l->l_selflag = SEL_SCANNING;
    268       1.1        ad 		ncoll = sc->sc_ncoll;
    269       1.1        ad 
    270  1.21.4.2     rmind 		if (op == SELOP_SELECT) {
    271  1.21.4.2     rmind 			error = selscan((char *)fds, nf, ni, retval);
    272      1.17     rmind 		} else {
    273  1.21.4.2     rmind 			error = pollscan((struct pollfd *)fds, nf, retval);
    274      1.17     rmind 		}
    275       1.1        ad 		if (error || *retval)
    276       1.1        ad 			break;
    277      1.14  christos 		if (ts && (timo = gettimeleft(ts, &sleepts)) <= 0)
    278       1.1        ad 			break;
    279  1.21.4.2     rmind 		/*
    280  1.21.4.2     rmind 		 * Acquire the lock and perform the (re)checks.  Note, if
    281  1.21.4.2     rmind 		 * collision has occured, then our state does not matter,
    282  1.21.4.2     rmind 		 * as we must perform re-scan.  Therefore, check it first.
    283  1.21.4.2     rmind 		 */
    284  1.21.4.2     rmind state_check:
    285      1.13        ad 		mutex_spin_enter(lock);
    286  1.21.4.2     rmind 		if (__predict_false(sc->sc_ncoll != ncoll)) {
    287  1.21.4.2     rmind 			/* Collision: perform re-scan. */
    288  1.21.4.2     rmind 			mutex_spin_exit(lock);
    289  1.21.4.2     rmind 			continue;
    290  1.21.4.2     rmind 		}
    291  1.21.4.2     rmind 		if (__predict_true(l->l_selflag == SEL_EVENT)) {
    292  1.21.4.2     rmind 			/* Events occured, they are set directly. */
    293  1.21.4.2     rmind 			mutex_spin_exit(lock);
    294  1.21.4.2     rmind 			KASSERT(l->l_selret != 0);
    295  1.21.4.2     rmind 			*retval = l->l_selret;
    296  1.21.4.2     rmind 			break;
    297  1.21.4.2     rmind 		}
    298  1.21.4.2     rmind 		if (__predict_true(l->l_selflag == SEL_RESET)) {
    299  1.21.4.2     rmind 			/* Events occured, but re-scan is requested. */
    300      1.13        ad 			mutex_spin_exit(lock);
    301       1.1        ad 			continue;
    302       1.1        ad 		}
    303  1.21.4.2     rmind 		/* Nothing happen, therefore - sleep. */
    304       1.1        ad 		l->l_selflag = SEL_BLOCKING;
    305       1.7        ad 		l->l_kpriority = true;
    306      1.13        ad 		sleepq_enter(&sc->sc_sleepq, l, lock);
    307       1.1        ad 		sleepq_enqueue(&sc->sc_sleepq, sc, "select", &select_sobj);
    308       1.1        ad 		error = sleepq_block(timo, true);
    309  1.21.4.2     rmind 		if (error != 0) {
    310       1.1        ad 			break;
    311  1.21.4.2     rmind 		}
    312  1.21.4.2     rmind 		/* Awoken: need to check the state. */
    313  1.21.4.2     rmind 		goto state_check;
    314       1.1        ad 	}
    315       1.1        ad 	selclear();
    316       1.1        ad 
    317      1.17     rmind 	if (__predict_false(mask)) {
    318       1.5        ad 		mutex_enter(p->p_lock);
    319       1.1        ad 		l->l_sigmask = oldmask;
    320       1.5        ad 		mutex_exit(p->p_lock);
    321       1.1        ad 	}
    322      1.20       dsl 
    323      1.20       dsl 	/* select and poll are not restarted after signals... */
    324      1.20       dsl 	if (error == ERESTART)
    325      1.20       dsl 		return EINTR;
    326      1.20       dsl 	if (error == EWOULDBLOCK)
    327      1.20       dsl 		return 0;
    328      1.17     rmind 	return error;
    329      1.17     rmind }
    330      1.17     rmind 
    331      1.17     rmind int
    332      1.19     rmind selcommon(register_t *retval, int nd, fd_set *u_in, fd_set *u_ou,
    333      1.19     rmind     fd_set *u_ex, struct timespec *ts, sigset_t *mask)
    334      1.17     rmind {
    335      1.17     rmind 	char		smallbits[howmany(FD_SETSIZE, NFDBITS) *
    336      1.17     rmind 			    sizeof(fd_mask) * 6];
    337      1.17     rmind 	char 		*bits;
    338      1.17     rmind 	int		error, nf;
    339      1.17     rmind 	size_t		ni;
    340      1.17     rmind 
    341      1.17     rmind 	if (nd < 0)
    342      1.17     rmind 		return (EINVAL);
    343      1.19     rmind 	nf = curlwp->l_fd->fd_dt->dt_nfiles;
    344      1.17     rmind 	if (nd > nf) {
    345      1.17     rmind 		/* forgiving; slightly wrong */
    346      1.17     rmind 		nd = nf;
    347      1.17     rmind 	}
    348      1.17     rmind 	ni = howmany(nd, NFDBITS) * sizeof(fd_mask);
    349      1.17     rmind 	if (ni * 6 > sizeof(smallbits)) {
    350      1.17     rmind 		bits = kmem_alloc(ni * 6, KM_SLEEP);
    351      1.17     rmind 		if (bits == NULL)
    352      1.17     rmind 			return ENOMEM;
    353      1.17     rmind 	} else
    354      1.17     rmind 		bits = smallbits;
    355      1.17     rmind 
    356      1.17     rmind #define	getbits(name, x)						\
    357      1.17     rmind 	if (u_ ## name) {						\
    358      1.17     rmind 		error = copyin(u_ ## name, bits + ni * x, ni);		\
    359      1.17     rmind 		if (error)						\
    360      1.20       dsl 			goto fail;					\
    361      1.17     rmind 	} else								\
    362      1.17     rmind 		memset(bits + ni * x, 0, ni);
    363      1.17     rmind 	getbits(in, 0);
    364      1.17     rmind 	getbits(ou, 1);
    365      1.17     rmind 	getbits(ex, 2);
    366      1.17     rmind #undef	getbits
    367       1.1        ad 
    368  1.21.4.2     rmind 	error = sel_do_scan(SELOP_SELECT, bits, nd, ni, ts, mask, retval);
    369       1.1        ad 	if (error == 0 && u_in != NULL)
    370       1.1        ad 		error = copyout(bits + ni * 3, u_in, ni);
    371       1.1        ad 	if (error == 0 && u_ou != NULL)
    372       1.1        ad 		error = copyout(bits + ni * 4, u_ou, ni);
    373       1.1        ad 	if (error == 0 && u_ex != NULL)
    374       1.1        ad 		error = copyout(bits + ni * 5, u_ex, ni);
    375      1.20       dsl  fail:
    376       1.1        ad 	if (bits != smallbits)
    377       1.1        ad 		kmem_free(bits, ni * 6);
    378       1.1        ad 	return (error);
    379       1.1        ad }
    380       1.1        ad 
    381      1.19     rmind static inline int
    382  1.21.4.2     rmind selscan(char *bits, const int nfd, const size_t ni, register_t *retval)
    383       1.1        ad {
    384      1.17     rmind 	fd_mask *ibitp, *obitp;
    385  1.21.4.2     rmind 	int msk, i, j, fd, n;
    386       1.1        ad 	file_t *fp;
    387       1.1        ad 
    388      1.17     rmind 	ibitp = (fd_mask *)(bits + ni * 0);
    389      1.17     rmind 	obitp = (fd_mask *)(bits + ni * 3);
    390       1.1        ad 	n = 0;
    391      1.17     rmind 
    392       1.1        ad 	for (msk = 0; msk < 3; msk++) {
    393       1.1        ad 		for (i = 0; i < nfd; i += NFDBITS) {
    394  1.21.4.2     rmind 			fd_mask ibits, obits;
    395  1.21.4.2     rmind 
    396       1.1        ad 			ibits = *ibitp++;
    397       1.1        ad 			obits = 0;
    398       1.1        ad 			while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
    399       1.1        ad 				ibits &= ~(1 << j);
    400       1.1        ad 				if ((fp = fd_getfile(fd)) == NULL)
    401       1.1        ad 					return (EBADF);
    402  1.21.4.2     rmind 				/*
    403  1.21.4.2     rmind 				 * Setup an argument to selrecord(), which is
    404  1.21.4.2     rmind 				 * a file descriptor number.
    405  1.21.4.2     rmind 				 */
    406  1.21.4.2     rmind 				curlwp->l_selrec = fd;
    407  1.21.4.2     rmind 				if ((*fp->f_ops->fo_poll)(fp, sel_flag[msk])) {
    408       1.1        ad 					obits |= (1 << j);
    409       1.1        ad 					n++;
    410       1.1        ad 				}
    411       1.1        ad 				fd_putfile(fd);
    412       1.1        ad 			}
    413       1.1        ad 			*obitp++ = obits;
    414       1.1        ad 		}
    415       1.1        ad 	}
    416       1.1        ad 	*retval = n;
    417       1.1        ad 	return (0);
    418       1.1        ad }
    419       1.1        ad 
    420       1.1        ad /*
    421       1.1        ad  * Poll system call.
    422       1.1        ad  */
    423       1.1        ad int
    424       1.1        ad sys_poll(struct lwp *l, const struct sys_poll_args *uap, register_t *retval)
    425       1.1        ad {
    426       1.1        ad 	/* {
    427       1.1        ad 		syscallarg(struct pollfd *)	fds;
    428       1.1        ad 		syscallarg(u_int)		nfds;
    429       1.1        ad 		syscallarg(int)			timeout;
    430       1.1        ad 	} */
    431      1.14  christos 	struct timespec	ats, *ts = NULL;
    432       1.1        ad 
    433       1.1        ad 	if (SCARG(uap, timeout) != INFTIM) {
    434      1.14  christos 		ats.tv_sec = SCARG(uap, timeout) / 1000;
    435      1.14  christos 		ats.tv_nsec = (SCARG(uap, timeout) % 1000) * 1000000;
    436      1.14  christos 		ts = &ats;
    437       1.1        ad 	}
    438       1.1        ad 
    439      1.19     rmind 	return pollcommon(retval, SCARG(uap, fds), SCARG(uap, nfds), ts, NULL);
    440       1.1        ad }
    441       1.1        ad 
    442       1.1        ad /*
    443       1.1        ad  * Poll system call.
    444       1.1        ad  */
    445       1.1        ad int
    446      1.12  christos sys___pollts50(struct lwp *l, const struct sys___pollts50_args *uap,
    447      1.12  christos     register_t *retval)
    448       1.1        ad {
    449       1.1        ad 	/* {
    450       1.1        ad 		syscallarg(struct pollfd *)		fds;
    451       1.1        ad 		syscallarg(u_int)			nfds;
    452       1.1        ad 		syscallarg(const struct timespec *)	ts;
    453       1.1        ad 		syscallarg(const sigset_t *)		mask;
    454       1.1        ad 	} */
    455      1.14  christos 	struct timespec	ats, *ts = NULL;
    456       1.1        ad 	sigset_t	amask, *mask = NULL;
    457       1.1        ad 	int		error;
    458       1.1        ad 
    459       1.1        ad 	if (SCARG(uap, ts)) {
    460       1.1        ad 		error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
    461       1.1        ad 		if (error)
    462       1.1        ad 			return error;
    463      1.14  christos 		ts = &ats;
    464       1.1        ad 	}
    465       1.1        ad 	if (SCARG(uap, mask)) {
    466       1.1        ad 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
    467       1.1        ad 		if (error)
    468       1.1        ad 			return error;
    469       1.1        ad 		mask = &amask;
    470       1.1        ad 	}
    471       1.1        ad 
    472      1.19     rmind 	return pollcommon(retval, SCARG(uap, fds), SCARG(uap, nfds), ts, mask);
    473       1.1        ad }
    474       1.1        ad 
    475       1.1        ad int
    476      1.19     rmind pollcommon(register_t *retval, struct pollfd *u_fds, u_int nfds,
    477      1.14  christos     struct timespec *ts, sigset_t *mask)
    478       1.1        ad {
    479      1.11      yamt 	struct pollfd	smallfds[32];
    480      1.11      yamt 	struct pollfd	*fds;
    481      1.17     rmind 	int		error;
    482      1.20       dsl 	size_t		ni;
    483       1.1        ad 
    484      1.20       dsl 	if (nfds > 1000 + curlwp->l_fd->fd_dt->dt_nfiles) {
    485      1.20       dsl 		/*
    486      1.20       dsl 		 * Either the user passed in a very sparse 'fds' or junk!
    487      1.20       dsl 		 * The kmem_alloc() call below would be bad news.
    488      1.20       dsl 		 * We could process the 'fds' array in chunks, but that
    489      1.20       dsl 		 * is a lot of code that isn't normally useful.
    490      1.20       dsl 		 * (Or just move the copyin/out into pollscan().)
    491      1.20       dsl 		 * Historically the code silently truncated 'fds' to
    492      1.20       dsl 		 * dt_nfiles entries - but that does cause issues.
    493      1.20       dsl 		 */
    494      1.20       dsl 		return EINVAL;
    495       1.1        ad 	}
    496       1.1        ad 	ni = nfds * sizeof(struct pollfd);
    497      1.11      yamt 	if (ni > sizeof(smallfds)) {
    498      1.11      yamt 		fds = kmem_alloc(ni, KM_SLEEP);
    499      1.11      yamt 		if (fds == NULL)
    500       1.9     rmind 			return ENOMEM;
    501       1.9     rmind 	} else
    502      1.11      yamt 		fds = smallfds;
    503       1.1        ad 
    504      1.11      yamt 	error = copyin(u_fds, fds, ni);
    505       1.1        ad 	if (error)
    506      1.20       dsl 		goto fail;
    507       1.1        ad 
    508  1.21.4.2     rmind 	error = sel_do_scan(SELOP_POLL, fds, nfds, ni, ts, mask, retval);
    509       1.1        ad 	if (error == 0)
    510      1.11      yamt 		error = copyout(fds, u_fds, ni);
    511      1.20       dsl  fail:
    512      1.11      yamt 	if (fds != smallfds)
    513      1.11      yamt 		kmem_free(fds, ni);
    514       1.1        ad 	return (error);
    515       1.1        ad }
    516       1.1        ad 
    517      1.19     rmind static inline int
    518  1.21.4.2     rmind pollscan(struct pollfd *fds, const int nfd, register_t *retval)
    519       1.1        ad {
    520       1.1        ad 	file_t *fp;
    521  1.21.4.2     rmind 	int i, n = 0;
    522       1.1        ad 
    523       1.1        ad 	for (i = 0; i < nfd; i++, fds++) {
    524       1.1        ad 		if (fds->fd < 0) {
    525       1.1        ad 			fds->revents = 0;
    526       1.1        ad 		} else if ((fp = fd_getfile(fds->fd)) == NULL) {
    527       1.1        ad 			fds->revents = POLLNVAL;
    528       1.1        ad 			n++;
    529       1.1        ad 		} else {
    530  1.21.4.2     rmind 			/*
    531  1.21.4.2     rmind 			 * Perform poll: registers select request or returns
    532  1.21.4.2     rmind 			 * the events which are set.  Setup an argument for
    533  1.21.4.2     rmind 			 * selrecord(), which is a pointer to struct pollfd.
    534  1.21.4.2     rmind 			 */
    535  1.21.4.2     rmind 			curlwp->l_selrec = (uintptr_t)fds;
    536       1.1        ad 			fds->revents = (*fp->f_ops->fo_poll)(fp,
    537       1.1        ad 			    fds->events | POLLERR | POLLHUP);
    538       1.1        ad 			if (fds->revents != 0)
    539       1.1        ad 				n++;
    540       1.1        ad 			fd_putfile(fds->fd);
    541       1.1        ad 		}
    542       1.1        ad 	}
    543       1.1        ad 	*retval = n;
    544       1.1        ad 	return (0);
    545       1.1        ad }
    546       1.1        ad 
    547       1.1        ad int
    548       1.1        ad seltrue(dev_t dev, int events, lwp_t *l)
    549       1.1        ad {
    550       1.1        ad 
    551       1.1        ad 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
    552       1.1        ad }
    553       1.1        ad 
    554       1.1        ad /*
    555       1.1        ad  * Record a select request.  Concurrency issues:
    556       1.1        ad  *
    557       1.1        ad  * The caller holds the same lock across calls to selrecord() and
    558       1.4      yamt  * selnotify(), so we don't need to consider a concurrent wakeup
    559       1.1        ad  * while in this routine.
    560       1.1        ad  *
    561       1.1        ad  * The only activity we need to guard against is selclear(), called by
    562      1.17     rmind  * another thread that is exiting sel_do_scan().
    563       1.1        ad  * `sel_lwp' can only become non-NULL while the caller's lock is held,
    564       1.1        ad  * so it cannot become non-NULL due to a change made by another thread
    565       1.1        ad  * while we are in this routine.  It can only become _NULL_ due to a
    566       1.1        ad  * call to selclear().
    567       1.1        ad  *
    568       1.1        ad  * If it is non-NULL and != selector there is the potential for
    569       1.1        ad  * selclear() to be called by another thread.  If either of those
    570       1.1        ad  * conditions are true, we're not interested in touching the `named
    571       1.1        ad  * waiter' part of the selinfo record because we need to record a
    572       1.1        ad  * collision.  Hence there is no need for additional locking in this
    573       1.1        ad  * routine.
    574       1.1        ad  */
    575       1.1        ad void
    576       1.1        ad selrecord(lwp_t *selector, struct selinfo *sip)
    577       1.1        ad {
    578  1.21.4.1     rmind 	selcluster_t *sc;
    579       1.1        ad 	lwp_t *other;
    580       1.1        ad 
    581       1.1        ad 	KASSERT(selector == curlwp);
    582       1.1        ad 
    583  1.21.4.1     rmind 	sc = selector->l_selcluster;
    584       1.1        ad 	other = sip->sel_lwp;
    585       1.1        ad 
    586       1.1        ad 	if (other == selector) {
    587  1.21.4.2     rmind 		/* 1. We (selector) already claimed to be the first LWP. */
    588  1.21.4.1     rmind 		KASSERT(sip->sel_cluster = sc);
    589       1.1        ad 	} else if (other == NULL) {
    590       1.1        ad 		/*
    591  1.21.4.2     rmind 		 * 2. No first LWP, therefore we (selector) are the first.
    592  1.21.4.2     rmind 		 *
    593  1.21.4.2     rmind 		 * There may be unnamed waiters (collisions).  Issue a memory
    594  1.21.4.2     rmind 		 * barrier to ensure that we access sel_lwp (above) before
    595  1.21.4.2     rmind 		 * other fields - this guards against a call to selclear().
    596       1.1        ad 		 */
    597       1.1        ad 		membar_enter();
    598       1.1        ad 		sip->sel_lwp = selector;
    599       1.1        ad 		SLIST_INSERT_HEAD(&selector->l_selwait, sip, sel_chain);
    600  1.21.4.2     rmind 		/* Copy the argument, which is for selnotify(). */
    601  1.21.4.2     rmind 		sip->sel_fdinfo = selector->l_selrec;
    602  1.21.4.1     rmind 		/* Replace selinfo's lock with the chosen cluster's lock. */
    603  1.21.4.1     rmind 		sip->sel_cluster = sc;
    604       1.1        ad 	} else {
    605  1.21.4.2     rmind 		/* 3. Multiple waiters: record a collision. */
    606       1.1        ad 		sip->sel_collision |= sc->sc_mask;
    607  1.21.4.1     rmind 		KASSERT(sip->sel_cluster != NULL);
    608       1.1        ad 	}
    609       1.1        ad }
    610       1.1        ad 
    611       1.1        ad /*
    612  1.21.4.2     rmind  * sel_setevents: a helper function for selnotify(), to set the events
    613  1.21.4.2     rmind  * for LWP sleeping in selcommon() or pollcommon().
    614  1.21.4.2     rmind  */
    615  1.21.4.3     rmind static inline bool
    616  1.21.4.2     rmind sel_setevents(lwp_t *l, struct selinfo *sip, const int events)
    617  1.21.4.2     rmind {
    618  1.21.4.2     rmind 	const int oflag = l->l_selflag;
    619  1.21.4.3     rmind 	int ret = 0;
    620  1.21.4.2     rmind 
    621  1.21.4.2     rmind 	/*
    622  1.21.4.2     rmind 	 * If we require re-scan or it was required by somebody else,
    623  1.21.4.2     rmind 	 * then just (re)set SEL_RESET and return.
    624  1.21.4.2     rmind 	 */
    625  1.21.4.2     rmind 	if (__predict_false(events == 0 || oflag == SEL_RESET)) {
    626  1.21.4.2     rmind 		l->l_selflag = SEL_RESET;
    627  1.21.4.3     rmind 		return true;
    628  1.21.4.2     rmind 	}
    629  1.21.4.2     rmind 	/*
    630  1.21.4.2     rmind 	 * Direct set.  Note: select state of LWP is locked.  First,
    631  1.21.4.2     rmind 	 * determine whether it is selcommon() or pollcommon().
    632  1.21.4.2     rmind 	 */
    633  1.21.4.2     rmind 	if (l->l_selbits != NULL) {
    634  1.21.4.2     rmind 		const size_t ni = l->l_selni;
    635  1.21.4.3     rmind 		fd_mask *fds = (fd_mask *)l->l_selbits;
    636  1.21.4.3     rmind 		fd_mask *ofds = (fd_mask *)((char *)fds + ni * 3);
    637  1.21.4.3     rmind 		const int fd = sip->sel_fdinfo, fbit = 1 << (fd & __NFDMASK);
    638  1.21.4.2     rmind 		const int idx = fd >> __NFDSHIFT;
    639  1.21.4.2     rmind 		int n;
    640  1.21.4.2     rmind 
    641  1.21.4.2     rmind 		for (n = 0; n < 3; n++) {
    642  1.21.4.3     rmind 			if ((fds[idx] & fbit) != 0 && (sel_flag[n] & events)) {
    643  1.21.4.3     rmind 				ofds[idx] |= fbit;
    644  1.21.4.3     rmind 				ret++;
    645  1.21.4.2     rmind 			}
    646  1.21.4.2     rmind 			fds = (fd_mask *)((char *)fds + ni);
    647  1.21.4.3     rmind 			ofds = (fd_mask *)((char *)ofds + ni);
    648  1.21.4.2     rmind 		}
    649  1.21.4.2     rmind 	} else {
    650  1.21.4.2     rmind 		struct pollfd *pfd = (void *)sip->sel_fdinfo;
    651  1.21.4.3     rmind 		int revents = events & (pfd->events | POLLERR | POLLHUP);
    652  1.21.4.3     rmind 
    653  1.21.4.3     rmind 		if (revents) {
    654  1.21.4.3     rmind 			pfd->revents |= revents;
    655  1.21.4.3     rmind 			ret = 1;
    656  1.21.4.3     rmind 		}
    657  1.21.4.3     rmind 	}
    658  1.21.4.3     rmind 	/* Check whether there are any events to return. */
    659  1.21.4.3     rmind 	if (!ret) {
    660  1.21.4.3     rmind 		return false;
    661  1.21.4.2     rmind 	}
    662  1.21.4.2     rmind 	/* Indicate direct set and note the event (cluster lock is held). */
    663  1.21.4.2     rmind 	l->l_selflag = SEL_EVENT;
    664  1.21.4.3     rmind 	l->l_selret += ret;
    665  1.21.4.3     rmind 	return true;
    666  1.21.4.2     rmind }
    667  1.21.4.2     rmind 
    668  1.21.4.2     rmind /*
    669       1.1        ad  * Do a wakeup when a selectable event occurs.  Concurrency issues:
    670       1.1        ad  *
    671       1.1        ad  * As per selrecord(), the caller's object lock is held.  If there
    672  1.21.4.1     rmind  * is a named waiter, we must acquire the associated selcluster's lock
    673       1.1        ad  * in order to synchronize with selclear() and pollers going to sleep
    674      1.17     rmind  * in sel_do_scan().
    675       1.1        ad  *
    676  1.21.4.1     rmind  * sip->sel_cluser cannot change at this point, as it is only changed
    677       1.1        ad  * in selrecord(), and concurrent calls to selrecord() are locked
    678       1.1        ad  * out by the caller.
    679       1.1        ad  */
    680       1.1        ad void
    681       1.1        ad selnotify(struct selinfo *sip, int events, long knhint)
    682       1.1        ad {
    683  1.21.4.1     rmind 	selcluster_t *sc;
    684       1.1        ad 	uint32_t mask;
    685      1.16     rmind 	int index, oflag;
    686       1.1        ad 	lwp_t *l;
    687      1.13        ad 	kmutex_t *lock;
    688       1.1        ad 
    689       1.1        ad 	KNOTE(&sip->sel_klist, knhint);
    690       1.1        ad 
    691       1.1        ad 	if (sip->sel_lwp != NULL) {
    692       1.1        ad 		/* One named LWP is waiting. */
    693  1.21.4.1     rmind 		sc = sip->sel_cluster;
    694      1.13        ad 		lock = sc->sc_lock;
    695      1.13        ad 		mutex_spin_enter(lock);
    696       1.1        ad 		/* Still there? */
    697       1.1        ad 		if (sip->sel_lwp != NULL) {
    698  1.21.4.2     rmind 			/*
    699  1.21.4.2     rmind 			 * Set the events for our LWP and indicate that.
    700  1.21.4.2     rmind 			 * Otherwise, request for a full re-scan.
    701  1.21.4.2     rmind 			 */
    702       1.1        ad 			l = sip->sel_lwp;
    703  1.21.4.2     rmind 			oflag = l->l_selflag;
    704  1.21.4.2     rmind #ifndef NO_DIRECT_SELECT
    705  1.21.4.3     rmind 			if (!sel_setevents(l, sip, events)) {
    706  1.21.4.3     rmind 				/* No events to return. */
    707  1.21.4.3     rmind 				mutex_spin_exit(lock);
    708  1.21.4.3     rmind 				return;
    709  1.21.4.3     rmind 			}
    710  1.21.4.2     rmind #else
    711  1.21.4.2     rmind 			l->l_selflag = SEL_RESET;
    712  1.21.4.2     rmind #endif
    713       1.1        ad 			/*
    714       1.1        ad 			 * If thread is sleeping, wake it up.  If it's not
    715       1.1        ad 			 * yet asleep, it will notice the change in state
    716       1.1        ad 			 * and will re-poll the descriptors.
    717       1.1        ad 			 */
    718      1.13        ad 			if (oflag == SEL_BLOCKING && l->l_mutex == lock) {
    719       1.1        ad 				KASSERT(l->l_wchan == sc);
    720      1.16     rmind 				sleepq_unsleep(l, false);
    721       1.1        ad 			}
    722       1.1        ad 		}
    723      1.13        ad 		mutex_spin_exit(lock);
    724       1.1        ad 	}
    725       1.1        ad 
    726       1.1        ad 	if ((mask = sip->sel_collision) != 0) {
    727       1.1        ad 		/*
    728       1.1        ad 		 * There was a collision (multiple waiters): we must
    729       1.1        ad 		 * inform all potentially interested waiters.
    730       1.1        ad 		 */
    731       1.1        ad 		sip->sel_collision = 0;
    732       1.3        ad 		do {
    733       1.1        ad 			index = ffs(mask) - 1;
    734       1.1        ad 			mask &= ~(1 << index);
    735  1.21.4.1     rmind 			sc = selcluster[index];
    736      1.13        ad 			lock = sc->sc_lock;
    737      1.13        ad 			mutex_spin_enter(lock);
    738       1.1        ad 			sc->sc_ncoll++;
    739      1.13        ad 			sleepq_wake(&sc->sc_sleepq, sc, (u_int)-1, lock);
    740       1.3        ad 		} while (__predict_false(mask != 0));
    741       1.1        ad 	}
    742       1.1        ad }
    743       1.1        ad 
    744       1.1        ad /*
    745       1.1        ad  * Remove an LWP from all objects that it is waiting for.  Concurrency
    746       1.1        ad  * issues:
    747       1.1        ad  *
    748       1.1        ad  * The object owner's (e.g. device driver) lock is not held here.  Calls
    749       1.1        ad  * can be made to selrecord() and we do not synchronize against those
    750       1.1        ad  * directly using locks.  However, we use `sel_lwp' to lock out changes.
    751       1.1        ad  * Before clearing it we must use memory barriers to ensure that we can
    752       1.1        ad  * safely traverse the list of selinfo records.
    753       1.1        ad  */
    754       1.1        ad static void
    755       1.1        ad selclear(void)
    756       1.1        ad {
    757       1.1        ad 	struct selinfo *sip, *next;
    758  1.21.4.1     rmind 	selcluster_t *sc;
    759       1.1        ad 	lwp_t *l;
    760      1.13        ad 	kmutex_t *lock;
    761       1.1        ad 
    762       1.1        ad 	l = curlwp;
    763  1.21.4.1     rmind 	sc = l->l_selcluster;
    764      1.13        ad 	lock = sc->sc_lock;
    765       1.1        ad 
    766      1.13        ad 	mutex_spin_enter(lock);
    767       1.1        ad 	for (sip = SLIST_FIRST(&l->l_selwait); sip != NULL; sip = next) {
    768       1.1        ad 		KASSERT(sip->sel_lwp == l);
    769  1.21.4.1     rmind 		KASSERT(sip->sel_cluster == l->l_selcluster);
    770  1.21.4.1     rmind 
    771       1.1        ad 		/*
    772       1.1        ad 		 * Read link to next selinfo record, if any.
    773       1.1        ad 		 * It's no longer safe to touch `sip' after clearing
    774       1.1        ad 		 * `sel_lwp', so ensure that the read of `sel_chain'
    775       1.1        ad 		 * completes before the clearing of sel_lwp becomes
    776       1.1        ad 		 * globally visible.
    777       1.1        ad 		 */
    778       1.1        ad 		next = SLIST_NEXT(sip, sel_chain);
    779       1.1        ad 		membar_exit();
    780       1.1        ad 		/* Release the record for another named waiter to use. */
    781       1.1        ad 		sip->sel_lwp = NULL;
    782       1.1        ad 	}
    783      1.13        ad 	mutex_spin_exit(lock);
    784       1.1        ad }
    785       1.1        ad 
    786       1.1        ad /*
    787       1.1        ad  * Initialize the select/poll system calls.  Called once for each
    788       1.1        ad  * CPU in the system, as they are attached.
    789       1.1        ad  */
    790       1.1        ad void
    791       1.1        ad selsysinit(struct cpu_info *ci)
    792       1.1        ad {
    793  1.21.4.1     rmind 	selcluster_t *sc;
    794  1.21.4.1     rmind 	u_int index;
    795       1.1        ad 
    796  1.21.4.1     rmind 	/* If already a cluster in place for this bit, re-use. */
    797  1.21.4.1     rmind 	index = cpu_index(ci) & SELCLUSTERMASK;
    798  1.21.4.1     rmind 	sc = selcluster[index];
    799  1.21.4.1     rmind 	if (sc == NULL) {
    800  1.21.4.1     rmind 		sc = kmem_alloc(roundup2(sizeof(selcluster_t),
    801  1.21.4.1     rmind 		    coherency_unit) + coherency_unit, KM_SLEEP);
    802  1.21.4.1     rmind 		sc = (void *)roundup2((uintptr_t)sc, coherency_unit);
    803  1.21.4.1     rmind 		sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
    804  1.21.4.1     rmind 		sleepq_init(&sc->sc_sleepq);
    805  1.21.4.1     rmind 		sc->sc_ncoll = 0;
    806  1.21.4.1     rmind 		sc->sc_mask = (1 << index);
    807  1.21.4.1     rmind 		selcluster[index] = sc;
    808  1.21.4.1     rmind 	}
    809  1.21.4.1     rmind 	ci->ci_data.cpu_selcluster = sc;
    810       1.1        ad }
    811       1.1        ad 
    812       1.1        ad /*
    813       1.1        ad  * Initialize a selinfo record.
    814       1.1        ad  */
    815       1.1        ad void
    816       1.1        ad selinit(struct selinfo *sip)
    817       1.1        ad {
    818       1.1        ad 
    819       1.1        ad 	memset(sip, 0, sizeof(*sip));
    820       1.1        ad }
    821       1.1        ad 
    822       1.1        ad /*
    823       1.1        ad  * Destroy a selinfo record.  The owning object must not gain new
    824       1.1        ad  * references while this is in progress: all activity on the record
    825       1.1        ad  * must be stopped.
    826       1.1        ad  *
    827       1.1        ad  * Concurrency issues: we only need guard against a call to selclear()
    828      1.17     rmind  * by a thread exiting sel_do_scan().  The caller has prevented further
    829      1.17     rmind  * references being made to the selinfo record via selrecord(), and it
    830  1.21.4.2     rmind  * will not call selnotify() again.
    831       1.1        ad  */
    832       1.1        ad void
    833       1.1        ad seldestroy(struct selinfo *sip)
    834       1.1        ad {
    835  1.21.4.1     rmind 	selcluster_t *sc;
    836      1.13        ad 	kmutex_t *lock;
    837       1.1        ad 	lwp_t *l;
    838       1.1        ad 
    839       1.1        ad 	if (sip->sel_lwp == NULL)
    840       1.1        ad 		return;
    841       1.1        ad 
    842       1.1        ad 	/*
    843  1.21.4.1     rmind 	 * Lock out selclear().  The selcluster pointer can't change while
    844       1.1        ad 	 * we are here since it is only ever changed in selrecord(),
    845       1.1        ad 	 * and that will not be entered again for this record because
    846       1.1        ad 	 * it is dying.
    847       1.1        ad 	 */
    848  1.21.4.1     rmind 	KASSERT(sip->sel_cluster != NULL);
    849  1.21.4.1     rmind 	sc = sip->sel_cluster;
    850      1.13        ad 	lock = sc->sc_lock;
    851      1.13        ad 	mutex_spin_enter(lock);
    852       1.1        ad 	if ((l = sip->sel_lwp) != NULL) {
    853       1.1        ad 		/*
    854       1.1        ad 		 * This should rarely happen, so although SLIST_REMOVE()
    855       1.1        ad 		 * is slow, using it here is not a problem.
    856       1.1        ad 		 */
    857  1.21.4.1     rmind 		KASSERT(l->l_selcluster == sc);
    858       1.1        ad 		SLIST_REMOVE(&l->l_selwait, sip, selinfo, sel_chain);
    859       1.1        ad 		sip->sel_lwp = NULL;
    860       1.1        ad 	}
    861      1.13        ad 	mutex_spin_exit(lock);
    862       1.1        ad }
    863       1.1        ad 
    864       1.1        ad int
    865      1.14  christos pollsock(struct socket *so, const struct timespec *tsp, int events)
    866       1.1        ad {
    867       1.1        ad 	int		ncoll, error, timo;
    868      1.14  christos 	struct timespec	sleepts, ts;
    869  1.21.4.1     rmind 	selcluster_t	*sc;
    870       1.1        ad 	lwp_t		*l;
    871      1.13        ad 	kmutex_t	*lock;
    872       1.1        ad 
    873       1.1        ad 	timo = 0;
    874      1.14  christos 	if (tsp != NULL) {
    875      1.14  christos 		ts = *tsp;
    876      1.14  christos 		if (inittimeleft(&ts, &sleepts) == -1)
    877       1.1        ad 			return EINVAL;
    878       1.1        ad 	}
    879       1.1        ad 
    880       1.1        ad 	l = curlwp;
    881  1.21.4.1     rmind 	sc = curcpu()->ci_data.cpu_selcluster;
    882      1.13        ad 	lock = sc->sc_lock;
    883  1.21.4.1     rmind 	l->l_selcluster = sc;
    884       1.1        ad 	SLIST_INIT(&l->l_selwait);
    885       1.1        ad 	error = 0;
    886       1.1        ad 	for (;;) {
    887       1.1        ad 		/*
    888       1.1        ad 		 * No need to lock.  If this is overwritten by another
    889       1.1        ad 		 * value while scanning, we will retry below.  We only
    890       1.1        ad 		 * need to see exact state from the descriptors that
    891       1.1        ad 		 * we are about to poll, and lock activity resulting
    892       1.1        ad 		 * from fo_poll is enough to provide an up to date value
    893       1.1        ad 		 * for new polling activity.
    894       1.1        ad 		 */
    895       1.1        ad 		ncoll = sc->sc_ncoll;
    896       1.1        ad 		l->l_selflag = SEL_SCANNING;
    897       1.1        ad 		if (sopoll(so, events) != 0)
    898       1.1        ad 			break;
    899      1.14  christos 		if (tsp && (timo = gettimeleft(&ts, &sleepts)) <= 0)
    900       1.1        ad 			break;
    901      1.13        ad 		mutex_spin_enter(lock);
    902       1.1        ad 		if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
    903      1.13        ad 			mutex_spin_exit(lock);
    904       1.1        ad 			continue;
    905       1.1        ad 		}
    906       1.1        ad 		l->l_selflag = SEL_BLOCKING;
    907      1.13        ad 		sleepq_enter(&sc->sc_sleepq, l, lock);
    908       1.1        ad 		sleepq_enqueue(&sc->sc_sleepq, sc, "pollsock", &select_sobj);
    909       1.1        ad 		error = sleepq_block(timo, true);
    910       1.1        ad 		if (error != 0)
    911       1.1        ad 			break;
    912       1.1        ad 	}
    913       1.1        ad 	selclear();
    914       1.1        ad 	/* poll is not restarted after signals... */
    915       1.1        ad 	if (error == ERESTART)
    916       1.1        ad 		error = EINTR;
    917       1.1        ad 	if (error == EWOULDBLOCK)
    918       1.1        ad 		error = 0;
    919       1.1        ad 	return (error);
    920       1.1        ad }
    921