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>
41 #include <sys/malloc.h>
42 #include <sys/unistd.h>
44 #include <sys/filedesc.h>
45 #include <sys/filio.h>
46 #include <sys/fcntl.h>
47 #include <sys/kthread.h>
48 #include <sys/selinfo.h>
49 #include <sys/queue.h>
50 #include <sys/event.h>
51 #include <sys/eventvar.h>
53 #include <sys/protosw.h>
54 #include <sys/sigio.h>
55 #include <sys/signalvar.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysproto.h>
61 #include <sys/syscallsubr.h>
62 #include <sys/taskqueue.h>
65 #include <sys/ktrace.h>
70 static MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
73 * This lock is used if multiple kq locks are required. This possibly
74 * should be made into a per proc lock.
76 static struct mtx kq_global;
77 MTX_SYSINIT(kq_global, &kq_global, "kqueue order", MTX_DEF);
78 #define KQ_GLOBAL_LOCK(lck, haslck) do { \
83 #define KQ_GLOBAL_UNLOCK(lck, haslck) do { \
89 TASKQUEUE_DEFINE_THREAD(kqueue);
91 static int kevent_copyout(void *arg, struct kevent *kevp, int count);
92 static int kevent_copyin(void *arg, struct kevent *kevp, int count);
93 static int kqueue_register(struct kqueue *kq, struct kevent *kev,
94 struct thread *td, int waitok);
95 static int kqueue_acquire(struct file *fp, struct kqueue **kqp);
96 static void kqueue_release(struct kqueue *kq, int locked);
97 static int kqueue_expand(struct kqueue *kq, struct filterops *fops,
98 uintptr_t ident, int waitok);
99 static void kqueue_task(void *arg, int pending);
100 static int kqueue_scan(struct kqueue *kq, int maxevents,
101 struct kevent_copyops *k_ops,
102 const struct timespec *timeout,
103 struct kevent *keva, struct thread *td);
104 static void kqueue_wakeup(struct kqueue *kq);
105 static struct filterops *kqueue_fo_find(int filt);
106 static void kqueue_fo_release(int filt);
108 static fo_rdwr_t kqueue_read;
109 static fo_rdwr_t kqueue_write;
110 static fo_truncate_t kqueue_truncate;
111 static fo_ioctl_t kqueue_ioctl;
112 static fo_poll_t kqueue_poll;
113 static fo_kqfilter_t kqueue_kqfilter;
114 static fo_stat_t kqueue_stat;
115 static fo_close_t kqueue_close;
117 static struct fileops kqueueops = {
118 .fo_read = kqueue_read,
119 .fo_write = kqueue_write,
120 .fo_truncate = kqueue_truncate,
121 .fo_ioctl = kqueue_ioctl,
122 .fo_poll = kqueue_poll,
123 .fo_kqfilter = kqueue_kqfilter,
124 .fo_stat = kqueue_stat,
125 .fo_close = kqueue_close,
126 .fo_chmod = invfo_chmod,
127 .fo_chown = invfo_chown,
130 static int knote_attach(struct knote *kn, struct kqueue *kq);
131 static void knote_drop(struct knote *kn, struct thread *td);
132 static void knote_enqueue(struct knote *kn);
133 static void knote_dequeue(struct knote *kn);
134 static void knote_init(void);
135 static struct knote *knote_alloc(int waitok);
136 static void knote_free(struct knote *kn);
138 static void filt_kqdetach(struct knote *kn);
139 static int filt_kqueue(struct knote *kn, long hint);
140 static int filt_procattach(struct knote *kn);
141 static void filt_procdetach(struct knote *kn);
142 static int filt_proc(struct knote *kn, long hint);
143 static int filt_fileattach(struct knote *kn);
144 static void filt_timerexpire(void *knx);
145 static int filt_timerattach(struct knote *kn);
146 static void filt_timerdetach(struct knote *kn);
147 static int filt_timer(struct knote *kn, long hint);
148 static int filt_userattach(struct knote *kn);
149 static void filt_userdetach(struct knote *kn);
150 static int filt_user(struct knote *kn, long hint);
151 static void filt_usertouch(struct knote *kn, struct kevent *kev,
154 static struct filterops file_filtops = {
156 .f_attach = filt_fileattach,
158 static struct filterops kqread_filtops = {
160 .f_detach = filt_kqdetach,
161 .f_event = filt_kqueue,
163 /* XXX - move to kern_proc.c? */
164 static struct filterops proc_filtops = {
166 .f_attach = filt_procattach,
167 .f_detach = filt_procdetach,
168 .f_event = filt_proc,
170 static struct filterops timer_filtops = {
172 .f_attach = filt_timerattach,
173 .f_detach = filt_timerdetach,
174 .f_event = filt_timer,
176 static struct filterops user_filtops = {
177 .f_attach = filt_userattach,
178 .f_detach = filt_userdetach,
179 .f_event = filt_user,
180 .f_touch = filt_usertouch,
183 static uma_zone_t knote_zone;
184 static int kq_ncallouts = 0;
185 static int kq_calloutmax = (4 * 1024);
186 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
187 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
189 /* XXX - ensure not KN_INFLUX?? */
190 #define KNOTE_ACTIVATE(kn, islock) do { \
192 mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED); \
194 KQ_LOCK((kn)->kn_kq); \
195 (kn)->kn_status |= KN_ACTIVE; \
196 if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
197 knote_enqueue((kn)); \
199 KQ_UNLOCK((kn)->kn_kq); \
201 #define KQ_LOCK(kq) do { \
202 mtx_lock(&(kq)->kq_lock); \
204 #define KQ_FLUX_WAKEUP(kq) do { \
205 if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) { \
206 (kq)->kq_state &= ~KQ_FLUXWAIT; \
210 #define KQ_UNLOCK_FLUX(kq) do { \
211 KQ_FLUX_WAKEUP(kq); \
212 mtx_unlock(&(kq)->kq_lock); \
214 #define KQ_UNLOCK(kq) do { \
215 mtx_unlock(&(kq)->kq_lock); \
217 #define KQ_OWNED(kq) do { \
218 mtx_assert(&(kq)->kq_lock, MA_OWNED); \
220 #define KQ_NOTOWNED(kq) do { \
221 mtx_assert(&(kq)->kq_lock, MA_NOTOWNED); \
223 #define KN_LIST_LOCK(kn) do { \
224 if (kn->kn_knlist != NULL) \
225 kn->kn_knlist->kl_lock(kn->kn_knlist->kl_lockarg); \
227 #define KN_LIST_UNLOCK(kn) do { \
228 if (kn->kn_knlist != NULL) \
229 kn->kn_knlist->kl_unlock(kn->kn_knlist->kl_lockarg); \
231 #define KNL_ASSERT_LOCK(knl, islocked) do { \
233 KNL_ASSERT_LOCKED(knl); \
235 KNL_ASSERT_UNLOCKED(knl); \
238 #define KNL_ASSERT_LOCKED(knl) do { \
239 knl->kl_assert_locked((knl)->kl_lockarg); \
241 #define KNL_ASSERT_UNLOCKED(knl) do { \
242 knl->kl_assert_unlocked((knl)->kl_lockarg); \
244 #else /* !INVARIANTS */
245 #define KNL_ASSERT_LOCKED(knl) do {} while(0)
246 #define KNL_ASSERT_UNLOCKED(knl) do {} while (0)
247 #endif /* INVARIANTS */
249 #define KN_HASHSIZE 64 /* XXX should be tunable */
250 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
253 filt_nullattach(struct knote *kn)
259 struct filterops null_filtops = {
261 .f_attach = filt_nullattach,
264 /* XXX - make SYSINIT to add these, and move into respective modules. */
265 extern struct filterops sig_filtops;
266 extern struct filterops fs_filtops;
269 * Table for for all system-defined filters.
271 static struct mtx filterops_lock;
272 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops",
275 struct filterops *for_fop;
277 } sysfilt_ops[EVFILT_SYSCOUNT] = {
278 { &file_filtops }, /* EVFILT_READ */
279 { &file_filtops }, /* EVFILT_WRITE */
280 { &null_filtops }, /* EVFILT_AIO */
281 { &file_filtops }, /* EVFILT_VNODE */
282 { &proc_filtops }, /* EVFILT_PROC */
283 { &sig_filtops }, /* EVFILT_SIGNAL */
284 { &timer_filtops }, /* EVFILT_TIMER */
285 { &null_filtops }, /* former EVFILT_NETDEV */
286 { &fs_filtops }, /* EVFILT_FS */
287 { &null_filtops }, /* EVFILT_LIO */
288 { &user_filtops }, /* EVFILT_USER */
292 * Simple redirection for all cdevsw style objects to call their fo_kqfilter
296 filt_fileattach(struct knote *kn)
299 return (fo_kqfilter(kn->kn_fp, kn));
304 kqueue_kqfilter(struct file *fp, struct knote *kn)
306 struct kqueue *kq = kn->kn_fp->f_data;
308 if (kn->kn_filter != EVFILT_READ)
311 kn->kn_status |= KN_KQUEUE;
312 kn->kn_fop = &kqread_filtops;
313 knlist_add(&kq->kq_sel.si_note, kn, 0);
319 filt_kqdetach(struct knote *kn)
321 struct kqueue *kq = kn->kn_fp->f_data;
323 knlist_remove(&kq->kq_sel.si_note, kn, 0);
328 filt_kqueue(struct knote *kn, long hint)
330 struct kqueue *kq = kn->kn_fp->f_data;
332 kn->kn_data = kq->kq_count;
333 return (kn->kn_data > 0);
336 /* XXX - move to kern_proc.c? */
338 filt_procattach(struct knote *kn)
345 p = pfind(kn->kn_id);
346 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
347 p = zpfind(kn->kn_id);
349 } else if (p != NULL && (p->p_flag & P_WEXIT)) {
355 if ((error = p_cansee(curthread, p))) {
360 kn->kn_ptr.p_proc = p;
361 kn->kn_flags |= EV_CLEAR; /* automatically set */
364 * internal flag indicating registration done by kernel
366 if (kn->kn_flags & EV_FLAG1) {
367 kn->kn_data = kn->kn_sdata; /* ppid */
368 kn->kn_fflags = NOTE_CHILD;
369 kn->kn_flags &= ~EV_FLAG1;
373 knlist_add(&p->p_klist, kn, 1);
376 * Immediately activate any exit notes if the target process is a
377 * zombie. This is necessary to handle the case where the target
378 * process, e.g. a child, dies before the kevent is registered.
380 if (immediate && filt_proc(kn, NOTE_EXIT))
381 KNOTE_ACTIVATE(kn, 0);
389 * The knote may be attached to a different process, which may exit,
390 * leaving nothing for the knote to be attached to. So when the process
391 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
392 * it will be deleted when read out. However, as part of the knote deletion,
393 * this routine is called, so a check is needed to avoid actually performing
394 * a detach, because the original process does not exist any more.
396 /* XXX - move to kern_proc.c? */
398 filt_procdetach(struct knote *kn)
402 p = kn->kn_ptr.p_proc;
403 knlist_remove(&p->p_klist, kn, 0);
404 kn->kn_ptr.p_proc = NULL;
407 /* XXX - move to kern_proc.c? */
409 filt_proc(struct knote *kn, long hint)
411 struct proc *p = kn->kn_ptr.p_proc;
415 * mask off extra data
417 event = (u_int)hint & NOTE_PCTRLMASK;
420 * if the user is interested in this event, record it.
422 if (kn->kn_sfflags & event)
423 kn->kn_fflags |= event;
426 * process is gone, so flag the event as finished.
428 if (event == NOTE_EXIT) {
429 if (!(kn->kn_status & KN_DETACHED))
430 knlist_remove_inevent(&p->p_klist, kn);
431 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
432 kn->kn_ptr.p_proc = NULL;
433 if (kn->kn_fflags & NOTE_EXIT)
434 kn->kn_data = p->p_xstat;
435 if (kn->kn_fflags == 0)
436 kn->kn_flags |= EV_DROP;
440 return (kn->kn_fflags != 0);
444 * Called when the process forked. It mostly does the same as the
445 * knote(), activating all knotes registered to be activated when the
446 * process forked. Additionally, for each knote attached to the
447 * parent, check whether user wants to track the new process. If so
448 * attach a new knote to it, and immediately report an event with the
452 knote_fork(struct knlist *list, int pid)
461 list->kl_lock(list->kl_lockarg);
463 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
464 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX)
468 if ((kn->kn_status & (KN_INFLUX | KN_SCAN)) == KN_INFLUX) {
474 * The same as knote(), activate the event.
476 if ((kn->kn_sfflags & NOTE_TRACK) == 0) {
477 kn->kn_status |= KN_HASKQLOCK;
478 if (kn->kn_fop->f_event(kn, NOTE_FORK))
479 KNOTE_ACTIVATE(kn, 1);
480 kn->kn_status &= ~KN_HASKQLOCK;
486 * The NOTE_TRACK case. In addition to the activation
487 * of the event, we need to register new event to
488 * track the child. Drop the locks in preparation for
489 * the call to kqueue_register().
491 kn->kn_status |= KN_INFLUX;
493 list->kl_unlock(list->kl_lockarg);
496 * Activate existing knote and register a knote with
500 kev.filter = kn->kn_filter;
501 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
502 kev.fflags = kn->kn_sfflags;
503 kev.data = kn->kn_id; /* parent */
504 kev.udata = kn->kn_kevent.udata;/* preserve udata */
505 error = kqueue_register(kq, &kev, NULL, 0);
507 kn->kn_fflags |= NOTE_TRACKERR;
508 if (kn->kn_fop->f_event(kn, NOTE_FORK))
509 KNOTE_ACTIVATE(kn, 0);
511 kn->kn_status &= ~KN_INFLUX;
513 list->kl_lock(list->kl_lockarg);
515 list->kl_unlock(list->kl_lockarg);
519 * XXX: EVFILT_TIMER should perhaps live in kern_time.c beside the
520 * interval timer support code.
523 timertoticks(intptr_t data)
528 tv.tv_sec = data / 1000;
529 tv.tv_usec = (data % 1000) * 1000;
530 tticks = tvtohz(&tv);
536 filt_timerexpire(void *knx)
538 struct knote *kn = knx;
539 struct callout *calloutp;
542 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */
545 * timertoticks() uses tvtohz() which always adds 1 to allow
546 * for the time until the next clock interrupt being strictly
547 * less than 1 clock tick. We don't want that here since we
548 * want to appear to be in sync with the clock interrupt even
549 * when we're delayed.
551 if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) {
552 calloutp = (struct callout *)kn->kn_hook;
553 callout_reset_curcpu(calloutp, timertoticks(kn->kn_sdata) - 1,
554 filt_timerexpire, kn);
559 * data contains amount of time to sleep, in milliseconds
562 filt_timerattach(struct knote *kn)
564 struct callout *calloutp;
566 atomic_add_int(&kq_ncallouts, 1);
568 if (kq_ncallouts >= kq_calloutmax) {
569 atomic_add_int(&kq_ncallouts, -1);
573 kn->kn_flags |= EV_CLEAR; /* automatically set */
574 kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */
575 calloutp = malloc(sizeof(*calloutp), M_KQUEUE, M_WAITOK);
576 callout_init(calloutp, CALLOUT_MPSAFE);
577 kn->kn_hook = calloutp;
578 callout_reset_curcpu(calloutp, timertoticks(kn->kn_sdata),
579 filt_timerexpire, kn);
585 filt_timerdetach(struct knote *kn)
587 struct callout *calloutp;
589 calloutp = (struct callout *)kn->kn_hook;
590 callout_drain(calloutp);
591 free(calloutp, M_KQUEUE);
592 atomic_add_int(&kq_ncallouts, -1);
593 kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */
597 filt_timer(struct knote *kn, long hint)
600 return (kn->kn_data != 0);
604 filt_userattach(struct knote *kn)
608 * EVFILT_USER knotes are not attached to anything in the kernel.
611 if (kn->kn_fflags & NOTE_TRIGGER)
619 filt_userdetach(__unused struct knote *kn)
623 * EVFILT_USER knotes are not attached to anything in the kernel.
628 filt_user(struct knote *kn, __unused long hint)
631 return (kn->kn_hookid);
635 filt_usertouch(struct knote *kn, struct kevent *kev, u_long type)
641 if (kev->fflags & NOTE_TRIGGER)
644 ffctrl = kev->fflags & NOTE_FFCTRLMASK;
645 kev->fflags &= NOTE_FFLAGSMASK;
651 kn->kn_sfflags &= kev->fflags;
655 kn->kn_sfflags |= kev->fflags;
659 kn->kn_sfflags = kev->fflags;
663 /* XXX Return error? */
666 kn->kn_sdata = kev->data;
667 if (kev->flags & EV_CLEAR) {
675 *kev = kn->kn_kevent;
676 kev->fflags = kn->kn_sfflags;
677 kev->data = kn->kn_sdata;
678 if (kn->kn_flags & EV_CLEAR) {
686 panic("filt_usertouch() - invalid type (%ld)", type);
692 sys_kqueue(struct thread *td, struct kqueue_args *uap)
694 struct filedesc *fdp;
699 fdp = td->td_proc->p_fd;
700 error = falloc(td, &fp, &fd, 0);
704 /* An extra reference on `nfp' has been held for us by falloc(). */
705 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
706 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK);
707 TAILQ_INIT(&kq->kq_head);
709 knlist_init_mtx(&kq->kq_sel.si_note, &kq->kq_lock);
710 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
713 TAILQ_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
714 FILEDESC_XUNLOCK(fdp);
716 finit(fp, FREAD | FWRITE, DTYPE_KQUEUE, kq, &kqueueops);
719 td->td_retval[0] = fd;
724 #ifndef _SYS_SYSPROTO_H_
727 const struct kevent *changelist;
729 struct kevent *eventlist;
731 const struct timespec *timeout;
735 sys_kevent(struct thread *td, struct kevent_args *uap)
737 struct timespec ts, *tsp;
738 struct kevent_copyops k_ops = { uap,
745 struct uio *ktruioin = NULL;
746 struct uio *ktruioout = NULL;
749 if (uap->timeout != NULL) {
750 error = copyin(uap->timeout, &ts, sizeof(ts));
758 if (KTRPOINT(td, KTR_GENIO)) {
759 ktriov.iov_base = uap->changelist;
760 ktriov.iov_len = uap->nchanges * sizeof(struct kevent);
761 ktruio = (struct uio){ .uio_iov = &ktriov, .uio_iovcnt = 1,
762 .uio_segflg = UIO_USERSPACE, .uio_rw = UIO_READ,
764 ktruioin = cloneuio(&ktruio);
765 ktriov.iov_base = uap->eventlist;
766 ktriov.iov_len = uap->nevents * sizeof(struct kevent);
767 ktruioout = cloneuio(&ktruio);
771 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
775 if (ktruioin != NULL) {
776 ktruioin->uio_resid = uap->nchanges * sizeof(struct kevent);
777 ktrgenio(uap->fd, UIO_WRITE, ktruioin, 0);
778 ktruioout->uio_resid = td->td_retval[0] * sizeof(struct kevent);
779 ktrgenio(uap->fd, UIO_READ, ktruioout, error);
787 * Copy 'count' items into the destination list pointed to by uap->eventlist.
790 kevent_copyout(void *arg, struct kevent *kevp, int count)
792 struct kevent_args *uap;
795 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
796 uap = (struct kevent_args *)arg;
798 error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
800 uap->eventlist += count;
805 * Copy 'count' items from the list pointed to by uap->changelist.
808 kevent_copyin(void *arg, struct kevent *kevp, int count)
810 struct kevent_args *uap;
813 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
814 uap = (struct kevent_args *)arg;
816 error = copyin(uap->changelist, kevp, count * sizeof *kevp);
818 uap->changelist += count;
823 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
824 struct kevent_copyops *k_ops, const struct timespec *timeout)
826 struct kevent keva[KQ_NEVENTS];
827 struct kevent *kevp, *changes;
830 int i, n, nerrors, error;
832 if ((error = fget(td, fd, CAP_POST_EVENT, &fp)) != 0)
834 if ((error = kqueue_acquire(fp, &kq)) != 0)
839 while (nchanges > 0) {
840 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
841 error = k_ops->k_copyin(k_ops->arg, keva, n);
845 for (i = 0; i < n; i++) {
849 kevp->flags &= ~EV_SYSFLAGS;
850 error = kqueue_register(kq, kevp, td, 1);
851 if (error || (kevp->flags & EV_RECEIPT)) {
853 kevp->flags = EV_ERROR;
855 (void) k_ops->k_copyout(k_ops->arg,
867 td->td_retval[0] = nerrors;
872 error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td);
874 kqueue_release(kq, 0);
881 kqueue_add_filteropts(int filt, struct filterops *filtops)
886 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
888 "trying to add a filterop that is out of range: %d is beyond %d\n",
889 ~filt, EVFILT_SYSCOUNT);
892 mtx_lock(&filterops_lock);
893 if (sysfilt_ops[~filt].for_fop != &null_filtops &&
894 sysfilt_ops[~filt].for_fop != NULL)
897 sysfilt_ops[~filt].for_fop = filtops;
898 sysfilt_ops[~filt].for_refcnt = 0;
900 mtx_unlock(&filterops_lock);
906 kqueue_del_filteropts(int filt)
911 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
914 mtx_lock(&filterops_lock);
915 if (sysfilt_ops[~filt].for_fop == &null_filtops ||
916 sysfilt_ops[~filt].for_fop == NULL)
918 else if (sysfilt_ops[~filt].for_refcnt != 0)
921 sysfilt_ops[~filt].for_fop = &null_filtops;
922 sysfilt_ops[~filt].for_refcnt = 0;
924 mtx_unlock(&filterops_lock);
929 static struct filterops *
930 kqueue_fo_find(int filt)
933 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
936 mtx_lock(&filterops_lock);
937 sysfilt_ops[~filt].for_refcnt++;
938 if (sysfilt_ops[~filt].for_fop == NULL)
939 sysfilt_ops[~filt].for_fop = &null_filtops;
940 mtx_unlock(&filterops_lock);
942 return sysfilt_ops[~filt].for_fop;
946 kqueue_fo_release(int filt)
949 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
952 mtx_lock(&filterops_lock);
953 KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
954 ("filter object refcount not valid on release"));
955 sysfilt_ops[~filt].for_refcnt--;
956 mtx_unlock(&filterops_lock);
960 * A ref to kq (obtained via kqueue_acquire) must be held. waitok will
961 * influence if memory allocation should wait. Make sure it is 0 if you
965 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
967 struct filterops *fops;
969 struct knote *kn, *tkn;
970 int error, filt, event;
971 int haskqglobal, filedesc_unlock;
980 fops = kqueue_fo_find(filt);
984 tkn = knote_alloc(waitok); /* prevent waiting with locks */
988 KASSERT(td != NULL, ("td is NULL"));
989 error = fget(td, kev->ident, 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) {
1018 * Pre-lock the filedesc before the global
1019 * lock mutex, see the comment in
1022 FILEDESC_XLOCK(td->td_proc->p_fd);
1023 filedesc_unlock = 1;
1024 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1028 if (kev->ident < kq->kq_knlistsize) {
1029 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
1030 if (kev->filter == kn->kn_filter)
1034 if ((kev->flags & EV_ADD) == EV_ADD)
1035 kqueue_expand(kq, fops, kev->ident, waitok);
1038 if (kq->kq_knhashmask != 0) {
1041 list = &kq->kq_knhash[
1042 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
1043 SLIST_FOREACH(kn, list, kn_link)
1044 if (kev->ident == kn->kn_id &&
1045 kev->filter == kn->kn_filter)
1050 /* knote is in the process of changing, wait for it to stablize. */
1051 if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1052 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1053 if (filedesc_unlock) {
1054 FILEDESC_XUNLOCK(td->td_proc->p_fd);
1055 filedesc_unlock = 0;
1057 kq->kq_state |= KQ_FLUXWAIT;
1058 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
1067 * kn now contains the matching knote, or NULL if no match
1070 if (kev->flags & EV_ADD) {
1082 * apply reference counts to knote structure, and
1083 * do not release it at the end of this routine.
1088 kn->kn_sfflags = kev->fflags;
1089 kn->kn_sdata = kev->data;
1092 kn->kn_kevent = *kev;
1093 kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE |
1094 EV_ENABLE | EV_DISABLE);
1095 kn->kn_status = KN_INFLUX|KN_DETACHED;
1097 error = knote_attach(kn, kq);
1104 if ((error = kn->kn_fop->f_attach(kn)) != 0) {
1111 /* No matching knote and the EV_ADD flag is not set. */
1118 if (kev->flags & EV_DELETE) {
1119 kn->kn_status |= KN_INFLUX;
1121 if (!(kn->kn_status & KN_DETACHED))
1122 kn->kn_fop->f_detach(kn);
1128 * The user may change some filter values after the initial EV_ADD,
1129 * but doing so will not reset any filter which has already been
1132 kn->kn_status |= KN_INFLUX | KN_SCAN;
1135 kn->kn_kevent.udata = kev->udata;
1136 if (!fops->f_isfd && fops->f_touch != NULL) {
1137 fops->f_touch(kn, kev, EVENT_REGISTER);
1139 kn->kn_sfflags = kev->fflags;
1140 kn->kn_sdata = kev->data;
1144 * We can get here with kn->kn_knlist == NULL. This can happen when
1145 * the initial attach event decides that the event is "completed"
1146 * already. i.e. filt_procattach is called on a zombie process. It
1147 * will call filt_proc which will remove it from the list, and NULL
1151 event = kn->kn_fop->f_event(kn, 0);
1154 KNOTE_ACTIVATE(kn, 1);
1155 kn->kn_status &= ~(KN_INFLUX | KN_SCAN);
1158 if ((kev->flags & EV_DISABLE) &&
1159 ((kn->kn_status & KN_DISABLED) == 0)) {
1160 kn->kn_status |= KN_DISABLED;
1163 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
1164 kn->kn_status &= ~KN_DISABLED;
1165 if ((kn->kn_status & KN_ACTIVE) &&
1166 ((kn->kn_status & KN_QUEUED) == 0))
1172 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1173 if (filedesc_unlock)
1174 FILEDESC_XUNLOCK(td->td_proc->p_fd);
1180 kqueue_fo_release(filt);
1185 kqueue_acquire(struct file *fp, struct kqueue **kqp)
1193 if (fp->f_type != DTYPE_KQUEUE || kq == NULL)
1197 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
1208 kqueue_release(struct kqueue *kq, int locked)
1215 if (kq->kq_refcnt == 1)
1216 wakeup(&kq->kq_refcnt);
1222 kqueue_schedtask(struct kqueue *kq)
1226 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1227 ("scheduling kqueue task while draining"));
1229 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1230 taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task);
1231 kq->kq_state |= KQ_TASKSCHED;
1236 * Expand the kq to make sure we have storage for fops/ident pair.
1238 * Return 0 on success (or no work necessary), return errno on failure.
1240 * Not calling hashinit w/ waitok (proper malloc flag) should be safe.
1241 * If kqueue_register is called from a non-fd context, there usually/should
1245 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
1248 struct klist *list, *tmp_knhash, *to_free;
1249 u_long tmp_knhashmask;
1252 int mflag = waitok ? M_WAITOK : M_NOWAIT;
1259 if (kq->kq_knlistsize <= fd) {
1260 size = kq->kq_knlistsize;
1263 list = malloc(size * sizeof(*list), M_KQUEUE, mflag);
1267 if (kq->kq_knlistsize > fd) {
1271 if (kq->kq_knlist != NULL) {
1272 bcopy(kq->kq_knlist, list,
1273 kq->kq_knlistsize * sizeof(*list));
1274 to_free = kq->kq_knlist;
1275 kq->kq_knlist = NULL;
1277 bzero((caddr_t)list +
1278 kq->kq_knlistsize * sizeof(*list),
1279 (size - kq->kq_knlistsize) * sizeof(*list));
1280 kq->kq_knlistsize = size;
1281 kq->kq_knlist = list;
1286 if (kq->kq_knhashmask == 0) {
1287 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1289 if (tmp_knhash == NULL)
1292 if (kq->kq_knhashmask == 0) {
1293 kq->kq_knhash = tmp_knhash;
1294 kq->kq_knhashmask = tmp_knhashmask;
1296 to_free = tmp_knhash;
1301 free(to_free, M_KQUEUE);
1308 kqueue_task(void *arg, int pending)
1316 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1319 KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1321 kq->kq_state &= ~KQ_TASKSCHED;
1322 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1323 wakeup(&kq->kq_state);
1326 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1330 * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1331 * We treat KN_MARKER knotes as if they are INFLUX.
1334 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1335 const struct timespec *tsp, struct kevent *keva, struct thread *td)
1337 struct kevent *kevp;
1338 struct timeval atv, rtv, ttv;
1339 struct knote *kn, *marker;
1340 int count, timeout, nkev, error, influx;
1341 int haskqglobal, touch;
1352 TIMESPEC_TO_TIMEVAL(&atv, tsp);
1353 if (itimerfix(&atv)) {
1357 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1360 timeout = atv.tv_sec > 24 * 60 * 60 ?
1361 24 * 60 * 60 * hz : tvtohz(&atv);
1362 getmicrouptime(&rtv);
1363 timevaladd(&atv, &rtv);
1369 marker = knote_alloc(1);
1370 if (marker == NULL) {
1374 marker->kn_status = KN_MARKER;
1379 if (atv.tv_sec || atv.tv_usec) {
1380 getmicrouptime(&rtv);
1381 if (timevalcmp(&rtv, &atv, >=))
1384 timevalsub(&ttv, &rtv);
1385 timeout = ttv.tv_sec > 24 * 60 * 60 ?
1386 24 * 60 * 60 * hz : tvtohz(&ttv);
1391 if (kq->kq_count == 0) {
1393 error = EWOULDBLOCK;
1395 kq->kq_state |= KQ_SLEEP;
1396 error = msleep(kq, &kq->kq_lock, PSOCK | PCATCH,
1401 /* don't restart after signals... */
1402 if (error == ERESTART)
1404 else if (error == EWOULDBLOCK)
1409 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1413 kn = TAILQ_FIRST(&kq->kq_head);
1415 if ((kn->kn_status == KN_MARKER && kn != marker) ||
1416 (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1421 kq->kq_state |= KQ_FLUXWAIT;
1422 error = msleep(kq, &kq->kq_lock, PSOCK,
1427 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1428 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1429 kn->kn_status &= ~KN_QUEUED;
1435 if (count == maxevents)
1439 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1440 ("KN_INFLUX set when not suppose to be"));
1442 if ((kn->kn_flags & EV_DROP) == EV_DROP) {
1443 kn->kn_status &= ~KN_QUEUED;
1444 kn->kn_status |= KN_INFLUX;
1448 * We don't need to lock the list since we've marked
1451 if (!(kn->kn_status & KN_DETACHED))
1452 kn->kn_fop->f_detach(kn);
1456 } else if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
1457 kn->kn_status &= ~KN_QUEUED;
1458 kn->kn_status |= KN_INFLUX;
1462 * We don't need to lock the list since we've marked
1465 *kevp = kn->kn_kevent;
1466 if (!(kn->kn_status & KN_DETACHED))
1467 kn->kn_fop->f_detach(kn);
1472 kn->kn_status |= KN_INFLUX | KN_SCAN;
1474 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
1475 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1477 if (kn->kn_fop->f_event(kn, 0) == 0) {
1479 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1481 ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX |
1488 touch = (!kn->kn_fop->f_isfd &&
1489 kn->kn_fop->f_touch != NULL);
1491 kn->kn_fop->f_touch(kn, kevp, EVENT_PROCESS);
1493 *kevp = kn->kn_kevent;
1495 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1496 if (kn->kn_flags & (EV_CLEAR | EV_DISPATCH)) {
1498 * Manually clear knotes who weren't
1501 if (touch == 0 && kn->kn_flags & EV_CLEAR) {
1505 if (kn->kn_flags & EV_DISPATCH)
1506 kn->kn_status |= KN_DISABLED;
1507 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1510 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1512 kn->kn_status &= ~(KN_INFLUX | KN_SCAN);
1517 /* we are returning a copy to the user */
1522 if (nkev == KQ_NEVENTS) {
1525 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1533 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1541 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1542 td->td_retval[0] = maxevents - count;
1548 * This could be expanded to call kqueue_scan, if desired.
1552 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1553 int flags, struct thread *td)
1560 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
1561 int flags, struct thread *td)
1568 kqueue_truncate(struct file *fp, off_t length, struct ucred *active_cred,
1577 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
1578 struct ucred *active_cred, struct thread *td)
1581 * Enabling sigio causes two major problems:
1582 * 1) infinite recursion:
1583 * Synopsys: kevent is being used to track signals and have FIOASYNC
1584 * set. On receipt of a signal this will cause a kqueue to recurse
1585 * into itself over and over. Sending the sigio causes the kqueue
1586 * to become ready, which in turn posts sigio again, forever.
1587 * Solution: this can be solved by setting a flag in the kqueue that
1588 * we have a SIGIO in progress.
1589 * 2) locking problems:
1590 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
1591 * us above the proc and pgrp locks.
1592 * Solution: Post a signal using an async mechanism, being sure to
1593 * record a generation count in the delivery so that we do not deliver
1594 * a signal to the wrong process.
1596 * Note, these two mechanisms are somewhat mutually exclusive!
1605 kq->kq_state |= KQ_ASYNC;
1607 kq->kq_state &= ~KQ_ASYNC;
1612 return (fsetown(*(int *)data, &kq->kq_sigio));
1615 *(int *)data = fgetown(&kq->kq_sigio);
1625 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
1632 if ((error = kqueue_acquire(fp, &kq)))
1636 if (events & (POLLIN | POLLRDNORM)) {
1638 revents |= events & (POLLIN | POLLRDNORM);
1640 selrecord(td, &kq->kq_sel);
1641 if (SEL_WAITING(&kq->kq_sel))
1642 kq->kq_state |= KQ_SEL;
1645 kqueue_release(kq, 1);
1652 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1656 bzero((void *)st, sizeof *st);
1658 * We no longer return kq_count because the unlocked value is useless.
1659 * If you spent all this time getting the count, why not spend your
1660 * syscall better by calling kevent?
1662 * XXX - This is needed for libc_r.
1664 st->st_mode = S_IFIFO;
1670 kqueue_close(struct file *fp, struct thread *td)
1672 struct kqueue *kq = fp->f_data;
1673 struct filedesc *fdp;
1677 int filedesc_unlock;
1679 if ((error = kqueue_acquire(fp, &kq)))
1682 filedesc_unlock = 0;
1685 KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
1686 ("kqueue already closing"));
1687 kq->kq_state |= KQ_CLOSING;
1688 if (kq->kq_refcnt > 1)
1689 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
1691 KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
1694 KASSERT(knlist_empty(&kq->kq_sel.si_note),
1695 ("kqueue's knlist not empty"));
1697 for (i = 0; i < kq->kq_knlistsize; i++) {
1698 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
1699 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1700 kq->kq_state |= KQ_FLUXWAIT;
1701 msleep(kq, &kq->kq_lock, PSOCK, "kqclo1", 0);
1704 kn->kn_status |= KN_INFLUX;
1706 if (!(kn->kn_status & KN_DETACHED))
1707 kn->kn_fop->f_detach(kn);
1712 if (kq->kq_knhashmask != 0) {
1713 for (i = 0; i <= kq->kq_knhashmask; i++) {
1714 while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
1715 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1716 kq->kq_state |= KQ_FLUXWAIT;
1717 msleep(kq, &kq->kq_lock, PSOCK,
1721 kn->kn_status |= KN_INFLUX;
1723 if (!(kn->kn_status & KN_DETACHED))
1724 kn->kn_fop->f_detach(kn);
1731 if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
1732 kq->kq_state |= KQ_TASKDRAIN;
1733 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
1736 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1737 selwakeuppri(&kq->kq_sel, PSOCK);
1738 if (!SEL_WAITING(&kq->kq_sel))
1739 kq->kq_state &= ~KQ_SEL;
1745 * We could be called due to the knote_drop() doing fdrop(),
1746 * called from kqueue_register(). In this case the global
1747 * lock is owned, and filedesc sx is locked before, to not
1748 * take the sleepable lock after non-sleepable.
1750 if (!sx_xlocked(FILEDESC_LOCK(fdp))) {
1751 FILEDESC_XLOCK(fdp);
1752 filedesc_unlock = 1;
1754 filedesc_unlock = 0;
1755 TAILQ_REMOVE(&fdp->fd_kqlist, kq, kq_list);
1756 if (filedesc_unlock)
1757 FILEDESC_XUNLOCK(fdp);
1759 seldrain(&kq->kq_sel);
1760 knlist_destroy(&kq->kq_sel.si_note);
1761 mtx_destroy(&kq->kq_lock);
1764 if (kq->kq_knhash != NULL)
1765 free(kq->kq_knhash, M_KQUEUE);
1766 if (kq->kq_knlist != NULL)
1767 free(kq->kq_knlist, M_KQUEUE);
1769 funsetown(&kq->kq_sigio);
1777 kqueue_wakeup(struct kqueue *kq)
1781 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
1782 kq->kq_state &= ~KQ_SLEEP;
1785 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1786 selwakeuppri(&kq->kq_sel, PSOCK);
1787 if (!SEL_WAITING(&kq->kq_sel))
1788 kq->kq_state &= ~KQ_SEL;
1790 if (!knlist_empty(&kq->kq_sel.si_note))
1791 kqueue_schedtask(kq);
1792 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
1793 pgsigio(&kq->kq_sigio, SIGIO, 0);
1798 * Walk down a list of knotes, activating them if their event has triggered.
1800 * There is a possibility to optimize in the case of one kq watching another.
1801 * Instead of scheduling a task to wake it up, you could pass enough state
1802 * down the chain to make up the parent kqueue. Make this code functional
1806 knote(struct knlist *list, long hint, int lockflags)
1815 KNL_ASSERT_LOCK(list, lockflags & KNF_LISTLOCKED);
1817 if ((lockflags & KNF_LISTLOCKED) == 0)
1818 list->kl_lock(list->kl_lockarg);
1821 * If we unlock the list lock (and set KN_INFLUX), we can eliminate
1822 * the kqueue scheduling, but this will introduce four
1823 * lock/unlock's for each knote to test. If we do, continue to use
1824 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is
1825 * only safe if you want to remove the current item, which we are
1828 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
1831 if ((kn->kn_status & (KN_INFLUX | KN_SCAN)) == KN_INFLUX) {
1833 * Do not process the influx notes, except for
1834 * the influx coming from the kq unlock in the
1835 * kqueue_scan(). In the later case, we do
1836 * not interfere with the scan, since the code
1837 * fragment in kqueue_scan() locks the knlist,
1838 * and cannot proceed until we finished.
1841 } else if ((lockflags & KNF_NOKQLOCK) != 0) {
1842 kn->kn_status |= KN_INFLUX;
1844 error = kn->kn_fop->f_event(kn, hint);
1846 kn->kn_status &= ~KN_INFLUX;
1848 KNOTE_ACTIVATE(kn, 1);
1851 kn->kn_status |= KN_HASKQLOCK;
1852 if (kn->kn_fop->f_event(kn, hint))
1853 KNOTE_ACTIVATE(kn, 1);
1854 kn->kn_status &= ~KN_HASKQLOCK;
1858 if ((lockflags & KNF_LISTLOCKED) == 0)
1859 list->kl_unlock(list->kl_lockarg);
1863 * add a knote to a knlist
1866 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
1868 KNL_ASSERT_LOCK(knl, islocked);
1869 KQ_NOTOWNED(kn->kn_kq);
1870 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) ==
1871 (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED"));
1873 knl->kl_lock(knl->kl_lockarg);
1874 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
1876 knl->kl_unlock(knl->kl_lockarg);
1878 kn->kn_knlist = knl;
1879 kn->kn_status &= ~KN_DETACHED;
1880 KQ_UNLOCK(kn->kn_kq);
1884 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked)
1886 KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked"));
1887 KNL_ASSERT_LOCK(knl, knlislocked);
1888 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
1890 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX,
1891 ("knlist_remove called w/o knote being KN_INFLUX or already removed"));
1893 knl->kl_lock(knl->kl_lockarg);
1894 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
1895 kn->kn_knlist = NULL;
1897 knl->kl_unlock(knl->kl_lockarg);
1900 kn->kn_status |= KN_DETACHED;
1902 KQ_UNLOCK(kn->kn_kq);
1906 * remove all knotes from a specified klist
1909 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
1912 knlist_remove_kq(knl, kn, islocked, 0);
1916 * remove knote from a specified klist while in f_event handler.
1919 knlist_remove_inevent(struct knlist *knl, struct knote *kn)
1922 knlist_remove_kq(knl, kn, 1,
1923 (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK);
1927 knlist_empty(struct knlist *knl)
1929 KNL_ASSERT_LOCKED(knl);
1930 return SLIST_EMPTY(&knl->kl_list);
1933 static struct mtx knlist_lock;
1934 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
1936 static void knlist_mtx_lock(void *arg);
1937 static void knlist_mtx_unlock(void *arg);
1940 knlist_mtx_lock(void *arg)
1942 mtx_lock((struct mtx *)arg);
1946 knlist_mtx_unlock(void *arg)
1948 mtx_unlock((struct mtx *)arg);
1952 knlist_mtx_assert_locked(void *arg)
1954 mtx_assert((struct mtx *)arg, MA_OWNED);
1958 knlist_mtx_assert_unlocked(void *arg)
1960 mtx_assert((struct mtx *)arg, MA_NOTOWNED);
1964 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
1965 void (*kl_unlock)(void *),
1966 void (*kl_assert_locked)(void *), void (*kl_assert_unlocked)(void *))
1970 knl->kl_lockarg = &knlist_lock;
1972 knl->kl_lockarg = lock;
1974 if (kl_lock == NULL)
1975 knl->kl_lock = knlist_mtx_lock;
1977 knl->kl_lock = kl_lock;
1978 if (kl_unlock == NULL)
1979 knl->kl_unlock = knlist_mtx_unlock;
1981 knl->kl_unlock = kl_unlock;
1982 if (kl_assert_locked == NULL)
1983 knl->kl_assert_locked = knlist_mtx_assert_locked;
1985 knl->kl_assert_locked = kl_assert_locked;
1986 if (kl_assert_unlocked == NULL)
1987 knl->kl_assert_unlocked = knlist_mtx_assert_unlocked;
1989 knl->kl_assert_unlocked = kl_assert_unlocked;
1991 SLIST_INIT(&knl->kl_list);
1995 knlist_init_mtx(struct knlist *knl, struct mtx *lock)
1998 knlist_init(knl, lock, NULL, NULL, NULL, NULL);
2002 knlist_destroy(struct knlist *knl)
2007 * if we run across this error, we need to find the offending
2008 * driver and have it call knlist_clear.
2010 if (!SLIST_EMPTY(&knl->kl_list))
2011 printf("WARNING: destroying knlist w/ knotes on it!\n");
2014 knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL;
2015 SLIST_INIT(&knl->kl_list);
2019 * Even if we are locked, we may need to drop the lock to allow any influx
2020 * knotes time to "settle".
2023 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
2025 struct knote *kn, *kn2;
2029 KNL_ASSERT_LOCKED(knl);
2031 KNL_ASSERT_UNLOCKED(knl);
2032 again: /* need to reacquire lock since we have dropped it */
2033 knl->kl_lock(knl->kl_lockarg);
2036 SLIST_FOREACH_SAFE(kn, &knl->kl_list, kn_selnext, kn2) {
2039 if ((kn->kn_status & KN_INFLUX)) {
2043 knlist_remove_kq(knl, kn, 1, 1);
2045 kn->kn_status |= KN_INFLUX | KN_DETACHED;
2049 /* Make sure cleared knotes disappear soon */
2050 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2056 if (!SLIST_EMPTY(&knl->kl_list)) {
2057 /* there are still KN_INFLUX remaining */
2058 kn = SLIST_FIRST(&knl->kl_list);
2061 KASSERT(kn->kn_status & KN_INFLUX,
2062 ("knote removed w/o list lock"));
2063 knl->kl_unlock(knl->kl_lockarg);
2064 kq->kq_state |= KQ_FLUXWAIT;
2065 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
2071 KNL_ASSERT_LOCKED(knl);
2073 knl->kl_unlock(knl->kl_lockarg);
2074 KNL_ASSERT_UNLOCKED(knl);
2079 * Remove all knotes referencing a specified fd must be called with FILEDESC
2080 * lock. This prevents a race where a new fd comes along and occupies the
2081 * entry and we attach a knote to the fd.
2084 knote_fdclose(struct thread *td, int fd)
2086 struct filedesc *fdp = td->td_proc->p_fd;
2091 FILEDESC_XLOCK_ASSERT(fdp);
2094 * We shouldn't have to worry about new kevents appearing on fd
2095 * since filedesc is locked.
2097 TAILQ_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
2102 while (kq->kq_knlistsize > fd &&
2103 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
2104 if (kn->kn_status & KN_INFLUX) {
2105 /* someone else might be waiting on our knote */
2108 kq->kq_state |= KQ_FLUXWAIT;
2109 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
2112 kn->kn_status |= KN_INFLUX;
2114 if (!(kn->kn_status & KN_DETACHED))
2115 kn->kn_fop->f_detach(kn);
2125 knote_attach(struct knote *kn, struct kqueue *kq)
2129 KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX"));
2132 if (kn->kn_fop->f_isfd) {
2133 if (kn->kn_id >= kq->kq_knlistsize)
2135 list = &kq->kq_knlist[kn->kn_id];
2137 if (kq->kq_knhash == NULL)
2139 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2142 SLIST_INSERT_HEAD(list, kn, kn_link);
2148 * knote must already have been detached using the f_detach method.
2149 * no lock need to be held, it is assumed that the KN_INFLUX flag is set
2150 * to prevent other removal.
2153 knote_drop(struct knote *kn, struct thread *td)
2161 KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX,
2162 ("knote_drop called without KN_INFLUX set in kn_status"));
2165 if (kn->kn_fop->f_isfd)
2166 list = &kq->kq_knlist[kn->kn_id];
2168 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2170 if (!SLIST_EMPTY(list))
2171 SLIST_REMOVE(list, kn, knote, kn_link);
2172 if (kn->kn_status & KN_QUEUED)
2176 if (kn->kn_fop->f_isfd) {
2177 fdrop(kn->kn_fp, td);
2180 kqueue_fo_release(kn->kn_kevent.filter);
2186 knote_enqueue(struct knote *kn)
2188 struct kqueue *kq = kn->kn_kq;
2190 KQ_OWNED(kn->kn_kq);
2191 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
2193 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
2194 kn->kn_status |= KN_QUEUED;
2200 knote_dequeue(struct knote *kn)
2202 struct kqueue *kq = kn->kn_kq;
2204 KQ_OWNED(kn->kn_kq);
2205 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
2207 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
2208 kn->kn_status &= ~KN_QUEUED;
2216 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
2217 NULL, NULL, UMA_ALIGN_PTR, 0);
2219 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL);
2221 static struct knote *
2222 knote_alloc(int waitok)
2224 return ((struct knote *)uma_zalloc(knote_zone,
2225 (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO));
2229 knote_free(struct knote *kn)
2232 uma_zfree(knote_zone, kn);
2236 * Register the kev w/ the kq specified by fd.
2239 kqfd_register(int fd, struct kevent *kev, struct thread *td, int waitok)
2245 if ((error = fget(td, fd, CAP_POST_EVENT, &fp)) != 0)
2247 if ((error = kqueue_acquire(fp, &kq)) != 0)
2250 error = kqueue_register(kq, kev, td, waitok);
2252 kqueue_release(kq, 0);