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;
404 return (kn->kn_fflags != 0);
408 * Called when the process forked. It mostly does the same as the
409 * knote(), activating all knotes registered to be activated when the
410 * process forked. Additionally, for each knote attached to the
411 * parent, check whether user wants to track the new process. If so
412 * attach a new knote to it, and immediately report an event with the
416 knote_fork(struct knlist *list, int pid)
425 list->kl_lock(list->kl_lockarg);
427 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
428 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX)
432 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
438 * The same as knote(), activate the event.
440 if ((kn->kn_sfflags & NOTE_TRACK) == 0) {
441 kn->kn_status |= KN_HASKQLOCK;
442 if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
443 KNOTE_ACTIVATE(kn, 1);
444 kn->kn_status &= ~KN_HASKQLOCK;
450 * The NOTE_TRACK case. In addition to the activation
451 * of the event, we need to register new event to
452 * track the child. Drop the locks in preparation for
453 * the call to kqueue_register().
455 kn->kn_status |= KN_INFLUX;
457 list->kl_unlock(list->kl_lockarg);
460 * Activate existing knote and register a knote with
464 kev.filter = kn->kn_filter;
465 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
466 kev.fflags = kn->kn_sfflags;
467 kev.data = kn->kn_id; /* parent */
468 kev.udata = kn->kn_kevent.udata;/* preserve udata */
469 error = kqueue_register(kq, &kev, NULL, 0);
470 if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
471 KNOTE_ACTIVATE(kn, 0);
473 kn->kn_fflags |= NOTE_TRACKERR;
475 kn->kn_status &= ~KN_INFLUX;
477 list->kl_lock(list->kl_lockarg);
479 list->kl_unlock(list->kl_lockarg);
483 timertoticks(intptr_t data)
488 tv.tv_sec = data / 1000;
489 tv.tv_usec = (data % 1000) * 1000;
490 tticks = tvtohz(&tv);
495 /* XXX - move to kern_timeout.c? */
497 filt_timerexpire(void *knx)
499 struct knote *kn = knx;
500 struct callout *calloutp;
503 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */
505 if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) {
506 calloutp = (struct callout *)kn->kn_hook;
507 callout_reset(calloutp, timertoticks(kn->kn_sdata),
508 filt_timerexpire, kn);
513 * data contains amount of time to sleep, in milliseconds
515 /* XXX - move to kern_timeout.c? */
517 filt_timerattach(struct knote *kn)
519 struct callout *calloutp;
521 atomic_add_int(&kq_ncallouts, 1);
523 if (kq_ncallouts >= kq_calloutmax) {
524 atomic_add_int(&kq_ncallouts, -1);
528 kn->kn_flags |= EV_CLEAR; /* automatically set */
529 kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */
530 MALLOC(calloutp, struct callout *, sizeof(*calloutp),
532 callout_init(calloutp, CALLOUT_MPSAFE);
533 kn->kn_hook = calloutp;
534 callout_reset(calloutp, timertoticks(kn->kn_sdata), filt_timerexpire,
540 /* XXX - move to kern_timeout.c? */
542 filt_timerdetach(struct knote *kn)
544 struct callout *calloutp;
546 calloutp = (struct callout *)kn->kn_hook;
547 callout_drain(calloutp);
548 FREE(calloutp, M_KQUEUE);
549 atomic_add_int(&kq_ncallouts, -1);
550 kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */
553 /* XXX - move to kern_timeout.c? */
555 filt_timer(struct knote *kn, long hint)
558 return (kn->kn_data != 0);
562 kqueue(struct thread *td, struct kqueue_args *uap)
564 struct filedesc *fdp;
569 fdp = td->td_proc->p_fd;
570 error = falloc(td, &fp, &fd);
574 /* An extra reference on `nfp' has been held for us by falloc(). */
575 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
576 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK);
577 TAILQ_INIT(&kq->kq_head);
579 knlist_init(&kq->kq_sel.si_note, &kq->kq_lock, NULL, NULL, NULL);
580 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
583 SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
584 FILEDESC_XUNLOCK(fdp);
587 fp->f_flag = FREAD | FWRITE;
588 fp->f_type = DTYPE_KQUEUE;
590 fp->f_ops = &kqueueops;
594 td->td_retval[0] = fd;
599 #ifndef _SYS_SYSPROTO_H_
602 const struct kevent *changelist;
604 struct kevent *eventlist;
606 const struct timespec *timeout;
610 kevent(struct thread *td, struct kevent_args *uap)
612 struct timespec ts, *tsp;
613 struct kevent_copyops k_ops = { uap,
620 struct uio *ktruioin = NULL;
621 struct uio *ktruioout = NULL;
624 if (uap->timeout != NULL) {
625 error = copyin(uap->timeout, &ts, sizeof(ts));
633 if (KTRPOINT(td, KTR_GENIO)) {
634 ktriov.iov_base = uap->changelist;
635 ktriov.iov_len = uap->nchanges * sizeof(struct kevent);
636 ktruio = (struct uio){ .uio_iov = &ktriov, .uio_iovcnt = 1,
637 .uio_segflg = UIO_USERSPACE, .uio_rw = UIO_READ,
639 ktruioin = cloneuio(&ktruio);
640 ktriov.iov_base = uap->eventlist;
641 ktriov.iov_len = uap->nevents * sizeof(struct kevent);
642 ktruioout = cloneuio(&ktruio);
646 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
650 if (ktruioin != NULL) {
651 ktruioin->uio_resid = uap->nchanges * sizeof(struct kevent);
652 ktrgenio(uap->fd, UIO_WRITE, ktruioin, 0);
653 ktruioout->uio_resid = td->td_retval[0] * sizeof(struct kevent);
654 ktrgenio(uap->fd, UIO_READ, ktruioout, error);
662 * Copy 'count' items into the destination list pointed to by uap->eventlist.
665 kevent_copyout(void *arg, struct kevent *kevp, int count)
667 struct kevent_args *uap;
670 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
671 uap = (struct kevent_args *)arg;
673 error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
675 uap->eventlist += count;
680 * Copy 'count' items from the list pointed to by uap->changelist.
683 kevent_copyin(void *arg, struct kevent *kevp, int count)
685 struct kevent_args *uap;
688 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
689 uap = (struct kevent_args *)arg;
691 error = copyin(uap->changelist, kevp, count * sizeof *kevp);
693 uap->changelist += count;
698 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
699 struct kevent_copyops *k_ops, const struct timespec *timeout)
701 struct kevent keva[KQ_NEVENTS];
702 struct kevent *kevp, *changes;
705 int i, n, nerrors, error;
707 if ((error = fget(td, fd, &fp)) != 0)
709 if ((error = kqueue_acquire(fp, &kq)) != 0)
714 while (nchanges > 0) {
715 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
716 error = k_ops->k_copyin(k_ops->arg, keva, n);
720 for (i = 0; i < n; i++) {
724 kevp->flags &= ~EV_SYSFLAGS;
725 error = kqueue_register(kq, kevp, td, 1);
728 kevp->flags = EV_ERROR;
730 (void) k_ops->k_copyout(k_ops->arg,
742 td->td_retval[0] = nerrors;
747 error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td);
749 kqueue_release(kq, 0);
756 kqueue_add_filteropts(int filt, struct filterops *filtops)
760 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
762 "trying to add a filterop that is out of range: %d is beyond %d\n",
763 ~filt, EVFILT_SYSCOUNT);
766 mtx_lock(&filterops_lock);
767 if (sysfilt_ops[~filt].for_fop != &null_filtops &&
768 sysfilt_ops[~filt].for_fop != NULL)
771 sysfilt_ops[~filt].for_fop = filtops;
772 sysfilt_ops[~filt].for_refcnt = 0;
774 mtx_unlock(&filterops_lock);
780 kqueue_del_filteropts(int filt)
785 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
788 mtx_lock(&filterops_lock);
789 if (sysfilt_ops[~filt].for_fop == &null_filtops ||
790 sysfilt_ops[~filt].for_fop == NULL)
792 else if (sysfilt_ops[~filt].for_refcnt != 0)
795 sysfilt_ops[~filt].for_fop = &null_filtops;
796 sysfilt_ops[~filt].for_refcnt = 0;
798 mtx_unlock(&filterops_lock);
803 static struct filterops *
804 kqueue_fo_find(int filt)
807 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
810 mtx_lock(&filterops_lock);
811 sysfilt_ops[~filt].for_refcnt++;
812 if (sysfilt_ops[~filt].for_fop == NULL)
813 sysfilt_ops[~filt].for_fop = &null_filtops;
814 mtx_unlock(&filterops_lock);
816 return sysfilt_ops[~filt].for_fop;
820 kqueue_fo_release(int filt)
823 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
826 mtx_lock(&filterops_lock);
827 KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
828 ("filter object refcount not valid on release"));
829 sysfilt_ops[~filt].for_refcnt--;
830 mtx_unlock(&filterops_lock);
834 * A ref to kq (obtained via kqueue_acquire) must be held. waitok will
835 * influence if memory allocation should wait. Make sure it is 0 if you
839 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
841 struct filterops *fops;
843 struct knote *kn, *tkn;
844 int error, filt, event;
853 fops = kqueue_fo_find(filt);
857 tkn = knote_alloc(waitok); /* prevent waiting with locks */
861 KASSERT(td != NULL, ("td is NULL"));
862 error = fget(td, kev->ident, &fp);
866 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
867 kev->ident, 0) != 0) {
871 error = kqueue_expand(kq, fops, kev->ident, waitok);
877 if (fp->f_type == DTYPE_KQUEUE) {
879 * if we add some inteligence about what we are doing,
880 * we should be able to support events on ourselves.
881 * We need to know when we are doing this to prevent
882 * getting both the knlist lock and the kq lock since
883 * they are the same thing.
885 if (fp->f_data == kq) {
890 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
894 if (kev->ident < kq->kq_knlistsize) {
895 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
896 if (kev->filter == kn->kn_filter)
900 if ((kev->flags & EV_ADD) == EV_ADD)
901 kqueue_expand(kq, fops, kev->ident, waitok);
904 if (kq->kq_knhashmask != 0) {
907 list = &kq->kq_knhash[
908 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
909 SLIST_FOREACH(kn, list, kn_link)
910 if (kev->ident == kn->kn_id &&
911 kev->filter == kn->kn_filter)
916 /* knote is in the process of changing, wait for it to stablize. */
917 if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
922 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
923 kq->kq_state |= KQ_FLUXWAIT;
924 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
928 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
935 * kn now contains the matching knote, or NULL if no match
937 if (kev->flags & EV_ADD) {
950 * apply reference counts to knote structure, and
951 * do not release it at the end of this routine.
956 kn->kn_sfflags = kev->fflags;
957 kn->kn_sdata = kev->data;
960 kn->kn_kevent = *kev;
961 kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE |
962 EV_ENABLE | EV_DISABLE);
963 kn->kn_status = KN_INFLUX|KN_DETACHED;
965 error = knote_attach(kn, kq);
972 if ((error = kn->kn_fop->f_attach(kn)) != 0) {
979 * The user may change some filter values after the
980 * initial EV_ADD, but doing so will not reset any
981 * filter which has already been triggered.
983 kn->kn_status |= KN_INFLUX;
986 kn->kn_sfflags = kev->fflags;
987 kn->kn_sdata = kev->data;
988 kn->kn_kevent.udata = kev->udata;
992 * We can get here with kn->kn_knlist == NULL.
993 * This can happen when the initial attach event decides that
994 * the event is "completed" already. i.e. filt_procattach
995 * is called on a zombie process. It will call filt_proc
996 * which will remove it from the list, and NULL kn_knlist.
998 event = kn->kn_fop->f_event(kn, 0);
1001 KNOTE_ACTIVATE(kn, 1);
1002 kn->kn_status &= ~KN_INFLUX;
1004 } else if (kev->flags & EV_DELETE) {
1005 kn->kn_status |= KN_INFLUX;
1007 if (!(kn->kn_status & KN_DETACHED))
1008 kn->kn_fop->f_detach(kn);
1013 if ((kev->flags & EV_DISABLE) &&
1014 ((kn->kn_status & KN_DISABLED) == 0)) {
1015 kn->kn_status |= KN_DISABLED;
1018 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
1019 kn->kn_status &= ~KN_DISABLED;
1020 if ((kn->kn_status & KN_ACTIVE) &&
1021 ((kn->kn_status & KN_QUEUED) == 0))
1027 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1033 kqueue_fo_release(filt);
1038 kqueue_acquire(struct file *fp, struct kqueue **kqp)
1048 if (fp->f_type != DTYPE_KQUEUE || kq == NULL) {
1054 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
1068 kqueue_release(struct kqueue *kq, int locked)
1075 if (kq->kq_refcnt == 1)
1076 wakeup(&kq->kq_refcnt);
1082 kqueue_schedtask(struct kqueue *kq)
1086 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1087 ("scheduling kqueue task while draining"));
1089 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1090 taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task);
1091 kq->kq_state |= KQ_TASKSCHED;
1096 * Expand the kq to make sure we have storage for fops/ident pair.
1098 * Return 0 on success (or no work necessary), return errno on failure.
1100 * Not calling hashinit w/ waitok (proper malloc flag) should be safe.
1101 * If kqueue_register is called from a non-fd context, there usually/should
1105 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
1108 struct klist *list, *tmp_knhash;
1109 u_long tmp_knhashmask;
1112 int mflag = waitok ? M_WAITOK : M_NOWAIT;
1118 if (kq->kq_knlistsize <= fd) {
1119 size = kq->kq_knlistsize;
1122 MALLOC(list, struct klist *,
1123 size * sizeof list, M_KQUEUE, mflag);
1127 if (kq->kq_knlistsize > fd) {
1128 FREE(list, M_KQUEUE);
1131 if (kq->kq_knlist != NULL) {
1132 bcopy(kq->kq_knlist, list,
1133 kq->kq_knlistsize * sizeof list);
1134 FREE(kq->kq_knlist, M_KQUEUE);
1135 kq->kq_knlist = NULL;
1137 bzero((caddr_t)list +
1138 kq->kq_knlistsize * sizeof list,
1139 (size - kq->kq_knlistsize) * sizeof list);
1140 kq->kq_knlistsize = size;
1141 kq->kq_knlist = list;
1146 if (kq->kq_knhashmask == 0) {
1147 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1149 if (tmp_knhash == NULL)
1152 if (kq->kq_knhashmask == 0) {
1153 kq->kq_knhash = tmp_knhash;
1154 kq->kq_knhashmask = tmp_knhashmask;
1156 free(tmp_knhash, M_KQUEUE);
1167 kqueue_task(void *arg, int pending)
1175 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1178 KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1180 kq->kq_state &= ~KQ_TASKSCHED;
1181 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1182 wakeup(&kq->kq_state);
1185 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1189 * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1190 * We treat KN_MARKER knotes as if they are INFLUX.
1193 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1194 const struct timespec *tsp, struct kevent *keva, struct thread *td)
1196 struct kevent *kevp;
1197 struct timeval atv, rtv, ttv;
1198 struct knote *kn, *marker;
1199 int count, timeout, nkev, error, influx;
1211 TIMESPEC_TO_TIMEVAL(&atv, tsp);
1212 if (itimerfix(&atv)) {
1216 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1219 timeout = atv.tv_sec > 24 * 60 * 60 ?
1220 24 * 60 * 60 * hz : tvtohz(&atv);
1221 getmicrouptime(&rtv);
1222 timevaladd(&atv, &rtv);
1228 marker = knote_alloc(1);
1229 if (marker == NULL) {
1233 marker->kn_status = KN_MARKER;
1238 if (atv.tv_sec || atv.tv_usec) {
1239 getmicrouptime(&rtv);
1240 if (timevalcmp(&rtv, &atv, >=))
1243 timevalsub(&ttv, &rtv);
1244 timeout = ttv.tv_sec > 24 * 60 * 60 ?
1245 24 * 60 * 60 * hz : tvtohz(&ttv);
1250 if (kq->kq_count == 0) {
1252 error = EWOULDBLOCK;
1254 kq->kq_state |= KQ_SLEEP;
1255 error = msleep(kq, &kq->kq_lock, PSOCK | PCATCH,
1260 /* don't restart after signals... */
1261 if (error == ERESTART)
1263 else if (error == EWOULDBLOCK)
1268 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1272 kn = TAILQ_FIRST(&kq->kq_head);
1274 if ((kn->kn_status == KN_MARKER && kn != marker) ||
1275 (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1280 kq->kq_state |= KQ_FLUXWAIT;
1281 error = msleep(kq, &kq->kq_lock, PSOCK,
1286 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1287 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1288 kn->kn_status &= ~KN_QUEUED;
1294 if (count == maxevents)
1298 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1299 ("KN_INFLUX set when not suppose to be"));
1301 if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
1302 kn->kn_status &= ~KN_QUEUED;
1303 kn->kn_status |= KN_INFLUX;
1307 * We don't need to lock the list since we've marked
1310 *kevp = kn->kn_kevent;
1311 if (!(kn->kn_status & KN_DETACHED))
1312 kn->kn_fop->f_detach(kn);
1317 kn->kn_status |= KN_INFLUX;
1319 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
1320 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1322 if (kn->kn_fop->f_event(kn, 0) == 0) {
1324 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1326 ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX);
1332 *kevp = kn->kn_kevent;
1334 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1335 if (kn->kn_flags & EV_CLEAR) {
1338 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1341 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1343 kn->kn_status &= ~(KN_INFLUX);
1348 /* we are returning a copy to the user */
1353 if (nkev == KQ_NEVENTS) {
1356 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1364 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1372 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1373 td->td_retval[0] = maxevents - count;
1379 * This could be expanded to call kqueue_scan, if desired.
1383 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1384 int flags, struct thread *td)
1391 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
1392 int flags, struct thread *td)
1399 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
1400 struct ucred *active_cred, struct thread *td)
1403 * Enabling sigio causes two major problems:
1404 * 1) infinite recursion:
1405 * Synopsys: kevent is being used to track signals and have FIOASYNC
1406 * set. On receipt of a signal this will cause a kqueue to recurse
1407 * into itself over and over. Sending the sigio causes the kqueue
1408 * to become ready, which in turn posts sigio again, forever.
1409 * Solution: this can be solved by setting a flag in the kqueue that
1410 * we have a SIGIO in progress.
1411 * 2) locking problems:
1412 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
1413 * us above the proc and pgrp locks.
1414 * Solution: Post a signal using an async mechanism, being sure to
1415 * record a generation count in the delivery so that we do not deliver
1416 * a signal to the wrong process.
1418 * Note, these two mechanisms are somewhat mutually exclusive!
1427 kq->kq_state |= KQ_ASYNC;
1429 kq->kq_state &= ~KQ_ASYNC;
1434 return (fsetown(*(int *)data, &kq->kq_sigio));
1437 *(int *)data = fgetown(&kq->kq_sigio);
1447 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
1454 if ((error = kqueue_acquire(fp, &kq)))
1458 if (events & (POLLIN | POLLRDNORM)) {
1460 revents |= events & (POLLIN | POLLRDNORM);
1462 selrecord(td, &kq->kq_sel);
1463 kq->kq_state |= KQ_SEL;
1466 kqueue_release(kq, 1);
1473 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1477 bzero((void *)st, sizeof *st);
1479 * We no longer return kq_count because the unlocked value is useless.
1480 * If you spent all this time getting the count, why not spend your
1481 * syscall better by calling kevent?
1483 * XXX - This is needed for libc_r.
1485 st->st_mode = S_IFIFO;
1491 kqueue_close(struct file *fp, struct thread *td)
1493 struct kqueue *kq = fp->f_data;
1494 struct filedesc *fdp;
1499 if ((error = kqueue_acquire(fp, &kq)))
1504 KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
1505 ("kqueue already closing"));
1506 kq->kq_state |= KQ_CLOSING;
1507 if (kq->kq_refcnt > 1)
1508 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
1510 KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
1513 KASSERT(knlist_empty(&kq->kq_sel.si_note),
1514 ("kqueue's knlist not empty"));
1516 for (i = 0; i < kq->kq_knlistsize; i++) {
1517 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
1518 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1519 kq->kq_state |= KQ_FLUXWAIT;
1520 msleep(kq, &kq->kq_lock, PSOCK, "kqclo1", 0);
1523 kn->kn_status |= KN_INFLUX;
1525 if (!(kn->kn_status & KN_DETACHED))
1526 kn->kn_fop->f_detach(kn);
1531 if (kq->kq_knhashmask != 0) {
1532 for (i = 0; i <= kq->kq_knhashmask; i++) {
1533 while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
1534 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1535 kq->kq_state |= KQ_FLUXWAIT;
1536 msleep(kq, &kq->kq_lock, PSOCK,
1540 kn->kn_status |= KN_INFLUX;
1542 if (!(kn->kn_status & KN_DETACHED))
1543 kn->kn_fop->f_detach(kn);
1550 if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
1551 kq->kq_state |= KQ_TASKDRAIN;
1552 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
1555 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1556 kq->kq_state &= ~KQ_SEL;
1557 selwakeuppri(&kq->kq_sel, PSOCK);
1562 FILEDESC_XLOCK(fdp);
1563 SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list);
1564 FILEDESC_XUNLOCK(fdp);
1566 knlist_destroy(&kq->kq_sel.si_note);
1567 mtx_destroy(&kq->kq_lock);
1570 if (kq->kq_knhash != NULL)
1571 free(kq->kq_knhash, M_KQUEUE);
1572 if (kq->kq_knlist != NULL)
1573 free(kq->kq_knlist, M_KQUEUE);
1575 funsetown(&kq->kq_sigio);
1583 kqueue_wakeup(struct kqueue *kq)
1587 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
1588 kq->kq_state &= ~KQ_SLEEP;
1591 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1592 kq->kq_state &= ~KQ_SEL;
1593 selwakeuppri(&kq->kq_sel, PSOCK);
1595 if (!knlist_empty(&kq->kq_sel.si_note))
1596 kqueue_schedtask(kq);
1597 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
1598 pgsigio(&kq->kq_sigio, SIGIO, 0);
1603 * Walk down a list of knotes, activating them if their event has triggered.
1605 * There is a possibility to optimize in the case of one kq watching another.
1606 * Instead of scheduling a task to wake it up, you could pass enough state
1607 * down the chain to make up the parent kqueue. Make this code functional
1611 knote(struct knlist *list, long hint, int islocked)
1619 KNL_ASSERT_LOCK(list, islocked);
1622 list->kl_lock(list->kl_lockarg);
1625 * If we unlock the list lock (and set KN_INFLUX), we can eliminate
1626 * the kqueue scheduling, but this will introduce four
1627 * lock/unlock's for each knote to test. If we do, continue to use
1628 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is
1629 * only safe if you want to remove the current item, which we are
1632 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
1634 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1636 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1637 kn->kn_status |= KN_HASKQLOCK;
1638 if (kn->kn_fop->f_event(kn, hint))
1639 KNOTE_ACTIVATE(kn, 1);
1640 kn->kn_status &= ~KN_HASKQLOCK;
1647 list->kl_unlock(list->kl_lockarg);
1651 * add a knote to a knlist
1654 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
1656 KNL_ASSERT_LOCK(knl, islocked);
1657 KQ_NOTOWNED(kn->kn_kq);
1658 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) ==
1659 (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED"));
1661 knl->kl_lock(knl->kl_lockarg);
1662 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
1664 knl->kl_unlock(knl->kl_lockarg);
1666 kn->kn_knlist = knl;
1667 kn->kn_status &= ~KN_DETACHED;
1668 KQ_UNLOCK(kn->kn_kq);
1672 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked)
1674 KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked"));
1675 KNL_ASSERT_LOCK(knl, knlislocked);
1676 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
1678 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX,
1679 ("knlist_remove called w/o knote being KN_INFLUX or already removed"));
1681 knl->kl_lock(knl->kl_lockarg);
1682 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
1683 kn->kn_knlist = NULL;
1685 knl->kl_unlock(knl->kl_lockarg);
1688 kn->kn_status |= KN_DETACHED;
1690 KQ_UNLOCK(kn->kn_kq);
1694 * remove all knotes from a specified klist
1697 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
1700 knlist_remove_kq(knl, kn, islocked, 0);
1704 * remove knote from a specified klist while in f_event handler.
1707 knlist_remove_inevent(struct knlist *knl, struct knote *kn)
1710 knlist_remove_kq(knl, kn, 1,
1711 (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK);
1715 knlist_empty(struct knlist *knl)
1717 KNL_ASSERT_LOCKED(knl);
1718 return SLIST_EMPTY(&knl->kl_list);
1721 static struct mtx knlist_lock;
1722 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
1724 static void knlist_mtx_lock(void *arg);
1725 static void knlist_mtx_unlock(void *arg);
1726 static int knlist_mtx_locked(void *arg);
1729 knlist_mtx_lock(void *arg)
1731 mtx_lock((struct mtx *)arg);
1735 knlist_mtx_unlock(void *arg)
1737 mtx_unlock((struct mtx *)arg);
1741 knlist_mtx_locked(void *arg)
1743 return (mtx_owned((struct mtx *)arg));
1747 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
1748 void (*kl_unlock)(void *), int (*kl_locked)(void *))
1752 knl->kl_lockarg = &knlist_lock;
1754 knl->kl_lockarg = lock;
1756 if (kl_lock == NULL)
1757 knl->kl_lock = knlist_mtx_lock;
1759 knl->kl_lock = kl_lock;
1760 if (kl_unlock == NULL)
1761 knl->kl_unlock = knlist_mtx_unlock;
1763 knl->kl_unlock = kl_unlock;
1764 if (kl_locked == NULL)
1765 knl->kl_locked = knlist_mtx_locked;
1767 knl->kl_locked = kl_locked;
1769 SLIST_INIT(&knl->kl_list);
1773 knlist_destroy(struct knlist *knl)
1778 * if we run across this error, we need to find the offending
1779 * driver and have it call knlist_clear.
1781 if (!SLIST_EMPTY(&knl->kl_list))
1782 printf("WARNING: destroying knlist w/ knotes on it!\n");
1785 knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL;
1786 SLIST_INIT(&knl->kl_list);
1790 * Even if we are locked, we may need to drop the lock to allow any influx
1791 * knotes time to "settle".
1794 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
1796 struct knote *kn, *kn2;
1800 KNL_ASSERT_LOCKED(knl);
1802 KNL_ASSERT_UNLOCKED(knl);
1803 again: /* need to reacquire lock since we have dropped it */
1804 knl->kl_lock(knl->kl_lockarg);
1807 SLIST_FOREACH_SAFE(kn, &knl->kl_list, kn_selnext, kn2) {
1810 if ((kn->kn_status & KN_INFLUX)) {
1814 knlist_remove_kq(knl, kn, 1, 1);
1816 kn->kn_status |= KN_INFLUX | KN_DETACHED;
1820 /* Make sure cleared knotes disappear soon */
1821 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1827 if (!SLIST_EMPTY(&knl->kl_list)) {
1828 /* there are still KN_INFLUX remaining */
1829 kn = SLIST_FIRST(&knl->kl_list);
1832 KASSERT(kn->kn_status & KN_INFLUX,
1833 ("knote removed w/o list lock"));
1834 knl->kl_unlock(knl->kl_lockarg);
1835 kq->kq_state |= KQ_FLUXWAIT;
1836 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
1842 KNL_ASSERT_LOCKED(knl);
1844 knl->kl_unlock(knl->kl_lockarg);
1845 KNL_ASSERT_UNLOCKED(knl);
1850 * Remove all knotes referencing a specified fd must be called with FILEDESC
1851 * lock. This prevents a race where a new fd comes along and occupies the
1852 * entry and we attach a knote to the fd.
1855 knote_fdclose(struct thread *td, int fd)
1857 struct filedesc *fdp = td->td_proc->p_fd;
1862 FILEDESC_XLOCK_ASSERT(fdp);
1865 * We shouldn't have to worry about new kevents appearing on fd
1866 * since filedesc is locked.
1868 SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
1873 while (kq->kq_knlistsize > fd &&
1874 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
1875 if (kn->kn_status & KN_INFLUX) {
1876 /* someone else might be waiting on our knote */
1879 kq->kq_state |= KQ_FLUXWAIT;
1880 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
1883 kn->kn_status |= KN_INFLUX;
1885 if (!(kn->kn_status & KN_DETACHED))
1886 kn->kn_fop->f_detach(kn);
1896 knote_attach(struct knote *kn, struct kqueue *kq)
1900 KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX"));
1903 if (kn->kn_fop->f_isfd) {
1904 if (kn->kn_id >= kq->kq_knlistsize)
1906 list = &kq->kq_knlist[kn->kn_id];
1908 if (kq->kq_knhash == NULL)
1910 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1913 SLIST_INSERT_HEAD(list, kn, kn_link);
1919 * knote must already have been detached using the f_detach method.
1920 * no lock need to be held, it is assumed that the KN_INFLUX flag is set
1921 * to prevent other removal.
1924 knote_drop(struct knote *kn, struct thread *td)
1932 KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX,
1933 ("knote_drop called without KN_INFLUX set in kn_status"));
1936 if (kn->kn_fop->f_isfd)
1937 list = &kq->kq_knlist[kn->kn_id];
1939 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1941 if (!SLIST_EMPTY(list))
1942 SLIST_REMOVE(list, kn, knote, kn_link);
1943 if (kn->kn_status & KN_QUEUED)
1947 if (kn->kn_fop->f_isfd) {
1948 fdrop(kn->kn_fp, td);
1951 kqueue_fo_release(kn->kn_kevent.filter);
1957 knote_enqueue(struct knote *kn)
1959 struct kqueue *kq = kn->kn_kq;
1961 KQ_OWNED(kn->kn_kq);
1962 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
1964 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1965 kn->kn_status |= KN_QUEUED;
1971 knote_dequeue(struct knote *kn)
1973 struct kqueue *kq = kn->kn_kq;
1975 KQ_OWNED(kn->kn_kq);
1976 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
1978 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1979 kn->kn_status &= ~KN_QUEUED;
1987 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
1988 NULL, NULL, UMA_ALIGN_PTR, 0);
1990 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL);
1992 static struct knote *
1993 knote_alloc(int waitok)
1995 return ((struct knote *)uma_zalloc(knote_zone,
1996 (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO));
2000 knote_free(struct knote *kn)
2003 uma_zfree(knote_zone, kn);
2007 * Register the kev w/ the kq specified by fd.
2010 kqfd_register(int fd, struct kevent *kev, struct thread *td, int waitok)
2016 if ((error = fget(td, fd, &fp)) != 0)
2018 if ((error = kqueue_acquire(fp, &kq)) != 0)
2021 error = kqueue_register(kq, kev, td, waitok);
2023 kqueue_release(kq, 0);