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,
132 static int knote_attach(struct knote *kn, struct kqueue *kq);
133 static void knote_drop(struct knote *kn, struct thread *td);
134 static void knote_enqueue(struct knote *kn);
135 static void knote_dequeue(struct knote *kn);
136 static void knote_init(void);
137 static struct knote *knote_alloc(int waitok);
138 static void knote_free(struct knote *kn);
140 static void filt_kqdetach(struct knote *kn);
141 static int filt_kqueue(struct knote *kn, long hint);
142 static int filt_procattach(struct knote *kn);
143 static void filt_procdetach(struct knote *kn);
144 static int filt_proc(struct knote *kn, long hint);
145 static int filt_fileattach(struct knote *kn);
146 static void filt_timerexpire(void *knx);
147 static int filt_timerattach(struct knote *kn);
148 static void filt_timerdetach(struct knote *kn);
149 static int filt_timer(struct knote *kn, long hint);
150 static int filt_userattach(struct knote *kn);
151 static void filt_userdetach(struct knote *kn);
152 static int filt_user(struct knote *kn, long hint);
153 static void filt_usertouch(struct knote *kn, struct kevent *kev,
156 static struct filterops file_filtops = {
158 .f_attach = filt_fileattach,
160 static struct filterops kqread_filtops = {
162 .f_detach = filt_kqdetach,
163 .f_event = filt_kqueue,
165 /* XXX - move to kern_proc.c? */
166 static struct filterops proc_filtops = {
168 .f_attach = filt_procattach,
169 .f_detach = filt_procdetach,
170 .f_event = filt_proc,
172 static struct filterops timer_filtops = {
174 .f_attach = filt_timerattach,
175 .f_detach = filt_timerdetach,
176 .f_event = filt_timer,
178 static struct filterops user_filtops = {
179 .f_attach = filt_userattach,
180 .f_detach = filt_userdetach,
181 .f_event = filt_user,
182 .f_touch = filt_usertouch,
185 static uma_zone_t knote_zone;
186 static atomic_uint kq_ncallouts = ATOMIC_VAR_INIT(0);
187 static unsigned int kq_calloutmax = 4 * 1024;
188 SYSCTL_UINT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
189 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
191 /* XXX - ensure not KN_INFLUX?? */
192 #define KNOTE_ACTIVATE(kn, islock) do { \
194 mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED); \
196 KQ_LOCK((kn)->kn_kq); \
197 (kn)->kn_status |= KN_ACTIVE; \
198 if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
199 knote_enqueue((kn)); \
201 KQ_UNLOCK((kn)->kn_kq); \
203 #define KQ_LOCK(kq) do { \
204 mtx_lock(&(kq)->kq_lock); \
206 #define KQ_FLUX_WAKEUP(kq) do { \
207 if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) { \
208 (kq)->kq_state &= ~KQ_FLUXWAIT; \
212 #define KQ_UNLOCK_FLUX(kq) do { \
213 KQ_FLUX_WAKEUP(kq); \
214 mtx_unlock(&(kq)->kq_lock); \
216 #define KQ_UNLOCK(kq) do { \
217 mtx_unlock(&(kq)->kq_lock); \
219 #define KQ_OWNED(kq) do { \
220 mtx_assert(&(kq)->kq_lock, MA_OWNED); \
222 #define KQ_NOTOWNED(kq) do { \
223 mtx_assert(&(kq)->kq_lock, MA_NOTOWNED); \
225 #define KN_LIST_LOCK(kn) do { \
226 if (kn->kn_knlist != NULL) \
227 kn->kn_knlist->kl_lock(kn->kn_knlist->kl_lockarg); \
229 #define KN_LIST_UNLOCK(kn) do { \
230 if (kn->kn_knlist != NULL) \
231 kn->kn_knlist->kl_unlock(kn->kn_knlist->kl_lockarg); \
233 #define KNL_ASSERT_LOCK(knl, islocked) do { \
235 KNL_ASSERT_LOCKED(knl); \
237 KNL_ASSERT_UNLOCKED(knl); \
240 #define KNL_ASSERT_LOCKED(knl) do { \
241 knl->kl_assert_locked((knl)->kl_lockarg); \
243 #define KNL_ASSERT_UNLOCKED(knl) do { \
244 knl->kl_assert_unlocked((knl)->kl_lockarg); \
246 #else /* !INVARIANTS */
247 #define KNL_ASSERT_LOCKED(knl) do {} while(0)
248 #define KNL_ASSERT_UNLOCKED(knl) do {} while (0)
249 #endif /* INVARIANTS */
251 #define KN_HASHSIZE 64 /* XXX should be tunable */
252 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
255 filt_nullattach(struct knote *kn)
261 struct filterops null_filtops = {
263 .f_attach = filt_nullattach,
266 /* XXX - make SYSINIT to add these, and move into respective modules. */
267 extern struct filterops sig_filtops;
268 extern struct filterops fs_filtops;
271 * Table for for all system-defined filters.
273 static struct mtx filterops_lock;
274 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops",
277 struct filterops *for_fop;
279 } sysfilt_ops[EVFILT_SYSCOUNT] = {
280 { &file_filtops }, /* EVFILT_READ */
281 { &file_filtops }, /* EVFILT_WRITE */
282 { &null_filtops }, /* EVFILT_AIO */
283 { &file_filtops }, /* EVFILT_VNODE */
284 { &proc_filtops }, /* EVFILT_PROC */
285 { &sig_filtops }, /* EVFILT_SIGNAL */
286 { &timer_filtops }, /* EVFILT_TIMER */
287 { &null_filtops }, /* former EVFILT_NETDEV */
288 { &fs_filtops }, /* EVFILT_FS */
289 { &null_filtops }, /* EVFILT_LIO */
290 { &user_filtops }, /* EVFILT_USER */
294 * Simple redirection for all cdevsw style objects to call their fo_kqfilter
298 filt_fileattach(struct knote *kn)
301 return (fo_kqfilter(kn->kn_fp, kn));
306 kqueue_kqfilter(struct file *fp, struct knote *kn)
308 struct kqueue *kq = kn->kn_fp->f_data;
310 if (kn->kn_filter != EVFILT_READ)
313 kn->kn_status |= KN_KQUEUE;
314 kn->kn_fop = &kqread_filtops;
315 knlist_add(&kq->kq_sel.si_note, kn, 0);
321 filt_kqdetach(struct knote *kn)
323 struct kqueue *kq = kn->kn_fp->f_data;
325 knlist_remove(&kq->kq_sel.si_note, kn, 0);
330 filt_kqueue(struct knote *kn, long hint)
332 struct kqueue *kq = kn->kn_fp->f_data;
334 kn->kn_data = kq->kq_count;
335 return (kn->kn_data > 0);
338 /* XXX - move to kern_proc.c? */
340 filt_procattach(struct knote *kn)
347 p = pfind(kn->kn_id);
348 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
349 p = zpfind(kn->kn_id);
351 } else if (p != NULL && (p->p_flag & P_WEXIT)) {
357 if ((error = p_cansee(curthread, p))) {
362 kn->kn_ptr.p_proc = p;
363 kn->kn_flags |= EV_CLEAR; /* automatically set */
366 * internal flag indicating registration done by kernel
368 if (kn->kn_flags & EV_FLAG1) {
369 kn->kn_data = kn->kn_sdata; /* ppid */
370 kn->kn_fflags = NOTE_CHILD;
371 kn->kn_flags &= ~EV_FLAG1;
375 knlist_add(&p->p_klist, kn, 1);
378 * Immediately activate any exit notes if the target process is a
379 * zombie. This is necessary to handle the case where the target
380 * process, e.g. a child, dies before the kevent is registered.
382 if (immediate && filt_proc(kn, NOTE_EXIT))
383 KNOTE_ACTIVATE(kn, 0);
391 * The knote may be attached to a different process, which may exit,
392 * leaving nothing for the knote to be attached to. So when the process
393 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
394 * it will be deleted when read out. However, as part of the knote deletion,
395 * this routine is called, so a check is needed to avoid actually performing
396 * a detach, because the original process does not exist any more.
398 /* XXX - move to kern_proc.c? */
400 filt_procdetach(struct knote *kn)
404 p = kn->kn_ptr.p_proc;
405 knlist_remove(&p->p_klist, kn, 0);
406 kn->kn_ptr.p_proc = NULL;
409 /* XXX - move to kern_proc.c? */
411 filt_proc(struct knote *kn, long hint)
413 struct proc *p = kn->kn_ptr.p_proc;
417 * mask off extra data
419 event = (u_int)hint & NOTE_PCTRLMASK;
422 * if the user is interested in this event, record it.
424 if (kn->kn_sfflags & event)
425 kn->kn_fflags |= event;
428 * process is gone, so flag the event as finished.
430 if (event == NOTE_EXIT) {
431 if (!(kn->kn_status & KN_DETACHED))
432 knlist_remove_inevent(&p->p_klist, kn);
433 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
434 kn->kn_data = p->p_xstat;
435 kn->kn_ptr.p_proc = NULL;
439 return (kn->kn_fflags != 0);
443 * Called when the process forked. It mostly does the same as the
444 * knote(), activating all knotes registered to be activated when the
445 * process forked. Additionally, for each knote attached to the
446 * parent, check whether user wants to track the new process. If so
447 * attach a new knote to it, and immediately report an event with the
451 knote_fork(struct knlist *list, int pid)
460 list->kl_lock(list->kl_lockarg);
462 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
463 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX)
467 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
473 * The same as knote(), activate the event.
475 if ((kn->kn_sfflags & NOTE_TRACK) == 0) {
476 kn->kn_status |= KN_HASKQLOCK;
477 if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
478 KNOTE_ACTIVATE(kn, 1);
479 kn->kn_status &= ~KN_HASKQLOCK;
485 * The NOTE_TRACK case. In addition to the activation
486 * of the event, we need to register new event to
487 * track the child. Drop the locks in preparation for
488 * the call to kqueue_register().
490 kn->kn_status |= KN_INFLUX;
492 list->kl_unlock(list->kl_lockarg);
495 * Activate existing knote and register a knote with
499 kev.filter = kn->kn_filter;
500 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
501 kev.fflags = kn->kn_sfflags;
502 kev.data = kn->kn_id; /* parent */
503 kev.udata = kn->kn_kevent.udata;/* preserve udata */
504 error = kqueue_register(kq, &kev, NULL, 0);
505 if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
506 KNOTE_ACTIVATE(kn, 0);
508 kn->kn_fflags |= NOTE_TRACKERR;
510 kn->kn_status &= ~KN_INFLUX;
512 list->kl_lock(list->kl_lockarg);
514 list->kl_unlock(list->kl_lockarg);
518 * XXX: EVFILT_TIMER should perhaps live in kern_time.c beside the
519 * interval timer support code.
521 static __inline sbintime_t
522 timer2sbintime(intptr_t data)
525 return (SBT_1MS * data);
529 filt_timerexpire(void *knx)
531 struct callout *calloutp;
536 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */
538 if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) {
539 calloutp = (struct callout *)kn->kn_hook;
540 callout_reset_sbt_on(calloutp,
541 timer2sbintime(kn->kn_sdata), 0 /* 1ms? */,
542 filt_timerexpire, kn, PCPU_GET(cpuid), 0);
547 * data contains amount of time to sleep, in milliseconds
550 filt_timerattach(struct knote *kn)
552 struct callout *calloutp;
553 unsigned int ncallouts;
555 ncallouts = atomic_load_explicit(&kq_ncallouts, memory_order_relaxed);
557 if (ncallouts >= kq_calloutmax)
559 } while (!atomic_compare_exchange_weak_explicit(&kq_ncallouts,
560 &ncallouts, ncallouts + 1, memory_order_relaxed,
561 memory_order_relaxed));
563 kn->kn_flags |= EV_CLEAR; /* automatically set */
564 kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */
565 calloutp = malloc(sizeof(*calloutp), M_KQUEUE, M_WAITOK);
566 callout_init(calloutp, CALLOUT_MPSAFE);
567 kn->kn_hook = calloutp;
568 callout_reset_sbt_on(calloutp,
569 timer2sbintime(kn->kn_sdata), 0 /* 1ms? */,
570 filt_timerexpire, kn, PCPU_GET(cpuid), 0);
576 filt_timerdetach(struct knote *kn)
578 struct callout *calloutp;
581 calloutp = (struct callout *)kn->kn_hook;
582 callout_drain(calloutp);
583 free(calloutp, M_KQUEUE);
584 old = atomic_fetch_sub_explicit(&kq_ncallouts, 1, memory_order_relaxed);
585 KASSERT(old > 0, ("Number of callouts cannot become negative"));
586 kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */
590 filt_timer(struct knote *kn, long hint)
593 return (kn->kn_data != 0);
597 filt_userattach(struct knote *kn)
601 * EVFILT_USER knotes are not attached to anything in the kernel.
604 if (kn->kn_fflags & NOTE_TRIGGER)
612 filt_userdetach(__unused struct knote *kn)
616 * EVFILT_USER knotes are not attached to anything in the kernel.
621 filt_user(struct knote *kn, __unused long hint)
624 return (kn->kn_hookid);
628 filt_usertouch(struct knote *kn, struct kevent *kev, u_long type)
634 if (kev->fflags & NOTE_TRIGGER)
637 ffctrl = kev->fflags & NOTE_FFCTRLMASK;
638 kev->fflags &= NOTE_FFLAGSMASK;
644 kn->kn_sfflags &= kev->fflags;
648 kn->kn_sfflags |= kev->fflags;
652 kn->kn_sfflags = kev->fflags;
656 /* XXX Return error? */
659 kn->kn_sdata = kev->data;
660 if (kev->flags & EV_CLEAR) {
668 *kev = kn->kn_kevent;
669 kev->fflags = kn->kn_sfflags;
670 kev->data = kn->kn_sdata;
671 if (kn->kn_flags & EV_CLEAR) {
679 panic("filt_usertouch() - invalid type (%ld)", type);
685 sys_kqueue(struct thread *td, struct kqueue_args *uap)
687 struct filedesc *fdp;
692 fdp = td->td_proc->p_fd;
693 error = falloc(td, &fp, &fd, 0);
697 /* An extra reference on `fp' has been held for us by falloc(). */
698 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
699 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK);
700 TAILQ_INIT(&kq->kq_head);
702 knlist_init_mtx(&kq->kq_sel.si_note, &kq->kq_lock);
703 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
706 SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
707 FILEDESC_XUNLOCK(fdp);
709 finit(fp, FREAD | FWRITE, DTYPE_KQUEUE, kq, &kqueueops);
712 td->td_retval[0] = fd;
717 #ifndef _SYS_SYSPROTO_H_
720 const struct kevent *changelist;
722 struct kevent *eventlist;
724 const struct timespec *timeout;
728 sys_kevent(struct thread *td, struct kevent_args *uap)
730 struct timespec ts, *tsp;
731 struct kevent_copyops k_ops = { uap,
738 struct uio *ktruioin = NULL;
739 struct uio *ktruioout = NULL;
742 if (uap->timeout != NULL) {
743 error = copyin(uap->timeout, &ts, sizeof(ts));
751 if (KTRPOINT(td, KTR_GENIO)) {
752 ktriov.iov_base = uap->changelist;
753 ktriov.iov_len = uap->nchanges * sizeof(struct kevent);
754 ktruio = (struct uio){ .uio_iov = &ktriov, .uio_iovcnt = 1,
755 .uio_segflg = UIO_USERSPACE, .uio_rw = UIO_READ,
757 ktruioin = cloneuio(&ktruio);
758 ktriov.iov_base = uap->eventlist;
759 ktriov.iov_len = uap->nevents * sizeof(struct kevent);
760 ktruioout = cloneuio(&ktruio);
764 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
768 if (ktruioin != NULL) {
769 ktruioin->uio_resid = uap->nchanges * sizeof(struct kevent);
770 ktrgenio(uap->fd, UIO_WRITE, ktruioin, 0);
771 ktruioout->uio_resid = td->td_retval[0] * sizeof(struct kevent);
772 ktrgenio(uap->fd, UIO_READ, ktruioout, error);
780 * Copy 'count' items into the destination list pointed to by uap->eventlist.
783 kevent_copyout(void *arg, struct kevent *kevp, int count)
785 struct kevent_args *uap;
788 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
789 uap = (struct kevent_args *)arg;
791 error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
793 uap->eventlist += count;
798 * Copy 'count' items from the list pointed to by uap->changelist.
801 kevent_copyin(void *arg, struct kevent *kevp, int count)
803 struct kevent_args *uap;
806 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
807 uap = (struct kevent_args *)arg;
809 error = copyin(uap->changelist, kevp, count * sizeof *kevp);
811 uap->changelist += count;
816 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
817 struct kevent_copyops *k_ops, const struct timespec *timeout)
819 struct kevent keva[KQ_NEVENTS];
820 struct kevent *kevp, *changes;
823 int i, n, nerrors, error;
825 if ((error = fget(td, fd, CAP_POST_EVENT, &fp)) != 0)
827 if ((error = kqueue_acquire(fp, &kq)) != 0)
832 while (nchanges > 0) {
833 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
834 error = k_ops->k_copyin(k_ops->arg, keva, n);
838 for (i = 0; i < n; i++) {
842 kevp->flags &= ~EV_SYSFLAGS;
843 error = kqueue_register(kq, kevp, td, 1);
844 if (error || (kevp->flags & EV_RECEIPT)) {
846 kevp->flags = EV_ERROR;
848 (void) k_ops->k_copyout(k_ops->arg,
860 td->td_retval[0] = nerrors;
865 error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td);
867 kqueue_release(kq, 0);
874 kqueue_add_filteropts(int filt, struct filterops *filtops)
879 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
881 "trying to add a filterop that is out of range: %d is beyond %d\n",
882 ~filt, EVFILT_SYSCOUNT);
885 mtx_lock(&filterops_lock);
886 if (sysfilt_ops[~filt].for_fop != &null_filtops &&
887 sysfilt_ops[~filt].for_fop != NULL)
890 sysfilt_ops[~filt].for_fop = filtops;
891 sysfilt_ops[~filt].for_refcnt = 0;
893 mtx_unlock(&filterops_lock);
899 kqueue_del_filteropts(int filt)
904 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
907 mtx_lock(&filterops_lock);
908 if (sysfilt_ops[~filt].for_fop == &null_filtops ||
909 sysfilt_ops[~filt].for_fop == NULL)
911 else if (sysfilt_ops[~filt].for_refcnt != 0)
914 sysfilt_ops[~filt].for_fop = &null_filtops;
915 sysfilt_ops[~filt].for_refcnt = 0;
917 mtx_unlock(&filterops_lock);
922 static struct filterops *
923 kqueue_fo_find(int filt)
926 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
929 mtx_lock(&filterops_lock);
930 sysfilt_ops[~filt].for_refcnt++;
931 if (sysfilt_ops[~filt].for_fop == NULL)
932 sysfilt_ops[~filt].for_fop = &null_filtops;
933 mtx_unlock(&filterops_lock);
935 return sysfilt_ops[~filt].for_fop;
939 kqueue_fo_release(int filt)
942 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
945 mtx_lock(&filterops_lock);
946 KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
947 ("filter object refcount not valid on release"));
948 sysfilt_ops[~filt].for_refcnt--;
949 mtx_unlock(&filterops_lock);
953 * A ref to kq (obtained via kqueue_acquire) must be held. waitok will
954 * influence if memory allocation should wait. Make sure it is 0 if you
958 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
960 struct filterops *fops;
962 struct knote *kn, *tkn;
963 int error, filt, event;
972 fops = kqueue_fo_find(filt);
976 tkn = knote_alloc(waitok); /* prevent waiting with locks */
980 KASSERT(td != NULL, ("td is NULL"));
981 error = fget(td, kev->ident, CAP_POLL_EVENT, &fp);
985 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
986 kev->ident, 0) != 0) {
990 error = kqueue_expand(kq, fops, kev->ident, waitok);
996 if (fp->f_type == DTYPE_KQUEUE) {
998 * if we add some inteligence about what we are doing,
999 * we should be able to support events on ourselves.
1000 * We need to know when we are doing this to prevent
1001 * getting both the knlist lock and the kq lock since
1002 * they are the same thing.
1004 if (fp->f_data == kq) {
1009 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1013 if (kev->ident < kq->kq_knlistsize) {
1014 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
1015 if (kev->filter == kn->kn_filter)
1019 if ((kev->flags & EV_ADD) == EV_ADD)
1020 kqueue_expand(kq, fops, kev->ident, waitok);
1023 if (kq->kq_knhashmask != 0) {
1026 list = &kq->kq_knhash[
1027 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
1028 SLIST_FOREACH(kn, list, kn_link)
1029 if (kev->ident == kn->kn_id &&
1030 kev->filter == kn->kn_filter)
1035 /* knote is in the process of changing, wait for it to stablize. */
1036 if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1037 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1038 kq->kq_state |= KQ_FLUXWAIT;
1039 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
1048 * kn now contains the matching knote, or NULL if no match
1051 if (kev->flags & EV_ADD) {
1063 * apply reference counts to knote structure, and
1064 * do not release it at the end of this routine.
1069 kn->kn_sfflags = kev->fflags;
1070 kn->kn_sdata = kev->data;
1073 kn->kn_kevent = *kev;
1074 kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE |
1075 EV_ENABLE | EV_DISABLE);
1076 kn->kn_status = KN_INFLUX|KN_DETACHED;
1078 error = knote_attach(kn, kq);
1085 if ((error = kn->kn_fop->f_attach(kn)) != 0) {
1092 /* No matching knote and the EV_ADD flag is not set. */
1099 if (kev->flags & EV_DELETE) {
1100 kn->kn_status |= KN_INFLUX;
1102 if (!(kn->kn_status & KN_DETACHED))
1103 kn->kn_fop->f_detach(kn);
1109 * The user may change some filter values after the initial EV_ADD,
1110 * but doing so will not reset any filter which has already been
1113 kn->kn_status |= KN_INFLUX;
1116 kn->kn_kevent.udata = kev->udata;
1117 if (!fops->f_isfd && fops->f_touch != NULL) {
1118 fops->f_touch(kn, kev, EVENT_REGISTER);
1120 kn->kn_sfflags = kev->fflags;
1121 kn->kn_sdata = kev->data;
1125 * We can get here with kn->kn_knlist == NULL. This can happen when
1126 * the initial attach event decides that the event is "completed"
1127 * already. i.e. filt_procattach is called on a zombie process. It
1128 * will call filt_proc which will remove it from the list, and NULL
1132 event = kn->kn_fop->f_event(kn, 0);
1135 KNOTE_ACTIVATE(kn, 1);
1136 kn->kn_status &= ~KN_INFLUX;
1139 if ((kev->flags & EV_DISABLE) &&
1140 ((kn->kn_status & KN_DISABLED) == 0)) {
1141 kn->kn_status |= KN_DISABLED;
1144 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
1145 kn->kn_status &= ~KN_DISABLED;
1146 if ((kn->kn_status & KN_ACTIVE) &&
1147 ((kn->kn_status & KN_QUEUED) == 0))
1153 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1159 kqueue_fo_release(filt);
1164 kqueue_acquire(struct file *fp, struct kqueue **kqp)
1172 if (fp->f_type != DTYPE_KQUEUE || kq == NULL)
1176 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
1187 kqueue_release(struct kqueue *kq, int locked)
1194 if (kq->kq_refcnt == 1)
1195 wakeup(&kq->kq_refcnt);
1201 kqueue_schedtask(struct kqueue *kq)
1205 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1206 ("scheduling kqueue task while draining"));
1208 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1209 taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task);
1210 kq->kq_state |= KQ_TASKSCHED;
1215 * Expand the kq to make sure we have storage for fops/ident pair.
1217 * Return 0 on success (or no work necessary), return errno on failure.
1219 * Not calling hashinit w/ waitok (proper malloc flag) should be safe.
1220 * If kqueue_register is called from a non-fd context, there usually/should
1224 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
1227 struct klist *list, *tmp_knhash, *to_free;
1228 u_long tmp_knhashmask;
1231 int mflag = waitok ? M_WAITOK : M_NOWAIT;
1238 if (kq->kq_knlistsize <= fd) {
1239 size = kq->kq_knlistsize;
1242 list = malloc(size * sizeof(*list), M_KQUEUE, mflag);
1246 if (kq->kq_knlistsize > fd) {
1250 if (kq->kq_knlist != NULL) {
1251 bcopy(kq->kq_knlist, list,
1252 kq->kq_knlistsize * sizeof(*list));
1253 to_free = kq->kq_knlist;
1254 kq->kq_knlist = NULL;
1256 bzero((caddr_t)list +
1257 kq->kq_knlistsize * sizeof(*list),
1258 (size - kq->kq_knlistsize) * sizeof(*list));
1259 kq->kq_knlistsize = size;
1260 kq->kq_knlist = list;
1265 if (kq->kq_knhashmask == 0) {
1266 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1268 if (tmp_knhash == NULL)
1271 if (kq->kq_knhashmask == 0) {
1272 kq->kq_knhash = tmp_knhash;
1273 kq->kq_knhashmask = tmp_knhashmask;
1275 to_free = tmp_knhash;
1280 free(to_free, M_KQUEUE);
1287 kqueue_task(void *arg, int pending)
1295 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1298 KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1300 kq->kq_state &= ~KQ_TASKSCHED;
1301 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1302 wakeup(&kq->kq_state);
1305 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1309 * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1310 * We treat KN_MARKER knotes as if they are INFLUX.
1313 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1314 const struct timespec *tsp, struct kevent *keva, struct thread *td)
1316 struct kevent *kevp;
1317 struct knote *kn, *marker;
1318 sbintime_t asbt, rsbt;
1319 int count, error, haskqglobal, influx, nkev, touch;
1331 if (tsp->tv_sec < 0 || tsp->tv_nsec < 0 ||
1332 tsp->tv_nsec >= 1000000000) {
1336 if (timespecisset(tsp)) {
1337 if (tsp->tv_sec <= INT32_MAX) {
1338 rsbt = tstosbt(*tsp);
1339 if (TIMESEL(&asbt, rsbt))
1340 asbt += tc_tick_sbt;
1341 if (asbt <= INT64_MAX - rsbt)
1345 rsbt >>= tc_precexp;
1352 marker = knote_alloc(1);
1353 if (marker == NULL) {
1357 marker->kn_status = KN_MARKER;
1362 if (kq->kq_count == 0) {
1364 error = EWOULDBLOCK;
1366 kq->kq_state |= KQ_SLEEP;
1367 error = msleep_sbt(kq, &kq->kq_lock, PSOCK | PCATCH,
1368 "kqread", asbt, rsbt, C_ABSOLUTE);
1372 /* don't restart after signals... */
1373 if (error == ERESTART)
1375 else if (error == EWOULDBLOCK)
1380 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1384 kn = TAILQ_FIRST(&kq->kq_head);
1386 if ((kn->kn_status == KN_MARKER && kn != marker) ||
1387 (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1392 kq->kq_state |= KQ_FLUXWAIT;
1393 error = msleep(kq, &kq->kq_lock, PSOCK,
1398 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1399 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1400 kn->kn_status &= ~KN_QUEUED;
1406 if (count == maxevents)
1410 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1411 ("KN_INFLUX set when not suppose to be"));
1413 if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
1414 kn->kn_status &= ~KN_QUEUED;
1415 kn->kn_status |= KN_INFLUX;
1419 * We don't need to lock the list since we've marked
1422 *kevp = kn->kn_kevent;
1423 if (!(kn->kn_status & KN_DETACHED))
1424 kn->kn_fop->f_detach(kn);
1429 kn->kn_status |= KN_INFLUX;
1431 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
1432 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1434 if (kn->kn_fop->f_event(kn, 0) == 0) {
1436 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1438 ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX);
1444 touch = (!kn->kn_fop->f_isfd &&
1445 kn->kn_fop->f_touch != NULL);
1447 kn->kn_fop->f_touch(kn, kevp, EVENT_PROCESS);
1449 *kevp = kn->kn_kevent;
1451 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1452 if (kn->kn_flags & (EV_CLEAR | EV_DISPATCH)) {
1454 * Manually clear knotes who weren't
1457 if (touch == 0 && kn->kn_flags & EV_CLEAR) {
1461 if (kn->kn_flags & EV_DISPATCH)
1462 kn->kn_status |= KN_DISABLED;
1463 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1466 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1468 kn->kn_status &= ~(KN_INFLUX);
1473 /* we are returning a copy to the user */
1478 if (nkev == KQ_NEVENTS) {
1481 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1489 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1497 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1498 td->td_retval[0] = maxevents - count;
1504 * This could be expanded to call kqueue_scan, if desired.
1508 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1509 int flags, struct thread *td)
1516 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
1517 int flags, struct thread *td)
1524 kqueue_truncate(struct file *fp, off_t length, struct ucred *active_cred,
1533 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
1534 struct ucred *active_cred, struct thread *td)
1537 * Enabling sigio causes two major problems:
1538 * 1) infinite recursion:
1539 * Synopsys: kevent is being used to track signals and have FIOASYNC
1540 * set. On receipt of a signal this will cause a kqueue to recurse
1541 * into itself over and over. Sending the sigio causes the kqueue
1542 * to become ready, which in turn posts sigio again, forever.
1543 * Solution: this can be solved by setting a flag in the kqueue that
1544 * we have a SIGIO in progress.
1545 * 2) locking problems:
1546 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
1547 * us above the proc and pgrp locks.
1548 * Solution: Post a signal using an async mechanism, being sure to
1549 * record a generation count in the delivery so that we do not deliver
1550 * a signal to the wrong process.
1552 * Note, these two mechanisms are somewhat mutually exclusive!
1561 kq->kq_state |= KQ_ASYNC;
1563 kq->kq_state &= ~KQ_ASYNC;
1568 return (fsetown(*(int *)data, &kq->kq_sigio));
1571 *(int *)data = fgetown(&kq->kq_sigio);
1581 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
1588 if ((error = kqueue_acquire(fp, &kq)))
1592 if (events & (POLLIN | POLLRDNORM)) {
1594 revents |= events & (POLLIN | POLLRDNORM);
1596 selrecord(td, &kq->kq_sel);
1597 if (SEL_WAITING(&kq->kq_sel))
1598 kq->kq_state |= KQ_SEL;
1601 kqueue_release(kq, 1);
1608 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1612 bzero((void *)st, sizeof *st);
1614 * We no longer return kq_count because the unlocked value is useless.
1615 * If you spent all this time getting the count, why not spend your
1616 * syscall better by calling kevent?
1618 * XXX - This is needed for libc_r.
1620 st->st_mode = S_IFIFO;
1626 kqueue_close(struct file *fp, struct thread *td)
1628 struct kqueue *kq = fp->f_data;
1629 struct filedesc *fdp;
1634 if ((error = kqueue_acquire(fp, &kq)))
1639 KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
1640 ("kqueue already closing"));
1641 kq->kq_state |= KQ_CLOSING;
1642 if (kq->kq_refcnt > 1)
1643 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
1645 KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
1648 KASSERT(knlist_empty(&kq->kq_sel.si_note),
1649 ("kqueue's knlist not empty"));
1651 for (i = 0; i < kq->kq_knlistsize; i++) {
1652 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
1653 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1654 kq->kq_state |= KQ_FLUXWAIT;
1655 msleep(kq, &kq->kq_lock, PSOCK, "kqclo1", 0);
1658 kn->kn_status |= KN_INFLUX;
1660 if (!(kn->kn_status & KN_DETACHED))
1661 kn->kn_fop->f_detach(kn);
1666 if (kq->kq_knhashmask != 0) {
1667 for (i = 0; i <= kq->kq_knhashmask; i++) {
1668 while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
1669 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1670 kq->kq_state |= KQ_FLUXWAIT;
1671 msleep(kq, &kq->kq_lock, PSOCK,
1675 kn->kn_status |= KN_INFLUX;
1677 if (!(kn->kn_status & KN_DETACHED))
1678 kn->kn_fop->f_detach(kn);
1685 if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
1686 kq->kq_state |= KQ_TASKDRAIN;
1687 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
1690 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1691 selwakeuppri(&kq->kq_sel, PSOCK);
1692 if (!SEL_WAITING(&kq->kq_sel))
1693 kq->kq_state &= ~KQ_SEL;
1698 FILEDESC_XLOCK(fdp);
1699 SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list);
1700 FILEDESC_XUNLOCK(fdp);
1702 seldrain(&kq->kq_sel);
1703 knlist_destroy(&kq->kq_sel.si_note);
1704 mtx_destroy(&kq->kq_lock);
1707 if (kq->kq_knhash != NULL)
1708 free(kq->kq_knhash, M_KQUEUE);
1709 if (kq->kq_knlist != NULL)
1710 free(kq->kq_knlist, M_KQUEUE);
1712 funsetown(&kq->kq_sigio);
1720 kqueue_wakeup(struct kqueue *kq)
1724 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
1725 kq->kq_state &= ~KQ_SLEEP;
1728 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1729 selwakeuppri(&kq->kq_sel, PSOCK);
1730 if (!SEL_WAITING(&kq->kq_sel))
1731 kq->kq_state &= ~KQ_SEL;
1733 if (!knlist_empty(&kq->kq_sel.si_note))
1734 kqueue_schedtask(kq);
1735 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
1736 pgsigio(&kq->kq_sigio, SIGIO, 0);
1741 * Walk down a list of knotes, activating them if their event has triggered.
1743 * There is a possibility to optimize in the case of one kq watching another.
1744 * Instead of scheduling a task to wake it up, you could pass enough state
1745 * down the chain to make up the parent kqueue. Make this code functional
1749 knote(struct knlist *list, long hint, int lockflags)
1758 KNL_ASSERT_LOCK(list, lockflags & KNF_LISTLOCKED);
1760 if ((lockflags & KNF_LISTLOCKED) == 0)
1761 list->kl_lock(list->kl_lockarg);
1764 * If we unlock the list lock (and set KN_INFLUX), we can eliminate
1765 * the kqueue scheduling, but this will introduce four
1766 * lock/unlock's for each knote to test. If we do, continue to use
1767 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is
1768 * only safe if you want to remove the current item, which we are
1771 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
1773 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1775 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1777 } else if ((lockflags & KNF_NOKQLOCK) != 0) {
1778 kn->kn_status |= KN_INFLUX;
1780 error = kn->kn_fop->f_event(kn, hint);
1782 kn->kn_status &= ~KN_INFLUX;
1784 KNOTE_ACTIVATE(kn, 1);
1787 kn->kn_status |= KN_HASKQLOCK;
1788 if (kn->kn_fop->f_event(kn, hint))
1789 KNOTE_ACTIVATE(kn, 1);
1790 kn->kn_status &= ~KN_HASKQLOCK;
1796 if ((lockflags & KNF_LISTLOCKED) == 0)
1797 list->kl_unlock(list->kl_lockarg);
1801 * add a knote to a knlist
1804 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
1806 KNL_ASSERT_LOCK(knl, islocked);
1807 KQ_NOTOWNED(kn->kn_kq);
1808 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) ==
1809 (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED"));
1811 knl->kl_lock(knl->kl_lockarg);
1812 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
1814 knl->kl_unlock(knl->kl_lockarg);
1816 kn->kn_knlist = knl;
1817 kn->kn_status &= ~KN_DETACHED;
1818 KQ_UNLOCK(kn->kn_kq);
1822 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked)
1824 KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked"));
1825 KNL_ASSERT_LOCK(knl, knlislocked);
1826 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
1828 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX,
1829 ("knlist_remove called w/o knote being KN_INFLUX or already removed"));
1831 knl->kl_lock(knl->kl_lockarg);
1832 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
1833 kn->kn_knlist = NULL;
1835 knl->kl_unlock(knl->kl_lockarg);
1838 kn->kn_status |= KN_DETACHED;
1840 KQ_UNLOCK(kn->kn_kq);
1844 * remove all knotes from a specified klist
1847 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
1850 knlist_remove_kq(knl, kn, islocked, 0);
1854 * remove knote from a specified klist while in f_event handler.
1857 knlist_remove_inevent(struct knlist *knl, struct knote *kn)
1860 knlist_remove_kq(knl, kn, 1,
1861 (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK);
1865 knlist_empty(struct knlist *knl)
1868 KNL_ASSERT_LOCKED(knl);
1869 return SLIST_EMPTY(&knl->kl_list);
1872 static struct mtx knlist_lock;
1873 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
1875 static void knlist_mtx_lock(void *arg);
1876 static void knlist_mtx_unlock(void *arg);
1879 knlist_mtx_lock(void *arg)
1882 mtx_lock((struct mtx *)arg);
1886 knlist_mtx_unlock(void *arg)
1889 mtx_unlock((struct mtx *)arg);
1893 knlist_mtx_assert_locked(void *arg)
1896 mtx_assert((struct mtx *)arg, MA_OWNED);
1900 knlist_mtx_assert_unlocked(void *arg)
1903 mtx_assert((struct mtx *)arg, MA_NOTOWNED);
1907 knlist_rw_rlock(void *arg)
1910 rw_rlock((struct rwlock *)arg);
1914 knlist_rw_runlock(void *arg)
1917 rw_runlock((struct rwlock *)arg);
1921 knlist_rw_assert_locked(void *arg)
1924 rw_assert((struct rwlock *)arg, RA_LOCKED);
1928 knlist_rw_assert_unlocked(void *arg)
1931 rw_assert((struct rwlock *)arg, RA_UNLOCKED);
1935 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
1936 void (*kl_unlock)(void *),
1937 void (*kl_assert_locked)(void *), void (*kl_assert_unlocked)(void *))
1941 knl->kl_lockarg = &knlist_lock;
1943 knl->kl_lockarg = lock;
1945 if (kl_lock == NULL)
1946 knl->kl_lock = knlist_mtx_lock;
1948 knl->kl_lock = kl_lock;
1949 if (kl_unlock == NULL)
1950 knl->kl_unlock = knlist_mtx_unlock;
1952 knl->kl_unlock = kl_unlock;
1953 if (kl_assert_locked == NULL)
1954 knl->kl_assert_locked = knlist_mtx_assert_locked;
1956 knl->kl_assert_locked = kl_assert_locked;
1957 if (kl_assert_unlocked == NULL)
1958 knl->kl_assert_unlocked = knlist_mtx_assert_unlocked;
1960 knl->kl_assert_unlocked = kl_assert_unlocked;
1962 SLIST_INIT(&knl->kl_list);
1966 knlist_init_mtx(struct knlist *knl, struct mtx *lock)
1969 knlist_init(knl, lock, NULL, NULL, NULL, NULL);
1973 knlist_init_rw_reader(struct knlist *knl, struct rwlock *lock)
1976 knlist_init(knl, lock, knlist_rw_rlock, knlist_rw_runlock,
1977 knlist_rw_assert_locked, knlist_rw_assert_unlocked);
1981 knlist_destroy(struct knlist *knl)
1986 * if we run across this error, we need to find the offending
1987 * driver and have it call knlist_clear.
1989 if (!SLIST_EMPTY(&knl->kl_list))
1990 printf("WARNING: destroying knlist w/ knotes on it!\n");
1993 knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL;
1994 SLIST_INIT(&knl->kl_list);
1998 * Even if we are locked, we may need to drop the lock to allow any influx
1999 * knotes time to "settle".
2002 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
2004 struct knote *kn, *kn2;
2008 KNL_ASSERT_LOCKED(knl);
2010 KNL_ASSERT_UNLOCKED(knl);
2011 again: /* need to reacquire lock since we have dropped it */
2012 knl->kl_lock(knl->kl_lockarg);
2015 SLIST_FOREACH_SAFE(kn, &knl->kl_list, kn_selnext, kn2) {
2018 if ((kn->kn_status & KN_INFLUX)) {
2022 knlist_remove_kq(knl, kn, 1, 1);
2024 kn->kn_status |= KN_INFLUX | KN_DETACHED;
2028 /* Make sure cleared knotes disappear soon */
2029 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2035 if (!SLIST_EMPTY(&knl->kl_list)) {
2036 /* there are still KN_INFLUX remaining */
2037 kn = SLIST_FIRST(&knl->kl_list);
2040 KASSERT(kn->kn_status & KN_INFLUX,
2041 ("knote removed w/o list lock"));
2042 knl->kl_unlock(knl->kl_lockarg);
2043 kq->kq_state |= KQ_FLUXWAIT;
2044 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
2050 KNL_ASSERT_LOCKED(knl);
2052 knl->kl_unlock(knl->kl_lockarg);
2053 KNL_ASSERT_UNLOCKED(knl);
2058 * Remove all knotes referencing a specified fd must be called with FILEDESC
2059 * lock. This prevents a race where a new fd comes along and occupies the
2060 * entry and we attach a knote to the fd.
2063 knote_fdclose(struct thread *td, int fd)
2065 struct filedesc *fdp = td->td_proc->p_fd;
2070 FILEDESC_XLOCK_ASSERT(fdp);
2073 * We shouldn't have to worry about new kevents appearing on fd
2074 * since filedesc is locked.
2076 SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
2081 while (kq->kq_knlistsize > fd &&
2082 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
2083 if (kn->kn_status & KN_INFLUX) {
2084 /* someone else might be waiting on our knote */
2087 kq->kq_state |= KQ_FLUXWAIT;
2088 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
2091 kn->kn_status |= KN_INFLUX;
2093 if (!(kn->kn_status & KN_DETACHED))
2094 kn->kn_fop->f_detach(kn);
2104 knote_attach(struct knote *kn, struct kqueue *kq)
2108 KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX"));
2111 if (kn->kn_fop->f_isfd) {
2112 if (kn->kn_id >= kq->kq_knlistsize)
2114 list = &kq->kq_knlist[kn->kn_id];
2116 if (kq->kq_knhash == NULL)
2118 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2121 SLIST_INSERT_HEAD(list, kn, kn_link);
2127 * knote must already have been detached using the f_detach method.
2128 * no lock need to be held, it is assumed that the KN_INFLUX flag is set
2129 * to prevent other removal.
2132 knote_drop(struct knote *kn, struct thread *td)
2140 KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX,
2141 ("knote_drop called without KN_INFLUX set in kn_status"));
2144 if (kn->kn_fop->f_isfd)
2145 list = &kq->kq_knlist[kn->kn_id];
2147 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2149 if (!SLIST_EMPTY(list))
2150 SLIST_REMOVE(list, kn, knote, kn_link);
2151 if (kn->kn_status & KN_QUEUED)
2155 if (kn->kn_fop->f_isfd) {
2156 fdrop(kn->kn_fp, td);
2159 kqueue_fo_release(kn->kn_kevent.filter);
2165 knote_enqueue(struct knote *kn)
2167 struct kqueue *kq = kn->kn_kq;
2169 KQ_OWNED(kn->kn_kq);
2170 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
2172 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
2173 kn->kn_status |= KN_QUEUED;
2179 knote_dequeue(struct knote *kn)
2181 struct kqueue *kq = kn->kn_kq;
2183 KQ_OWNED(kn->kn_kq);
2184 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
2186 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
2187 kn->kn_status &= ~KN_QUEUED;
2195 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
2196 NULL, NULL, UMA_ALIGN_PTR, 0);
2198 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL);
2200 static struct knote *
2201 knote_alloc(int waitok)
2203 return ((struct knote *)uma_zalloc(knote_zone,
2204 (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO));
2208 knote_free(struct knote *kn)
2211 uma_zfree(knote_zone, kn);
2215 * Register the kev w/ the kq specified by fd.
2218 kqfd_register(int fd, struct kevent *kev, struct thread *td, int waitok)
2224 if ((error = fget(td, fd, CAP_POST_EVENT, &fp)) != 0)
2226 if ((error = kqueue_acquire(fp, &kq)) != 0)
2229 error = kqueue_register(kq, kev, td, waitok);
2231 kqueue_release(kq, 0);