sys_select.c revision 1.10.14.1 1 /* $NetBSD: sys_select.c,v 1.10.14.1 2015/04/24 05:46:09 msaitoh Exp $ */
2
3 /*-
4 * Copyright (c) 2007, 2008 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 relating to files.
70 */
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: sys_select.c,v 1.10.14.1 2015/04/24 05:46:09 msaitoh Exp $");
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/filedesc.h>
78 #include <sys/ioctl.h>
79 #include <sys/file.h>
80 #include <sys/proc.h>
81 #include <sys/socketvar.h>
82 #include <sys/signalvar.h>
83 #include <sys/uio.h>
84 #include <sys/kernel.h>
85 #include <sys/stat.h>
86 #include <sys/poll.h>
87 #include <sys/vnode.h>
88 #include <sys/mount.h>
89 #include <sys/syscallargs.h>
90 #include <sys/cpu.h>
91 #include <sys/atomic.h>
92 #include <sys/socketvar.h>
93 #include <sys/sleepq.h>
94
95 /* Flags for lwp::l_selflag. */
96 #define SEL_RESET 0 /* awoken, interrupted, or not yet polling */
97 #define SEL_SCANNING 1 /* polling descriptors */
98 #define SEL_BLOCKING 2 /* about to block on select_cv */
99
100 /* Per-CPU state for select()/poll(). */
101 #if MAXCPUS > 32
102 #error adjust this code
103 #endif
104 typedef struct selcpu {
105 kmutex_t sc_lock;
106 sleepq_t sc_sleepq;
107 int sc_ncoll;
108 uint32_t sc_mask;
109 } selcpu_t;
110
111 static int selscan(lwp_t *, fd_mask *, fd_mask *, int, register_t *);
112 static int pollscan(lwp_t *, struct pollfd *, int, register_t *);
113 static void selclear(void);
114
115 static syncobj_t select_sobj = {
116 SOBJ_SLEEPQ_FIFO,
117 sleepq_unsleep,
118 sleepq_changepri,
119 sleepq_lendpri,
120 syncobj_noowner,
121 };
122
123 /*
124 * Select system call.
125 */
126 int
127 sys_pselect(struct lwp *l, const struct sys_pselect_args *uap, register_t *retval)
128 {
129 /* {
130 syscallarg(int) nd;
131 syscallarg(fd_set *) in;
132 syscallarg(fd_set *) ou;
133 syscallarg(fd_set *) ex;
134 syscallarg(const struct timespec *) ts;
135 syscallarg(sigset_t *) mask;
136 } */
137 struct timespec ats;
138 struct timeval atv, *tv = NULL;
139 sigset_t amask, *mask = NULL;
140 int error;
141
142 if (SCARG(uap, ts)) {
143 error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
144 if (error)
145 return error;
146 atv.tv_sec = ats.tv_sec;
147 atv.tv_usec = ats.tv_nsec / 1000;
148 tv = &atv;
149 }
150 if (SCARG(uap, mask) != NULL) {
151 error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
152 if (error)
153 return error;
154 mask = &amask;
155 }
156
157 return selcommon(l, retval, SCARG(uap, nd), SCARG(uap, in),
158 SCARG(uap, ou), SCARG(uap, ex), tv, mask);
159 }
160
161 int
162 inittimeleft(struct timeval *tv, struct timeval *sleeptv)
163 {
164 if (itimerfix(tv))
165 return -1;
166 getmicrouptime(sleeptv);
167 return 0;
168 }
169
170 int
171 gettimeleft(struct timeval *tv, struct timeval *sleeptv)
172 {
173 /*
174 * We have to recalculate the timeout on every retry.
175 */
176 struct timeval slepttv;
177 /*
178 * reduce tv by elapsed time
179 * based on monotonic time scale
180 */
181 getmicrouptime(&slepttv);
182 timeradd(tv, sleeptv, tv);
183 timersub(tv, &slepttv, tv);
184 *sleeptv = slepttv;
185 return tvtohz(tv);
186 }
187
188 int
189 sys_select(struct lwp *l, const struct sys_select_args *uap, register_t *retval)
190 {
191 /* {
192 syscallarg(int) nd;
193 syscallarg(fd_set *) in;
194 syscallarg(fd_set *) ou;
195 syscallarg(fd_set *) ex;
196 syscallarg(struct timeval *) tv;
197 } */
198 struct timeval atv, *tv = NULL;
199 int error;
200
201 if (SCARG(uap, tv)) {
202 error = copyin(SCARG(uap, tv), (void *)&atv,
203 sizeof(atv));
204 if (error)
205 return error;
206 tv = &atv;
207 }
208
209 return selcommon(l, retval, SCARG(uap, nd), SCARG(uap, in),
210 SCARG(uap, ou), SCARG(uap, ex), tv, NULL);
211 }
212
213 int
214 selcommon(lwp_t *l, register_t *retval, int nd, fd_set *u_in,
215 fd_set *u_ou, fd_set *u_ex, struct timeval *tv, sigset_t *mask)
216 {
217 char smallbits[howmany(FD_SETSIZE, NFDBITS) *
218 sizeof(fd_mask) * 6];
219 proc_t * const p = l->l_proc;
220 char *bits;
221 int ncoll, error, timo;
222 size_t ni;
223 sigset_t oldmask;
224 struct timeval sleeptv;
225 selcpu_t *sc;
226
227 error = 0;
228 if (nd < 0)
229 return (EINVAL);
230 if (nd > p->p_fd->fd_nfiles) {
231 /* forgiving; slightly wrong */
232 nd = p->p_fd->fd_nfiles;
233 }
234 ni = howmany(nd, NFDBITS) * sizeof(fd_mask);
235 if (ni * 6 > sizeof(smallbits)) {
236 bits = kmem_alloc(ni * 6, KM_SLEEP);
237 if (bits == NULL)
238 return ENOMEM;
239 } else
240 bits = smallbits;
241
242 #define getbits(name, x) \
243 if (u_ ## name) { \
244 error = copyin(u_ ## name, bits + ni * x, ni); \
245 if (error) \
246 goto done; \
247 } else \
248 memset(bits + ni * x, 0, ni);
249 getbits(in, 0);
250 getbits(ou, 1);
251 getbits(ex, 2);
252 #undef getbits
253
254 timo = 0;
255 if (tv && inittimeleft(tv, &sleeptv) == -1) {
256 error = EINVAL;
257 goto done;
258 }
259
260 if (mask) {
261 sigminusset(&sigcantmask, mask);
262 mutex_enter(p->p_lock);
263 oldmask = l->l_sigmask;
264 l->l_sigmask = *mask;
265 mutex_exit(p->p_lock);
266 } else
267 oldmask = l->l_sigmask; /* XXXgcc */
268
269 sc = curcpu()->ci_data.cpu_selcpu;
270 l->l_selcpu = sc;
271 SLIST_INIT(&l->l_selwait);
272 for (;;) {
273 /*
274 * No need to lock. If this is overwritten by another
275 * value while scanning, we will retry below. We only
276 * need to see exact state from the descriptors that
277 * we are about to poll, and lock activity resulting
278 * from fo_poll is enough to provide an up to date value
279 * for new polling activity.
280 */
281 l->l_selflag = SEL_SCANNING;
282 ncoll = sc->sc_ncoll;
283
284 error = selscan(l, (fd_mask *)(bits + ni * 0),
285 (fd_mask *)(bits + ni * 3), nd, retval);
286
287 if (error || *retval)
288 break;
289 if (tv && (timo = gettimeleft(tv, &sleeptv)) <= 0)
290 break;
291 mutex_spin_enter(&sc->sc_lock);
292 if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
293 mutex_spin_exit(&sc->sc_lock);
294 continue;
295 }
296 l->l_selflag = SEL_BLOCKING;
297 l->l_kpriority = true;
298 sleepq_enter(&sc->sc_sleepq, l, &sc->sc_lock);
299 sleepq_enqueue(&sc->sc_sleepq, sc, "select", &select_sobj);
300 error = sleepq_block(timo, true);
301 if (error != 0)
302 break;
303 }
304 selclear();
305
306 if (mask) {
307 mutex_enter(p->p_lock);
308 l->l_sigmask = oldmask;
309 mutex_exit(p->p_lock);
310 }
311
312 done:
313 /* select is not restarted after signals... */
314 if (error == ERESTART)
315 error = EINTR;
316 if (error == EWOULDBLOCK)
317 error = 0;
318 if (error == 0 && u_in != NULL)
319 error = copyout(bits + ni * 3, u_in, ni);
320 if (error == 0 && u_ou != NULL)
321 error = copyout(bits + ni * 4, u_ou, ni);
322 if (error == 0 && u_ex != NULL)
323 error = copyout(bits + ni * 5, u_ex, ni);
324 if (bits != smallbits)
325 kmem_free(bits, ni * 6);
326 return (error);
327 }
328
329 int
330 selscan(lwp_t *l, fd_mask *ibitp, fd_mask *obitp, int nfd,
331 register_t *retval)
332 {
333 static const int flag[3] = { POLLRDNORM | POLLHUP | POLLERR,
334 POLLWRNORM | POLLHUP | POLLERR,
335 POLLRDBAND };
336 int msk, i, j, fd, n;
337 fd_mask ibits, obits;
338 file_t *fp;
339
340 n = 0;
341 for (msk = 0; msk < 3; msk++) {
342 for (i = 0; i < nfd; i += NFDBITS) {
343 ibits = *ibitp++;
344 obits = 0;
345 while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
346 ibits &= ~(1 << j);
347 if ((fp = fd_getfile(fd)) == NULL)
348 return (EBADF);
349 if ((*fp->f_ops->fo_poll)(fp, flag[msk])) {
350 obits |= (1 << j);
351 n++;
352 }
353 fd_putfile(fd);
354 }
355 *obitp++ = obits;
356 }
357 }
358 *retval = n;
359 return (0);
360 }
361
362 /*
363 * Poll system call.
364 */
365 int
366 sys_poll(struct lwp *l, const struct sys_poll_args *uap, register_t *retval)
367 {
368 /* {
369 syscallarg(struct pollfd *) fds;
370 syscallarg(u_int) nfds;
371 syscallarg(int) timeout;
372 } */
373 struct timeval atv, *tv = NULL;
374
375 if (SCARG(uap, timeout) != INFTIM) {
376 atv.tv_sec = SCARG(uap, timeout) / 1000;
377 atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
378 tv = &atv;
379 }
380
381 return pollcommon(l, retval, SCARG(uap, fds), SCARG(uap, nfds),
382 tv, NULL);
383 }
384
385 /*
386 * Poll system call.
387 */
388 int
389 sys_pollts(struct lwp *l, const struct sys_pollts_args *uap, register_t *retval)
390 {
391 /* {
392 syscallarg(struct pollfd *) fds;
393 syscallarg(u_int) nfds;
394 syscallarg(const struct timespec *) ts;
395 syscallarg(const sigset_t *) mask;
396 } */
397 struct timespec ats;
398 struct timeval atv, *tv = NULL;
399 sigset_t amask, *mask = NULL;
400 int error;
401
402 if (SCARG(uap, ts)) {
403 error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
404 if (error)
405 return error;
406 atv.tv_sec = ats.tv_sec;
407 atv.tv_usec = ats.tv_nsec / 1000;
408 tv = &atv;
409 }
410 if (SCARG(uap, mask)) {
411 error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
412 if (error)
413 return error;
414 mask = &amask;
415 }
416
417 return pollcommon(l, retval, SCARG(uap, fds), SCARG(uap, nfds),
418 tv, mask);
419 }
420
421 int
422 pollcommon(lwp_t *l, register_t *retval,
423 struct pollfd *u_fds, u_int nfds,
424 struct timeval *tv, sigset_t *mask)
425 {
426 char smallbits[32 * sizeof(struct pollfd)];
427 proc_t * const p = l->l_proc;
428 void * bits;
429 sigset_t oldmask;
430 int ncoll, error, timo;
431 size_t ni;
432 struct timeval sleeptv;
433 selcpu_t *sc;
434
435 if (nfds > 1000 + p->p_fd->fd_nfiles) {
436 /*
437 * Either the user passed in a very sparse 'fds' or junk!
438 * The kmem_alloc() call below would be bad news.
439 * We could process the 'fds' array in chunks, but that
440 * is a lot of code that isn't normally useful.
441 * (Or just move the copyin/out into pollscan().)
442 * Historically the code silently truncated 'fds' to
443 * dt_nfiles entries - but that does cause issues.
444 */
445 return EINVAL;
446 }
447 ni = nfds * sizeof(struct pollfd);
448 if (ni > sizeof(smallbits)) {
449 bits = kmem_alloc(ni, KM_SLEEP);
450 if (bits == NULL)
451 return ENOMEM;
452 } else
453 bits = smallbits;
454
455 error = copyin(u_fds, bits, ni);
456 if (error)
457 goto done;
458
459 timo = 0;
460 if (tv && inittimeleft(tv, &sleeptv) == -1) {
461 error = EINVAL;
462 goto done;
463 }
464
465 if (mask) {
466 sigminusset(&sigcantmask, mask);
467 mutex_enter(p->p_lock);
468 oldmask = l->l_sigmask;
469 l->l_sigmask = *mask;
470 mutex_exit(p->p_lock);
471 } else
472 oldmask = l->l_sigmask; /* XXXgcc */
473
474 sc = curcpu()->ci_data.cpu_selcpu;
475 l->l_selcpu = sc;
476 SLIST_INIT(&l->l_selwait);
477 for (;;) {
478 /*
479 * No need to lock. If this is overwritten by another
480 * value while scanning, we will retry below. We only
481 * need to see exact state from the descriptors that
482 * we are about to poll, and lock activity resulting
483 * from fo_poll is enough to provide an up to date value
484 * for new polling activity.
485 */
486 ncoll = sc->sc_ncoll;
487 l->l_selflag = SEL_SCANNING;
488
489 error = pollscan(l, (struct pollfd *)bits, nfds, retval);
490
491 if (error || *retval)
492 break;
493 if (tv && (timo = gettimeleft(tv, &sleeptv)) <= 0)
494 break;
495 mutex_spin_enter(&sc->sc_lock);
496 if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
497 mutex_spin_exit(&sc->sc_lock);
498 continue;
499 }
500 l->l_selflag = SEL_BLOCKING;
501 l->l_kpriority = true;
502 sleepq_enter(&sc->sc_sleepq, l, &sc->sc_lock);
503 sleepq_enqueue(&sc->sc_sleepq, sc, "select", &select_sobj);
504 error = sleepq_block(timo, true);
505 if (error != 0)
506 break;
507 }
508 selclear();
509
510 if (mask) {
511 mutex_enter(p->p_lock);
512 l->l_sigmask = oldmask;
513 mutex_exit(p->p_lock);
514 }
515 done:
516 /* poll is not restarted after signals... */
517 if (error == ERESTART)
518 error = EINTR;
519 if (error == EWOULDBLOCK)
520 error = 0;
521 if (error == 0)
522 error = copyout(bits, u_fds, ni);
523 if (bits != smallbits)
524 kmem_free(bits, ni);
525 return (error);
526 }
527
528 int
529 pollscan(lwp_t *l, struct pollfd *fds, int nfd, register_t *retval)
530 {
531 int i, n;
532 file_t *fp;
533
534 n = 0;
535 for (i = 0; i < nfd; i++, fds++) {
536 if (fds->fd < 0) {
537 fds->revents = 0;
538 } else if ((fp = fd_getfile(fds->fd)) == NULL) {
539 fds->revents = POLLNVAL;
540 n++;
541 } else {
542 fds->revents = (*fp->f_ops->fo_poll)(fp,
543 fds->events | POLLERR | POLLHUP);
544 if (fds->revents != 0)
545 n++;
546 fd_putfile(fds->fd);
547 }
548 }
549 *retval = n;
550 return (0);
551 }
552
553 /*ARGSUSED*/
554 int
555 seltrue(dev_t dev, int events, lwp_t *l)
556 {
557
558 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
559 }
560
561 /*
562 * Record a select request. Concurrency issues:
563 *
564 * The caller holds the same lock across calls to selrecord() and
565 * selnotify(), so we don't need to consider a concurrent wakeup
566 * while in this routine.
567 *
568 * The only activity we need to guard against is selclear(), called by
569 * another thread that is exiting selcommon() or pollcommon().
570 * `sel_lwp' can only become non-NULL while the caller's lock is held,
571 * so it cannot become non-NULL due to a change made by another thread
572 * while we are in this routine. It can only become _NULL_ due to a
573 * call to selclear().
574 *
575 * If it is non-NULL and != selector there is the potential for
576 * selclear() to be called by another thread. If either of those
577 * conditions are true, we're not interested in touching the `named
578 * waiter' part of the selinfo record because we need to record a
579 * collision. Hence there is no need for additional locking in this
580 * routine.
581 */
582 void
583 selrecord(lwp_t *selector, struct selinfo *sip)
584 {
585 selcpu_t *sc;
586 lwp_t *other;
587
588 KASSERT(selector == curlwp);
589
590 sc = selector->l_selcpu;
591 other = sip->sel_lwp;
592
593 if (other == selector) {
594 /* `selector' has already claimed it. */
595 KASSERT(sip->sel_cpu = sc);
596 } else if (other == NULL) {
597 /*
598 * First named waiter, although there may be unnamed
599 * waiters (collisions). Issue a memory barrier to
600 * ensure that we access sel_lwp (above) before other
601 * fields - this guards against a call to selclear().
602 */
603 membar_enter();
604 sip->sel_lwp = selector;
605 SLIST_INSERT_HEAD(&selector->l_selwait, sip, sel_chain);
606 /* Replace selinfo's lock with our chosen CPU's lock. */
607 sip->sel_cpu = sc;
608 } else {
609 /* Multiple waiters: record a collision. */
610 sip->sel_collision |= sc->sc_mask;
611 KASSERT(sip->sel_cpu != NULL);
612 }
613 }
614
615 /*
616 * Do a wakeup when a selectable event occurs. Concurrency issues:
617 *
618 * As per selrecord(), the caller's object lock is held. If there
619 * is a named waiter, we must acquire the associated selcpu's lock
620 * in order to synchronize with selclear() and pollers going to sleep
621 * in selcommon() and/or pollcommon().
622 *
623 * sip->sel_cpu cannot change at this point, as it is only changed
624 * in selrecord(), and concurrent calls to selrecord() are locked
625 * out by the caller.
626 */
627 void
628 selnotify(struct selinfo *sip, int events, long knhint)
629 {
630 selcpu_t *sc;
631 uint32_t mask;
632 int index, oflag, swapin;
633 lwp_t *l;
634
635 KNOTE(&sip->sel_klist, knhint);
636
637 if (sip->sel_lwp != NULL) {
638 /* One named LWP is waiting. */
639 swapin = 0;
640 sc = sip->sel_cpu;
641 mutex_spin_enter(&sc->sc_lock);
642 /* Still there? */
643 if (sip->sel_lwp != NULL) {
644 l = sip->sel_lwp;
645 /*
646 * If thread is sleeping, wake it up. If it's not
647 * yet asleep, it will notice the change in state
648 * and will re-poll the descriptors.
649 */
650 oflag = l->l_selflag;
651 l->l_selflag = SEL_RESET;
652 if (oflag == SEL_BLOCKING &&
653 l->l_mutex == &sc->sc_lock) {
654 KASSERT(l->l_wchan == sc);
655 swapin = sleepq_unsleep(l, false);
656 }
657 }
658 mutex_spin_exit(&sc->sc_lock);
659 if (swapin)
660 uvm_kick_scheduler();
661 }
662
663 if ((mask = sip->sel_collision) != 0) {
664 /*
665 * There was a collision (multiple waiters): we must
666 * inform all potentially interested waiters.
667 */
668 sip->sel_collision = 0;
669 do {
670 index = ffs(mask) - 1;
671 mask &= ~(1 << index);
672 sc = cpu_lookup(index)->ci_data.cpu_selcpu;
673 mutex_spin_enter(&sc->sc_lock);
674 sc->sc_ncoll++;
675 sleepq_wake(&sc->sc_sleepq, sc, (u_int)-1,
676 &sc->sc_lock);
677 } while (__predict_false(mask != 0));
678 }
679 }
680
681 /*
682 * Remove an LWP from all objects that it is waiting for. Concurrency
683 * issues:
684 *
685 * The object owner's (e.g. device driver) lock is not held here. Calls
686 * can be made to selrecord() and we do not synchronize against those
687 * directly using locks. However, we use `sel_lwp' to lock out changes.
688 * Before clearing it we must use memory barriers to ensure that we can
689 * safely traverse the list of selinfo records.
690 */
691 static void
692 selclear(void)
693 {
694 struct selinfo *sip, *next;
695 selcpu_t *sc;
696 lwp_t *l;
697
698 l = curlwp;
699 sc = l->l_selcpu;
700
701 mutex_spin_enter(&sc->sc_lock);
702 for (sip = SLIST_FIRST(&l->l_selwait); sip != NULL; sip = next) {
703 KASSERT(sip->sel_lwp == l);
704 KASSERT(sip->sel_cpu == l->l_selcpu);
705 /*
706 * Read link to next selinfo record, if any.
707 * It's no longer safe to touch `sip' after clearing
708 * `sel_lwp', so ensure that the read of `sel_chain'
709 * completes before the clearing of sel_lwp becomes
710 * globally visible.
711 */
712 next = SLIST_NEXT(sip, sel_chain);
713 membar_exit();
714 /* Release the record for another named waiter to use. */
715 sip->sel_lwp = NULL;
716 }
717 mutex_spin_exit(&sc->sc_lock);
718 }
719
720 /*
721 * Initialize the select/poll system calls. Called once for each
722 * CPU in the system, as they are attached.
723 */
724 void
725 selsysinit(struct cpu_info *ci)
726 {
727 selcpu_t *sc;
728
729 sc = kmem_alloc(roundup2(sizeof(selcpu_t), coherency_unit) +
730 coherency_unit, KM_SLEEP);
731 sc = (void *)roundup2((uintptr_t)sc, coherency_unit);
732 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SCHED);
733 sleepq_init(&sc->sc_sleepq);
734 sc->sc_ncoll = 0;
735 sc->sc_mask = (1 << cpu_index(ci));
736 ci->ci_data.cpu_selcpu = sc;
737 }
738
739 /*
740 * Initialize a selinfo record.
741 */
742 void
743 selinit(struct selinfo *sip)
744 {
745
746 memset(sip, 0, sizeof(*sip));
747 }
748
749 /*
750 * Destroy a selinfo record. The owning object must not gain new
751 * references while this is in progress: all activity on the record
752 * must be stopped.
753 *
754 * Concurrency issues: we only need guard against a call to selclear()
755 * by a thread exiting selcommon() and/or pollcommon(). The caller has
756 * prevented further references being made to the selinfo record via
757 * selrecord(), and it won't call selwakeup() again.
758 */
759 void
760 seldestroy(struct selinfo *sip)
761 {
762 selcpu_t *sc;
763 lwp_t *l;
764
765 if (sip->sel_lwp == NULL)
766 return;
767
768 /*
769 * Lock out selclear(). The selcpu pointer can't change while
770 * we are here since it is only ever changed in selrecord(),
771 * and that will not be entered again for this record because
772 * it is dying.
773 */
774 KASSERT(sip->sel_cpu != NULL);
775 sc = sip->sel_cpu;
776 mutex_spin_enter(&sc->sc_lock);
777 if ((l = sip->sel_lwp) != NULL) {
778 /*
779 * This should rarely happen, so although SLIST_REMOVE()
780 * is slow, using it here is not a problem.
781 */
782 KASSERT(l->l_selcpu == sc);
783 SLIST_REMOVE(&l->l_selwait, sip, selinfo, sel_chain);
784 sip->sel_lwp = NULL;
785 }
786 mutex_spin_exit(&sc->sc_lock);
787 }
788
789 int
790 pollsock(struct socket *so, const struct timeval *tvp, int events)
791 {
792 int ncoll, error, timo;
793 struct timeval sleeptv, tv;
794 selcpu_t *sc;
795 lwp_t *l;
796
797 timo = 0;
798 if (tvp != NULL) {
799 tv = *tvp;
800 if (inittimeleft(&tv, &sleeptv) == -1)
801 return EINVAL;
802 }
803
804 l = curlwp;
805 sc = l->l_cpu->ci_data.cpu_selcpu;
806 l->l_selcpu = sc;
807 SLIST_INIT(&l->l_selwait);
808 error = 0;
809 for (;;) {
810 /*
811 * No need to lock. If this is overwritten by another
812 * value while scanning, we will retry below. We only
813 * need to see exact state from the descriptors that
814 * we are about to poll, and lock activity resulting
815 * from fo_poll is enough to provide an up to date value
816 * for new polling activity.
817 */
818 ncoll = sc->sc_ncoll;
819 l->l_selflag = SEL_SCANNING;
820 if (sopoll(so, events) != 0)
821 break;
822 if (tvp && (timo = gettimeleft(&tv, &sleeptv)) <= 0)
823 break;
824 mutex_spin_enter(&sc->sc_lock);
825 if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
826 mutex_spin_exit(&sc->sc_lock);
827 continue;
828 }
829 l->l_selflag = SEL_BLOCKING;
830 sleepq_enter(&sc->sc_sleepq, l, &sc->sc_lock);
831 sleepq_enqueue(&sc->sc_sleepq, sc, "pollsock", &select_sobj);
832 error = sleepq_block(timo, true);
833 if (error != 0)
834 break;
835 }
836 selclear();
837 /* poll is not restarted after signals... */
838 if (error == ERESTART)
839 error = EINTR;
840 if (error == EWOULDBLOCK)
841 error = 0;
842 return (error);
843 }
844