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_truncate_t kqueue_truncate;
109 static fo_ioctl_t kqueue_ioctl;
110 static fo_poll_t kqueue_poll;
111 static fo_kqfilter_t kqueue_kqfilter;
112 static fo_stat_t kqueue_stat;
113 static fo_close_t kqueue_close;
115 static struct fileops kqueueops = {
116 .fo_read = kqueue_read,
117 .fo_write = kqueue_write,
118 .fo_truncate = kqueue_truncate,
119 .fo_ioctl = kqueue_ioctl,
120 .fo_poll = kqueue_poll,
121 .fo_kqfilter = kqueue_kqfilter,
122 .fo_stat = kqueue_stat,
123 .fo_close = kqueue_close,
126 static int knote_attach(struct knote *kn, struct kqueue *kq);
127 static void knote_drop(struct knote *kn, struct thread *td);
128 static void knote_enqueue(struct knote *kn);
129 static void knote_dequeue(struct knote *kn);
130 static void knote_init(void);
131 static struct knote *knote_alloc(int waitok);
132 static void knote_free(struct knote *kn);
134 static void filt_kqdetach(struct knote *kn);
135 static int filt_kqueue(struct knote *kn, long hint);
136 static int filt_procattach(struct knote *kn);
137 static void filt_procdetach(struct knote *kn);
138 static int filt_proc(struct knote *kn, long hint);
139 static int filt_fileattach(struct knote *kn);
140 static void filt_timerexpire(void *knx);
141 static int filt_timerattach(struct knote *kn);
142 static void filt_timerdetach(struct knote *kn);
143 static int filt_timer(struct knote *kn, long hint);
145 static struct filterops file_filtops =
146 { 1, filt_fileattach, NULL, NULL };
147 static struct filterops kqread_filtops =
148 { 1, NULL, filt_kqdetach, filt_kqueue };
149 /* XXX - move to kern_proc.c? */
150 static struct filterops proc_filtops =
151 { 0, filt_procattach, filt_procdetach, filt_proc };
152 static struct filterops timer_filtops =
153 { 0, filt_timerattach, filt_timerdetach, filt_timer };
155 static uma_zone_t knote_zone;
156 static int kq_ncallouts = 0;
157 static int kq_calloutmax = (4 * 1024);
158 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
159 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
161 /* XXX - ensure not KN_INFLUX?? */
162 #define KNOTE_ACTIVATE(kn, islock) do { \
164 mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED); \
166 KQ_LOCK((kn)->kn_kq); \
167 (kn)->kn_status |= KN_ACTIVE; \
168 if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
169 knote_enqueue((kn)); \
171 KQ_UNLOCK((kn)->kn_kq); \
173 #define KQ_LOCK(kq) do { \
174 mtx_lock(&(kq)->kq_lock); \
176 #define KQ_FLUX_WAKEUP(kq) do { \
177 if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) { \
178 (kq)->kq_state &= ~KQ_FLUXWAIT; \
182 #define KQ_UNLOCK_FLUX(kq) do { \
183 KQ_FLUX_WAKEUP(kq); \
184 mtx_unlock(&(kq)->kq_lock); \
186 #define KQ_UNLOCK(kq) do { \
187 mtx_unlock(&(kq)->kq_lock); \
189 #define KQ_OWNED(kq) do { \
190 mtx_assert(&(kq)->kq_lock, MA_OWNED); \
192 #define KQ_NOTOWNED(kq) do { \
193 mtx_assert(&(kq)->kq_lock, MA_NOTOWNED); \
195 #define KN_LIST_LOCK(kn) do { \
196 if (kn->kn_knlist != NULL) \
197 kn->kn_knlist->kl_lock(kn->kn_knlist->kl_lockarg); \
199 #define KN_LIST_UNLOCK(kn) do { \
200 if (kn->kn_knlist != NULL) \
201 kn->kn_knlist->kl_unlock(kn->kn_knlist->kl_lockarg); \
203 #define KNL_ASSERT_LOCK(knl, islocked) do { \
205 KNL_ASSERT_LOCKED(knl); \
207 KNL_ASSERT_UNLOCKED(knl); \
210 #define KNL_ASSERT_LOCKED(knl) do { \
211 if (!knl->kl_locked((knl)->kl_lockarg)) \
212 panic("knlist not locked, but should be"); \
214 #define KNL_ASSERT_UNLOCKED(knl) do { \
215 if (knl->kl_locked((knl)->kl_lockarg)) \
216 panic("knlist locked, but should not be"); \
218 #else /* !INVARIANTS */
219 #define KNL_ASSERT_LOCKED(knl) do {} while(0)
220 #define KNL_ASSERT_UNLOCKED(knl) do {} while (0)
221 #endif /* INVARIANTS */
223 #define KN_HASHSIZE 64 /* XXX should be tunable */
224 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
227 filt_nullattach(struct knote *kn)
233 struct filterops null_filtops =
234 { 0, filt_nullattach, NULL, NULL };
236 /* XXX - make SYSINIT to add these, and move into respective modules. */
237 extern struct filterops sig_filtops;
238 extern struct filterops fs_filtops;
241 * Table for for all system-defined filters.
243 static struct mtx filterops_lock;
244 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops",
247 struct filterops *for_fop;
249 } sysfilt_ops[EVFILT_SYSCOUNT] = {
250 { &file_filtops }, /* EVFILT_READ */
251 { &file_filtops }, /* EVFILT_WRITE */
252 { &null_filtops }, /* EVFILT_AIO */
253 { &file_filtops }, /* EVFILT_VNODE */
254 { &proc_filtops }, /* EVFILT_PROC */
255 { &sig_filtops }, /* EVFILT_SIGNAL */
256 { &timer_filtops }, /* EVFILT_TIMER */
257 { &file_filtops }, /* EVFILT_NETDEV */
258 { &fs_filtops }, /* EVFILT_FS */
259 { &null_filtops }, /* EVFILT_LIO */
263 * Simple redirection for all cdevsw style objects to call their fo_kqfilter
267 filt_fileattach(struct knote *kn)
270 return (fo_kqfilter(kn->kn_fp, kn));
275 kqueue_kqfilter(struct file *fp, struct knote *kn)
277 struct kqueue *kq = kn->kn_fp->f_data;
279 if (kn->kn_filter != EVFILT_READ)
282 kn->kn_status |= KN_KQUEUE;
283 kn->kn_fop = &kqread_filtops;
284 knlist_add(&kq->kq_sel.si_note, kn, 0);
290 filt_kqdetach(struct knote *kn)
292 struct kqueue *kq = kn->kn_fp->f_data;
294 knlist_remove(&kq->kq_sel.si_note, kn, 0);
299 filt_kqueue(struct knote *kn, long hint)
301 struct kqueue *kq = kn->kn_fp->f_data;
303 kn->kn_data = kq->kq_count;
304 return (kn->kn_data > 0);
307 /* XXX - move to kern_proc.c? */
309 filt_procattach(struct knote *kn)
316 p = pfind(kn->kn_id);
317 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
318 p = zpfind(kn->kn_id);
320 } else if (p != NULL && (p->p_flag & P_WEXIT)) {
326 if ((error = p_cansee(curthread, p)))
329 kn->kn_ptr.p_proc = p;
330 kn->kn_flags |= EV_CLEAR; /* automatically set */
333 * internal flag indicating registration done by kernel
335 if (kn->kn_flags & EV_FLAG1) {
336 kn->kn_data = kn->kn_sdata; /* ppid */
337 kn->kn_fflags = NOTE_CHILD;
338 kn->kn_flags &= ~EV_FLAG1;
342 knlist_add(&p->p_klist, kn, 1);
345 * Immediately activate any exit notes if the target process is a
346 * zombie. This is necessary to handle the case where the target
347 * process, e.g. a child, dies before the kevent is registered.
349 if (immediate && filt_proc(kn, NOTE_EXIT))
350 KNOTE_ACTIVATE(kn, 0);
358 * The knote may be attached to a different process, which may exit,
359 * leaving nothing for the knote to be attached to. So when the process
360 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
361 * it will be deleted when read out. However, as part of the knote deletion,
362 * this routine is called, so a check is needed to avoid actually performing
363 * a detach, because the original process does not exist any more.
365 /* XXX - move to kern_proc.c? */
367 filt_procdetach(struct knote *kn)
371 p = kn->kn_ptr.p_proc;
372 knlist_remove(&p->p_klist, kn, 0);
373 kn->kn_ptr.p_proc = NULL;
376 /* XXX - move to kern_proc.c? */
378 filt_proc(struct knote *kn, long hint)
380 struct proc *p = kn->kn_ptr.p_proc;
384 * mask off extra data
386 event = (u_int)hint & NOTE_PCTRLMASK;
389 * if the user is interested in this event, record it.
391 if (kn->kn_sfflags & event)
392 kn->kn_fflags |= event;
395 * process is gone, so flag the event as finished.
397 if (event == NOTE_EXIT) {
398 if (!(kn->kn_status & KN_DETACHED))
399 knlist_remove_inevent(&p->p_klist, kn);
400 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
401 kn->kn_data = p->p_xstat;
402 kn->kn_ptr.p_proc = NULL;
407 * process forked, and user wants to track the new process,
408 * so attach a new knote to it, and immediately report an
409 * event with the parent's pid.
411 if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
416 * register knote with new process.
418 kev.ident = hint & NOTE_PDATAMASK; /* pid */
419 kev.filter = kn->kn_filter;
420 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
421 kev.fflags = kn->kn_sfflags;
422 kev.data = kn->kn_id; /* parent */
423 kev.udata = kn->kn_kevent.udata; /* preserve udata */
424 error = kqueue_register(kn->kn_kq, &kev, NULL, 0);
426 kn->kn_fflags |= NOTE_TRACKERR;
429 return (kn->kn_fflags != 0);
433 timertoticks(intptr_t data)
438 tv.tv_sec = data / 1000;
439 tv.tv_usec = (data % 1000) * 1000;
440 tticks = tvtohz(&tv);
445 /* XXX - move to kern_timeout.c? */
447 filt_timerexpire(void *knx)
449 struct knote *kn = knx;
450 struct callout *calloutp;
453 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */
455 if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) {
456 calloutp = (struct callout *)kn->kn_hook;
457 callout_reset_curcpu(calloutp, timertoticks(kn->kn_sdata),
458 filt_timerexpire, kn);
463 * data contains amount of time to sleep, in milliseconds
465 /* XXX - move to kern_timeout.c? */
467 filt_timerattach(struct knote *kn)
469 struct callout *calloutp;
471 atomic_add_int(&kq_ncallouts, 1);
473 if (kq_ncallouts >= kq_calloutmax) {
474 atomic_add_int(&kq_ncallouts, -1);
478 kn->kn_flags |= EV_CLEAR; /* automatically set */
479 kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */
480 MALLOC(calloutp, struct callout *, sizeof(*calloutp),
482 callout_init(calloutp, CALLOUT_MPSAFE);
483 kn->kn_hook = calloutp;
484 callout_reset_curcpu(calloutp, timertoticks(kn->kn_sdata),
485 filt_timerexpire, kn);
490 /* XXX - move to kern_timeout.c? */
492 filt_timerdetach(struct knote *kn)
494 struct callout *calloutp;
496 calloutp = (struct callout *)kn->kn_hook;
497 callout_drain(calloutp);
498 FREE(calloutp, M_KQUEUE);
499 atomic_add_int(&kq_ncallouts, -1);
500 kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */
503 /* XXX - move to kern_timeout.c? */
505 filt_timer(struct knote *kn, long hint)
508 return (kn->kn_data != 0);
512 kqueue(struct thread *td, struct kqueue_args *uap)
514 struct filedesc *fdp;
519 fdp = td->td_proc->p_fd;
520 error = falloc(td, &fp, &fd);
524 /* An extra reference on `nfp' has been held for us by falloc(). */
525 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
526 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK);
527 TAILQ_INIT(&kq->kq_head);
529 knlist_init(&kq->kq_sel.si_note, &kq->kq_lock, NULL, NULL, NULL);
530 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
533 SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
534 FILEDESC_XUNLOCK(fdp);
536 finit(fp, FREAD | FWRITE, DTYPE_KQUEUE, kq, &kqueueops);
539 td->td_retval[0] = fd;
544 #ifndef _SYS_SYSPROTO_H_
547 const struct kevent *changelist;
549 struct kevent *eventlist;
551 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,
565 struct uio *ktruioin = NULL;
566 struct uio *ktruioout = NULL;
569 if (uap->timeout != NULL) {
570 error = copyin(uap->timeout, &ts, sizeof(ts));
578 if (KTRPOINT(td, KTR_GENIO)) {
579 ktriov.iov_base = uap->changelist;
580 ktriov.iov_len = uap->nchanges * sizeof(struct kevent);
581 ktruio = (struct uio){ .uio_iov = &ktriov, .uio_iovcnt = 1,
582 .uio_segflg = UIO_USERSPACE, .uio_rw = UIO_READ,
584 ktruioin = cloneuio(&ktruio);
585 ktriov.iov_base = uap->eventlist;
586 ktriov.iov_len = uap->nevents * sizeof(struct kevent);
587 ktruioout = cloneuio(&ktruio);
591 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
595 if (ktruioin != NULL) {
596 ktruioin->uio_resid = uap->nchanges * sizeof(struct kevent);
597 ktrgenio(uap->fd, UIO_WRITE, ktruioin, 0);
598 ktruioout->uio_resid = td->td_retval[0] * sizeof(struct kevent);
599 ktrgenio(uap->fd, UIO_READ, ktruioout, error);
607 * Copy 'count' items into the destination list pointed to by uap->eventlist.
610 kevent_copyout(void *arg, struct kevent *kevp, int count)
612 struct kevent_args *uap;
615 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
616 uap = (struct kevent_args *)arg;
618 error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
620 uap->eventlist += count;
625 * Copy 'count' items from the list pointed to by uap->changelist.
628 kevent_copyin(void *arg, struct kevent *kevp, int count)
630 struct kevent_args *uap;
633 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
634 uap = (struct kevent_args *)arg;
636 error = copyin(uap->changelist, kevp, count * sizeof *kevp);
638 uap->changelist += count;
643 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
644 struct kevent_copyops *k_ops, const struct timespec *timeout)
646 struct kevent keva[KQ_NEVENTS];
647 struct kevent *kevp, *changes;
650 int i, n, nerrors, error;
652 if ((error = fget(td, fd, &fp)) != 0)
654 if ((error = kqueue_acquire(fp, &kq)) != 0)
659 while (nchanges > 0) {
660 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
661 error = k_ops->k_copyin(k_ops->arg, keva, n);
665 for (i = 0; i < n; i++) {
669 kevp->flags &= ~EV_SYSFLAGS;
670 error = kqueue_register(kq, kevp, td, 1);
673 kevp->flags = EV_ERROR;
675 (void) k_ops->k_copyout(k_ops->arg,
687 td->td_retval[0] = nerrors;
692 error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td);
694 kqueue_release(kq, 0);
701 kqueue_add_filteropts(int filt, struct filterops *filtops)
705 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
707 "trying to add a filterop that is out of range: %d is beyond %d\n",
708 ~filt, EVFILT_SYSCOUNT);
711 mtx_lock(&filterops_lock);
712 if (sysfilt_ops[~filt].for_fop != &null_filtops &&
713 sysfilt_ops[~filt].for_fop != NULL)
716 sysfilt_ops[~filt].for_fop = filtops;
717 sysfilt_ops[~filt].for_refcnt = 0;
719 mtx_unlock(&filterops_lock);
725 kqueue_del_filteropts(int filt)
730 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
733 mtx_lock(&filterops_lock);
734 if (sysfilt_ops[~filt].for_fop == &null_filtops ||
735 sysfilt_ops[~filt].for_fop == NULL)
737 else if (sysfilt_ops[~filt].for_refcnt != 0)
740 sysfilt_ops[~filt].for_fop = &null_filtops;
741 sysfilt_ops[~filt].for_refcnt = 0;
743 mtx_unlock(&filterops_lock);
748 static struct filterops *
749 kqueue_fo_find(int filt)
752 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
755 mtx_lock(&filterops_lock);
756 sysfilt_ops[~filt].for_refcnt++;
757 if (sysfilt_ops[~filt].for_fop == NULL)
758 sysfilt_ops[~filt].for_fop = &null_filtops;
759 mtx_unlock(&filterops_lock);
761 return sysfilt_ops[~filt].for_fop;
765 kqueue_fo_release(int filt)
768 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
771 mtx_lock(&filterops_lock);
772 KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
773 ("filter object refcount not valid on release"));
774 sysfilt_ops[~filt].for_refcnt--;
775 mtx_unlock(&filterops_lock);
779 * A ref to kq (obtained via kqueue_acquire) must be held. waitok will
780 * influence if memory allocation should wait. Make sure it is 0 if you
784 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
786 struct filterops *fops;
788 struct knote *kn, *tkn;
789 int error, filt, event;
798 fops = kqueue_fo_find(filt);
802 tkn = knote_alloc(waitok); /* prevent waiting with locks */
806 KASSERT(td != NULL, ("td is NULL"));
807 error = fget(td, kev->ident, &fp);
811 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
812 kev->ident, 0) != 0) {
816 error = kqueue_expand(kq, fops, kev->ident, waitok);
822 if (fp->f_type == DTYPE_KQUEUE) {
824 * if we add some inteligence about what we are doing,
825 * we should be able to support events on ourselves.
826 * We need to know when we are doing this to prevent
827 * getting both the knlist lock and the kq lock since
828 * they are the same thing.
830 if (fp->f_data == kq) {
835 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
839 if (kev->ident < kq->kq_knlistsize) {
840 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
841 if (kev->filter == kn->kn_filter)
845 if ((kev->flags & EV_ADD) == EV_ADD)
846 kqueue_expand(kq, fops, kev->ident, waitok);
849 if (kq->kq_knhashmask != 0) {
852 list = &kq->kq_knhash[
853 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
854 SLIST_FOREACH(kn, list, kn_link)
855 if (kev->ident == kn->kn_id &&
856 kev->filter == kn->kn_filter)
861 /* knote is in the process of changing, wait for it to stablize. */
862 if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
867 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
868 kq->kq_state |= KQ_FLUXWAIT;
869 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
873 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
880 * kn now contains the matching knote, or NULL if no match
882 if (kev->flags & EV_ADD) {
895 * apply reference counts to knote structure, and
896 * do not release it at the end of this routine.
901 kn->kn_sfflags = kev->fflags;
902 kn->kn_sdata = kev->data;
905 kn->kn_kevent = *kev;
906 kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE |
907 EV_ENABLE | EV_DISABLE);
908 kn->kn_status = KN_INFLUX|KN_DETACHED;
910 error = knote_attach(kn, kq);
917 if ((error = kn->kn_fop->f_attach(kn)) != 0) {
924 * The user may change some filter values after the
925 * initial EV_ADD, but doing so will not reset any
926 * filter which has already been triggered.
928 kn->kn_status |= KN_INFLUX;
931 kn->kn_sfflags = kev->fflags;
932 kn->kn_sdata = kev->data;
933 kn->kn_kevent.udata = kev->udata;
937 * We can get here with kn->kn_knlist == NULL.
938 * This can happen when the initial attach event decides that
939 * the event is "completed" already. i.e. filt_procattach
940 * is called on a zombie process. It will call filt_proc
941 * which will remove it from the list, and NULL kn_knlist.
943 event = kn->kn_fop->f_event(kn, 0);
946 KNOTE_ACTIVATE(kn, 1);
947 kn->kn_status &= ~KN_INFLUX;
949 } else if (kev->flags & EV_DELETE) {
950 kn->kn_status |= KN_INFLUX;
952 if (!(kn->kn_status & KN_DETACHED))
953 kn->kn_fop->f_detach(kn);
958 if ((kev->flags & EV_DISABLE) &&
959 ((kn->kn_status & KN_DISABLED) == 0)) {
960 kn->kn_status |= KN_DISABLED;
963 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
964 kn->kn_status &= ~KN_DISABLED;
965 if ((kn->kn_status & KN_ACTIVE) &&
966 ((kn->kn_status & KN_QUEUED) == 0))
972 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
978 kqueue_fo_release(filt);
983 kqueue_acquire(struct file *fp, struct kqueue **kqp)
991 if (fp->f_type != DTYPE_KQUEUE || kq == NULL)
995 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
1006 kqueue_release(struct kqueue *kq, int locked)
1013 if (kq->kq_refcnt == 1)
1014 wakeup(&kq->kq_refcnt);
1020 kqueue_schedtask(struct kqueue *kq)
1024 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1025 ("scheduling kqueue task while draining"));
1027 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1028 taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task);
1029 kq->kq_state |= KQ_TASKSCHED;
1034 * Expand the kq to make sure we have storage for fops/ident pair.
1036 * Return 0 on success (or no work necessary), return errno on failure.
1038 * Not calling hashinit w/ waitok (proper malloc flag) should be safe.
1039 * If kqueue_register is called from a non-fd context, there usually/should
1043 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
1046 struct klist *list, *tmp_knhash;
1047 u_long tmp_knhashmask;
1050 int mflag = waitok ? M_WAITOK : M_NOWAIT;
1056 if (kq->kq_knlistsize <= fd) {
1057 size = kq->kq_knlistsize;
1060 MALLOC(list, struct klist *,
1061 size * sizeof list, M_KQUEUE, mflag);
1065 if (kq->kq_knlistsize > fd) {
1066 FREE(list, M_KQUEUE);
1069 if (kq->kq_knlist != NULL) {
1070 bcopy(kq->kq_knlist, list,
1071 kq->kq_knlistsize * sizeof list);
1072 FREE(kq->kq_knlist, M_KQUEUE);
1073 kq->kq_knlist = NULL;
1075 bzero((caddr_t)list +
1076 kq->kq_knlistsize * sizeof list,
1077 (size - kq->kq_knlistsize) * sizeof list);
1078 kq->kq_knlistsize = size;
1079 kq->kq_knlist = list;
1084 if (kq->kq_knhashmask == 0) {
1085 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1087 if (tmp_knhash == NULL)
1090 if (kq->kq_knhashmask == 0) {
1091 kq->kq_knhash = tmp_knhash;
1092 kq->kq_knhashmask = tmp_knhashmask;
1094 free(tmp_knhash, M_KQUEUE);
1105 kqueue_task(void *arg, int pending)
1113 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1116 KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1118 kq->kq_state &= ~KQ_TASKSCHED;
1119 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1120 wakeup(&kq->kq_state);
1123 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1127 * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1128 * We treat KN_MARKER knotes as if they are INFLUX.
1131 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1132 const struct timespec *tsp, struct kevent *keva, struct thread *td)
1134 struct kevent *kevp;
1135 struct timeval atv, rtv, ttv;
1136 struct knote *kn, *marker;
1137 int count, timeout, nkev, error;
1149 TIMESPEC_TO_TIMEVAL(&atv, tsp);
1150 if (itimerfix(&atv)) {
1154 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1157 timeout = atv.tv_sec > 24 * 60 * 60 ?
1158 24 * 60 * 60 * hz : tvtohz(&atv);
1159 getmicrouptime(&rtv);
1160 timevaladd(&atv, &rtv);
1166 marker = knote_alloc(1);
1167 if (marker == NULL) {
1171 marker->kn_status = KN_MARKER;
1176 if (atv.tv_sec || atv.tv_usec) {
1177 getmicrouptime(&rtv);
1178 if (timevalcmp(&rtv, &atv, >=))
1181 timevalsub(&ttv, &rtv);
1182 timeout = ttv.tv_sec > 24 * 60 * 60 ?
1183 24 * 60 * 60 * hz : tvtohz(&ttv);
1188 if (kq->kq_count == 0) {
1190 error = EWOULDBLOCK;
1192 kq->kq_state |= KQ_SLEEP;
1193 error = msleep(kq, &kq->kq_lock, PSOCK | PCATCH,
1198 /* don't restart after signals... */
1199 if (error == ERESTART)
1201 else if (error == EWOULDBLOCK)
1206 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1209 kn = TAILQ_FIRST(&kq->kq_head);
1211 if ((kn->kn_status == KN_MARKER && kn != marker) ||
1212 (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1213 kq->kq_state |= KQ_FLUXWAIT;
1214 error = msleep(kq, &kq->kq_lock, PSOCK,
1219 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1220 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1221 kn->kn_status &= ~KN_QUEUED;
1227 if (count == maxevents)
1231 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1232 ("KN_INFLUX set when not suppose to be"));
1234 if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
1235 kn->kn_status &= ~KN_QUEUED;
1236 kn->kn_status |= KN_INFLUX;
1240 * We don't need to lock the list since we've marked
1243 *kevp = kn->kn_kevent;
1244 if (!(kn->kn_status & KN_DETACHED))
1245 kn->kn_fop->f_detach(kn);
1250 kn->kn_status |= KN_INFLUX;
1252 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
1253 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1255 if (kn->kn_fop->f_event(kn, 0) == 0) {
1257 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1259 ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX);
1264 *kevp = kn->kn_kevent;
1266 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1267 if (kn->kn_flags & EV_CLEAR) {
1270 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1273 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1275 kn->kn_status &= ~(KN_INFLUX);
1279 /* we are returning a copy to the user */
1284 if (nkev == KQ_NEVENTS) {
1286 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1294 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1302 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1303 td->td_retval[0] = maxevents - count;
1309 * This could be expanded to call kqueue_scan, if desired.
1313 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1314 int flags, struct thread *td)
1321 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
1322 int flags, struct thread *td)
1329 kqueue_truncate(struct file *fp, off_t length, struct ucred *active_cred,
1338 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
1339 struct ucred *active_cred, struct thread *td)
1342 * Enabling sigio causes two major problems:
1343 * 1) infinite recursion:
1344 * Synopsys: kevent is being used to track signals and have FIOASYNC
1345 * set. On receipt of a signal this will cause a kqueue to recurse
1346 * into itself over and over. Sending the sigio causes the kqueue
1347 * to become ready, which in turn posts sigio again, forever.
1348 * Solution: this can be solved by setting a flag in the kqueue that
1349 * we have a SIGIO in progress.
1350 * 2) locking problems:
1351 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
1352 * us above the proc and pgrp locks.
1353 * Solution: Post a signal using an async mechanism, being sure to
1354 * record a generation count in the delivery so that we do not deliver
1355 * a signal to the wrong process.
1357 * Note, these two mechanisms are somewhat mutually exclusive!
1366 kq->kq_state |= KQ_ASYNC;
1368 kq->kq_state &= ~KQ_ASYNC;
1373 return (fsetown(*(int *)data, &kq->kq_sigio));
1376 *(int *)data = fgetown(&kq->kq_sigio);
1386 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
1393 if ((error = kqueue_acquire(fp, &kq)))
1397 if (events & (POLLIN | POLLRDNORM)) {
1399 revents |= events & (POLLIN | POLLRDNORM);
1401 selrecord(td, &kq->kq_sel);
1402 if (SEL_WAITING(&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 selwakeuppri(&kq->kq_sel, PSOCK);
1490 if (!SEL_WAITING(&kq->kq_sel))
1491 kq->kq_state &= ~KQ_SEL;
1496 FILEDESC_XLOCK(fdp);
1497 SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list);
1498 FILEDESC_XUNLOCK(fdp);
1500 knlist_destroy(&kq->kq_sel.si_note);
1501 mtx_destroy(&kq->kq_lock);
1504 if (kq->kq_knhash != NULL)
1505 free(kq->kq_knhash, M_KQUEUE);
1506 if (kq->kq_knlist != NULL)
1507 free(kq->kq_knlist, M_KQUEUE);
1509 funsetown(&kq->kq_sigio);
1517 kqueue_wakeup(struct kqueue *kq)
1521 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
1522 kq->kq_state &= ~KQ_SLEEP;
1525 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1526 selwakeuppri(&kq->kq_sel, PSOCK);
1527 if (!SEL_WAITING(&kq->kq_sel))
1528 kq->kq_state &= ~KQ_SEL;
1530 if (!knlist_empty(&kq->kq_sel.si_note))
1531 kqueue_schedtask(kq);
1532 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
1533 pgsigio(&kq->kq_sigio, SIGIO, 0);
1538 * Walk down a list of knotes, activating them if their event has triggered.
1540 * There is a possibility to optimize in the case of one kq watching another.
1541 * Instead of scheduling a task to wake it up, you could pass enough state
1542 * down the chain to make up the parent kqueue. Make this code functional
1546 knote(struct knlist *list, long hint, int islocked)
1554 KNL_ASSERT_LOCK(list, islocked);
1557 list->kl_lock(list->kl_lockarg);
1560 * If we unlock the list lock (and set KN_INFLUX), we can eliminate
1561 * the kqueue scheduling, but this will introduce four
1562 * lock/unlock's for each knote to test. If we do, continue to use
1563 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is
1564 * only safe if you want to remove the current item, which we are
1567 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
1569 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1571 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1572 kn->kn_status |= KN_HASKQLOCK;
1573 if (kn->kn_fop->f_event(kn, hint))
1574 KNOTE_ACTIVATE(kn, 1);
1575 kn->kn_status &= ~KN_HASKQLOCK;
1582 list->kl_unlock(list->kl_lockarg);
1586 * add a knote to a knlist
1589 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
1591 KNL_ASSERT_LOCK(knl, islocked);
1592 KQ_NOTOWNED(kn->kn_kq);
1593 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) ==
1594 (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED"));
1596 knl->kl_lock(knl->kl_lockarg);
1597 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
1599 knl->kl_unlock(knl->kl_lockarg);
1601 kn->kn_knlist = knl;
1602 kn->kn_status &= ~KN_DETACHED;
1603 KQ_UNLOCK(kn->kn_kq);
1607 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked)
1609 KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked"));
1610 KNL_ASSERT_LOCK(knl, knlislocked);
1611 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
1613 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX,
1614 ("knlist_remove called w/o knote being KN_INFLUX or already removed"));
1616 knl->kl_lock(knl->kl_lockarg);
1617 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
1618 kn->kn_knlist = NULL;
1620 knl->kl_unlock(knl->kl_lockarg);
1623 kn->kn_status |= KN_DETACHED;
1625 KQ_UNLOCK(kn->kn_kq);
1629 * remove all knotes from a specified klist
1632 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
1635 knlist_remove_kq(knl, kn, islocked, 0);
1639 * remove knote from a specified klist while in f_event handler.
1642 knlist_remove_inevent(struct knlist *knl, struct knote *kn)
1645 knlist_remove_kq(knl, kn, 1,
1646 (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK);
1650 knlist_empty(struct knlist *knl)
1652 KNL_ASSERT_LOCKED(knl);
1653 return SLIST_EMPTY(&knl->kl_list);
1656 static struct mtx knlist_lock;
1657 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
1659 static void knlist_mtx_lock(void *arg);
1660 static void knlist_mtx_unlock(void *arg);
1661 static int knlist_mtx_locked(void *arg);
1664 knlist_mtx_lock(void *arg)
1666 mtx_lock((struct mtx *)arg);
1670 knlist_mtx_unlock(void *arg)
1672 mtx_unlock((struct mtx *)arg);
1676 knlist_mtx_locked(void *arg)
1678 return (mtx_owned((struct mtx *)arg));
1682 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
1683 void (*kl_unlock)(void *), int (*kl_locked)(void *))
1687 knl->kl_lockarg = &knlist_lock;
1689 knl->kl_lockarg = lock;
1691 if (kl_lock == NULL)
1692 knl->kl_lock = knlist_mtx_lock;
1694 knl->kl_lock = kl_lock;
1695 if (kl_unlock == NULL)
1696 knl->kl_unlock = knlist_mtx_unlock;
1698 knl->kl_unlock = kl_unlock;
1699 if (kl_locked == NULL)
1700 knl->kl_locked = knlist_mtx_locked;
1702 knl->kl_locked = kl_locked;
1704 SLIST_INIT(&knl->kl_list);
1708 knlist_destroy(struct knlist *knl)
1713 * if we run across this error, we need to find the offending
1714 * driver and have it call knlist_clear.
1716 if (!SLIST_EMPTY(&knl->kl_list))
1717 printf("WARNING: destroying knlist w/ knotes on it!\n");
1720 knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL;
1721 SLIST_INIT(&knl->kl_list);
1725 * Even if we are locked, we may need to drop the lock to allow any influx
1726 * knotes time to "settle".
1729 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
1731 struct knote *kn, *kn2;
1735 KNL_ASSERT_LOCKED(knl);
1737 KNL_ASSERT_UNLOCKED(knl);
1738 again: /* need to reacquire lock since we have dropped it */
1739 knl->kl_lock(knl->kl_lockarg);
1742 SLIST_FOREACH_SAFE(kn, &knl->kl_list, kn_selnext, kn2) {
1745 if ((kn->kn_status & KN_INFLUX)) {
1749 knlist_remove_kq(knl, kn, 1, 1);
1751 kn->kn_status |= KN_INFLUX | KN_DETACHED;
1755 /* Make sure cleared knotes disappear soon */
1756 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1762 if (!SLIST_EMPTY(&knl->kl_list)) {
1763 /* there are still KN_INFLUX remaining */
1764 kn = SLIST_FIRST(&knl->kl_list);
1767 KASSERT(kn->kn_status & KN_INFLUX,
1768 ("knote removed w/o list lock"));
1769 knl->kl_unlock(knl->kl_lockarg);
1770 kq->kq_state |= KQ_FLUXWAIT;
1771 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
1777 KNL_ASSERT_LOCKED(knl);
1779 knl->kl_unlock(knl->kl_lockarg);
1780 KNL_ASSERT_UNLOCKED(knl);
1785 * Remove all knotes referencing a specified fd must be called with FILEDESC
1786 * lock. This prevents a race where a new fd comes along and occupies the
1787 * entry and we attach a knote to the fd.
1790 knote_fdclose(struct thread *td, int fd)
1792 struct filedesc *fdp = td->td_proc->p_fd;
1797 FILEDESC_XLOCK_ASSERT(fdp);
1800 * We shouldn't have to worry about new kevents appearing on fd
1801 * since filedesc is locked.
1803 SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
1808 while (kq->kq_knlistsize > fd &&
1809 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
1810 if (kn->kn_status & KN_INFLUX) {
1811 /* someone else might be waiting on our knote */
1814 kq->kq_state |= KQ_FLUXWAIT;
1815 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
1818 kn->kn_status |= KN_INFLUX;
1820 if (!(kn->kn_status & KN_DETACHED))
1821 kn->kn_fop->f_detach(kn);
1831 knote_attach(struct knote *kn, struct kqueue *kq)
1835 KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX"));
1838 if (kn->kn_fop->f_isfd) {
1839 if (kn->kn_id >= kq->kq_knlistsize)
1841 list = &kq->kq_knlist[kn->kn_id];
1843 if (kq->kq_knhash == NULL)
1845 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1848 SLIST_INSERT_HEAD(list, kn, kn_link);
1854 * knote must already have been detached using the f_detach method.
1855 * no lock need to be held, it is assumed that the KN_INFLUX flag is set
1856 * to prevent other removal.
1859 knote_drop(struct knote *kn, struct thread *td)
1867 KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX,
1868 ("knote_drop called without KN_INFLUX set in kn_status"));
1871 if (kn->kn_fop->f_isfd)
1872 list = &kq->kq_knlist[kn->kn_id];
1874 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1876 if (!SLIST_EMPTY(list))
1877 SLIST_REMOVE(list, kn, knote, kn_link);
1878 if (kn->kn_status & KN_QUEUED)
1882 if (kn->kn_fop->f_isfd) {
1883 fdrop(kn->kn_fp, td);
1886 kqueue_fo_release(kn->kn_kevent.filter);
1892 knote_enqueue(struct knote *kn)
1894 struct kqueue *kq = kn->kn_kq;
1896 KQ_OWNED(kn->kn_kq);
1897 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
1899 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1900 kn->kn_status |= KN_QUEUED;
1906 knote_dequeue(struct knote *kn)
1908 struct kqueue *kq = kn->kn_kq;
1910 KQ_OWNED(kn->kn_kq);
1911 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
1913 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1914 kn->kn_status &= ~KN_QUEUED;
1922 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
1923 NULL, NULL, UMA_ALIGN_PTR, 0);
1925 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL);
1927 static struct knote *
1928 knote_alloc(int waitok)
1930 return ((struct knote *)uma_zalloc(knote_zone,
1931 (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO));
1935 knote_free(struct knote *kn)
1938 uma_zfree(knote_zone, kn);
1942 * Register the kev w/ the kq specified by fd.
1945 kqfd_register(int fd, struct kevent *kev, struct thread *td, int waitok)
1951 if ((error = fget(td, fd, &fp)) != 0)
1953 if ((error = kqueue_acquire(fp, &kq)) != 0)
1956 error = kqueue_register(kq, kev, td, waitok);
1958 kqueue_release(kq, 0);