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 =
146 { 1, filt_fileattach, NULL, NULL };
147 static struct filterops kqread_filtops =
148 { 1, NULL, filt_kqdetach, filt_kqueue };
149 /* XXX - move to kern_proc.c? */
150 static struct filterops proc_filtops =
151 { 0, filt_procattach, filt_procdetach, filt_proc };
152 static struct filterops timer_filtops =
153 { 0, filt_timerattach, filt_timerdetach, filt_timer };
155 static uma_zone_t knote_zone;
156 static int kq_ncallouts = 0;
157 static int kq_calloutmax = (4 * 1024);
158 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
159 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
161 /* XXX - ensure not KN_INFLUX?? */
162 #define KNOTE_ACTIVATE(kn, islock) do { \
164 mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED); \
166 KQ_LOCK((kn)->kn_kq); \
167 (kn)->kn_status |= KN_ACTIVE; \
168 if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \
169 knote_enqueue((kn)); \
171 KQ_UNLOCK((kn)->kn_kq); \
173 #define KQ_LOCK(kq) do { \
174 mtx_lock(&(kq)->kq_lock); \
176 #define KQ_FLUX_WAKEUP(kq) do { \
177 if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) { \
178 (kq)->kq_state &= ~KQ_FLUXWAIT; \
182 #define KQ_UNLOCK_FLUX(kq) do { \
183 KQ_FLUX_WAKEUP(kq); \
184 mtx_unlock(&(kq)->kq_lock); \
186 #define KQ_UNLOCK(kq) do { \
187 mtx_unlock(&(kq)->kq_lock); \
189 #define KQ_OWNED(kq) do { \
190 mtx_assert(&(kq)->kq_lock, MA_OWNED); \
192 #define KQ_NOTOWNED(kq) do { \
193 mtx_assert(&(kq)->kq_lock, MA_NOTOWNED); \
195 #define KN_LIST_LOCK(kn) do { \
196 if (kn->kn_knlist != NULL) \
197 kn->kn_knlist->kl_lock(kn->kn_knlist->kl_lockarg); \
199 #define KN_LIST_UNLOCK(kn) do { \
200 if (kn->kn_knlist != NULL) \
201 kn->kn_knlist->kl_unlock(kn->kn_knlist->kl_lockarg); \
203 #define KNL_ASSERT_LOCK(knl, islocked) do { \
205 KNL_ASSERT_LOCKED(knl); \
207 KNL_ASSERT_UNLOCKED(knl); \
210 #define KNL_ASSERT_LOCKED(knl) do { \
211 knl->kl_assert_locked((knl)->kl_lockarg); \
213 #define KNL_ASSERT_UNLOCKED(knl) do { \
214 knl->kl_assert_unlocked((knl)->kl_lockarg); \
216 #else /* !INVARIANTS */
217 #define KNL_ASSERT_LOCKED(knl) do {} while(0)
218 #define KNL_ASSERT_UNLOCKED(knl) do {} while (0)
219 #endif /* INVARIANTS */
221 #define KN_HASHSIZE 64 /* XXX should be tunable */
222 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask))
225 filt_nullattach(struct knote *kn)
231 struct filterops null_filtops =
232 { 0, filt_nullattach, NULL, NULL };
234 /* XXX - make SYSINIT to add these, and move into respective modules. */
235 extern struct filterops sig_filtops;
236 extern struct filterops fs_filtops;
239 * Table for for all system-defined filters.
241 static struct mtx filterops_lock;
242 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops",
245 struct filterops *for_fop;
247 } sysfilt_ops[EVFILT_SYSCOUNT] = {
248 { &file_filtops }, /* EVFILT_READ */
249 { &file_filtops }, /* EVFILT_WRITE */
250 { &null_filtops }, /* EVFILT_AIO */
251 { &file_filtops }, /* EVFILT_VNODE */
252 { &proc_filtops }, /* EVFILT_PROC */
253 { &sig_filtops }, /* EVFILT_SIGNAL */
254 { &timer_filtops }, /* EVFILT_TIMER */
255 { &file_filtops }, /* EVFILT_NETDEV */
256 { &fs_filtops }, /* EVFILT_FS */
257 { &null_filtops }, /* EVFILT_LIO */
261 * Simple redirection for all cdevsw style objects to call their fo_kqfilter
265 filt_fileattach(struct knote *kn)
268 return (fo_kqfilter(kn->kn_fp, kn));
273 kqueue_kqfilter(struct file *fp, struct knote *kn)
275 struct kqueue *kq = kn->kn_fp->f_data;
277 if (kn->kn_filter != EVFILT_READ)
280 kn->kn_status |= KN_KQUEUE;
281 kn->kn_fop = &kqread_filtops;
282 knlist_add(&kq->kq_sel.si_note, kn, 0);
288 filt_kqdetach(struct knote *kn)
290 struct kqueue *kq = kn->kn_fp->f_data;
292 knlist_remove(&kq->kq_sel.si_note, kn, 0);
297 filt_kqueue(struct knote *kn, long hint)
299 struct kqueue *kq = kn->kn_fp->f_data;
301 kn->kn_data = kq->kq_count;
302 return (kn->kn_data > 0);
305 /* XXX - move to kern_proc.c? */
307 filt_procattach(struct knote *kn)
314 p = pfind(kn->kn_id);
315 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
316 p = zpfind(kn->kn_id);
318 } else if (p != NULL && (p->p_flag & P_WEXIT)) {
324 if ((error = p_cansee(curthread, p)))
327 kn->kn_ptr.p_proc = p;
328 kn->kn_flags |= EV_CLEAR; /* automatically set */
331 * internal flag indicating registration done by kernel
333 if (kn->kn_flags & EV_FLAG1) {
334 kn->kn_data = kn->kn_sdata; /* ppid */
335 kn->kn_fflags = NOTE_CHILD;
336 kn->kn_flags &= ~EV_FLAG1;
340 knlist_add(&p->p_klist, kn, 1);
343 * Immediately activate any exit notes if the target process is a
344 * zombie. This is necessary to handle the case where the target
345 * process, e.g. a child, dies before the kevent is registered.
347 if (immediate && filt_proc(kn, NOTE_EXIT))
348 KNOTE_ACTIVATE(kn, 0);
356 * The knote may be attached to a different process, which may exit,
357 * leaving nothing for the knote to be attached to. So when the process
358 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
359 * it will be deleted when read out. However, as part of the knote deletion,
360 * this routine is called, so a check is needed to avoid actually performing
361 * a detach, because the original process does not exist any more.
363 /* XXX - move to kern_proc.c? */
365 filt_procdetach(struct knote *kn)
369 p = kn->kn_ptr.p_proc;
370 knlist_remove(&p->p_klist, kn, 0);
371 kn->kn_ptr.p_proc = NULL;
374 /* XXX - move to kern_proc.c? */
376 filt_proc(struct knote *kn, long hint)
378 struct proc *p = kn->kn_ptr.p_proc;
382 * mask off extra data
384 event = (u_int)hint & NOTE_PCTRLMASK;
387 * if the user is interested in this event, record it.
389 if (kn->kn_sfflags & event)
390 kn->kn_fflags |= event;
393 * process is gone, so flag the event as finished.
395 if (event == NOTE_EXIT) {
396 if (!(kn->kn_status & KN_DETACHED))
397 knlist_remove_inevent(&p->p_klist, kn);
398 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
399 kn->kn_data = p->p_xstat;
400 kn->kn_ptr.p_proc = NULL;
404 return (kn->kn_fflags != 0);
408 * Called when the process forked. It mostly does the same as the
409 * knote(), activating all knotes registered to be activated when the
410 * process forked. Additionally, for each knote attached to the
411 * parent, check whether user wants to track the new process. If so
412 * attach a new knote to it, and immediately report an event with the
416 knote_fork(struct knlist *list, int pid)
425 list->kl_lock(list->kl_lockarg);
427 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
428 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX)
432 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
438 * The same as knote(), activate the event.
440 if ((kn->kn_sfflags & NOTE_TRACK) == 0) {
441 kn->kn_status |= KN_HASKQLOCK;
442 if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
443 KNOTE_ACTIVATE(kn, 1);
444 kn->kn_status &= ~KN_HASKQLOCK;
450 * The NOTE_TRACK case. In addition to the activation
451 * of the event, we need to register new event to
452 * track the child. Drop the locks in preparation for
453 * the call to kqueue_register().
455 kn->kn_status |= KN_INFLUX;
457 list->kl_unlock(list->kl_lockarg);
460 * Activate existing knote and register a knote with
464 kev.filter = kn->kn_filter;
465 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
466 kev.fflags = kn->kn_sfflags;
467 kev.data = kn->kn_id; /* parent */
468 kev.udata = kn->kn_kevent.udata;/* preserve udata */
469 error = kqueue_register(kq, &kev, NULL, 0);
470 if (kn->kn_fop->f_event(kn, NOTE_FORK | pid))
471 KNOTE_ACTIVATE(kn, 0);
473 kn->kn_fflags |= NOTE_TRACKERR;
475 kn->kn_status &= ~KN_INFLUX;
477 list->kl_lock(list->kl_lockarg);
479 list->kl_unlock(list->kl_lockarg);
483 timertoticks(intptr_t data)
488 tv.tv_sec = data / 1000;
489 tv.tv_usec = (data % 1000) * 1000;
490 tticks = tvtohz(&tv);
495 /* XXX - move to kern_timeout.c? */
497 filt_timerexpire(void *knx)
499 struct knote *kn = knx;
500 struct callout *calloutp;
503 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */
505 if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) {
506 calloutp = (struct callout *)kn->kn_hook;
507 callout_reset_curcpu(calloutp, timertoticks(kn->kn_sdata),
508 filt_timerexpire, kn);
513 * data contains amount of time to sleep, in milliseconds
515 /* XXX - move to kern_timeout.c? */
517 filt_timerattach(struct knote *kn)
519 struct callout *calloutp;
521 atomic_add_int(&kq_ncallouts, 1);
523 if (kq_ncallouts >= kq_calloutmax) {
524 atomic_add_int(&kq_ncallouts, -1);
528 kn->kn_flags |= EV_CLEAR; /* automatically set */
529 kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */
530 calloutp = malloc(sizeof(*calloutp), M_KQUEUE, M_WAITOK);
531 callout_init(calloutp, CALLOUT_MPSAFE);
532 kn->kn_hook = calloutp;
533 callout_reset_curcpu(calloutp, timertoticks(kn->kn_sdata),
534 filt_timerexpire, kn);
539 /* XXX - move to kern_timeout.c? */
541 filt_timerdetach(struct knote *kn)
543 struct callout *calloutp;
545 calloutp = (struct callout *)kn->kn_hook;
546 callout_drain(calloutp);
547 free(calloutp, M_KQUEUE);
548 atomic_add_int(&kq_ncallouts, -1);
549 kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */
552 /* XXX - move to kern_timeout.c? */
554 filt_timer(struct knote *kn, long hint)
557 return (kn->kn_data != 0);
561 kqueue(struct thread *td, struct kqueue_args *uap)
563 struct filedesc *fdp;
568 fdp = td->td_proc->p_fd;
569 error = falloc(td, &fp, &fd);
573 /* An extra reference on `nfp' has been held for us by falloc(). */
574 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
575 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK);
576 TAILQ_INIT(&kq->kq_head);
578 knlist_init_mtx(&kq->kq_sel.si_note, &kq->kq_lock);
579 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
582 SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
583 FILEDESC_XUNLOCK(fdp);
585 finit(fp, FREAD | FWRITE, DTYPE_KQUEUE, kq, &kqueueops);
588 td->td_retval[0] = fd;
593 #ifndef _SYS_SYSPROTO_H_
596 const struct kevent *changelist;
598 struct kevent *eventlist;
600 const struct timespec *timeout;
604 kevent(struct thread *td, struct kevent_args *uap)
606 struct timespec ts, *tsp;
607 struct kevent_copyops k_ops = { uap,
614 struct uio *ktruioin = NULL;
615 struct uio *ktruioout = NULL;
618 if (uap->timeout != NULL) {
619 error = copyin(uap->timeout, &ts, sizeof(ts));
627 if (KTRPOINT(td, KTR_GENIO)) {
628 ktriov.iov_base = uap->changelist;
629 ktriov.iov_len = uap->nchanges * sizeof(struct kevent);
630 ktruio = (struct uio){ .uio_iov = &ktriov, .uio_iovcnt = 1,
631 .uio_segflg = UIO_USERSPACE, .uio_rw = UIO_READ,
633 ktruioin = cloneuio(&ktruio);
634 ktriov.iov_base = uap->eventlist;
635 ktriov.iov_len = uap->nevents * sizeof(struct kevent);
636 ktruioout = cloneuio(&ktruio);
640 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
644 if (ktruioin != NULL) {
645 ktruioin->uio_resid = uap->nchanges * sizeof(struct kevent);
646 ktrgenio(uap->fd, UIO_WRITE, ktruioin, 0);
647 ktruioout->uio_resid = td->td_retval[0] * sizeof(struct kevent);
648 ktrgenio(uap->fd, UIO_READ, ktruioout, error);
656 * Copy 'count' items into the destination list pointed to by uap->eventlist.
659 kevent_copyout(void *arg, struct kevent *kevp, int count)
661 struct kevent_args *uap;
664 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
665 uap = (struct kevent_args *)arg;
667 error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
669 uap->eventlist += count;
674 * Copy 'count' items from the list pointed to by uap->changelist.
677 kevent_copyin(void *arg, struct kevent *kevp, int count)
679 struct kevent_args *uap;
682 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
683 uap = (struct kevent_args *)arg;
685 error = copyin(uap->changelist, kevp, count * sizeof *kevp);
687 uap->changelist += count;
692 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
693 struct kevent_copyops *k_ops, const struct timespec *timeout)
695 struct kevent keva[KQ_NEVENTS];
696 struct kevent *kevp, *changes;
699 int i, n, nerrors, error;
701 if ((error = fget(td, fd, &fp)) != 0)
703 if ((error = kqueue_acquire(fp, &kq)) != 0)
708 while (nchanges > 0) {
709 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
710 error = k_ops->k_copyin(k_ops->arg, keva, n);
714 for (i = 0; i < n; i++) {
718 kevp->flags &= ~EV_SYSFLAGS;
719 error = kqueue_register(kq, kevp, td, 1);
722 kevp->flags = EV_ERROR;
724 (void) k_ops->k_copyout(k_ops->arg,
736 td->td_retval[0] = nerrors;
741 error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td);
743 kqueue_release(kq, 0);
750 kqueue_add_filteropts(int filt, struct filterops *filtops)
754 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
756 "trying to add a filterop that is out of range: %d is beyond %d\n",
757 ~filt, EVFILT_SYSCOUNT);
760 mtx_lock(&filterops_lock);
761 if (sysfilt_ops[~filt].for_fop != &null_filtops &&
762 sysfilt_ops[~filt].for_fop != NULL)
765 sysfilt_ops[~filt].for_fop = filtops;
766 sysfilt_ops[~filt].for_refcnt = 0;
768 mtx_unlock(&filterops_lock);
774 kqueue_del_filteropts(int filt)
779 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
782 mtx_lock(&filterops_lock);
783 if (sysfilt_ops[~filt].for_fop == &null_filtops ||
784 sysfilt_ops[~filt].for_fop == NULL)
786 else if (sysfilt_ops[~filt].for_refcnt != 0)
789 sysfilt_ops[~filt].for_fop = &null_filtops;
790 sysfilt_ops[~filt].for_refcnt = 0;
792 mtx_unlock(&filterops_lock);
797 static struct filterops *
798 kqueue_fo_find(int filt)
801 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
804 mtx_lock(&filterops_lock);
805 sysfilt_ops[~filt].for_refcnt++;
806 if (sysfilt_ops[~filt].for_fop == NULL)
807 sysfilt_ops[~filt].for_fop = &null_filtops;
808 mtx_unlock(&filterops_lock);
810 return sysfilt_ops[~filt].for_fop;
814 kqueue_fo_release(int filt)
817 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
820 mtx_lock(&filterops_lock);
821 KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
822 ("filter object refcount not valid on release"));
823 sysfilt_ops[~filt].for_refcnt--;
824 mtx_unlock(&filterops_lock);
828 * A ref to kq (obtained via kqueue_acquire) must be held. waitok will
829 * influence if memory allocation should wait. Make sure it is 0 if you
833 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok)
835 struct filterops *fops;
837 struct knote *kn, *tkn;
838 int error, filt, event;
847 fops = kqueue_fo_find(filt);
851 tkn = knote_alloc(waitok); /* prevent waiting with locks */
855 KASSERT(td != NULL, ("td is NULL"));
856 error = fget(td, kev->ident, &fp);
860 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
861 kev->ident, 0) != 0) {
865 error = kqueue_expand(kq, fops, kev->ident, waitok);
871 if (fp->f_type == DTYPE_KQUEUE) {
873 * if we add some inteligence about what we are doing,
874 * we should be able to support events on ourselves.
875 * We need to know when we are doing this to prevent
876 * getting both the knlist lock and the kq lock since
877 * they are the same thing.
879 if (fp->f_data == kq) {
884 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
888 if (kev->ident < kq->kq_knlistsize) {
889 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
890 if (kev->filter == kn->kn_filter)
894 if ((kev->flags & EV_ADD) == EV_ADD)
895 kqueue_expand(kq, fops, kev->ident, waitok);
898 if (kq->kq_knhashmask != 0) {
901 list = &kq->kq_knhash[
902 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
903 SLIST_FOREACH(kn, list, kn_link)
904 if (kev->ident == kn->kn_id &&
905 kev->filter == kn->kn_filter)
910 /* knote is in the process of changing, wait for it to stablize. */
911 if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
916 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
917 kq->kq_state |= KQ_FLUXWAIT;
918 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
922 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
929 * kn now contains the matching knote, or NULL if no match
931 if (kev->flags & EV_ADD) {
944 * apply reference counts to knote structure, and
945 * do not release it at the end of this routine.
950 kn->kn_sfflags = kev->fflags;
951 kn->kn_sdata = kev->data;
954 kn->kn_kevent = *kev;
955 kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE |
956 EV_ENABLE | EV_DISABLE);
957 kn->kn_status = KN_INFLUX|KN_DETACHED;
959 error = knote_attach(kn, kq);
966 if ((error = kn->kn_fop->f_attach(kn)) != 0) {
973 * The user may change some filter values after the
974 * initial EV_ADD, but doing so will not reset any
975 * filter which has already been triggered.
977 kn->kn_status |= KN_INFLUX;
980 kn->kn_sfflags = kev->fflags;
981 kn->kn_sdata = kev->data;
982 kn->kn_kevent.udata = kev->udata;
986 * We can get here with kn->kn_knlist == NULL.
987 * This can happen when the initial attach event decides that
988 * the event is "completed" already. i.e. filt_procattach
989 * is called on a zombie process. It will call filt_proc
990 * which will remove it from the list, and NULL kn_knlist.
992 event = kn->kn_fop->f_event(kn, 0);
995 KNOTE_ACTIVATE(kn, 1);
996 kn->kn_status &= ~KN_INFLUX;
998 } else if (kev->flags & EV_DELETE) {
999 kn->kn_status |= KN_INFLUX;
1001 if (!(kn->kn_status & KN_DETACHED))
1002 kn->kn_fop->f_detach(kn);
1007 if ((kev->flags & EV_DISABLE) &&
1008 ((kn->kn_status & KN_DISABLED) == 0)) {
1009 kn->kn_status |= KN_DISABLED;
1012 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
1013 kn->kn_status &= ~KN_DISABLED;
1014 if ((kn->kn_status & KN_ACTIVE) &&
1015 ((kn->kn_status & KN_QUEUED) == 0))
1021 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1027 kqueue_fo_release(filt);
1032 kqueue_acquire(struct file *fp, struct kqueue **kqp)
1040 if (fp->f_type != DTYPE_KQUEUE || kq == NULL)
1044 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
1055 kqueue_release(struct kqueue *kq, int locked)
1062 if (kq->kq_refcnt == 1)
1063 wakeup(&kq->kq_refcnt);
1069 kqueue_schedtask(struct kqueue *kq)
1073 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1074 ("scheduling kqueue task while draining"));
1076 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1077 taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task);
1078 kq->kq_state |= KQ_TASKSCHED;
1083 * Expand the kq to make sure we have storage for fops/ident pair.
1085 * Return 0 on success (or no work necessary), return errno on failure.
1087 * Not calling hashinit w/ waitok (proper malloc flag) should be safe.
1088 * If kqueue_register is called from a non-fd context, there usually/should
1092 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident,
1095 struct klist *list, *tmp_knhash;
1096 u_long tmp_knhashmask;
1099 int mflag = waitok ? M_WAITOK : M_NOWAIT;
1105 if (kq->kq_knlistsize <= fd) {
1106 size = kq->kq_knlistsize;
1109 list = malloc(size * sizeof list, M_KQUEUE, mflag);
1113 if (kq->kq_knlistsize > fd) {
1114 free(list, M_KQUEUE);
1117 if (kq->kq_knlist != NULL) {
1118 bcopy(kq->kq_knlist, list,
1119 kq->kq_knlistsize * sizeof list);
1120 free(kq->kq_knlist, M_KQUEUE);
1121 kq->kq_knlist = NULL;
1123 bzero((caddr_t)list +
1124 kq->kq_knlistsize * sizeof list,
1125 (size - kq->kq_knlistsize) * sizeof list);
1126 kq->kq_knlistsize = size;
1127 kq->kq_knlist = list;
1132 if (kq->kq_knhashmask == 0) {
1133 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1135 if (tmp_knhash == NULL)
1138 if (kq->kq_knhashmask == 0) {
1139 kq->kq_knhash = tmp_knhash;
1140 kq->kq_knhashmask = tmp_knhashmask;
1142 free(tmp_knhash, M_KQUEUE);
1153 kqueue_task(void *arg, int pending)
1161 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1164 KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1166 kq->kq_state &= ~KQ_TASKSCHED;
1167 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1168 wakeup(&kq->kq_state);
1171 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1175 * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1176 * We treat KN_MARKER knotes as if they are INFLUX.
1179 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1180 const struct timespec *tsp, struct kevent *keva, struct thread *td)
1182 struct kevent *kevp;
1183 struct timeval atv, rtv, ttv;
1184 struct knote *kn, *marker;
1185 int count, timeout, nkev, error, influx;
1197 TIMESPEC_TO_TIMEVAL(&atv, tsp);
1198 if (itimerfix(&atv)) {
1202 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1205 timeout = atv.tv_sec > 24 * 60 * 60 ?
1206 24 * 60 * 60 * hz : tvtohz(&atv);
1207 getmicrouptime(&rtv);
1208 timevaladd(&atv, &rtv);
1214 marker = knote_alloc(1);
1215 if (marker == NULL) {
1219 marker->kn_status = KN_MARKER;
1224 if (atv.tv_sec || atv.tv_usec) {
1225 getmicrouptime(&rtv);
1226 if (timevalcmp(&rtv, &atv, >=))
1229 timevalsub(&ttv, &rtv);
1230 timeout = ttv.tv_sec > 24 * 60 * 60 ?
1231 24 * 60 * 60 * hz : tvtohz(&ttv);
1236 if (kq->kq_count == 0) {
1238 error = EWOULDBLOCK;
1240 kq->kq_state |= KQ_SLEEP;
1241 error = msleep(kq, &kq->kq_lock, PSOCK | PCATCH,
1246 /* don't restart after signals... */
1247 if (error == ERESTART)
1249 else if (error == EWOULDBLOCK)
1254 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1258 kn = TAILQ_FIRST(&kq->kq_head);
1260 if ((kn->kn_status == KN_MARKER && kn != marker) ||
1261 (kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1266 kq->kq_state |= KQ_FLUXWAIT;
1267 error = msleep(kq, &kq->kq_lock, PSOCK,
1272 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1273 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1274 kn->kn_status &= ~KN_QUEUED;
1280 if (count == maxevents)
1284 KASSERT((kn->kn_status & KN_INFLUX) == 0,
1285 ("KN_INFLUX set when not suppose to be"));
1287 if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
1288 kn->kn_status &= ~KN_QUEUED;
1289 kn->kn_status |= KN_INFLUX;
1293 * We don't need to lock the list since we've marked
1296 *kevp = kn->kn_kevent;
1297 if (!(kn->kn_status & KN_DETACHED))
1298 kn->kn_fop->f_detach(kn);
1303 kn->kn_status |= KN_INFLUX;
1305 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
1306 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1308 if (kn->kn_fop->f_event(kn, 0) == 0) {
1310 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1312 ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX);
1318 *kevp = kn->kn_kevent;
1320 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1321 if (kn->kn_flags & EV_CLEAR) {
1324 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1327 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1329 kn->kn_status &= ~(KN_INFLUX);
1334 /* we are returning a copy to the user */
1339 if (nkev == KQ_NEVENTS) {
1342 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1350 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
1358 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
1359 td->td_retval[0] = maxevents - count;
1365 * This could be expanded to call kqueue_scan, if desired.
1369 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1370 int flags, struct thread *td)
1377 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
1378 int flags, struct thread *td)
1385 kqueue_truncate(struct file *fp, off_t length, struct ucred *active_cred,
1394 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
1395 struct ucred *active_cred, struct thread *td)
1398 * Enabling sigio causes two major problems:
1399 * 1) infinite recursion:
1400 * Synopsys: kevent is being used to track signals and have FIOASYNC
1401 * set. On receipt of a signal this will cause a kqueue to recurse
1402 * into itself over and over. Sending the sigio causes the kqueue
1403 * to become ready, which in turn posts sigio again, forever.
1404 * Solution: this can be solved by setting a flag in the kqueue that
1405 * we have a SIGIO in progress.
1406 * 2) locking problems:
1407 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
1408 * us above the proc and pgrp locks.
1409 * Solution: Post a signal using an async mechanism, being sure to
1410 * record a generation count in the delivery so that we do not deliver
1411 * a signal to the wrong process.
1413 * Note, these two mechanisms are somewhat mutually exclusive!
1422 kq->kq_state |= KQ_ASYNC;
1424 kq->kq_state &= ~KQ_ASYNC;
1429 return (fsetown(*(int *)data, &kq->kq_sigio));
1432 *(int *)data = fgetown(&kq->kq_sigio);
1442 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
1449 if ((error = kqueue_acquire(fp, &kq)))
1453 if (events & (POLLIN | POLLRDNORM)) {
1455 revents |= events & (POLLIN | POLLRDNORM);
1457 selrecord(td, &kq->kq_sel);
1458 if (SEL_WAITING(&kq->kq_sel))
1459 kq->kq_state |= KQ_SEL;
1462 kqueue_release(kq, 1);
1469 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1473 bzero((void *)st, sizeof *st);
1475 * We no longer return kq_count because the unlocked value is useless.
1476 * If you spent all this time getting the count, why not spend your
1477 * syscall better by calling kevent?
1479 * XXX - This is needed for libc_r.
1481 st->st_mode = S_IFIFO;
1487 kqueue_close(struct file *fp, struct thread *td)
1489 struct kqueue *kq = fp->f_data;
1490 struct filedesc *fdp;
1495 if ((error = kqueue_acquire(fp, &kq)))
1500 KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
1501 ("kqueue already closing"));
1502 kq->kq_state |= KQ_CLOSING;
1503 if (kq->kq_refcnt > 1)
1504 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
1506 KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
1509 KASSERT(knlist_empty(&kq->kq_sel.si_note),
1510 ("kqueue's knlist not empty"));
1512 for (i = 0; i < kq->kq_knlistsize; i++) {
1513 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
1514 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1515 kq->kq_state |= KQ_FLUXWAIT;
1516 msleep(kq, &kq->kq_lock, PSOCK, "kqclo1", 0);
1519 kn->kn_status |= KN_INFLUX;
1521 if (!(kn->kn_status & KN_DETACHED))
1522 kn->kn_fop->f_detach(kn);
1527 if (kq->kq_knhashmask != 0) {
1528 for (i = 0; i <= kq->kq_knhashmask; i++) {
1529 while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
1530 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1531 kq->kq_state |= KQ_FLUXWAIT;
1532 msleep(kq, &kq->kq_lock, PSOCK,
1536 kn->kn_status |= KN_INFLUX;
1538 if (!(kn->kn_status & KN_DETACHED))
1539 kn->kn_fop->f_detach(kn);
1546 if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
1547 kq->kq_state |= KQ_TASKDRAIN;
1548 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
1551 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1552 selwakeuppri(&kq->kq_sel, PSOCK);
1553 if (!SEL_WAITING(&kq->kq_sel))
1554 kq->kq_state &= ~KQ_SEL;
1559 FILEDESC_XLOCK(fdp);
1560 SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list);
1561 FILEDESC_XUNLOCK(fdp);
1563 knlist_destroy(&kq->kq_sel.si_note);
1564 mtx_destroy(&kq->kq_lock);
1567 if (kq->kq_knhash != NULL)
1568 free(kq->kq_knhash, M_KQUEUE);
1569 if (kq->kq_knlist != NULL)
1570 free(kq->kq_knlist, M_KQUEUE);
1572 funsetown(&kq->kq_sigio);
1580 kqueue_wakeup(struct kqueue *kq)
1584 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
1585 kq->kq_state &= ~KQ_SLEEP;
1588 if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
1589 selwakeuppri(&kq->kq_sel, PSOCK);
1590 if (!SEL_WAITING(&kq->kq_sel))
1591 kq->kq_state &= ~KQ_SEL;
1593 if (!knlist_empty(&kq->kq_sel.si_note))
1594 kqueue_schedtask(kq);
1595 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
1596 pgsigio(&kq->kq_sigio, SIGIO, 0);
1601 * Walk down a list of knotes, activating them if their event has triggered.
1603 * There is a possibility to optimize in the case of one kq watching another.
1604 * Instead of scheduling a task to wake it up, you could pass enough state
1605 * down the chain to make up the parent kqueue. Make this code functional
1609 knote(struct knlist *list, long hint, int lockflags)
1618 KNL_ASSERT_LOCK(list, lockflags & KNF_LISTLOCKED);
1620 if ((lockflags & KNF_LISTLOCKED) == 0)
1621 list->kl_lock(list->kl_lockarg);
1624 * If we unlock the list lock (and set KN_INFLUX), we can eliminate
1625 * the kqueue scheduling, but this will introduce four
1626 * lock/unlock's for each knote to test. If we do, continue to use
1627 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is
1628 * only safe if you want to remove the current item, which we are
1631 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
1633 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) {
1635 if ((kn->kn_status & KN_INFLUX) == KN_INFLUX) {
1637 } else if ((lockflags & KNF_NOKQLOCK) != 0) {
1638 kn->kn_status |= KN_INFLUX;
1640 error = kn->kn_fop->f_event(kn, hint);
1642 kn->kn_status &= ~KN_INFLUX;
1644 KNOTE_ACTIVATE(kn, 1);
1647 kn->kn_status |= KN_HASKQLOCK;
1648 if (kn->kn_fop->f_event(kn, hint))
1649 KNOTE_ACTIVATE(kn, 1);
1650 kn->kn_status &= ~KN_HASKQLOCK;
1656 if ((lockflags & KNF_LISTLOCKED) == 0)
1657 list->kl_unlock(list->kl_lockarg);
1661 * add a knote to a knlist
1664 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
1666 KNL_ASSERT_LOCK(knl, islocked);
1667 KQ_NOTOWNED(kn->kn_kq);
1668 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) ==
1669 (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED"));
1671 knl->kl_lock(knl->kl_lockarg);
1672 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
1674 knl->kl_unlock(knl->kl_lockarg);
1676 kn->kn_knlist = knl;
1677 kn->kn_status &= ~KN_DETACHED;
1678 KQ_UNLOCK(kn->kn_kq);
1682 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked)
1684 KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked"));
1685 KNL_ASSERT_LOCK(knl, knlislocked);
1686 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
1688 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX,
1689 ("knlist_remove called w/o knote being KN_INFLUX or already removed"));
1691 knl->kl_lock(knl->kl_lockarg);
1692 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
1693 kn->kn_knlist = NULL;
1695 knl->kl_unlock(knl->kl_lockarg);
1698 kn->kn_status |= KN_DETACHED;
1700 KQ_UNLOCK(kn->kn_kq);
1704 * remove all knotes from a specified klist
1707 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
1710 knlist_remove_kq(knl, kn, islocked, 0);
1714 * remove knote from a specified klist while in f_event handler.
1717 knlist_remove_inevent(struct knlist *knl, struct knote *kn)
1720 knlist_remove_kq(knl, kn, 1,
1721 (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK);
1725 knlist_empty(struct knlist *knl)
1727 KNL_ASSERT_LOCKED(knl);
1728 return SLIST_EMPTY(&knl->kl_list);
1731 static struct mtx knlist_lock;
1732 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
1734 static void knlist_mtx_lock(void *arg);
1735 static void knlist_mtx_unlock(void *arg);
1738 knlist_mtx_lock(void *arg)
1740 mtx_lock((struct mtx *)arg);
1744 knlist_mtx_unlock(void *arg)
1746 mtx_unlock((struct mtx *)arg);
1750 knlist_mtx_assert_locked(void *arg)
1752 mtx_assert((struct mtx *)arg, MA_OWNED);
1756 knlist_mtx_assert_unlocked(void *arg)
1758 mtx_assert((struct mtx *)arg, MA_NOTOWNED);
1762 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
1763 void (*kl_unlock)(void *),
1764 void (*kl_assert_locked)(void *), void (*kl_assert_unlocked)(void *))
1768 knl->kl_lockarg = &knlist_lock;
1770 knl->kl_lockarg = lock;
1772 if (kl_lock == NULL)
1773 knl->kl_lock = knlist_mtx_lock;
1775 knl->kl_lock = kl_lock;
1776 if (kl_unlock == NULL)
1777 knl->kl_unlock = knlist_mtx_unlock;
1779 knl->kl_unlock = kl_unlock;
1780 if (kl_assert_locked == NULL)
1781 knl->kl_assert_locked = knlist_mtx_assert_locked;
1783 knl->kl_assert_locked = kl_assert_locked;
1784 if (kl_assert_unlocked == NULL)
1785 knl->kl_assert_unlocked = knlist_mtx_assert_unlocked;
1787 knl->kl_assert_unlocked = kl_assert_unlocked;
1789 SLIST_INIT(&knl->kl_list);
1793 knlist_init_mtx(struct knlist *knl, struct mtx *lock)
1796 knlist_init(knl, lock, NULL, NULL, NULL, NULL);
1800 knlist_destroy(struct knlist *knl)
1805 * if we run across this error, we need to find the offending
1806 * driver and have it call knlist_clear.
1808 if (!SLIST_EMPTY(&knl->kl_list))
1809 printf("WARNING: destroying knlist w/ knotes on it!\n");
1812 knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL;
1813 SLIST_INIT(&knl->kl_list);
1817 * Even if we are locked, we may need to drop the lock to allow any influx
1818 * knotes time to "settle".
1821 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
1823 struct knote *kn, *kn2;
1827 KNL_ASSERT_LOCKED(knl);
1829 KNL_ASSERT_UNLOCKED(knl);
1830 again: /* need to reacquire lock since we have dropped it */
1831 knl->kl_lock(knl->kl_lockarg);
1834 SLIST_FOREACH_SAFE(kn, &knl->kl_list, kn_selnext, kn2) {
1837 if ((kn->kn_status & KN_INFLUX)) {
1841 knlist_remove_kq(knl, kn, 1, 1);
1843 kn->kn_status |= KN_INFLUX | KN_DETACHED;
1847 /* Make sure cleared knotes disappear soon */
1848 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1854 if (!SLIST_EMPTY(&knl->kl_list)) {
1855 /* there are still KN_INFLUX remaining */
1856 kn = SLIST_FIRST(&knl->kl_list);
1859 KASSERT(kn->kn_status & KN_INFLUX,
1860 ("knote removed w/o list lock"));
1861 knl->kl_unlock(knl->kl_lockarg);
1862 kq->kq_state |= KQ_FLUXWAIT;
1863 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
1869 KNL_ASSERT_LOCKED(knl);
1871 knl->kl_unlock(knl->kl_lockarg);
1872 KNL_ASSERT_UNLOCKED(knl);
1877 * Remove all knotes referencing a specified fd must be called with FILEDESC
1878 * lock. This prevents a race where a new fd comes along and occupies the
1879 * entry and we attach a knote to the fd.
1882 knote_fdclose(struct thread *td, int fd)
1884 struct filedesc *fdp = td->td_proc->p_fd;
1889 FILEDESC_XLOCK_ASSERT(fdp);
1892 * We shouldn't have to worry about new kevents appearing on fd
1893 * since filedesc is locked.
1895 SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
1900 while (kq->kq_knlistsize > fd &&
1901 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
1902 if (kn->kn_status & KN_INFLUX) {
1903 /* someone else might be waiting on our knote */
1906 kq->kq_state |= KQ_FLUXWAIT;
1907 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
1910 kn->kn_status |= KN_INFLUX;
1912 if (!(kn->kn_status & KN_DETACHED))
1913 kn->kn_fop->f_detach(kn);
1923 knote_attach(struct knote *kn, struct kqueue *kq)
1927 KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX"));
1930 if (kn->kn_fop->f_isfd) {
1931 if (kn->kn_id >= kq->kq_knlistsize)
1933 list = &kq->kq_knlist[kn->kn_id];
1935 if (kq->kq_knhash == NULL)
1937 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1940 SLIST_INSERT_HEAD(list, kn, kn_link);
1946 * knote must already have been detached using the f_detach method.
1947 * no lock need to be held, it is assumed that the KN_INFLUX flag is set
1948 * to prevent other removal.
1951 knote_drop(struct knote *kn, struct thread *td)
1959 KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX,
1960 ("knote_drop called without KN_INFLUX set in kn_status"));
1963 if (kn->kn_fop->f_isfd)
1964 list = &kq->kq_knlist[kn->kn_id];
1966 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1968 if (!SLIST_EMPTY(list))
1969 SLIST_REMOVE(list, kn, knote, kn_link);
1970 if (kn->kn_status & KN_QUEUED)
1974 if (kn->kn_fop->f_isfd) {
1975 fdrop(kn->kn_fp, td);
1978 kqueue_fo_release(kn->kn_kevent.filter);
1984 knote_enqueue(struct knote *kn)
1986 struct kqueue *kq = kn->kn_kq;
1988 KQ_OWNED(kn->kn_kq);
1989 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
1991 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
1992 kn->kn_status |= KN_QUEUED;
1998 knote_dequeue(struct knote *kn)
2000 struct kqueue *kq = kn->kn_kq;
2002 KQ_OWNED(kn->kn_kq);
2003 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
2005 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
2006 kn->kn_status &= ~KN_QUEUED;
2014 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
2015 NULL, NULL, UMA_ALIGN_PTR, 0);
2017 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL);
2019 static struct knote *
2020 knote_alloc(int waitok)
2022 return ((struct knote *)uma_zalloc(knote_zone,
2023 (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO));
2027 knote_free(struct knote *kn)
2030 uma_zfree(knote_zone, kn);
2034 * Register the kev w/ the kq specified by fd.
2037 kqfd_register(int fd, struct kevent *kev, struct thread *td, int waitok)
2043 if ((error = fget(td, fd, &fp)) != 0)
2045 if ((error = kqueue_acquire(fp, &kq)) != 0)
2048 error = kqueue_register(kq, kev, td, waitok);
2050 kqueue_release(kq, 0);