2 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
3 * Copyright 2004 John-Mark Gurney <jmg@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/mutex.h>
37 #include <sys/malloc.h>
38 #include <sys/unistd.h>
40 #include <sys/filedesc.h>
41 #include <sys/filio.h>
42 #include <sys/fcntl.h>
43 #include <sys/kthread.h>
44 #include <sys/selinfo.h>
45 #include <sys/queue.h>
46 #include <sys/event.h>
47 #include <sys/eventvar.h>
49 #include <sys/protosw.h>
50 #include <sys/sigio.h>
51 #include <sys/signalvar.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
55 #include <sys/sysctl.h>
56 #include <sys/sysproto.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/taskqueue.h>
63 static MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
66 * This lock is used if multiple kq locks are required. This possibly
67 * should be made into a per proc lock.
69 static struct mtx kq_global;
70 MTX_SYSINIT(kq_global, &kq_global, "kqueue order", MTX_DEF);
71 #define KQ_GLOBAL_LOCK(lck, haslck) do { \
76 #define KQ_GLOBAL_UNLOCK(lck, haslck) do { \
82 TASKQUEUE_DEFINE_THREAD(kqueue);
84 static int kevent_copyout(void *arg, struct kevent *kevp, int count);
85 static int kevent_copyin(void *arg, struct kevent *kevp, int count);
86 static int kqueue_aquire(struct file *fp, struct kqueue **kqp);
87 static void kqueue_release(struct kqueue *kq, int locked);
88 static int kqueue_expand(struct kqueue *kq, struct filterops *fops,
89 uintptr_t ident, int waitok);
90 static void kqueue_task(void *arg, int pending);
91 static int kqueue_scan(struct kqueue *kq, int maxevents,
92 struct kevent_copyops *k_ops,
93 const struct timespec *timeout,
94 struct kevent *keva, struct thread *td);
95 static void kqueue_wakeup(struct kqueue *kq);
96 static struct filterops *kqueue_fo_find(int filt);
97 static void kqueue_fo_release(int filt);
99 static fo_rdwr_t kqueue_read;
100 static fo_rdwr_t kqueue_write;
101 static fo_ioctl_t kqueue_ioctl;
102 static fo_poll_t kqueue_poll;
103 static fo_kqfilter_t kqueue_kqfilter;
104 static fo_stat_t kqueue_stat;
105 static fo_close_t kqueue_close;
107 static struct fileops kqueueops = {
108 .fo_read = kqueue_read,
109 .fo_write = kqueue_write,
110 .fo_ioctl = kqueue_ioctl,
111 .fo_poll = kqueue_poll,
112 .fo_kqfilter = kqueue_kqfilter,
113 .fo_stat = kqueue_stat,
114 .fo_close = kqueue_close,
117 static int knote_attach(struct knote *kn, struct kqueue *kq);
118 static void knote_drop(struct knote *kn, struct thread *td);
119 static void knote_enqueue(struct knote *kn);
120 static void knote_dequeue(struct knote *kn);
121 static void knote_init(void);
122 static struct knote *knote_alloc(int waitok);
123 static void knote_free(struct knote *kn);
125 static void filt_kqdetach(struct knote *kn);
126 static int filt_kqueue(struct knote *kn, long hint);
127 static int filt_procattach(struct knote *kn);
128 static void filt_procdetach(struct knote *kn);
129 static int filt_proc(struct knote *kn, long hint);
130 static int filt_fileattach(struct knote *kn);
131 static void filt_timerexpire(void *knx);
132 static int filt_timerattach(struct knote *kn);
133 static void filt_timerdetach(struct knote *kn);
134 static int filt_timer(struct knote *kn, long hint);
136 static struct filterops file_filtops =
137 { 1, filt_fileattach, NULL, NULL };
138 static struct filterops kqread_filtops =
139 { 1, NULL, filt_kqdetach, filt_kqueue };
140 /* XXX - move to kern_proc.c? */
141 static struct filterops proc_filtops =
142 { 0, filt_procattach, filt_procdetach, filt_proc };
143 static struct filterops timer_filtops =
144 { 0, filt_timerattach, filt_timerdetach, filt_timer };
146 static uma_zone_t knote_zone;
147 static int kq_ncallouts = 0;
148 static int kq_calloutmax = (4 * 1024);
149 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
150 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
152 /* XXX - ensure not KN_INFLUX?? */
153 #define KNOTE_ACTIVATE(kn, islock) do { \
155 mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED); \
157 KQ_LOCK((kn)->kn_kq); \
158 (kn)->kn_status |= KN_ACTIVE; \
159 if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
160 knote_enqueue((kn)); \
162 KQ_UNLOCK((kn)->kn_kq); \
164 #define KQ_LOCK(kq) do { \
165 mtx_lock(&(kq)->kq_lock); \
167 #define KQ_FLUX_WAKEUP(kq) do { \
168 if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) { \
169 (kq)->kq_state &= ~KQ_FLUXWAIT; \
173 #define KQ_UNLOCK_FLUX(kq) do { \
174 KQ_FLUX_WAKEUP(kq); \
175 mtx_unlock(&(kq)->kq_lock); \
177 #define KQ_UNLOCK(kq) do { \
178 mtx_unlock(&(kq)->kq_lock); \
180 #define KQ_OWNED(kq) do { \
181 mtx_assert(&(kq)->kq_lock, MA_OWNED); \
183 #define KQ_NOTOWNED(kq) do { \
184 mtx_assert(&(kq)->kq_lock, MA_NOTOWNED); \
186 #define KN_LIST_LOCK(kn) do { \
187 if (kn->kn_knlist != NULL) \
188 kn->kn_knlist->kl_lock(kn->kn_knlist->kl_lockarg); \
190 #define KN_LIST_UNLOCK(kn) do { \
191 if (kn->kn_knlist != NULL) \
192 kn->kn_knlist->kl_unlock(kn->kn_knlist->kl_lockarg); \
194 #define KNL_ASSERT_LOCK(knl, islocked) do { \
196 KNL_ASSERT_LOCKED(knl); \
198 KNL_ASSERT_UNLOCKED(knl); \
201 #define KNL_ASSERT_LOCKED(knl) do { \
202 if (!knl->kl_locked((knl)->kl_lockarg)) \
203 panic("knlist not locked, but should be"); \
205 #define KNL_ASSERT_UNLOCKED(knl) do { \
206 if (knl->kl_locked((knl)->kl_lockarg)) \
207 panic("knlist locked, but should not be"); \
209 #else /* !INVARIANTS */
210 #define KNL_ASSERT_LOCKED(knl) do {} while(0)
211 #define KNL_ASSERT_UNLOCKED(knl) do {} while (0)
212 #endif /* INVARIANTS */
214 #define KN_HASHSIZE 64 /* XXX should be tunable */
215 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
218 filt_nullattach(struct knote *kn)
224 struct filterops null_filtops =
225 { 0, filt_nullattach, NULL, NULL };
227 /* XXX - make SYSINIT to add these, and move into respective modules. */
228 extern struct filterops sig_filtops;
229 extern struct filterops fs_filtops;
232 * Table for for all system-defined filters.
234 static struct mtx filterops_lock;
235 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops",
238 struct filterops *for_fop;
240 } sysfilt_ops[EVFILT_SYSCOUNT] = {
241 { &file_filtops }, /* EVFILT_READ */
242 { &file_filtops }, /* EVFILT_WRITE */
243 { &null_filtops }, /* EVFILT_AIO */
244 { &file_filtops }, /* EVFILT_VNODE */
245 { &proc_filtops }, /* EVFILT_PROC */
246 { &sig_filtops }, /* EVFILT_SIGNAL */
247 { &timer_filtops }, /* EVFILT_TIMER */
248 { &file_filtops }, /* EVFILT_NETDEV */
249 { &fs_filtops }, /* EVFILT_FS */
250 { &null_filtops }, /* EVFILT_LIO */
254 * Simple redirection for all cdevsw style objects to call their fo_kqfilter
258 filt_fileattach(struct knote *kn)
261 return (fo_kqfilter(kn->kn_fp, kn));
266 kqueue_kqfilter(struct file *fp, struct knote *kn)
268 struct kqueue *kq = kn->kn_fp->f_data;
270 if (kn->kn_filter != EVFILT_READ)
273 kn->kn_status |= KN_KQUEUE;
274 kn->kn_fop = &kqread_filtops;
275 knlist_add(&kq->kq_sel.si_note, kn, 0);
281 filt_kqdetach(struct knote *kn)
283 struct kqueue *kq = kn->kn_fp->f_data;
285 knlist_remove(&kq->kq_sel.si_note, kn, 0);
290 filt_kqueue(struct knote *kn, long hint)
292 struct kqueue *kq = kn->kn_fp->f_data;
294 kn->kn_data = kq->kq_count;
295 return (kn->kn_data > 0);
298 /* XXX - move to kern_proc.c? */
300 filt_procattach(struct knote *kn)
307 p = pfind(kn->kn_id);
308 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
309 p = zpfind(kn->kn_id);
311 } else if (p != NULL && (p->p_flag & P_WEXIT)) {
317 if ((error = p_cansee(curthread, p)))
320 kn->kn_ptr.p_proc = p;
321 kn->kn_flags |= EV_CLEAR; /* automatically set */
324 * internal flag indicating registration done by kernel
326 if (kn->kn_flags & EV_FLAG1) {
327 kn->kn_data = kn->kn_sdata; /* ppid */
328 kn->kn_fflags = NOTE_CHILD;
329 kn->kn_flags &= ~EV_FLAG1;
333 knlist_add(&p->p_klist, kn, 1);
336 * Immediately activate any exit notes if the target process is a
337 * zombie. This is necessary to handle the case where the target
338 * process, e.g. a child, dies before the kevent is registered.
340 if (immediate && filt_proc(kn, NOTE_EXIT))
341 KNOTE_ACTIVATE(kn, 0);
349 * The knote may be attached to a different process, which may exit,
350 * leaving nothing for the knote to be attached to. So when the process
351 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
352 * it will be deleted when read out. However, as part of the knote deletion,
353 * this routine is called, so a check is needed to avoid actually performing
354 * a detach, because the original process does not exist any more.
356 /* XXX - move to kern_proc.c? */
358 filt_procdetach(struct knote *kn)
362 p = kn->kn_ptr.p_proc;
363 knlist_remove(&p->p_klist, kn, 0);
364 kn->kn_ptr.p_proc = NULL;
367 /* XXX - move to kern_proc.c? */
369 filt_proc(struct knote *kn, long hint)
371 struct proc *p = kn->kn_ptr.p_proc;
375 * mask off extra data
377 event = (u_int)hint & NOTE_PCTRLMASK;
380 * if the user is interested in this event, record it.
382 if (kn->kn_sfflags & event)
383 kn->kn_fflags |= event;
386 * process is gone, so flag the event as finished.
388 if (event == NOTE_EXIT) {
389 if (!(kn->kn_status & KN_DETACHED))
390 knlist_remove_inevent(&p->p_klist, kn);
391 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
392 kn->kn_ptr.p_proc = NULL;
397 * process forked, and user wants to track the new process,
398 * so attach a new knote to it, and immediately report an
399 * event with the parent's pid.
401 if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
406 * register knote with new process.
408 kev.ident = hint & NOTE_PDATAMASK; /* pid */
409 kev.filter = kn->kn_filter;
410 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
411 kev.fflags = kn->kn_sfflags;
412 kev.data = kn->kn_id; /* parent */
413 kev.udata = kn->kn_kevent.udata; /* preserve udata */
414 error = kqueue_register(kn->kn_kq, &kev, NULL, 0);
416 kn->kn_fflags |= NOTE_TRACKERR;
419 return (kn->kn_fflags != 0);
423 timertoticks(intptr_t data)
428 tv.tv_sec = data / 1000;
429 tv.tv_usec = (data % 1000) * 1000;
430 tticks = tvtohz(&tv);
435 /* XXX - move to kern_timeout.c? */
437 filt_timerexpire(void *knx)
439 struct knote *kn = knx;
440 struct callout *calloutp;
443 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */
445 if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) {
446 calloutp = (struct callout *)kn->kn_hook;
447 callout_reset(calloutp, timertoticks(kn->kn_sdata),
448 filt_timerexpire, kn);
453 * data contains amount of time to sleep, in milliseconds
455 /* XXX - move to kern_timeout.c? */
457 filt_timerattach(struct knote *kn)
459 struct callout *calloutp;
461 atomic_add_int(&kq_ncallouts, 1);
463 if (kq_ncallouts >= kq_calloutmax) {
464 atomic_add_int(&kq_ncallouts, -1);
468 kn->kn_flags |= EV_CLEAR; /* automatically set */
469 kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */
470 MALLOC(calloutp, struct callout *, sizeof(*calloutp),
472 callout_init(calloutp, CALLOUT_MPSAFE);
473 kn->kn_hook = calloutp;
474 callout_reset(calloutp, timertoticks(kn->kn_sdata), filt_timerexpire,
480 /* XXX - move to kern_timeout.c? */
482 filt_timerdetach(struct knote *kn)
484 struct callout *calloutp;
486 calloutp = (struct callout *)kn->kn_hook;
487 callout_drain(calloutp);
488 FREE(calloutp, M_KQUEUE);
489 atomic_add_int(&kq_ncallouts, -1);
490 kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */
493 /* XXX - move to kern_timeout.c? */
495 filt_timer(struct knote *kn, long hint)
498 return (kn->kn_data != 0);
505 kqueue(struct thread *td, struct kqueue_args *uap)
507 struct filedesc *fdp;
512 fdp = td->td_proc->p_fd;
513 error = falloc(td, &fp, &fd);
517 /* An extra reference on `nfp' has been held for us by falloc(). */
518 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
519 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK);
520 TAILQ_INIT(&kq->kq_head);
522 knlist_init(&kq->kq_sel.si_note, &kq->kq_lock, NULL, NULL, NULL);
523 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
525 FILEDESC_LOCK_FAST(fdp);
526 SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
527 FILEDESC_UNLOCK_FAST(fdp);
530 fp->f_flag = FREAD | FWRITE;
531 fp->f_type = DTYPE_KQUEUE;
532 fp->f_ops = &kqueueops;
537 td->td_retval[0] = fd;
542 #ifndef _SYS_SYSPROTO_H_
545 const struct kevent *changelist;
547 struct kevent *eventlist;
549 const struct timespec *timeout;
556 kevent(struct thread *td, struct kevent_args *uap)
558 struct timespec ts, *tsp;
559 struct kevent_copyops k_ops = { uap,
564 if (uap->timeout != NULL) {
565 error = copyin(uap->timeout, &ts, sizeof(ts));
572 return (kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
577 * Copy 'count' items into the destination list pointed to by uap->eventlist.
580 kevent_copyout(void *arg, struct kevent *kevp, int count)
582 struct kevent_args *uap;
585 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
586 uap = (struct kevent_args *)arg;
588 error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
590 uap->eventlist += count;
595 * Copy 'count' items from the list pointed to by uap->changelist.
598 kevent_copyin(void *arg, struct kevent *kevp, int count)
600 struct kevent_args *uap;
603 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
604 uap = (struct kevent_args *)arg;
606 error = copyin(uap->changelist, kevp, count * sizeof *kevp);
608 uap->changelist += count;
613 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
614 struct kevent_copyops *k_ops, const struct timespec *timeout)
616 struct kevent keva[KQ_NEVENTS];
617 struct kevent *kevp, *changes;
620 int i, n, nerrors, error;
622 if ((error = fget(td, fd, &fp)) != 0)
624 if ((error = kqueue_aquire(fp, &kq)) != 0)
629 while (nchanges > 0) {
630 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
631 error = k_ops->k_copyin(k_ops->arg, keva, n);
635 for (i = 0; i < n; i++) {
639 kevp->flags &= ~EV_SYSFLAGS;
640 error = kqueue_register(kq, kevp, td, 1);
643 kevp->flags = EV_ERROR;
645 (void) k_ops->k_copyout(k_ops->arg,
657 td->td_retval[0] = nerrors;
662 error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td);
664 kqueue_release(kq, 0);
672 kqueue_add_filteropts(int filt, struct filterops *filtops)
676 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
678 "trying to add a filterop that is out of range: %d is beyond %d\n",
679 ~filt, EVFILT_SYSCOUNT);
682 mtx_lock(&filterops_lock);
683 if (sysfilt_ops[~filt].for_fop != &null_filtops &&
684 sysfilt_ops[~filt].for_fop != NULL)
687 sysfilt_ops[~filt].for_fop = filtops;
688 sysfilt_ops[~filt].for_refcnt = 0;
690 mtx_unlock(&filterops_lock);
696 kqueue_del_filteropts(int filt)
701 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
704 mtx_lock(&filterops_lock);
705 if (sysfilt_ops[~filt].for_fop == &null_filtops ||
706 sysfilt_ops[~filt].for_fop == NULL)
708 else if (sysfilt_ops[~filt].for_refcnt != 0)
711 sysfilt_ops[~filt].for_fop = &null_filtops;
712 sysfilt_ops[~filt].for_refcnt = 0;
714 mtx_unlock(&filterops_lock);
719 static struct filterops *
720 kqueue_fo_find(int filt)
723 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
726 mtx_lock(&filterops_lock);
727 sysfilt_ops[~filt].for_refcnt++;
728 if (sysfilt_ops[~filt].for_fop == NULL)
729 sysfilt_ops[~filt].for_fop = &null_filtops;
730 mtx_unlock(&filterops_lock);
732 return sysfilt_ops[~filt].for_fop;
736 kqueue_fo_release(int filt)
739 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
742 mtx_lock(&filterops_lock);
743 KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
744 ("filter object refcount not valid on release"));
745 sysfilt_ops[~filt].for_refcnt--;
746 mtx_unlock(&filterops_lock);
750 * A ref to kq (obtained via kqueue_aquire) should be held. waitok will
751 * influence if memory allocation should wait. Make sure it is 0 if you
755 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
757 struct filedesc *fdp;
758 struct filterops *fops;
760 struct knote *kn, *tkn;
761 int error, filt, event;
772 fops = kqueue_fo_find(filt);
776 tkn = knote_alloc(waitok); /* prevent waiting with locks */
780 KASSERT(td != NULL, ("td is NULL"));
781 fdp = td->td_proc->p_fd;
783 /* validate descriptor */
785 if (fd < 0 || fd >= fdp->fd_nfiles ||
786 (fp = fdp->fd_ofiles[fd]) == NULL) {
787 FILEDESC_UNLOCK(fdp);
793 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
794 kev->ident, 0) != 0) {
795 /* unlock and try again */
796 FILEDESC_UNLOCK(fdp);
799 error = kqueue_expand(kq, fops, kev->ident, waitok);
805 if (fp->f_type == DTYPE_KQUEUE) {
807 * if we add some inteligence about what we are doing,
808 * we should be able to support events on ourselves.
809 * We need to know when we are doing this to prevent
810 * getting both the knlist lock and the kq lock since
811 * they are the same thing.
813 if (fp->f_data == kq) {
814 FILEDESC_UNLOCK(fdp);
819 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
822 FILEDESC_UNLOCK(fdp);
824 if (kev->ident < kq->kq_knlistsize) {
825 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
826 if (kev->filter == kn->kn_filter)
830 if ((kev->flags & EV_ADD) == EV_ADD)
831 kqueue_expand(kq, fops, kev->ident, waitok);
834 if (kq->kq_knhashmask != 0) {
837 list = &kq->kq_knhash[
838 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
839 SLIST_FOREACH(kn, list, kn_link)
840 if (kev->ident == kn->kn_id &&
841 kev->filter == kn->kn_filter)
846 /* knote is in the process of changing, wait for it to stablize. */
847 if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
852 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
853 kq->kq_state |= KQ_FLUXWAIT;
854 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
858 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
865 * kn now contains the matching knote, or NULL if no match
867 if (kev->flags & EV_ADD) {
879 * apply reference counts to knote structure, and
880 * do not release it at the end of this routine.
885 kn->kn_sfflags = kev->fflags;
886 kn->kn_sdata = kev->data;
889 kn->kn_kevent = *kev;
890 kn->kn_status = KN_INFLUX|KN_DETACHED;
892 error = knote_attach(kn, kq);
899 if ((error = kn->kn_fop->f_attach(kn)) != 0) {
906 * The user may change some filter values after the
907 * initial EV_ADD, but doing so will not reset any
908 * filter which has already been triggered.
910 kn->kn_status |= KN_INFLUX;
913 kn->kn_sfflags = kev->fflags;
914 kn->kn_sdata = kev->data;
915 kn->kn_kevent.udata = kev->udata;
919 * We can get here with kn->kn_knlist == NULL.
920 * This can happen when the initial attach event decides that
921 * the event is "completed" already. i.e. filt_procattach
922 * is called on a zombie process. It will call filt_proc
923 * which will remove it from the list, and NULL kn_knlist.
925 event = kn->kn_fop->f_event(kn, 0);
929 KNOTE_ACTIVATE(kn, 1);
930 kn->kn_status &= ~KN_INFLUX;
931 } else if (kev->flags & EV_DELETE) {
932 kn->kn_status |= KN_INFLUX;
934 if (!(kn->kn_status & KN_DETACHED))
935 kn->kn_fop->f_detach(kn);
940 if ((kev->flags & EV_DISABLE) &&
941 ((kn->kn_status & KN_DISABLED) == 0)) {
942 kn->kn_status |= KN_DISABLED;
945 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
946 kn->kn_status &= ~KN_DISABLED;
947 if ((kn->kn_status & KN_ACTIVE) &&
948 ((kn->kn_status & KN_QUEUED) == 0))
954 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
961 kqueue_fo_release(filt);
966 kqueue_aquire(struct file *fp, struct kqueue **kqp)
976 if (fp->f_type != DTYPE_KQUEUE || kq == NULL) {
982 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
996 kqueue_release(struct kqueue *kq, int locked)
1003 if (kq->kq_refcnt == 1)
1004 wakeup(&kq->kq_refcnt);
1010 kqueue_schedtask(struct kqueue *kq)
1014 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1015 ("scheduling kqueue task while draining"));
1017 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1018 taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task);
1019 kq->kq_state |= KQ_TASKSCHED;
1024 * Expand the kq to make sure we have storage for fops/ident pair.
1026 * Return 0 on success (or no work necessary), return errno on failure.
1028 * Not calling hashinit w/ waitok (proper malloc flag) should be safe.
1029 * If kqueue_register is called from a non-fd context, there usually/should
1033 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
1036 struct klist *list, *tmp_knhash;
1037 u_long tmp_knhashmask;
1040 int mflag = waitok ? M_WAITOK : M_NOWAIT;
1046 if (kq->kq_knlistsize <= fd) {
1047 size = kq->kq_knlistsize;
1050 MALLOC(list, struct klist *,
1051 size * sizeof list, M_KQUEUE, mflag);
1055 if (kq->kq_knlistsize > fd) {
1056 FREE(list, M_KQUEUE);
1059 if (kq->kq_knlist != NULL) {
1060 bcopy(kq->kq_knlist, list,
1061 kq->kq_knlistsize * sizeof list);
1062 FREE(kq->kq_knlist, M_KQUEUE);
1063 kq->kq_knlist = NULL;
1065 bzero((caddr_t)list +
1066 kq->kq_knlistsize * sizeof list,
1067 (size - kq->kq_knlistsize) * sizeof list);
1068 kq->kq_knlistsize = size;
1069 kq->kq_knlist = list;
1074 if (kq->kq_knhashmask == 0) {
1075 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1077 if (tmp_knhash == NULL)
1080 if (kq->kq_knhashmask == 0) {
1081 kq->kq_knhash = tmp_knhash;
1082 kq->kq_knhashmask = tmp_knhashmask;
1084 free(tmp_knhash, M_KQUEUE);
1095 kqueue_task(void *arg, int pending)
1103 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1106 KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1108 kq->kq_state &= ~KQ_TASKSCHED;
1109 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1110 wakeup(&kq->kq_state);
1113 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1117 * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1118 * We treat KN_MARKER knotes as if they are INFLUX.
1121 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1122 const struct timespec *tsp, struct kevent *keva, struct thread *td)
1124 struct kevent *kevp;
1125 struct timeval atv, rtv, ttv;
1126 struct knote *kn, *marker;
1127 int count, timeout, nkev, error;
1139 TIMESPEC_TO_TIMEVAL(&atv, tsp);
1140 if (itimerfix(&atv)) {
1144 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1147 timeout = atv.tv_sec > 24 * 60 * 60 ?
1148 24 * 60 * 60 * hz : tvtohz(&atv);
1149 getmicrouptime(&rtv);
1150 timevaladd(&atv, &rtv);
1156 marker = knote_alloc(1);
1157 if (marker == NULL) {
1161 marker->kn_status = KN_MARKER;
1166 if (atv.tv_sec || atv.tv_usec) {
1167 getmicrouptime(&rtv);
1168 if (timevalcmp(&rtv, &atv, >=))
1171 timevalsub(&ttv, &rtv);
1172 timeout = ttv.tv_sec > 24 * 60 * 60 ?
1173 24 * 60 * 60 * hz : tvtohz(&ttv);
1178 if (kq->kq_count == 0) {
1180 error = EWOULDBLOCK;
1182 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1183 kq->kq_state |= KQ_SLEEP;
1184 error = msleep(kq, &kq->kq_lock, PSOCK | PCATCH,
1189 /* don't restart after signals... */
1190 if (error == ERESTART)
1192 else if (error == EWOULDBLOCK)
1197 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1200 kn = TAILQ_FIRST(&kq->kq_head);
1202 if ((kn->kn_status == KN_MARKER && kn != marker) ||
1203 (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1204 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1205 kq->kq_state |= KQ_FLUXWAIT;
1206 error = msleep(kq, &kq->kq_lock, PSOCK,
1211 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1212 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1213 kn->kn_status &= ~KN_QUEUED;
1219 if (count == maxevents)
1223 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1224 ("KN_INFLUX set when not suppose to be"));
1226 if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
1227 kn->kn_status &= ~KN_QUEUED;
1228 kn->kn_status |= KN_INFLUX;
1232 * We don't need to lock the list since we've marked
1235 *kevp = kn->kn_kevent;
1236 if (!(kn->kn_status & KN_DETACHED))
1237 kn->kn_fop->f_detach(kn);
1242 kn->kn_status |= KN_INFLUX;
1244 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
1245 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1247 if (kn->kn_fop->f_event(kn, 0) == 0) {
1250 ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX);
1255 *kevp = kn->kn_kevent;
1257 if (kn->kn_flags & EV_CLEAR) {
1260 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1263 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1265 kn->kn_status &= ~(KN_INFLUX);
1270 /* we are returning a copy to the user */
1275 if (nkev == KQ_NEVENTS) {
1277 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1285 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1289 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1294 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1295 td->td_retval[0] = maxevents - count;
1301 * This could be expanded to call kqueue_scan, if desired.
1305 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1306 int flags, struct thread *td)
1313 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
1314 int flags, struct thread *td)
1321 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
1322 struct ucred *active_cred, struct thread *td)
1325 * Enabling sigio causes two major problems:
1326 * 1) infinite recursion:
1327 * Synopsys: kevent is being used to track signals and have FIOASYNC
1328 * set. On receipt of a signal this will cause a kqueue to recurse
1329 * into itself over and over. Sending the sigio causes the kqueue
1330 * to become ready, which in turn posts sigio again, forever.
1331 * Solution: this can be solved by setting a flag in the kqueue that
1332 * we have a SIGIO in progress.
1333 * 2) locking problems:
1334 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
1335 * us above the proc and pgrp locks.
1336 * Solution: Post a signal using an async mechanism, being sure to
1337 * record a generation count in the delivery so that we do not deliver
1338 * a signal to the wrong process.
1340 * Note, these two mechanisms are somewhat mutually exclusive!
1349 kq->kq_state |= KQ_ASYNC;
1351 kq->kq_state &= ~KQ_ASYNC;
1356 return (fsetown(*(int *)data, &kq->kq_sigio));
1359 *(int *)data = fgetown(&kq->kq_sigio);
1369 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
1376 if ((error = kqueue_aquire(fp, &kq)))
1380 if (events & (POLLIN | POLLRDNORM)) {
1382 revents |= events & (POLLIN | POLLRDNORM);
1384 selrecord(td, &kq->kq_sel);
1385 kq->kq_state |= KQ_SEL;
1388 kqueue_release(kq, 1);
1395 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1399 bzero((void *)st, sizeof *st);
1401 * We no longer return kq_count because the unlocked value is useless.
1402 * If you spent all this time getting the count, why not spend your
1403 * syscall better by calling kevent?
1405 * XXX - This is needed for libc_r.
1407 st->st_mode = S_IFIFO;
1413 kqueue_close(struct file *fp, struct thread *td)
1415 struct kqueue *kq = fp->f_data;
1416 struct filedesc *fdp;
1421 if ((error = kqueue_aquire(fp, &kq)))
1426 KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
1427 ("kqueue already closing"));
1428 kq->kq_state |= KQ_CLOSING;
1429 if (kq->kq_refcnt > 1)
1430 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
1432 KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
1435 KASSERT(knlist_empty(&kq->kq_sel.si_note),
1436 ("kqueue's knlist not empty"));
1438 for (i = 0; i < kq->kq_knlistsize; i++) {
1439 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
1440 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1441 ("KN_INFLUX set when not suppose to be"));
1442 kn->kn_status |= KN_INFLUX;
1444 if (!(kn->kn_status & KN_DETACHED))
1445 kn->kn_fop->f_detach(kn);
1450 if (kq->kq_knhashmask != 0) {
1451 for (i = 0; i <= kq->kq_knhashmask; i++) {
1452 while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
1453 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1454 ("KN_INFLUX set when not suppose to be"));
1455 kn->kn_status |= KN_INFLUX;
1457 if (!(kn->kn_status & KN_DETACHED))
1458 kn->kn_fop->f_detach(kn);
1465 if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
1466 kq->kq_state |= KQ_TASKDRAIN;
1467 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
1470 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1471 kq->kq_state &= ~KQ_SEL;
1472 selwakeuppri(&kq->kq_sel, PSOCK);
1477 FILEDESC_LOCK_FAST(fdp);
1478 SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list);
1479 FILEDESC_UNLOCK_FAST(fdp);
1481 knlist_destroy(&kq->kq_sel.si_note);
1482 mtx_destroy(&kq->kq_lock);
1485 if (kq->kq_knhash != NULL)
1486 free(kq->kq_knhash, M_KQUEUE);
1487 if (kq->kq_knlist != NULL)
1488 free(kq->kq_knlist, M_KQUEUE);
1490 funsetown(&kq->kq_sigio);
1498 kqueue_wakeup(struct kqueue *kq)
1502 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
1503 kq->kq_state &= ~KQ_SLEEP;
1506 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1507 kq->kq_state &= ~KQ_SEL;
1508 selwakeuppri(&kq->kq_sel, PSOCK);
1510 if (!knlist_empty(&kq->kq_sel.si_note))
1511 kqueue_schedtask(kq);
1512 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
1513 pgsigio(&kq->kq_sigio, SIGIO, 0);
1518 * Walk down a list of knotes, activating them if their event has triggered.
1520 * There is a possibility to optimize in the case of one kq watching another.
1521 * Instead of scheduling a task to wake it up, you could pass enough state
1522 * down the chain to make up the parent kqueue. Make this code functional
1526 knote(struct knlist *list, long hint, int islocked)
1534 KNL_ASSERT_LOCK(list, islocked);
1537 list->kl_lock(list->kl_lockarg);
1540 * If we unlock the list lock (and set KN_INFLUX), we can eliminate
1541 * the kqueue scheduling, but this will introduce four
1542 * lock/unlock's for each knote to test. If we do, continue to use
1543 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is
1544 * only safe if you want to remove the current item, which we are
1547 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
1549 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1551 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1552 kn->kn_status |= KN_HASKQLOCK;
1553 if (kn->kn_fop->f_event(kn, hint))
1554 KNOTE_ACTIVATE(kn, 1);
1555 kn->kn_status &= ~KN_HASKQLOCK;
1562 list->kl_unlock(list->kl_lockarg);
1566 * add a knote to a knlist
1569 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
1571 KNL_ASSERT_LOCK(knl, islocked);
1572 KQ_NOTOWNED(kn->kn_kq);
1573 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) ==
1574 (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED"));
1576 knl->kl_lock(knl->kl_lockarg);
1577 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
1579 knl->kl_unlock(knl->kl_lockarg);
1581 kn->kn_knlist = knl;
1582 kn->kn_status &= ~KN_DETACHED;
1583 KQ_UNLOCK(kn->kn_kq);
1587 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked)
1589 KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked"));
1590 KNL_ASSERT_LOCK(knl, knlislocked);
1591 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
1593 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX,
1594 ("knlist_remove called w/o knote being KN_INFLUX or already removed"));
1596 knl->kl_lock(knl->kl_lockarg);
1597 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
1598 kn->kn_knlist = NULL;
1600 knl->kl_unlock(knl->kl_lockarg);
1603 kn->kn_status |= KN_DETACHED;
1605 KQ_UNLOCK(kn->kn_kq);
1609 * remove all knotes from a specified klist
1612 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
1615 knlist_remove_kq(knl, kn, islocked, 0);
1619 * remove knote from a specified klist while in f_event handler.
1622 knlist_remove_inevent(struct knlist *knl, struct knote *kn)
1625 knlist_remove_kq(knl, kn, 1,
1626 (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK);
1630 knlist_empty(struct knlist *knl)
1632 KNL_ASSERT_LOCKED(knl);
1633 return SLIST_EMPTY(&knl->kl_list);
1636 static struct mtx knlist_lock;
1637 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
1639 static void knlist_mtx_lock(void *arg);
1640 static void knlist_mtx_unlock(void *arg);
1641 static int knlist_mtx_locked(void *arg);
1644 knlist_mtx_lock(void *arg)
1646 mtx_lock((struct mtx *)arg);
1650 knlist_mtx_unlock(void *arg)
1652 mtx_unlock((struct mtx *)arg);
1656 knlist_mtx_locked(void *arg)
1658 return (mtx_owned((struct mtx *)arg));
1662 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
1663 void (*kl_unlock)(void *), int (*kl_locked)(void *))
1667 knl->kl_lockarg = &knlist_lock;
1669 knl->kl_lockarg = lock;
1671 if (kl_lock == NULL)
1672 knl->kl_lock = knlist_mtx_lock;
1674 knl->kl_lock = kl_lock;
1675 if (kl_lock == NULL)
1676 knl->kl_unlock = knlist_mtx_unlock;
1678 knl->kl_unlock = kl_unlock;
1679 if (kl_locked == NULL)
1680 knl->kl_locked = knlist_mtx_locked;
1682 knl->kl_locked = kl_locked;
1684 SLIST_INIT(&knl->kl_list);
1688 knlist_destroy(struct knlist *knl)
1693 * if we run across this error, we need to find the offending
1694 * driver and have it call knlist_clear.
1696 if (!SLIST_EMPTY(&knl->kl_list))
1697 printf("WARNING: destroying knlist w/ knotes on it!\n");
1700 knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL;
1701 SLIST_INIT(&knl->kl_list);
1705 * Even if we are locked, we may need to drop the lock to allow any influx
1706 * knotes time to "settle".
1709 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
1715 KNL_ASSERT_LOCKED(knl);
1717 KNL_ASSERT_UNLOCKED(knl);
1718 again: /* need to reaquire lock since we have dropped it */
1719 knl->kl_lock(knl->kl_lockarg);
1722 SLIST_FOREACH(kn, &knl->kl_list, kn_selnext) {
1725 if ((kn->kn_status & KN_INFLUX)) {
1729 knlist_remove_kq(knl, kn, 1, 1);
1731 kn->kn_status |= KN_INFLUX | KN_DETACHED;
1735 /* Make sure cleared knotes disappear soon */
1736 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1742 if (!SLIST_EMPTY(&knl->kl_list)) {
1743 /* there are still KN_INFLUX remaining */
1744 kn = SLIST_FIRST(&knl->kl_list);
1747 KASSERT(kn->kn_status & KN_INFLUX,
1748 ("knote removed w/o list lock"));
1749 knl->kl_unlock(knl->kl_lockarg);
1750 kq->kq_state |= KQ_FLUXWAIT;
1751 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
1757 KNL_ASSERT_LOCKED(knl);
1759 knl->kl_unlock(knl->kl_lockarg);
1760 KNL_ASSERT_UNLOCKED(knl);
1765 * remove all knotes referencing a specified fd
1766 * must be called with FILEDESC lock. This prevents a race where a new fd
1767 * comes along and occupies the entry and we attach a knote to the fd.
1770 knote_fdclose(struct thread *td, int fd)
1772 struct filedesc *fdp = td->td_proc->p_fd;
1777 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1780 * We shouldn't have to worry about new kevents appearing on fd
1781 * since filedesc is locked.
1783 SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
1788 while (kq->kq_knlistsize > fd &&
1789 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
1790 if (kn->kn_status & KN_INFLUX) {
1791 /* someone else might be waiting on our knote */
1794 kq->kq_state |= KQ_FLUXWAIT;
1795 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
1798 kn->kn_status |= KN_INFLUX;
1800 if (!(kn->kn_status & KN_DETACHED))
1801 kn->kn_fop->f_detach(kn);
1811 knote_attach(struct knote *kn, struct kqueue *kq)
1815 KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX"));
1818 if (kn->kn_fop->f_isfd) {
1819 if (kn->kn_id >= kq->kq_knlistsize)
1821 list = &kq->kq_knlist[kn->kn_id];
1823 if (kq->kq_knhash == NULL)
1825 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1828 SLIST_INSERT_HEAD(list, kn, kn_link);
1834 * knote must already have been detached using the f_detach method.
1835 * no lock need to be held, it is assumed that the KN_INFLUX flag is set
1836 * to prevent other removal.
1839 knote_drop(struct knote *kn, struct thread *td)
1847 KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX,
1848 ("knote_drop called without KN_INFLUX set in kn_status"));
1851 if (kn->kn_fop->f_isfd)
1852 list = &kq->kq_knlist[kn->kn_id];
1854 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1856 if (!SLIST_EMPTY(list))
1857 SLIST_REMOVE(list, kn, knote, kn_link);
1858 if (kn->kn_status & KN_QUEUED)
1862 if (kn->kn_fop->f_isfd) {
1863 fdrop(kn->kn_fp, td);
1866 kqueue_fo_release(kn->kn_kevent.filter);
1872 knote_enqueue(struct knote *kn)
1874 struct kqueue *kq = kn->kn_kq;
1876 KQ_OWNED(kn->kn_kq);
1877 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
1879 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1880 kn->kn_status |= KN_QUEUED;
1886 knote_dequeue(struct knote *kn)
1888 struct kqueue *kq = kn->kn_kq;
1890 KQ_OWNED(kn->kn_kq);
1891 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
1893 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1894 kn->kn_status &= ~KN_QUEUED;
1902 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
1903 NULL, NULL, UMA_ALIGN_PTR, 0);
1905 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL)
1907 static struct knote *
1908 knote_alloc(int waitok)
1910 return ((struct knote *)uma_zalloc(knote_zone,
1911 (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO));
1915 knote_free(struct knote *kn)
1918 uma_zfree(knote_zone, kn);