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 "opt_ktrace.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/mutex.h>
39 #include <sys/malloc.h>
40 #include <sys/unistd.h>
42 #include <sys/filedesc.h>
43 #include <sys/filio.h>
44 #include <sys/fcntl.h>
45 #include <sys/kthread.h>
46 #include <sys/selinfo.h>
47 #include <sys/queue.h>
48 #include <sys/event.h>
49 #include <sys/eventvar.h>
51 #include <sys/protosw.h>
52 #include <sys/sigio.h>
53 #include <sys/signalvar.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
57 #include <sys/sysctl.h>
58 #include <sys/sysproto.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/taskqueue.h>
63 #include <sys/ktrace.h>
68 static MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
71 * This lock is used if multiple kq locks are required. This possibly
72 * should be made into a per proc lock.
74 static struct mtx kq_global;
75 MTX_SYSINIT(kq_global, &kq_global, "kqueue order", MTX_DEF);
76 #define KQ_GLOBAL_LOCK(lck, haslck) do { \
81 #define KQ_GLOBAL_UNLOCK(lck, haslck) do { \
87 TASKQUEUE_DEFINE_THREAD(kqueue);
89 static int kevent_copyout(void *arg, struct kevent *kevp, int count);
90 static int kevent_copyin(void *arg, struct kevent *kevp, int count);
91 static int kqueue_register(struct kqueue *kq, struct kevent *kev,
92 struct thread *td, int waitok);
93 static int kqueue_acquire(struct file *fp, struct kqueue **kqp);
94 static void kqueue_release(struct kqueue *kq, int locked);
95 static int kqueue_expand(struct kqueue *kq, struct filterops *fops,
96 uintptr_t ident, int waitok);
97 static void kqueue_task(void *arg, int pending);
98 static int kqueue_scan(struct kqueue *kq, int maxevents,
99 struct kevent_copyops *k_ops,
100 const struct timespec *timeout,
101 struct kevent *keva, struct thread *td);
102 static void kqueue_wakeup(struct kqueue *kq);
103 static struct filterops *kqueue_fo_find(int filt);
104 static void kqueue_fo_release(int filt);
106 static fo_rdwr_t kqueue_read;
107 static fo_rdwr_t kqueue_write;
108 static fo_ioctl_t kqueue_ioctl;
109 static fo_poll_t kqueue_poll;
110 static fo_kqfilter_t kqueue_kqfilter;
111 static fo_stat_t kqueue_stat;
112 static fo_close_t kqueue_close;
114 static struct fileops kqueueops = {
115 .fo_read = kqueue_read,
116 .fo_write = kqueue_write,
117 .fo_ioctl = kqueue_ioctl,
118 .fo_poll = kqueue_poll,
119 .fo_kqfilter = kqueue_kqfilter,
120 .fo_stat = kqueue_stat,
121 .fo_close = kqueue_close,
124 static int knote_attach(struct knote *kn, struct kqueue *kq);
125 static void knote_drop(struct knote *kn, struct thread *td);
126 static void knote_enqueue(struct knote *kn);
127 static void knote_dequeue(struct knote *kn);
128 static void knote_init(void);
129 static struct knote *knote_alloc(int waitok);
130 static void knote_free(struct knote *kn);
132 static void filt_kqdetach(struct knote *kn);
133 static int filt_kqueue(struct knote *kn, long hint);
134 static int filt_procattach(struct knote *kn);
135 static void filt_procdetach(struct knote *kn);
136 static int filt_proc(struct knote *kn, long hint);
137 static int filt_fileattach(struct knote *kn);
138 static void filt_timerexpire(void *knx);
139 static int filt_timerattach(struct knote *kn);
140 static void filt_timerdetach(struct knote *kn);
141 static int filt_timer(struct knote *kn, long hint);
143 static struct filterops file_filtops =
144 { 1, filt_fileattach, NULL, NULL };
145 static struct filterops kqread_filtops =
146 { 1, NULL, filt_kqdetach, filt_kqueue };
147 /* XXX - move to kern_proc.c? */
148 static struct filterops proc_filtops =
149 { 0, filt_procattach, filt_procdetach, filt_proc };
150 static struct filterops timer_filtops =
151 { 0, filt_timerattach, filt_timerdetach, filt_timer };
153 static uma_zone_t knote_zone;
154 static int kq_ncallouts = 0;
155 static int kq_calloutmax = (4 * 1024);
156 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
157 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
159 /* XXX - ensure not KN_INFLUX?? */
160 #define KNOTE_ACTIVATE(kn, islock) do { \
162 mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED); \
164 KQ_LOCK((kn)->kn_kq); \
165 (kn)->kn_status |= KN_ACTIVE; \
166 if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
167 knote_enqueue((kn)); \
169 KQ_UNLOCK((kn)->kn_kq); \
171 #define KQ_LOCK(kq) do { \
172 mtx_lock(&(kq)->kq_lock); \
174 #define KQ_FLUX_WAKEUP(kq) do { \
175 if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) { \
176 (kq)->kq_state &= ~KQ_FLUXWAIT; \
180 #define KQ_UNLOCK_FLUX(kq) do { \
181 KQ_FLUX_WAKEUP(kq); \
182 mtx_unlock(&(kq)->kq_lock); \
184 #define KQ_UNLOCK(kq) do { \
185 mtx_unlock(&(kq)->kq_lock); \
187 #define KQ_OWNED(kq) do { \
188 mtx_assert(&(kq)->kq_lock, MA_OWNED); \
190 #define KQ_NOTOWNED(kq) do { \
191 mtx_assert(&(kq)->kq_lock, MA_NOTOWNED); \
193 #define KN_LIST_LOCK(kn) do { \
194 if (kn->kn_knlist != NULL) \
195 kn->kn_knlist->kl_lock(kn->kn_knlist->kl_lockarg); \
197 #define KN_LIST_UNLOCK(kn) do { \
198 if (kn->kn_knlist != NULL) \
199 kn->kn_knlist->kl_unlock(kn->kn_knlist->kl_lockarg); \
201 #define KNL_ASSERT_LOCK(knl, islocked) do { \
203 KNL_ASSERT_LOCKED(knl); \
205 KNL_ASSERT_UNLOCKED(knl); \
208 #define KNL_ASSERT_LOCKED(knl) do { \
209 if (!knl->kl_locked((knl)->kl_lockarg)) \
210 panic("knlist not locked, but should be"); \
212 #define KNL_ASSERT_UNLOCKED(knl) do { \
213 if (knl->kl_locked((knl)->kl_lockarg)) \
214 panic("knlist locked, but should not be"); \
216 #else /* !INVARIANTS */
217 #define KNL_ASSERT_LOCKED(knl) do {} while(0)
218 #define KNL_ASSERT_UNLOCKED(knl) do {} while (0)
219 #endif /* INVARIANTS */
221 #define KN_HASHSIZE 64 /* XXX should be tunable */
222 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
225 filt_nullattach(struct knote *kn)
231 struct filterops null_filtops =
232 { 0, filt_nullattach, NULL, NULL };
234 /* XXX - make SYSINIT to add these, and move into respective modules. */
235 extern struct filterops sig_filtops;
236 extern struct filterops fs_filtops;
239 * Table for for all system-defined filters.
241 static struct mtx filterops_lock;
242 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops",
245 struct filterops *for_fop;
247 } sysfilt_ops[EVFILT_SYSCOUNT] = {
248 { &file_filtops }, /* EVFILT_READ */
249 { &file_filtops }, /* EVFILT_WRITE */
250 { &null_filtops }, /* EVFILT_AIO */
251 { &file_filtops }, /* EVFILT_VNODE */
252 { &proc_filtops }, /* EVFILT_PROC */
253 { &sig_filtops }, /* EVFILT_SIGNAL */
254 { &timer_filtops }, /* EVFILT_TIMER */
255 { &file_filtops }, /* EVFILT_NETDEV */
256 { &fs_filtops }, /* EVFILT_FS */
257 { &null_filtops }, /* EVFILT_LIO */
261 * Simple redirection for all cdevsw style objects to call their fo_kqfilter
265 filt_fileattach(struct knote *kn)
268 return (fo_kqfilter(kn->kn_fp, kn));
273 kqueue_kqfilter(struct file *fp, struct knote *kn)
275 struct kqueue *kq = kn->kn_fp->f_data;
277 if (kn->kn_filter != EVFILT_READ)
280 kn->kn_status |= KN_KQUEUE;
281 kn->kn_fop = &kqread_filtops;
282 knlist_add(&kq->kq_sel.si_note, kn, 0);
288 filt_kqdetach(struct knote *kn)
290 struct kqueue *kq = kn->kn_fp->f_data;
292 knlist_remove(&kq->kq_sel.si_note, kn, 0);
297 filt_kqueue(struct knote *kn, long hint)
299 struct kqueue *kq = kn->kn_fp->f_data;
301 kn->kn_data = kq->kq_count;
302 return (kn->kn_data > 0);
305 /* XXX - move to kern_proc.c? */
307 filt_procattach(struct knote *kn)
314 p = pfind(kn->kn_id);
315 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
316 p = zpfind(kn->kn_id);
318 } else if (p != NULL && (p->p_flag & P_WEXIT)) {
324 if ((error = p_cansee(curthread, p)))
327 kn->kn_ptr.p_proc = p;
328 kn->kn_flags |= EV_CLEAR; /* automatically set */
331 * internal flag indicating registration done by kernel
333 if (kn->kn_flags & EV_FLAG1) {
334 kn->kn_data = kn->kn_sdata; /* ppid */
335 kn->kn_fflags = NOTE_CHILD;
336 kn->kn_flags &= ~EV_FLAG1;
340 knlist_add(&p->p_klist, kn, 1);
343 * Immediately activate any exit notes if the target process is a
344 * zombie. This is necessary to handle the case where the target
345 * process, e.g. a child, dies before the kevent is registered.
347 if (immediate && filt_proc(kn, NOTE_EXIT))
348 KNOTE_ACTIVATE(kn, 0);
356 * The knote may be attached to a different process, which may exit,
357 * leaving nothing for the knote to be attached to. So when the process
358 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
359 * it will be deleted when read out. However, as part of the knote deletion,
360 * this routine is called, so a check is needed to avoid actually performing
361 * a detach, because the original process does not exist any more.
363 /* XXX - move to kern_proc.c? */
365 filt_procdetach(struct knote *kn)
369 p = kn->kn_ptr.p_proc;
370 knlist_remove(&p->p_klist, kn, 0);
371 kn->kn_ptr.p_proc = NULL;
374 /* XXX - move to kern_proc.c? */
376 filt_proc(struct knote *kn, long hint)
378 struct proc *p = kn->kn_ptr.p_proc;
382 * mask off extra data
384 event = (u_int)hint & NOTE_PCTRLMASK;
387 * if the user is interested in this event, record it.
389 if (kn->kn_sfflags & event)
390 kn->kn_fflags |= event;
393 * process is gone, so flag the event as finished.
395 if (event == NOTE_EXIT) {
396 if (!(kn->kn_status & KN_DETACHED))
397 knlist_remove_inevent(&p->p_klist, kn);
398 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
399 kn->kn_data = p->p_xstat;
400 kn->kn_ptr.p_proc = NULL;
405 * process forked, and user wants to track the new process,
406 * so attach a new knote to it, and immediately report an
407 * event with the parent's pid.
409 if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
414 * register knote with new process.
416 kev.ident = hint & NOTE_PDATAMASK; /* pid */
417 kev.filter = kn->kn_filter;
418 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
419 kev.fflags = kn->kn_sfflags;
420 kev.data = kn->kn_id; /* parent */
421 kev.udata = kn->kn_kevent.udata; /* preserve udata */
422 error = kqueue_register(kn->kn_kq, &kev, NULL, 0);
424 kn->kn_fflags |= NOTE_TRACKERR;
427 return (kn->kn_fflags != 0);
431 timertoticks(intptr_t data)
436 tv.tv_sec = data / 1000;
437 tv.tv_usec = (data % 1000) * 1000;
438 tticks = tvtohz(&tv);
443 /* XXX - move to kern_timeout.c? */
445 filt_timerexpire(void *knx)
447 struct knote *kn = knx;
448 struct callout *calloutp;
451 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */
453 if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) {
454 calloutp = (struct callout *)kn->kn_hook;
455 callout_reset(calloutp, timertoticks(kn->kn_sdata),
456 filt_timerexpire, kn);
461 * data contains amount of time to sleep, in milliseconds
463 /* XXX - move to kern_timeout.c? */
465 filt_timerattach(struct knote *kn)
467 struct callout *calloutp;
469 atomic_add_int(&kq_ncallouts, 1);
471 if (kq_ncallouts >= kq_calloutmax) {
472 atomic_add_int(&kq_ncallouts, -1);
476 kn->kn_flags |= EV_CLEAR; /* automatically set */
477 kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */
478 MALLOC(calloutp, struct callout *, sizeof(*calloutp),
480 callout_init(calloutp, CALLOUT_MPSAFE);
481 kn->kn_hook = calloutp;
482 callout_reset(calloutp, timertoticks(kn->kn_sdata), filt_timerexpire,
488 /* XXX - move to kern_timeout.c? */
490 filt_timerdetach(struct knote *kn)
492 struct callout *calloutp;
494 calloutp = (struct callout *)kn->kn_hook;
495 callout_drain(calloutp);
496 FREE(calloutp, M_KQUEUE);
497 atomic_add_int(&kq_ncallouts, -1);
498 kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */
501 /* XXX - move to kern_timeout.c? */
503 filt_timer(struct knote *kn, long hint)
506 return (kn->kn_data != 0);
510 kqueue(struct thread *td, struct kqueue_args *uap)
512 struct filedesc *fdp;
517 fdp = td->td_proc->p_fd;
518 error = falloc(td, &fp, &fd);
522 /* An extra reference on `nfp' has been held for us by falloc(). */
523 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
524 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK);
525 TAILQ_INIT(&kq->kq_head);
527 knlist_init(&kq->kq_sel.si_note, &kq->kq_lock, NULL, NULL, NULL);
528 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
531 SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
532 FILEDESC_XUNLOCK(fdp);
535 fp->f_flag = FREAD | FWRITE;
536 fp->f_type = DTYPE_KQUEUE;
538 fp->f_ops = &kqueueops;
542 td->td_retval[0] = fd;
547 #ifndef _SYS_SYSPROTO_H_
550 const struct kevent *changelist;
552 struct kevent *eventlist;
554 const struct timespec *timeout;
558 kevent(struct thread *td, struct kevent_args *uap)
560 struct timespec ts, *tsp;
561 struct kevent_copyops k_ops = { uap,
568 struct uio *ktruioin = NULL;
569 struct uio *ktruioout = NULL;
572 if (uap->timeout != NULL) {
573 error = copyin(uap->timeout, &ts, sizeof(ts));
581 if (KTRPOINT(td, KTR_GENIO)) {
582 ktriov.iov_base = uap->changelist;
583 ktriov.iov_len = uap->nchanges * sizeof(struct kevent);
584 ktruio = (struct uio){ .uio_iov = &ktriov, .uio_iovcnt = 1,
585 .uio_segflg = UIO_USERSPACE, .uio_rw = UIO_READ,
587 ktruioin = cloneuio(&ktruio);
588 ktriov.iov_base = uap->eventlist;
589 ktriov.iov_len = uap->nevents * sizeof(struct kevent);
590 ktruioout = cloneuio(&ktruio);
594 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
598 if (ktruioin != NULL) {
599 ktruioin->uio_resid = uap->nchanges * sizeof(struct kevent);
600 ktrgenio(uap->fd, UIO_WRITE, ktruioin, 0);
601 ktruioout->uio_resid = td->td_retval[0] * sizeof(struct kevent);
602 ktrgenio(uap->fd, UIO_READ, ktruioout, error);
610 * Copy 'count' items into the destination list pointed to by uap->eventlist.
613 kevent_copyout(void *arg, struct kevent *kevp, int count)
615 struct kevent_args *uap;
618 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
619 uap = (struct kevent_args *)arg;
621 error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
623 uap->eventlist += count;
628 * Copy 'count' items from the list pointed to by uap->changelist.
631 kevent_copyin(void *arg, struct kevent *kevp, int count)
633 struct kevent_args *uap;
636 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
637 uap = (struct kevent_args *)arg;
639 error = copyin(uap->changelist, kevp, count * sizeof *kevp);
641 uap->changelist += count;
646 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
647 struct kevent_copyops *k_ops, const struct timespec *timeout)
649 struct kevent keva[KQ_NEVENTS];
650 struct kevent *kevp, *changes;
653 int i, n, nerrors, error;
655 if ((error = fget(td, fd, &fp)) != 0)
657 if ((error = kqueue_acquire(fp, &kq)) != 0)
662 while (nchanges > 0) {
663 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
664 error = k_ops->k_copyin(k_ops->arg, keva, n);
668 for (i = 0; i < n; i++) {
672 kevp->flags &= ~EV_SYSFLAGS;
673 error = kqueue_register(kq, kevp, td, 1);
676 kevp->flags = EV_ERROR;
678 (void) k_ops->k_copyout(k_ops->arg,
690 td->td_retval[0] = nerrors;
695 error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td);
697 kqueue_release(kq, 0);
704 kqueue_add_filteropts(int filt, struct filterops *filtops)
708 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
710 "trying to add a filterop that is out of range: %d is beyond %d\n",
711 ~filt, EVFILT_SYSCOUNT);
714 mtx_lock(&filterops_lock);
715 if (sysfilt_ops[~filt].for_fop != &null_filtops &&
716 sysfilt_ops[~filt].for_fop != NULL)
719 sysfilt_ops[~filt].for_fop = filtops;
720 sysfilt_ops[~filt].for_refcnt = 0;
722 mtx_unlock(&filterops_lock);
728 kqueue_del_filteropts(int filt)
733 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
736 mtx_lock(&filterops_lock);
737 if (sysfilt_ops[~filt].for_fop == &null_filtops ||
738 sysfilt_ops[~filt].for_fop == NULL)
740 else if (sysfilt_ops[~filt].for_refcnt != 0)
743 sysfilt_ops[~filt].for_fop = &null_filtops;
744 sysfilt_ops[~filt].for_refcnt = 0;
746 mtx_unlock(&filterops_lock);
751 static struct filterops *
752 kqueue_fo_find(int filt)
755 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
758 mtx_lock(&filterops_lock);
759 sysfilt_ops[~filt].for_refcnt++;
760 if (sysfilt_ops[~filt].for_fop == NULL)
761 sysfilt_ops[~filt].for_fop = &null_filtops;
762 mtx_unlock(&filterops_lock);
764 return sysfilt_ops[~filt].for_fop;
768 kqueue_fo_release(int filt)
771 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
774 mtx_lock(&filterops_lock);
775 KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
776 ("filter object refcount not valid on release"));
777 sysfilt_ops[~filt].for_refcnt--;
778 mtx_unlock(&filterops_lock);
782 * A ref to kq (obtained via kqueue_acquire) must be held. waitok will
783 * influence if memory allocation should wait. Make sure it is 0 if you
787 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
789 struct filterops *fops;
791 struct knote *kn, *tkn;
792 int error, filt, event;
801 fops = kqueue_fo_find(filt);
805 tkn = knote_alloc(waitok); /* prevent waiting with locks */
809 KASSERT(td != NULL, ("td is NULL"));
810 error = fget(td, kev->ident, &fp);
814 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
815 kev->ident, 0) != 0) {
819 error = kqueue_expand(kq, fops, kev->ident, waitok);
825 if (fp->f_type == DTYPE_KQUEUE) {
827 * if we add some inteligence about what we are doing,
828 * we should be able to support events on ourselves.
829 * We need to know when we are doing this to prevent
830 * getting both the knlist lock and the kq lock since
831 * they are the same thing.
833 if (fp->f_data == kq) {
838 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
842 if (kev->ident < kq->kq_knlistsize) {
843 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
844 if (kev->filter == kn->kn_filter)
848 if ((kev->flags & EV_ADD) == EV_ADD)
849 kqueue_expand(kq, fops, kev->ident, waitok);
852 if (kq->kq_knhashmask != 0) {
855 list = &kq->kq_knhash[
856 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
857 SLIST_FOREACH(kn, list, kn_link)
858 if (kev->ident == kn->kn_id &&
859 kev->filter == kn->kn_filter)
864 /* knote is in the process of changing, wait for it to stablize. */
865 if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
870 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
871 kq->kq_state |= KQ_FLUXWAIT;
872 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
876 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
883 * kn now contains the matching knote, or NULL if no match
885 if (kev->flags & EV_ADD) {
898 * apply reference counts to knote structure, and
899 * do not release it at the end of this routine.
904 kn->kn_sfflags = kev->fflags;
905 kn->kn_sdata = kev->data;
908 kn->kn_kevent = *kev;
909 kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE |
910 EV_ENABLE | EV_DISABLE);
911 kn->kn_status = KN_INFLUX|KN_DETACHED;
913 error = knote_attach(kn, kq);
920 if ((error = kn->kn_fop->f_attach(kn)) != 0) {
927 * The user may change some filter values after the
928 * initial EV_ADD, but doing so will not reset any
929 * filter which has already been triggered.
931 kn->kn_status |= KN_INFLUX;
934 kn->kn_sfflags = kev->fflags;
935 kn->kn_sdata = kev->data;
936 kn->kn_kevent.udata = kev->udata;
940 * We can get here with kn->kn_knlist == NULL.
941 * This can happen when the initial attach event decides that
942 * the event is "completed" already. i.e. filt_procattach
943 * is called on a zombie process. It will call filt_proc
944 * which will remove it from the list, and NULL kn_knlist.
946 event = kn->kn_fop->f_event(kn, 0);
949 KNOTE_ACTIVATE(kn, 1);
950 kn->kn_status &= ~KN_INFLUX;
952 } else if (kev->flags & EV_DELETE) {
953 kn->kn_status |= KN_INFLUX;
955 if (!(kn->kn_status & KN_DETACHED))
956 kn->kn_fop->f_detach(kn);
961 if ((kev->flags & EV_DISABLE) &&
962 ((kn->kn_status & KN_DISABLED) == 0)) {
963 kn->kn_status |= KN_DISABLED;
966 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
967 kn->kn_status &= ~KN_DISABLED;
968 if ((kn->kn_status & KN_ACTIVE) &&
969 ((kn->kn_status & KN_QUEUED) == 0))
975 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
981 kqueue_fo_release(filt);
986 kqueue_acquire(struct file *fp, struct kqueue **kqp)
996 if (fp->f_type != DTYPE_KQUEUE || kq == NULL) {
1002 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
1016 kqueue_release(struct kqueue *kq, int locked)
1023 if (kq->kq_refcnt == 1)
1024 wakeup(&kq->kq_refcnt);
1030 kqueue_schedtask(struct kqueue *kq)
1034 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1035 ("scheduling kqueue task while draining"));
1037 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1038 taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task);
1039 kq->kq_state |= KQ_TASKSCHED;
1044 * Expand the kq to make sure we have storage for fops/ident pair.
1046 * Return 0 on success (or no work necessary), return errno on failure.
1048 * Not calling hashinit w/ waitok (proper malloc flag) should be safe.
1049 * If kqueue_register is called from a non-fd context, there usually/should
1053 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
1056 struct klist *list, *tmp_knhash;
1057 u_long tmp_knhashmask;
1060 int mflag = waitok ? M_WAITOK : M_NOWAIT;
1066 if (kq->kq_knlistsize <= fd) {
1067 size = kq->kq_knlistsize;
1070 MALLOC(list, struct klist *,
1071 size * sizeof list, M_KQUEUE, mflag);
1075 if (kq->kq_knlistsize > fd) {
1076 FREE(list, M_KQUEUE);
1079 if (kq->kq_knlist != NULL) {
1080 bcopy(kq->kq_knlist, list,
1081 kq->kq_knlistsize * sizeof list);
1082 FREE(kq->kq_knlist, M_KQUEUE);
1083 kq->kq_knlist = NULL;
1085 bzero((caddr_t)list +
1086 kq->kq_knlistsize * sizeof list,
1087 (size - kq->kq_knlistsize) * sizeof list);
1088 kq->kq_knlistsize = size;
1089 kq->kq_knlist = list;
1094 if (kq->kq_knhashmask == 0) {
1095 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1097 if (tmp_knhash == NULL)
1100 if (kq->kq_knhashmask == 0) {
1101 kq->kq_knhash = tmp_knhash;
1102 kq->kq_knhashmask = tmp_knhashmask;
1104 free(tmp_knhash, M_KQUEUE);
1115 kqueue_task(void *arg, int pending)
1123 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1126 KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1128 kq->kq_state &= ~KQ_TASKSCHED;
1129 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1130 wakeup(&kq->kq_state);
1133 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1137 * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1138 * We treat KN_MARKER knotes as if they are INFLUX.
1141 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1142 const struct timespec *tsp, struct kevent *keva, struct thread *td)
1144 struct kevent *kevp;
1145 struct timeval atv, rtv, ttv;
1146 struct knote *kn, *marker;
1147 int count, timeout, nkev, error;
1159 TIMESPEC_TO_TIMEVAL(&atv, tsp);
1160 if (itimerfix(&atv)) {
1164 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1167 timeout = atv.tv_sec > 24 * 60 * 60 ?
1168 24 * 60 * 60 * hz : tvtohz(&atv);
1169 getmicrouptime(&rtv);
1170 timevaladd(&atv, &rtv);
1176 marker = knote_alloc(1);
1177 if (marker == NULL) {
1181 marker->kn_status = KN_MARKER;
1186 if (atv.tv_sec || atv.tv_usec) {
1187 getmicrouptime(&rtv);
1188 if (timevalcmp(&rtv, &atv, >=))
1191 timevalsub(&ttv, &rtv);
1192 timeout = ttv.tv_sec > 24 * 60 * 60 ?
1193 24 * 60 * 60 * hz : tvtohz(&ttv);
1198 if (kq->kq_count == 0) {
1200 error = EWOULDBLOCK;
1202 kq->kq_state |= KQ_SLEEP;
1203 error = msleep(kq, &kq->kq_lock, PSOCK | PCATCH,
1208 /* don't restart after signals... */
1209 if (error == ERESTART)
1211 else if (error == EWOULDBLOCK)
1216 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1219 kn = TAILQ_FIRST(&kq->kq_head);
1221 if ((kn->kn_status == KN_MARKER && kn != marker) ||
1222 (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1223 kq->kq_state |= KQ_FLUXWAIT;
1224 error = msleep(kq, &kq->kq_lock, PSOCK,
1229 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1230 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1231 kn->kn_status &= ~KN_QUEUED;
1237 if (count == maxevents)
1241 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1242 ("KN_INFLUX set when not suppose to be"));
1244 if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
1245 kn->kn_status &= ~KN_QUEUED;
1246 kn->kn_status |= KN_INFLUX;
1250 * We don't need to lock the list since we've marked
1253 *kevp = kn->kn_kevent;
1254 if (!(kn->kn_status & KN_DETACHED))
1255 kn->kn_fop->f_detach(kn);
1260 kn->kn_status |= KN_INFLUX;
1262 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
1263 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1265 if (kn->kn_fop->f_event(kn, 0) == 0) {
1267 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1269 ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX);
1274 *kevp = kn->kn_kevent;
1276 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1277 if (kn->kn_flags & EV_CLEAR) {
1280 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1283 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1285 kn->kn_status &= ~(KN_INFLUX);
1289 /* we are returning a copy to the user */
1294 if (nkev == KQ_NEVENTS) {
1296 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1304 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1312 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1313 td->td_retval[0] = maxevents - count;
1319 * This could be expanded to call kqueue_scan, if desired.
1323 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1324 int flags, struct thread *td)
1331 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
1332 int flags, struct thread *td)
1339 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
1340 struct ucred *active_cred, struct thread *td)
1343 * Enabling sigio causes two major problems:
1344 * 1) infinite recursion:
1345 * Synopsys: kevent is being used to track signals and have FIOASYNC
1346 * set. On receipt of a signal this will cause a kqueue to recurse
1347 * into itself over and over. Sending the sigio causes the kqueue
1348 * to become ready, which in turn posts sigio again, forever.
1349 * Solution: this can be solved by setting a flag in the kqueue that
1350 * we have a SIGIO in progress.
1351 * 2) locking problems:
1352 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
1353 * us above the proc and pgrp locks.
1354 * Solution: Post a signal using an async mechanism, being sure to
1355 * record a generation count in the delivery so that we do not deliver
1356 * a signal to the wrong process.
1358 * Note, these two mechanisms are somewhat mutually exclusive!
1367 kq->kq_state |= KQ_ASYNC;
1369 kq->kq_state &= ~KQ_ASYNC;
1374 return (fsetown(*(int *)data, &kq->kq_sigio));
1377 *(int *)data = fgetown(&kq->kq_sigio);
1387 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
1394 if ((error = kqueue_acquire(fp, &kq)))
1398 if (events & (POLLIN | POLLRDNORM)) {
1400 revents |= events & (POLLIN | POLLRDNORM);
1402 selrecord(td, &kq->kq_sel);
1403 kq->kq_state |= KQ_SEL;
1406 kqueue_release(kq, 1);
1413 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1417 bzero((void *)st, sizeof *st);
1419 * We no longer return kq_count because the unlocked value is useless.
1420 * If you spent all this time getting the count, why not spend your
1421 * syscall better by calling kevent?
1423 * XXX - This is needed for libc_r.
1425 st->st_mode = S_IFIFO;
1431 kqueue_close(struct file *fp, struct thread *td)
1433 struct kqueue *kq = fp->f_data;
1434 struct filedesc *fdp;
1439 if ((error = kqueue_acquire(fp, &kq)))
1444 KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
1445 ("kqueue already closing"));
1446 kq->kq_state |= KQ_CLOSING;
1447 if (kq->kq_refcnt > 1)
1448 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
1450 KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
1453 KASSERT(knlist_empty(&kq->kq_sel.si_note),
1454 ("kqueue's knlist not empty"));
1456 for (i = 0; i < kq->kq_knlistsize; i++) {
1457 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
1458 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1459 ("KN_INFLUX set when not suppose to be"));
1460 kn->kn_status |= KN_INFLUX;
1462 if (!(kn->kn_status & KN_DETACHED))
1463 kn->kn_fop->f_detach(kn);
1468 if (kq->kq_knhashmask != 0) {
1469 for (i = 0; i <= kq->kq_knhashmask; i++) {
1470 while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
1471 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1472 ("KN_INFLUX set when not suppose to be"));
1473 kn->kn_status |= KN_INFLUX;
1475 if (!(kn->kn_status & KN_DETACHED))
1476 kn->kn_fop->f_detach(kn);
1483 if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
1484 kq->kq_state |= KQ_TASKDRAIN;
1485 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
1488 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1489 kq->kq_state &= ~KQ_SEL;
1490 selwakeuppri(&kq->kq_sel, PSOCK);
1495 FILEDESC_XLOCK(fdp);
1496 SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list);
1497 FILEDESC_XUNLOCK(fdp);
1499 knlist_destroy(&kq->kq_sel.si_note);
1500 mtx_destroy(&kq->kq_lock);
1503 if (kq->kq_knhash != NULL)
1504 free(kq->kq_knhash, M_KQUEUE);
1505 if (kq->kq_knlist != NULL)
1506 free(kq->kq_knlist, M_KQUEUE);
1508 funsetown(&kq->kq_sigio);
1516 kqueue_wakeup(struct kqueue *kq)
1520 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
1521 kq->kq_state &= ~KQ_SLEEP;
1524 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1525 kq->kq_state &= ~KQ_SEL;
1526 selwakeuppri(&kq->kq_sel, PSOCK);
1528 if (!knlist_empty(&kq->kq_sel.si_note))
1529 kqueue_schedtask(kq);
1530 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
1531 pgsigio(&kq->kq_sigio, SIGIO, 0);
1536 * Walk down a list of knotes, activating them if their event has triggered.
1538 * There is a possibility to optimize in the case of one kq watching another.
1539 * Instead of scheduling a task to wake it up, you could pass enough state
1540 * down the chain to make up the parent kqueue. Make this code functional
1544 knote(struct knlist *list, long hint, int islocked)
1552 KNL_ASSERT_LOCK(list, islocked);
1555 list->kl_lock(list->kl_lockarg);
1558 * If we unlock the list lock (and set KN_INFLUX), we can eliminate
1559 * the kqueue scheduling, but this will introduce four
1560 * lock/unlock's for each knote to test. If we do, continue to use
1561 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is
1562 * only safe if you want to remove the current item, which we are
1565 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
1567 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1569 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1570 kn->kn_status |= KN_HASKQLOCK;
1571 if (kn->kn_fop->f_event(kn, hint))
1572 KNOTE_ACTIVATE(kn, 1);
1573 kn->kn_status &= ~KN_HASKQLOCK;
1580 list->kl_unlock(list->kl_lockarg);
1584 * add a knote to a knlist
1587 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
1589 KNL_ASSERT_LOCK(knl, islocked);
1590 KQ_NOTOWNED(kn->kn_kq);
1591 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) ==
1592 (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED"));
1594 knl->kl_lock(knl->kl_lockarg);
1595 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
1597 knl->kl_unlock(knl->kl_lockarg);
1599 kn->kn_knlist = knl;
1600 kn->kn_status &= ~KN_DETACHED;
1601 KQ_UNLOCK(kn->kn_kq);
1605 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked)
1607 KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked"));
1608 KNL_ASSERT_LOCK(knl, knlislocked);
1609 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
1611 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX,
1612 ("knlist_remove called w/o knote being KN_INFLUX or already removed"));
1614 knl->kl_lock(knl->kl_lockarg);
1615 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
1616 kn->kn_knlist = NULL;
1618 knl->kl_unlock(knl->kl_lockarg);
1621 kn->kn_status |= KN_DETACHED;
1623 KQ_UNLOCK(kn->kn_kq);
1627 * remove all knotes from a specified klist
1630 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
1633 knlist_remove_kq(knl, kn, islocked, 0);
1637 * remove knote from a specified klist while in f_event handler.
1640 knlist_remove_inevent(struct knlist *knl, struct knote *kn)
1643 knlist_remove_kq(knl, kn, 1,
1644 (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK);
1648 knlist_empty(struct knlist *knl)
1650 KNL_ASSERT_LOCKED(knl);
1651 return SLIST_EMPTY(&knl->kl_list);
1654 static struct mtx knlist_lock;
1655 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
1657 static void knlist_mtx_lock(void *arg);
1658 static void knlist_mtx_unlock(void *arg);
1659 static int knlist_mtx_locked(void *arg);
1662 knlist_mtx_lock(void *arg)
1664 mtx_lock((struct mtx *)arg);
1668 knlist_mtx_unlock(void *arg)
1670 mtx_unlock((struct mtx *)arg);
1674 knlist_mtx_locked(void *arg)
1676 return (mtx_owned((struct mtx *)arg));
1680 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
1681 void (*kl_unlock)(void *), int (*kl_locked)(void *))
1685 knl->kl_lockarg = &knlist_lock;
1687 knl->kl_lockarg = lock;
1689 if (kl_lock == NULL)
1690 knl->kl_lock = knlist_mtx_lock;
1692 knl->kl_lock = kl_lock;
1693 if (kl_unlock == NULL)
1694 knl->kl_unlock = knlist_mtx_unlock;
1696 knl->kl_unlock = kl_unlock;
1697 if (kl_locked == NULL)
1698 knl->kl_locked = knlist_mtx_locked;
1700 knl->kl_locked = kl_locked;
1702 SLIST_INIT(&knl->kl_list);
1706 knlist_destroy(struct knlist *knl)
1711 * if we run across this error, we need to find the offending
1712 * driver and have it call knlist_clear.
1714 if (!SLIST_EMPTY(&knl->kl_list))
1715 printf("WARNING: destroying knlist w/ knotes on it!\n");
1718 knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL;
1719 SLIST_INIT(&knl->kl_list);
1723 * Even if we are locked, we may need to drop the lock to allow any influx
1724 * knotes time to "settle".
1727 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
1729 struct knote *kn, *kn2;
1733 KNL_ASSERT_LOCKED(knl);
1735 KNL_ASSERT_UNLOCKED(knl);
1736 again: /* need to reacquire lock since we have dropped it */
1737 knl->kl_lock(knl->kl_lockarg);
1740 SLIST_FOREACH_SAFE(kn, &knl->kl_list, kn_selnext, kn2) {
1743 if ((kn->kn_status & KN_INFLUX)) {
1747 knlist_remove_kq(knl, kn, 1, 1);
1749 kn->kn_status |= KN_INFLUX | KN_DETACHED;
1753 /* Make sure cleared knotes disappear soon */
1754 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1760 if (!SLIST_EMPTY(&knl->kl_list)) {
1761 /* there are still KN_INFLUX remaining */
1762 kn = SLIST_FIRST(&knl->kl_list);
1765 KASSERT(kn->kn_status & KN_INFLUX,
1766 ("knote removed w/o list lock"));
1767 knl->kl_unlock(knl->kl_lockarg);
1768 kq->kq_state |= KQ_FLUXWAIT;
1769 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
1775 KNL_ASSERT_LOCKED(knl);
1777 knl->kl_unlock(knl->kl_lockarg);
1778 KNL_ASSERT_UNLOCKED(knl);
1783 * Remove all knotes referencing a specified fd must be called with FILEDESC
1784 * lock. This prevents a race where a new fd comes along and occupies the
1785 * entry and we attach a knote to the fd.
1788 knote_fdclose(struct thread *td, int fd)
1790 struct filedesc *fdp = td->td_proc->p_fd;
1795 FILEDESC_XLOCK_ASSERT(fdp);
1798 * We shouldn't have to worry about new kevents appearing on fd
1799 * since filedesc is locked.
1801 SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
1806 while (kq->kq_knlistsize > fd &&
1807 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
1808 if (kn->kn_status & KN_INFLUX) {
1809 /* someone else might be waiting on our knote */
1812 kq->kq_state |= KQ_FLUXWAIT;
1813 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
1816 kn->kn_status |= KN_INFLUX;
1818 if (!(kn->kn_status & KN_DETACHED))
1819 kn->kn_fop->f_detach(kn);
1829 knote_attach(struct knote *kn, struct kqueue *kq)
1833 KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX"));
1836 if (kn->kn_fop->f_isfd) {
1837 if (kn->kn_id >= kq->kq_knlistsize)
1839 list = &kq->kq_knlist[kn->kn_id];
1841 if (kq->kq_knhash == NULL)
1843 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1846 SLIST_INSERT_HEAD(list, kn, kn_link);
1852 * knote must already have been detached using the f_detach method.
1853 * no lock need to be held, it is assumed that the KN_INFLUX flag is set
1854 * to prevent other removal.
1857 knote_drop(struct knote *kn, struct thread *td)
1865 KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX,
1866 ("knote_drop called without KN_INFLUX set in kn_status"));
1869 if (kn->kn_fop->f_isfd)
1870 list = &kq->kq_knlist[kn->kn_id];
1872 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1874 if (!SLIST_EMPTY(list))
1875 SLIST_REMOVE(list, kn, knote, kn_link);
1876 if (kn->kn_status & KN_QUEUED)
1880 if (kn->kn_fop->f_isfd) {
1881 fdrop(kn->kn_fp, td);
1884 kqueue_fo_release(kn->kn_kevent.filter);
1890 knote_enqueue(struct knote *kn)
1892 struct kqueue *kq = kn->kn_kq;
1894 KQ_OWNED(kn->kn_kq);
1895 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
1897 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1898 kn->kn_status |= KN_QUEUED;
1904 knote_dequeue(struct knote *kn)
1906 struct kqueue *kq = kn->kn_kq;
1908 KQ_OWNED(kn->kn_kq);
1909 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
1911 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1912 kn->kn_status &= ~KN_QUEUED;
1920 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
1921 NULL, NULL, UMA_ALIGN_PTR, 0);
1923 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL)
1925 static struct knote *
1926 knote_alloc(int waitok)
1928 return ((struct knote *)uma_zalloc(knote_zone,
1929 (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO));
1933 knote_free(struct knote *kn)
1936 uma_zfree(knote_zone, kn);
1940 * Register the kev w/ the kq specified by fd.
1943 kqfd_register(int fd, struct kevent *kev, struct thread *td, int waitok)
1949 if ((error = fget(td, fd, &fp)) != 0)
1951 if ((error = kqueue_acquire(fp, &kq)) != 0)
1954 error = kqueue_register(kq, kev, td, waitok);
1956 kqueue_release(kq, 0);