2 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
3 * Copyright 2004 John-Mark Gurney <jmg@FreeBSD.org>
4 * Copyright (c) 2009 Apple, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_ktrace.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/mutex.h>
40 #include <sys/malloc.h>
41 #include <sys/unistd.h>
43 #include <sys/filedesc.h>
44 #include <sys/filio.h>
45 #include <sys/fcntl.h>
46 #include <sys/kthread.h>
47 #include <sys/selinfo.h>
48 #include <sys/queue.h>
49 #include <sys/event.h>
50 #include <sys/eventvar.h>
52 #include <sys/protosw.h>
53 #include <sys/sigio.h>
54 #include <sys/signalvar.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/sysproto.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/taskqueue.h>
64 #include <sys/ktrace.h>
69 static MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
72 * This lock is used if multiple kq locks are required. This possibly
73 * should be made into a per proc lock.
75 static struct mtx kq_global;
76 MTX_SYSINIT(kq_global, &kq_global, "kqueue order", MTX_DEF);
77 #define KQ_GLOBAL_LOCK(lck, haslck) do { \
82 #define KQ_GLOBAL_UNLOCK(lck, haslck) do { \
88 TASKQUEUE_DEFINE_THREAD(kqueue);
90 static int kevent_copyout(void *arg, struct kevent *kevp, int count);
91 static int kevent_copyin(void *arg, struct kevent *kevp, int count);
92 static int kqueue_register(struct kqueue *kq, struct kevent *kev,
93 struct thread *td, int waitok);
94 static int kqueue_acquire(struct file *fp, struct kqueue **kqp);
95 static void kqueue_release(struct kqueue *kq, int locked);
96 static int kqueue_expand(struct kqueue *kq, struct filterops *fops,
97 uintptr_t ident, int waitok);
98 static void kqueue_task(void *arg, int pending);
99 static int kqueue_scan(struct kqueue *kq, int maxevents,
100 struct kevent_copyops *k_ops,
101 const struct timespec *timeout,
102 struct kevent *keva, struct thread *td);
103 static void kqueue_wakeup(struct kqueue *kq);
104 static struct filterops *kqueue_fo_find(int filt);
105 static void kqueue_fo_release(int filt);
107 static fo_rdwr_t kqueue_read;
108 static fo_rdwr_t kqueue_write;
109 static fo_truncate_t kqueue_truncate;
110 static fo_ioctl_t kqueue_ioctl;
111 static fo_poll_t kqueue_poll;
112 static fo_kqfilter_t kqueue_kqfilter;
113 static fo_stat_t kqueue_stat;
114 static fo_close_t kqueue_close;
116 static struct fileops kqueueops = {
117 .fo_read = kqueue_read,
118 .fo_write = kqueue_write,
119 .fo_truncate = kqueue_truncate,
120 .fo_ioctl = kqueue_ioctl,
121 .fo_poll = kqueue_poll,
122 .fo_kqfilter = kqueue_kqfilter,
123 .fo_stat = kqueue_stat,
124 .fo_close = kqueue_close,
127 static int knote_attach(struct knote *kn, struct kqueue *kq);
128 static void knote_drop(struct knote *kn, struct thread *td);
129 static void knote_enqueue(struct knote *kn);
130 static void knote_dequeue(struct knote *kn);
131 static void knote_init(void);
132 static struct knote *knote_alloc(int waitok);
133 static void knote_free(struct knote *kn);
135 static void filt_kqdetach(struct knote *kn);
136 static int filt_kqueue(struct knote *kn, long hint);
137 static int filt_procattach(struct knote *kn);
138 static void filt_procdetach(struct knote *kn);
139 static int filt_proc(struct knote *kn, long hint);
140 static int filt_fileattach(struct knote *kn);
141 static void filt_timerexpire(void *knx);
142 static int filt_timerattach(struct knote *kn);
143 static void filt_timerdetach(struct knote *kn);
144 static int filt_timer(struct knote *kn, long hint);
145 static int filt_userattach(struct knote *kn);
146 static void filt_userdetach(struct knote *kn);
147 static int filt_user(struct knote *kn, long hint);
148 static void filt_usertouch(struct knote *kn, struct kevent *kev,
151 static struct filterops file_filtops = {
153 .f_attach = filt_fileattach,
155 static struct filterops kqread_filtops = {
157 .f_detach = filt_kqdetach,
158 .f_event = filt_kqueue,
160 /* XXX - move to kern_proc.c? */
161 static struct filterops proc_filtops = {
163 .f_attach = filt_procattach,
164 .f_detach = filt_procdetach,
165 .f_event = filt_proc,
167 static struct filterops timer_filtops = {
169 .f_attach = filt_timerattach,
170 .f_detach = filt_timerdetach,
171 .f_event = filt_timer,
173 static struct filterops user_filtops = {
174 .f_attach = filt_userattach,
175 .f_detach = filt_userdetach,
176 .f_event = filt_user,
177 .f_touch = filt_usertouch,
180 static uma_zone_t knote_zone;
181 static int kq_ncallouts = 0;
182 static int kq_calloutmax = (4 * 1024);
183 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
184 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
186 /* XXX - ensure not KN_INFLUX?? */
187 #define KNOTE_ACTIVATE(kn, islock) do { \
189 mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED); \
191 KQ_LOCK((kn)->kn_kq); \
192 (kn)->kn_status |= KN_ACTIVE; \
193 if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
194 knote_enqueue((kn)); \
196 KQ_UNLOCK((kn)->kn_kq); \
198 #define KQ_LOCK(kq) do { \
199 mtx_lock(&(kq)->kq_lock); \
201 #define KQ_FLUX_WAKEUP(kq) do { \
202 if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) { \
203 (kq)->kq_state &= ~KQ_FLUXWAIT; \
207 #define KQ_UNLOCK_FLUX(kq) do { \
208 KQ_FLUX_WAKEUP(kq); \
209 mtx_unlock(&(kq)->kq_lock); \
211 #define KQ_UNLOCK(kq) do { \
212 mtx_unlock(&(kq)->kq_lock); \
214 #define KQ_OWNED(kq) do { \
215 mtx_assert(&(kq)->kq_lock, MA_OWNED); \
217 #define KQ_NOTOWNED(kq) do { \
218 mtx_assert(&(kq)->kq_lock, MA_NOTOWNED); \
220 #define KN_LIST_LOCK(kn) do { \
221 if (kn->kn_knlist != NULL) \
222 kn->kn_knlist->kl_lock(kn->kn_knlist->kl_lockarg); \
224 #define KN_LIST_UNLOCK(kn) do { \
225 if (kn->kn_knlist != NULL) \
226 kn->kn_knlist->kl_unlock(kn->kn_knlist->kl_lockarg); \
228 #define KNL_ASSERT_LOCK(knl, islocked) do { \
230 KNL_ASSERT_LOCKED(knl); \
232 KNL_ASSERT_UNLOCKED(knl); \
235 #define KNL_ASSERT_LOCKED(knl) do { \
236 knl->kl_assert_locked((knl)->kl_lockarg); \
238 #define KNL_ASSERT_UNLOCKED(knl) do { \
239 knl->kl_assert_unlocked((knl)->kl_lockarg); \
241 #else /* !INVARIANTS */
242 #define KNL_ASSERT_LOCKED(knl) do {} while(0)
243 #define KNL_ASSERT_UNLOCKED(knl) do {} while (0)
244 #endif /* INVARIANTS */
246 #define KN_HASHSIZE 64 /* XXX should be tunable */
247 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
250 filt_nullattach(struct knote *kn)
256 struct filterops null_filtops = {
258 .f_attach = filt_nullattach,
261 /* XXX - make SYSINIT to add these, and move into respective modules. */
262 extern struct filterops sig_filtops;
263 extern struct filterops fs_filtops;
266 * Table for for all system-defined filters.
268 static struct mtx filterops_lock;
269 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops",
272 struct filterops *for_fop;
274 } sysfilt_ops[EVFILT_SYSCOUNT] = {
275 { &file_filtops }, /* EVFILT_READ */
276 { &file_filtops }, /* EVFILT_WRITE */
277 { &null_filtops }, /* EVFILT_AIO */
278 { &file_filtops }, /* EVFILT_VNODE */
279 { &proc_filtops }, /* EVFILT_PROC */
280 { &sig_filtops }, /* EVFILT_SIGNAL */
281 { &timer_filtops }, /* EVFILT_TIMER */
282 { &null_filtops }, /* former EVFILT_NETDEV */
283 { &fs_filtops }, /* EVFILT_FS */
284 { &null_filtops }, /* EVFILT_LIO */
285 { &user_filtops }, /* EVFILT_USER */
289 * Simple redirection for all cdevsw style objects to call their fo_kqfilter
293 filt_fileattach(struct knote *kn)
296 return (fo_kqfilter(kn->kn_fp, kn));
301 kqueue_kqfilter(struct file *fp, struct knote *kn)
303 struct kqueue *kq = kn->kn_fp->f_data;
305 if (kn->kn_filter != EVFILT_READ)
308 kn->kn_status |= KN_KQUEUE;
309 kn->kn_fop = &kqread_filtops;
310 knlist_add(&kq->kq_sel.si_note, kn, 0);
316 filt_kqdetach(struct knote *kn)
318 struct kqueue *kq = kn->kn_fp->f_data;
320 knlist_remove(&kq->kq_sel.si_note, kn, 0);
325 filt_kqueue(struct knote *kn, long hint)
327 struct kqueue *kq = kn->kn_fp->f_data;
329 kn->kn_data = kq->kq_count;
330 return (kn->kn_data > 0);
333 /* XXX - move to kern_proc.c? */
335 filt_procattach(struct knote *kn)
342 p = pfind(kn->kn_id);
343 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
344 p = zpfind(kn->kn_id);
346 } else if (p != NULL && (p->p_flag & P_WEXIT)) {
352 if ((error = p_cansee(curthread, p))) {
357 kn->kn_ptr.p_proc = p;
358 kn->kn_flags |= EV_CLEAR; /* automatically set */
361 * internal flag indicating registration done by kernel
363 if (kn->kn_flags & EV_FLAG1) {
364 kn->kn_data = kn->kn_sdata; /* ppid */
365 kn->kn_fflags = NOTE_CHILD;
366 kn->kn_flags &= ~EV_FLAG1;
370 knlist_add(&p->p_klist, kn, 1);
373 * Immediately activate any exit notes if the target process is a
374 * zombie. This is necessary to handle the case where the target
375 * process, e.g. a child, dies before the kevent is registered.
377 if (immediate && filt_proc(kn, NOTE_EXIT))
378 KNOTE_ACTIVATE(kn, 0);
386 * The knote may be attached to a different process, which may exit,
387 * leaving nothing for the knote to be attached to. So when the process
388 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
389 * it will be deleted when read out. However, as part of the knote deletion,
390 * this routine is called, so a check is needed to avoid actually performing
391 * a detach, because the original process does not exist any more.
393 /* XXX - move to kern_proc.c? */
395 filt_procdetach(struct knote *kn)
399 p = kn->kn_ptr.p_proc;
400 knlist_remove(&p->p_klist, kn, 0);
401 kn->kn_ptr.p_proc = NULL;
404 /* XXX - move to kern_proc.c? */
406 filt_proc(struct knote *kn, long hint)
408 struct proc *p = kn->kn_ptr.p_proc;
412 * mask off extra data
414 event = (u_int)hint & NOTE_PCTRLMASK;
417 * if the user is interested in this event, record it.
419 if (kn->kn_sfflags & event)
420 kn->kn_fflags |= event;
423 * process is gone, so flag the event as finished.
425 if (event == NOTE_EXIT) {
426 if (!(kn->kn_status & KN_DETACHED))
427 knlist_remove_inevent(&p->p_klist, kn);
428 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
429 kn->kn_data = p->p_xstat;
430 kn->kn_ptr.p_proc = NULL;
434 return (kn->kn_fflags != 0);
438 * Called when the process forked. It mostly does the same as the
439 * knote(), activating all knotes registered to be activated when the
440 * process forked. Additionally, for each knote attached to the
441 * parent, check whether user wants to track the new process. If so
442 * attach a new knote to it, and immediately report an event with the
446 knote_fork(struct knlist *list, int pid)
455 list->kl_lock(list->kl_lockarg);
457 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
458 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX)
462 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
468 * The same as knote(), activate the event.
470 if ((kn->kn_sfflags & NOTE_TRACK) == 0) {
471 kn->kn_status |= KN_HASKQLOCK;
472 if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
473 KNOTE_ACTIVATE(kn, 1);
474 kn->kn_status &= ~KN_HASKQLOCK;
480 * The NOTE_TRACK case. In addition to the activation
481 * of the event, we need to register new event to
482 * track the child. Drop the locks in preparation for
483 * the call to kqueue_register().
485 kn->kn_status |= KN_INFLUX;
487 list->kl_unlock(list->kl_lockarg);
490 * Activate existing knote and register a knote with
494 kev.filter = kn->kn_filter;
495 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
496 kev.fflags = kn->kn_sfflags;
497 kev.data = kn->kn_id; /* parent */
498 kev.udata = kn->kn_kevent.udata;/* preserve udata */
499 error = kqueue_register(kq, &kev, NULL, 0);
500 if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
501 KNOTE_ACTIVATE(kn, 0);
503 kn->kn_fflags |= NOTE_TRACKERR;
505 kn->kn_status &= ~KN_INFLUX;
507 list->kl_lock(list->kl_lockarg);
509 list->kl_unlock(list->kl_lockarg);
513 timertoticks(intptr_t data)
518 tv.tv_sec = data / 1000;
519 tv.tv_usec = (data % 1000) * 1000;
520 tticks = tvtohz(&tv);
525 /* XXX - move to kern_timeout.c? */
527 filt_timerexpire(void *knx)
529 struct knote *kn = knx;
530 struct callout *calloutp;
533 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */
535 if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) {
536 calloutp = (struct callout *)kn->kn_hook;
537 callout_reset_curcpu(calloutp, timertoticks(kn->kn_sdata),
538 filt_timerexpire, kn);
543 * data contains amount of time to sleep, in milliseconds
545 /* XXX - move to kern_timeout.c? */
547 filt_timerattach(struct knote *kn)
549 struct callout *calloutp;
551 atomic_add_int(&kq_ncallouts, 1);
553 if (kq_ncallouts >= kq_calloutmax) {
554 atomic_add_int(&kq_ncallouts, -1);
558 kn->kn_flags |= EV_CLEAR; /* automatically set */
559 kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */
560 calloutp = malloc(sizeof(*calloutp), M_KQUEUE, M_WAITOK);
561 callout_init(calloutp, CALLOUT_MPSAFE);
562 kn->kn_hook = calloutp;
563 callout_reset_curcpu(calloutp, timertoticks(kn->kn_sdata),
564 filt_timerexpire, kn);
569 /* XXX - move to kern_timeout.c? */
571 filt_timerdetach(struct knote *kn)
573 struct callout *calloutp;
575 calloutp = (struct callout *)kn->kn_hook;
576 callout_drain(calloutp);
577 free(calloutp, M_KQUEUE);
578 atomic_add_int(&kq_ncallouts, -1);
579 kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */
582 /* XXX - move to kern_timeout.c? */
584 filt_timer(struct knote *kn, long hint)
587 return (kn->kn_data != 0);
591 filt_userattach(struct knote *kn)
595 * EVFILT_USER knotes are not attached to anything in the kernel.
598 if (kn->kn_fflags & NOTE_TRIGGER)
606 filt_userdetach(__unused struct knote *kn)
610 * EVFILT_USER knotes are not attached to anything in the kernel.
615 filt_user(struct knote *kn, __unused long hint)
618 return (kn->kn_hookid);
622 filt_usertouch(struct knote *kn, struct kevent *kev, u_long type)
628 if (kev->fflags & NOTE_TRIGGER)
631 ffctrl = kev->fflags & NOTE_FFCTRLMASK;
632 kev->fflags &= NOTE_FFLAGSMASK;
638 kn->kn_sfflags &= kev->fflags;
642 kn->kn_sfflags |= kev->fflags;
646 kn->kn_sfflags = kev->fflags;
650 /* XXX Return error? */
653 kn->kn_sdata = kev->data;
654 if (kev->flags & EV_CLEAR) {
662 *kev = kn->kn_kevent;
663 kev->fflags = kn->kn_sfflags;
664 kev->data = kn->kn_sdata;
665 if (kn->kn_flags & EV_CLEAR) {
673 panic("filt_usertouch() - invalid type (%ld)", type);
679 kqueue(struct thread *td, struct kqueue_args *uap)
681 struct filedesc *fdp;
686 fdp = td->td_proc->p_fd;
687 error = falloc(td, &fp, &fd);
691 /* An extra reference on `nfp' has been held for us by falloc(). */
692 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
693 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK);
694 TAILQ_INIT(&kq->kq_head);
696 knlist_init_mtx(&kq->kq_sel.si_note, &kq->kq_lock);
697 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
700 SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
701 FILEDESC_XUNLOCK(fdp);
703 finit(fp, FREAD | FWRITE, DTYPE_KQUEUE, kq, &kqueueops);
706 td->td_retval[0] = fd;
711 #ifndef _SYS_SYSPROTO_H_
714 const struct kevent *changelist;
716 struct kevent *eventlist;
718 const struct timespec *timeout;
722 kevent(struct thread *td, struct kevent_args *uap)
724 struct timespec ts, *tsp;
725 struct kevent_copyops k_ops = { uap,
732 struct uio *ktruioin = NULL;
733 struct uio *ktruioout = NULL;
736 if (uap->timeout != NULL) {
737 error = copyin(uap->timeout, &ts, sizeof(ts));
745 if (KTRPOINT(td, KTR_GENIO)) {
746 ktriov.iov_base = uap->changelist;
747 ktriov.iov_len = uap->nchanges * sizeof(struct kevent);
748 ktruio = (struct uio){ .uio_iov = &ktriov, .uio_iovcnt = 1,
749 .uio_segflg = UIO_USERSPACE, .uio_rw = UIO_READ,
751 ktruioin = cloneuio(&ktruio);
752 ktriov.iov_base = uap->eventlist;
753 ktriov.iov_len = uap->nevents * sizeof(struct kevent);
754 ktruioout = cloneuio(&ktruio);
758 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
762 if (ktruioin != NULL) {
763 ktruioin->uio_resid = uap->nchanges * sizeof(struct kevent);
764 ktrgenio(uap->fd, UIO_WRITE, ktruioin, 0);
765 ktruioout->uio_resid = td->td_retval[0] * sizeof(struct kevent);
766 ktrgenio(uap->fd, UIO_READ, ktruioout, error);
774 * Copy 'count' items into the destination list pointed to by uap->eventlist.
777 kevent_copyout(void *arg, struct kevent *kevp, int count)
779 struct kevent_args *uap;
782 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
783 uap = (struct kevent_args *)arg;
785 error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
787 uap->eventlist += count;
792 * Copy 'count' items from the list pointed to by uap->changelist.
795 kevent_copyin(void *arg, struct kevent *kevp, int count)
797 struct kevent_args *uap;
800 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
801 uap = (struct kevent_args *)arg;
803 error = copyin(uap->changelist, kevp, count * sizeof *kevp);
805 uap->changelist += count;
810 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
811 struct kevent_copyops *k_ops, const struct timespec *timeout)
813 struct kevent keva[KQ_NEVENTS];
814 struct kevent *kevp, *changes;
817 int i, n, nerrors, error;
819 if ((error = fget(td, fd, &fp)) != 0)
821 if ((error = kqueue_acquire(fp, &kq)) != 0)
826 while (nchanges > 0) {
827 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
828 error = k_ops->k_copyin(k_ops->arg, keva, n);
832 for (i = 0; i < n; i++) {
836 kevp->flags &= ~EV_SYSFLAGS;
837 error = kqueue_register(kq, kevp, td, 1);
838 if (error || (kevp->flags & EV_RECEIPT)) {
840 kevp->flags = EV_ERROR;
842 (void) k_ops->k_copyout(k_ops->arg,
854 td->td_retval[0] = nerrors;
859 error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td);
861 kqueue_release(kq, 0);
868 kqueue_add_filteropts(int filt, struct filterops *filtops)
873 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
875 "trying to add a filterop that is out of range: %d is beyond %d\n",
876 ~filt, EVFILT_SYSCOUNT);
879 mtx_lock(&filterops_lock);
880 if (sysfilt_ops[~filt].for_fop != &null_filtops &&
881 sysfilt_ops[~filt].for_fop != NULL)
884 sysfilt_ops[~filt].for_fop = filtops;
885 sysfilt_ops[~filt].for_refcnt = 0;
887 mtx_unlock(&filterops_lock);
893 kqueue_del_filteropts(int filt)
898 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
901 mtx_lock(&filterops_lock);
902 if (sysfilt_ops[~filt].for_fop == &null_filtops ||
903 sysfilt_ops[~filt].for_fop == NULL)
905 else if (sysfilt_ops[~filt].for_refcnt != 0)
908 sysfilt_ops[~filt].for_fop = &null_filtops;
909 sysfilt_ops[~filt].for_refcnt = 0;
911 mtx_unlock(&filterops_lock);
916 static struct filterops *
917 kqueue_fo_find(int filt)
920 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
923 mtx_lock(&filterops_lock);
924 sysfilt_ops[~filt].for_refcnt++;
925 if (sysfilt_ops[~filt].for_fop == NULL)
926 sysfilt_ops[~filt].for_fop = &null_filtops;
927 mtx_unlock(&filterops_lock);
929 return sysfilt_ops[~filt].for_fop;
933 kqueue_fo_release(int filt)
936 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
939 mtx_lock(&filterops_lock);
940 KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
941 ("filter object refcount not valid on release"));
942 sysfilt_ops[~filt].for_refcnt--;
943 mtx_unlock(&filterops_lock);
947 * A ref to kq (obtained via kqueue_acquire) must be held. waitok will
948 * influence if memory allocation should wait. Make sure it is 0 if you
952 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
954 struct filterops *fops;
956 struct knote *kn, *tkn;
957 int error, filt, event;
966 fops = kqueue_fo_find(filt);
970 tkn = knote_alloc(waitok); /* prevent waiting with locks */
974 KASSERT(td != NULL, ("td is NULL"));
975 error = fget(td, kev->ident, &fp);
979 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
980 kev->ident, 0) != 0) {
984 error = kqueue_expand(kq, fops, kev->ident, waitok);
990 if (fp->f_type == DTYPE_KQUEUE) {
992 * if we add some inteligence about what we are doing,
993 * we should be able to support events on ourselves.
994 * We need to know when we are doing this to prevent
995 * getting both the knlist lock and the kq lock since
996 * they are the same thing.
998 if (fp->f_data == kq) {
1003 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1007 if (kev->ident < kq->kq_knlistsize) {
1008 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
1009 if (kev->filter == kn->kn_filter)
1013 if ((kev->flags & EV_ADD) == EV_ADD)
1014 kqueue_expand(kq, fops, kev->ident, waitok);
1017 if (kq->kq_knhashmask != 0) {
1020 list = &kq->kq_knhash[
1021 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
1022 SLIST_FOREACH(kn, list, kn_link)
1023 if (kev->ident == kn->kn_id &&
1024 kev->filter == kn->kn_filter)
1029 /* knote is in the process of changing, wait for it to stablize. */
1030 if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1031 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1032 kq->kq_state |= KQ_FLUXWAIT;
1033 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
1042 * kn now contains the matching knote, or NULL if no match
1045 if (kev->flags & EV_ADD) {
1057 * apply reference counts to knote structure, and
1058 * do not release it at the end of this routine.
1063 kn->kn_sfflags = kev->fflags;
1064 kn->kn_sdata = kev->data;
1067 kn->kn_kevent = *kev;
1068 kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE |
1069 EV_ENABLE | EV_DISABLE);
1070 kn->kn_status = KN_INFLUX|KN_DETACHED;
1072 error = knote_attach(kn, kq);
1079 if ((error = kn->kn_fop->f_attach(kn)) != 0) {
1086 /* No matching knote and the EV_ADD flag is not set. */
1093 if (kev->flags & EV_DELETE) {
1094 kn->kn_status |= KN_INFLUX;
1096 if (!(kn->kn_status & KN_DETACHED))
1097 kn->kn_fop->f_detach(kn);
1103 * The user may change some filter values after the initial EV_ADD,
1104 * but doing so will not reset any filter which has already been
1107 kn->kn_status |= KN_INFLUX;
1110 kn->kn_kevent.udata = kev->udata;
1111 if (!fops->f_isfd && fops->f_touch != NULL) {
1112 fops->f_touch(kn, kev, EVENT_REGISTER);
1114 kn->kn_sfflags = kev->fflags;
1115 kn->kn_sdata = kev->data;
1119 * We can get here with kn->kn_knlist == NULL. This can happen when
1120 * the initial attach event decides that the event is "completed"
1121 * already. i.e. filt_procattach is called on a zombie process. It
1122 * will call filt_proc which will remove it from the list, and NULL
1126 event = kn->kn_fop->f_event(kn, 0);
1129 KNOTE_ACTIVATE(kn, 1);
1130 kn->kn_status &= ~KN_INFLUX;
1133 if ((kev->flags & EV_DISABLE) &&
1134 ((kn->kn_status & KN_DISABLED) == 0)) {
1135 kn->kn_status |= KN_DISABLED;
1138 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
1139 kn->kn_status &= ~KN_DISABLED;
1140 if ((kn->kn_status & KN_ACTIVE) &&
1141 ((kn->kn_status & KN_QUEUED) == 0))
1147 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1153 kqueue_fo_release(filt);
1158 kqueue_acquire(struct file *fp, struct kqueue **kqp)
1166 if (fp->f_type != DTYPE_KQUEUE || kq == NULL)
1170 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
1181 kqueue_release(struct kqueue *kq, int locked)
1188 if (kq->kq_refcnt == 1)
1189 wakeup(&kq->kq_refcnt);
1195 kqueue_schedtask(struct kqueue *kq)
1199 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1200 ("scheduling kqueue task while draining"));
1202 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1203 taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task);
1204 kq->kq_state |= KQ_TASKSCHED;
1209 * Expand the kq to make sure we have storage for fops/ident pair.
1211 * Return 0 on success (or no work necessary), return errno on failure.
1213 * Not calling hashinit w/ waitok (proper malloc flag) should be safe.
1214 * If kqueue_register is called from a non-fd context, there usually/should
1218 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
1221 struct klist *list, *tmp_knhash, *to_free;
1222 u_long tmp_knhashmask;
1225 int mflag = waitok ? M_WAITOK : M_NOWAIT;
1232 if (kq->kq_knlistsize <= fd) {
1233 size = kq->kq_knlistsize;
1236 list = malloc(size * sizeof(*list), M_KQUEUE, mflag);
1240 if (kq->kq_knlistsize > fd) {
1244 if (kq->kq_knlist != NULL) {
1245 bcopy(kq->kq_knlist, list,
1246 kq->kq_knlistsize * sizeof(*list));
1247 to_free = kq->kq_knlist;
1248 kq->kq_knlist = NULL;
1250 bzero((caddr_t)list +
1251 kq->kq_knlistsize * sizeof(*list),
1252 (size - kq->kq_knlistsize) * sizeof(*list));
1253 kq->kq_knlistsize = size;
1254 kq->kq_knlist = list;
1259 if (kq->kq_knhashmask == 0) {
1260 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1262 if (tmp_knhash == NULL)
1265 if (kq->kq_knhashmask == 0) {
1266 kq->kq_knhash = tmp_knhash;
1267 kq->kq_knhashmask = tmp_knhashmask;
1269 to_free = tmp_knhash;
1274 free(to_free, M_KQUEUE);
1281 kqueue_task(void *arg, int pending)
1289 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1292 KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1294 kq->kq_state &= ~KQ_TASKSCHED;
1295 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1296 wakeup(&kq->kq_state);
1299 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1303 * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1304 * We treat KN_MARKER knotes as if they are INFLUX.
1307 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1308 const struct timespec *tsp, struct kevent *keva, struct thread *td)
1310 struct kevent *kevp;
1311 struct timeval atv, rtv, ttv;
1312 struct knote *kn, *marker;
1313 int count, timeout, nkev, error, influx;
1314 int haskqglobal, touch;
1325 TIMESPEC_TO_TIMEVAL(&atv, tsp);
1326 if (itimerfix(&atv)) {
1330 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1333 timeout = atv.tv_sec > 24 * 60 * 60 ?
1334 24 * 60 * 60 * hz : tvtohz(&atv);
1335 getmicrouptime(&rtv);
1336 timevaladd(&atv, &rtv);
1342 marker = knote_alloc(1);
1343 if (marker == NULL) {
1347 marker->kn_status = KN_MARKER;
1352 if (atv.tv_sec || atv.tv_usec) {
1353 getmicrouptime(&rtv);
1354 if (timevalcmp(&rtv, &atv, >=))
1357 timevalsub(&ttv, &rtv);
1358 timeout = ttv.tv_sec > 24 * 60 * 60 ?
1359 24 * 60 * 60 * hz : tvtohz(&ttv);
1364 if (kq->kq_count == 0) {
1366 error = EWOULDBLOCK;
1368 kq->kq_state |= KQ_SLEEP;
1369 error = msleep(kq, &kq->kq_lock, PSOCK | PCATCH,
1374 /* don't restart after signals... */
1375 if (error == ERESTART)
1377 else if (error == EWOULDBLOCK)
1382 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1386 kn = TAILQ_FIRST(&kq->kq_head);
1388 if ((kn->kn_status == KN_MARKER && kn != marker) ||
1389 (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1394 kq->kq_state |= KQ_FLUXWAIT;
1395 error = msleep(kq, &kq->kq_lock, PSOCK,
1400 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1401 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1402 kn->kn_status &= ~KN_QUEUED;
1408 if (count == maxevents)
1412 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1413 ("KN_INFLUX set when not suppose to be"));
1415 if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
1416 kn->kn_status &= ~KN_QUEUED;
1417 kn->kn_status |= KN_INFLUX;
1421 * We don't need to lock the list since we've marked
1424 *kevp = kn->kn_kevent;
1425 if (!(kn->kn_status & KN_DETACHED))
1426 kn->kn_fop->f_detach(kn);
1431 kn->kn_status |= KN_INFLUX;
1433 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
1434 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1436 if (kn->kn_fop->f_event(kn, 0) == 0) {
1438 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1440 ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX);
1446 touch = (!kn->kn_fop->f_isfd &&
1447 kn->kn_fop->f_touch != NULL);
1449 kn->kn_fop->f_touch(kn, kevp, EVENT_PROCESS);
1451 *kevp = kn->kn_kevent;
1453 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1454 if (kn->kn_flags & (EV_CLEAR | EV_DISPATCH)) {
1456 * Manually clear knotes who weren't
1459 if (touch == 0 && kn->kn_flags & EV_CLEAR) {
1463 if (kn->kn_flags & EV_DISPATCH)
1464 kn->kn_status |= KN_DISABLED;
1465 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1468 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1470 kn->kn_status &= ~(KN_INFLUX);
1475 /* we are returning a copy to the user */
1480 if (nkev == KQ_NEVENTS) {
1483 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1491 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1499 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1500 td->td_retval[0] = maxevents - count;
1506 * This could be expanded to call kqueue_scan, if desired.
1510 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1511 int flags, struct thread *td)
1518 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
1519 int flags, struct thread *td)
1526 kqueue_truncate(struct file *fp, off_t length, struct ucred *active_cred,
1535 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
1536 struct ucred *active_cred, struct thread *td)
1539 * Enabling sigio causes two major problems:
1540 * 1) infinite recursion:
1541 * Synopsys: kevent is being used to track signals and have FIOASYNC
1542 * set. On receipt of a signal this will cause a kqueue to recurse
1543 * into itself over and over. Sending the sigio causes the kqueue
1544 * to become ready, which in turn posts sigio again, forever.
1545 * Solution: this can be solved by setting a flag in the kqueue that
1546 * we have a SIGIO in progress.
1547 * 2) locking problems:
1548 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
1549 * us above the proc and pgrp locks.
1550 * Solution: Post a signal using an async mechanism, being sure to
1551 * record a generation count in the delivery so that we do not deliver
1552 * a signal to the wrong process.
1554 * Note, these two mechanisms are somewhat mutually exclusive!
1563 kq->kq_state |= KQ_ASYNC;
1565 kq->kq_state &= ~KQ_ASYNC;
1570 return (fsetown(*(int *)data, &kq->kq_sigio));
1573 *(int *)data = fgetown(&kq->kq_sigio);
1583 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
1590 if ((error = kqueue_acquire(fp, &kq)))
1594 if (events & (POLLIN | POLLRDNORM)) {
1596 revents |= events & (POLLIN | POLLRDNORM);
1598 selrecord(td, &kq->kq_sel);
1599 if (SEL_WAITING(&kq->kq_sel))
1600 kq->kq_state |= KQ_SEL;
1603 kqueue_release(kq, 1);
1610 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1614 bzero((void *)st, sizeof *st);
1616 * We no longer return kq_count because the unlocked value is useless.
1617 * If you spent all this time getting the count, why not spend your
1618 * syscall better by calling kevent?
1620 * XXX - This is needed for libc_r.
1622 st->st_mode = S_IFIFO;
1628 kqueue_close(struct file *fp, struct thread *td)
1630 struct kqueue *kq = fp->f_data;
1631 struct filedesc *fdp;
1636 if ((error = kqueue_acquire(fp, &kq)))
1641 KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
1642 ("kqueue already closing"));
1643 kq->kq_state |= KQ_CLOSING;
1644 if (kq->kq_refcnt > 1)
1645 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
1647 KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
1650 KASSERT(knlist_empty(&kq->kq_sel.si_note),
1651 ("kqueue's knlist not empty"));
1653 for (i = 0; i < kq->kq_knlistsize; i++) {
1654 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
1655 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1656 kq->kq_state |= KQ_FLUXWAIT;
1657 msleep(kq, &kq->kq_lock, PSOCK, "kqclo1", 0);
1660 kn->kn_status |= KN_INFLUX;
1662 if (!(kn->kn_status & KN_DETACHED))
1663 kn->kn_fop->f_detach(kn);
1668 if (kq->kq_knhashmask != 0) {
1669 for (i = 0; i <= kq->kq_knhashmask; i++) {
1670 while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
1671 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1672 kq->kq_state |= KQ_FLUXWAIT;
1673 msleep(kq, &kq->kq_lock, PSOCK,
1677 kn->kn_status |= KN_INFLUX;
1679 if (!(kn->kn_status & KN_DETACHED))
1680 kn->kn_fop->f_detach(kn);
1687 if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
1688 kq->kq_state |= KQ_TASKDRAIN;
1689 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
1692 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1693 selwakeuppri(&kq->kq_sel, PSOCK);
1694 if (!SEL_WAITING(&kq->kq_sel))
1695 kq->kq_state &= ~KQ_SEL;
1700 FILEDESC_XLOCK(fdp);
1701 SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list);
1702 FILEDESC_XUNLOCK(fdp);
1704 knlist_destroy(&kq->kq_sel.si_note);
1705 mtx_destroy(&kq->kq_lock);
1708 if (kq->kq_knhash != NULL)
1709 free(kq->kq_knhash, M_KQUEUE);
1710 if (kq->kq_knlist != NULL)
1711 free(kq->kq_knlist, M_KQUEUE);
1713 funsetown(&kq->kq_sigio);
1721 kqueue_wakeup(struct kqueue *kq)
1725 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
1726 kq->kq_state &= ~KQ_SLEEP;
1729 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1730 selwakeuppri(&kq->kq_sel, PSOCK);
1731 if (!SEL_WAITING(&kq->kq_sel))
1732 kq->kq_state &= ~KQ_SEL;
1734 if (!knlist_empty(&kq->kq_sel.si_note))
1735 kqueue_schedtask(kq);
1736 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
1737 pgsigio(&kq->kq_sigio, SIGIO, 0);
1742 * Walk down a list of knotes, activating them if their event has triggered.
1744 * There is a possibility to optimize in the case of one kq watching another.
1745 * Instead of scheduling a task to wake it up, you could pass enough state
1746 * down the chain to make up the parent kqueue. Make this code functional
1750 knote(struct knlist *list, long hint, int lockflags)
1759 KNL_ASSERT_LOCK(list, lockflags & KNF_LISTLOCKED);
1761 if ((lockflags & KNF_LISTLOCKED) == 0)
1762 list->kl_lock(list->kl_lockarg);
1765 * If we unlock the list lock (and set KN_INFLUX), we can eliminate
1766 * the kqueue scheduling, but this will introduce four
1767 * lock/unlock's for each knote to test. If we do, continue to use
1768 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is
1769 * only safe if you want to remove the current item, which we are
1772 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
1774 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1776 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1778 } else if ((lockflags & KNF_NOKQLOCK) != 0) {
1779 kn->kn_status |= KN_INFLUX;
1781 error = kn->kn_fop->f_event(kn, hint);
1783 kn->kn_status &= ~KN_INFLUX;
1785 KNOTE_ACTIVATE(kn, 1);
1788 kn->kn_status |= KN_HASKQLOCK;
1789 if (kn->kn_fop->f_event(kn, hint))
1790 KNOTE_ACTIVATE(kn, 1);
1791 kn->kn_status &= ~KN_HASKQLOCK;
1797 if ((lockflags & KNF_LISTLOCKED) == 0)
1798 list->kl_unlock(list->kl_lockarg);
1802 * add a knote to a knlist
1805 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
1807 KNL_ASSERT_LOCK(knl, islocked);
1808 KQ_NOTOWNED(kn->kn_kq);
1809 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) ==
1810 (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED"));
1812 knl->kl_lock(knl->kl_lockarg);
1813 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
1815 knl->kl_unlock(knl->kl_lockarg);
1817 kn->kn_knlist = knl;
1818 kn->kn_status &= ~KN_DETACHED;
1819 KQ_UNLOCK(kn->kn_kq);
1823 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked)
1825 KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked"));
1826 KNL_ASSERT_LOCK(knl, knlislocked);
1827 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
1829 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX,
1830 ("knlist_remove called w/o knote being KN_INFLUX or already removed"));
1832 knl->kl_lock(knl->kl_lockarg);
1833 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
1834 kn->kn_knlist = NULL;
1836 knl->kl_unlock(knl->kl_lockarg);
1839 kn->kn_status |= KN_DETACHED;
1841 KQ_UNLOCK(kn->kn_kq);
1845 * remove all knotes from a specified klist
1848 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
1851 knlist_remove_kq(knl, kn, islocked, 0);
1855 * remove knote from a specified klist while in f_event handler.
1858 knlist_remove_inevent(struct knlist *knl, struct knote *kn)
1861 knlist_remove_kq(knl, kn, 1,
1862 (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK);
1866 knlist_empty(struct knlist *knl)
1868 KNL_ASSERT_LOCKED(knl);
1869 return SLIST_EMPTY(&knl->kl_list);
1872 static struct mtx knlist_lock;
1873 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
1875 static void knlist_mtx_lock(void *arg);
1876 static void knlist_mtx_unlock(void *arg);
1879 knlist_mtx_lock(void *arg)
1881 mtx_lock((struct mtx *)arg);
1885 knlist_mtx_unlock(void *arg)
1887 mtx_unlock((struct mtx *)arg);
1891 knlist_mtx_assert_locked(void *arg)
1893 mtx_assert((struct mtx *)arg, MA_OWNED);
1897 knlist_mtx_assert_unlocked(void *arg)
1899 mtx_assert((struct mtx *)arg, MA_NOTOWNED);
1903 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
1904 void (*kl_unlock)(void *),
1905 void (*kl_assert_locked)(void *), void (*kl_assert_unlocked)(void *))
1909 knl->kl_lockarg = &knlist_lock;
1911 knl->kl_lockarg = lock;
1913 if (kl_lock == NULL)
1914 knl->kl_lock = knlist_mtx_lock;
1916 knl->kl_lock = kl_lock;
1917 if (kl_unlock == NULL)
1918 knl->kl_unlock = knlist_mtx_unlock;
1920 knl->kl_unlock = kl_unlock;
1921 if (kl_assert_locked == NULL)
1922 knl->kl_assert_locked = knlist_mtx_assert_locked;
1924 knl->kl_assert_locked = kl_assert_locked;
1925 if (kl_assert_unlocked == NULL)
1926 knl->kl_assert_unlocked = knlist_mtx_assert_unlocked;
1928 knl->kl_assert_unlocked = kl_assert_unlocked;
1930 SLIST_INIT(&knl->kl_list);
1934 knlist_init_mtx(struct knlist *knl, struct mtx *lock)
1937 knlist_init(knl, lock, NULL, NULL, NULL, NULL);
1941 knlist_destroy(struct knlist *knl)
1946 * if we run across this error, we need to find the offending
1947 * driver and have it call knlist_clear.
1949 if (!SLIST_EMPTY(&knl->kl_list))
1950 printf("WARNING: destroying knlist w/ knotes on it!\n");
1953 knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL;
1954 SLIST_INIT(&knl->kl_list);
1958 * Even if we are locked, we may need to drop the lock to allow any influx
1959 * knotes time to "settle".
1962 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
1964 struct knote *kn, *kn2;
1968 KNL_ASSERT_LOCKED(knl);
1970 KNL_ASSERT_UNLOCKED(knl);
1971 again: /* need to reacquire lock since we have dropped it */
1972 knl->kl_lock(knl->kl_lockarg);
1975 SLIST_FOREACH_SAFE(kn, &knl->kl_list, kn_selnext, kn2) {
1978 if ((kn->kn_status & KN_INFLUX)) {
1982 knlist_remove_kq(knl, kn, 1, 1);
1984 kn->kn_status |= KN_INFLUX | KN_DETACHED;
1988 /* Make sure cleared knotes disappear soon */
1989 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1995 if (!SLIST_EMPTY(&knl->kl_list)) {
1996 /* there are still KN_INFLUX remaining */
1997 kn = SLIST_FIRST(&knl->kl_list);
2000 KASSERT(kn->kn_status & KN_INFLUX,
2001 ("knote removed w/o list lock"));
2002 knl->kl_unlock(knl->kl_lockarg);
2003 kq->kq_state |= KQ_FLUXWAIT;
2004 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
2010 KNL_ASSERT_LOCKED(knl);
2012 knl->kl_unlock(knl->kl_lockarg);
2013 KNL_ASSERT_UNLOCKED(knl);
2018 * Remove all knotes referencing a specified fd must be called with FILEDESC
2019 * lock. This prevents a race where a new fd comes along and occupies the
2020 * entry and we attach a knote to the fd.
2023 knote_fdclose(struct thread *td, int fd)
2025 struct filedesc *fdp = td->td_proc->p_fd;
2030 FILEDESC_XLOCK_ASSERT(fdp);
2033 * We shouldn't have to worry about new kevents appearing on fd
2034 * since filedesc is locked.
2036 SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
2041 while (kq->kq_knlistsize > fd &&
2042 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
2043 if (kn->kn_status & KN_INFLUX) {
2044 /* someone else might be waiting on our knote */
2047 kq->kq_state |= KQ_FLUXWAIT;
2048 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
2051 kn->kn_status |= KN_INFLUX;
2053 if (!(kn->kn_status & KN_DETACHED))
2054 kn->kn_fop->f_detach(kn);
2064 knote_attach(struct knote *kn, struct kqueue *kq)
2068 KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX"));
2071 if (kn->kn_fop->f_isfd) {
2072 if (kn->kn_id >= kq->kq_knlistsize)
2074 list = &kq->kq_knlist[kn->kn_id];
2076 if (kq->kq_knhash == NULL)
2078 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2081 SLIST_INSERT_HEAD(list, kn, kn_link);
2087 * knote must already have been detached using the f_detach method.
2088 * no lock need to be held, it is assumed that the KN_INFLUX flag is set
2089 * to prevent other removal.
2092 knote_drop(struct knote *kn, struct thread *td)
2100 KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX,
2101 ("knote_drop called without KN_INFLUX set in kn_status"));
2104 if (kn->kn_fop->f_isfd)
2105 list = &kq->kq_knlist[kn->kn_id];
2107 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2109 if (!SLIST_EMPTY(list))
2110 SLIST_REMOVE(list, kn, knote, kn_link);
2111 if (kn->kn_status & KN_QUEUED)
2115 if (kn->kn_fop->f_isfd) {
2116 fdrop(kn->kn_fp, td);
2119 kqueue_fo_release(kn->kn_kevent.filter);
2125 knote_enqueue(struct knote *kn)
2127 struct kqueue *kq = kn->kn_kq;
2129 KQ_OWNED(kn->kn_kq);
2130 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
2132 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
2133 kn->kn_status |= KN_QUEUED;
2139 knote_dequeue(struct knote *kn)
2141 struct kqueue *kq = kn->kn_kq;
2143 KQ_OWNED(kn->kn_kq);
2144 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
2146 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
2147 kn->kn_status &= ~KN_QUEUED;
2155 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
2156 NULL, NULL, UMA_ALIGN_PTR, 0);
2158 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL);
2160 static struct knote *
2161 knote_alloc(int waitok)
2163 return ((struct knote *)uma_zalloc(knote_zone,
2164 (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO));
2168 knote_free(struct knote *kn)
2171 uma_zfree(knote_zone, kn);
2175 * Register the kev w/ the kq specified by fd.
2178 kqfd_register(int fd, struct kevent *kev, struct thread *td, int waitok)
2184 if ((error = fget(td, fd, &fp)) != 0)
2186 if ((error = kqueue_acquire(fp, &kq)) != 0)
2189 error = kqueue_register(kq, kev, td, waitok);
2191 kqueue_release(kq, 0);