2 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
3 * Copyright 2004 John-Mark Gurney <jmg@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_ktrace.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/mutex.h>
39 #include <sys/malloc.h>
40 #include <sys/unistd.h>
42 #include <sys/filedesc.h>
43 #include <sys/filio.h>
44 #include <sys/fcntl.h>
45 #include <sys/kthread.h>
46 #include <sys/selinfo.h>
47 #include <sys/queue.h>
48 #include <sys/event.h>
49 #include <sys/eventvar.h>
51 #include <sys/protosw.h>
52 #include <sys/sigio.h>
53 #include <sys/signalvar.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
57 #include <sys/sysctl.h>
58 #include <sys/sysproto.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/taskqueue.h>
63 #include <sys/ktrace.h>
68 static MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
71 * This lock is used if multiple kq locks are required. This possibly
72 * should be made into a per proc lock.
74 static struct mtx kq_global;
75 MTX_SYSINIT(kq_global, &kq_global, "kqueue order", MTX_DEF);
76 #define KQ_GLOBAL_LOCK(lck, haslck) do { \
81 #define KQ_GLOBAL_UNLOCK(lck, haslck) do { \
87 TASKQUEUE_DEFINE_THREAD(kqueue);
89 static int kevent_copyout(void *arg, struct kevent *kevp, int count);
90 static int kevent_copyin(void *arg, struct kevent *kevp, int count);
91 static int kqueue_register(struct kqueue *kq, struct kevent *kev,
92 struct thread *td, int waitok);
93 static int kqueue_acquire(struct file *fp, struct kqueue **kqp);
94 static void kqueue_release(struct kqueue *kq, int locked);
95 static int kqueue_expand(struct kqueue *kq, struct filterops *fops,
96 uintptr_t ident, int waitok);
97 static void kqueue_task(void *arg, int pending);
98 static int kqueue_scan(struct kqueue *kq, int maxevents,
99 struct kevent_copyops *k_ops,
100 const struct timespec *timeout,
101 struct kevent *keva, struct thread *td);
102 static void kqueue_wakeup(struct kqueue *kq);
103 static struct filterops *kqueue_fo_find(int filt);
104 static void kqueue_fo_release(int filt);
106 static fo_rdwr_t kqueue_read;
107 static fo_rdwr_t kqueue_write;
108 static fo_truncate_t kqueue_truncate;
109 static fo_ioctl_t kqueue_ioctl;
110 static fo_poll_t kqueue_poll;
111 static fo_kqfilter_t kqueue_kqfilter;
112 static fo_stat_t kqueue_stat;
113 static fo_close_t kqueue_close;
115 static struct fileops kqueueops = {
116 .fo_read = kqueue_read,
117 .fo_write = kqueue_write,
118 .fo_truncate = kqueue_truncate,
119 .fo_ioctl = kqueue_ioctl,
120 .fo_poll = kqueue_poll,
121 .fo_kqfilter = kqueue_kqfilter,
122 .fo_stat = kqueue_stat,
123 .fo_close = kqueue_close,
126 static int knote_attach(struct knote *kn, struct kqueue *kq);
127 static void knote_drop(struct knote *kn, struct thread *td);
128 static void knote_enqueue(struct knote *kn);
129 static void knote_dequeue(struct knote *kn);
130 static void knote_init(void);
131 static struct knote *knote_alloc(int waitok);
132 static void knote_free(struct knote *kn);
134 static void filt_kqdetach(struct knote *kn);
135 static int filt_kqueue(struct knote *kn, long hint);
136 static int filt_procattach(struct knote *kn);
137 static void filt_procdetach(struct knote *kn);
138 static int filt_proc(struct knote *kn, long hint);
139 static int filt_fileattach(struct knote *kn);
140 static void filt_timerexpire(void *knx);
141 static int filt_timerattach(struct knote *kn);
142 static void filt_timerdetach(struct knote *kn);
143 static int filt_timer(struct knote *kn, long hint);
145 static struct filterops file_filtops = {
147 .f_attach = filt_fileattach,
149 static struct filterops kqread_filtops = {
151 .f_detach = filt_kqdetach,
152 .f_event = filt_kqueue,
154 /* XXX - move to kern_proc.c? */
155 static struct filterops proc_filtops = {
157 .f_attach = filt_procattach,
158 .f_detach = filt_procdetach,
159 .f_event = filt_proc,
161 static struct filterops timer_filtops = {
163 .f_attach = filt_timerattach,
164 .f_detach = filt_timerdetach,
165 .f_event = filt_timer,
168 static uma_zone_t knote_zone;
169 static int kq_ncallouts = 0;
170 static int kq_calloutmax = (4 * 1024);
171 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
172 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
174 /* XXX - ensure not KN_INFLUX?? */
175 #define KNOTE_ACTIVATE(kn, islock) do { \
177 mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED); \
179 KQ_LOCK((kn)->kn_kq); \
180 (kn)->kn_status |= KN_ACTIVE; \
181 if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
182 knote_enqueue((kn)); \
184 KQ_UNLOCK((kn)->kn_kq); \
186 #define KQ_LOCK(kq) do { \
187 mtx_lock(&(kq)->kq_lock); \
189 #define KQ_FLUX_WAKEUP(kq) do { \
190 if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) { \
191 (kq)->kq_state &= ~KQ_FLUXWAIT; \
195 #define KQ_UNLOCK_FLUX(kq) do { \
196 KQ_FLUX_WAKEUP(kq); \
197 mtx_unlock(&(kq)->kq_lock); \
199 #define KQ_UNLOCK(kq) do { \
200 mtx_unlock(&(kq)->kq_lock); \
202 #define KQ_OWNED(kq) do { \
203 mtx_assert(&(kq)->kq_lock, MA_OWNED); \
205 #define KQ_NOTOWNED(kq) do { \
206 mtx_assert(&(kq)->kq_lock, MA_NOTOWNED); \
208 #define KN_LIST_LOCK(kn) do { \
209 if (kn->kn_knlist != NULL) \
210 kn->kn_knlist->kl_lock(kn->kn_knlist->kl_lockarg); \
212 #define KN_LIST_UNLOCK(kn) do { \
213 if (kn->kn_knlist != NULL) \
214 kn->kn_knlist->kl_unlock(kn->kn_knlist->kl_lockarg); \
216 #define KNL_ASSERT_LOCK(knl, islocked) do { \
218 KNL_ASSERT_LOCKED(knl); \
220 KNL_ASSERT_UNLOCKED(knl); \
223 #define KNL_ASSERT_LOCKED(knl) do { \
224 knl->kl_assert_locked((knl)->kl_lockarg); \
226 #define KNL_ASSERT_UNLOCKED(knl) do { \
227 knl->kl_assert_unlocked((knl)->kl_lockarg); \
229 #else /* !INVARIANTS */
230 #define KNL_ASSERT_LOCKED(knl) do {} while(0)
231 #define KNL_ASSERT_UNLOCKED(knl) do {} while (0)
232 #endif /* INVARIANTS */
234 #define KN_HASHSIZE 64 /* XXX should be tunable */
235 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
238 filt_nullattach(struct knote *kn)
244 struct filterops null_filtops = {
246 .f_attach = filt_nullattach,
249 /* XXX - make SYSINIT to add these, and move into respective modules. */
250 extern struct filterops sig_filtops;
251 extern struct filterops fs_filtops;
254 * Table for for all system-defined filters.
256 static struct mtx filterops_lock;
257 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops",
260 struct filterops *for_fop;
262 } sysfilt_ops[EVFILT_SYSCOUNT] = {
263 { &file_filtops }, /* EVFILT_READ */
264 { &file_filtops }, /* EVFILT_WRITE */
265 { &null_filtops }, /* EVFILT_AIO */
266 { &file_filtops }, /* EVFILT_VNODE */
267 { &proc_filtops }, /* EVFILT_PROC */
268 { &sig_filtops }, /* EVFILT_SIGNAL */
269 { &timer_filtops }, /* EVFILT_TIMER */
270 { &file_filtops }, /* EVFILT_NETDEV */
271 { &fs_filtops }, /* EVFILT_FS */
272 { &null_filtops }, /* EVFILT_LIO */
276 * Simple redirection for all cdevsw style objects to call their fo_kqfilter
280 filt_fileattach(struct knote *kn)
283 return (fo_kqfilter(kn->kn_fp, kn));
288 kqueue_kqfilter(struct file *fp, struct knote *kn)
290 struct kqueue *kq = kn->kn_fp->f_data;
292 if (kn->kn_filter != EVFILT_READ)
295 kn->kn_status |= KN_KQUEUE;
296 kn->kn_fop = &kqread_filtops;
297 knlist_add(&kq->kq_sel.si_note, kn, 0);
303 filt_kqdetach(struct knote *kn)
305 struct kqueue *kq = kn->kn_fp->f_data;
307 knlist_remove(&kq->kq_sel.si_note, kn, 0);
312 filt_kqueue(struct knote *kn, long hint)
314 struct kqueue *kq = kn->kn_fp->f_data;
316 kn->kn_data = kq->kq_count;
317 return (kn->kn_data > 0);
320 /* XXX - move to kern_proc.c? */
322 filt_procattach(struct knote *kn)
329 p = pfind(kn->kn_id);
330 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
331 p = zpfind(kn->kn_id);
333 } else if (p != NULL && (p->p_flag & P_WEXIT)) {
339 if ((error = p_cansee(curthread, p)))
342 kn->kn_ptr.p_proc = p;
343 kn->kn_flags |= EV_CLEAR; /* automatically set */
346 * internal flag indicating registration done by kernel
348 if (kn->kn_flags & EV_FLAG1) {
349 kn->kn_data = kn->kn_sdata; /* ppid */
350 kn->kn_fflags = NOTE_CHILD;
351 kn->kn_flags &= ~EV_FLAG1;
355 knlist_add(&p->p_klist, kn, 1);
358 * Immediately activate any exit notes if the target process is a
359 * zombie. This is necessary to handle the case where the target
360 * process, e.g. a child, dies before the kevent is registered.
362 if (immediate && filt_proc(kn, NOTE_EXIT))
363 KNOTE_ACTIVATE(kn, 0);
371 * The knote may be attached to a different process, which may exit,
372 * leaving nothing for the knote to be attached to. So when the process
373 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
374 * it will be deleted when read out. However, as part of the knote deletion,
375 * this routine is called, so a check is needed to avoid actually performing
376 * a detach, because the original process does not exist any more.
378 /* XXX - move to kern_proc.c? */
380 filt_procdetach(struct knote *kn)
384 p = kn->kn_ptr.p_proc;
385 knlist_remove(&p->p_klist, kn, 0);
386 kn->kn_ptr.p_proc = NULL;
389 /* XXX - move to kern_proc.c? */
391 filt_proc(struct knote *kn, long hint)
393 struct proc *p = kn->kn_ptr.p_proc;
397 * mask off extra data
399 event = (u_int)hint & NOTE_PCTRLMASK;
402 * if the user is interested in this event, record it.
404 if (kn->kn_sfflags & event)
405 kn->kn_fflags |= event;
408 * process is gone, so flag the event as finished.
410 if (event == NOTE_EXIT) {
411 if (!(kn->kn_status & KN_DETACHED))
412 knlist_remove_inevent(&p->p_klist, kn);
413 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
414 kn->kn_data = p->p_xstat;
415 kn->kn_ptr.p_proc = NULL;
419 return (kn->kn_fflags != 0);
423 * Called when the process forked. It mostly does the same as the
424 * knote(), activating all knotes registered to be activated when the
425 * process forked. Additionally, for each knote attached to the
426 * parent, check whether user wants to track the new process. If so
427 * attach a new knote to it, and immediately report an event with the
431 knote_fork(struct knlist *list, int pid)
440 list->kl_lock(list->kl_lockarg);
442 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
443 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX)
447 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
453 * The same as knote(), activate the event.
455 if ((kn->kn_sfflags & NOTE_TRACK) == 0) {
456 kn->kn_status |= KN_HASKQLOCK;
457 if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
458 KNOTE_ACTIVATE(kn, 1);
459 kn->kn_status &= ~KN_HASKQLOCK;
465 * The NOTE_TRACK case. In addition to the activation
466 * of the event, we need to register new event to
467 * track the child. Drop the locks in preparation for
468 * the call to kqueue_register().
470 kn->kn_status |= KN_INFLUX;
472 list->kl_unlock(list->kl_lockarg);
475 * Activate existing knote and register a knote with
479 kev.filter = kn->kn_filter;
480 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
481 kev.fflags = kn->kn_sfflags;
482 kev.data = kn->kn_id; /* parent */
483 kev.udata = kn->kn_kevent.udata;/* preserve udata */
484 error = kqueue_register(kq, &kev, NULL, 0);
485 if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
486 KNOTE_ACTIVATE(kn, 0);
488 kn->kn_fflags |= NOTE_TRACKERR;
490 kn->kn_status &= ~KN_INFLUX;
492 list->kl_lock(list->kl_lockarg);
494 list->kl_unlock(list->kl_lockarg);
498 timertoticks(intptr_t data)
503 tv.tv_sec = data / 1000;
504 tv.tv_usec = (data % 1000) * 1000;
505 tticks = tvtohz(&tv);
510 /* XXX - move to kern_timeout.c? */
512 filt_timerexpire(void *knx)
514 struct knote *kn = knx;
515 struct callout *calloutp;
518 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */
520 if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) {
521 calloutp = (struct callout *)kn->kn_hook;
522 callout_reset_curcpu(calloutp, timertoticks(kn->kn_sdata),
523 filt_timerexpire, kn);
528 * data contains amount of time to sleep, in milliseconds
530 /* XXX - move to kern_timeout.c? */
532 filt_timerattach(struct knote *kn)
534 struct callout *calloutp;
536 atomic_add_int(&kq_ncallouts, 1);
538 if (kq_ncallouts >= kq_calloutmax) {
539 atomic_add_int(&kq_ncallouts, -1);
543 kn->kn_flags |= EV_CLEAR; /* automatically set */
544 kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */
545 calloutp = malloc(sizeof(*calloutp), M_KQUEUE, M_WAITOK);
546 callout_init(calloutp, CALLOUT_MPSAFE);
547 kn->kn_hook = calloutp;
548 callout_reset_curcpu(calloutp, timertoticks(kn->kn_sdata),
549 filt_timerexpire, kn);
554 /* XXX - move to kern_timeout.c? */
556 filt_timerdetach(struct knote *kn)
558 struct callout *calloutp;
560 calloutp = (struct callout *)kn->kn_hook;
561 callout_drain(calloutp);
562 free(calloutp, M_KQUEUE);
563 atomic_add_int(&kq_ncallouts, -1);
564 kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */
567 /* XXX - move to kern_timeout.c? */
569 filt_timer(struct knote *kn, long hint)
572 return (kn->kn_data != 0);
576 kqueue(struct thread *td, struct kqueue_args *uap)
578 struct filedesc *fdp;
583 fdp = td->td_proc->p_fd;
584 error = falloc(td, &fp, &fd);
588 /* An extra reference on `nfp' has been held for us by falloc(). */
589 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
590 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK);
591 TAILQ_INIT(&kq->kq_head);
593 knlist_init_mtx(&kq->kq_sel.si_note, &kq->kq_lock);
594 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
597 SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
598 FILEDESC_XUNLOCK(fdp);
600 finit(fp, FREAD | FWRITE, DTYPE_KQUEUE, kq, &kqueueops);
603 td->td_retval[0] = fd;
608 #ifndef _SYS_SYSPROTO_H_
611 const struct kevent *changelist;
613 struct kevent *eventlist;
615 const struct timespec *timeout;
619 kevent(struct thread *td, struct kevent_args *uap)
621 struct timespec ts, *tsp;
622 struct kevent_copyops k_ops = { uap,
629 struct uio *ktruioin = NULL;
630 struct uio *ktruioout = NULL;
633 if (uap->timeout != NULL) {
634 error = copyin(uap->timeout, &ts, sizeof(ts));
642 if (KTRPOINT(td, KTR_GENIO)) {
643 ktriov.iov_base = uap->changelist;
644 ktriov.iov_len = uap->nchanges * sizeof(struct kevent);
645 ktruio = (struct uio){ .uio_iov = &ktriov, .uio_iovcnt = 1,
646 .uio_segflg = UIO_USERSPACE, .uio_rw = UIO_READ,
648 ktruioin = cloneuio(&ktruio);
649 ktriov.iov_base = uap->eventlist;
650 ktriov.iov_len = uap->nevents * sizeof(struct kevent);
651 ktruioout = cloneuio(&ktruio);
655 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
659 if (ktruioin != NULL) {
660 ktruioin->uio_resid = uap->nchanges * sizeof(struct kevent);
661 ktrgenio(uap->fd, UIO_WRITE, ktruioin, 0);
662 ktruioout->uio_resid = td->td_retval[0] * sizeof(struct kevent);
663 ktrgenio(uap->fd, UIO_READ, ktruioout, error);
671 * Copy 'count' items into the destination list pointed to by uap->eventlist.
674 kevent_copyout(void *arg, struct kevent *kevp, int count)
676 struct kevent_args *uap;
679 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
680 uap = (struct kevent_args *)arg;
682 error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
684 uap->eventlist += count;
689 * Copy 'count' items from the list pointed to by uap->changelist.
692 kevent_copyin(void *arg, struct kevent *kevp, int count)
694 struct kevent_args *uap;
697 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
698 uap = (struct kevent_args *)arg;
700 error = copyin(uap->changelist, kevp, count * sizeof *kevp);
702 uap->changelist += count;
707 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
708 struct kevent_copyops *k_ops, const struct timespec *timeout)
710 struct kevent keva[KQ_NEVENTS];
711 struct kevent *kevp, *changes;
714 int i, n, nerrors, error;
716 if ((error = fget(td, fd, &fp)) != 0)
718 if ((error = kqueue_acquire(fp, &kq)) != 0)
723 while (nchanges > 0) {
724 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
725 error = k_ops->k_copyin(k_ops->arg, keva, n);
729 for (i = 0; i < n; i++) {
733 kevp->flags &= ~EV_SYSFLAGS;
734 error = kqueue_register(kq, kevp, td, 1);
737 kevp->flags = EV_ERROR;
739 (void) k_ops->k_copyout(k_ops->arg,
751 td->td_retval[0] = nerrors;
756 error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td);
758 kqueue_release(kq, 0);
765 kqueue_add_filteropts(int filt, struct filterops *filtops)
769 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
771 "trying to add a filterop that is out of range: %d is beyond %d\n",
772 ~filt, EVFILT_SYSCOUNT);
775 mtx_lock(&filterops_lock);
776 if (sysfilt_ops[~filt].for_fop != &null_filtops &&
777 sysfilt_ops[~filt].for_fop != NULL)
780 sysfilt_ops[~filt].for_fop = filtops;
781 sysfilt_ops[~filt].for_refcnt = 0;
783 mtx_unlock(&filterops_lock);
789 kqueue_del_filteropts(int filt)
794 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
797 mtx_lock(&filterops_lock);
798 if (sysfilt_ops[~filt].for_fop == &null_filtops ||
799 sysfilt_ops[~filt].for_fop == NULL)
801 else if (sysfilt_ops[~filt].for_refcnt != 0)
804 sysfilt_ops[~filt].for_fop = &null_filtops;
805 sysfilt_ops[~filt].for_refcnt = 0;
807 mtx_unlock(&filterops_lock);
812 static struct filterops *
813 kqueue_fo_find(int filt)
816 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
819 mtx_lock(&filterops_lock);
820 sysfilt_ops[~filt].for_refcnt++;
821 if (sysfilt_ops[~filt].for_fop == NULL)
822 sysfilt_ops[~filt].for_fop = &null_filtops;
823 mtx_unlock(&filterops_lock);
825 return sysfilt_ops[~filt].for_fop;
829 kqueue_fo_release(int filt)
832 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
835 mtx_lock(&filterops_lock);
836 KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
837 ("filter object refcount not valid on release"));
838 sysfilt_ops[~filt].for_refcnt--;
839 mtx_unlock(&filterops_lock);
843 * A ref to kq (obtained via kqueue_acquire) must be held. waitok will
844 * influence if memory allocation should wait. Make sure it is 0 if you
848 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
850 struct filterops *fops;
852 struct knote *kn, *tkn;
853 int error, filt, event;
862 fops = kqueue_fo_find(filt);
866 tkn = knote_alloc(waitok); /* prevent waiting with locks */
870 KASSERT(td != NULL, ("td is NULL"));
871 error = fget(td, kev->ident, &fp);
875 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
876 kev->ident, 0) != 0) {
880 error = kqueue_expand(kq, fops, kev->ident, waitok);
886 if (fp->f_type == DTYPE_KQUEUE) {
888 * if we add some inteligence about what we are doing,
889 * we should be able to support events on ourselves.
890 * We need to know when we are doing this to prevent
891 * getting both the knlist lock and the kq lock since
892 * they are the same thing.
894 if (fp->f_data == kq) {
899 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
903 if (kev->ident < kq->kq_knlistsize) {
904 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
905 if (kev->filter == kn->kn_filter)
909 if ((kev->flags & EV_ADD) == EV_ADD)
910 kqueue_expand(kq, fops, kev->ident, waitok);
913 if (kq->kq_knhashmask != 0) {
916 list = &kq->kq_knhash[
917 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
918 SLIST_FOREACH(kn, list, kn_link)
919 if (kev->ident == kn->kn_id &&
920 kev->filter == kn->kn_filter)
925 /* knote is in the process of changing, wait for it to stablize. */
926 if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
931 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
932 kq->kq_state |= KQ_FLUXWAIT;
933 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
937 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
944 * kn now contains the matching knote, or NULL if no match
946 if (kev->flags & EV_ADD) {
959 * apply reference counts to knote structure, and
960 * do not release it at the end of this routine.
965 kn->kn_sfflags = kev->fflags;
966 kn->kn_sdata = kev->data;
969 kn->kn_kevent = *kev;
970 kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE |
971 EV_ENABLE | EV_DISABLE);
972 kn->kn_status = KN_INFLUX|KN_DETACHED;
974 error = knote_attach(kn, kq);
981 if ((error = kn->kn_fop->f_attach(kn)) != 0) {
988 * The user may change some filter values after the
989 * initial EV_ADD, but doing so will not reset any
990 * filter which has already been triggered.
992 kn->kn_status |= KN_INFLUX;
995 kn->kn_sfflags = kev->fflags;
996 kn->kn_sdata = kev->data;
997 kn->kn_kevent.udata = kev->udata;
1001 * We can get here with kn->kn_knlist == NULL.
1002 * This can happen when the initial attach event decides that
1003 * the event is "completed" already. i.e. filt_procattach
1004 * is called on a zombie process. It will call filt_proc
1005 * which will remove it from the list, and NULL kn_knlist.
1007 event = kn->kn_fop->f_event(kn, 0);
1010 KNOTE_ACTIVATE(kn, 1);
1011 kn->kn_status &= ~KN_INFLUX;
1013 } else if (kev->flags & EV_DELETE) {
1014 kn->kn_status |= KN_INFLUX;
1016 if (!(kn->kn_status & KN_DETACHED))
1017 kn->kn_fop->f_detach(kn);
1022 if ((kev->flags & EV_DISABLE) &&
1023 ((kn->kn_status & KN_DISABLED) == 0)) {
1024 kn->kn_status |= KN_DISABLED;
1027 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
1028 kn->kn_status &= ~KN_DISABLED;
1029 if ((kn->kn_status & KN_ACTIVE) &&
1030 ((kn->kn_status & KN_QUEUED) == 0))
1036 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1042 kqueue_fo_release(filt);
1047 kqueue_acquire(struct file *fp, struct kqueue **kqp)
1055 if (fp->f_type != DTYPE_KQUEUE || kq == NULL)
1059 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
1070 kqueue_release(struct kqueue *kq, int locked)
1077 if (kq->kq_refcnt == 1)
1078 wakeup(&kq->kq_refcnt);
1084 kqueue_schedtask(struct kqueue *kq)
1088 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1089 ("scheduling kqueue task while draining"));
1091 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1092 taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task);
1093 kq->kq_state |= KQ_TASKSCHED;
1098 * Expand the kq to make sure we have storage for fops/ident pair.
1100 * Return 0 on success (or no work necessary), return errno on failure.
1102 * Not calling hashinit w/ waitok (proper malloc flag) should be safe.
1103 * If kqueue_register is called from a non-fd context, there usually/should
1107 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
1110 struct klist *list, *tmp_knhash;
1111 u_long tmp_knhashmask;
1114 int mflag = waitok ? M_WAITOK : M_NOWAIT;
1120 if (kq->kq_knlistsize <= fd) {
1121 size = kq->kq_knlistsize;
1124 list = malloc(size * sizeof list, M_KQUEUE, mflag);
1128 if (kq->kq_knlistsize > fd) {
1129 free(list, M_KQUEUE);
1132 if (kq->kq_knlist != NULL) {
1133 bcopy(kq->kq_knlist, list,
1134 kq->kq_knlistsize * sizeof list);
1135 free(kq->kq_knlist, M_KQUEUE);
1136 kq->kq_knlist = NULL;
1138 bzero((caddr_t)list +
1139 kq->kq_knlistsize * sizeof list,
1140 (size - kq->kq_knlistsize) * sizeof list);
1141 kq->kq_knlistsize = size;
1142 kq->kq_knlist = list;
1147 if (kq->kq_knhashmask == 0) {
1148 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1150 if (tmp_knhash == NULL)
1153 if (kq->kq_knhashmask == 0) {
1154 kq->kq_knhash = tmp_knhash;
1155 kq->kq_knhashmask = tmp_knhashmask;
1157 free(tmp_knhash, M_KQUEUE);
1168 kqueue_task(void *arg, int pending)
1176 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1179 KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1181 kq->kq_state &= ~KQ_TASKSCHED;
1182 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1183 wakeup(&kq->kq_state);
1186 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1190 * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1191 * We treat KN_MARKER knotes as if they are INFLUX.
1194 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1195 const struct timespec *tsp, struct kevent *keva, struct thread *td)
1197 struct kevent *kevp;
1198 struct timeval atv, rtv, ttv;
1199 struct knote *kn, *marker;
1200 int count, timeout, nkev, error, influx;
1212 TIMESPEC_TO_TIMEVAL(&atv, tsp);
1213 if (itimerfix(&atv)) {
1217 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1220 timeout = atv.tv_sec > 24 * 60 * 60 ?
1221 24 * 60 * 60 * hz : tvtohz(&atv);
1222 getmicrouptime(&rtv);
1223 timevaladd(&atv, &rtv);
1229 marker = knote_alloc(1);
1230 if (marker == NULL) {
1234 marker->kn_status = KN_MARKER;
1239 if (atv.tv_sec || atv.tv_usec) {
1240 getmicrouptime(&rtv);
1241 if (timevalcmp(&rtv, &atv, >=))
1244 timevalsub(&ttv, &rtv);
1245 timeout = ttv.tv_sec > 24 * 60 * 60 ?
1246 24 * 60 * 60 * hz : tvtohz(&ttv);
1251 if (kq->kq_count == 0) {
1253 error = EWOULDBLOCK;
1255 kq->kq_state |= KQ_SLEEP;
1256 error = msleep(kq, &kq->kq_lock, PSOCK | PCATCH,
1261 /* don't restart after signals... */
1262 if (error == ERESTART)
1264 else if (error == EWOULDBLOCK)
1269 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1273 kn = TAILQ_FIRST(&kq->kq_head);
1275 if ((kn->kn_status == KN_MARKER && kn != marker) ||
1276 (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1281 kq->kq_state |= KQ_FLUXWAIT;
1282 error = msleep(kq, &kq->kq_lock, PSOCK,
1287 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1288 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1289 kn->kn_status &= ~KN_QUEUED;
1295 if (count == maxevents)
1299 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1300 ("KN_INFLUX set when not suppose to be"));
1302 if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
1303 kn->kn_status &= ~KN_QUEUED;
1304 kn->kn_status |= KN_INFLUX;
1308 * We don't need to lock the list since we've marked
1311 *kevp = kn->kn_kevent;
1312 if (!(kn->kn_status & KN_DETACHED))
1313 kn->kn_fop->f_detach(kn);
1318 kn->kn_status |= KN_INFLUX;
1320 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
1321 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1323 if (kn->kn_fop->f_event(kn, 0) == 0) {
1325 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1327 ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX);
1333 *kevp = kn->kn_kevent;
1335 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1336 if (kn->kn_flags & EV_CLEAR) {
1339 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1342 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1344 kn->kn_status &= ~(KN_INFLUX);
1349 /* we are returning a copy to the user */
1354 if (nkev == KQ_NEVENTS) {
1357 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1365 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1373 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1374 td->td_retval[0] = maxevents - count;
1380 * This could be expanded to call kqueue_scan, if desired.
1384 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1385 int flags, struct thread *td)
1392 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
1393 int flags, struct thread *td)
1400 kqueue_truncate(struct file *fp, off_t length, struct ucred *active_cred,
1409 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
1410 struct ucred *active_cred, struct thread *td)
1413 * Enabling sigio causes two major problems:
1414 * 1) infinite recursion:
1415 * Synopsys: kevent is being used to track signals and have FIOASYNC
1416 * set. On receipt of a signal this will cause a kqueue to recurse
1417 * into itself over and over. Sending the sigio causes the kqueue
1418 * to become ready, which in turn posts sigio again, forever.
1419 * Solution: this can be solved by setting a flag in the kqueue that
1420 * we have a SIGIO in progress.
1421 * 2) locking problems:
1422 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
1423 * us above the proc and pgrp locks.
1424 * Solution: Post a signal using an async mechanism, being sure to
1425 * record a generation count in the delivery so that we do not deliver
1426 * a signal to the wrong process.
1428 * Note, these two mechanisms are somewhat mutually exclusive!
1437 kq->kq_state |= KQ_ASYNC;
1439 kq->kq_state &= ~KQ_ASYNC;
1444 return (fsetown(*(int *)data, &kq->kq_sigio));
1447 *(int *)data = fgetown(&kq->kq_sigio);
1457 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
1464 if ((error = kqueue_acquire(fp, &kq)))
1468 if (events & (POLLIN | POLLRDNORM)) {
1470 revents |= events & (POLLIN | POLLRDNORM);
1472 selrecord(td, &kq->kq_sel);
1473 if (SEL_WAITING(&kq->kq_sel))
1474 kq->kq_state |= KQ_SEL;
1477 kqueue_release(kq, 1);
1484 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1488 bzero((void *)st, sizeof *st);
1490 * We no longer return kq_count because the unlocked value is useless.
1491 * If you spent all this time getting the count, why not spend your
1492 * syscall better by calling kevent?
1494 * XXX - This is needed for libc_r.
1496 st->st_mode = S_IFIFO;
1502 kqueue_close(struct file *fp, struct thread *td)
1504 struct kqueue *kq = fp->f_data;
1505 struct filedesc *fdp;
1510 if ((error = kqueue_acquire(fp, &kq)))
1515 KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
1516 ("kqueue already closing"));
1517 kq->kq_state |= KQ_CLOSING;
1518 if (kq->kq_refcnt > 1)
1519 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
1521 KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
1524 KASSERT(knlist_empty(&kq->kq_sel.si_note),
1525 ("kqueue's knlist not empty"));
1527 for (i = 0; i < kq->kq_knlistsize; i++) {
1528 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
1529 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1530 kq->kq_state |= KQ_FLUXWAIT;
1531 msleep(kq, &kq->kq_lock, PSOCK, "kqclo1", 0);
1534 kn->kn_status |= KN_INFLUX;
1536 if (!(kn->kn_status & KN_DETACHED))
1537 kn->kn_fop->f_detach(kn);
1542 if (kq->kq_knhashmask != 0) {
1543 for (i = 0; i <= kq->kq_knhashmask; i++) {
1544 while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
1545 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1546 kq->kq_state |= KQ_FLUXWAIT;
1547 msleep(kq, &kq->kq_lock, PSOCK,
1551 kn->kn_status |= KN_INFLUX;
1553 if (!(kn->kn_status & KN_DETACHED))
1554 kn->kn_fop->f_detach(kn);
1561 if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
1562 kq->kq_state |= KQ_TASKDRAIN;
1563 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
1566 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1567 selwakeuppri(&kq->kq_sel, PSOCK);
1568 if (!SEL_WAITING(&kq->kq_sel))
1569 kq->kq_state &= ~KQ_SEL;
1574 FILEDESC_XLOCK(fdp);
1575 SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list);
1576 FILEDESC_XUNLOCK(fdp);
1578 knlist_destroy(&kq->kq_sel.si_note);
1579 mtx_destroy(&kq->kq_lock);
1582 if (kq->kq_knhash != NULL)
1583 free(kq->kq_knhash, M_KQUEUE);
1584 if (kq->kq_knlist != NULL)
1585 free(kq->kq_knlist, M_KQUEUE);
1587 funsetown(&kq->kq_sigio);
1595 kqueue_wakeup(struct kqueue *kq)
1599 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
1600 kq->kq_state &= ~KQ_SLEEP;
1603 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1604 selwakeuppri(&kq->kq_sel, PSOCK);
1605 if (!SEL_WAITING(&kq->kq_sel))
1606 kq->kq_state &= ~KQ_SEL;
1608 if (!knlist_empty(&kq->kq_sel.si_note))
1609 kqueue_schedtask(kq);
1610 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
1611 pgsigio(&kq->kq_sigio, SIGIO, 0);
1616 * Walk down a list of knotes, activating them if their event has triggered.
1618 * There is a possibility to optimize in the case of one kq watching another.
1619 * Instead of scheduling a task to wake it up, you could pass enough state
1620 * down the chain to make up the parent kqueue. Make this code functional
1624 knote(struct knlist *list, long hint, int lockflags)
1633 KNL_ASSERT_LOCK(list, lockflags & KNF_LISTLOCKED);
1635 if ((lockflags & KNF_LISTLOCKED) == 0)
1636 list->kl_lock(list->kl_lockarg);
1639 * If we unlock the list lock (and set KN_INFLUX), we can eliminate
1640 * the kqueue scheduling, but this will introduce four
1641 * lock/unlock's for each knote to test. If we do, continue to use
1642 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is
1643 * only safe if you want to remove the current item, which we are
1646 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
1648 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1650 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1652 } else if ((lockflags & KNF_NOKQLOCK) != 0) {
1653 kn->kn_status |= KN_INFLUX;
1655 error = kn->kn_fop->f_event(kn, hint);
1657 kn->kn_status &= ~KN_INFLUX;
1659 KNOTE_ACTIVATE(kn, 1);
1662 kn->kn_status |= KN_HASKQLOCK;
1663 if (kn->kn_fop->f_event(kn, hint))
1664 KNOTE_ACTIVATE(kn, 1);
1665 kn->kn_status &= ~KN_HASKQLOCK;
1671 if ((lockflags & KNF_LISTLOCKED) == 0)
1672 list->kl_unlock(list->kl_lockarg);
1676 * add a knote to a knlist
1679 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
1681 KNL_ASSERT_LOCK(knl, islocked);
1682 KQ_NOTOWNED(kn->kn_kq);
1683 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) ==
1684 (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED"));
1686 knl->kl_lock(knl->kl_lockarg);
1687 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
1689 knl->kl_unlock(knl->kl_lockarg);
1691 kn->kn_knlist = knl;
1692 kn->kn_status &= ~KN_DETACHED;
1693 KQ_UNLOCK(kn->kn_kq);
1697 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked)
1699 KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked"));
1700 KNL_ASSERT_LOCK(knl, knlislocked);
1701 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
1703 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX,
1704 ("knlist_remove called w/o knote being KN_INFLUX or already removed"));
1706 knl->kl_lock(knl->kl_lockarg);
1707 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
1708 kn->kn_knlist = NULL;
1710 knl->kl_unlock(knl->kl_lockarg);
1713 kn->kn_status |= KN_DETACHED;
1715 KQ_UNLOCK(kn->kn_kq);
1719 * remove all knotes from a specified klist
1722 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
1725 knlist_remove_kq(knl, kn, islocked, 0);
1729 * remove knote from a specified klist while in f_event handler.
1732 knlist_remove_inevent(struct knlist *knl, struct knote *kn)
1735 knlist_remove_kq(knl, kn, 1,
1736 (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK);
1740 knlist_empty(struct knlist *knl)
1742 KNL_ASSERT_LOCKED(knl);
1743 return SLIST_EMPTY(&knl->kl_list);
1746 static struct mtx knlist_lock;
1747 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
1749 static void knlist_mtx_lock(void *arg);
1750 static void knlist_mtx_unlock(void *arg);
1753 knlist_mtx_lock(void *arg)
1755 mtx_lock((struct mtx *)arg);
1759 knlist_mtx_unlock(void *arg)
1761 mtx_unlock((struct mtx *)arg);
1765 knlist_mtx_assert_locked(void *arg)
1767 mtx_assert((struct mtx *)arg, MA_OWNED);
1771 knlist_mtx_assert_unlocked(void *arg)
1773 mtx_assert((struct mtx *)arg, MA_NOTOWNED);
1777 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
1778 void (*kl_unlock)(void *),
1779 void (*kl_assert_locked)(void *), void (*kl_assert_unlocked)(void *))
1783 knl->kl_lockarg = &knlist_lock;
1785 knl->kl_lockarg = lock;
1787 if (kl_lock == NULL)
1788 knl->kl_lock = knlist_mtx_lock;
1790 knl->kl_lock = kl_lock;
1791 if (kl_unlock == NULL)
1792 knl->kl_unlock = knlist_mtx_unlock;
1794 knl->kl_unlock = kl_unlock;
1795 if (kl_assert_locked == NULL)
1796 knl->kl_assert_locked = knlist_mtx_assert_locked;
1798 knl->kl_assert_locked = kl_assert_locked;
1799 if (kl_assert_unlocked == NULL)
1800 knl->kl_assert_unlocked = knlist_mtx_assert_unlocked;
1802 knl->kl_assert_unlocked = kl_assert_unlocked;
1804 SLIST_INIT(&knl->kl_list);
1808 knlist_init_mtx(struct knlist *knl, struct mtx *lock)
1811 knlist_init(knl, lock, NULL, NULL, NULL, NULL);
1815 knlist_destroy(struct knlist *knl)
1820 * if we run across this error, we need to find the offending
1821 * driver and have it call knlist_clear.
1823 if (!SLIST_EMPTY(&knl->kl_list))
1824 printf("WARNING: destroying knlist w/ knotes on it!\n");
1827 knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL;
1828 SLIST_INIT(&knl->kl_list);
1832 * Even if we are locked, we may need to drop the lock to allow any influx
1833 * knotes time to "settle".
1836 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
1838 struct knote *kn, *kn2;
1842 KNL_ASSERT_LOCKED(knl);
1844 KNL_ASSERT_UNLOCKED(knl);
1845 again: /* need to reacquire lock since we have dropped it */
1846 knl->kl_lock(knl->kl_lockarg);
1849 SLIST_FOREACH_SAFE(kn, &knl->kl_list, kn_selnext, kn2) {
1852 if ((kn->kn_status & KN_INFLUX)) {
1856 knlist_remove_kq(knl, kn, 1, 1);
1858 kn->kn_status |= KN_INFLUX | KN_DETACHED;
1862 /* Make sure cleared knotes disappear soon */
1863 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1869 if (!SLIST_EMPTY(&knl->kl_list)) {
1870 /* there are still KN_INFLUX remaining */
1871 kn = SLIST_FIRST(&knl->kl_list);
1874 KASSERT(kn->kn_status & KN_INFLUX,
1875 ("knote removed w/o list lock"));
1876 knl->kl_unlock(knl->kl_lockarg);
1877 kq->kq_state |= KQ_FLUXWAIT;
1878 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
1884 KNL_ASSERT_LOCKED(knl);
1886 knl->kl_unlock(knl->kl_lockarg);
1887 KNL_ASSERT_UNLOCKED(knl);
1892 * Remove all knotes referencing a specified fd must be called with FILEDESC
1893 * lock. This prevents a race where a new fd comes along and occupies the
1894 * entry and we attach a knote to the fd.
1897 knote_fdclose(struct thread *td, int fd)
1899 struct filedesc *fdp = td->td_proc->p_fd;
1904 FILEDESC_XLOCK_ASSERT(fdp);
1907 * We shouldn't have to worry about new kevents appearing on fd
1908 * since filedesc is locked.
1910 SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
1915 while (kq->kq_knlistsize > fd &&
1916 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
1917 if (kn->kn_status & KN_INFLUX) {
1918 /* someone else might be waiting on our knote */
1921 kq->kq_state |= KQ_FLUXWAIT;
1922 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
1925 kn->kn_status |= KN_INFLUX;
1927 if (!(kn->kn_status & KN_DETACHED))
1928 kn->kn_fop->f_detach(kn);
1938 knote_attach(struct knote *kn, struct kqueue *kq)
1942 KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX"));
1945 if (kn->kn_fop->f_isfd) {
1946 if (kn->kn_id >= kq->kq_knlistsize)
1948 list = &kq->kq_knlist[kn->kn_id];
1950 if (kq->kq_knhash == NULL)
1952 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1955 SLIST_INSERT_HEAD(list, kn, kn_link);
1961 * knote must already have been detached using the f_detach method.
1962 * no lock need to be held, it is assumed that the KN_INFLUX flag is set
1963 * to prevent other removal.
1966 knote_drop(struct knote *kn, struct thread *td)
1974 KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX,
1975 ("knote_drop called without KN_INFLUX set in kn_status"));
1978 if (kn->kn_fop->f_isfd)
1979 list = &kq->kq_knlist[kn->kn_id];
1981 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1983 if (!SLIST_EMPTY(list))
1984 SLIST_REMOVE(list, kn, knote, kn_link);
1985 if (kn->kn_status & KN_QUEUED)
1989 if (kn->kn_fop->f_isfd) {
1990 fdrop(kn->kn_fp, td);
1993 kqueue_fo_release(kn->kn_kevent.filter);
1999 knote_enqueue(struct knote *kn)
2001 struct kqueue *kq = kn->kn_kq;
2003 KQ_OWNED(kn->kn_kq);
2004 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
2006 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
2007 kn->kn_status |= KN_QUEUED;
2013 knote_dequeue(struct knote *kn)
2015 struct kqueue *kq = kn->kn_kq;
2017 KQ_OWNED(kn->kn_kq);
2018 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
2020 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
2021 kn->kn_status &= ~KN_QUEUED;
2029 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
2030 NULL, NULL, UMA_ALIGN_PTR, 0);
2032 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL);
2034 static struct knote *
2035 knote_alloc(int waitok)
2037 return ((struct knote *)uma_zalloc(knote_zone,
2038 (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO));
2042 knote_free(struct knote *kn)
2045 uma_zfree(knote_zone, kn);
2049 * Register the kev w/ the kq specified by fd.
2052 kqfd_register(int fd, struct kevent *kev, struct thread *td, int waitok)
2058 if ((error = fget(td, fd, &fp)) != 0)
2060 if ((error = kqueue_acquire(fp, &kq)) != 0)
2063 error = kqueue_register(kq, kev, td, waitok);
2065 kqueue_release(kq, 0);