2011-05-28 19:33:40 +04:00
|
|
|
/* $NetBSD: sys_select.c,v 1.33 2011/05/28 15:33:41 christos Exp $ */
|
2008-03-23 17:02:49 +03:00
|
|
|
|
|
|
|
/*-
|
2010-04-25 19:55:24 +04:00
|
|
|
* Copyright (c) 2007, 2008, 2009, 2010 The NetBSD Foundation, Inc.
|
2008-03-23 17:02:49 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2010-07-08 16:23:31 +04:00
|
|
|
* by Andrew Doran and Mindaugas Rasiukevicius.
|
2008-03-23 17:02:49 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1982, 1986, 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
* (c) UNIX System Laboratories, Inc.
|
|
|
|
* All or some portions of this file are derived from material licensed
|
|
|
|
* to the University of California by American Telephone and Telegraph
|
|
|
|
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
|
|
|
|
* the permission of UNIX System Laboratories, Inc.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2009-12-21 02:00:59 +03:00
|
|
|
* System calls of synchronous I/O multiplexing subsystem.
|
|
|
|
*
|
|
|
|
* Locking
|
|
|
|
*
|
2010-04-25 19:55:24 +04:00
|
|
|
* Two locks are used: <object-lock> and selcluster_t::sc_lock.
|
2009-12-21 02:00:59 +03:00
|
|
|
*
|
|
|
|
* The <object-lock> might be a device driver or another subsystem, e.g.
|
|
|
|
* socket or pipe. This lock is not exported, and thus invisible to this
|
|
|
|
* subsystem. Mainly, synchronisation between selrecord() and selnotify()
|
|
|
|
* routines depends on this lock, as it will be described in the comments.
|
|
|
|
*
|
|
|
|
* Lock order
|
|
|
|
*
|
|
|
|
* <object-lock> ->
|
2010-04-25 19:55:24 +04:00
|
|
|
* selcluster_t::sc_lock
|
2008-03-23 17:02:49 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2011-05-28 19:33:40 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: sys_select.c,v 1.33 2011/05/28 15:33:41 christos Exp $");
|
2008-03-23 17:02:49 +03:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/signalvar.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/kernel.h>
|
2010-12-18 04:36:19 +03:00
|
|
|
#include <sys/lwp.h>
|
2008-03-23 17:02:49 +03:00
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/syscallargs.h>
|
|
|
|
#include <sys/cpu.h>
|
|
|
|
#include <sys/atomic.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/sleepq.h>
|
|
|
|
|
|
|
|
/* Flags for lwp::l_selflag. */
|
|
|
|
#define SEL_RESET 0 /* awoken, interrupted, or not yet polling */
|
|
|
|
#define SEL_SCANNING 1 /* polling descriptors */
|
2010-07-08 16:23:31 +04:00
|
|
|
#define SEL_BLOCKING 2 /* blocking and waiting for event */
|
|
|
|
#define SEL_EVENT 3 /* interrupted, events set directly */
|
|
|
|
|
|
|
|
/* Operations: either select() or poll(). */
|
|
|
|
#define SELOP_SELECT 1
|
|
|
|
#define SELOP_POLL 2
|
2008-03-23 17:02:49 +03:00
|
|
|
|
2010-04-25 19:55:24 +04:00
|
|
|
/*
|
|
|
|
* Per-cluster state for select()/poll(). For a system with fewer
|
|
|
|
* than 32 CPUs, this gives us per-CPU clusters.
|
|
|
|
*/
|
|
|
|
#define SELCLUSTERS 32
|
|
|
|
#define SELCLUSTERMASK (SELCLUSTERS - 1)
|
|
|
|
|
|
|
|
typedef struct selcluster {
|
2009-03-21 16:11:14 +03:00
|
|
|
kmutex_t *sc_lock;
|
2008-03-23 17:02:49 +03:00
|
|
|
sleepq_t sc_sleepq;
|
|
|
|
int sc_ncoll;
|
|
|
|
uint32_t sc_mask;
|
2010-04-25 19:55:24 +04:00
|
|
|
} selcluster_t;
|
2008-03-23 17:02:49 +03:00
|
|
|
|
2010-07-08 16:23:31 +04:00
|
|
|
static inline int selscan(char *, const int, const size_t, register_t *);
|
|
|
|
static inline int pollscan(struct pollfd *, const int, register_t *);
|
2009-11-11 12:48:50 +03:00
|
|
|
static void selclear(void);
|
2008-03-23 17:02:49 +03:00
|
|
|
|
2010-07-08 16:23:31 +04:00
|
|
|
static const int sel_flag[] = {
|
|
|
|
POLLRDNORM | POLLHUP | POLLERR,
|
|
|
|
POLLWRNORM | POLLHUP | POLLERR,
|
|
|
|
POLLRDBAND
|
|
|
|
};
|
|
|
|
|
2008-03-23 17:02:49 +03:00
|
|
|
static syncobj_t select_sobj = {
|
|
|
|
SOBJ_SLEEPQ_FIFO,
|
|
|
|
sleepq_unsleep,
|
|
|
|
sleepq_changepri,
|
|
|
|
sleepq_lendpri,
|
|
|
|
syncobj_noowner,
|
|
|
|
};
|
|
|
|
|
2010-07-08 16:23:31 +04:00
|
|
|
static selcluster_t *selcluster[SELCLUSTERS] __read_mostly;
|
2010-04-25 19:55:24 +04:00
|
|
|
|
2008-03-23 17:02:49 +03:00
|
|
|
/*
|
|
|
|
* Select system call.
|
|
|
|
*/
|
|
|
|
int
|
2009-01-11 05:45:45 +03:00
|
|
|
sys___pselect50(struct lwp *l, const struct sys___pselect50_args *uap,
|
|
|
|
register_t *retval)
|
2008-03-23 17:02:49 +03:00
|
|
|
{
|
|
|
|
/* {
|
|
|
|
syscallarg(int) nd;
|
|
|
|
syscallarg(fd_set *) in;
|
|
|
|
syscallarg(fd_set *) ou;
|
|
|
|
syscallarg(fd_set *) ex;
|
|
|
|
syscallarg(const struct timespec *) ts;
|
|
|
|
syscallarg(sigset_t *) mask;
|
|
|
|
} */
|
2009-03-29 23:21:19 +04:00
|
|
|
struct timespec ats, *ts = NULL;
|
2008-03-23 17:02:49 +03:00
|
|
|
sigset_t amask, *mask = NULL;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (SCARG(uap, ts)) {
|
|
|
|
error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
|
|
|
|
if (error)
|
|
|
|
return error;
|
2009-03-29 23:21:19 +04:00
|
|
|
ts = &ats;
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
if (SCARG(uap, mask) != NULL) {
|
|
|
|
error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
mask = &amask;
|
|
|
|
}
|
|
|
|
|
2009-11-11 12:48:50 +03:00
|
|
|
return selcommon(retval, SCARG(uap, nd), SCARG(uap, in),
|
2009-03-29 23:21:19 +04:00
|
|
|
SCARG(uap, ou), SCARG(uap, ex), ts, mask);
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-01-11 05:45:45 +03:00
|
|
|
sys___select50(struct lwp *l, const struct sys___select50_args *uap,
|
|
|
|
register_t *retval)
|
2008-03-23 17:02:49 +03:00
|
|
|
{
|
|
|
|
/* {
|
|
|
|
syscallarg(int) nd;
|
|
|
|
syscallarg(fd_set *) in;
|
|
|
|
syscallarg(fd_set *) ou;
|
|
|
|
syscallarg(fd_set *) ex;
|
|
|
|
syscallarg(struct timeval *) tv;
|
|
|
|
} */
|
2009-03-29 23:21:19 +04:00
|
|
|
struct timeval atv;
|
|
|
|
struct timespec ats, *ts = NULL;
|
2008-03-23 17:02:49 +03:00
|
|
|
int error;
|
|
|
|
|
|
|
|
if (SCARG(uap, tv)) {
|
2009-03-29 23:21:19 +04:00
|
|
|
error = copyin(SCARG(uap, tv), (void *)&atv, sizeof(atv));
|
2008-03-23 17:02:49 +03:00
|
|
|
if (error)
|
|
|
|
return error;
|
2009-03-29 23:21:19 +04:00
|
|
|
TIMEVAL_TO_TIMESPEC(&atv, &ats);
|
|
|
|
ts = &ats;
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
|
2009-11-11 12:48:50 +03:00
|
|
|
return selcommon(retval, SCARG(uap, nd), SCARG(uap, in),
|
2009-03-29 23:21:19 +04:00
|
|
|
SCARG(uap, ou), SCARG(uap, ex), ts, NULL);
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
|
2009-11-02 00:14:21 +03:00
|
|
|
/*
|
|
|
|
* sel_do_scan: common code to perform the scan on descriptors.
|
|
|
|
*/
|
|
|
|
static int
|
2010-07-08 16:23:31 +04:00
|
|
|
sel_do_scan(const int op, void *fds, const int nf, const size_t ni,
|
|
|
|
struct timespec *ts, sigset_t *mask, register_t *retval)
|
2008-03-23 17:02:49 +03:00
|
|
|
{
|
2009-11-02 00:14:21 +03:00
|
|
|
lwp_t * const l = curlwp;
|
2010-04-25 19:55:24 +04:00
|
|
|
selcluster_t *sc;
|
2009-03-21 16:11:14 +03:00
|
|
|
kmutex_t *lock;
|
2009-11-02 00:14:21 +03:00
|
|
|
struct timespec sleepts;
|
|
|
|
int error, timo;
|
2008-03-23 17:02:49 +03:00
|
|
|
|
|
|
|
timo = 0;
|
2009-03-29 23:21:19 +04:00
|
|
|
if (ts && inittimeleft(ts, &sleepts) == -1) {
|
2009-11-02 00:14:21 +03:00
|
|
|
return EINVAL;
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
|
2011-05-18 18:48:04 +04:00
|
|
|
if (__predict_false(mask))
|
2011-05-18 07:51:41 +04:00
|
|
|
sigsuspendsetup(l, mask);
|
2008-03-23 17:02:49 +03:00
|
|
|
|
2010-04-25 19:55:24 +04:00
|
|
|
sc = curcpu()->ci_data.cpu_selcluster;
|
2009-03-21 16:11:14 +03:00
|
|
|
lock = sc->sc_lock;
|
2010-04-25 19:55:24 +04:00
|
|
|
l->l_selcluster = sc;
|
2008-03-23 17:02:49 +03:00
|
|
|
SLIST_INIT(&l->l_selwait);
|
2010-07-08 16:23:31 +04:00
|
|
|
|
|
|
|
l->l_selret = 0;
|
|
|
|
if (op == SELOP_SELECT) {
|
2011-03-06 07:41:58 +03:00
|
|
|
l->l_selbits = fds;
|
2010-07-08 16:23:31 +04:00
|
|
|
l->l_selni = ni;
|
|
|
|
} else {
|
|
|
|
l->l_selbits = NULL;
|
|
|
|
}
|
2008-03-23 17:02:49 +03:00
|
|
|
for (;;) {
|
2009-11-02 00:14:21 +03:00
|
|
|
int ncoll;
|
|
|
|
|
2008-03-23 17:02:49 +03:00
|
|
|
/*
|
2009-11-02 00:14:21 +03:00
|
|
|
* No need to lock. If this is overwritten by another value
|
|
|
|
* while scanning, we will retry below. We only need to see
|
|
|
|
* exact state from the descriptors that we are about to poll,
|
|
|
|
* and lock activity resulting from fo_poll is enough to
|
|
|
|
* provide an up to date value for new polling activity.
|
2008-03-23 17:02:49 +03:00
|
|
|
*/
|
2009-11-02 00:14:21 +03:00
|
|
|
l->l_selflag = SEL_SCANNING;
|
2008-03-23 17:02:49 +03:00
|
|
|
ncoll = sc->sc_ncoll;
|
|
|
|
|
2010-07-08 16:23:31 +04:00
|
|
|
if (op == SELOP_SELECT) {
|
|
|
|
error = selscan((char *)fds, nf, ni, retval);
|
2009-11-02 00:14:21 +03:00
|
|
|
} else {
|
2010-07-08 16:23:31 +04:00
|
|
|
error = pollscan((struct pollfd *)fds, nf, retval);
|
2009-11-02 00:14:21 +03:00
|
|
|
}
|
2008-03-23 17:02:49 +03:00
|
|
|
if (error || *retval)
|
|
|
|
break;
|
2009-03-29 23:21:19 +04:00
|
|
|
if (ts && (timo = gettimeleft(ts, &sleepts)) <= 0)
|
2008-03-23 17:02:49 +03:00
|
|
|
break;
|
2010-07-08 16:23:31 +04:00
|
|
|
/*
|
|
|
|
* Acquire the lock and perform the (re)checks. Note, if
|
|
|
|
* collision has occured, then our state does not matter,
|
|
|
|
* as we must perform re-scan. Therefore, check it first.
|
|
|
|
*/
|
|
|
|
state_check:
|
2009-03-21 16:11:14 +03:00
|
|
|
mutex_spin_enter(lock);
|
2010-07-08 16:23:31 +04:00
|
|
|
if (__predict_false(sc->sc_ncoll != ncoll)) {
|
|
|
|
/* Collision: perform re-scan. */
|
2009-03-21 16:11:14 +03:00
|
|
|
mutex_spin_exit(lock);
|
2008-03-23 17:02:49 +03:00
|
|
|
continue;
|
|
|
|
}
|
2010-07-08 16:23:31 +04:00
|
|
|
if (__predict_true(l->l_selflag == SEL_EVENT)) {
|
|
|
|
/* Events occured, they are set directly. */
|
|
|
|
mutex_spin_exit(lock);
|
|
|
|
KASSERT(l->l_selret != 0);
|
|
|
|
*retval = l->l_selret;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (__predict_true(l->l_selflag == SEL_RESET)) {
|
|
|
|
/* Events occured, but re-scan is requested. */
|
|
|
|
mutex_spin_exit(lock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Nothing happen, therefore - sleep. */
|
2008-03-23 17:02:49 +03:00
|
|
|
l->l_selflag = SEL_BLOCKING;
|
2008-04-30 16:45:21 +04:00
|
|
|
l->l_kpriority = true;
|
2009-03-21 16:11:14 +03:00
|
|
|
sleepq_enter(&sc->sc_sleepq, l, lock);
|
2008-03-23 17:02:49 +03:00
|
|
|
sleepq_enqueue(&sc->sc_sleepq, sc, "select", &select_sobj);
|
|
|
|
error = sleepq_block(timo, true);
|
2010-07-08 16:23:31 +04:00
|
|
|
if (error != 0) {
|
2008-03-23 17:02:49 +03:00
|
|
|
break;
|
2010-07-08 16:23:31 +04:00
|
|
|
}
|
|
|
|
/* Awoken: need to check the state. */
|
|
|
|
goto state_check;
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
selclear();
|
|
|
|
|
2011-05-28 19:33:40 +04:00
|
|
|
if (__predict_false(mask))
|
|
|
|
sigsuspendteardown(l);
|
|
|
|
|
2009-12-12 20:47:05 +03:00
|
|
|
/* select and poll are not restarted after signals... */
|
|
|
|
if (error == ERESTART)
|
|
|
|
return EINTR;
|
|
|
|
if (error == EWOULDBLOCK)
|
|
|
|
return 0;
|
2009-11-02 00:14:21 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-11-11 12:48:50 +03:00
|
|
|
selcommon(register_t *retval, int nd, fd_set *u_in, fd_set *u_ou,
|
|
|
|
fd_set *u_ex, struct timespec *ts, sigset_t *mask)
|
2009-11-02 00:14:21 +03:00
|
|
|
{
|
|
|
|
char smallbits[howmany(FD_SETSIZE, NFDBITS) *
|
|
|
|
sizeof(fd_mask) * 6];
|
|
|
|
char *bits;
|
|
|
|
int error, nf;
|
|
|
|
size_t ni;
|
|
|
|
|
|
|
|
if (nd < 0)
|
|
|
|
return (EINVAL);
|
2009-11-11 12:48:50 +03:00
|
|
|
nf = curlwp->l_fd->fd_dt->dt_nfiles;
|
2009-11-02 00:14:21 +03:00
|
|
|
if (nd > nf) {
|
|
|
|
/* forgiving; slightly wrong */
|
|
|
|
nd = nf;
|
|
|
|
}
|
|
|
|
ni = howmany(nd, NFDBITS) * sizeof(fd_mask);
|
|
|
|
if (ni * 6 > sizeof(smallbits)) {
|
|
|
|
bits = kmem_alloc(ni * 6, KM_SLEEP);
|
|
|
|
if (bits == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
} else
|
|
|
|
bits = smallbits;
|
|
|
|
|
|
|
|
#define getbits(name, x) \
|
|
|
|
if (u_ ## name) { \
|
|
|
|
error = copyin(u_ ## name, bits + ni * x, ni); \
|
|
|
|
if (error) \
|
2009-12-12 20:47:05 +03:00
|
|
|
goto fail; \
|
2009-11-02 00:14:21 +03:00
|
|
|
} else \
|
|
|
|
memset(bits + ni * x, 0, ni);
|
|
|
|
getbits(in, 0);
|
|
|
|
getbits(ou, 1);
|
|
|
|
getbits(ex, 2);
|
|
|
|
#undef getbits
|
2008-03-23 17:02:49 +03:00
|
|
|
|
2010-07-08 16:23:31 +04:00
|
|
|
error = sel_do_scan(SELOP_SELECT, bits, nd, ni, ts, mask, retval);
|
2008-03-23 17:02:49 +03:00
|
|
|
if (error == 0 && u_in != NULL)
|
|
|
|
error = copyout(bits + ni * 3, u_in, ni);
|
|
|
|
if (error == 0 && u_ou != NULL)
|
|
|
|
error = copyout(bits + ni * 4, u_ou, ni);
|
|
|
|
if (error == 0 && u_ex != NULL)
|
|
|
|
error = copyout(bits + ni * 5, u_ex, ni);
|
2009-12-12 20:47:05 +03:00
|
|
|
fail:
|
2008-03-23 17:02:49 +03:00
|
|
|
if (bits != smallbits)
|
|
|
|
kmem_free(bits, ni * 6);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2009-11-11 12:48:50 +03:00
|
|
|
static inline int
|
2010-07-08 16:23:31 +04:00
|
|
|
selscan(char *bits, const int nfd, const size_t ni, register_t *retval)
|
2008-03-23 17:02:49 +03:00
|
|
|
{
|
2009-11-02 00:14:21 +03:00
|
|
|
fd_mask *ibitp, *obitp;
|
2010-07-08 16:23:31 +04:00
|
|
|
int msk, i, j, fd, n;
|
2008-03-23 17:02:49 +03:00
|
|
|
file_t *fp;
|
|
|
|
|
2009-11-02 00:14:21 +03:00
|
|
|
ibitp = (fd_mask *)(bits + ni * 0);
|
|
|
|
obitp = (fd_mask *)(bits + ni * 3);
|
2008-03-23 17:02:49 +03:00
|
|
|
n = 0;
|
2009-11-02 00:14:21 +03:00
|
|
|
|
2008-03-23 17:02:49 +03:00
|
|
|
for (msk = 0; msk < 3; msk++) {
|
|
|
|
for (i = 0; i < nfd; i += NFDBITS) {
|
2010-07-08 16:23:31 +04:00
|
|
|
fd_mask ibits, obits;
|
|
|
|
|
2008-03-23 17:02:49 +03:00
|
|
|
ibits = *ibitp++;
|
|
|
|
obits = 0;
|
|
|
|
while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
|
|
|
|
ibits &= ~(1 << j);
|
|
|
|
if ((fp = fd_getfile(fd)) == NULL)
|
|
|
|
return (EBADF);
|
2010-07-08 16:23:31 +04:00
|
|
|
/*
|
|
|
|
* Setup an argument to selrecord(), which is
|
|
|
|
* a file descriptor number.
|
|
|
|
*/
|
|
|
|
curlwp->l_selrec = fd;
|
|
|
|
if ((*fp->f_ops->fo_poll)(fp, sel_flag[msk])) {
|
2008-03-23 17:02:49 +03:00
|
|
|
obits |= (1 << j);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
fd_putfile(fd);
|
|
|
|
}
|
|
|
|
*obitp++ = obits;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*retval = n;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Poll system call.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sys_poll(struct lwp *l, const struct sys_poll_args *uap, register_t *retval)
|
|
|
|
{
|
|
|
|
/* {
|
|
|
|
syscallarg(struct pollfd *) fds;
|
|
|
|
syscallarg(u_int) nfds;
|
|
|
|
syscallarg(int) timeout;
|
|
|
|
} */
|
2009-03-29 23:21:19 +04:00
|
|
|
struct timespec ats, *ts = NULL;
|
2008-03-23 17:02:49 +03:00
|
|
|
|
|
|
|
if (SCARG(uap, timeout) != INFTIM) {
|
2009-03-29 23:21:19 +04:00
|
|
|
ats.tv_sec = SCARG(uap, timeout) / 1000;
|
|
|
|
ats.tv_nsec = (SCARG(uap, timeout) % 1000) * 1000000;
|
|
|
|
ts = &ats;
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
|
2009-11-11 12:48:50 +03:00
|
|
|
return pollcommon(retval, SCARG(uap, fds), SCARG(uap, nfds), ts, NULL);
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Poll system call.
|
|
|
|
*/
|
|
|
|
int
|
2009-01-11 05:45:45 +03:00
|
|
|
sys___pollts50(struct lwp *l, const struct sys___pollts50_args *uap,
|
|
|
|
register_t *retval)
|
2008-03-23 17:02:49 +03:00
|
|
|
{
|
|
|
|
/* {
|
|
|
|
syscallarg(struct pollfd *) fds;
|
|
|
|
syscallarg(u_int) nfds;
|
|
|
|
syscallarg(const struct timespec *) ts;
|
|
|
|
syscallarg(const sigset_t *) mask;
|
|
|
|
} */
|
2009-03-29 23:21:19 +04:00
|
|
|
struct timespec ats, *ts = NULL;
|
2008-03-23 17:02:49 +03:00
|
|
|
sigset_t amask, *mask = NULL;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (SCARG(uap, ts)) {
|
|
|
|
error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
|
|
|
|
if (error)
|
|
|
|
return error;
|
2009-03-29 23:21:19 +04:00
|
|
|
ts = &ats;
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
if (SCARG(uap, mask)) {
|
|
|
|
error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
|
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
mask = &amask;
|
|
|
|
}
|
|
|
|
|
2009-11-11 12:48:50 +03:00
|
|
|
return pollcommon(retval, SCARG(uap, fds), SCARG(uap, nfds), ts, mask);
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-11-11 12:48:50 +03:00
|
|
|
pollcommon(register_t *retval, struct pollfd *u_fds, u_int nfds,
|
2009-03-29 23:21:19 +04:00
|
|
|
struct timespec *ts, sigset_t *mask)
|
2008-03-23 17:02:49 +03:00
|
|
|
{
|
2008-11-20 04:25:28 +03:00
|
|
|
struct pollfd smallfds[32];
|
|
|
|
struct pollfd *fds;
|
2009-11-02 00:14:21 +03:00
|
|
|
int error;
|
2009-12-12 20:47:05 +03:00
|
|
|
size_t ni;
|
2008-03-23 17:02:49 +03:00
|
|
|
|
2009-12-12 20:47:05 +03:00
|
|
|
if (nfds > 1000 + curlwp->l_fd->fd_dt->dt_nfiles) {
|
|
|
|
/*
|
|
|
|
* Either the user passed in a very sparse 'fds' or junk!
|
|
|
|
* The kmem_alloc() call below would be bad news.
|
|
|
|
* We could process the 'fds' array in chunks, but that
|
|
|
|
* is a lot of code that isn't normally useful.
|
|
|
|
* (Or just move the copyin/out into pollscan().)
|
|
|
|
* Historically the code silently truncated 'fds' to
|
|
|
|
* dt_nfiles entries - but that does cause issues.
|
|
|
|
*/
|
|
|
|
return EINVAL;
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
ni = nfds * sizeof(struct pollfd);
|
2008-11-20 04:25:28 +03:00
|
|
|
if (ni > sizeof(smallfds)) {
|
|
|
|
fds = kmem_alloc(ni, KM_SLEEP);
|
|
|
|
if (fds == NULL)
|
2008-06-04 17:02:41 +04:00
|
|
|
return ENOMEM;
|
|
|
|
} else
|
2008-11-20 04:25:28 +03:00
|
|
|
fds = smallfds;
|
2008-03-23 17:02:49 +03:00
|
|
|
|
2008-11-20 04:25:28 +03:00
|
|
|
error = copyin(u_fds, fds, ni);
|
2008-03-23 17:02:49 +03:00
|
|
|
if (error)
|
2009-12-12 20:47:05 +03:00
|
|
|
goto fail;
|
2008-03-23 17:02:49 +03:00
|
|
|
|
2010-07-08 16:23:31 +04:00
|
|
|
error = sel_do_scan(SELOP_POLL, fds, nfds, ni, ts, mask, retval);
|
2008-03-23 17:02:49 +03:00
|
|
|
if (error == 0)
|
2008-11-20 04:25:28 +03:00
|
|
|
error = copyout(fds, u_fds, ni);
|
2009-12-12 20:47:05 +03:00
|
|
|
fail:
|
2008-11-20 04:25:28 +03:00
|
|
|
if (fds != smallfds)
|
|
|
|
kmem_free(fds, ni);
|
2008-03-23 17:02:49 +03:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2009-11-11 12:48:50 +03:00
|
|
|
static inline int
|
2010-07-08 16:23:31 +04:00
|
|
|
pollscan(struct pollfd *fds, const int nfd, register_t *retval)
|
2008-03-23 17:02:49 +03:00
|
|
|
{
|
|
|
|
file_t *fp;
|
2010-07-08 16:23:31 +04:00
|
|
|
int i, n = 0;
|
2008-03-23 17:02:49 +03:00
|
|
|
|
|
|
|
for (i = 0; i < nfd; i++, fds++) {
|
|
|
|
if (fds->fd < 0) {
|
|
|
|
fds->revents = 0;
|
|
|
|
} else if ((fp = fd_getfile(fds->fd)) == NULL) {
|
|
|
|
fds->revents = POLLNVAL;
|
|
|
|
n++;
|
|
|
|
} else {
|
2010-07-08 16:23:31 +04:00
|
|
|
/*
|
|
|
|
* Perform poll: registers select request or returns
|
|
|
|
* the events which are set. Setup an argument for
|
|
|
|
* selrecord(), which is a pointer to struct pollfd.
|
|
|
|
*/
|
|
|
|
curlwp->l_selrec = (uintptr_t)fds;
|
2008-03-23 17:02:49 +03:00
|
|
|
fds->revents = (*fp->f_ops->fo_poll)(fp,
|
|
|
|
fds->events | POLLERR | POLLHUP);
|
|
|
|
if (fds->revents != 0)
|
|
|
|
n++;
|
|
|
|
fd_putfile(fds->fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*retval = n;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
seltrue(dev_t dev, int events, lwp_t *l)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Record a select request. Concurrency issues:
|
|
|
|
*
|
|
|
|
* The caller holds the same lock across calls to selrecord() and
|
2008-04-17 18:02:24 +04:00
|
|
|
* selnotify(), so we don't need to consider a concurrent wakeup
|
2008-03-23 17:02:49 +03:00
|
|
|
* while in this routine.
|
|
|
|
*
|
|
|
|
* The only activity we need to guard against is selclear(), called by
|
2009-11-02 00:14:21 +03:00
|
|
|
* another thread that is exiting sel_do_scan().
|
2008-03-23 17:02:49 +03:00
|
|
|
* `sel_lwp' can only become non-NULL while the caller's lock is held,
|
|
|
|
* so it cannot become non-NULL due to a change made by another thread
|
|
|
|
* while we are in this routine. It can only become _NULL_ due to a
|
|
|
|
* call to selclear().
|
|
|
|
*
|
|
|
|
* If it is non-NULL and != selector there is the potential for
|
|
|
|
* selclear() to be called by another thread. If either of those
|
|
|
|
* conditions are true, we're not interested in touching the `named
|
|
|
|
* waiter' part of the selinfo record because we need to record a
|
|
|
|
* collision. Hence there is no need for additional locking in this
|
|
|
|
* routine.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
selrecord(lwp_t *selector, struct selinfo *sip)
|
|
|
|
{
|
2010-04-25 19:55:24 +04:00
|
|
|
selcluster_t *sc;
|
2008-03-23 17:02:49 +03:00
|
|
|
lwp_t *other;
|
|
|
|
|
|
|
|
KASSERT(selector == curlwp);
|
|
|
|
|
2010-04-25 19:55:24 +04:00
|
|
|
sc = selector->l_selcluster;
|
2008-03-23 17:02:49 +03:00
|
|
|
other = sip->sel_lwp;
|
|
|
|
|
|
|
|
if (other == selector) {
|
2010-07-08 16:23:31 +04:00
|
|
|
/* 1. We (selector) already claimed to be the first LWP. */
|
2010-04-25 19:55:24 +04:00
|
|
|
KASSERT(sip->sel_cluster = sc);
|
2008-03-23 17:02:49 +03:00
|
|
|
} else if (other == NULL) {
|
|
|
|
/*
|
2010-07-08 16:23:31 +04:00
|
|
|
* 2. No first LWP, therefore we (selector) are the first.
|
|
|
|
*
|
|
|
|
* There may be unnamed waiters (collisions). Issue a memory
|
|
|
|
* barrier to ensure that we access sel_lwp (above) before
|
|
|
|
* other fields - this guards against a call to selclear().
|
2008-03-23 17:02:49 +03:00
|
|
|
*/
|
|
|
|
membar_enter();
|
|
|
|
sip->sel_lwp = selector;
|
|
|
|
SLIST_INSERT_HEAD(&selector->l_selwait, sip, sel_chain);
|
2010-07-08 16:23:31 +04:00
|
|
|
/* Copy the argument, which is for selnotify(). */
|
|
|
|
sip->sel_fdinfo = selector->l_selrec;
|
2010-04-25 19:55:24 +04:00
|
|
|
/* Replace selinfo's lock with the chosen cluster's lock. */
|
|
|
|
sip->sel_cluster = sc;
|
2008-03-23 17:02:49 +03:00
|
|
|
} else {
|
2010-07-08 16:23:31 +04:00
|
|
|
/* 3. Multiple waiters: record a collision. */
|
2008-03-23 17:02:49 +03:00
|
|
|
sip->sel_collision |= sc->sc_mask;
|
2010-04-25 19:55:24 +04:00
|
|
|
KASSERT(sip->sel_cluster != NULL);
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-08 16:23:31 +04:00
|
|
|
/*
|
|
|
|
* sel_setevents: a helper function for selnotify(), to set the events
|
|
|
|
* for LWP sleeping in selcommon() or pollcommon().
|
|
|
|
*/
|
2011-03-06 07:41:58 +03:00
|
|
|
static inline bool
|
2010-07-08 16:23:31 +04:00
|
|
|
sel_setevents(lwp_t *l, struct selinfo *sip, const int events)
|
|
|
|
{
|
|
|
|
const int oflag = l->l_selflag;
|
2011-03-06 07:41:58 +03:00
|
|
|
int ret = 0;
|
2010-07-08 16:23:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we require re-scan or it was required by somebody else,
|
|
|
|
* then just (re)set SEL_RESET and return.
|
|
|
|
*/
|
|
|
|
if (__predict_false(events == 0 || oflag == SEL_RESET)) {
|
|
|
|
l->l_selflag = SEL_RESET;
|
2011-03-06 07:41:58 +03:00
|
|
|
return true;
|
2010-07-08 16:23:31 +04:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Direct set. Note: select state of LWP is locked. First,
|
|
|
|
* determine whether it is selcommon() or pollcommon().
|
|
|
|
*/
|
|
|
|
if (l->l_selbits != NULL) {
|
2010-07-10 21:04:25 +04:00
|
|
|
const size_t ni = l->l_selni;
|
2011-03-06 07:41:58 +03:00
|
|
|
fd_mask *fds = (fd_mask *)l->l_selbits;
|
|
|
|
fd_mask *ofds = (fd_mask *)((char *)fds + ni * 3);
|
|
|
|
const int fd = sip->sel_fdinfo, fbit = 1 << (fd & __NFDMASK);
|
2010-07-10 21:04:25 +04:00
|
|
|
const int idx = fd >> __NFDSHIFT;
|
2010-07-08 16:23:31 +04:00
|
|
|
int n;
|
|
|
|
|
|
|
|
for (n = 0; n < 3; n++) {
|
2011-03-06 07:41:58 +03:00
|
|
|
if ((fds[idx] & fbit) != 0 && (sel_flag[n] & events)) {
|
|
|
|
ofds[idx] |= fbit;
|
|
|
|
ret++;
|
2010-07-08 16:23:31 +04:00
|
|
|
}
|
|
|
|
fds = (fd_mask *)((char *)fds + ni);
|
2011-03-06 07:41:58 +03:00
|
|
|
ofds = (fd_mask *)((char *)ofds + ni);
|
2010-07-08 16:23:31 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct pollfd *pfd = (void *)sip->sel_fdinfo;
|
2011-03-06 07:41:58 +03:00
|
|
|
int revents = events & (pfd->events | POLLERR | POLLHUP);
|
|
|
|
|
|
|
|
if (revents) {
|
|
|
|
pfd->revents |= revents;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Check whether there are any events to return. */
|
|
|
|
if (!ret) {
|
|
|
|
return false;
|
2010-07-08 16:23:31 +04:00
|
|
|
}
|
|
|
|
/* Indicate direct set and note the event (cluster lock is held). */
|
|
|
|
l->l_selflag = SEL_EVENT;
|
2011-03-06 07:41:58 +03:00
|
|
|
l->l_selret += ret;
|
|
|
|
return true;
|
2010-07-08 16:23:31 +04:00
|
|
|
}
|
|
|
|
|
2008-03-23 17:02:49 +03:00
|
|
|
/*
|
|
|
|
* Do a wakeup when a selectable event occurs. Concurrency issues:
|
|
|
|
*
|
|
|
|
* As per selrecord(), the caller's object lock is held. If there
|
2010-04-25 19:55:24 +04:00
|
|
|
* is a named waiter, we must acquire the associated selcluster's lock
|
2008-03-23 17:02:49 +03:00
|
|
|
* in order to synchronize with selclear() and pollers going to sleep
|
2009-11-02 00:14:21 +03:00
|
|
|
* in sel_do_scan().
|
2008-03-23 17:02:49 +03:00
|
|
|
*
|
2010-04-25 19:55:24 +04:00
|
|
|
* sip->sel_cluser cannot change at this point, as it is only changed
|
2008-03-23 17:02:49 +03:00
|
|
|
* in selrecord(), and concurrent calls to selrecord() are locked
|
|
|
|
* out by the caller.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
selnotify(struct selinfo *sip, int events, long knhint)
|
|
|
|
{
|
2010-04-25 19:55:24 +04:00
|
|
|
selcluster_t *sc;
|
2008-03-23 17:02:49 +03:00
|
|
|
uint32_t mask;
|
2009-10-22 01:11:57 +04:00
|
|
|
int index, oflag;
|
2008-03-23 17:02:49 +03:00
|
|
|
lwp_t *l;
|
2009-03-21 16:11:14 +03:00
|
|
|
kmutex_t *lock;
|
2008-03-23 17:02:49 +03:00
|
|
|
|
|
|
|
KNOTE(&sip->sel_klist, knhint);
|
|
|
|
|
|
|
|
if (sip->sel_lwp != NULL) {
|
|
|
|
/* One named LWP is waiting. */
|
2010-04-25 19:55:24 +04:00
|
|
|
sc = sip->sel_cluster;
|
2009-03-21 16:11:14 +03:00
|
|
|
lock = sc->sc_lock;
|
|
|
|
mutex_spin_enter(lock);
|
2008-03-23 17:02:49 +03:00
|
|
|
/* Still there? */
|
|
|
|
if (sip->sel_lwp != NULL) {
|
2010-07-08 16:23:31 +04:00
|
|
|
/*
|
|
|
|
* Set the events for our LWP and indicate that.
|
|
|
|
* Otherwise, request for a full re-scan.
|
|
|
|
*/
|
2008-03-23 17:02:49 +03:00
|
|
|
l = sip->sel_lwp;
|
2010-07-08 16:23:31 +04:00
|
|
|
oflag = l->l_selflag;
|
2010-10-15 09:39:19 +04:00
|
|
|
#ifndef NO_DIRECT_SELECT
|
2011-03-06 07:41:58 +03:00
|
|
|
if (!sel_setevents(l, sip, events)) {
|
|
|
|
/* No events to return. */
|
|
|
|
mutex_spin_exit(lock);
|
|
|
|
return;
|
|
|
|
}
|
2010-07-11 15:17:58 +04:00
|
|
|
#else
|
|
|
|
l->l_selflag = SEL_RESET;
|
|
|
|
#endif
|
2008-03-23 17:02:49 +03:00
|
|
|
/*
|
|
|
|
* If thread is sleeping, wake it up. If it's not
|
|
|
|
* yet asleep, it will notice the change in state
|
|
|
|
* and will re-poll the descriptors.
|
|
|
|
*/
|
2009-03-21 16:11:14 +03:00
|
|
|
if (oflag == SEL_BLOCKING && l->l_mutex == lock) {
|
2008-03-23 17:02:49 +03:00
|
|
|
KASSERT(l->l_wchan == sc);
|
2009-10-22 01:11:57 +04:00
|
|
|
sleepq_unsleep(l, false);
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
}
|
2009-03-21 16:11:14 +03:00
|
|
|
mutex_spin_exit(lock);
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((mask = sip->sel_collision) != 0) {
|
|
|
|
/*
|
|
|
|
* There was a collision (multiple waiters): we must
|
|
|
|
* inform all potentially interested waiters.
|
|
|
|
*/
|
|
|
|
sip->sel_collision = 0;
|
2008-03-29 17:08:35 +03:00
|
|
|
do {
|
2008-03-23 17:02:49 +03:00
|
|
|
index = ffs(mask) - 1;
|
|
|
|
mask &= ~(1 << index);
|
2010-04-25 19:55:24 +04:00
|
|
|
sc = selcluster[index];
|
2009-03-21 16:11:14 +03:00
|
|
|
lock = sc->sc_lock;
|
|
|
|
mutex_spin_enter(lock);
|
2008-03-23 17:02:49 +03:00
|
|
|
sc->sc_ncoll++;
|
2009-03-21 16:11:14 +03:00
|
|
|
sleepq_wake(&sc->sc_sleepq, sc, (u_int)-1, lock);
|
2008-03-29 17:08:35 +03:00
|
|
|
} while (__predict_false(mask != 0));
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove an LWP from all objects that it is waiting for. Concurrency
|
|
|
|
* issues:
|
|
|
|
*
|
|
|
|
* The object owner's (e.g. device driver) lock is not held here. Calls
|
|
|
|
* can be made to selrecord() and we do not synchronize against those
|
|
|
|
* directly using locks. However, we use `sel_lwp' to lock out changes.
|
|
|
|
* Before clearing it we must use memory barriers to ensure that we can
|
|
|
|
* safely traverse the list of selinfo records.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
selclear(void)
|
|
|
|
{
|
|
|
|
struct selinfo *sip, *next;
|
2010-04-25 19:55:24 +04:00
|
|
|
selcluster_t *sc;
|
2008-03-23 17:02:49 +03:00
|
|
|
lwp_t *l;
|
2009-03-21 16:11:14 +03:00
|
|
|
kmutex_t *lock;
|
2008-03-23 17:02:49 +03:00
|
|
|
|
|
|
|
l = curlwp;
|
2010-04-25 19:55:24 +04:00
|
|
|
sc = l->l_selcluster;
|
2009-03-21 16:11:14 +03:00
|
|
|
lock = sc->sc_lock;
|
2008-03-23 17:02:49 +03:00
|
|
|
|
2009-03-21 16:11:14 +03:00
|
|
|
mutex_spin_enter(lock);
|
2008-03-23 17:02:49 +03:00
|
|
|
for (sip = SLIST_FIRST(&l->l_selwait); sip != NULL; sip = next) {
|
|
|
|
KASSERT(sip->sel_lwp == l);
|
2010-04-25 19:55:24 +04:00
|
|
|
KASSERT(sip->sel_cluster == l->l_selcluster);
|
|
|
|
|
2008-03-23 17:02:49 +03:00
|
|
|
/*
|
|
|
|
* Read link to next selinfo record, if any.
|
|
|
|
* It's no longer safe to touch `sip' after clearing
|
|
|
|
* `sel_lwp', so ensure that the read of `sel_chain'
|
|
|
|
* completes before the clearing of sel_lwp becomes
|
|
|
|
* globally visible.
|
|
|
|
*/
|
|
|
|
next = SLIST_NEXT(sip, sel_chain);
|
|
|
|
membar_exit();
|
|
|
|
/* Release the record for another named waiter to use. */
|
|
|
|
sip->sel_lwp = NULL;
|
|
|
|
}
|
2009-03-21 16:11:14 +03:00
|
|
|
mutex_spin_exit(lock);
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the select/poll system calls. Called once for each
|
|
|
|
* CPU in the system, as they are attached.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
selsysinit(struct cpu_info *ci)
|
|
|
|
{
|
2010-04-25 19:55:24 +04:00
|
|
|
selcluster_t *sc;
|
|
|
|
u_int index;
|
|
|
|
|
|
|
|
/* If already a cluster in place for this bit, re-use. */
|
|
|
|
index = cpu_index(ci) & SELCLUSTERMASK;
|
|
|
|
sc = selcluster[index];
|
|
|
|
if (sc == NULL) {
|
|
|
|
sc = kmem_alloc(roundup2(sizeof(selcluster_t),
|
|
|
|
coherency_unit) + coherency_unit, KM_SLEEP);
|
|
|
|
sc = (void *)roundup2((uintptr_t)sc, coherency_unit);
|
|
|
|
sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SCHED);
|
|
|
|
sleepq_init(&sc->sc_sleepq);
|
|
|
|
sc->sc_ncoll = 0;
|
|
|
|
sc->sc_mask = (1 << index);
|
|
|
|
selcluster[index] = sc;
|
|
|
|
}
|
|
|
|
ci->ci_data.cpu_selcluster = sc;
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize a selinfo record.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
selinit(struct selinfo *sip)
|
|
|
|
{
|
|
|
|
|
|
|
|
memset(sip, 0, sizeof(*sip));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy a selinfo record. The owning object must not gain new
|
|
|
|
* references while this is in progress: all activity on the record
|
|
|
|
* must be stopped.
|
|
|
|
*
|
|
|
|
* Concurrency issues: we only need guard against a call to selclear()
|
2009-11-02 00:14:21 +03:00
|
|
|
* by a thread exiting sel_do_scan(). The caller has prevented further
|
|
|
|
* references being made to the selinfo record via selrecord(), and it
|
2010-07-08 16:23:31 +04:00
|
|
|
* will not call selnotify() again.
|
2008-03-23 17:02:49 +03:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
seldestroy(struct selinfo *sip)
|
|
|
|
{
|
2010-04-25 19:55:24 +04:00
|
|
|
selcluster_t *sc;
|
2009-03-21 16:11:14 +03:00
|
|
|
kmutex_t *lock;
|
2008-03-23 17:02:49 +03:00
|
|
|
lwp_t *l;
|
|
|
|
|
|
|
|
if (sip->sel_lwp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
2010-04-25 19:55:24 +04:00
|
|
|
* Lock out selclear(). The selcluster pointer can't change while
|
2008-03-23 17:02:49 +03:00
|
|
|
* we are here since it is only ever changed in selrecord(),
|
|
|
|
* and that will not be entered again for this record because
|
|
|
|
* it is dying.
|
|
|
|
*/
|
2010-04-25 19:55:24 +04:00
|
|
|
KASSERT(sip->sel_cluster != NULL);
|
|
|
|
sc = sip->sel_cluster;
|
2009-03-21 16:11:14 +03:00
|
|
|
lock = sc->sc_lock;
|
|
|
|
mutex_spin_enter(lock);
|
2008-03-23 17:02:49 +03:00
|
|
|
if ((l = sip->sel_lwp) != NULL) {
|
|
|
|
/*
|
|
|
|
* This should rarely happen, so although SLIST_REMOVE()
|
|
|
|
* is slow, using it here is not a problem.
|
|
|
|
*/
|
2010-04-25 19:55:24 +04:00
|
|
|
KASSERT(l->l_selcluster == sc);
|
2008-03-23 17:02:49 +03:00
|
|
|
SLIST_REMOVE(&l->l_selwait, sip, selinfo, sel_chain);
|
|
|
|
sip->sel_lwp = NULL;
|
|
|
|
}
|
2009-03-21 16:11:14 +03:00
|
|
|
mutex_spin_exit(lock);
|
2008-03-23 17:02:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-03-29 23:21:19 +04:00
|
|
|
pollsock(struct socket *so, const struct timespec *tsp, int events)
|
2008-03-23 17:02:49 +03:00
|
|
|
{
|
|
|
|
int ncoll, error, timo;
|
2009-03-29 23:21:19 +04:00
|
|
|
struct timespec sleepts, ts;
|
2010-04-25 19:55:24 +04:00
|
|
|
selcluster_t *sc;
|
2008-03-23 17:02:49 +03:00
|
|
|
lwp_t *l;
|
2009-03-21 16:11:14 +03:00
|
|
|
kmutex_t *lock;
|
2008-03-23 17:02:49 +03:00
|
|
|
|
|
|
|
timo = 0;
|
2009-03-29 23:21:19 +04:00
|
|
|
if (tsp != NULL) {
|
|
|
|
ts = *tsp;
|
|
|
|
if (inittimeleft(&ts, &sleepts) == -1)
|
2008-03-23 17:02:49 +03:00
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
l = curlwp;
|
2010-04-25 19:55:24 +04:00
|
|
|
sc = curcpu()->ci_data.cpu_selcluster;
|
2009-03-21 16:11:14 +03:00
|
|
|
lock = sc->sc_lock;
|
2010-04-25 19:55:24 +04:00
|
|
|
l->l_selcluster = sc;
|
2008-03-23 17:02:49 +03:00
|
|
|
SLIST_INIT(&l->l_selwait);
|
|
|
|
error = 0;
|
|
|
|
for (;;) {
|
|
|
|
/*
|
|
|
|
* No need to lock. If this is overwritten by another
|
|
|
|
* value while scanning, we will retry below. We only
|
|
|
|
* need to see exact state from the descriptors that
|
|
|
|
* we are about to poll, and lock activity resulting
|
|
|
|
* from fo_poll is enough to provide an up to date value
|
|
|
|
* for new polling activity.
|
|
|
|
*/
|
|
|
|
ncoll = sc->sc_ncoll;
|
|
|
|
l->l_selflag = SEL_SCANNING;
|
|
|
|
if (sopoll(so, events) != 0)
|
|
|
|
break;
|
2009-03-29 23:21:19 +04:00
|
|
|
if (tsp && (timo = gettimeleft(&ts, &sleepts)) <= 0)
|
2008-03-23 17:02:49 +03:00
|
|
|
break;
|
2009-03-21 16:11:14 +03:00
|
|
|
mutex_spin_enter(lock);
|
2008-03-23 17:02:49 +03:00
|
|
|
if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
|
2009-03-21 16:11:14 +03:00
|
|
|
mutex_spin_exit(lock);
|
2008-03-23 17:02:49 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
l->l_selflag = SEL_BLOCKING;
|
2009-03-21 16:11:14 +03:00
|
|
|
sleepq_enter(&sc->sc_sleepq, l, lock);
|
2008-03-23 17:02:49 +03:00
|
|
|
sleepq_enqueue(&sc->sc_sleepq, sc, "pollsock", &select_sobj);
|
|
|
|
error = sleepq_block(timo, true);
|
|
|
|
if (error != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
selclear();
|
|
|
|
/* poll is not restarted after signals... */
|
|
|
|
if (error == ERESTART)
|
|
|
|
error = EINTR;
|
|
|
|
if (error == EWOULDBLOCK)
|
|
|
|
error = 0;
|
|
|
|
return (error);
|
|
|
|
}
|