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/stdatomic.h>
51 #include <sys/queue.h>
52 #include <sys/event.h>
53 #include <sys/eventvar.h>
55 #include <sys/protosw.h>
56 #include <sys/sigio.h>
57 #include <sys/signalvar.h>
58 #include <sys/socket.h>
59 #include <sys/socketvar.h>
61 #include <sys/sysctl.h>
62 #include <sys/sysproto.h>
63 #include <sys/syscallsubr.h>
64 #include <sys/taskqueue.h>
67 #include <sys/ktrace.h>
72 static MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
75 * This lock is used if multiple kq locks are required. This possibly
76 * should be made into a per proc lock.
78 static struct mtx kq_global;
79 MTX_SYSINIT(kq_global, &kq_global, "kqueue order", MTX_DEF);
80 #define KQ_GLOBAL_LOCK(lck, haslck) do { \
85 #define KQ_GLOBAL_UNLOCK(lck, haslck) do { \
91 TASKQUEUE_DEFINE_THREAD(kqueue);
93 static int kevent_copyout(void *arg, struct kevent *kevp, int count);
94 static int kevent_copyin(void *arg, struct kevent *kevp, int count);
95 static int kqueue_register(struct kqueue *kq, struct kevent *kev,
96 struct thread *td, int waitok);
97 static int kqueue_acquire(struct file *fp, struct kqueue **kqp);
98 static void kqueue_release(struct kqueue *kq, int locked);
99 static int kqueue_expand(struct kqueue *kq, struct filterops *fops,
100 uintptr_t ident, int waitok);
101 static void kqueue_task(void *arg, int pending);
102 static int kqueue_scan(struct kqueue *kq, int maxevents,
103 struct kevent_copyops *k_ops,
104 const struct timespec *timeout,
105 struct kevent *keva, struct thread *td);
106 static void kqueue_wakeup(struct kqueue *kq);
107 static struct filterops *kqueue_fo_find(int filt);
108 static void kqueue_fo_release(int filt);
110 static fo_rdwr_t kqueue_read;
111 static fo_rdwr_t kqueue_write;
112 static fo_truncate_t kqueue_truncate;
113 static fo_ioctl_t kqueue_ioctl;
114 static fo_poll_t kqueue_poll;
115 static fo_kqfilter_t kqueue_kqfilter;
116 static fo_stat_t kqueue_stat;
117 static fo_close_t kqueue_close;
119 static struct fileops kqueueops = {
120 .fo_read = kqueue_read,
121 .fo_write = kqueue_write,
122 .fo_truncate = kqueue_truncate,
123 .fo_ioctl = kqueue_ioctl,
124 .fo_poll = kqueue_poll,
125 .fo_kqfilter = kqueue_kqfilter,
126 .fo_stat = kqueue_stat,
127 .fo_close = kqueue_close,
128 .fo_chmod = invfo_chmod,
129 .fo_chown = invfo_chown,
130 .fo_sendfile = invfo_sendfile,
133 static int knote_attach(struct knote *kn, struct kqueue *kq);
134 static void knote_drop(struct knote *kn, struct thread *td);
135 static void knote_enqueue(struct knote *kn);
136 static void knote_dequeue(struct knote *kn);
137 static void knote_init(void);
138 static struct knote *knote_alloc(int waitok);
139 static void knote_free(struct knote *kn);
141 static void filt_kqdetach(struct knote *kn);
142 static int filt_kqueue(struct knote *kn, long hint);
143 static int filt_procattach(struct knote *kn);
144 static void filt_procdetach(struct knote *kn);
145 static int filt_proc(struct knote *kn, long hint);
146 static int filt_fileattach(struct knote *kn);
147 static void filt_timerexpire(void *knx);
148 static int filt_timerattach(struct knote *kn);
149 static void filt_timerdetach(struct knote *kn);
150 static int filt_timer(struct knote *kn, long hint);
151 static int filt_userattach(struct knote *kn);
152 static void filt_userdetach(struct knote *kn);
153 static int filt_user(struct knote *kn, long hint);
154 static void filt_usertouch(struct knote *kn, struct kevent *kev,
157 static struct filterops file_filtops = {
159 .f_attach = filt_fileattach,
161 static struct filterops kqread_filtops = {
163 .f_detach = filt_kqdetach,
164 .f_event = filt_kqueue,
166 /* XXX - move to kern_proc.c? */
167 static struct filterops proc_filtops = {
169 .f_attach = filt_procattach,
170 .f_detach = filt_procdetach,
171 .f_event = filt_proc,
173 static struct filterops timer_filtops = {
175 .f_attach = filt_timerattach,
176 .f_detach = filt_timerdetach,
177 .f_event = filt_timer,
179 static struct filterops user_filtops = {
180 .f_attach = filt_userattach,
181 .f_detach = filt_userdetach,
182 .f_event = filt_user,
183 .f_touch = filt_usertouch,
186 static uma_zone_t knote_zone;
187 static atomic_uint kq_ncallouts = ATOMIC_VAR_INIT(0);
188 static unsigned int kq_calloutmax = 4 * 1024;
189 SYSCTL_UINT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
190 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
192 /* XXX - ensure not KN_INFLUX?? */
193 #define KNOTE_ACTIVATE(kn, islock) do { \
195 mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED); \
197 KQ_LOCK((kn)->kn_kq); \
198 (kn)->kn_status |= KN_ACTIVE; \
199 if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
200 knote_enqueue((kn)); \
202 KQ_UNLOCK((kn)->kn_kq); \
204 #define KQ_LOCK(kq) do { \
205 mtx_lock(&(kq)->kq_lock); \
207 #define KQ_FLUX_WAKEUP(kq) do { \
208 if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) { \
209 (kq)->kq_state &= ~KQ_FLUXWAIT; \
213 #define KQ_UNLOCK_FLUX(kq) do { \
214 KQ_FLUX_WAKEUP(kq); \
215 mtx_unlock(&(kq)->kq_lock); \
217 #define KQ_UNLOCK(kq) do { \
218 mtx_unlock(&(kq)->kq_lock); \
220 #define KQ_OWNED(kq) do { \
221 mtx_assert(&(kq)->kq_lock, MA_OWNED); \
223 #define KQ_NOTOWNED(kq) do { \
224 mtx_assert(&(kq)->kq_lock, MA_NOTOWNED); \
226 #define KN_LIST_LOCK(kn) do { \
227 if (kn->kn_knlist != NULL) \
228 kn->kn_knlist->kl_lock(kn->kn_knlist->kl_lockarg); \
230 #define KN_LIST_UNLOCK(kn) do { \
231 if (kn->kn_knlist != NULL) \
232 kn->kn_knlist->kl_unlock(kn->kn_knlist->kl_lockarg); \
234 #define KNL_ASSERT_LOCK(knl, islocked) do { \
236 KNL_ASSERT_LOCKED(knl); \
238 KNL_ASSERT_UNLOCKED(knl); \
241 #define KNL_ASSERT_LOCKED(knl) do { \
242 knl->kl_assert_locked((knl)->kl_lockarg); \
244 #define KNL_ASSERT_UNLOCKED(knl) do { \
245 knl->kl_assert_unlocked((knl)->kl_lockarg); \
247 #else /* !INVARIANTS */
248 #define KNL_ASSERT_LOCKED(knl) do {} while(0)
249 #define KNL_ASSERT_UNLOCKED(knl) do {} while (0)
250 #endif /* INVARIANTS */
252 #define KN_HASHSIZE 64 /* XXX should be tunable */
253 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
256 filt_nullattach(struct knote *kn)
262 struct filterops null_filtops = {
264 .f_attach = filt_nullattach,
267 /* XXX - make SYSINIT to add these, and move into respective modules. */
268 extern struct filterops sig_filtops;
269 extern struct filterops fs_filtops;
272 * Table for for all system-defined filters.
274 static struct mtx filterops_lock;
275 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops",
278 struct filterops *for_fop;
280 } sysfilt_ops[EVFILT_SYSCOUNT] = {
281 { &file_filtops }, /* EVFILT_READ */
282 { &file_filtops }, /* EVFILT_WRITE */
283 { &null_filtops }, /* EVFILT_AIO */
284 { &file_filtops }, /* EVFILT_VNODE */
285 { &proc_filtops }, /* EVFILT_PROC */
286 { &sig_filtops }, /* EVFILT_SIGNAL */
287 { &timer_filtops }, /* EVFILT_TIMER */
288 { &null_filtops }, /* former EVFILT_NETDEV */
289 { &fs_filtops }, /* EVFILT_FS */
290 { &null_filtops }, /* EVFILT_LIO */
291 { &user_filtops }, /* EVFILT_USER */
295 * Simple redirection for all cdevsw style objects to call their fo_kqfilter
299 filt_fileattach(struct knote *kn)
302 return (fo_kqfilter(kn->kn_fp, kn));
307 kqueue_kqfilter(struct file *fp, struct knote *kn)
309 struct kqueue *kq = kn->kn_fp->f_data;
311 if (kn->kn_filter != EVFILT_READ)
314 kn->kn_status |= KN_KQUEUE;
315 kn->kn_fop = &kqread_filtops;
316 knlist_add(&kq->kq_sel.si_note, kn, 0);
322 filt_kqdetach(struct knote *kn)
324 struct kqueue *kq = kn->kn_fp->f_data;
326 knlist_remove(&kq->kq_sel.si_note, kn, 0);
331 filt_kqueue(struct knote *kn, long hint)
333 struct kqueue *kq = kn->kn_fp->f_data;
335 kn->kn_data = kq->kq_count;
336 return (kn->kn_data > 0);
339 /* XXX - move to kern_proc.c? */
341 filt_procattach(struct knote *kn)
348 p = pfind(kn->kn_id);
349 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
350 p = zpfind(kn->kn_id);
352 } else if (p != NULL && (p->p_flag & P_WEXIT)) {
358 if ((error = p_cansee(curthread, p))) {
363 kn->kn_ptr.p_proc = p;
364 kn->kn_flags |= EV_CLEAR; /* automatically set */
367 * internal flag indicating registration done by kernel
369 if (kn->kn_flags & EV_FLAG1) {
370 kn->kn_data = kn->kn_sdata; /* ppid */
371 kn->kn_fflags = NOTE_CHILD;
372 kn->kn_flags &= ~EV_FLAG1;
376 knlist_add(&p->p_klist, kn, 1);
379 * Immediately activate any exit notes if the target process is a
380 * zombie. This is necessary to handle the case where the target
381 * process, e.g. a child, dies before the kevent is registered.
383 if (immediate && filt_proc(kn, NOTE_EXIT))
384 KNOTE_ACTIVATE(kn, 0);
392 * The knote may be attached to a different process, which may exit,
393 * leaving nothing for the knote to be attached to. So when the process
394 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
395 * it will be deleted when read out. However, as part of the knote deletion,
396 * this routine is called, so a check is needed to avoid actually performing
397 * a detach, because the original process does not exist any more.
399 /* XXX - move to kern_proc.c? */
401 filt_procdetach(struct knote *kn)
405 p = kn->kn_ptr.p_proc;
406 knlist_remove(&p->p_klist, kn, 0);
407 kn->kn_ptr.p_proc = NULL;
410 /* XXX - move to kern_proc.c? */
412 filt_proc(struct knote *kn, long hint)
414 struct proc *p = kn->kn_ptr.p_proc;
418 * mask off extra data
420 event = (u_int)hint & NOTE_PCTRLMASK;
423 * if the user is interested in this event, record it.
425 if (kn->kn_sfflags & event)
426 kn->kn_fflags |= event;
429 * process is gone, so flag the event as finished.
431 if (event == NOTE_EXIT) {
432 if (!(kn->kn_status & KN_DETACHED))
433 knlist_remove_inevent(&p->p_klist, kn);
434 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
435 kn->kn_ptr.p_proc = NULL;
436 if (kn->kn_fflags & NOTE_EXIT)
437 kn->kn_data = p->p_xstat;
438 if (kn->kn_fflags == 0)
439 kn->kn_flags |= EV_DROP;
443 return (kn->kn_fflags != 0);
447 * Called when the process forked. It mostly does the same as the
448 * knote(), activating all knotes registered to be activated when the
449 * process forked. Additionally, for each knote attached to the
450 * parent, check whether user wants to track the new process. If so
451 * attach a new knote to it, and immediately report an event with the
455 knote_fork(struct knlist *list, int pid)
464 list->kl_lock(list->kl_lockarg);
466 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
467 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX)
471 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
477 * The same as knote(), activate the event.
479 if ((kn->kn_sfflags & NOTE_TRACK) == 0) {
480 kn->kn_status |= KN_HASKQLOCK;
481 if (kn->kn_fop->f_event(kn, NOTE_FORK))
482 KNOTE_ACTIVATE(kn, 1);
483 kn->kn_status &= ~KN_HASKQLOCK;
489 * The NOTE_TRACK case. In addition to the activation
490 * of the event, we need to register new event to
491 * track the child. Drop the locks in preparation for
492 * the call to kqueue_register().
494 kn->kn_status |= KN_INFLUX;
496 list->kl_unlock(list->kl_lockarg);
499 * Activate existing knote and register a knote with
503 kev.filter = kn->kn_filter;
504 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
505 kev.fflags = kn->kn_sfflags;
506 kev.data = kn->kn_id; /* parent */
507 kev.udata = kn->kn_kevent.udata;/* preserve udata */
508 error = kqueue_register(kq, &kev, NULL, 0);
510 kn->kn_fflags |= NOTE_TRACKERR;
511 if (kn->kn_fop->f_event(kn, NOTE_FORK))
512 KNOTE_ACTIVATE(kn, 0);
514 kn->kn_status &= ~KN_INFLUX;
516 list->kl_lock(list->kl_lockarg);
518 list->kl_unlock(list->kl_lockarg);
522 * XXX: EVFILT_TIMER should perhaps live in kern_time.c beside the
523 * interval timer support code.
525 static __inline sbintime_t
526 timer2sbintime(intptr_t data)
529 return (SBT_1MS * data);
533 filt_timerexpire(void *knx)
535 struct callout *calloutp;
540 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */
542 if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) {
543 calloutp = (struct callout *)kn->kn_hook;
544 callout_reset_sbt_on(calloutp,
545 timer2sbintime(kn->kn_sdata), 0 /* 1ms? */,
546 filt_timerexpire, kn, PCPU_GET(cpuid), 0);
551 * data contains amount of time to sleep, in milliseconds
554 filt_timerattach(struct knote *kn)
556 struct callout *calloutp;
557 unsigned int ncallouts;
559 ncallouts = atomic_load_explicit(&kq_ncallouts, memory_order_relaxed);
561 if (ncallouts >= kq_calloutmax)
563 } while (!atomic_compare_exchange_weak_explicit(&kq_ncallouts,
564 &ncallouts, ncallouts + 1, memory_order_relaxed,
565 memory_order_relaxed));
567 kn->kn_flags |= EV_CLEAR; /* automatically set */
568 kn->kn_status &= ~KN_DETACHED; /* knlist_add clears it */
569 calloutp = malloc(sizeof(*calloutp), M_KQUEUE, M_WAITOK);
570 callout_init(calloutp, CALLOUT_MPSAFE);
571 kn->kn_hook = calloutp;
572 callout_reset_sbt_on(calloutp,
573 timer2sbintime(kn->kn_sdata), 0 /* 1ms? */,
574 filt_timerexpire, kn, PCPU_GET(cpuid), 0);
580 filt_timerdetach(struct knote *kn)
582 struct callout *calloutp;
585 calloutp = (struct callout *)kn->kn_hook;
586 callout_drain(calloutp);
587 free(calloutp, M_KQUEUE);
588 old = atomic_fetch_sub_explicit(&kq_ncallouts, 1, memory_order_relaxed);
589 KASSERT(old > 0, ("Number of callouts cannot become negative"));
590 kn->kn_status |= KN_DETACHED; /* knlist_remove sets it */
594 filt_timer(struct knote *kn, long hint)
597 return (kn->kn_data != 0);
601 filt_userattach(struct knote *kn)
605 * EVFILT_USER knotes are not attached to anything in the kernel.
608 if (kn->kn_fflags & NOTE_TRIGGER)
616 filt_userdetach(__unused struct knote *kn)
620 * EVFILT_USER knotes are not attached to anything in the kernel.
625 filt_user(struct knote *kn, __unused long hint)
628 return (kn->kn_hookid);
632 filt_usertouch(struct knote *kn, struct kevent *kev, u_long type)
638 if (kev->fflags & NOTE_TRIGGER)
641 ffctrl = kev->fflags & NOTE_FFCTRLMASK;
642 kev->fflags &= NOTE_FFLAGSMASK;
648 kn->kn_sfflags &= kev->fflags;
652 kn->kn_sfflags |= kev->fflags;
656 kn->kn_sfflags = kev->fflags;
660 /* XXX Return error? */
663 kn->kn_sdata = kev->data;
664 if (kev->flags & EV_CLEAR) {
672 *kev = kn->kn_kevent;
673 kev->fflags = kn->kn_sfflags;
674 kev->data = kn->kn_sdata;
675 if (kn->kn_flags & EV_CLEAR) {
683 panic("filt_usertouch() - invalid type (%ld)", type);
689 sys_kqueue(struct thread *td, struct kqueue_args *uap)
691 struct filedesc *fdp;
696 fdp = td->td_proc->p_fd;
697 error = falloc(td, &fp, &fd, 0);
701 /* An extra reference on `fp' has been held for us by falloc(). */
702 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
703 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK);
704 TAILQ_INIT(&kq->kq_head);
706 knlist_init_mtx(&kq->kq_sel.si_note, &kq->kq_lock);
707 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
710 SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
711 FILEDESC_XUNLOCK(fdp);
713 finit(fp, FREAD | FWRITE, DTYPE_KQUEUE, kq, &kqueueops);
716 td->td_retval[0] = fd;
721 #ifndef _SYS_SYSPROTO_H_
724 const struct kevent *changelist;
726 struct kevent *eventlist;
728 const struct timespec *timeout;
732 sys_kevent(struct thread *td, struct kevent_args *uap)
734 struct timespec ts, *tsp;
735 struct kevent_copyops k_ops = { uap,
742 struct uio *ktruioin = NULL;
743 struct uio *ktruioout = NULL;
746 if (uap->timeout != NULL) {
747 error = copyin(uap->timeout, &ts, sizeof(ts));
755 if (KTRPOINT(td, KTR_GENIO)) {
756 ktriov.iov_base = uap->changelist;
757 ktriov.iov_len = uap->nchanges * sizeof(struct kevent);
758 ktruio = (struct uio){ .uio_iov = &ktriov, .uio_iovcnt = 1,
759 .uio_segflg = UIO_USERSPACE, .uio_rw = UIO_READ,
761 ktruioin = cloneuio(&ktruio);
762 ktriov.iov_base = uap->eventlist;
763 ktriov.iov_len = uap->nevents * sizeof(struct kevent);
764 ktruioout = cloneuio(&ktruio);
768 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
772 if (ktruioin != NULL) {
773 ktruioin->uio_resid = uap->nchanges * sizeof(struct kevent);
774 ktrgenio(uap->fd, UIO_WRITE, ktruioin, 0);
775 ktruioout->uio_resid = td->td_retval[0] * sizeof(struct kevent);
776 ktrgenio(uap->fd, UIO_READ, ktruioout, error);
784 * Copy 'count' items into the destination list pointed to by uap->eventlist.
787 kevent_copyout(void *arg, struct kevent *kevp, int count)
789 struct kevent_args *uap;
792 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
793 uap = (struct kevent_args *)arg;
795 error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
797 uap->eventlist += count;
802 * Copy 'count' items from the list pointed to by uap->changelist.
805 kevent_copyin(void *arg, struct kevent *kevp, int count)
807 struct kevent_args *uap;
810 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
811 uap = (struct kevent_args *)arg;
813 error = copyin(uap->changelist, kevp, count * sizeof *kevp);
815 uap->changelist += count;
820 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
821 struct kevent_copyops *k_ops, const struct timespec *timeout)
823 struct kevent keva[KQ_NEVENTS];
824 struct kevent *kevp, *changes;
828 int i, n, nerrors, error;
830 error = fget(td, fd, cap_rights_init(&rights, CAP_POST_EVENT), &fp);
833 if ((error = kqueue_acquire(fp, &kq)) != 0)
838 while (nchanges > 0) {
839 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
840 error = k_ops->k_copyin(k_ops->arg, keva, n);
844 for (i = 0; i < n; i++) {
848 kevp->flags &= ~EV_SYSFLAGS;
849 error = kqueue_register(kq, kevp, td, 1);
850 if (error || (kevp->flags & EV_RECEIPT)) {
852 kevp->flags = EV_ERROR;
854 (void) k_ops->k_copyout(k_ops->arg,
866 td->td_retval[0] = nerrors;
871 error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td);
873 kqueue_release(kq, 0);
880 kqueue_add_filteropts(int filt, struct filterops *filtops)
885 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
887 "trying to add a filterop that is out of range: %d is beyond %d\n",
888 ~filt, EVFILT_SYSCOUNT);
891 mtx_lock(&filterops_lock);
892 if (sysfilt_ops[~filt].for_fop != &null_filtops &&
893 sysfilt_ops[~filt].for_fop != NULL)
896 sysfilt_ops[~filt].for_fop = filtops;
897 sysfilt_ops[~filt].for_refcnt = 0;
899 mtx_unlock(&filterops_lock);
905 kqueue_del_filteropts(int filt)
910 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
913 mtx_lock(&filterops_lock);
914 if (sysfilt_ops[~filt].for_fop == &null_filtops ||
915 sysfilt_ops[~filt].for_fop == NULL)
917 else if (sysfilt_ops[~filt].for_refcnt != 0)
920 sysfilt_ops[~filt].for_fop = &null_filtops;
921 sysfilt_ops[~filt].for_refcnt = 0;
923 mtx_unlock(&filterops_lock);
928 static struct filterops *
929 kqueue_fo_find(int filt)
932 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
935 mtx_lock(&filterops_lock);
936 sysfilt_ops[~filt].for_refcnt++;
937 if (sysfilt_ops[~filt].for_fop == NULL)
938 sysfilt_ops[~filt].for_fop = &null_filtops;
939 mtx_unlock(&filterops_lock);
941 return sysfilt_ops[~filt].for_fop;
945 kqueue_fo_release(int filt)
948 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
951 mtx_lock(&filterops_lock);
952 KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
953 ("filter object refcount not valid on release"));
954 sysfilt_ops[~filt].for_refcnt--;
955 mtx_unlock(&filterops_lock);
959 * A ref to kq (obtained via kqueue_acquire) must be held. waitok will
960 * influence if memory allocation should wait. Make sure it is 0 if you
964 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
966 struct filterops *fops;
968 struct knote *kn, *tkn;
970 int error, filt, event;
979 fops = kqueue_fo_find(filt);
983 tkn = knote_alloc(waitok); /* prevent waiting with locks */
987 KASSERT(td != NULL, ("td is NULL"));
988 error = fget(td, kev->ident,
989 cap_rights_init(&rights, CAP_POLL_EVENT), &fp);
993 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
994 kev->ident, 0) != 0) {
998 error = kqueue_expand(kq, fops, kev->ident, waitok);
1004 if (fp->f_type == DTYPE_KQUEUE) {
1006 * if we add some inteligence about what we are doing,
1007 * we should be able to support events on ourselves.
1008 * We need to know when we are doing this to prevent
1009 * getting both the knlist lock and the kq lock since
1010 * they are the same thing.
1012 if (fp->f_data == kq) {
1017 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1021 if (kev->ident < kq->kq_knlistsize) {
1022 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
1023 if (kev->filter == kn->kn_filter)
1027 if ((kev->flags & EV_ADD) == EV_ADD)
1028 kqueue_expand(kq, fops, kev->ident, waitok);
1031 if (kq->kq_knhashmask != 0) {
1034 list = &kq->kq_knhash[
1035 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
1036 SLIST_FOREACH(kn, list, kn_link)
1037 if (kev->ident == kn->kn_id &&
1038 kev->filter == kn->kn_filter)
1043 /* knote is in the process of changing, wait for it to stablize. */
1044 if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1045 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1046 kq->kq_state |= KQ_FLUXWAIT;
1047 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
1056 * kn now contains the matching knote, or NULL if no match
1059 if (kev->flags & EV_ADD) {
1071 * apply reference counts to knote structure, and
1072 * do not release it at the end of this routine.
1077 kn->kn_sfflags = kev->fflags;
1078 kn->kn_sdata = kev->data;
1081 kn->kn_kevent = *kev;
1082 kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE |
1083 EV_ENABLE | EV_DISABLE);
1084 kn->kn_status = KN_INFLUX|KN_DETACHED;
1086 error = knote_attach(kn, kq);
1093 if ((error = kn->kn_fop->f_attach(kn)) != 0) {
1100 /* No matching knote and the EV_ADD flag is not set. */
1107 if (kev->flags & EV_DELETE) {
1108 kn->kn_status |= KN_INFLUX;
1110 if (!(kn->kn_status & KN_DETACHED))
1111 kn->kn_fop->f_detach(kn);
1117 * The user may change some filter values after the initial EV_ADD,
1118 * but doing so will not reset any filter which has already been
1121 kn->kn_status |= KN_INFLUX;
1124 kn->kn_kevent.udata = kev->udata;
1125 if (!fops->f_isfd && fops->f_touch != NULL) {
1126 fops->f_touch(kn, kev, EVENT_REGISTER);
1128 kn->kn_sfflags = kev->fflags;
1129 kn->kn_sdata = kev->data;
1133 * We can get here with kn->kn_knlist == NULL. This can happen when
1134 * the initial attach event decides that the event is "completed"
1135 * already. i.e. filt_procattach is called on a zombie process. It
1136 * will call filt_proc which will remove it from the list, and NULL
1140 event = kn->kn_fop->f_event(kn, 0);
1143 KNOTE_ACTIVATE(kn, 1);
1144 kn->kn_status &= ~KN_INFLUX;
1147 if ((kev->flags & EV_DISABLE) &&
1148 ((kn->kn_status & KN_DISABLED) == 0)) {
1149 kn->kn_status |= KN_DISABLED;
1152 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
1153 kn->kn_status &= ~KN_DISABLED;
1154 if ((kn->kn_status & KN_ACTIVE) &&
1155 ((kn->kn_status & KN_QUEUED) == 0))
1161 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1167 kqueue_fo_release(filt);
1172 kqueue_acquire(struct file *fp, struct kqueue **kqp)
1180 if (fp->f_type != DTYPE_KQUEUE || kq == NULL)
1184 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
1195 kqueue_release(struct kqueue *kq, int locked)
1202 if (kq->kq_refcnt == 1)
1203 wakeup(&kq->kq_refcnt);
1209 kqueue_schedtask(struct kqueue *kq)
1213 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1214 ("scheduling kqueue task while draining"));
1216 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1217 taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task);
1218 kq->kq_state |= KQ_TASKSCHED;
1223 * Expand the kq to make sure we have storage for fops/ident pair.
1225 * Return 0 on success (or no work necessary), return errno on failure.
1227 * Not calling hashinit w/ waitok (proper malloc flag) should be safe.
1228 * If kqueue_register is called from a non-fd context, there usually/should
1232 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
1235 struct klist *list, *tmp_knhash, *to_free;
1236 u_long tmp_knhashmask;
1239 int mflag = waitok ? M_WAITOK : M_NOWAIT;
1246 if (kq->kq_knlistsize <= fd) {
1247 size = kq->kq_knlistsize;
1250 list = malloc(size * sizeof(*list), M_KQUEUE, mflag);
1254 if (kq->kq_knlistsize > fd) {
1258 if (kq->kq_knlist != NULL) {
1259 bcopy(kq->kq_knlist, list,
1260 kq->kq_knlistsize * sizeof(*list));
1261 to_free = kq->kq_knlist;
1262 kq->kq_knlist = NULL;
1264 bzero((caddr_t)list +
1265 kq->kq_knlistsize * sizeof(*list),
1266 (size - kq->kq_knlistsize) * sizeof(*list));
1267 kq->kq_knlistsize = size;
1268 kq->kq_knlist = list;
1273 if (kq->kq_knhashmask == 0) {
1274 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1276 if (tmp_knhash == NULL)
1279 if (kq->kq_knhashmask == 0) {
1280 kq->kq_knhash = tmp_knhash;
1281 kq->kq_knhashmask = tmp_knhashmask;
1283 to_free = tmp_knhash;
1288 free(to_free, M_KQUEUE);
1295 kqueue_task(void *arg, int pending)
1303 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1306 KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1308 kq->kq_state &= ~KQ_TASKSCHED;
1309 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1310 wakeup(&kq->kq_state);
1313 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1317 * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1318 * We treat KN_MARKER knotes as if they are INFLUX.
1321 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1322 const struct timespec *tsp, struct kevent *keva, struct thread *td)
1324 struct kevent *kevp;
1325 struct knote *kn, *marker;
1326 sbintime_t asbt, rsbt;
1327 int count, error, haskqglobal, influx, nkev, touch;
1339 if (tsp->tv_sec < 0 || tsp->tv_nsec < 0 ||
1340 tsp->tv_nsec >= 1000000000) {
1344 if (timespecisset(tsp)) {
1345 if (tsp->tv_sec <= INT32_MAX) {
1346 rsbt = tstosbt(*tsp);
1347 if (TIMESEL(&asbt, rsbt))
1348 asbt += tc_tick_sbt;
1349 if (asbt <= INT64_MAX - rsbt)
1353 rsbt >>= tc_precexp;
1360 marker = knote_alloc(1);
1361 if (marker == NULL) {
1365 marker->kn_status = KN_MARKER;
1370 if (kq->kq_count == 0) {
1372 error = EWOULDBLOCK;
1374 kq->kq_state |= KQ_SLEEP;
1375 error = msleep_sbt(kq, &kq->kq_lock, PSOCK | PCATCH,
1376 "kqread", asbt, rsbt, C_ABSOLUTE);
1380 /* don't restart after signals... */
1381 if (error == ERESTART)
1383 else if (error == EWOULDBLOCK)
1388 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1392 kn = TAILQ_FIRST(&kq->kq_head);
1394 if ((kn->kn_status == KN_MARKER && kn != marker) ||
1395 (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1400 kq->kq_state |= KQ_FLUXWAIT;
1401 error = msleep(kq, &kq->kq_lock, PSOCK,
1406 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1407 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1408 kn->kn_status &= ~KN_QUEUED;
1414 if (count == maxevents)
1418 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1419 ("KN_INFLUX set when not suppose to be"));
1421 if ((kn->kn_flags & EV_DROP) == EV_DROP) {
1422 kn->kn_status &= ~KN_QUEUED;
1423 kn->kn_status |= KN_INFLUX;
1427 * We don't need to lock the list since we've marked
1430 if (!(kn->kn_status & KN_DETACHED))
1431 kn->kn_fop->f_detach(kn);
1435 } else if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
1436 kn->kn_status &= ~KN_QUEUED;
1437 kn->kn_status |= KN_INFLUX;
1441 * We don't need to lock the list since we've marked
1444 *kevp = kn->kn_kevent;
1445 if (!(kn->kn_status & KN_DETACHED))
1446 kn->kn_fop->f_detach(kn);
1451 kn->kn_status |= KN_INFLUX;
1453 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
1454 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1456 if (kn->kn_fop->f_event(kn, 0) == 0) {
1458 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1460 ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX);
1466 touch = (!kn->kn_fop->f_isfd &&
1467 kn->kn_fop->f_touch != NULL);
1469 kn->kn_fop->f_touch(kn, kevp, EVENT_PROCESS);
1471 *kevp = kn->kn_kevent;
1473 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1474 if (kn->kn_flags & (EV_CLEAR | EV_DISPATCH)) {
1476 * Manually clear knotes who weren't
1479 if (touch == 0 && kn->kn_flags & EV_CLEAR) {
1483 if (kn->kn_flags & EV_DISPATCH)
1484 kn->kn_status |= KN_DISABLED;
1485 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1488 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1490 kn->kn_status &= ~(KN_INFLUX);
1495 /* we are returning a copy to the user */
1500 if (nkev == KQ_NEVENTS) {
1503 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1511 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1519 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1520 td->td_retval[0] = maxevents - count;
1526 * This could be expanded to call kqueue_scan, if desired.
1530 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1531 int flags, struct thread *td)
1538 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
1539 int flags, struct thread *td)
1546 kqueue_truncate(struct file *fp, off_t length, struct ucred *active_cred,
1555 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
1556 struct ucred *active_cred, struct thread *td)
1559 * Enabling sigio causes two major problems:
1560 * 1) infinite recursion:
1561 * Synopsys: kevent is being used to track signals and have FIOASYNC
1562 * set. On receipt of a signal this will cause a kqueue to recurse
1563 * into itself over and over. Sending the sigio causes the kqueue
1564 * to become ready, which in turn posts sigio again, forever.
1565 * Solution: this can be solved by setting a flag in the kqueue that
1566 * we have a SIGIO in progress.
1567 * 2) locking problems:
1568 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
1569 * us above the proc and pgrp locks.
1570 * Solution: Post a signal using an async mechanism, being sure to
1571 * record a generation count in the delivery so that we do not deliver
1572 * a signal to the wrong process.
1574 * Note, these two mechanisms are somewhat mutually exclusive!
1583 kq->kq_state |= KQ_ASYNC;
1585 kq->kq_state &= ~KQ_ASYNC;
1590 return (fsetown(*(int *)data, &kq->kq_sigio));
1593 *(int *)data = fgetown(&kq->kq_sigio);
1603 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
1610 if ((error = kqueue_acquire(fp, &kq)))
1614 if (events & (POLLIN | POLLRDNORM)) {
1616 revents |= events & (POLLIN | POLLRDNORM);
1618 selrecord(td, &kq->kq_sel);
1619 if (SEL_WAITING(&kq->kq_sel))
1620 kq->kq_state |= KQ_SEL;
1623 kqueue_release(kq, 1);
1630 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1634 bzero((void *)st, sizeof *st);
1636 * We no longer return kq_count because the unlocked value is useless.
1637 * If you spent all this time getting the count, why not spend your
1638 * syscall better by calling kevent?
1640 * XXX - This is needed for libc_r.
1642 st->st_mode = S_IFIFO;
1648 kqueue_close(struct file *fp, struct thread *td)
1650 struct kqueue *kq = fp->f_data;
1651 struct filedesc *fdp;
1656 if ((error = kqueue_acquire(fp, &kq)))
1661 KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
1662 ("kqueue already closing"));
1663 kq->kq_state |= KQ_CLOSING;
1664 if (kq->kq_refcnt > 1)
1665 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
1667 KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
1670 KASSERT(knlist_empty(&kq->kq_sel.si_note),
1671 ("kqueue's knlist not empty"));
1673 for (i = 0; i < kq->kq_knlistsize; i++) {
1674 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
1675 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1676 kq->kq_state |= KQ_FLUXWAIT;
1677 msleep(kq, &kq->kq_lock, PSOCK, "kqclo1", 0);
1680 kn->kn_status |= KN_INFLUX;
1682 if (!(kn->kn_status & KN_DETACHED))
1683 kn->kn_fop->f_detach(kn);
1688 if (kq->kq_knhashmask != 0) {
1689 for (i = 0; i <= kq->kq_knhashmask; i++) {
1690 while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
1691 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1692 kq->kq_state |= KQ_FLUXWAIT;
1693 msleep(kq, &kq->kq_lock, PSOCK,
1697 kn->kn_status |= KN_INFLUX;
1699 if (!(kn->kn_status & KN_DETACHED))
1700 kn->kn_fop->f_detach(kn);
1707 if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
1708 kq->kq_state |= KQ_TASKDRAIN;
1709 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
1712 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1713 selwakeuppri(&kq->kq_sel, PSOCK);
1714 if (!SEL_WAITING(&kq->kq_sel))
1715 kq->kq_state &= ~KQ_SEL;
1720 FILEDESC_XLOCK(fdp);
1721 SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list);
1722 FILEDESC_XUNLOCK(fdp);
1724 seldrain(&kq->kq_sel);
1725 knlist_destroy(&kq->kq_sel.si_note);
1726 mtx_destroy(&kq->kq_lock);
1729 if (kq->kq_knhash != NULL)
1730 free(kq->kq_knhash, M_KQUEUE);
1731 if (kq->kq_knlist != NULL)
1732 free(kq->kq_knlist, M_KQUEUE);
1734 funsetown(&kq->kq_sigio);
1742 kqueue_wakeup(struct kqueue *kq)
1746 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
1747 kq->kq_state &= ~KQ_SLEEP;
1750 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1751 selwakeuppri(&kq->kq_sel, PSOCK);
1752 if (!SEL_WAITING(&kq->kq_sel))
1753 kq->kq_state &= ~KQ_SEL;
1755 if (!knlist_empty(&kq->kq_sel.si_note))
1756 kqueue_schedtask(kq);
1757 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
1758 pgsigio(&kq->kq_sigio, SIGIO, 0);
1763 * Walk down a list of knotes, activating them if their event has triggered.
1765 * There is a possibility to optimize in the case of one kq watching another.
1766 * Instead of scheduling a task to wake it up, you could pass enough state
1767 * down the chain to make up the parent kqueue. Make this code functional
1771 knote(struct knlist *list, long hint, int lockflags)
1780 KNL_ASSERT_LOCK(list, lockflags & KNF_LISTLOCKED);
1782 if ((lockflags & KNF_LISTLOCKED) == 0)
1783 list->kl_lock(list->kl_lockarg);
1786 * If we unlock the list lock (and set KN_INFLUX), we can eliminate
1787 * the kqueue scheduling, but this will introduce four
1788 * lock/unlock's for each knote to test. If we do, continue to use
1789 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is
1790 * only safe if you want to remove the current item, which we are
1793 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
1795 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1797 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1799 } else if ((lockflags & KNF_NOKQLOCK) != 0) {
1800 kn->kn_status |= KN_INFLUX;
1802 error = kn->kn_fop->f_event(kn, hint);
1804 kn->kn_status &= ~KN_INFLUX;
1806 KNOTE_ACTIVATE(kn, 1);
1809 kn->kn_status |= KN_HASKQLOCK;
1810 if (kn->kn_fop->f_event(kn, hint))
1811 KNOTE_ACTIVATE(kn, 1);
1812 kn->kn_status &= ~KN_HASKQLOCK;
1818 if ((lockflags & KNF_LISTLOCKED) == 0)
1819 list->kl_unlock(list->kl_lockarg);
1823 * add a knote to a knlist
1826 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
1828 KNL_ASSERT_LOCK(knl, islocked);
1829 KQ_NOTOWNED(kn->kn_kq);
1830 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) ==
1831 (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED"));
1833 knl->kl_lock(knl->kl_lockarg);
1834 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
1836 knl->kl_unlock(knl->kl_lockarg);
1838 kn->kn_knlist = knl;
1839 kn->kn_status &= ~KN_DETACHED;
1840 KQ_UNLOCK(kn->kn_kq);
1844 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked)
1846 KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked"));
1847 KNL_ASSERT_LOCK(knl, knlislocked);
1848 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
1850 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX,
1851 ("knlist_remove called w/o knote being KN_INFLUX or already removed"));
1853 knl->kl_lock(knl->kl_lockarg);
1854 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
1855 kn->kn_knlist = NULL;
1857 knl->kl_unlock(knl->kl_lockarg);
1860 kn->kn_status |= KN_DETACHED;
1862 KQ_UNLOCK(kn->kn_kq);
1866 * remove knote from the specified knlist
1869 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
1872 knlist_remove_kq(knl, kn, islocked, 0);
1876 * remove knote from the specified knlist while in f_event handler.
1879 knlist_remove_inevent(struct knlist *knl, struct knote *kn)
1882 knlist_remove_kq(knl, kn, 1,
1883 (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK);
1887 knlist_empty(struct knlist *knl)
1890 KNL_ASSERT_LOCKED(knl);
1891 return SLIST_EMPTY(&knl->kl_list);
1894 static struct mtx knlist_lock;
1895 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
1897 static void knlist_mtx_lock(void *arg);
1898 static void knlist_mtx_unlock(void *arg);
1901 knlist_mtx_lock(void *arg)
1904 mtx_lock((struct mtx *)arg);
1908 knlist_mtx_unlock(void *arg)
1911 mtx_unlock((struct mtx *)arg);
1915 knlist_mtx_assert_locked(void *arg)
1918 mtx_assert((struct mtx *)arg, MA_OWNED);
1922 knlist_mtx_assert_unlocked(void *arg)
1925 mtx_assert((struct mtx *)arg, MA_NOTOWNED);
1929 knlist_rw_rlock(void *arg)
1932 rw_rlock((struct rwlock *)arg);
1936 knlist_rw_runlock(void *arg)
1939 rw_runlock((struct rwlock *)arg);
1943 knlist_rw_assert_locked(void *arg)
1946 rw_assert((struct rwlock *)arg, RA_LOCKED);
1950 knlist_rw_assert_unlocked(void *arg)
1953 rw_assert((struct rwlock *)arg, RA_UNLOCKED);
1957 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
1958 void (*kl_unlock)(void *),
1959 void (*kl_assert_locked)(void *), void (*kl_assert_unlocked)(void *))
1963 knl->kl_lockarg = &knlist_lock;
1965 knl->kl_lockarg = lock;
1967 if (kl_lock == NULL)
1968 knl->kl_lock = knlist_mtx_lock;
1970 knl->kl_lock = kl_lock;
1971 if (kl_unlock == NULL)
1972 knl->kl_unlock = knlist_mtx_unlock;
1974 knl->kl_unlock = kl_unlock;
1975 if (kl_assert_locked == NULL)
1976 knl->kl_assert_locked = knlist_mtx_assert_locked;
1978 knl->kl_assert_locked = kl_assert_locked;
1979 if (kl_assert_unlocked == NULL)
1980 knl->kl_assert_unlocked = knlist_mtx_assert_unlocked;
1982 knl->kl_assert_unlocked = kl_assert_unlocked;
1984 SLIST_INIT(&knl->kl_list);
1988 knlist_init_mtx(struct knlist *knl, struct mtx *lock)
1991 knlist_init(knl, lock, NULL, NULL, NULL, NULL);
1995 knlist_init_rw_reader(struct knlist *knl, struct rwlock *lock)
1998 knlist_init(knl, lock, knlist_rw_rlock, knlist_rw_runlock,
1999 knlist_rw_assert_locked, knlist_rw_assert_unlocked);
2003 knlist_destroy(struct knlist *knl)
2008 * if we run across this error, we need to find the offending
2009 * driver and have it call knlist_clear or knlist_delete.
2011 if (!SLIST_EMPTY(&knl->kl_list))
2012 printf("WARNING: destroying knlist w/ knotes on it!\n");
2015 knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL;
2016 SLIST_INIT(&knl->kl_list);
2020 * Even if we are locked, we may need to drop the lock to allow any influx
2021 * knotes time to "settle".
2024 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
2026 struct knote *kn, *kn2;
2030 KNL_ASSERT_LOCKED(knl);
2032 KNL_ASSERT_UNLOCKED(knl);
2033 again: /* need to reacquire lock since we have dropped it */
2034 knl->kl_lock(knl->kl_lockarg);
2037 SLIST_FOREACH_SAFE(kn, &knl->kl_list, kn_selnext, kn2) {
2040 if ((kn->kn_status & KN_INFLUX)) {
2044 knlist_remove_kq(knl, kn, 1, 1);
2046 kn->kn_status |= KN_INFLUX | KN_DETACHED;
2050 /* Make sure cleared knotes disappear soon */
2051 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2057 if (!SLIST_EMPTY(&knl->kl_list)) {
2058 /* there are still KN_INFLUX remaining */
2059 kn = SLIST_FIRST(&knl->kl_list);
2062 KASSERT(kn->kn_status & KN_INFLUX,
2063 ("knote removed w/o list lock"));
2064 knl->kl_unlock(knl->kl_lockarg);
2065 kq->kq_state |= KQ_FLUXWAIT;
2066 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
2072 KNL_ASSERT_LOCKED(knl);
2074 knl->kl_unlock(knl->kl_lockarg);
2075 KNL_ASSERT_UNLOCKED(knl);
2080 * Remove all knotes referencing a specified fd must be called with FILEDESC
2081 * lock. This prevents a race where a new fd comes along and occupies the
2082 * entry and we attach a knote to the fd.
2085 knote_fdclose(struct thread *td, int fd)
2087 struct filedesc *fdp = td->td_proc->p_fd;
2092 FILEDESC_XLOCK_ASSERT(fdp);
2095 * We shouldn't have to worry about new kevents appearing on fd
2096 * since filedesc is locked.
2098 SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
2103 while (kq->kq_knlistsize > fd &&
2104 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
2105 if (kn->kn_status & KN_INFLUX) {
2106 /* someone else might be waiting on our knote */
2109 kq->kq_state |= KQ_FLUXWAIT;
2110 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
2113 kn->kn_status |= KN_INFLUX;
2115 if (!(kn->kn_status & KN_DETACHED))
2116 kn->kn_fop->f_detach(kn);
2126 knote_attach(struct knote *kn, struct kqueue *kq)
2130 KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX"));
2133 if (kn->kn_fop->f_isfd) {
2134 if (kn->kn_id >= kq->kq_knlistsize)
2136 list = &kq->kq_knlist[kn->kn_id];
2138 if (kq->kq_knhash == NULL)
2140 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2143 SLIST_INSERT_HEAD(list, kn, kn_link);
2149 * knote must already have been detached using the f_detach method.
2150 * no lock need to be held, it is assumed that the KN_INFLUX flag is set
2151 * to prevent other removal.
2154 knote_drop(struct knote *kn, struct thread *td)
2162 KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX,
2163 ("knote_drop called without KN_INFLUX set in kn_status"));
2166 if (kn->kn_fop->f_isfd)
2167 list = &kq->kq_knlist[kn->kn_id];
2169 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2171 if (!SLIST_EMPTY(list))
2172 SLIST_REMOVE(list, kn, knote, kn_link);
2173 if (kn->kn_status & KN_QUEUED)
2177 if (kn->kn_fop->f_isfd) {
2178 fdrop(kn->kn_fp, td);
2181 kqueue_fo_release(kn->kn_kevent.filter);
2187 knote_enqueue(struct knote *kn)
2189 struct kqueue *kq = kn->kn_kq;
2191 KQ_OWNED(kn->kn_kq);
2192 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
2194 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
2195 kn->kn_status |= KN_QUEUED;
2201 knote_dequeue(struct knote *kn)
2203 struct kqueue *kq = kn->kn_kq;
2205 KQ_OWNED(kn->kn_kq);
2206 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
2208 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
2209 kn->kn_status &= ~KN_QUEUED;
2217 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
2218 NULL, NULL, UMA_ALIGN_PTR, 0);
2220 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL);
2222 static struct knote *
2223 knote_alloc(int waitok)
2225 return ((struct knote *)uma_zalloc(knote_zone,
2226 (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO));
2230 knote_free(struct knote *kn)
2233 uma_zfree(knote_zone, kn);
2237 * Register the kev w/ the kq specified by fd.
2240 kqfd_register(int fd, struct kevent *kev, struct thread *td, int waitok)
2244 cap_rights_t rights;
2247 error = fget(td, fd, cap_rights_init(&rights, CAP_POST_EVENT), &fp);
2250 if ((error = kqueue_acquire(fp, &kq)) != 0)
2253 error = kqueue_register(kq, kev, td, waitok);
2255 kqueue_release(kq, 0);