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