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 */
253 * Simple redirection for all cdevsw style objects to call their fo_kqfilter
257 filt_fileattach(struct knote *kn)
260 return (fo_kqfilter(kn->kn_fp, kn));
265 kqueue_kqfilter(struct file *fp, struct knote *kn)
267 struct kqueue *kq = kn->kn_fp->f_data;
269 if (kn->kn_filter != EVFILT_READ)
272 kn->kn_status |= KN_KQUEUE;
273 kn->kn_fop = &kqread_filtops;
274 knlist_add(&kq->kq_sel.si_note, kn, 0);
280 filt_kqdetach(struct knote *kn)
282 struct kqueue *kq = kn->kn_fp->f_data;
284 knlist_remove(&kq->kq_sel.si_note, kn, 0);
289 filt_kqueue(struct knote *kn, long hint)
291 struct kqueue *kq = kn->kn_fp->f_data;
293 kn->kn_data = kq->kq_count;
294 return (kn->kn_data > 0);
297 /* XXX - move to kern_proc.c? */
299 filt_procattach(struct knote *kn)
306 p = pfind(kn->kn_id);
307 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
308 p = zpfind(kn->kn_id);
310 } else if (p != NULL && (p->p_flag & P_WEXIT)) {
316 if ((error = p_cansee(curthread, p)))
319 kn->kn_ptr.p_proc = p;
320 kn->kn_flags |= EV_CLEAR; /* automatically set */
323 * internal flag indicating registration done by kernel
325 if (kn->kn_flags & EV_FLAG1) {
326 kn->kn_data = kn->kn_sdata; /* ppid */
327 kn->kn_fflags = NOTE_CHILD;
328 kn->kn_flags &= ~EV_FLAG1;
332 knlist_add(&p->p_klist, kn, 1);
335 * Immediately activate any exit notes if the target process is a
336 * zombie. This is necessary to handle the case where the target
337 * process, e.g. a child, dies before the kevent is registered.
339 if (immediate && filt_proc(kn, NOTE_EXIT))
340 KNOTE_ACTIVATE(kn, 0);
348 * The knote may be attached to a different process, which may exit,
349 * leaving nothing for the knote to be attached to. So when the process
350 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
351 * it will be deleted when read out. However, as part of the knote deletion,
352 * this routine is called, so a check is needed to avoid actually performing
353 * a detach, because the original process does not exist any more.
355 /* XXX - move to kern_proc.c? */
357 filt_procdetach(struct knote *kn)
361 p = kn->kn_ptr.p_proc;
362 knlist_remove(&p->p_klist, kn, 0);
363 kn->kn_ptr.p_proc = NULL;
366 /* XXX - move to kern_proc.c? */
368 filt_proc(struct knote *kn, long hint)
370 struct proc *p = kn->kn_ptr.p_proc;
374 * mask off extra data
376 event = (u_int)hint & NOTE_PCTRLMASK;
379 * if the user is interested in this event, record it.
381 if (kn->kn_sfflags & event)
382 kn->kn_fflags |= event;
385 * process is gone, so flag the event as finished.
387 if (event == NOTE_EXIT) {
388 if (!(kn->kn_status & KN_DETACHED))
389 knlist_remove_inevent(&p->p_klist, kn);
390 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
391 kn->kn_ptr.p_proc = NULL;
396 * process forked, and user wants to track the new process,
397 * so attach a new knote to it, and immediately report an
398 * event with the parent's pid.
400 if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
405 * register knote with new process.
407 kev.ident = hint & NOTE_PDATAMASK; /* pid */
408 kev.filter = kn->kn_filter;
409 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
410 kev.fflags = kn->kn_sfflags;
411 kev.data = kn->kn_id; /* parent */
412 kev.udata = kn->kn_kevent.udata; /* preserve udata */
413 error = kqueue_register(kn->kn_kq, &kev, NULL, 0);
415 kn->kn_fflags |= NOTE_TRACKERR;
418 return (kn->kn_fflags != 0);
422 timertoticks(intptr_t data)
427 tv.tv_sec = data / 1000;
428 tv.tv_usec = (data % 1000) * 1000;
429 tticks = tvtohz(&tv);
434 /* XXX - move to kern_timeout.c? */
436 filt_timerexpire(void *knx)
438 struct knote *kn = knx;
439 struct callout *calloutp;
442 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */
444 if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) {
445 calloutp = (struct callout *)kn->kn_hook;
446 callout_reset(calloutp, timertoticks(kn->kn_sdata),
447 filt_timerexpire, kn);
452 * data contains amount of time to sleep, in milliseconds
454 /* XXX - move to kern_timeout.c? */
456 filt_timerattach(struct knote *kn)
458 struct callout *calloutp;
460 atomic_add_int(&kq_ncallouts, 1);
462 if (kq_ncallouts >= kq_calloutmax) {
463 atomic_add_int(&kq_ncallouts, -1);
467 kn->kn_flags |= EV_CLEAR; /* automatically set */
468 kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */
469 MALLOC(calloutp, struct callout *, sizeof(*calloutp),
471 callout_init(calloutp, CALLOUT_MPSAFE);
472 kn->kn_hook = calloutp;
473 callout_reset(calloutp, timertoticks(kn->kn_sdata), filt_timerexpire,
479 /* XXX - move to kern_timeout.c? */
481 filt_timerdetach(struct knote *kn)
483 struct callout *calloutp;
485 calloutp = (struct callout *)kn->kn_hook;
486 callout_drain(calloutp);
487 FREE(calloutp, M_KQUEUE);
488 atomic_add_int(&kq_ncallouts, -1);
489 kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */
492 /* XXX - move to kern_timeout.c? */
494 filt_timer(struct knote *kn, long hint)
497 return (kn->kn_data != 0);
504 kqueue(struct thread *td, struct kqueue_args *uap)
506 struct filedesc *fdp;
511 fdp = td->td_proc->p_fd;
512 error = falloc(td, &fp, &fd);
516 /* An extra reference on `nfp' has been held for us by falloc(). */
517 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
518 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK);
519 TAILQ_INIT(&kq->kq_head);
521 knlist_init(&kq->kq_sel.si_note, &kq->kq_lock, NULL, NULL, NULL);
522 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
524 FILEDESC_LOCK_FAST(fdp);
525 SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
526 FILEDESC_UNLOCK_FAST(fdp);
529 fp->f_flag = FREAD | FWRITE;
530 fp->f_type = DTYPE_KQUEUE;
531 fp->f_ops = &kqueueops;
536 td->td_retval[0] = fd;
541 #ifndef _SYS_SYSPROTO_H_
544 const struct kevent *changelist;
546 struct kevent *eventlist;
548 const struct timespec *timeout;
555 kevent(struct thread *td, struct kevent_args *uap)
557 struct timespec ts, *tsp;
558 struct kevent_copyops k_ops = { uap,
563 if (uap->timeout != NULL) {
564 error = copyin(uap->timeout, &ts, sizeof(ts));
571 return (kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
576 * Copy 'count' items into the destination list pointed to by uap->eventlist.
579 kevent_copyout(void *arg, struct kevent *kevp, int count)
581 struct kevent_args *uap;
584 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
585 uap = (struct kevent_args *)arg;
587 error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
589 uap->eventlist += count;
594 * Copy 'count' items from the list pointed to by uap->changelist.
597 kevent_copyin(void *arg, struct kevent *kevp, int count)
599 struct kevent_args *uap;
602 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
603 uap = (struct kevent_args *)arg;
605 error = copyin(uap->changelist, kevp, count * sizeof *kevp);
607 uap->changelist += count;
612 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
613 struct kevent_copyops *k_ops, const struct timespec *timeout)
615 struct kevent keva[KQ_NEVENTS];
616 struct kevent *kevp, *changes;
619 int i, n, nerrors, error;
621 if ((error = fget(td, fd, &fp)) != 0)
623 if ((error = kqueue_aquire(fp, &kq)) != 0)
628 while (nchanges > 0) {
629 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
630 error = k_ops->k_copyin(k_ops->arg, keva, n);
634 for (i = 0; i < n; i++) {
636 kevp->flags &= ~EV_SYSFLAGS;
637 error = kqueue_register(kq, kevp, td, 1);
640 kevp->flags = EV_ERROR;
642 (void) k_ops->k_copyout(k_ops->arg,
654 td->td_retval[0] = nerrors;
659 error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td);
661 kqueue_release(kq, 0);
669 kqueue_add_filteropts(int filt, struct filterops *filtops)
673 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
675 "trying to add a filterop that is out of range: %d is beyond %d\n",
676 ~filt, EVFILT_SYSCOUNT);
679 mtx_lock(&filterops_lock);
680 if (sysfilt_ops[~filt].for_fop != &null_filtops &&
681 sysfilt_ops[~filt].for_fop != NULL)
684 sysfilt_ops[~filt].for_fop = filtops;
685 sysfilt_ops[~filt].for_refcnt = 0;
687 mtx_unlock(&filterops_lock);
693 kqueue_del_filteropts(int filt)
698 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
701 mtx_lock(&filterops_lock);
702 if (sysfilt_ops[~filt].for_fop == &null_filtops ||
703 sysfilt_ops[~filt].for_fop == NULL)
705 else if (sysfilt_ops[~filt].for_refcnt != 0)
708 sysfilt_ops[~filt].for_fop = &null_filtops;
709 sysfilt_ops[~filt].for_refcnt = 0;
711 mtx_unlock(&filterops_lock);
716 static struct filterops *
717 kqueue_fo_find(int filt)
720 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
723 mtx_lock(&filterops_lock);
724 sysfilt_ops[~filt].for_refcnt++;
725 if (sysfilt_ops[~filt].for_fop == NULL)
726 sysfilt_ops[~filt].for_fop = &null_filtops;
727 mtx_unlock(&filterops_lock);
729 return sysfilt_ops[~filt].for_fop;
733 kqueue_fo_release(int filt)
736 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
739 mtx_lock(&filterops_lock);
740 KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
741 ("filter object refcount not valid on release"));
742 sysfilt_ops[~filt].for_refcnt--;
743 mtx_unlock(&filterops_lock);
747 * A ref to kq (obtained via kqueue_aquire) should be held. waitok will
748 * influence if memory allocation should wait. Make sure it is 0 if you
752 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
754 struct filedesc *fdp;
755 struct filterops *fops;
757 struct knote *kn, *tkn;
758 int error, filt, event;
769 fops = kqueue_fo_find(filt);
773 tkn = knote_alloc(waitok); /* prevent waiting with locks */
777 KASSERT(td != NULL, ("td is NULL"));
778 fdp = td->td_proc->p_fd;
780 /* validate descriptor */
782 if (fd < 0 || fd >= fdp->fd_nfiles ||
783 (fp = fdp->fd_ofiles[fd]) == NULL) {
784 FILEDESC_UNLOCK(fdp);
790 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
791 kev->ident, 0) != 0) {
792 /* unlock and try again */
793 FILEDESC_UNLOCK(fdp);
796 error = kqueue_expand(kq, fops, kev->ident, waitok);
802 if (fp->f_type == DTYPE_KQUEUE) {
804 * if we add some inteligence about what we are doing,
805 * we should be able to support events on ourselves.
806 * We need to know when we are doing this to prevent
807 * getting both the knlist lock and the kq lock since
808 * they are the same thing.
810 if (fp->f_data == kq) {
811 FILEDESC_UNLOCK(fdp);
816 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
819 FILEDESC_UNLOCK(fdp);
821 if (kev->ident < kq->kq_knlistsize) {
822 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
823 if (kev->filter == kn->kn_filter)
827 if ((kev->flags & EV_ADD) == EV_ADD)
828 kqueue_expand(kq, fops, kev->ident, waitok);
831 if (kq->kq_knhashmask != 0) {
834 list = &kq->kq_knhash[
835 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
836 SLIST_FOREACH(kn, list, kn_link)
837 if (kev->ident == kn->kn_id &&
838 kev->filter == kn->kn_filter)
843 /* knote is in the process of changing, wait for it to stablize. */
844 if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
849 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
850 kq->kq_state |= KQ_FLUXWAIT;
851 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
855 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
862 * kn now contains the matching knote, or NULL if no match
864 if (kev->flags & EV_ADD) {
876 * apply reference counts to knote structure, and
877 * do not release it at the end of this routine.
882 kn->kn_sfflags = kev->fflags;
883 kn->kn_sdata = kev->data;
886 kn->kn_kevent = *kev;
887 kn->kn_status = KN_INFLUX|KN_DETACHED;
889 error = knote_attach(kn, kq);
896 if ((error = kn->kn_fop->f_attach(kn)) != 0) {
903 * The user may change some filter values after the
904 * initial EV_ADD, but doing so will not reset any
905 * filter which has already been triggered.
907 kn->kn_status |= KN_INFLUX;
910 kn->kn_sfflags = kev->fflags;
911 kn->kn_sdata = kev->data;
912 kn->kn_kevent.udata = kev->udata;
916 * We can get here with kn->kn_knlist == NULL.
917 * This can happen when the initial attach event decides that
918 * the event is "completed" already. i.e. filt_procattach
919 * is called on a zombie process. It will call filt_proc
920 * which will remove it from the list, and NULL kn_knlist.
922 event = kn->kn_fop->f_event(kn, 0);
926 KNOTE_ACTIVATE(kn, 1);
927 kn->kn_status &= ~KN_INFLUX;
928 } else if (kev->flags & EV_DELETE) {
929 kn->kn_status |= KN_INFLUX;
931 if (!(kn->kn_status & KN_DETACHED))
932 kn->kn_fop->f_detach(kn);
937 if ((kev->flags & EV_DISABLE) &&
938 ((kn->kn_status & KN_DISABLED) == 0)) {
939 kn->kn_status |= KN_DISABLED;
942 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
943 kn->kn_status &= ~KN_DISABLED;
944 if ((kn->kn_status & KN_ACTIVE) &&
945 ((kn->kn_status & KN_QUEUED) == 0))
951 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
958 kqueue_fo_release(filt);
963 kqueue_aquire(struct file *fp, struct kqueue **kqp)
973 if (fp->f_type != DTYPE_KQUEUE || kq == NULL) {
979 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
993 kqueue_release(struct kqueue *kq, int locked)
1000 if (kq->kq_refcnt == 1)
1001 wakeup(&kq->kq_refcnt);
1007 kqueue_schedtask(struct kqueue *kq)
1011 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1012 ("scheduling kqueue task while draining"));
1014 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1015 taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task);
1016 kq->kq_state |= KQ_TASKSCHED;
1021 * Expand the kq to make sure we have storage for fops/ident pair.
1023 * Return 0 on success (or no work necessary), return errno on failure.
1025 * Not calling hashinit w/ waitok (proper malloc flag) should be safe.
1026 * If kqueue_register is called from a non-fd context, there usually/should
1030 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
1033 struct klist *list, *tmp_knhash;
1034 u_long tmp_knhashmask;
1037 int mflag = waitok ? M_WAITOK : M_NOWAIT;
1043 if (kq->kq_knlistsize <= fd) {
1044 size = kq->kq_knlistsize;
1047 MALLOC(list, struct klist *,
1048 size * sizeof list, M_KQUEUE, mflag);
1052 if (kq->kq_knlistsize > fd) {
1053 FREE(list, M_KQUEUE);
1056 if (kq->kq_knlist != NULL) {
1057 bcopy(kq->kq_knlist, list,
1058 kq->kq_knlistsize * sizeof list);
1059 FREE(kq->kq_knlist, M_KQUEUE);
1060 kq->kq_knlist = NULL;
1062 bzero((caddr_t)list +
1063 kq->kq_knlistsize * sizeof list,
1064 (size - kq->kq_knlistsize) * sizeof list);
1065 kq->kq_knlistsize = size;
1066 kq->kq_knlist = list;
1071 if (kq->kq_knhashmask == 0) {
1072 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1074 if (tmp_knhash == NULL)
1077 if (kq->kq_knhashmask == 0) {
1078 kq->kq_knhash = tmp_knhash;
1079 kq->kq_knhashmask = tmp_knhashmask;
1081 free(tmp_knhash, M_KQUEUE);
1092 kqueue_task(void *arg, int pending)
1100 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1103 KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1105 kq->kq_state &= ~KQ_TASKSCHED;
1106 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1107 wakeup(&kq->kq_state);
1110 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1114 * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1115 * We treat KN_MARKER knotes as if they are INFLUX.
1118 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1119 const struct timespec *tsp, struct kevent *keva, struct thread *td)
1121 struct kevent *kevp;
1122 struct timeval atv, rtv, ttv;
1123 struct knote *kn, *marker;
1124 int count, timeout, nkev, error;
1136 TIMESPEC_TO_TIMEVAL(&atv, tsp);
1137 if (itimerfix(&atv)) {
1141 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1144 timeout = atv.tv_sec > 24 * 60 * 60 ?
1145 24 * 60 * 60 * hz : tvtohz(&atv);
1146 getmicrouptime(&rtv);
1147 timevaladd(&atv, &rtv);
1153 marker = knote_alloc(1);
1154 if (marker == NULL) {
1158 marker->kn_status = KN_MARKER;
1163 if (atv.tv_sec || atv.tv_usec) {
1164 getmicrouptime(&rtv);
1165 if (timevalcmp(&rtv, &atv, >=))
1168 timevalsub(&ttv, &rtv);
1169 timeout = ttv.tv_sec > 24 * 60 * 60 ?
1170 24 * 60 * 60 * hz : tvtohz(&ttv);
1175 if (kq->kq_count == 0) {
1177 error = EWOULDBLOCK;
1179 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1180 kq->kq_state |= KQ_SLEEP;
1181 error = msleep(kq, &kq->kq_lock, PSOCK | PCATCH,
1186 /* don't restart after signals... */
1187 if (error == ERESTART)
1189 else if (error == EWOULDBLOCK)
1194 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1197 kn = TAILQ_FIRST(&kq->kq_head);
1199 if ((kn->kn_status == KN_MARKER && kn != marker) ||
1200 (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1201 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1202 kq->kq_state |= KQ_FLUXWAIT;
1203 error = msleep(kq, &kq->kq_lock, PSOCK,
1208 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1209 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1210 kn->kn_status &= ~KN_QUEUED;
1216 if (count == maxevents)
1220 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1221 ("KN_INFLUX set when not suppose to be"));
1223 if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
1224 kn->kn_status &= ~KN_QUEUED;
1225 kn->kn_status |= KN_INFLUX;
1229 * We don't need to lock the list since we've marked
1232 *kevp = kn->kn_kevent;
1233 if (!(kn->kn_status & KN_DETACHED))
1234 kn->kn_fop->f_detach(kn);
1239 kn->kn_status |= KN_INFLUX;
1241 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
1242 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1244 if (kn->kn_fop->f_event(kn, 0) == 0) {
1247 ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX);
1252 *kevp = kn->kn_kevent;
1254 if (kn->kn_flags & EV_CLEAR) {
1257 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1260 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1262 kn->kn_status &= ~(KN_INFLUX);
1267 /* we are returning a copy to the user */
1272 if (nkev == KQ_NEVENTS) {
1274 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1282 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1286 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1291 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1292 td->td_retval[0] = maxevents - count;
1298 * This could be expanded to call kqueue_scan, if desired.
1302 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1303 int flags, struct thread *td)
1310 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
1311 int flags, struct thread *td)
1318 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
1319 struct ucred *active_cred, struct thread *td)
1322 * Enabling sigio causes two major problems:
1323 * 1) infinite recursion:
1324 * Synopsys: kevent is being used to track signals and have FIOASYNC
1325 * set. On receipt of a signal this will cause a kqueue to recurse
1326 * into itself over and over. Sending the sigio causes the kqueue
1327 * to become ready, which in turn posts sigio again, forever.
1328 * Solution: this can be solved by setting a flag in the kqueue that
1329 * we have a SIGIO in progress.
1330 * 2) locking problems:
1331 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
1332 * us above the proc and pgrp locks.
1333 * Solution: Post a signal using an async mechanism, being sure to
1334 * record a generation count in the delivery so that we do not deliver
1335 * a signal to the wrong process.
1337 * Note, these two mechanisms are somewhat mutually exclusive!
1346 kq->kq_state |= KQ_ASYNC;
1348 kq->kq_state &= ~KQ_ASYNC;
1353 return (fsetown(*(int *)data, &kq->kq_sigio));
1356 *(int *)data = fgetown(&kq->kq_sigio);
1366 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
1373 if ((error = kqueue_aquire(fp, &kq)))
1377 if (events & (POLLIN | POLLRDNORM)) {
1379 revents |= events & (POLLIN | POLLRDNORM);
1381 selrecord(td, &kq->kq_sel);
1382 kq->kq_state |= KQ_SEL;
1385 kqueue_release(kq, 1);
1392 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1396 bzero((void *)st, sizeof *st);
1398 * We no longer return kq_count because the unlocked value is useless.
1399 * If you spent all this time getting the count, why not spend your
1400 * syscall better by calling kevent?
1402 * XXX - This is needed for libc_r.
1404 st->st_mode = S_IFIFO;
1410 kqueue_close(struct file *fp, struct thread *td)
1412 struct kqueue *kq = fp->f_data;
1413 struct filedesc *fdp;
1418 if ((error = kqueue_aquire(fp, &kq)))
1423 KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
1424 ("kqueue already closing"));
1425 kq->kq_state |= KQ_CLOSING;
1426 if (kq->kq_refcnt > 1)
1427 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
1429 KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
1432 KASSERT(knlist_empty(&kq->kq_sel.si_note),
1433 ("kqueue's knlist not empty"));
1435 for (i = 0; i < kq->kq_knlistsize; i++) {
1436 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
1437 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1438 ("KN_INFLUX set when not suppose to be"));
1439 kn->kn_status |= KN_INFLUX;
1441 if (!(kn->kn_status & KN_DETACHED))
1442 kn->kn_fop->f_detach(kn);
1447 if (kq->kq_knhashmask != 0) {
1448 for (i = 0; i <= kq->kq_knhashmask; i++) {
1449 while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
1450 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1451 ("KN_INFLUX set when not suppose to be"));
1452 kn->kn_status |= KN_INFLUX;
1454 if (!(kn->kn_status & KN_DETACHED))
1455 kn->kn_fop->f_detach(kn);
1462 if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
1463 kq->kq_state |= KQ_TASKDRAIN;
1464 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
1467 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1468 kq->kq_state &= ~KQ_SEL;
1469 selwakeuppri(&kq->kq_sel, PSOCK);
1474 FILEDESC_LOCK_FAST(fdp);
1475 SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list);
1476 FILEDESC_UNLOCK_FAST(fdp);
1478 knlist_destroy(&kq->kq_sel.si_note);
1479 mtx_destroy(&kq->kq_lock);
1482 if (kq->kq_knhash != NULL)
1483 free(kq->kq_knhash, M_KQUEUE);
1484 if (kq->kq_knlist != NULL)
1485 free(kq->kq_knlist, M_KQUEUE);
1487 funsetown(&kq->kq_sigio);
1495 kqueue_wakeup(struct kqueue *kq)
1499 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
1500 kq->kq_state &= ~KQ_SLEEP;
1503 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1504 kq->kq_state &= ~KQ_SEL;
1505 selwakeuppri(&kq->kq_sel, PSOCK);
1507 if (!knlist_empty(&kq->kq_sel.si_note))
1508 kqueue_schedtask(kq);
1509 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
1510 pgsigio(&kq->kq_sigio, SIGIO, 0);
1515 * Walk down a list of knotes, activating them if their event has triggered.
1517 * There is a possibility to optimize in the case of one kq watching another.
1518 * Instead of scheduling a task to wake it up, you could pass enough state
1519 * down the chain to make up the parent kqueue. Make this code functional
1523 knote(struct knlist *list, long hint, int islocked)
1531 KNL_ASSERT_LOCK(list, islocked);
1534 list->kl_lock(list->kl_lockarg);
1537 * If we unlock the list lock (and set KN_INFLUX), we can eliminate
1538 * the kqueue scheduling, but this will introduce four
1539 * lock/unlock's for each knote to test. If we do, continue to use
1540 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is
1541 * only safe if you want to remove the current item, which we are
1544 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
1546 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1548 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1549 kn->kn_status |= KN_HASKQLOCK;
1550 if (kn->kn_fop->f_event(kn, hint))
1551 KNOTE_ACTIVATE(kn, 1);
1552 kn->kn_status &= ~KN_HASKQLOCK;
1559 list->kl_unlock(list->kl_lockarg);
1563 * add a knote to a knlist
1566 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
1568 KNL_ASSERT_LOCK(knl, islocked);
1569 KQ_NOTOWNED(kn->kn_kq);
1570 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) ==
1571 (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED"));
1573 knl->kl_lock(knl->kl_lockarg);
1574 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
1576 knl->kl_unlock(knl->kl_lockarg);
1578 kn->kn_knlist = knl;
1579 kn->kn_status &= ~KN_DETACHED;
1580 KQ_UNLOCK(kn->kn_kq);
1584 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked)
1586 KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked"));
1587 KNL_ASSERT_LOCK(knl, knlislocked);
1588 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
1590 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX,
1591 ("knlist_remove called w/o knote being KN_INFLUX or already removed"));
1593 knl->kl_lock(knl->kl_lockarg);
1594 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
1595 kn->kn_knlist = NULL;
1597 knl->kl_unlock(knl->kl_lockarg);
1600 kn->kn_status |= KN_DETACHED;
1602 KQ_UNLOCK(kn->kn_kq);
1606 * remove all knotes from a specified klist
1609 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
1612 knlist_remove_kq(knl, kn, islocked, 0);
1616 * remove knote from a specified klist while in f_event handler.
1619 knlist_remove_inevent(struct knlist *knl, struct knote *kn)
1622 knlist_remove_kq(knl, kn, 1,
1623 (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK);
1627 knlist_empty(struct knlist *knl)
1629 KNL_ASSERT_LOCKED(knl);
1630 return SLIST_EMPTY(&knl->kl_list);
1633 static struct mtx knlist_lock;
1634 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
1636 static void knlist_mtx_lock(void *arg);
1637 static void knlist_mtx_unlock(void *arg);
1638 static int knlist_mtx_locked(void *arg);
1641 knlist_mtx_lock(void *arg)
1643 mtx_lock((struct mtx *)arg);
1647 knlist_mtx_unlock(void *arg)
1649 mtx_unlock((struct mtx *)arg);
1653 knlist_mtx_locked(void *arg)
1655 return (mtx_owned((struct mtx *)arg));
1659 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
1660 void (*kl_unlock)(void *), int (*kl_locked)(void *))
1664 knl->kl_lockarg = &knlist_lock;
1666 knl->kl_lockarg = lock;
1668 if (kl_lock == NULL)
1669 knl->kl_lock = knlist_mtx_lock;
1671 knl->kl_lock = kl_lock;
1672 if (kl_lock == NULL)
1673 knl->kl_unlock = knlist_mtx_unlock;
1675 knl->kl_unlock = kl_unlock;
1676 if (kl_locked == NULL)
1677 knl->kl_locked = knlist_mtx_locked;
1679 knl->kl_locked = kl_locked;
1681 SLIST_INIT(&knl->kl_list);
1685 knlist_destroy(struct knlist *knl)
1690 * if we run across this error, we need to find the offending
1691 * driver and have it call knlist_clear.
1693 if (!SLIST_EMPTY(&knl->kl_list))
1694 printf("WARNING: destroying knlist w/ knotes on it!\n");
1697 knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL;
1698 SLIST_INIT(&knl->kl_list);
1702 * Even if we are locked, we may need to drop the lock to allow any influx
1703 * knotes time to "settle".
1706 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
1712 KNL_ASSERT_LOCKED(knl);
1714 KNL_ASSERT_UNLOCKED(knl);
1715 again: /* need to reaquire lock since we have dropped it */
1716 knl->kl_lock(knl->kl_lockarg);
1719 SLIST_FOREACH(kn, &knl->kl_list, kn_selnext) {
1722 if ((kn->kn_status & KN_INFLUX)) {
1726 knlist_remove_kq(knl, kn, 1, 1);
1728 kn->kn_status |= KN_INFLUX | KN_DETACHED;
1732 /* Make sure cleared knotes disappear soon */
1733 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1739 if (!SLIST_EMPTY(&knl->kl_list)) {
1740 /* there are still KN_INFLUX remaining */
1741 kn = SLIST_FIRST(&knl->kl_list);
1744 KASSERT(kn->kn_status & KN_INFLUX,
1745 ("knote removed w/o list lock"));
1746 knl->kl_unlock(knl->kl_lockarg);
1747 kq->kq_state |= KQ_FLUXWAIT;
1748 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
1754 KNL_ASSERT_LOCKED(knl);
1756 knl->kl_unlock(knl->kl_lockarg);
1757 KNL_ASSERT_UNLOCKED(knl);
1762 * remove all knotes referencing a specified fd
1763 * must be called with FILEDESC lock. This prevents a race where a new fd
1764 * comes along and occupies the entry and we attach a knote to the fd.
1767 knote_fdclose(struct thread *td, int fd)
1769 struct filedesc *fdp = td->td_proc->p_fd;
1774 FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1777 * We shouldn't have to worry about new kevents appearing on fd
1778 * since filedesc is locked.
1780 SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
1785 while (kq->kq_knlistsize > fd &&
1786 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
1787 if (kn->kn_status & KN_INFLUX) {
1788 /* someone else might be waiting on our knote */
1791 kq->kq_state |= KQ_FLUXWAIT;
1792 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
1795 kn->kn_status |= KN_INFLUX;
1797 if (!(kn->kn_status & KN_DETACHED))
1798 kn->kn_fop->f_detach(kn);
1808 knote_attach(struct knote *kn, struct kqueue *kq)
1812 KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX"));
1815 if (kn->kn_fop->f_isfd) {
1816 if (kn->kn_id >= kq->kq_knlistsize)
1818 list = &kq->kq_knlist[kn->kn_id];
1820 if (kq->kq_knhash == NULL)
1822 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1825 SLIST_INSERT_HEAD(list, kn, kn_link);
1831 * knote must already have been detatched using the f_detach method.
1832 * no lock need to be held, it is assumed that the KN_INFLUX flag is set
1833 * to prevent other removal.
1836 knote_drop(struct knote *kn, struct thread *td)
1844 KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX,
1845 ("knote_drop called without KN_INFLUX set in kn_status"));
1848 if (kn->kn_fop->f_isfd)
1849 list = &kq->kq_knlist[kn->kn_id];
1851 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1853 SLIST_REMOVE(list, kn, knote, kn_link);
1854 if (kn->kn_status & KN_QUEUED)
1858 if (kn->kn_fop->f_isfd) {
1859 fdrop(kn->kn_fp, td);
1862 kqueue_fo_release(kn->kn_kevent.filter);
1868 knote_enqueue(struct knote *kn)
1870 struct kqueue *kq = kn->kn_kq;
1872 KQ_OWNED(kn->kn_kq);
1873 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
1875 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1876 kn->kn_status |= KN_QUEUED;
1882 knote_dequeue(struct knote *kn)
1884 struct kqueue *kq = kn->kn_kq;
1886 KQ_OWNED(kn->kn_kq);
1887 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
1889 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1890 kn->kn_status &= ~KN_QUEUED;
1898 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
1899 NULL, NULL, UMA_ALIGN_PTR, 0);
1901 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL)
1903 static struct knote *
1904 knote_alloc(int waitok)
1906 return ((struct knote *)uma_zalloc(knote_zone,
1907 (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO));
1911 knote_free(struct knote *kn)
1914 uma_zfree(knote_zone, kn);