2 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
3 * Copyright 2004 John-Mark Gurney <jmg@FreeBSD.org>
4 * Copyright (c) 2009 Apple, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_ktrace.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/capability.h>
37 #include <sys/kernel.h>
39 #include <sys/mutex.h>
40 #include <sys/rwlock.h>
42 #include <sys/malloc.h>
43 #include <sys/unistd.h>
45 #include <sys/filedesc.h>
46 #include <sys/filio.h>
47 #include <sys/fcntl.h>
48 #include <sys/kthread.h>
49 #include <sys/selinfo.h>
50 #include <sys/queue.h>
51 #include <sys/event.h>
52 #include <sys/eventvar.h>
54 #include <sys/protosw.h>
55 #include <sys/sigio.h>
56 #include <sys/signalvar.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysproto.h>
62 #include <sys/syscallsubr.h>
63 #include <sys/taskqueue.h>
66 #include <sys/ktrace.h>
71 static MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
74 * This lock is used if multiple kq locks are required. This possibly
75 * should be made into a per proc lock.
77 static struct mtx kq_global;
78 MTX_SYSINIT(kq_global, &kq_global, "kqueue order", MTX_DEF);
79 #define KQ_GLOBAL_LOCK(lck, haslck) do { \
84 #define KQ_GLOBAL_UNLOCK(lck, haslck) do { \
90 TASKQUEUE_DEFINE_THREAD(kqueue);
92 static int kevent_copyout(void *arg, struct kevent *kevp, int count);
93 static int kevent_copyin(void *arg, struct kevent *kevp, int count);
94 static int kqueue_register(struct kqueue *kq, struct kevent *kev,
95 struct thread *td, int waitok);
96 static int kqueue_acquire(struct file *fp, struct kqueue **kqp);
97 static void kqueue_release(struct kqueue *kq, int locked);
98 static int kqueue_expand(struct kqueue *kq, struct filterops *fops,
99 uintptr_t ident, int waitok);
100 static void kqueue_task(void *arg, int pending);
101 static int kqueue_scan(struct kqueue *kq, int maxevents,
102 struct kevent_copyops *k_ops,
103 const struct timespec *timeout,
104 struct kevent *keva, struct thread *td);
105 static void kqueue_wakeup(struct kqueue *kq);
106 static struct filterops *kqueue_fo_find(int filt);
107 static void kqueue_fo_release(int filt);
109 static fo_rdwr_t kqueue_read;
110 static fo_rdwr_t kqueue_write;
111 static fo_truncate_t kqueue_truncate;
112 static fo_ioctl_t kqueue_ioctl;
113 static fo_poll_t kqueue_poll;
114 static fo_kqfilter_t kqueue_kqfilter;
115 static fo_stat_t kqueue_stat;
116 static fo_close_t kqueue_close;
118 static struct fileops kqueueops = {
119 .fo_read = kqueue_read,
120 .fo_write = kqueue_write,
121 .fo_truncate = kqueue_truncate,
122 .fo_ioctl = kqueue_ioctl,
123 .fo_poll = kqueue_poll,
124 .fo_kqfilter = kqueue_kqfilter,
125 .fo_stat = kqueue_stat,
126 .fo_close = kqueue_close,
127 .fo_chmod = invfo_chmod,
128 .fo_chown = invfo_chown,
131 static int knote_attach(struct knote *kn, struct kqueue *kq);
132 static void knote_drop(struct knote *kn, struct thread *td);
133 static void knote_enqueue(struct knote *kn);
134 static void knote_dequeue(struct knote *kn);
135 static void knote_init(void);
136 static struct knote *knote_alloc(int waitok);
137 static void knote_free(struct knote *kn);
139 static void filt_kqdetach(struct knote *kn);
140 static int filt_kqueue(struct knote *kn, long hint);
141 static int filt_procattach(struct knote *kn);
142 static void filt_procdetach(struct knote *kn);
143 static int filt_proc(struct knote *kn, long hint);
144 static int filt_fileattach(struct knote *kn);
145 static void filt_timerexpire(void *knx);
146 static int filt_timerattach(struct knote *kn);
147 static void filt_timerdetach(struct knote *kn);
148 static int filt_timer(struct knote *kn, long hint);
149 static int filt_userattach(struct knote *kn);
150 static void filt_userdetach(struct knote *kn);
151 static int filt_user(struct knote *kn, long hint);
152 static void filt_usertouch(struct knote *kn, struct kevent *kev,
155 static struct filterops file_filtops = {
157 .f_attach = filt_fileattach,
159 static struct filterops kqread_filtops = {
161 .f_detach = filt_kqdetach,
162 .f_event = filt_kqueue,
164 /* XXX - move to kern_proc.c? */
165 static struct filterops proc_filtops = {
167 .f_attach = filt_procattach,
168 .f_detach = filt_procdetach,
169 .f_event = filt_proc,
171 static struct filterops timer_filtops = {
173 .f_attach = filt_timerattach,
174 .f_detach = filt_timerdetach,
175 .f_event = filt_timer,
177 static struct filterops user_filtops = {
178 .f_attach = filt_userattach,
179 .f_detach = filt_userdetach,
180 .f_event = filt_user,
181 .f_touch = filt_usertouch,
184 static uma_zone_t knote_zone;
185 static int kq_ncallouts = 0;
186 static int kq_calloutmax = (4 * 1024);
187 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
188 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
190 /* XXX - ensure not KN_INFLUX?? */
191 #define KNOTE_ACTIVATE(kn, islock) do { \
193 mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED); \
195 KQ_LOCK((kn)->kn_kq); \
196 (kn)->kn_status |= KN_ACTIVE; \
197 if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
198 knote_enqueue((kn)); \
200 KQ_UNLOCK((kn)->kn_kq); \
202 #define KQ_LOCK(kq) do { \
203 mtx_lock(&(kq)->kq_lock); \
205 #define KQ_FLUX_WAKEUP(kq) do { \
206 if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) { \
207 (kq)->kq_state &= ~KQ_FLUXWAIT; \
211 #define KQ_UNLOCK_FLUX(kq) do { \
212 KQ_FLUX_WAKEUP(kq); \
213 mtx_unlock(&(kq)->kq_lock); \
215 #define KQ_UNLOCK(kq) do { \
216 mtx_unlock(&(kq)->kq_lock); \
218 #define KQ_OWNED(kq) do { \
219 mtx_assert(&(kq)->kq_lock, MA_OWNED); \
221 #define KQ_NOTOWNED(kq) do { \
222 mtx_assert(&(kq)->kq_lock, MA_NOTOWNED); \
224 #define KN_LIST_LOCK(kn) do { \
225 if (kn->kn_knlist != NULL) \
226 kn->kn_knlist->kl_lock(kn->kn_knlist->kl_lockarg); \
228 #define KN_LIST_UNLOCK(kn) do { \
229 if (kn->kn_knlist != NULL) \
230 kn->kn_knlist->kl_unlock(kn->kn_knlist->kl_lockarg); \
232 #define KNL_ASSERT_LOCK(knl, islocked) do { \
234 KNL_ASSERT_LOCKED(knl); \
236 KNL_ASSERT_UNLOCKED(knl); \
239 #define KNL_ASSERT_LOCKED(knl) do { \
240 knl->kl_assert_locked((knl)->kl_lockarg); \
242 #define KNL_ASSERT_UNLOCKED(knl) do { \
243 knl->kl_assert_unlocked((knl)->kl_lockarg); \
245 #else /* !INVARIANTS */
246 #define KNL_ASSERT_LOCKED(knl) do {} while(0)
247 #define KNL_ASSERT_UNLOCKED(knl) do {} while (0)
248 #endif /* INVARIANTS */
250 #define KN_HASHSIZE 64 /* XXX should be tunable */
251 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
254 filt_nullattach(struct knote *kn)
260 struct filterops null_filtops = {
262 .f_attach = filt_nullattach,
265 /* XXX - make SYSINIT to add these, and move into respective modules. */
266 extern struct filterops sig_filtops;
267 extern struct filterops fs_filtops;
270 * Table for for all system-defined filters.
272 static struct mtx filterops_lock;
273 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops",
276 struct filterops *for_fop;
278 } sysfilt_ops[EVFILT_SYSCOUNT] = {
279 { &file_filtops }, /* EVFILT_READ */
280 { &file_filtops }, /* EVFILT_WRITE */
281 { &null_filtops }, /* EVFILT_AIO */
282 { &file_filtops }, /* EVFILT_VNODE */
283 { &proc_filtops }, /* EVFILT_PROC */
284 { &sig_filtops }, /* EVFILT_SIGNAL */
285 { &timer_filtops }, /* EVFILT_TIMER */
286 { &null_filtops }, /* former EVFILT_NETDEV */
287 { &fs_filtops }, /* EVFILT_FS */
288 { &null_filtops }, /* EVFILT_LIO */
289 { &user_filtops }, /* EVFILT_USER */
293 * Simple redirection for all cdevsw style objects to call their fo_kqfilter
297 filt_fileattach(struct knote *kn)
300 return (fo_kqfilter(kn->kn_fp, kn));
305 kqueue_kqfilter(struct file *fp, struct knote *kn)
307 struct kqueue *kq = kn->kn_fp->f_data;
309 if (kn->kn_filter != EVFILT_READ)
312 kn->kn_status |= KN_KQUEUE;
313 kn->kn_fop = &kqread_filtops;
314 knlist_add(&kq->kq_sel.si_note, kn, 0);
320 filt_kqdetach(struct knote *kn)
322 struct kqueue *kq = kn->kn_fp->f_data;
324 knlist_remove(&kq->kq_sel.si_note, kn, 0);
329 filt_kqueue(struct knote *kn, long hint)
331 struct kqueue *kq = kn->kn_fp->f_data;
333 kn->kn_data = kq->kq_count;
334 return (kn->kn_data > 0);
337 /* XXX - move to kern_proc.c? */
339 filt_procattach(struct knote *kn)
346 p = pfind(kn->kn_id);
347 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
348 p = zpfind(kn->kn_id);
350 } else if (p != NULL && (p->p_flag & P_WEXIT)) {
356 if ((error = p_cansee(curthread, p))) {
361 kn->kn_ptr.p_proc = p;
362 kn->kn_flags |= EV_CLEAR; /* automatically set */
365 * internal flag indicating registration done by kernel
367 if (kn->kn_flags & EV_FLAG1) {
368 kn->kn_data = kn->kn_sdata; /* ppid */
369 kn->kn_fflags = NOTE_CHILD;
370 kn->kn_flags &= ~EV_FLAG1;
374 knlist_add(&p->p_klist, kn, 1);
377 * Immediately activate any exit notes if the target process is a
378 * zombie. This is necessary to handle the case where the target
379 * process, e.g. a child, dies before the kevent is registered.
381 if (immediate && filt_proc(kn, NOTE_EXIT))
382 KNOTE_ACTIVATE(kn, 0);
390 * The knote may be attached to a different process, which may exit,
391 * leaving nothing for the knote to be attached to. So when the process
392 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
393 * it will be deleted when read out. However, as part of the knote deletion,
394 * this routine is called, so a check is needed to avoid actually performing
395 * a detach, because the original process does not exist any more.
397 /* XXX - move to kern_proc.c? */
399 filt_procdetach(struct knote *kn)
403 p = kn->kn_ptr.p_proc;
404 knlist_remove(&p->p_klist, kn, 0);
405 kn->kn_ptr.p_proc = NULL;
408 /* XXX - move to kern_proc.c? */
410 filt_proc(struct knote *kn, long hint)
412 struct proc *p = kn->kn_ptr.p_proc;
416 * mask off extra data
418 event = (u_int)hint & NOTE_PCTRLMASK;
421 * if the user is interested in this event, record it.
423 if (kn->kn_sfflags & event)
424 kn->kn_fflags |= event;
427 * process is gone, so flag the event as finished.
429 if (event == NOTE_EXIT) {
430 if (!(kn->kn_status & KN_DETACHED))
431 knlist_remove_inevent(&p->p_klist, kn);
432 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
433 kn->kn_data = p->p_xstat;
434 kn->kn_ptr.p_proc = NULL;
438 return (kn->kn_fflags != 0);
442 * Called when the process forked. It mostly does the same as the
443 * knote(), activating all knotes registered to be activated when the
444 * process forked. Additionally, for each knote attached to the
445 * parent, check whether user wants to track the new process. If so
446 * attach a new knote to it, and immediately report an event with the
450 knote_fork(struct knlist *list, int pid)
459 list->kl_lock(list->kl_lockarg);
461 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
462 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX)
466 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
472 * The same as knote(), activate the event.
474 if ((kn->kn_sfflags & NOTE_TRACK) == 0) {
475 kn->kn_status |= KN_HASKQLOCK;
476 if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
477 KNOTE_ACTIVATE(kn, 1);
478 kn->kn_status &= ~KN_HASKQLOCK;
484 * The NOTE_TRACK case. In addition to the activation
485 * of the event, we need to register new event to
486 * track the child. Drop the locks in preparation for
487 * the call to kqueue_register().
489 kn->kn_status |= KN_INFLUX;
491 list->kl_unlock(list->kl_lockarg);
494 * Activate existing knote and register a knote with
498 kev.filter = kn->kn_filter;
499 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
500 kev.fflags = kn->kn_sfflags;
501 kev.data = kn->kn_id; /* parent */
502 kev.udata = kn->kn_kevent.udata;/* preserve udata */
503 error = kqueue_register(kq, &kev, NULL, 0);
504 if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
505 KNOTE_ACTIVATE(kn, 0);
507 kn->kn_fflags |= NOTE_TRACKERR;
509 kn->kn_status &= ~KN_INFLUX;
511 list->kl_lock(list->kl_lockarg);
513 list->kl_unlock(list->kl_lockarg);
517 * XXX: EVFILT_TIMER should perhaps live in kern_time.c beside the
518 * interval timer support code.
520 static __inline sbintime_t
521 timer2sbintime(intptr_t data)
524 return (SBT_1MS * data);
528 filt_timerexpire(void *knx)
530 struct callout *calloutp;
535 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */
537 if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) {
538 calloutp = (struct callout *)kn->kn_hook;
539 callout_reset_sbt_on(calloutp,
540 timer2sbintime(kn->kn_sdata), 0 /* 1ms? */,
541 filt_timerexpire, kn, PCPU_GET(cpuid), 0);
546 * data contains amount of time to sleep, in milliseconds
549 filt_timerattach(struct knote *kn)
551 struct callout *calloutp;
553 atomic_add_int(&kq_ncallouts, 1);
555 if (kq_ncallouts >= kq_calloutmax) {
556 atomic_add_int(&kq_ncallouts, -1);
560 kn->kn_flags |= EV_CLEAR; /* automatically set */
561 kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */
562 calloutp = malloc(sizeof(*calloutp), M_KQUEUE, M_WAITOK);
563 callout_init(calloutp, CALLOUT_MPSAFE);
564 kn->kn_hook = calloutp;
565 callout_reset_sbt_on(calloutp,
566 timer2sbintime(kn->kn_sdata), 0 /* 1ms? */,
567 filt_timerexpire, kn, PCPU_GET(cpuid), 0);
573 filt_timerdetach(struct knote *kn)
575 struct callout *calloutp;
577 calloutp = (struct callout *)kn->kn_hook;
578 callout_drain(calloutp);
579 free(calloutp, M_KQUEUE);
580 atomic_add_int(&kq_ncallouts, -1);
581 kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */
585 filt_timer(struct knote *kn, long hint)
588 return (kn->kn_data != 0);
592 filt_userattach(struct knote *kn)
596 * EVFILT_USER knotes are not attached to anything in the kernel.
599 if (kn->kn_fflags & NOTE_TRIGGER)
607 filt_userdetach(__unused struct knote *kn)
611 * EVFILT_USER knotes are not attached to anything in the kernel.
616 filt_user(struct knote *kn, __unused long hint)
619 return (kn->kn_hookid);
623 filt_usertouch(struct knote *kn, struct kevent *kev, u_long type)
629 if (kev->fflags & NOTE_TRIGGER)
632 ffctrl = kev->fflags & NOTE_FFCTRLMASK;
633 kev->fflags &= NOTE_FFLAGSMASK;
639 kn->kn_sfflags &= kev->fflags;
643 kn->kn_sfflags |= kev->fflags;
647 kn->kn_sfflags = kev->fflags;
651 /* XXX Return error? */
654 kn->kn_sdata = kev->data;
655 if (kev->flags & EV_CLEAR) {
663 *kev = kn->kn_kevent;
664 kev->fflags = kn->kn_sfflags;
665 kev->data = kn->kn_sdata;
666 if (kn->kn_flags & EV_CLEAR) {
674 panic("filt_usertouch() - invalid type (%ld)", type);
680 sys_kqueue(struct thread *td, struct kqueue_args *uap)
682 struct filedesc *fdp;
687 fdp = td->td_proc->p_fd;
688 error = falloc(td, &fp, &fd, 0);
692 /* An extra reference on `fp' has been held for us by falloc(). */
693 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
694 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK);
695 TAILQ_INIT(&kq->kq_head);
697 knlist_init_mtx(&kq->kq_sel.si_note, &kq->kq_lock);
698 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
701 SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
702 FILEDESC_XUNLOCK(fdp);
704 finit(fp, FREAD | FWRITE, DTYPE_KQUEUE, kq, &kqueueops);
707 td->td_retval[0] = fd;
712 #ifndef _SYS_SYSPROTO_H_
715 const struct kevent *changelist;
717 struct kevent *eventlist;
719 const struct timespec *timeout;
723 sys_kevent(struct thread *td, struct kevent_args *uap)
725 struct timespec ts, *tsp;
726 struct kevent_copyops k_ops = { uap,
733 struct uio *ktruioin = NULL;
734 struct uio *ktruioout = NULL;
737 if (uap->timeout != NULL) {
738 error = copyin(uap->timeout, &ts, sizeof(ts));
746 if (KTRPOINT(td, KTR_GENIO)) {
747 ktriov.iov_base = uap->changelist;
748 ktriov.iov_len = uap->nchanges * sizeof(struct kevent);
749 ktruio = (struct uio){ .uio_iov = &ktriov, .uio_iovcnt = 1,
750 .uio_segflg = UIO_USERSPACE, .uio_rw = UIO_READ,
752 ktruioin = cloneuio(&ktruio);
753 ktriov.iov_base = uap->eventlist;
754 ktriov.iov_len = uap->nevents * sizeof(struct kevent);
755 ktruioout = cloneuio(&ktruio);
759 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
763 if (ktruioin != NULL) {
764 ktruioin->uio_resid = uap->nchanges * sizeof(struct kevent);
765 ktrgenio(uap->fd, UIO_WRITE, ktruioin, 0);
766 ktruioout->uio_resid = td->td_retval[0] * sizeof(struct kevent);
767 ktrgenio(uap->fd, UIO_READ, ktruioout, error);
775 * Copy 'count' items into the destination list pointed to by uap->eventlist.
778 kevent_copyout(void *arg, struct kevent *kevp, int count)
780 struct kevent_args *uap;
783 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
784 uap = (struct kevent_args *)arg;
786 error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
788 uap->eventlist += count;
793 * Copy 'count' items from the list pointed to by uap->changelist.
796 kevent_copyin(void *arg, struct kevent *kevp, int count)
798 struct kevent_args *uap;
801 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
802 uap = (struct kevent_args *)arg;
804 error = copyin(uap->changelist, kevp, count * sizeof *kevp);
806 uap->changelist += count;
811 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
812 struct kevent_copyops *k_ops, const struct timespec *timeout)
814 struct kevent keva[KQ_NEVENTS];
815 struct kevent *kevp, *changes;
818 int i, n, nerrors, error;
820 if ((error = fget(td, fd, CAP_POST_EVENT, &fp)) != 0)
822 if ((error = kqueue_acquire(fp, &kq)) != 0)
827 while (nchanges > 0) {
828 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
829 error = k_ops->k_copyin(k_ops->arg, keva, n);
833 for (i = 0; i < n; i++) {
837 kevp->flags &= ~EV_SYSFLAGS;
838 error = kqueue_register(kq, kevp, td, 1);
839 if (error || (kevp->flags & EV_RECEIPT)) {
841 kevp->flags = EV_ERROR;
843 (void) k_ops->k_copyout(k_ops->arg,
855 td->td_retval[0] = nerrors;
860 error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td);
862 kqueue_release(kq, 0);
869 kqueue_add_filteropts(int filt, struct filterops *filtops)
874 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
876 "trying to add a filterop that is out of range: %d is beyond %d\n",
877 ~filt, EVFILT_SYSCOUNT);
880 mtx_lock(&filterops_lock);
881 if (sysfilt_ops[~filt].for_fop != &null_filtops &&
882 sysfilt_ops[~filt].for_fop != NULL)
885 sysfilt_ops[~filt].for_fop = filtops;
886 sysfilt_ops[~filt].for_refcnt = 0;
888 mtx_unlock(&filterops_lock);
894 kqueue_del_filteropts(int filt)
899 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
902 mtx_lock(&filterops_lock);
903 if (sysfilt_ops[~filt].for_fop == &null_filtops ||
904 sysfilt_ops[~filt].for_fop == NULL)
906 else if (sysfilt_ops[~filt].for_refcnt != 0)
909 sysfilt_ops[~filt].for_fop = &null_filtops;
910 sysfilt_ops[~filt].for_refcnt = 0;
912 mtx_unlock(&filterops_lock);
917 static struct filterops *
918 kqueue_fo_find(int filt)
921 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
924 mtx_lock(&filterops_lock);
925 sysfilt_ops[~filt].for_refcnt++;
926 if (sysfilt_ops[~filt].for_fop == NULL)
927 sysfilt_ops[~filt].for_fop = &null_filtops;
928 mtx_unlock(&filterops_lock);
930 return sysfilt_ops[~filt].for_fop;
934 kqueue_fo_release(int filt)
937 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
940 mtx_lock(&filterops_lock);
941 KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
942 ("filter object refcount not valid on release"));
943 sysfilt_ops[~filt].for_refcnt--;
944 mtx_unlock(&filterops_lock);
948 * A ref to kq (obtained via kqueue_acquire) must be held. waitok will
949 * influence if memory allocation should wait. Make sure it is 0 if you
953 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
955 struct filterops *fops;
957 struct knote *kn, *tkn;
958 int error, filt, event;
967 fops = kqueue_fo_find(filt);
971 tkn = knote_alloc(waitok); /* prevent waiting with locks */
975 KASSERT(td != NULL, ("td is NULL"));
976 error = fget(td, kev->ident, CAP_POLL_EVENT, &fp);
980 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
981 kev->ident, 0) != 0) {
985 error = kqueue_expand(kq, fops, kev->ident, waitok);
991 if (fp->f_type == DTYPE_KQUEUE) {
993 * if we add some inteligence about what we are doing,
994 * we should be able to support events on ourselves.
995 * We need to know when we are doing this to prevent
996 * getting both the knlist lock and the kq lock since
997 * they are the same thing.
999 if (fp->f_data == kq) {
1004 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1008 if (kev->ident < kq->kq_knlistsize) {
1009 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
1010 if (kev->filter == kn->kn_filter)
1014 if ((kev->flags & EV_ADD) == EV_ADD)
1015 kqueue_expand(kq, fops, kev->ident, waitok);
1018 if (kq->kq_knhashmask != 0) {
1021 list = &kq->kq_knhash[
1022 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
1023 SLIST_FOREACH(kn, list, kn_link)
1024 if (kev->ident == kn->kn_id &&
1025 kev->filter == kn->kn_filter)
1030 /* knote is in the process of changing, wait for it to stablize. */
1031 if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1032 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1033 kq->kq_state |= KQ_FLUXWAIT;
1034 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
1043 * kn now contains the matching knote, or NULL if no match
1046 if (kev->flags & EV_ADD) {
1058 * apply reference counts to knote structure, and
1059 * do not release it at the end of this routine.
1064 kn->kn_sfflags = kev->fflags;
1065 kn->kn_sdata = kev->data;
1068 kn->kn_kevent = *kev;
1069 kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE |
1070 EV_ENABLE | EV_DISABLE);
1071 kn->kn_status = KN_INFLUX|KN_DETACHED;
1073 error = knote_attach(kn, kq);
1080 if ((error = kn->kn_fop->f_attach(kn)) != 0) {
1087 /* No matching knote and the EV_ADD flag is not set. */
1094 if (kev->flags & EV_DELETE) {
1095 kn->kn_status |= KN_INFLUX;
1097 if (!(kn->kn_status & KN_DETACHED))
1098 kn->kn_fop->f_detach(kn);
1104 * The user may change some filter values after the initial EV_ADD,
1105 * but doing so will not reset any filter which has already been
1108 kn->kn_status |= KN_INFLUX;
1111 kn->kn_kevent.udata = kev->udata;
1112 if (!fops->f_isfd && fops->f_touch != NULL) {
1113 fops->f_touch(kn, kev, EVENT_REGISTER);
1115 kn->kn_sfflags = kev->fflags;
1116 kn->kn_sdata = kev->data;
1120 * We can get here with kn->kn_knlist == NULL. This can happen when
1121 * the initial attach event decides that the event is "completed"
1122 * already. i.e. filt_procattach is called on a zombie process. It
1123 * will call filt_proc which will remove it from the list, and NULL
1127 event = kn->kn_fop->f_event(kn, 0);
1130 KNOTE_ACTIVATE(kn, 1);
1131 kn->kn_status &= ~KN_INFLUX;
1134 if ((kev->flags & EV_DISABLE) &&
1135 ((kn->kn_status & KN_DISABLED) == 0)) {
1136 kn->kn_status |= KN_DISABLED;
1139 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
1140 kn->kn_status &= ~KN_DISABLED;
1141 if ((kn->kn_status & KN_ACTIVE) &&
1142 ((kn->kn_status & KN_QUEUED) == 0))
1148 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1154 kqueue_fo_release(filt);
1159 kqueue_acquire(struct file *fp, struct kqueue **kqp)
1167 if (fp->f_type != DTYPE_KQUEUE || kq == NULL)
1171 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
1182 kqueue_release(struct kqueue *kq, int locked)
1189 if (kq->kq_refcnt == 1)
1190 wakeup(&kq->kq_refcnt);
1196 kqueue_schedtask(struct kqueue *kq)
1200 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1201 ("scheduling kqueue task while draining"));
1203 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1204 taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task);
1205 kq->kq_state |= KQ_TASKSCHED;
1210 * Expand the kq to make sure we have storage for fops/ident pair.
1212 * Return 0 on success (or no work necessary), return errno on failure.
1214 * Not calling hashinit w/ waitok (proper malloc flag) should be safe.
1215 * If kqueue_register is called from a non-fd context, there usually/should
1219 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
1222 struct klist *list, *tmp_knhash, *to_free;
1223 u_long tmp_knhashmask;
1226 int mflag = waitok ? M_WAITOK : M_NOWAIT;
1233 if (kq->kq_knlistsize <= fd) {
1234 size = kq->kq_knlistsize;
1237 list = malloc(size * sizeof(*list), M_KQUEUE, mflag);
1241 if (kq->kq_knlistsize > fd) {
1245 if (kq->kq_knlist != NULL) {
1246 bcopy(kq->kq_knlist, list,
1247 kq->kq_knlistsize * sizeof(*list));
1248 to_free = kq->kq_knlist;
1249 kq->kq_knlist = NULL;
1251 bzero((caddr_t)list +
1252 kq->kq_knlistsize * sizeof(*list),
1253 (size - kq->kq_knlistsize) * sizeof(*list));
1254 kq->kq_knlistsize = size;
1255 kq->kq_knlist = list;
1260 if (kq->kq_knhashmask == 0) {
1261 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1263 if (tmp_knhash == NULL)
1266 if (kq->kq_knhashmask == 0) {
1267 kq->kq_knhash = tmp_knhash;
1268 kq->kq_knhashmask = tmp_knhashmask;
1270 to_free = tmp_knhash;
1275 free(to_free, M_KQUEUE);
1282 kqueue_task(void *arg, int pending)
1290 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1293 KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1295 kq->kq_state &= ~KQ_TASKSCHED;
1296 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1297 wakeup(&kq->kq_state);
1300 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1304 * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1305 * We treat KN_MARKER knotes as if they are INFLUX.
1308 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1309 const struct timespec *tsp, struct kevent *keva, struct thread *td)
1311 struct kevent *kevp;
1312 struct knote *kn, *marker;
1313 sbintime_t asbt, rsbt;
1314 int count, error, haskqglobal, influx, nkev, touch;
1326 if (tsp->tv_sec < 0 || tsp->tv_nsec < 0 ||
1327 tsp->tv_nsec > 1000000000) {
1331 if (timespecisset(tsp)) {
1332 if (tsp->tv_sec < INT32_MAX) {
1333 rsbt = tstosbt(*tsp);
1334 if (TIMESEL(&asbt, rsbt))
1335 asbt += tc_tick_sbt;
1339 rsbt >>= tc_precexp;
1346 marker = knote_alloc(1);
1347 if (marker == NULL) {
1351 marker->kn_status = KN_MARKER;
1356 if (kq->kq_count == 0) {
1358 error = EWOULDBLOCK;
1360 kq->kq_state |= KQ_SLEEP;
1361 error = msleep_sbt(kq, &kq->kq_lock, PSOCK | PCATCH,
1362 "kqread", asbt, rsbt, C_ABSOLUTE);
1366 /* don't restart after signals... */
1367 if (error == ERESTART)
1369 else if (error == EWOULDBLOCK)
1374 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1378 kn = TAILQ_FIRST(&kq->kq_head);
1380 if ((kn->kn_status == KN_MARKER && kn != marker) ||
1381 (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1386 kq->kq_state |= KQ_FLUXWAIT;
1387 error = msleep(kq, &kq->kq_lock, PSOCK,
1392 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1393 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1394 kn->kn_status &= ~KN_QUEUED;
1400 if (count == maxevents)
1404 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1405 ("KN_INFLUX set when not suppose to be"));
1407 if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
1408 kn->kn_status &= ~KN_QUEUED;
1409 kn->kn_status |= KN_INFLUX;
1413 * We don't need to lock the list since we've marked
1416 *kevp = kn->kn_kevent;
1417 if (!(kn->kn_status & KN_DETACHED))
1418 kn->kn_fop->f_detach(kn);
1423 kn->kn_status |= KN_INFLUX;
1425 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
1426 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1428 if (kn->kn_fop->f_event(kn, 0) == 0) {
1430 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1432 ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX);
1438 touch = (!kn->kn_fop->f_isfd &&
1439 kn->kn_fop->f_touch != NULL);
1441 kn->kn_fop->f_touch(kn, kevp, EVENT_PROCESS);
1443 *kevp = kn->kn_kevent;
1445 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1446 if (kn->kn_flags & (EV_CLEAR | EV_DISPATCH)) {
1448 * Manually clear knotes who weren't
1451 if (touch == 0 && kn->kn_flags & EV_CLEAR) {
1455 if (kn->kn_flags & EV_DISPATCH)
1456 kn->kn_status |= KN_DISABLED;
1457 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1460 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1462 kn->kn_status &= ~(KN_INFLUX);
1467 /* we are returning a copy to the user */
1472 if (nkev == KQ_NEVENTS) {
1475 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1483 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1491 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1492 td->td_retval[0] = maxevents - count;
1498 * This could be expanded to call kqueue_scan, if desired.
1502 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1503 int flags, struct thread *td)
1510 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
1511 int flags, struct thread *td)
1518 kqueue_truncate(struct file *fp, off_t length, struct ucred *active_cred,
1527 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
1528 struct ucred *active_cred, struct thread *td)
1531 * Enabling sigio causes two major problems:
1532 * 1) infinite recursion:
1533 * Synopsys: kevent is being used to track signals and have FIOASYNC
1534 * set. On receipt of a signal this will cause a kqueue to recurse
1535 * into itself over and over. Sending the sigio causes the kqueue
1536 * to become ready, which in turn posts sigio again, forever.
1537 * Solution: this can be solved by setting a flag in the kqueue that
1538 * we have a SIGIO in progress.
1539 * 2) locking problems:
1540 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
1541 * us above the proc and pgrp locks.
1542 * Solution: Post a signal using an async mechanism, being sure to
1543 * record a generation count in the delivery so that we do not deliver
1544 * a signal to the wrong process.
1546 * Note, these two mechanisms are somewhat mutually exclusive!
1555 kq->kq_state |= KQ_ASYNC;
1557 kq->kq_state &= ~KQ_ASYNC;
1562 return (fsetown(*(int *)data, &kq->kq_sigio));
1565 *(int *)data = fgetown(&kq->kq_sigio);
1575 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
1582 if ((error = kqueue_acquire(fp, &kq)))
1586 if (events & (POLLIN | POLLRDNORM)) {
1588 revents |= events & (POLLIN | POLLRDNORM);
1590 selrecord(td, &kq->kq_sel);
1591 if (SEL_WAITING(&kq->kq_sel))
1592 kq->kq_state |= KQ_SEL;
1595 kqueue_release(kq, 1);
1602 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1606 bzero((void *)st, sizeof *st);
1608 * We no longer return kq_count because the unlocked value is useless.
1609 * If you spent all this time getting the count, why not spend your
1610 * syscall better by calling kevent?
1612 * XXX - This is needed for libc_r.
1614 st->st_mode = S_IFIFO;
1620 kqueue_close(struct file *fp, struct thread *td)
1622 struct kqueue *kq = fp->f_data;
1623 struct filedesc *fdp;
1628 if ((error = kqueue_acquire(fp, &kq)))
1633 KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
1634 ("kqueue already closing"));
1635 kq->kq_state |= KQ_CLOSING;
1636 if (kq->kq_refcnt > 1)
1637 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
1639 KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
1642 KASSERT(knlist_empty(&kq->kq_sel.si_note),
1643 ("kqueue's knlist not empty"));
1645 for (i = 0; i < kq->kq_knlistsize; i++) {
1646 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
1647 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1648 kq->kq_state |= KQ_FLUXWAIT;
1649 msleep(kq, &kq->kq_lock, PSOCK, "kqclo1", 0);
1652 kn->kn_status |= KN_INFLUX;
1654 if (!(kn->kn_status & KN_DETACHED))
1655 kn->kn_fop->f_detach(kn);
1660 if (kq->kq_knhashmask != 0) {
1661 for (i = 0; i <= kq->kq_knhashmask; i++) {
1662 while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
1663 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1664 kq->kq_state |= KQ_FLUXWAIT;
1665 msleep(kq, &kq->kq_lock, PSOCK,
1669 kn->kn_status |= KN_INFLUX;
1671 if (!(kn->kn_status & KN_DETACHED))
1672 kn->kn_fop->f_detach(kn);
1679 if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
1680 kq->kq_state |= KQ_TASKDRAIN;
1681 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
1684 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1685 selwakeuppri(&kq->kq_sel, PSOCK);
1686 if (!SEL_WAITING(&kq->kq_sel))
1687 kq->kq_state &= ~KQ_SEL;
1692 FILEDESC_XLOCK(fdp);
1693 SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list);
1694 FILEDESC_XUNLOCK(fdp);
1696 seldrain(&kq->kq_sel);
1697 knlist_destroy(&kq->kq_sel.si_note);
1698 mtx_destroy(&kq->kq_lock);
1701 if (kq->kq_knhash != NULL)
1702 free(kq->kq_knhash, M_KQUEUE);
1703 if (kq->kq_knlist != NULL)
1704 free(kq->kq_knlist, M_KQUEUE);
1706 funsetown(&kq->kq_sigio);
1714 kqueue_wakeup(struct kqueue *kq)
1718 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
1719 kq->kq_state &= ~KQ_SLEEP;
1722 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1723 selwakeuppri(&kq->kq_sel, PSOCK);
1724 if (!SEL_WAITING(&kq->kq_sel))
1725 kq->kq_state &= ~KQ_SEL;
1727 if (!knlist_empty(&kq->kq_sel.si_note))
1728 kqueue_schedtask(kq);
1729 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
1730 pgsigio(&kq->kq_sigio, SIGIO, 0);
1735 * Walk down a list of knotes, activating them if their event has triggered.
1737 * There is a possibility to optimize in the case of one kq watching another.
1738 * Instead of scheduling a task to wake it up, you could pass enough state
1739 * down the chain to make up the parent kqueue. Make this code functional
1743 knote(struct knlist *list, long hint, int lockflags)
1752 KNL_ASSERT_LOCK(list, lockflags & KNF_LISTLOCKED);
1754 if ((lockflags & KNF_LISTLOCKED) == 0)
1755 list->kl_lock(list->kl_lockarg);
1758 * If we unlock the list lock (and set KN_INFLUX), we can eliminate
1759 * the kqueue scheduling, but this will introduce four
1760 * lock/unlock's for each knote to test. If we do, continue to use
1761 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is
1762 * only safe if you want to remove the current item, which we are
1765 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
1767 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1769 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1771 } else if ((lockflags & KNF_NOKQLOCK) != 0) {
1772 kn->kn_status |= KN_INFLUX;
1774 error = kn->kn_fop->f_event(kn, hint);
1776 kn->kn_status &= ~KN_INFLUX;
1778 KNOTE_ACTIVATE(kn, 1);
1781 kn->kn_status |= KN_HASKQLOCK;
1782 if (kn->kn_fop->f_event(kn, hint))
1783 KNOTE_ACTIVATE(kn, 1);
1784 kn->kn_status &= ~KN_HASKQLOCK;
1790 if ((lockflags & KNF_LISTLOCKED) == 0)
1791 list->kl_unlock(list->kl_lockarg);
1795 * add a knote to a knlist
1798 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
1800 KNL_ASSERT_LOCK(knl, islocked);
1801 KQ_NOTOWNED(kn->kn_kq);
1802 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) ==
1803 (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED"));
1805 knl->kl_lock(knl->kl_lockarg);
1806 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
1808 knl->kl_unlock(knl->kl_lockarg);
1810 kn->kn_knlist = knl;
1811 kn->kn_status &= ~KN_DETACHED;
1812 KQ_UNLOCK(kn->kn_kq);
1816 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked)
1818 KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked"));
1819 KNL_ASSERT_LOCK(knl, knlislocked);
1820 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
1822 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX,
1823 ("knlist_remove called w/o knote being KN_INFLUX or already removed"));
1825 knl->kl_lock(knl->kl_lockarg);
1826 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
1827 kn->kn_knlist = NULL;
1829 knl->kl_unlock(knl->kl_lockarg);
1832 kn->kn_status |= KN_DETACHED;
1834 KQ_UNLOCK(kn->kn_kq);
1838 * remove all knotes from a specified klist
1841 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
1844 knlist_remove_kq(knl, kn, islocked, 0);
1848 * remove knote from a specified klist while in f_event handler.
1851 knlist_remove_inevent(struct knlist *knl, struct knote *kn)
1854 knlist_remove_kq(knl, kn, 1,
1855 (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK);
1859 knlist_empty(struct knlist *knl)
1862 KNL_ASSERT_LOCKED(knl);
1863 return SLIST_EMPTY(&knl->kl_list);
1866 static struct mtx knlist_lock;
1867 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
1869 static void knlist_mtx_lock(void *arg);
1870 static void knlist_mtx_unlock(void *arg);
1873 knlist_mtx_lock(void *arg)
1876 mtx_lock((struct mtx *)arg);
1880 knlist_mtx_unlock(void *arg)
1883 mtx_unlock((struct mtx *)arg);
1887 knlist_mtx_assert_locked(void *arg)
1890 mtx_assert((struct mtx *)arg, MA_OWNED);
1894 knlist_mtx_assert_unlocked(void *arg)
1897 mtx_assert((struct mtx *)arg, MA_NOTOWNED);
1901 knlist_rw_rlock(void *arg)
1904 rw_rlock((struct rwlock *)arg);
1908 knlist_rw_runlock(void *arg)
1911 rw_runlock((struct rwlock *)arg);
1915 knlist_rw_assert_locked(void *arg)
1918 rw_assert((struct rwlock *)arg, RA_LOCKED);
1922 knlist_rw_assert_unlocked(void *arg)
1925 rw_assert((struct rwlock *)arg, RA_UNLOCKED);
1929 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
1930 void (*kl_unlock)(void *),
1931 void (*kl_assert_locked)(void *), void (*kl_assert_unlocked)(void *))
1935 knl->kl_lockarg = &knlist_lock;
1937 knl->kl_lockarg = lock;
1939 if (kl_lock == NULL)
1940 knl->kl_lock = knlist_mtx_lock;
1942 knl->kl_lock = kl_lock;
1943 if (kl_unlock == NULL)
1944 knl->kl_unlock = knlist_mtx_unlock;
1946 knl->kl_unlock = kl_unlock;
1947 if (kl_assert_locked == NULL)
1948 knl->kl_assert_locked = knlist_mtx_assert_locked;
1950 knl->kl_assert_locked = kl_assert_locked;
1951 if (kl_assert_unlocked == NULL)
1952 knl->kl_assert_unlocked = knlist_mtx_assert_unlocked;
1954 knl->kl_assert_unlocked = kl_assert_unlocked;
1956 SLIST_INIT(&knl->kl_list);
1960 knlist_init_mtx(struct knlist *knl, struct mtx *lock)
1963 knlist_init(knl, lock, NULL, NULL, NULL, NULL);
1967 knlist_init_rw_reader(struct knlist *knl, struct rwlock *lock)
1970 knlist_init(knl, lock, knlist_rw_rlock, knlist_rw_runlock,
1971 knlist_rw_assert_locked, knlist_rw_assert_unlocked);
1975 knlist_destroy(struct knlist *knl)
1980 * if we run across this error, we need to find the offending
1981 * driver and have it call knlist_clear.
1983 if (!SLIST_EMPTY(&knl->kl_list))
1984 printf("WARNING: destroying knlist w/ knotes on it!\n");
1987 knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL;
1988 SLIST_INIT(&knl->kl_list);
1992 * Even if we are locked, we may need to drop the lock to allow any influx
1993 * knotes time to "settle".
1996 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
1998 struct knote *kn, *kn2;
2002 KNL_ASSERT_LOCKED(knl);
2004 KNL_ASSERT_UNLOCKED(knl);
2005 again: /* need to reacquire lock since we have dropped it */
2006 knl->kl_lock(knl->kl_lockarg);
2009 SLIST_FOREACH_SAFE(kn, &knl->kl_list, kn_selnext, kn2) {
2012 if ((kn->kn_status & KN_INFLUX)) {
2016 knlist_remove_kq(knl, kn, 1, 1);
2018 kn->kn_status |= KN_INFLUX | KN_DETACHED;
2022 /* Make sure cleared knotes disappear soon */
2023 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2029 if (!SLIST_EMPTY(&knl->kl_list)) {
2030 /* there are still KN_INFLUX remaining */
2031 kn = SLIST_FIRST(&knl->kl_list);
2034 KASSERT(kn->kn_status & KN_INFLUX,
2035 ("knote removed w/o list lock"));
2036 knl->kl_unlock(knl->kl_lockarg);
2037 kq->kq_state |= KQ_FLUXWAIT;
2038 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
2044 KNL_ASSERT_LOCKED(knl);
2046 knl->kl_unlock(knl->kl_lockarg);
2047 KNL_ASSERT_UNLOCKED(knl);
2052 * Remove all knotes referencing a specified fd must be called with FILEDESC
2053 * lock. This prevents a race where a new fd comes along and occupies the
2054 * entry and we attach a knote to the fd.
2057 knote_fdclose(struct thread *td, int fd)
2059 struct filedesc *fdp = td->td_proc->p_fd;
2064 FILEDESC_XLOCK_ASSERT(fdp);
2067 * We shouldn't have to worry about new kevents appearing on fd
2068 * since filedesc is locked.
2070 SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
2075 while (kq->kq_knlistsize > fd &&
2076 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
2077 if (kn->kn_status & KN_INFLUX) {
2078 /* someone else might be waiting on our knote */
2081 kq->kq_state |= KQ_FLUXWAIT;
2082 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
2085 kn->kn_status |= KN_INFLUX;
2087 if (!(kn->kn_status & KN_DETACHED))
2088 kn->kn_fop->f_detach(kn);
2098 knote_attach(struct knote *kn, struct kqueue *kq)
2102 KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX"));
2105 if (kn->kn_fop->f_isfd) {
2106 if (kn->kn_id >= kq->kq_knlistsize)
2108 list = &kq->kq_knlist[kn->kn_id];
2110 if (kq->kq_knhash == NULL)
2112 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2115 SLIST_INSERT_HEAD(list, kn, kn_link);
2121 * knote must already have been detached using the f_detach method.
2122 * no lock need to be held, it is assumed that the KN_INFLUX flag is set
2123 * to prevent other removal.
2126 knote_drop(struct knote *kn, struct thread *td)
2134 KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX,
2135 ("knote_drop called without KN_INFLUX set in kn_status"));
2138 if (kn->kn_fop->f_isfd)
2139 list = &kq->kq_knlist[kn->kn_id];
2141 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2143 if (!SLIST_EMPTY(list))
2144 SLIST_REMOVE(list, kn, knote, kn_link);
2145 if (kn->kn_status & KN_QUEUED)
2149 if (kn->kn_fop->f_isfd) {
2150 fdrop(kn->kn_fp, td);
2153 kqueue_fo_release(kn->kn_kevent.filter);
2159 knote_enqueue(struct knote *kn)
2161 struct kqueue *kq = kn->kn_kq;
2163 KQ_OWNED(kn->kn_kq);
2164 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
2166 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
2167 kn->kn_status |= KN_QUEUED;
2173 knote_dequeue(struct knote *kn)
2175 struct kqueue *kq = kn->kn_kq;
2177 KQ_OWNED(kn->kn_kq);
2178 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
2180 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
2181 kn->kn_status &= ~KN_QUEUED;
2189 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
2190 NULL, NULL, UMA_ALIGN_PTR, 0);
2192 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL);
2194 static struct knote *
2195 knote_alloc(int waitok)
2197 return ((struct knote *)uma_zalloc(knote_zone,
2198 (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO));
2202 knote_free(struct knote *kn)
2205 uma_zfree(knote_zone, kn);
2209 * Register the kev w/ the kq specified by fd.
2212 kqfd_register(int fd, struct kevent *kev, struct thread *td, int waitok)
2218 if ((error = fget(td, fd, CAP_POST_EVENT, &fp)) != 0)
2220 if ((error = kqueue_acquire(fp, &kq)) != 0)
2223 error = kqueue_register(kq, kev, td, waitok);
2225 kqueue_release(kq, 0);