Home | History | Annotate | Line # | Download | only in kern
sys_select.c revision 1.46.2.2
      1  1.46.2.2    martin /*	$NetBSD: sys_select.c,v 1.46.2.2 2024/11/20 14:09:10 martin Exp $	*/
      2       1.1        ad 
      3       1.1        ad /*-
      4      1.22        ad  * 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.23     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.22        ad  * 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.22        ad  *		selcluster_t::sc_lock
     84       1.1        ad  */
     85       1.1        ad 
     86       1.1        ad #include <sys/cdefs.h>
     87  1.46.2.2    martin __KERNEL_RCSID(0, "$NetBSD: sys_select.c,v 1.46.2.2 2024/11/20 14:09:10 martin 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.29     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.36     rmind #include <sys/sysctl.h>
    107       1.1        ad 
    108       1.1        ad /* Flags for lwp::l_selflag. */
    109       1.1        ad #define	SEL_RESET	0	/* awoken, interrupted, or not yet polling */
    110       1.1        ad #define	SEL_SCANNING	1	/* polling descriptors */
    111      1.23     rmind #define	SEL_BLOCKING	2	/* blocking and waiting for event */
    112      1.23     rmind #define	SEL_EVENT	3	/* interrupted, events set directly */
    113      1.23     rmind 
    114      1.23     rmind /* Operations: either select() or poll(). */
    115      1.23     rmind #define	SELOP_SELECT	1
    116      1.23     rmind #define	SELOP_POLL	2
    117       1.1        ad 
    118      1.22        ad /*
    119      1.22        ad  * Per-cluster state for select()/poll().  For a system with fewer
    120      1.22        ad  * than 32 CPUs, this gives us per-CPU clusters.
    121      1.22        ad  */
    122      1.22        ad #define	SELCLUSTERS	32
    123      1.22        ad #define	SELCLUSTERMASK	(SELCLUSTERS - 1)
    124      1.22        ad 
    125      1.22        ad typedef struct selcluster {
    126      1.13        ad 	kmutex_t	*sc_lock;
    127       1.1        ad 	sleepq_t	sc_sleepq;
    128       1.1        ad 	int		sc_ncoll;
    129       1.1        ad 	uint32_t	sc_mask;
    130      1.22        ad } selcluster_t;
    131       1.1        ad 
    132      1.23     rmind static inline int	selscan(char *, const int, const size_t, register_t *);
    133      1.23     rmind static inline int	pollscan(struct pollfd *, const int, register_t *);
    134      1.19     rmind static void		selclear(void);
    135       1.1        ad 
    136      1.23     rmind static const int sel_flag[] = {
    137      1.23     rmind 	POLLRDNORM | POLLHUP | POLLERR,
    138      1.23     rmind 	POLLWRNORM | POLLHUP | POLLERR,
    139      1.23     rmind 	POLLRDBAND
    140      1.23     rmind };
    141      1.23     rmind 
    142       1.1        ad static syncobj_t select_sobj = {
    143      1.41     ozaki 	.sobj_flag	= SOBJ_SLEEPQ_FIFO,
    144      1.41     ozaki 	.sobj_unsleep	= sleepq_unsleep,
    145      1.41     ozaki 	.sobj_changepri	= sleepq_changepri,
    146      1.41     ozaki 	.sobj_lendpri	= sleepq_lendpri,
    147      1.41     ozaki 	.sobj_owner	= syncobj_noowner,
    148       1.1        ad };
    149       1.1        ad 
    150      1.23     rmind static selcluster_t	*selcluster[SELCLUSTERS] __read_mostly;
    151      1.36     rmind static int		direct_select __read_mostly = 0;
    152      1.22        ad 
    153       1.1        ad /*
    154       1.1        ad  * Select system call.
    155       1.1        ad  */
    156       1.1        ad int
    157      1.12  christos sys___pselect50(struct lwp *l, const struct sys___pselect50_args *uap,
    158      1.12  christos     register_t *retval)
    159       1.1        ad {
    160       1.1        ad 	/* {
    161       1.1        ad 		syscallarg(int)				nd;
    162       1.1        ad 		syscallarg(fd_set *)			in;
    163       1.1        ad 		syscallarg(fd_set *)			ou;
    164       1.1        ad 		syscallarg(fd_set *)			ex;
    165       1.1        ad 		syscallarg(const struct timespec *)	ts;
    166       1.1        ad 		syscallarg(sigset_t *)			mask;
    167       1.1        ad 	} */
    168      1.14  christos 	struct timespec	ats, *ts = NULL;
    169       1.1        ad 	sigset_t	amask, *mask = NULL;
    170       1.1        ad 	int		error;
    171       1.1        ad 
    172       1.1        ad 	if (SCARG(uap, ts)) {
    173       1.1        ad 		error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
    174       1.1        ad 		if (error)
    175       1.1        ad 			return error;
    176      1.14  christos 		ts = &ats;
    177       1.1        ad 	}
    178       1.1        ad 	if (SCARG(uap, mask) != NULL) {
    179       1.1        ad 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
    180       1.1        ad 		if (error)
    181       1.1        ad 			return error;
    182       1.1        ad 		mask = &amask;
    183       1.1        ad 	}
    184       1.1        ad 
    185      1.19     rmind 	return selcommon(retval, SCARG(uap, nd), SCARG(uap, in),
    186      1.14  christos 	    SCARG(uap, ou), SCARG(uap, ex), ts, mask);
    187       1.1        ad }
    188       1.1        ad 
    189       1.1        ad int
    190      1.12  christos sys___select50(struct lwp *l, const struct sys___select50_args *uap,
    191      1.12  christos     register_t *retval)
    192       1.1        ad {
    193       1.1        ad 	/* {
    194       1.1        ad 		syscallarg(int)			nd;
    195       1.1        ad 		syscallarg(fd_set *)		in;
    196       1.1        ad 		syscallarg(fd_set *)		ou;
    197       1.1        ad 		syscallarg(fd_set *)		ex;
    198       1.1        ad 		syscallarg(struct timeval *)	tv;
    199       1.1        ad 	} */
    200      1.14  christos 	struct timeval atv;
    201      1.14  christos 	struct timespec ats, *ts = NULL;
    202       1.1        ad 	int error;
    203       1.1        ad 
    204       1.1        ad 	if (SCARG(uap, tv)) {
    205      1.14  christos 		error = copyin(SCARG(uap, tv), (void *)&atv, sizeof(atv));
    206       1.1        ad 		if (error)
    207       1.1        ad 			return error;
    208      1.14  christos 		TIMEVAL_TO_TIMESPEC(&atv, &ats);
    209      1.14  christos 		ts = &ats;
    210       1.1        ad 	}
    211       1.1        ad 
    212      1.19     rmind 	return selcommon(retval, SCARG(uap, nd), SCARG(uap, in),
    213      1.14  christos 	    SCARG(uap, ou), SCARG(uap, ex), ts, NULL);
    214       1.1        ad }
    215       1.1        ad 
    216      1.17     rmind /*
    217      1.17     rmind  * sel_do_scan: common code to perform the scan on descriptors.
    218      1.17     rmind  */
    219      1.17     rmind static int
    220      1.23     rmind sel_do_scan(const int op, void *fds, const int nf, const size_t ni,
    221      1.23     rmind     struct timespec *ts, sigset_t *mask, register_t *retval)
    222       1.1        ad {
    223      1.17     rmind 	lwp_t		* const l = curlwp;
    224      1.22        ad 	selcluster_t	*sc;
    225      1.13        ad 	kmutex_t	*lock;
    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.32  christos 	if (__predict_false(mask))
    235      1.31  christos 		sigsuspendsetup(l, mask);
    236       1.1        ad 
    237      1.22        ad 	sc = curcpu()->ci_data.cpu_selcluster;
    238      1.13        ad 	lock = sc->sc_lock;
    239      1.22        ad 	l->l_selcluster = sc;
    240      1.23     rmind 	if (op == SELOP_SELECT) {
    241      1.30     rmind 		l->l_selbits = fds;
    242      1.23     rmind 		l->l_selni = ni;
    243      1.23     rmind 	} else {
    244      1.23     rmind 		l->l_selbits = NULL;
    245      1.23     rmind 	}
    246      1.34   hannken 
    247       1.1        ad 	for (;;) {
    248      1.17     rmind 		int ncoll;
    249      1.17     rmind 
    250      1.34   hannken 		SLIST_INIT(&l->l_selwait);
    251      1.34   hannken 		l->l_selret = 0;
    252      1.34   hannken 
    253       1.1        ad 		/*
    254      1.17     rmind 		 * No need to lock.  If this is overwritten by another value
    255      1.17     rmind 		 * while scanning, we will retry below.  We only need to see
    256      1.17     rmind 		 * exact state from the descriptors that we are about to poll,
    257      1.17     rmind 		 * and lock activity resulting from fo_poll is enough to
    258      1.17     rmind 		 * provide an up to date value for new polling activity.
    259       1.1        ad 		 */
    260      1.17     rmind 		l->l_selflag = SEL_SCANNING;
    261       1.1        ad 		ncoll = sc->sc_ncoll;
    262       1.1        ad 
    263      1.23     rmind 		if (op == SELOP_SELECT) {
    264      1.23     rmind 			error = selscan((char *)fds, nf, ni, retval);
    265      1.17     rmind 		} else {
    266      1.23     rmind 			error = pollscan((struct pollfd *)fds, nf, retval);
    267      1.17     rmind 		}
    268       1.1        ad 		if (error || *retval)
    269       1.1        ad 			break;
    270      1.14  christos 		if (ts && (timo = gettimeleft(ts, &sleepts)) <= 0)
    271       1.1        ad 			break;
    272      1.23     rmind 		/*
    273      1.23     rmind 		 * Acquire the lock and perform the (re)checks.  Note, if
    274      1.23     rmind 		 * collision has occured, then our state does not matter,
    275      1.23     rmind 		 * as we must perform re-scan.  Therefore, check it first.
    276      1.23     rmind 		 */
    277      1.23     rmind state_check:
    278      1.13        ad 		mutex_spin_enter(lock);
    279      1.23     rmind 		if (__predict_false(sc->sc_ncoll != ncoll)) {
    280      1.23     rmind 			/* Collision: perform re-scan. */
    281      1.23     rmind 			mutex_spin_exit(lock);
    282      1.34   hannken 			selclear();
    283      1.23     rmind 			continue;
    284      1.23     rmind 		}
    285      1.23     rmind 		if (__predict_true(l->l_selflag == SEL_EVENT)) {
    286      1.23     rmind 			/* Events occured, they are set directly. */
    287      1.23     rmind 			mutex_spin_exit(lock);
    288      1.23     rmind 			break;
    289      1.23     rmind 		}
    290      1.23     rmind 		if (__predict_true(l->l_selflag == SEL_RESET)) {
    291      1.23     rmind 			/* Events occured, but re-scan is requested. */
    292      1.13        ad 			mutex_spin_exit(lock);
    293      1.34   hannken 			selclear();
    294       1.1        ad 			continue;
    295       1.1        ad 		}
    296      1.23     rmind 		/* Nothing happen, therefore - sleep. */
    297       1.1        ad 		l->l_selflag = SEL_BLOCKING;
    298       1.7        ad 		l->l_kpriority = true;
    299      1.13        ad 		sleepq_enter(&sc->sc_sleepq, l, lock);
    300       1.1        ad 		sleepq_enqueue(&sc->sc_sleepq, sc, "select", &select_sobj);
    301       1.1        ad 		error = sleepq_block(timo, true);
    302      1.23     rmind 		if (error != 0) {
    303       1.1        ad 			break;
    304      1.23     rmind 		}
    305      1.23     rmind 		/* Awoken: need to check the state. */
    306      1.23     rmind 		goto state_check;
    307       1.1        ad 	}
    308       1.1        ad 	selclear();
    309       1.1        ad 
    310      1.34   hannken 	/* Add direct events if any. */
    311      1.34   hannken 	if (l->l_selflag == SEL_EVENT) {
    312      1.34   hannken 		KASSERT(l->l_selret != 0);
    313      1.34   hannken 		*retval += l->l_selret;
    314      1.34   hannken 	}
    315      1.34   hannken 
    316      1.33  christos 	if (__predict_false(mask))
    317      1.33  christos 		sigsuspendteardown(l);
    318      1.33  christos 
    319      1.20       dsl 	/* select and poll are not restarted after signals... */
    320      1.20       dsl 	if (error == ERESTART)
    321      1.20       dsl 		return EINTR;
    322      1.20       dsl 	if (error == EWOULDBLOCK)
    323      1.20       dsl 		return 0;
    324      1.17     rmind 	return error;
    325      1.17     rmind }
    326      1.17     rmind 
    327  1.46.2.2    martin /* designed to be compatible with FD_SET() FD_ISSET() ... */
    328  1.46.2.2    martin static int
    329  1.46.2.2    martin anyset(void *p, size_t nbits)
    330  1.46.2.2    martin {
    331  1.46.2.2    martin 	size_t nwords;
    332  1.46.2.2    martin 	__fd_mask mask;
    333  1.46.2.2    martin 	__fd_mask *f = (__fd_mask *)p;
    334  1.46.2.2    martin 
    335  1.46.2.2    martin 	nwords = nbits / __NFDBITS;
    336  1.46.2.2    martin 
    337  1.46.2.2    martin 	while (nwords-- > 0)
    338  1.46.2.2    martin 		if (*f++ != 0)
    339  1.46.2.2    martin 			return 1;
    340  1.46.2.2    martin 
    341  1.46.2.2    martin 	nbits &= __NFDMASK;
    342  1.46.2.2    martin 	if (nbits != 0) {
    343  1.46.2.2    martin 		mask = (1U << nbits) - 1;
    344  1.46.2.2    martin 		if ((*f & mask) != 0)
    345  1.46.2.2    martin 			return 1;
    346  1.46.2.2    martin 	}
    347  1.46.2.2    martin 	return 0;
    348  1.46.2.2    martin }
    349  1.46.2.2    martin 
    350      1.17     rmind int
    351      1.19     rmind selcommon(register_t *retval, int nd, fd_set *u_in, fd_set *u_ou,
    352      1.19     rmind     fd_set *u_ex, struct timespec *ts, sigset_t *mask)
    353      1.17     rmind {
    354      1.17     rmind 	char		smallbits[howmany(FD_SETSIZE, NFDBITS) *
    355      1.17     rmind 			    sizeof(fd_mask) * 6];
    356      1.17     rmind 	char 		*bits;
    357  1.46.2.2    martin 	int		error, nf, fb, db;
    358      1.17     rmind 	size_t		ni;
    359      1.17     rmind 
    360      1.17     rmind 	if (nd < 0)
    361  1.46.2.2    martin 		return EINVAL;
    362  1.46.2.2    martin 
    363  1.46.2.1    martin 	nf = atomic_load_consume(&curlwp->l_fd->fd_dt)->dt_nfiles;
    364  1.46.2.2    martin 
    365  1.46.2.2    martin 	/*
    366  1.46.2.2    martin 	 * Don't allow absurdly large numbers of fds to be selected.
    367  1.46.2.2    martin 	 * (used to silently truncate, naughty naughty, no more ...)
    368  1.46.2.2    martin 	 *
    369  1.46.2.2    martin 	 * The additional FD_SETSISE allows for cases where the limit
    370  1.46.2.2    martin 	 * is not a round binary number, but the fd_set wants to
    371  1.46.2.2    martin 	 * include all the possible fds, as fd_sets are always
    372  1.46.2.2    martin 	 * multiples of 32 bits (__NFDBITS extra would be enough).
    373  1.46.2.2    martin 	 *
    374  1.46.2.2    martin 	 * The first test handles the case where the res limit has been
    375  1.46.2.2    martin 	 * set lower after some fds were opened, we always allow selecting
    376  1.46.2.2    martin 	 * up to the highest currently open fd.
    377  1.46.2.2    martin 	 */
    378  1.46.2.2    martin 	if (nd > nf + FD_SETSIZE &&
    379  1.46.2.2    martin 	    nd > curlwp->l_proc->p_rlimit[RLIMIT_NOFILE].rlim_max + FD_SETSIZE)
    380  1.46.2.2    martin 		return EINVAL;
    381  1.46.2.2    martin 
    382  1.46.2.2    martin 	fb = howmany(nf, __NFDBITS);		/* how many fd_masks */
    383  1.46.2.2    martin 	db = howmany(nd, __NFDBITS);
    384  1.46.2.2    martin 
    385  1.46.2.2    martin 	if (db > fb) {
    386  1.46.2.2    martin 		size_t off;
    387  1.46.2.2    martin 
    388  1.46.2.2    martin 		/*
    389  1.46.2.2    martin 		 * the application wants to supply more fd masks than can
    390  1.46.2.2    martin 		 * possibly represent valid file descriptors.
    391  1.46.2.2    martin 		 *
    392  1.46.2.2    martin 		 * Check the excess fd_masks, if any bits are set in them
    393  1.46.2.2    martin 		 * that must be an error (cannot represent valid fd).
    394  1.46.2.2    martin 		 *
    395  1.46.2.2    martin 		 * Supplying lots of extra cleared fd_masks is dumb,
    396  1.46.2.2    martin 		 * but harmless, so allow that.
    397  1.46.2.2    martin 		 */
    398  1.46.2.2    martin 		ni = (db - fb) * sizeof(fd_mask);	/* excess bytes */
    399  1.46.2.2    martin 		bits = smallbits;
    400  1.46.2.2    martin 
    401  1.46.2.2    martin 		/* skip over the valid fd_masks, those will be checked below */
    402  1.46.2.2    martin 		off = howmany(nf, __NFDBITS) * sizeof(__fd_mask);
    403  1.46.2.2    martin 
    404  1.46.2.2    martin 		nd -= fb * NFDBITS;	/* the number of excess fds */
    405  1.46.2.2    martin 
    406  1.46.2.2    martin #define checkbits(name, o, sz, fds)					\
    407  1.46.2.2    martin 		do {							\
    408  1.46.2.2    martin 		    if (u_ ## name != NULL) {				\
    409  1.46.2.2    martin 			error = copyin((char *)u_ ## name + o,		\
    410  1.46.2.2    martin 					bits, sz);			\
    411  1.46.2.2    martin 			if (error)					\
    412  1.46.2.2    martin 			    goto fail;					\
    413  1.46.2.2    martin 			if (anyset(bits, (fds) ?			\
    414  1.46.2.2    martin 				 (size_t)(fds) : CHAR_BIT * (sz))) {	\
    415  1.46.2.2    martin 			    error = EBADF;				\
    416  1.46.2.2    martin 			    goto fail;					\
    417  1.46.2.2    martin 			}						\
    418  1.46.2.2    martin 		    }							\
    419  1.46.2.2    martin 		} while (0)
    420  1.46.2.2    martin 
    421  1.46.2.2    martin 		while (ni > sizeof(smallbits)) {
    422  1.46.2.2    martin 			checkbits(in, off, sizeof(smallbits), 0);
    423  1.46.2.2    martin 			checkbits(ou, off, sizeof(smallbits), 0);
    424  1.46.2.2    martin 			checkbits(ex, off, sizeof(smallbits), 0);
    425  1.46.2.2    martin 
    426  1.46.2.2    martin 			off += sizeof(smallbits);
    427  1.46.2.2    martin 			ni -= sizeof(smallbits);
    428  1.46.2.2    martin 			nd -= sizeof(smallbits) * CHAR_BIT;
    429  1.46.2.2    martin 		}
    430  1.46.2.2    martin 		checkbits(in, off, ni, nd);
    431  1.46.2.2    martin 		checkbits(ou, off, ni, nd);
    432  1.46.2.2    martin 		checkbits(ex, off, ni, nd);
    433  1.46.2.2    martin #undef checkbits
    434  1.46.2.2    martin 
    435  1.46.2.2    martin 		db = fb;	/* now just check the plausible fds */
    436  1.46.2.2    martin 		nd = db * __NFDBITS;
    437      1.17     rmind 	}
    438  1.46.2.2    martin 
    439  1.46.2.2    martin 	ni = db * sizeof(fd_mask);
    440      1.40       chs 	if (ni * 6 > sizeof(smallbits))
    441      1.17     rmind 		bits = kmem_alloc(ni * 6, KM_SLEEP);
    442      1.40       chs 	else
    443      1.17     rmind 		bits = smallbits;
    444      1.17     rmind 
    445      1.17     rmind #define	getbits(name, x)						\
    446  1.46.2.2    martin 	do {								\
    447  1.46.2.2    martin 		if (u_ ## name) {					\
    448  1.46.2.2    martin 			error = copyin(u_ ## name, bits + ni * x, ni);	\
    449  1.46.2.2    martin 			if (error)					\
    450  1.46.2.2    martin 				goto fail;				\
    451  1.46.2.2    martin 		} else							\
    452  1.46.2.2    martin 			memset(bits + ni * x, 0, ni);			\
    453  1.46.2.2    martin 	} while (0)
    454  1.46.2.2    martin 
    455      1.17     rmind 	getbits(in, 0);
    456      1.17     rmind 	getbits(ou, 1);
    457      1.17     rmind 	getbits(ex, 2);
    458      1.17     rmind #undef	getbits
    459       1.1        ad 
    460      1.23     rmind 	error = sel_do_scan(SELOP_SELECT, bits, nd, ni, ts, mask, retval);
    461  1.46.2.2    martin 
    462  1.46.2.2    martin #define copyback(name, x)						\
    463  1.46.2.2    martin 		do {							\
    464  1.46.2.2    martin 			if (error == 0 && u_ ## name != NULL)		\
    465  1.46.2.2    martin 				error = copyout(bits + ni * x,		\
    466  1.46.2.2    martin 						u_ ## name, ni);	\
    467  1.46.2.2    martin 		} while (0)
    468  1.46.2.2    martin 
    469  1.46.2.2    martin 	copyback(in, 3);
    470  1.46.2.2    martin 	copyback(ou, 4);
    471  1.46.2.2    martin 	copyback(ex, 5);
    472  1.46.2.2    martin #undef copyback
    473  1.46.2.2    martin 
    474      1.20       dsl  fail:
    475       1.1        ad 	if (bits != smallbits)
    476       1.1        ad 		kmem_free(bits, ni * 6);
    477       1.1        ad 	return (error);
    478       1.1        ad }
    479       1.1        ad 
    480      1.19     rmind static inline int
    481      1.23     rmind selscan(char *bits, const int nfd, const size_t ni, register_t *retval)
    482       1.1        ad {
    483      1.17     rmind 	fd_mask *ibitp, *obitp;
    484      1.23     rmind 	int msk, i, j, fd, n;
    485       1.1        ad 	file_t *fp;
    486       1.1        ad 
    487      1.17     rmind 	ibitp = (fd_mask *)(bits + ni * 0);
    488      1.17     rmind 	obitp = (fd_mask *)(bits + ni * 3);
    489       1.1        ad 	n = 0;
    490      1.17     rmind 
    491      1.34   hannken 	memset(obitp, 0, ni * 3);
    492       1.1        ad 	for (msk = 0; msk < 3; msk++) {
    493       1.1        ad 		for (i = 0; i < nfd; i += NFDBITS) {
    494      1.23     rmind 			fd_mask ibits, obits;
    495      1.23     rmind 
    496      1.35   hannken 			ibits = *ibitp;
    497       1.1        ad 			obits = 0;
    498       1.1        ad 			while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
    499       1.1        ad 				ibits &= ~(1 << j);
    500       1.1        ad 				if ((fp = fd_getfile(fd)) == NULL)
    501       1.1        ad 					return (EBADF);
    502      1.23     rmind 				/*
    503      1.23     rmind 				 * Setup an argument to selrecord(), which is
    504      1.23     rmind 				 * a file descriptor number.
    505      1.23     rmind 				 */
    506      1.23     rmind 				curlwp->l_selrec = fd;
    507      1.23     rmind 				if ((*fp->f_ops->fo_poll)(fp, sel_flag[msk])) {
    508       1.1        ad 					obits |= (1 << j);
    509       1.1        ad 					n++;
    510       1.1        ad 				}
    511       1.1        ad 				fd_putfile(fd);
    512       1.1        ad 			}
    513      1.34   hannken 			if (obits != 0) {
    514      1.36     rmind 				if (direct_select) {
    515      1.36     rmind 					kmutex_t *lock;
    516      1.36     rmind 					lock = curlwp->l_selcluster->sc_lock;
    517      1.35   hannken 					mutex_spin_enter(lock);
    518      1.36     rmind 					*obitp |= obits;
    519      1.35   hannken 					mutex_spin_exit(lock);
    520      1.36     rmind 				} else {
    521      1.36     rmind 					*obitp |= obits;
    522      1.36     rmind 				}
    523      1.34   hannken 			}
    524      1.35   hannken 			ibitp++;
    525      1.34   hannken 			obitp++;
    526       1.1        ad 		}
    527       1.1        ad 	}
    528       1.1        ad 	*retval = n;
    529       1.1        ad 	return (0);
    530       1.1        ad }
    531       1.1        ad 
    532       1.1        ad /*
    533       1.1        ad  * Poll system call.
    534       1.1        ad  */
    535       1.1        ad int
    536       1.1        ad sys_poll(struct lwp *l, const struct sys_poll_args *uap, register_t *retval)
    537       1.1        ad {
    538       1.1        ad 	/* {
    539       1.1        ad 		syscallarg(struct pollfd *)	fds;
    540       1.1        ad 		syscallarg(u_int)		nfds;
    541       1.1        ad 		syscallarg(int)			timeout;
    542       1.1        ad 	} */
    543      1.14  christos 	struct timespec	ats, *ts = NULL;
    544       1.1        ad 
    545       1.1        ad 	if (SCARG(uap, timeout) != INFTIM) {
    546      1.14  christos 		ats.tv_sec = SCARG(uap, timeout) / 1000;
    547      1.14  christos 		ats.tv_nsec = (SCARG(uap, timeout) % 1000) * 1000000;
    548      1.14  christos 		ts = &ats;
    549       1.1        ad 	}
    550       1.1        ad 
    551      1.19     rmind 	return pollcommon(retval, SCARG(uap, fds), SCARG(uap, nfds), ts, NULL);
    552       1.1        ad }
    553       1.1        ad 
    554       1.1        ad /*
    555       1.1        ad  * Poll system call.
    556       1.1        ad  */
    557       1.1        ad int
    558      1.12  christos sys___pollts50(struct lwp *l, const struct sys___pollts50_args *uap,
    559      1.12  christos     register_t *retval)
    560       1.1        ad {
    561       1.1        ad 	/* {
    562       1.1        ad 		syscallarg(struct pollfd *)		fds;
    563       1.1        ad 		syscallarg(u_int)			nfds;
    564       1.1        ad 		syscallarg(const struct timespec *)	ts;
    565       1.1        ad 		syscallarg(const sigset_t *)		mask;
    566       1.1        ad 	} */
    567      1.14  christos 	struct timespec	ats, *ts = NULL;
    568       1.1        ad 	sigset_t	amask, *mask = NULL;
    569       1.1        ad 	int		error;
    570       1.1        ad 
    571       1.1        ad 	if (SCARG(uap, ts)) {
    572       1.1        ad 		error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
    573       1.1        ad 		if (error)
    574       1.1        ad 			return error;
    575      1.14  christos 		ts = &ats;
    576       1.1        ad 	}
    577       1.1        ad 	if (SCARG(uap, mask)) {
    578       1.1        ad 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
    579       1.1        ad 		if (error)
    580       1.1        ad 			return error;
    581       1.1        ad 		mask = &amask;
    582       1.1        ad 	}
    583       1.1        ad 
    584      1.19     rmind 	return pollcommon(retval, SCARG(uap, fds), SCARG(uap, nfds), ts, mask);
    585       1.1        ad }
    586       1.1        ad 
    587       1.1        ad int
    588      1.19     rmind pollcommon(register_t *retval, struct pollfd *u_fds, u_int nfds,
    589      1.14  christos     struct timespec *ts, sigset_t *mask)
    590       1.1        ad {
    591      1.11      yamt 	struct pollfd	smallfds[32];
    592      1.11      yamt 	struct pollfd	*fds;
    593      1.17     rmind 	int		error;
    594      1.20       dsl 	size_t		ni;
    595       1.1        ad 
    596      1.45  christos 	if (nfds > curlwp->l_proc->p_rlimit[RLIMIT_NOFILE].rlim_max + 1000) {
    597      1.20       dsl 		/*
    598      1.43  christos 		 * Prevent userland from causing over-allocation.
    599      1.43  christos 		 * Raising the default limit too high can still cause
    600      1.43  christos 		 * a lot of memory to be allocated, but this also means
    601      1.43  christos 		 * that the file descriptor array will also be large.
    602      1.43  christos 		 *
    603      1.43  christos 		 * To reduce the memory requirements here, we could
    604      1.43  christos 		 * process the 'fds' array in chunks, but that
    605      1.20       dsl 		 * is a lot of code that isn't normally useful.
    606      1.20       dsl 		 * (Or just move the copyin/out into pollscan().)
    607      1.43  christos 		 *
    608      1.20       dsl 		 * Historically the code silently truncated 'fds' to
    609      1.20       dsl 		 * dt_nfiles entries - but that does cause issues.
    610      1.44  christos 		 *
    611      1.44  christos 		 * Using the max limit equivalent to sysctl
    612      1.44  christos 		 * kern.maxfiles is the moral equivalent of OPEN_MAX
    613      1.45  christos 		 * as specified by POSIX.
    614      1.45  christos 		 *
    615      1.45  christos 		 * We add a slop of 1000 in case the resource limit was
    616      1.45  christos 		 * changed after opening descriptors or the same descriptor
    617      1.45  christos 		 * was specified more than once.
    618      1.20       dsl 		 */
    619      1.20       dsl 		return EINVAL;
    620       1.1        ad 	}
    621       1.1        ad 	ni = nfds * sizeof(struct pollfd);
    622      1.40       chs 	if (ni > sizeof(smallfds))
    623      1.11      yamt 		fds = kmem_alloc(ni, KM_SLEEP);
    624      1.40       chs 	else
    625      1.11      yamt 		fds = smallfds;
    626       1.1        ad 
    627      1.11      yamt 	error = copyin(u_fds, fds, ni);
    628       1.1        ad 	if (error)
    629      1.20       dsl 		goto fail;
    630       1.1        ad 
    631      1.23     rmind 	error = sel_do_scan(SELOP_POLL, fds, nfds, ni, ts, mask, retval);
    632       1.1        ad 	if (error == 0)
    633      1.11      yamt 		error = copyout(fds, u_fds, ni);
    634      1.20       dsl  fail:
    635      1.11      yamt 	if (fds != smallfds)
    636      1.11      yamt 		kmem_free(fds, ni);
    637       1.1        ad 	return (error);
    638       1.1        ad }
    639       1.1        ad 
    640      1.19     rmind static inline int
    641      1.23     rmind pollscan(struct pollfd *fds, const int nfd, register_t *retval)
    642       1.1        ad {
    643       1.1        ad 	file_t *fp;
    644      1.34   hannken 	int i, n = 0, revents;
    645       1.1        ad 
    646       1.1        ad 	for (i = 0; i < nfd; i++, fds++) {
    647      1.34   hannken 		fds->revents = 0;
    648       1.1        ad 		if (fds->fd < 0) {
    649      1.34   hannken 			revents = 0;
    650       1.1        ad 		} else if ((fp = fd_getfile(fds->fd)) == NULL) {
    651      1.34   hannken 			revents = POLLNVAL;
    652       1.1        ad 		} else {
    653      1.23     rmind 			/*
    654      1.23     rmind 			 * Perform poll: registers select request or returns
    655      1.23     rmind 			 * the events which are set.  Setup an argument for
    656      1.23     rmind 			 * selrecord(), which is a pointer to struct pollfd.
    657      1.23     rmind 			 */
    658      1.23     rmind 			curlwp->l_selrec = (uintptr_t)fds;
    659      1.34   hannken 			revents = (*fp->f_ops->fo_poll)(fp,
    660       1.1        ad 			    fds->events | POLLERR | POLLHUP);
    661       1.1        ad 			fd_putfile(fds->fd);
    662       1.1        ad 		}
    663      1.34   hannken 		if (revents) {
    664      1.34   hannken 			fds->revents = revents;
    665      1.34   hannken 			n++;
    666      1.34   hannken 		}
    667       1.1        ad 	}
    668       1.1        ad 	*retval = n;
    669       1.1        ad 	return (0);
    670       1.1        ad }
    671       1.1        ad 
    672       1.1        ad int
    673       1.1        ad seltrue(dev_t dev, int events, lwp_t *l)
    674       1.1        ad {
    675       1.1        ad 
    676       1.1        ad 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
    677       1.1        ad }
    678       1.1        ad 
    679       1.1        ad /*
    680       1.1        ad  * Record a select request.  Concurrency issues:
    681       1.1        ad  *
    682       1.1        ad  * The caller holds the same lock across calls to selrecord() and
    683       1.4      yamt  * selnotify(), so we don't need to consider a concurrent wakeup
    684       1.1        ad  * while in this routine.
    685       1.1        ad  *
    686       1.1        ad  * The only activity we need to guard against is selclear(), called by
    687      1.17     rmind  * another thread that is exiting sel_do_scan().
    688       1.1        ad  * `sel_lwp' can only become non-NULL while the caller's lock is held,
    689       1.1        ad  * so it cannot become non-NULL due to a change made by another thread
    690       1.1        ad  * while we are in this routine.  It can only become _NULL_ due to a
    691       1.1        ad  * call to selclear().
    692       1.1        ad  *
    693       1.1        ad  * If it is non-NULL and != selector there is the potential for
    694       1.1        ad  * selclear() to be called by another thread.  If either of those
    695       1.1        ad  * conditions are true, we're not interested in touching the `named
    696       1.1        ad  * waiter' part of the selinfo record because we need to record a
    697       1.1        ad  * collision.  Hence there is no need for additional locking in this
    698       1.1        ad  * routine.
    699       1.1        ad  */
    700       1.1        ad void
    701       1.1        ad selrecord(lwp_t *selector, struct selinfo *sip)
    702       1.1        ad {
    703      1.22        ad 	selcluster_t *sc;
    704       1.1        ad 	lwp_t *other;
    705       1.1        ad 
    706       1.1        ad 	KASSERT(selector == curlwp);
    707       1.1        ad 
    708      1.22        ad 	sc = selector->l_selcluster;
    709       1.1        ad 	other = sip->sel_lwp;
    710       1.1        ad 
    711       1.1        ad 	if (other == selector) {
    712      1.23     rmind 		/* 1. We (selector) already claimed to be the first LWP. */
    713      1.37  riastrad 		KASSERT(sip->sel_cluster == sc);
    714       1.1        ad 	} else if (other == NULL) {
    715       1.1        ad 		/*
    716      1.23     rmind 		 * 2. No first LWP, therefore we (selector) are the first.
    717      1.23     rmind 		 *
    718      1.23     rmind 		 * There may be unnamed waiters (collisions).  Issue a memory
    719      1.23     rmind 		 * barrier to ensure that we access sel_lwp (above) before
    720      1.23     rmind 		 * other fields - this guards against a call to selclear().
    721       1.1        ad 		 */
    722       1.1        ad 		membar_enter();
    723       1.1        ad 		sip->sel_lwp = selector;
    724       1.1        ad 		SLIST_INSERT_HEAD(&selector->l_selwait, sip, sel_chain);
    725      1.23     rmind 		/* Copy the argument, which is for selnotify(). */
    726      1.23     rmind 		sip->sel_fdinfo = selector->l_selrec;
    727      1.22        ad 		/* Replace selinfo's lock with the chosen cluster's lock. */
    728      1.22        ad 		sip->sel_cluster = sc;
    729       1.1        ad 	} else {
    730      1.23     rmind 		/* 3. Multiple waiters: record a collision. */
    731       1.1        ad 		sip->sel_collision |= sc->sc_mask;
    732      1.22        ad 		KASSERT(sip->sel_cluster != NULL);
    733       1.1        ad 	}
    734       1.1        ad }
    735       1.1        ad 
    736       1.1        ad /*
    737      1.23     rmind  * sel_setevents: a helper function for selnotify(), to set the events
    738      1.23     rmind  * for LWP sleeping in selcommon() or pollcommon().
    739      1.23     rmind  */
    740      1.30     rmind static inline bool
    741      1.23     rmind sel_setevents(lwp_t *l, struct selinfo *sip, const int events)
    742      1.23     rmind {
    743      1.23     rmind 	const int oflag = l->l_selflag;
    744      1.30     rmind 	int ret = 0;
    745      1.23     rmind 
    746      1.23     rmind 	/*
    747      1.23     rmind 	 * If we require re-scan or it was required by somebody else,
    748      1.23     rmind 	 * then just (re)set SEL_RESET and return.
    749      1.23     rmind 	 */
    750      1.23     rmind 	if (__predict_false(events == 0 || oflag == SEL_RESET)) {
    751      1.23     rmind 		l->l_selflag = SEL_RESET;
    752      1.30     rmind 		return true;
    753      1.23     rmind 	}
    754      1.23     rmind 	/*
    755      1.23     rmind 	 * Direct set.  Note: select state of LWP is locked.  First,
    756      1.23     rmind 	 * determine whether it is selcommon() or pollcommon().
    757      1.23     rmind 	 */
    758      1.23     rmind 	if (l->l_selbits != NULL) {
    759      1.30     rmind 		const size_t ni = l->l_selni;
    760      1.23     rmind 		fd_mask *fds = (fd_mask *)l->l_selbits;
    761      1.30     rmind 		fd_mask *ofds = (fd_mask *)((char *)fds + ni * 3);
    762      1.30     rmind 		const int fd = sip->sel_fdinfo, fbit = 1 << (fd & __NFDMASK);
    763      1.25     rmind 		const int idx = fd >> __NFDSHIFT;
    764      1.23     rmind 		int n;
    765      1.23     rmind 
    766      1.23     rmind 		for (n = 0; n < 3; n++) {
    767      1.34   hannken 			if ((fds[idx] & fbit) != 0 &&
    768      1.34   hannken 			    (ofds[idx] & fbit) == 0 &&
    769      1.34   hannken 			    (sel_flag[n] & events)) {
    770      1.30     rmind 				ofds[idx] |= fbit;
    771      1.30     rmind 				ret++;
    772      1.23     rmind 			}
    773      1.23     rmind 			fds = (fd_mask *)((char *)fds + ni);
    774      1.30     rmind 			ofds = (fd_mask *)((char *)ofds + ni);
    775      1.23     rmind 		}
    776      1.23     rmind 	} else {
    777      1.23     rmind 		struct pollfd *pfd = (void *)sip->sel_fdinfo;
    778      1.30     rmind 		int revents = events & (pfd->events | POLLERR | POLLHUP);
    779      1.30     rmind 
    780      1.30     rmind 		if (revents) {
    781      1.34   hannken 			if (pfd->revents == 0)
    782      1.34   hannken 				ret = 1;
    783      1.30     rmind 			pfd->revents |= revents;
    784      1.30     rmind 		}
    785      1.30     rmind 	}
    786      1.30     rmind 	/* Check whether there are any events to return. */
    787      1.30     rmind 	if (!ret) {
    788      1.30     rmind 		return false;
    789      1.23     rmind 	}
    790      1.23     rmind 	/* Indicate direct set and note the event (cluster lock is held). */
    791      1.23     rmind 	l->l_selflag = SEL_EVENT;
    792      1.30     rmind 	l->l_selret += ret;
    793      1.30     rmind 	return true;
    794      1.23     rmind }
    795      1.23     rmind 
    796      1.23     rmind /*
    797       1.1        ad  * Do a wakeup when a selectable event occurs.  Concurrency issues:
    798       1.1        ad  *
    799       1.1        ad  * As per selrecord(), the caller's object lock is held.  If there
    800      1.22        ad  * is a named waiter, we must acquire the associated selcluster's lock
    801       1.1        ad  * in order to synchronize with selclear() and pollers going to sleep
    802      1.17     rmind  * in sel_do_scan().
    803       1.1        ad  *
    804      1.22        ad  * sip->sel_cluser cannot change at this point, as it is only changed
    805       1.1        ad  * in selrecord(), and concurrent calls to selrecord() are locked
    806       1.1        ad  * out by the caller.
    807       1.1        ad  */
    808       1.1        ad void
    809       1.1        ad selnotify(struct selinfo *sip, int events, long knhint)
    810       1.1        ad {
    811      1.22        ad 	selcluster_t *sc;
    812       1.1        ad 	uint32_t mask;
    813      1.16     rmind 	int index, oflag;
    814       1.1        ad 	lwp_t *l;
    815      1.13        ad 	kmutex_t *lock;
    816       1.1        ad 
    817       1.1        ad 	KNOTE(&sip->sel_klist, knhint);
    818       1.1        ad 
    819       1.1        ad 	if (sip->sel_lwp != NULL) {
    820       1.1        ad 		/* One named LWP is waiting. */
    821      1.22        ad 		sc = sip->sel_cluster;
    822      1.13        ad 		lock = sc->sc_lock;
    823      1.13        ad 		mutex_spin_enter(lock);
    824       1.1        ad 		/* Still there? */
    825       1.1        ad 		if (sip->sel_lwp != NULL) {
    826      1.23     rmind 			/*
    827      1.23     rmind 			 * Set the events for our LWP and indicate that.
    828      1.23     rmind 			 * Otherwise, request for a full re-scan.
    829      1.23     rmind 			 */
    830       1.1        ad 			l = sip->sel_lwp;
    831      1.23     rmind 			oflag = l->l_selflag;
    832      1.36     rmind 
    833      1.36     rmind 			if (!direct_select) {
    834      1.36     rmind 				l->l_selflag = SEL_RESET;
    835      1.36     rmind 			} else if (!sel_setevents(l, sip, events)) {
    836      1.30     rmind 				/* No events to return. */
    837      1.30     rmind 				mutex_spin_exit(lock);
    838      1.30     rmind 				return;
    839      1.30     rmind 			}
    840      1.36     rmind 
    841       1.1        ad 			/*
    842       1.1        ad 			 * If thread is sleeping, wake it up.  If it's not
    843       1.1        ad 			 * yet asleep, it will notice the change in state
    844       1.1        ad 			 * and will re-poll the descriptors.
    845       1.1        ad 			 */
    846      1.13        ad 			if (oflag == SEL_BLOCKING && l->l_mutex == lock) {
    847       1.1        ad 				KASSERT(l->l_wchan == sc);
    848      1.16     rmind 				sleepq_unsleep(l, false);
    849       1.1        ad 			}
    850       1.1        ad 		}
    851      1.13        ad 		mutex_spin_exit(lock);
    852       1.1        ad 	}
    853       1.1        ad 
    854       1.1        ad 	if ((mask = sip->sel_collision) != 0) {
    855       1.1        ad 		/*
    856       1.1        ad 		 * There was a collision (multiple waiters): we must
    857       1.1        ad 		 * inform all potentially interested waiters.
    858       1.1        ad 		 */
    859       1.1        ad 		sip->sel_collision = 0;
    860       1.3        ad 		do {
    861       1.1        ad 			index = ffs(mask) - 1;
    862       1.1        ad 			mask &= ~(1 << index);
    863      1.22        ad 			sc = selcluster[index];
    864      1.13        ad 			lock = sc->sc_lock;
    865      1.13        ad 			mutex_spin_enter(lock);
    866       1.1        ad 			sc->sc_ncoll++;
    867      1.13        ad 			sleepq_wake(&sc->sc_sleepq, sc, (u_int)-1, lock);
    868       1.3        ad 		} while (__predict_false(mask != 0));
    869       1.1        ad 	}
    870       1.1        ad }
    871       1.1        ad 
    872       1.1        ad /*
    873       1.1        ad  * Remove an LWP from all objects that it is waiting for.  Concurrency
    874       1.1        ad  * issues:
    875       1.1        ad  *
    876       1.1        ad  * The object owner's (e.g. device driver) lock is not held here.  Calls
    877       1.1        ad  * can be made to selrecord() and we do not synchronize against those
    878       1.1        ad  * directly using locks.  However, we use `sel_lwp' to lock out changes.
    879       1.1        ad  * Before clearing it we must use memory barriers to ensure that we can
    880       1.1        ad  * safely traverse the list of selinfo records.
    881       1.1        ad  */
    882       1.1        ad static void
    883       1.1        ad selclear(void)
    884       1.1        ad {
    885       1.1        ad 	struct selinfo *sip, *next;
    886      1.22        ad 	selcluster_t *sc;
    887       1.1        ad 	lwp_t *l;
    888      1.13        ad 	kmutex_t *lock;
    889       1.1        ad 
    890       1.1        ad 	l = curlwp;
    891      1.22        ad 	sc = l->l_selcluster;
    892      1.13        ad 	lock = sc->sc_lock;
    893       1.1        ad 
    894      1.13        ad 	mutex_spin_enter(lock);
    895       1.1        ad 	for (sip = SLIST_FIRST(&l->l_selwait); sip != NULL; sip = next) {
    896       1.1        ad 		KASSERT(sip->sel_lwp == l);
    897      1.22        ad 		KASSERT(sip->sel_cluster == l->l_selcluster);
    898      1.22        ad 
    899       1.1        ad 		/*
    900       1.1        ad 		 * Read link to next selinfo record, if any.
    901       1.1        ad 		 * It's no longer safe to touch `sip' after clearing
    902       1.1        ad 		 * `sel_lwp', so ensure that the read of `sel_chain'
    903       1.1        ad 		 * completes before the clearing of sel_lwp becomes
    904       1.1        ad 		 * globally visible.
    905       1.1        ad 		 */
    906       1.1        ad 		next = SLIST_NEXT(sip, sel_chain);
    907       1.1        ad 		membar_exit();
    908       1.1        ad 		/* Release the record for another named waiter to use. */
    909       1.1        ad 		sip->sel_lwp = NULL;
    910       1.1        ad 	}
    911      1.13        ad 	mutex_spin_exit(lock);
    912       1.1        ad }
    913       1.1        ad 
    914       1.1        ad /*
    915       1.1        ad  * Initialize the select/poll system calls.  Called once for each
    916       1.1        ad  * CPU in the system, as they are attached.
    917       1.1        ad  */
    918       1.1        ad void
    919       1.1        ad selsysinit(struct cpu_info *ci)
    920       1.1        ad {
    921      1.22        ad 	selcluster_t *sc;
    922      1.22        ad 	u_int index;
    923       1.1        ad 
    924      1.22        ad 	/* If already a cluster in place for this bit, re-use. */
    925      1.22        ad 	index = cpu_index(ci) & SELCLUSTERMASK;
    926      1.22        ad 	sc = selcluster[index];
    927      1.22        ad 	if (sc == NULL) {
    928      1.22        ad 		sc = kmem_alloc(roundup2(sizeof(selcluster_t),
    929      1.22        ad 		    coherency_unit) + coherency_unit, KM_SLEEP);
    930      1.22        ad 		sc = (void *)roundup2((uintptr_t)sc, coherency_unit);
    931      1.22        ad 		sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
    932      1.22        ad 		sleepq_init(&sc->sc_sleepq);
    933      1.22        ad 		sc->sc_ncoll = 0;
    934      1.46   msaitoh 		sc->sc_mask = __BIT(index);
    935      1.22        ad 		selcluster[index] = sc;
    936      1.22        ad 	}
    937      1.22        ad 	ci->ci_data.cpu_selcluster = sc;
    938       1.1        ad }
    939       1.1        ad 
    940       1.1        ad /*
    941       1.1        ad  * Initialize a selinfo record.
    942       1.1        ad  */
    943       1.1        ad void
    944       1.1        ad selinit(struct selinfo *sip)
    945       1.1        ad {
    946       1.1        ad 
    947       1.1        ad 	memset(sip, 0, sizeof(*sip));
    948       1.1        ad }
    949       1.1        ad 
    950       1.1        ad /*
    951       1.1        ad  * Destroy a selinfo record.  The owning object must not gain new
    952       1.1        ad  * references while this is in progress: all activity on the record
    953       1.1        ad  * must be stopped.
    954       1.1        ad  *
    955       1.1        ad  * Concurrency issues: we only need guard against a call to selclear()
    956      1.17     rmind  * by a thread exiting sel_do_scan().  The caller has prevented further
    957      1.17     rmind  * references being made to the selinfo record via selrecord(), and it
    958      1.23     rmind  * will not call selnotify() again.
    959       1.1        ad  */
    960       1.1        ad void
    961       1.1        ad seldestroy(struct selinfo *sip)
    962       1.1        ad {
    963      1.22        ad 	selcluster_t *sc;
    964      1.13        ad 	kmutex_t *lock;
    965       1.1        ad 	lwp_t *l;
    966       1.1        ad 
    967       1.1        ad 	if (sip->sel_lwp == NULL)
    968       1.1        ad 		return;
    969       1.1        ad 
    970       1.1        ad 	/*
    971      1.22        ad 	 * Lock out selclear().  The selcluster pointer can't change while
    972       1.1        ad 	 * we are here since it is only ever changed in selrecord(),
    973       1.1        ad 	 * and that will not be entered again for this record because
    974       1.1        ad 	 * it is dying.
    975       1.1        ad 	 */
    976      1.22        ad 	KASSERT(sip->sel_cluster != NULL);
    977      1.22        ad 	sc = sip->sel_cluster;
    978      1.13        ad 	lock = sc->sc_lock;
    979      1.13        ad 	mutex_spin_enter(lock);
    980       1.1        ad 	if ((l = sip->sel_lwp) != NULL) {
    981       1.1        ad 		/*
    982       1.1        ad 		 * This should rarely happen, so although SLIST_REMOVE()
    983       1.1        ad 		 * is slow, using it here is not a problem.
    984       1.1        ad 		 */
    985      1.22        ad 		KASSERT(l->l_selcluster == sc);
    986       1.1        ad 		SLIST_REMOVE(&l->l_selwait, sip, selinfo, sel_chain);
    987       1.1        ad 		sip->sel_lwp = NULL;
    988       1.1        ad 	}
    989      1.13        ad 	mutex_spin_exit(lock);
    990       1.1        ad }
    991       1.1        ad 
    992      1.36     rmind /*
    993      1.36     rmind  * System control nodes.
    994      1.36     rmind  */
    995      1.36     rmind SYSCTL_SETUP(sysctl_select_setup, "sysctl select setup")
    996      1.36     rmind {
    997      1.36     rmind 
    998      1.38     pooka 	sysctl_createv(clog, 0, NULL, NULL,
    999      1.36     rmind 		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1000      1.36     rmind 		CTLTYPE_INT, "direct_select",
   1001      1.36     rmind 		SYSCTL_DESCR("Enable/disable direct select (for testing)"),
   1002      1.36     rmind 		NULL, 0, &direct_select, 0,
   1003      1.38     pooka 		CTL_KERN, CTL_CREATE, CTL_EOL);
   1004      1.36     rmind }
   1005