]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_event.c
Schedule fast taskqueue callouts on right CPU.
[FreeBSD/FreeBSD.git] / sys / kern / kern_event.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
5  * Copyright 2004 John-Mark Gurney <jmg@FreeBSD.org>
6  * Copyright (c) 2009 Apple, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30
31 #include <sys/cdefs.h>
32 #include "opt_ktrace.h"
33 #include "opt_kqueue.h"
34
35 #ifdef COMPAT_FREEBSD11
36 #define _WANT_FREEBSD11_KEVENT
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/capsicum.h>
42 #include <sys/kernel.h>
43 #include <sys/limits.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/rwlock.h>
47 #include <sys/proc.h>
48 #include <sys/malloc.h>
49 #include <sys/unistd.h>
50 #include <sys/file.h>
51 #include <sys/filedesc.h>
52 #include <sys/filio.h>
53 #include <sys/fcntl.h>
54 #include <sys/kthread.h>
55 #include <sys/selinfo.h>
56 #include <sys/queue.h>
57 #include <sys/event.h>
58 #include <sys/eventvar.h>
59 #include <sys/poll.h>
60 #include <sys/protosw.h>
61 #include <sys/resourcevar.h>
62 #include <sys/sigio.h>
63 #include <sys/signalvar.h>
64 #include <sys/socket.h>
65 #include <sys/socketvar.h>
66 #include <sys/stat.h>
67 #include <sys/sysctl.h>
68 #include <sys/sysproto.h>
69 #include <sys/syscallsubr.h>
70 #include <sys/taskqueue.h>
71 #include <sys/uio.h>
72 #include <sys/user.h>
73 #ifdef KTRACE
74 #include <sys/ktrace.h>
75 #endif
76 #include <machine/atomic.h>
77
78 #include <vm/uma.h>
79
80 static MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
81
82 /*
83  * This lock is used if multiple kq locks are required.  This possibly
84  * should be made into a per proc lock.
85  */
86 static struct mtx       kq_global;
87 MTX_SYSINIT(kq_global, &kq_global, "kqueue order", MTX_DEF);
88 #define KQ_GLOBAL_LOCK(lck, haslck)     do {    \
89         if (!haslck)                            \
90                 mtx_lock(lck);                  \
91         haslck = 1;                             \
92 } while (0)
93 #define KQ_GLOBAL_UNLOCK(lck, haslck)   do {    \
94         if (haslck)                             \
95                 mtx_unlock(lck);                        \
96         haslck = 0;                             \
97 } while (0)
98
99 TASKQUEUE_DEFINE_THREAD(kqueue_ctx);
100
101 static int      kevent_copyout(void *arg, struct kevent *kevp, int count);
102 static int      kevent_copyin(void *arg, struct kevent *kevp, int count);
103 static int      kqueue_register(struct kqueue *kq, struct kevent *kev,
104                     struct thread *td, int mflag);
105 static int      kqueue_acquire(struct file *fp, struct kqueue **kqp);
106 static void     kqueue_release(struct kqueue *kq, int locked);
107 static void     kqueue_destroy(struct kqueue *kq);
108 static void     kqueue_drain(struct kqueue *kq, struct thread *td);
109 static int      kqueue_expand(struct kqueue *kq, const struct filterops *fops,
110                     uintptr_t ident, int mflag);
111 static void     kqueue_task(void *arg, int pending);
112 static int      kqueue_scan(struct kqueue *kq, int maxevents,
113                     struct kevent_copyops *k_ops,
114                     const struct timespec *timeout,
115                     struct kevent *keva, struct thread *td);
116 static void     kqueue_wakeup(struct kqueue *kq);
117 static const struct filterops *kqueue_fo_find(int filt);
118 static void     kqueue_fo_release(int filt);
119 struct g_kevent_args;
120 static int      kern_kevent_generic(struct thread *td,
121                     struct g_kevent_args *uap,
122                     struct kevent_copyops *k_ops, const char *struct_name);
123
124 static fo_ioctl_t       kqueue_ioctl;
125 static fo_poll_t        kqueue_poll;
126 static fo_kqfilter_t    kqueue_kqfilter;
127 static fo_stat_t        kqueue_stat;
128 static fo_close_t       kqueue_close;
129 static fo_fill_kinfo_t  kqueue_fill_kinfo;
130
131 static struct fileops kqueueops = {
132         .fo_read = invfo_rdwr,
133         .fo_write = invfo_rdwr,
134         .fo_truncate = invfo_truncate,
135         .fo_ioctl = kqueue_ioctl,
136         .fo_poll = kqueue_poll,
137         .fo_kqfilter = kqueue_kqfilter,
138         .fo_stat = kqueue_stat,
139         .fo_close = kqueue_close,
140         .fo_chmod = invfo_chmod,
141         .fo_chown = invfo_chown,
142         .fo_sendfile = invfo_sendfile,
143         .fo_fill_kinfo = kqueue_fill_kinfo,
144 };
145
146 static int      knote_attach(struct knote *kn, struct kqueue *kq);
147 static void     knote_drop(struct knote *kn, struct thread *td);
148 static void     knote_drop_detached(struct knote *kn, struct thread *td);
149 static void     knote_enqueue(struct knote *kn);
150 static void     knote_dequeue(struct knote *kn);
151 static void     knote_init(void);
152 static struct   knote *knote_alloc(int mflag);
153 static void     knote_free(struct knote *kn);
154
155 static void     filt_kqdetach(struct knote *kn);
156 static int      filt_kqueue(struct knote *kn, long hint);
157 static int      filt_procattach(struct knote *kn);
158 static void     filt_procdetach(struct knote *kn);
159 static int      filt_proc(struct knote *kn, long hint);
160 static int      filt_fileattach(struct knote *kn);
161 static void     filt_timerexpire(void *knx);
162 static void     filt_timerexpire_l(struct knote *kn, bool proc_locked);
163 static int      filt_timerattach(struct knote *kn);
164 static void     filt_timerdetach(struct knote *kn);
165 static void     filt_timerstart(struct knote *kn, sbintime_t to);
166 static void     filt_timertouch(struct knote *kn, struct kevent *kev,
167                     u_long type);
168 static int      filt_timervalidate(struct knote *kn, sbintime_t *to);
169 static int      filt_timer(struct knote *kn, long hint);
170 static int      filt_userattach(struct knote *kn);
171 static void     filt_userdetach(struct knote *kn);
172 static int      filt_user(struct knote *kn, long hint);
173 static void     filt_usertouch(struct knote *kn, struct kevent *kev,
174                     u_long type);
175
176 static struct filterops file_filtops = {
177         .f_isfd = 1,
178         .f_attach = filt_fileattach,
179 };
180 static struct filterops kqread_filtops = {
181         .f_isfd = 1,
182         .f_detach = filt_kqdetach,
183         .f_event = filt_kqueue,
184 };
185 /* XXX - move to kern_proc.c?  */
186 static struct filterops proc_filtops = {
187         .f_isfd = 0,
188         .f_attach = filt_procattach,
189         .f_detach = filt_procdetach,
190         .f_event = filt_proc,
191 };
192 static struct filterops timer_filtops = {
193         .f_isfd = 0,
194         .f_attach = filt_timerattach,
195         .f_detach = filt_timerdetach,
196         .f_event = filt_timer,
197         .f_touch = filt_timertouch,
198 };
199 static struct filterops user_filtops = {
200         .f_attach = filt_userattach,
201         .f_detach = filt_userdetach,
202         .f_event = filt_user,
203         .f_touch = filt_usertouch,
204 };
205
206 static uma_zone_t       knote_zone;
207 static unsigned int __exclusive_cache_line      kq_ncallouts;
208 static unsigned int     kq_calloutmax = 4 * 1024;
209 SYSCTL_UINT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
210     &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
211
212 /* XXX - ensure not influx ? */
213 #define KNOTE_ACTIVATE(kn, islock) do {                                 \
214         if ((islock))                                                   \
215                 mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED);            \
216         else                                                            \
217                 KQ_LOCK((kn)->kn_kq);                                   \
218         (kn)->kn_status |= KN_ACTIVE;                                   \
219         if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0)         \
220                 knote_enqueue((kn));                                    \
221         if (!(islock))                                                  \
222                 KQ_UNLOCK((kn)->kn_kq);                                 \
223 } while(0)
224 #define KQ_LOCK(kq) do {                                                \
225         mtx_lock(&(kq)->kq_lock);                                       \
226 } while (0)
227 #define KQ_FLUX_WAKEUP(kq) do {                                         \
228         if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) {            \
229                 (kq)->kq_state &= ~KQ_FLUXWAIT;                         \
230                 wakeup((kq));                                           \
231         }                                                               \
232 } while (0)
233 #define KQ_UNLOCK_FLUX(kq) do {                                         \
234         KQ_FLUX_WAKEUP(kq);                                             \
235         mtx_unlock(&(kq)->kq_lock);                                     \
236 } while (0)
237 #define KQ_UNLOCK(kq) do {                                              \
238         mtx_unlock(&(kq)->kq_lock);                                     \
239 } while (0)
240 #define KQ_OWNED(kq) do {                                               \
241         mtx_assert(&(kq)->kq_lock, MA_OWNED);                           \
242 } while (0)
243 #define KQ_NOTOWNED(kq) do {                                            \
244         mtx_assert(&(kq)->kq_lock, MA_NOTOWNED);                        \
245 } while (0)
246
247 static struct knlist *
248 kn_list_lock(struct knote *kn)
249 {
250         struct knlist *knl;
251
252         knl = kn->kn_knlist;
253         if (knl != NULL)
254                 knl->kl_lock(knl->kl_lockarg);
255         return (knl);
256 }
257
258 static void
259 kn_list_unlock(struct knlist *knl)
260 {
261         bool do_free;
262
263         if (knl == NULL)
264                 return;
265         do_free = knl->kl_autodestroy && knlist_empty(knl);
266         knl->kl_unlock(knl->kl_lockarg);
267         if (do_free) {
268                 knlist_destroy(knl);
269                 free(knl, M_KQUEUE);
270         }
271 }
272
273 static bool
274 kn_in_flux(struct knote *kn)
275 {
276
277         return (kn->kn_influx > 0);
278 }
279
280 static void
281 kn_enter_flux(struct knote *kn)
282 {
283
284         KQ_OWNED(kn->kn_kq);
285         MPASS(kn->kn_influx < INT_MAX);
286         kn->kn_influx++;
287 }
288
289 static bool
290 kn_leave_flux(struct knote *kn)
291 {
292
293         KQ_OWNED(kn->kn_kq);
294         MPASS(kn->kn_influx > 0);
295         kn->kn_influx--;
296         return (kn->kn_influx == 0);
297 }
298
299 #define KNL_ASSERT_LOCK(knl, islocked) do {                             \
300         if (islocked)                                                   \
301                 KNL_ASSERT_LOCKED(knl);                         \
302         else                                                            \
303                 KNL_ASSERT_UNLOCKED(knl);                               \
304 } while (0)
305 #ifdef INVARIANTS
306 #define KNL_ASSERT_LOCKED(knl) do {                                     \
307         knl->kl_assert_lock((knl)->kl_lockarg, LA_LOCKED);              \
308 } while (0)
309 #define KNL_ASSERT_UNLOCKED(knl) do {                                   \
310         knl->kl_assert_lock((knl)->kl_lockarg, LA_UNLOCKED);            \
311 } while (0)
312 #else /* !INVARIANTS */
313 #define KNL_ASSERT_LOCKED(knl) do {} while(0)
314 #define KNL_ASSERT_UNLOCKED(knl) do {} while (0)
315 #endif /* INVARIANTS */
316
317 #ifndef KN_HASHSIZE
318 #define KN_HASHSIZE             64              /* XXX should be tunable */
319 #endif
320
321 #define KN_HASH(val, mask)      (((val) ^ (val >> 8)) & (mask))
322
323 static int
324 filt_nullattach(struct knote *kn)
325 {
326
327         return (ENXIO);
328 };
329
330 struct filterops null_filtops = {
331         .f_isfd = 0,
332         .f_attach = filt_nullattach,
333 };
334
335 /* XXX - make SYSINIT to add these, and move into respective modules. */
336 extern struct filterops sig_filtops;
337 extern struct filterops fs_filtops;
338
339 /*
340  * Table for all system-defined filters.
341  */
342 static struct mtx       filterops_lock;
343 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops", MTX_DEF);
344 static struct {
345         const struct filterops *for_fop;
346         int for_nolock;
347         int for_refcnt;
348 } sysfilt_ops[EVFILT_SYSCOUNT] = {
349         { &file_filtops, 1 },                   /* EVFILT_READ */
350         { &file_filtops, 1 },                   /* EVFILT_WRITE */
351         { &null_filtops },                      /* EVFILT_AIO */
352         { &file_filtops, 1 },                   /* EVFILT_VNODE */
353         { &proc_filtops, 1 },                   /* EVFILT_PROC */
354         { &sig_filtops, 1 },                    /* EVFILT_SIGNAL */
355         { &timer_filtops, 1 },                  /* EVFILT_TIMER */
356         { &file_filtops, 1 },                   /* EVFILT_PROCDESC */
357         { &fs_filtops, 1 },                     /* EVFILT_FS */
358         { &null_filtops },                      /* EVFILT_LIO */
359         { &user_filtops, 1 },                   /* EVFILT_USER */
360         { &null_filtops },                      /* EVFILT_SENDFILE */
361         { &file_filtops, 1 },                   /* EVFILT_EMPTY */
362 };
363
364 /*
365  * Simple redirection for all cdevsw style objects to call their fo_kqfilter
366  * method.
367  */
368 static int
369 filt_fileattach(struct knote *kn)
370 {
371
372         return (fo_kqfilter(kn->kn_fp, kn));
373 }
374
375 /*ARGSUSED*/
376 static int
377 kqueue_kqfilter(struct file *fp, struct knote *kn)
378 {
379         struct kqueue *kq = kn->kn_fp->f_data;
380
381         if (kn->kn_filter != EVFILT_READ)
382                 return (EINVAL);
383
384         kn->kn_status |= KN_KQUEUE;
385         kn->kn_fop = &kqread_filtops;
386         knlist_add(&kq->kq_sel.si_note, kn, 0);
387
388         return (0);
389 }
390
391 static void
392 filt_kqdetach(struct knote *kn)
393 {
394         struct kqueue *kq = kn->kn_fp->f_data;
395
396         knlist_remove(&kq->kq_sel.si_note, kn, 0);
397 }
398
399 /*ARGSUSED*/
400 static int
401 filt_kqueue(struct knote *kn, long hint)
402 {
403         struct kqueue *kq = kn->kn_fp->f_data;
404
405         kn->kn_data = kq->kq_count;
406         return (kn->kn_data > 0);
407 }
408
409 /* XXX - move to kern_proc.c?  */
410 static int
411 filt_procattach(struct knote *kn)
412 {
413         struct proc *p;
414         int error;
415         bool exiting, immediate;
416
417         exiting = immediate = false;
418         if (kn->kn_sfflags & NOTE_EXIT)
419                 p = pfind_any(kn->kn_id);
420         else
421                 p = pfind(kn->kn_id);
422         if (p == NULL)
423                 return (ESRCH);
424         if (p->p_flag & P_WEXIT)
425                 exiting = true;
426
427         if ((error = p_cansee(curthread, p))) {
428                 PROC_UNLOCK(p);
429                 return (error);
430         }
431
432         kn->kn_ptr.p_proc = p;
433         kn->kn_flags |= EV_CLEAR;               /* automatically set */
434
435         /*
436          * Internal flag indicating registration done by kernel for the
437          * purposes of getting a NOTE_CHILD notification.
438          */
439         if (kn->kn_flags & EV_FLAG2) {
440                 kn->kn_flags &= ~EV_FLAG2;
441                 kn->kn_data = kn->kn_sdata;             /* ppid */
442                 kn->kn_fflags = NOTE_CHILD;
443                 kn->kn_sfflags &= ~(NOTE_EXIT | NOTE_EXEC | NOTE_FORK);
444                 immediate = true; /* Force immediate activation of child note. */
445         }
446         /*
447          * Internal flag indicating registration done by kernel (for other than
448          * NOTE_CHILD).
449          */
450         if (kn->kn_flags & EV_FLAG1) {
451                 kn->kn_flags &= ~EV_FLAG1;
452         }
453
454         knlist_add(p->p_klist, kn, 1);
455
456         /*
457          * Immediately activate any child notes or, in the case of a zombie
458          * target process, exit notes.  The latter is necessary to handle the
459          * case where the target process, e.g. a child, dies before the kevent
460          * is registered.
461          */
462         if (immediate || (exiting && filt_proc(kn, NOTE_EXIT)))
463                 KNOTE_ACTIVATE(kn, 0);
464
465         PROC_UNLOCK(p);
466
467         return (0);
468 }
469
470 /*
471  * The knote may be attached to a different process, which may exit,
472  * leaving nothing for the knote to be attached to.  So when the process
473  * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
474  * it will be deleted when read out.  However, as part of the knote deletion,
475  * this routine is called, so a check is needed to avoid actually performing
476  * a detach, because the original process does not exist any more.
477  */
478 /* XXX - move to kern_proc.c?  */
479 static void
480 filt_procdetach(struct knote *kn)
481 {
482
483         knlist_remove(kn->kn_knlist, kn, 0);
484         kn->kn_ptr.p_proc = NULL;
485 }
486
487 /* XXX - move to kern_proc.c?  */
488 static int
489 filt_proc(struct knote *kn, long hint)
490 {
491         struct proc *p;
492         u_int event;
493
494         p = kn->kn_ptr.p_proc;
495         if (p == NULL) /* already activated, from attach filter */
496                 return (0);
497
498         /* Mask off extra data. */
499         event = (u_int)hint & NOTE_PCTRLMASK;
500
501         /* If the user is interested in this event, record it. */
502         if (kn->kn_sfflags & event)
503                 kn->kn_fflags |= event;
504
505         /* Process is gone, so flag the event as finished. */
506         if (event == NOTE_EXIT) {
507                 kn->kn_flags |= EV_EOF | EV_ONESHOT;
508                 kn->kn_ptr.p_proc = NULL;
509                 if (kn->kn_fflags & NOTE_EXIT)
510                         kn->kn_data = KW_EXITCODE(p->p_xexit, p->p_xsig);
511                 if (kn->kn_fflags == 0)
512                         kn->kn_flags |= EV_DROP;
513                 return (1);
514         }
515
516         return (kn->kn_fflags != 0);
517 }
518
519 /*
520  * Called when the process forked. It mostly does the same as the
521  * knote(), activating all knotes registered to be activated when the
522  * process forked. Additionally, for each knote attached to the
523  * parent, check whether user wants to track the new process. If so
524  * attach a new knote to it, and immediately report an event with the
525  * child's pid.
526  */
527 void
528 knote_fork(struct knlist *list, int pid)
529 {
530         struct kqueue *kq;
531         struct knote *kn;
532         struct kevent kev;
533         int error;
534
535         MPASS(list != NULL);
536         KNL_ASSERT_LOCKED(list);
537         if (SLIST_EMPTY(&list->kl_list))
538                 return;
539
540         memset(&kev, 0, sizeof(kev));
541         SLIST_FOREACH(kn, &list->kl_list, kn_selnext) {
542                 kq = kn->kn_kq;
543                 KQ_LOCK(kq);
544                 if (kn_in_flux(kn) && (kn->kn_status & KN_SCAN) == 0) {
545                         KQ_UNLOCK(kq);
546                         continue;
547                 }
548
549                 /*
550                  * The same as knote(), activate the event.
551                  */
552                 if ((kn->kn_sfflags & NOTE_TRACK) == 0) {
553                         if (kn->kn_fop->f_event(kn, NOTE_FORK))
554                                 KNOTE_ACTIVATE(kn, 1);
555                         KQ_UNLOCK(kq);
556                         continue;
557                 }
558
559                 /*
560                  * The NOTE_TRACK case. In addition to the activation
561                  * of the event, we need to register new events to
562                  * track the child. Drop the locks in preparation for
563                  * the call to kqueue_register().
564                  */
565                 kn_enter_flux(kn);
566                 KQ_UNLOCK(kq);
567                 list->kl_unlock(list->kl_lockarg);
568
569                 /*
570                  * Activate existing knote and register tracking knotes with
571                  * new process.
572                  *
573                  * First register a knote to get just the child notice. This
574                  * must be a separate note from a potential NOTE_EXIT
575                  * notification since both NOTE_CHILD and NOTE_EXIT are defined
576                  * to use the data field (in conflicting ways).
577                  */
578                 kev.ident = pid;
579                 kev.filter = kn->kn_filter;
580                 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_ONESHOT |
581                     EV_FLAG2;
582                 kev.fflags = kn->kn_sfflags;
583                 kev.data = kn->kn_id;           /* parent */
584                 kev.udata = kn->kn_kevent.udata;/* preserve udata */
585                 error = kqueue_register(kq, &kev, NULL, M_NOWAIT);
586                 if (error)
587                         kn->kn_fflags |= NOTE_TRACKERR;
588
589                 /*
590                  * Then register another knote to track other potential events
591                  * from the new process.
592                  */
593                 kev.ident = pid;
594                 kev.filter = kn->kn_filter;
595                 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
596                 kev.fflags = kn->kn_sfflags;
597                 kev.data = kn->kn_id;           /* parent */
598                 kev.udata = kn->kn_kevent.udata;/* preserve udata */
599                 error = kqueue_register(kq, &kev, NULL, M_NOWAIT);
600                 if (error)
601                         kn->kn_fflags |= NOTE_TRACKERR;
602                 if (kn->kn_fop->f_event(kn, NOTE_FORK))
603                         KNOTE_ACTIVATE(kn, 0);
604                 list->kl_lock(list->kl_lockarg);
605                 KQ_LOCK(kq);
606                 kn_leave_flux(kn);
607                 KQ_UNLOCK_FLUX(kq);
608         }
609 }
610
611 /*
612  * XXX: EVFILT_TIMER should perhaps live in kern_time.c beside the
613  * interval timer support code.
614  */
615
616 #define NOTE_TIMER_PRECMASK                                             \
617     (NOTE_SECONDS | NOTE_MSECONDS | NOTE_USECONDS | NOTE_NSECONDS)
618
619 static sbintime_t
620 timer2sbintime(int64_t data, int flags)
621 {
622         int64_t secs;
623
624         /*
625          * Macros for converting to the fractional second portion of an
626          * sbintime_t using 64bit multiplication to improve precision.
627          */
628 #define NS_TO_SBT(ns) (((ns) * (((uint64_t)1 << 63) / 500000000)) >> 32)
629 #define US_TO_SBT(us) (((us) * (((uint64_t)1 << 63) / 500000)) >> 32)
630 #define MS_TO_SBT(ms) (((ms) * (((uint64_t)1 << 63) / 500)) >> 32)
631         switch (flags & NOTE_TIMER_PRECMASK) {
632         case NOTE_SECONDS:
633 #ifdef __LP64__
634                 if (data > (SBT_MAX / SBT_1S))
635                         return (SBT_MAX);
636 #endif
637                 return ((sbintime_t)data << 32);
638         case NOTE_MSECONDS: /* FALLTHROUGH */
639         case 0:
640                 if (data >= 1000) {
641                         secs = data / 1000;
642 #ifdef __LP64__
643                         if (secs > (SBT_MAX / SBT_1S))
644                                 return (SBT_MAX);
645 #endif
646                         return (secs << 32 | MS_TO_SBT(data % 1000));
647                 }
648                 return (MS_TO_SBT(data));
649         case NOTE_USECONDS:
650                 if (data >= 1000000) {
651                         secs = data / 1000000;
652 #ifdef __LP64__
653                         if (secs > (SBT_MAX / SBT_1S))
654                                 return (SBT_MAX);
655 #endif
656                         return (secs << 32 | US_TO_SBT(data % 1000000));
657                 }
658                 return (US_TO_SBT(data));
659         case NOTE_NSECONDS:
660                 if (data >= 1000000000) {
661                         secs = data / 1000000000;
662 #ifdef __LP64__
663                         if (secs > (SBT_MAX / SBT_1S))
664                                 return (SBT_MAX);
665 #endif
666                         return (secs << 32 | NS_TO_SBT(data % 1000000000));
667                 }
668                 return (NS_TO_SBT(data));
669         default:
670                 break;
671         }
672         return (-1);
673 }
674
675 struct kq_timer_cb_data {
676         struct callout c;
677         struct proc *p;
678         struct knote *kn;
679         int cpuid;
680         int flags;
681         TAILQ_ENTRY(kq_timer_cb_data) link;
682         sbintime_t next;        /* next timer event fires at */
683         sbintime_t to;          /* precalculated timer period, 0 for abs */
684 };
685
686 #define KQ_TIMER_CB_ENQUEUED    0x01
687
688 static void
689 kqtimer_sched_callout(struct kq_timer_cb_data *kc)
690 {
691         callout_reset_sbt_on(&kc->c, kc->next, 0, filt_timerexpire, kc->kn,
692             kc->cpuid, C_ABSOLUTE);
693 }
694
695 void
696 kqtimer_proc_continue(struct proc *p)
697 {
698         struct kq_timer_cb_data *kc, *kc1;
699         struct bintime bt;
700         sbintime_t now;
701
702         PROC_LOCK_ASSERT(p, MA_OWNED);
703
704         getboottimebin(&bt);
705         now = bttosbt(bt);
706
707         TAILQ_FOREACH_SAFE(kc, &p->p_kqtim_stop, link, kc1) {
708                 TAILQ_REMOVE(&p->p_kqtim_stop, kc, link);
709                 kc->flags &= ~KQ_TIMER_CB_ENQUEUED;
710                 if (kc->next <= now)
711                         filt_timerexpire_l(kc->kn, true);
712                 else
713                         kqtimer_sched_callout(kc);
714         }
715 }
716
717 static void
718 filt_timerexpire_l(struct knote *kn, bool proc_locked)
719 {
720         struct kq_timer_cb_data *kc;
721         struct proc *p;
722         uint64_t delta;
723         sbintime_t now;
724
725         kc = kn->kn_ptr.p_v;
726
727         if ((kn->kn_flags & EV_ONESHOT) != 0 || kc->to == 0) {
728                 kn->kn_data++;
729                 KNOTE_ACTIVATE(kn, 0);
730                 return;
731         }
732
733         now = sbinuptime();
734         if (now >= kc->next) {
735                 delta = (now - kc->next) / kc->to;
736                 if (delta == 0)
737                         delta = 1;
738                 kn->kn_data += delta;
739                 kc->next += delta * kc->to;
740                 if (now >= kc->next)    /* overflow */
741                         kc->next = now + kc->to;
742                 KNOTE_ACTIVATE(kn, 0);  /* XXX - handle locking */
743         }
744
745         /*
746          * Initial check for stopped kc->p is racy.  It is fine to
747          * miss the set of the stop flags, at worst we would schedule
748          * one more callout.  On the other hand, it is not fine to not
749          * schedule when we we missed clearing of the flags, we
750          * recheck them under the lock and observe consistent state.
751          */
752         p = kc->p;
753         if (P_SHOULDSTOP(p) || P_KILLED(p)) {
754                 if (!proc_locked)
755                         PROC_LOCK(p);
756                 if (P_SHOULDSTOP(p) || P_KILLED(p)) {
757                         if ((kc->flags & KQ_TIMER_CB_ENQUEUED) == 0) {
758                                 kc->flags |= KQ_TIMER_CB_ENQUEUED;
759                                 TAILQ_INSERT_TAIL(&p->p_kqtim_stop, kc, link);
760                         }
761                         if (!proc_locked)
762                                 PROC_UNLOCK(p);
763                         return;
764                 }
765                 if (!proc_locked)
766                         PROC_UNLOCK(p);
767         }
768         kqtimer_sched_callout(kc);
769 }
770
771 static void
772 filt_timerexpire(void *knx)
773 {
774         filt_timerexpire_l(knx, false);
775 }
776
777 /*
778  * data contains amount of time to sleep
779  */
780 static int
781 filt_timervalidate(struct knote *kn, sbintime_t *to)
782 {
783         struct bintime bt;
784         sbintime_t sbt;
785
786         if (kn->kn_sdata < 0)
787                 return (EINVAL);
788         if (kn->kn_sdata == 0 && (kn->kn_flags & EV_ONESHOT) == 0)
789                 kn->kn_sdata = 1;
790         /*
791          * The only fflags values supported are the timer unit
792          * (precision) and the absolute time indicator.
793          */
794         if ((kn->kn_sfflags & ~(NOTE_TIMER_PRECMASK | NOTE_ABSTIME)) != 0)
795                 return (EINVAL);
796
797         *to = timer2sbintime(kn->kn_sdata, kn->kn_sfflags);
798         if (*to < 0)
799                 return (EINVAL);
800         if ((kn->kn_sfflags & NOTE_ABSTIME) != 0) {
801                 getboottimebin(&bt);
802                 sbt = bttosbt(bt);
803                 *to = MAX(0, *to - sbt);
804         }
805         return (0);
806 }
807
808 static int
809 filt_timerattach(struct knote *kn)
810 {
811         struct kq_timer_cb_data *kc;
812         sbintime_t to;
813         int error;
814
815         to = -1;
816         error = filt_timervalidate(kn, &to);
817         if (error != 0)
818                 return (error);
819         KASSERT(to > 0 || (kn->kn_flags & EV_ONESHOT) != 0 ||
820             (kn->kn_sfflags & NOTE_ABSTIME) != 0,
821             ("%s: periodic timer has a calculated zero timeout", __func__));
822         KASSERT(to >= 0,
823             ("%s: timer has a calculated negative timeout", __func__));
824
825         if (atomic_fetchadd_int(&kq_ncallouts, 1) + 1 > kq_calloutmax) {
826                 atomic_subtract_int(&kq_ncallouts, 1);
827                 return (ENOMEM);
828         }
829
830         if ((kn->kn_sfflags & NOTE_ABSTIME) == 0)
831                 kn->kn_flags |= EV_CLEAR;       /* automatically set */
832         kn->kn_status &= ~KN_DETACHED;          /* knlist_add clears it */
833         kn->kn_ptr.p_v = kc = malloc(sizeof(*kc), M_KQUEUE, M_WAITOK);
834         kc->kn = kn;
835         kc->p = curproc;
836         kc->cpuid = PCPU_GET(cpuid);
837         kc->flags = 0;
838         callout_init(&kc->c, 1);
839         filt_timerstart(kn, to);
840
841         return (0);
842 }
843
844 static void
845 filt_timerstart(struct knote *kn, sbintime_t to)
846 {
847         struct kq_timer_cb_data *kc;
848
849         kc = kn->kn_ptr.p_v;
850         if ((kn->kn_sfflags & NOTE_ABSTIME) != 0) {
851                 kc->next = to;
852                 kc->to = 0;
853         } else {
854                 kc->next = to + sbinuptime();
855                 kc->to = to;
856         }
857         kqtimer_sched_callout(kc);
858 }
859
860 static void
861 filt_timerdetach(struct knote *kn)
862 {
863         struct kq_timer_cb_data *kc;
864         unsigned int old __unused;
865         bool pending;
866
867         kc = kn->kn_ptr.p_v;
868         do {
869                 callout_drain(&kc->c);
870
871                 /*
872                  * kqtimer_proc_continue() might have rescheduled this callout.
873                  * Double-check, using the process mutex as an interlock.
874                  */
875                 PROC_LOCK(kc->p);
876                 if ((kc->flags & KQ_TIMER_CB_ENQUEUED) != 0) {
877                         kc->flags &= ~KQ_TIMER_CB_ENQUEUED;
878                         TAILQ_REMOVE(&kc->p->p_kqtim_stop, kc, link);
879                 }
880                 pending = callout_pending(&kc->c);
881                 PROC_UNLOCK(kc->p);
882         } while (pending);
883         free(kc, M_KQUEUE);
884         old = atomic_fetchadd_int(&kq_ncallouts, -1);
885         KASSERT(old > 0, ("Number of callouts cannot become negative"));
886         kn->kn_status |= KN_DETACHED;   /* knlist_remove sets it */
887 }
888
889 static void
890 filt_timertouch(struct knote *kn, struct kevent *kev, u_long type)
891 {
892         struct kq_timer_cb_data *kc;
893         struct kqueue *kq;
894         sbintime_t to;
895         int error;
896
897         switch (type) {
898         case EVENT_REGISTER:
899                 /* Handle re-added timers that update data/fflags */
900                 if (kev->flags & EV_ADD) {
901                         kc = kn->kn_ptr.p_v;
902
903                         /* Drain any existing callout. */
904                         callout_drain(&kc->c);
905
906                         /* Throw away any existing undelivered record
907                          * of the timer expiration. This is done under
908                          * the presumption that if a process is
909                          * re-adding this timer with new parameters,
910                          * it is no longer interested in what may have
911                          * happened under the old parameters. If it is
912                          * interested, it can wait for the expiration,
913                          * delete the old timer definition, and then
914                          * add the new one.
915                          *
916                          * This has to be done while the kq is locked:
917                          *   - if enqueued, dequeue
918                          *   - make it no longer active
919                          *   - clear the count of expiration events
920                          */
921                         kq = kn->kn_kq;
922                         KQ_LOCK(kq);
923                         if (kn->kn_status & KN_QUEUED)
924                                 knote_dequeue(kn);
925
926                         kn->kn_status &= ~KN_ACTIVE;
927                         kn->kn_data = 0;
928                         KQ_UNLOCK(kq);
929
930                         /* Reschedule timer based on new data/fflags */
931                         kn->kn_sfflags = kev->fflags;
932                         kn->kn_sdata = kev->data;
933                         error = filt_timervalidate(kn, &to);
934                         if (error != 0) {
935                                 kn->kn_flags |= EV_ERROR;
936                                 kn->kn_data = error;
937                         } else
938                                 filt_timerstart(kn, to);
939                 }
940                 break;
941
942         case EVENT_PROCESS:
943                 *kev = kn->kn_kevent;
944                 if (kn->kn_flags & EV_CLEAR) {
945                         kn->kn_data = 0;
946                         kn->kn_fflags = 0;
947                 }
948                 break;
949
950         default:
951                 panic("filt_timertouch() - invalid type (%ld)", type);
952                 break;
953         }
954 }
955
956 static int
957 filt_timer(struct knote *kn, long hint)
958 {
959
960         return (kn->kn_data != 0);
961 }
962
963 static int
964 filt_userattach(struct knote *kn)
965 {
966
967         /*
968          * EVFILT_USER knotes are not attached to anything in the kernel.
969          */
970         kn->kn_hook = NULL;
971         if (kn->kn_fflags & NOTE_TRIGGER)
972                 kn->kn_hookid = 1;
973         else
974                 kn->kn_hookid = 0;
975         return (0);
976 }
977
978 static void
979 filt_userdetach(__unused struct knote *kn)
980 {
981
982         /*
983          * EVFILT_USER knotes are not attached to anything in the kernel.
984          */
985 }
986
987 static int
988 filt_user(struct knote *kn, __unused long hint)
989 {
990
991         return (kn->kn_hookid);
992 }
993
994 static void
995 filt_usertouch(struct knote *kn, struct kevent *kev, u_long type)
996 {
997         u_int ffctrl;
998
999         switch (type) {
1000         case EVENT_REGISTER:
1001                 if (kev->fflags & NOTE_TRIGGER)
1002                         kn->kn_hookid = 1;
1003
1004                 ffctrl = kev->fflags & NOTE_FFCTRLMASK;
1005                 kev->fflags &= NOTE_FFLAGSMASK;
1006                 switch (ffctrl) {
1007                 case NOTE_FFNOP:
1008                         break;
1009
1010                 case NOTE_FFAND:
1011                         kn->kn_sfflags &= kev->fflags;
1012                         break;
1013
1014                 case NOTE_FFOR:
1015                         kn->kn_sfflags |= kev->fflags;
1016                         break;
1017
1018                 case NOTE_FFCOPY:
1019                         kn->kn_sfflags = kev->fflags;
1020                         break;
1021
1022                 default:
1023                         /* XXX Return error? */
1024                         break;
1025                 }
1026                 kn->kn_sdata = kev->data;
1027                 if (kev->flags & EV_CLEAR) {
1028                         kn->kn_hookid = 0;
1029                         kn->kn_data = 0;
1030                         kn->kn_fflags = 0;
1031                 }
1032                 break;
1033
1034         case EVENT_PROCESS:
1035                 *kev = kn->kn_kevent;
1036                 kev->fflags = kn->kn_sfflags;
1037                 kev->data = kn->kn_sdata;
1038                 if (kn->kn_flags & EV_CLEAR) {
1039                         kn->kn_hookid = 0;
1040                         kn->kn_data = 0;
1041                         kn->kn_fflags = 0;
1042                 }
1043                 break;
1044
1045         default:
1046                 panic("filt_usertouch() - invalid type (%ld)", type);
1047                 break;
1048         }
1049 }
1050
1051 int
1052 sys_kqueue(struct thread *td, struct kqueue_args *uap)
1053 {
1054
1055         return (kern_kqueue(td, 0, NULL));
1056 }
1057
1058 int
1059 sys_kqueuex(struct thread *td, struct kqueuex_args *uap)
1060 {
1061         int flags;
1062
1063         if ((uap->flags & ~(KQUEUE_CLOEXEC)) != 0)
1064                 return (EINVAL);
1065         flags = 0;
1066         if ((uap->flags & KQUEUE_CLOEXEC) != 0)
1067                 flags |= O_CLOEXEC;
1068         return (kern_kqueue(td, flags, NULL));
1069 }
1070
1071 static void
1072 kqueue_init(struct kqueue *kq)
1073 {
1074
1075         mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF | MTX_DUPOK);
1076         TAILQ_INIT(&kq->kq_head);
1077         knlist_init_mtx(&kq->kq_sel.si_note, &kq->kq_lock);
1078         TASK_INIT(&kq->kq_task, 0, kqueue_task, kq);
1079 }
1080
1081 int
1082 kern_kqueue(struct thread *td, int flags, struct filecaps *fcaps)
1083 {
1084         struct filedesc *fdp;
1085         struct kqueue *kq;
1086         struct file *fp;
1087         struct ucred *cred;
1088         int fd, error;
1089
1090         fdp = td->td_proc->p_fd;
1091         cred = td->td_ucred;
1092         if (!chgkqcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_KQUEUES)))
1093                 return (ENOMEM);
1094
1095         error = falloc_caps(td, &fp, &fd, flags, fcaps);
1096         if (error != 0) {
1097                 chgkqcnt(cred->cr_ruidinfo, -1, 0);
1098                 return (error);
1099         }
1100
1101         /* An extra reference on `fp' has been held for us by falloc(). */
1102         kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO);
1103         kqueue_init(kq);
1104         kq->kq_fdp = fdp;
1105         kq->kq_cred = crhold(cred);
1106
1107         FILEDESC_XLOCK(fdp);
1108         TAILQ_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list);
1109         FILEDESC_XUNLOCK(fdp);
1110
1111         finit(fp, FREAD | FWRITE, DTYPE_KQUEUE, kq, &kqueueops);
1112         fdrop(fp, td);
1113
1114         td->td_retval[0] = fd;
1115         return (0);
1116 }
1117
1118 struct g_kevent_args {
1119         int     fd;
1120         void    *changelist;
1121         int     nchanges;
1122         void    *eventlist;
1123         int     nevents;
1124         const struct timespec *timeout;
1125 };
1126
1127 int
1128 sys_kevent(struct thread *td, struct kevent_args *uap)
1129 {
1130         struct kevent_copyops k_ops = {
1131                 .arg = uap,
1132                 .k_copyout = kevent_copyout,
1133                 .k_copyin = kevent_copyin,
1134                 .kevent_size = sizeof(struct kevent),
1135         };
1136         struct g_kevent_args gk_args = {
1137                 .fd = uap->fd,
1138                 .changelist = uap->changelist,
1139                 .nchanges = uap->nchanges,
1140                 .eventlist = uap->eventlist,
1141                 .nevents = uap->nevents,
1142                 .timeout = uap->timeout,
1143         };
1144
1145         return (kern_kevent_generic(td, &gk_args, &k_ops, "kevent"));
1146 }
1147
1148 static int
1149 kern_kevent_generic(struct thread *td, struct g_kevent_args *uap,
1150     struct kevent_copyops *k_ops, const char *struct_name)
1151 {
1152         struct timespec ts, *tsp;
1153 #ifdef KTRACE
1154         struct kevent *eventlist = uap->eventlist;
1155 #endif
1156         int error;
1157
1158         if (uap->timeout != NULL) {
1159                 error = copyin(uap->timeout, &ts, sizeof(ts));
1160                 if (error)
1161                         return (error);
1162                 tsp = &ts;
1163         } else
1164                 tsp = NULL;
1165
1166 #ifdef KTRACE
1167         if (KTRPOINT(td, KTR_STRUCT_ARRAY))
1168                 ktrstructarray(struct_name, UIO_USERSPACE, uap->changelist,
1169                     uap->nchanges, k_ops->kevent_size);
1170 #endif
1171
1172         error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents,
1173             k_ops, tsp);
1174
1175 #ifdef KTRACE
1176         if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
1177                 ktrstructarray(struct_name, UIO_USERSPACE, eventlist,
1178                     td->td_retval[0], k_ops->kevent_size);
1179 #endif
1180
1181         return (error);
1182 }
1183
1184 /*
1185  * Copy 'count' items into the destination list pointed to by uap->eventlist.
1186  */
1187 static int
1188 kevent_copyout(void *arg, struct kevent *kevp, int count)
1189 {
1190         struct kevent_args *uap;
1191         int error;
1192
1193         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
1194         uap = (struct kevent_args *)arg;
1195
1196         error = copyout(kevp, uap->eventlist, count * sizeof *kevp);
1197         if (error == 0)
1198                 uap->eventlist += count;
1199         return (error);
1200 }
1201
1202 /*
1203  * Copy 'count' items from the list pointed to by uap->changelist.
1204  */
1205 static int
1206 kevent_copyin(void *arg, struct kevent *kevp, int count)
1207 {
1208         struct kevent_args *uap;
1209         int error;
1210
1211         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
1212         uap = (struct kevent_args *)arg;
1213
1214         error = copyin(uap->changelist, kevp, count * sizeof *kevp);
1215         if (error == 0)
1216                 uap->changelist += count;
1217         return (error);
1218 }
1219
1220 #ifdef COMPAT_FREEBSD11
1221 static int
1222 kevent11_copyout(void *arg, struct kevent *kevp, int count)
1223 {
1224         struct freebsd11_kevent_args *uap;
1225         struct kevent_freebsd11 kev11;
1226         int error, i;
1227
1228         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
1229         uap = (struct freebsd11_kevent_args *)arg;
1230
1231         for (i = 0; i < count; i++) {
1232                 kev11.ident = kevp->ident;
1233                 kev11.filter = kevp->filter;
1234                 kev11.flags = kevp->flags;
1235                 kev11.fflags = kevp->fflags;
1236                 kev11.data = kevp->data;
1237                 kev11.udata = kevp->udata;
1238                 error = copyout(&kev11, uap->eventlist, sizeof(kev11));
1239                 if (error != 0)
1240                         break;
1241                 uap->eventlist++;
1242                 kevp++;
1243         }
1244         return (error);
1245 }
1246
1247 /*
1248  * Copy 'count' items from the list pointed to by uap->changelist.
1249  */
1250 static int
1251 kevent11_copyin(void *arg, struct kevent *kevp, int count)
1252 {
1253         struct freebsd11_kevent_args *uap;
1254         struct kevent_freebsd11 kev11;
1255         int error, i;
1256
1257         KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count));
1258         uap = (struct freebsd11_kevent_args *)arg;
1259
1260         for (i = 0; i < count; i++) {
1261                 error = copyin(uap->changelist, &kev11, sizeof(kev11));
1262                 if (error != 0)
1263                         break;
1264                 kevp->ident = kev11.ident;
1265                 kevp->filter = kev11.filter;
1266                 kevp->flags = kev11.flags;
1267                 kevp->fflags = kev11.fflags;
1268                 kevp->data = (uintptr_t)kev11.data;
1269                 kevp->udata = kev11.udata;
1270                 bzero(&kevp->ext, sizeof(kevp->ext));
1271                 uap->changelist++;
1272                 kevp++;
1273         }
1274         return (error);
1275 }
1276
1277 int
1278 freebsd11_kevent(struct thread *td, struct freebsd11_kevent_args *uap)
1279 {
1280         struct kevent_copyops k_ops = {
1281                 .arg = uap,
1282                 .k_copyout = kevent11_copyout,
1283                 .k_copyin = kevent11_copyin,
1284                 .kevent_size = sizeof(struct kevent_freebsd11),
1285         };
1286         struct g_kevent_args gk_args = {
1287                 .fd = uap->fd,
1288                 .changelist = uap->changelist,
1289                 .nchanges = uap->nchanges,
1290                 .eventlist = uap->eventlist,
1291                 .nevents = uap->nevents,
1292                 .timeout = uap->timeout,
1293         };
1294
1295         return (kern_kevent_generic(td, &gk_args, &k_ops, "kevent_freebsd11"));
1296 }
1297 #endif
1298
1299 int
1300 kern_kevent(struct thread *td, int fd, int nchanges, int nevents,
1301     struct kevent_copyops *k_ops, const struct timespec *timeout)
1302 {
1303         cap_rights_t rights;
1304         struct file *fp;
1305         int error;
1306
1307         cap_rights_init_zero(&rights);
1308         if (nchanges > 0)
1309                 cap_rights_set_one(&rights, CAP_KQUEUE_CHANGE);
1310         if (nevents > 0)
1311                 cap_rights_set_one(&rights, CAP_KQUEUE_EVENT);
1312         error = fget(td, fd, &rights, &fp);
1313         if (error != 0)
1314                 return (error);
1315
1316         error = kern_kevent_fp(td, fp, nchanges, nevents, k_ops, timeout);
1317         fdrop(fp, td);
1318
1319         return (error);
1320 }
1321
1322 static int
1323 kqueue_kevent(struct kqueue *kq, struct thread *td, int nchanges, int nevents,
1324     struct kevent_copyops *k_ops, const struct timespec *timeout)
1325 {
1326         struct kevent keva[KQ_NEVENTS];
1327         struct kevent *kevp, *changes;
1328         int i, n, nerrors, error;
1329
1330         if (nchanges < 0)
1331                 return (EINVAL);
1332
1333         nerrors = 0;
1334         while (nchanges > 0) {
1335                 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges;
1336                 error = k_ops->k_copyin(k_ops->arg, keva, n);
1337                 if (error)
1338                         return (error);
1339                 changes = keva;
1340                 for (i = 0; i < n; i++) {
1341                         kevp = &changes[i];
1342                         if (!kevp->filter)
1343                                 continue;
1344                         kevp->flags &= ~EV_SYSFLAGS;
1345                         error = kqueue_register(kq, kevp, td, M_WAITOK);
1346                         if (error || (kevp->flags & EV_RECEIPT)) {
1347                                 if (nevents == 0)
1348                                         return (error);
1349                                 kevp->flags = EV_ERROR;
1350                                 kevp->data = error;
1351                                 (void)k_ops->k_copyout(k_ops->arg, kevp, 1);
1352                                 nevents--;
1353                                 nerrors++;
1354                         }
1355                 }
1356                 nchanges -= n;
1357         }
1358         if (nerrors) {
1359                 td->td_retval[0] = nerrors;
1360                 return (0);
1361         }
1362
1363         return (kqueue_scan(kq, nevents, k_ops, timeout, keva, td));
1364 }
1365
1366 int
1367 kern_kevent_fp(struct thread *td, struct file *fp, int nchanges, int nevents,
1368     struct kevent_copyops *k_ops, const struct timespec *timeout)
1369 {
1370         struct kqueue *kq;
1371         int error;
1372
1373         error = kqueue_acquire(fp, &kq);
1374         if (error != 0)
1375                 return (error);
1376         error = kqueue_kevent(kq, td, nchanges, nevents, k_ops, timeout);
1377         kqueue_release(kq, 0);
1378         return (error);
1379 }
1380
1381 /*
1382  * Performs a kevent() call on a temporarily created kqueue. This can be
1383  * used to perform one-shot polling, similar to poll() and select().
1384  */
1385 int
1386 kern_kevent_anonymous(struct thread *td, int nevents,
1387     struct kevent_copyops *k_ops)
1388 {
1389         struct kqueue kq = {};
1390         int error;
1391
1392         kqueue_init(&kq);
1393         kq.kq_refcnt = 1;
1394         error = kqueue_kevent(&kq, td, nevents, nevents, k_ops, NULL);
1395         kqueue_drain(&kq, td);
1396         kqueue_destroy(&kq);
1397         return (error);
1398 }
1399
1400 int
1401 kqueue_add_filteropts(int filt, const struct filterops *filtops)
1402 {
1403         int error;
1404
1405         error = 0;
1406         if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) {
1407                 printf(
1408 "trying to add a filterop that is out of range: %d is beyond %d\n",
1409                     ~filt, EVFILT_SYSCOUNT);
1410                 return EINVAL;
1411         }
1412         mtx_lock(&filterops_lock);
1413         if (sysfilt_ops[~filt].for_fop != &null_filtops &&
1414             sysfilt_ops[~filt].for_fop != NULL)
1415                 error = EEXIST;
1416         else {
1417                 sysfilt_ops[~filt].for_fop = filtops;
1418                 sysfilt_ops[~filt].for_refcnt = 0;
1419         }
1420         mtx_unlock(&filterops_lock);
1421
1422         return (error);
1423 }
1424
1425 int
1426 kqueue_del_filteropts(int filt)
1427 {
1428         int error;
1429
1430         error = 0;
1431         if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
1432                 return EINVAL;
1433
1434         mtx_lock(&filterops_lock);
1435         if (sysfilt_ops[~filt].for_fop == &null_filtops ||
1436             sysfilt_ops[~filt].for_fop == NULL)
1437                 error = EINVAL;
1438         else if (sysfilt_ops[~filt].for_refcnt != 0)
1439                 error = EBUSY;
1440         else {
1441                 sysfilt_ops[~filt].for_fop = &null_filtops;
1442                 sysfilt_ops[~filt].for_refcnt = 0;
1443         }
1444         mtx_unlock(&filterops_lock);
1445
1446         return error;
1447 }
1448
1449 static const struct filterops *
1450 kqueue_fo_find(int filt)
1451 {
1452
1453         if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
1454                 return NULL;
1455
1456         if (sysfilt_ops[~filt].for_nolock)
1457                 return sysfilt_ops[~filt].for_fop;
1458
1459         mtx_lock(&filterops_lock);
1460         sysfilt_ops[~filt].for_refcnt++;
1461         if (sysfilt_ops[~filt].for_fop == NULL)
1462                 sysfilt_ops[~filt].for_fop = &null_filtops;
1463         mtx_unlock(&filterops_lock);
1464
1465         return sysfilt_ops[~filt].for_fop;
1466 }
1467
1468 static void
1469 kqueue_fo_release(int filt)
1470 {
1471
1472         if (filt > 0 || filt + EVFILT_SYSCOUNT < 0)
1473                 return;
1474
1475         if (sysfilt_ops[~filt].for_nolock)
1476                 return;
1477
1478         mtx_lock(&filterops_lock);
1479         KASSERT(sysfilt_ops[~filt].for_refcnt > 0,
1480             ("filter object refcount not valid on release"));
1481         sysfilt_ops[~filt].for_refcnt--;
1482         mtx_unlock(&filterops_lock);
1483 }
1484
1485 /*
1486  * A ref to kq (obtained via kqueue_acquire) must be held.
1487  */
1488 static int
1489 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td,
1490     int mflag)
1491 {
1492         const struct filterops *fops;
1493         struct file *fp;
1494         struct knote *kn, *tkn;
1495         struct knlist *knl;
1496         int error, filt, event;
1497         int haskqglobal, filedesc_unlock;
1498
1499         if ((kev->flags & (EV_ENABLE | EV_DISABLE)) == (EV_ENABLE | EV_DISABLE))
1500                 return (EINVAL);
1501
1502         fp = NULL;
1503         kn = NULL;
1504         knl = NULL;
1505         error = 0;
1506         haskqglobal = 0;
1507         filedesc_unlock = 0;
1508
1509         filt = kev->filter;
1510         fops = kqueue_fo_find(filt);
1511         if (fops == NULL)
1512                 return EINVAL;
1513
1514         if (kev->flags & EV_ADD) {
1515                 /*
1516                  * Prevent waiting with locks.  Non-sleepable
1517                  * allocation failures are handled in the loop, only
1518                  * if the spare knote appears to be actually required.
1519                  */
1520                 tkn = knote_alloc(mflag);
1521         } else {
1522                 tkn = NULL;
1523         }
1524
1525 findkn:
1526         if (fops->f_isfd) {
1527                 KASSERT(td != NULL, ("td is NULL"));
1528                 if (kev->ident > INT_MAX)
1529                         error = EBADF;
1530                 else
1531                         error = fget(td, kev->ident, &cap_event_rights, &fp);
1532                 if (error)
1533                         goto done;
1534
1535                 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops,
1536                     kev->ident, M_NOWAIT) != 0) {
1537                         /* try again */
1538                         fdrop(fp, td);
1539                         fp = NULL;
1540                         error = kqueue_expand(kq, fops, kev->ident, mflag);
1541                         if (error)
1542                                 goto done;
1543                         goto findkn;
1544                 }
1545
1546                 if (fp->f_type == DTYPE_KQUEUE) {
1547                         /*
1548                          * If we add some intelligence about what we are doing,
1549                          * we should be able to support events on ourselves.
1550                          * We need to know when we are doing this to prevent
1551                          * getting both the knlist lock and the kq lock since
1552                          * they are the same thing.
1553                          */
1554                         if (fp->f_data == kq) {
1555                                 error = EINVAL;
1556                                 goto done;
1557                         }
1558
1559                         /*
1560                          * Pre-lock the filedesc before the global
1561                          * lock mutex, see the comment in
1562                          * kqueue_close().
1563                          */
1564                         FILEDESC_XLOCK(td->td_proc->p_fd);
1565                         filedesc_unlock = 1;
1566                         KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1567                 }
1568
1569                 KQ_LOCK(kq);
1570                 if (kev->ident < kq->kq_knlistsize) {
1571                         SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link)
1572                                 if (kev->filter == kn->kn_filter)
1573                                         break;
1574                 }
1575         } else {
1576                 if ((kev->flags & EV_ADD) == EV_ADD) {
1577                         error = kqueue_expand(kq, fops, kev->ident, mflag);
1578                         if (error != 0)
1579                                 goto done;
1580                 }
1581
1582                 KQ_LOCK(kq);
1583
1584                 /*
1585                  * If possible, find an existing knote to use for this kevent.
1586                  */
1587                 if (kev->filter == EVFILT_PROC &&
1588                     (kev->flags & (EV_FLAG1 | EV_FLAG2)) != 0) {
1589                         /* This is an internal creation of a process tracking
1590                          * note. Don't attempt to coalesce this with an
1591                          * existing note.
1592                          */
1593                         ;
1594                 } else if (kq->kq_knhashmask != 0) {
1595                         struct klist *list;
1596
1597                         list = &kq->kq_knhash[
1598                             KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
1599                         SLIST_FOREACH(kn, list, kn_link)
1600                                 if (kev->ident == kn->kn_id &&
1601                                     kev->filter == kn->kn_filter)
1602                                         break;
1603                 }
1604         }
1605
1606         /* knote is in the process of changing, wait for it to stabilize. */
1607         if (kn != NULL && kn_in_flux(kn)) {
1608                 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1609                 if (filedesc_unlock) {
1610                         FILEDESC_XUNLOCK(td->td_proc->p_fd);
1611                         filedesc_unlock = 0;
1612                 }
1613                 kq->kq_state |= KQ_FLUXWAIT;
1614                 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0);
1615                 if (fp != NULL) {
1616                         fdrop(fp, td);
1617                         fp = NULL;
1618                 }
1619                 goto findkn;
1620         }
1621
1622         /*
1623          * kn now contains the matching knote, or NULL if no match
1624          */
1625         if (kn == NULL) {
1626                 if (kev->flags & EV_ADD) {
1627                         kn = tkn;
1628                         tkn = NULL;
1629                         if (kn == NULL) {
1630                                 KQ_UNLOCK(kq);
1631                                 error = ENOMEM;
1632                                 goto done;
1633                         }
1634                         kn->kn_fp = fp;
1635                         kn->kn_kq = kq;
1636                         kn->kn_fop = fops;
1637                         /*
1638                          * apply reference counts to knote structure, and
1639                          * do not release it at the end of this routine.
1640                          */
1641                         fops = NULL;
1642                         fp = NULL;
1643
1644                         kn->kn_sfflags = kev->fflags;
1645                         kn->kn_sdata = kev->data;
1646                         kev->fflags = 0;
1647                         kev->data = 0;
1648                         kn->kn_kevent = *kev;
1649                         kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE |
1650                             EV_ENABLE | EV_DISABLE | EV_FORCEONESHOT);
1651                         kn->kn_status = KN_DETACHED;
1652                         if ((kev->flags & EV_DISABLE) != 0)
1653                                 kn->kn_status |= KN_DISABLED;
1654                         kn_enter_flux(kn);
1655
1656                         error = knote_attach(kn, kq);
1657                         KQ_UNLOCK(kq);
1658                         if (error != 0) {
1659                                 tkn = kn;
1660                                 goto done;
1661                         }
1662
1663                         if ((error = kn->kn_fop->f_attach(kn)) != 0) {
1664                                 knote_drop_detached(kn, td);
1665                                 goto done;
1666                         }
1667                         knl = kn_list_lock(kn);
1668                         goto done_ev_add;
1669                 } else {
1670                         /* No matching knote and the EV_ADD flag is not set. */
1671                         KQ_UNLOCK(kq);
1672                         error = ENOENT;
1673                         goto done;
1674                 }
1675         }
1676
1677         if (kev->flags & EV_DELETE) {
1678                 kn_enter_flux(kn);
1679                 KQ_UNLOCK(kq);
1680                 knote_drop(kn, td);
1681                 goto done;
1682         }
1683
1684         if (kev->flags & EV_FORCEONESHOT) {
1685                 kn->kn_flags |= EV_ONESHOT;
1686                 KNOTE_ACTIVATE(kn, 1);
1687         }
1688
1689         if ((kev->flags & EV_ENABLE) != 0)
1690                 kn->kn_status &= ~KN_DISABLED;
1691         else if ((kev->flags & EV_DISABLE) != 0)
1692                 kn->kn_status |= KN_DISABLED;
1693
1694         /*
1695          * The user may change some filter values after the initial EV_ADD,
1696          * but doing so will not reset any filter which has already been
1697          * triggered.
1698          */
1699         kn->kn_status |= KN_SCAN;
1700         kn_enter_flux(kn);
1701         KQ_UNLOCK(kq);
1702         knl = kn_list_lock(kn);
1703         kn->kn_kevent.udata = kev->udata;
1704         if (!fops->f_isfd && fops->f_touch != NULL) {
1705                 fops->f_touch(kn, kev, EVENT_REGISTER);
1706         } else {
1707                 kn->kn_sfflags = kev->fflags;
1708                 kn->kn_sdata = kev->data;
1709         }
1710
1711 done_ev_add:
1712         /*
1713          * We can get here with kn->kn_knlist == NULL.  This can happen when
1714          * the initial attach event decides that the event is "completed"
1715          * already, e.g., filt_procattach() is called on a zombie process.  It
1716          * will call filt_proc() which will remove it from the list, and NULL
1717          * kn_knlist.
1718          *
1719          * KN_DISABLED will be stable while the knote is in flux, so the
1720          * unlocked read will not race with an update.
1721          */
1722         if ((kn->kn_status & KN_DISABLED) == 0)
1723                 event = kn->kn_fop->f_event(kn, 0);
1724         else
1725                 event = 0;
1726
1727         KQ_LOCK(kq);
1728         if (event)
1729                 kn->kn_status |= KN_ACTIVE;
1730         if ((kn->kn_status & (KN_ACTIVE | KN_DISABLED | KN_QUEUED)) ==
1731             KN_ACTIVE)
1732                 knote_enqueue(kn);
1733         kn->kn_status &= ~KN_SCAN;
1734         kn_leave_flux(kn);
1735         kn_list_unlock(knl);
1736         KQ_UNLOCK_FLUX(kq);
1737
1738 done:
1739         KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1740         if (filedesc_unlock)
1741                 FILEDESC_XUNLOCK(td->td_proc->p_fd);
1742         if (fp != NULL)
1743                 fdrop(fp, td);
1744         knote_free(tkn);
1745         if (fops != NULL)
1746                 kqueue_fo_release(filt);
1747         return (error);
1748 }
1749
1750 static int
1751 kqueue_acquire(struct file *fp, struct kqueue **kqp)
1752 {
1753         int error;
1754         struct kqueue *kq;
1755
1756         error = 0;
1757
1758         kq = fp->f_data;
1759         if (fp->f_type != DTYPE_KQUEUE || kq == NULL)
1760                 return (EBADF);
1761         *kqp = kq;
1762         KQ_LOCK(kq);
1763         if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) {
1764                 KQ_UNLOCK(kq);
1765                 return (EBADF);
1766         }
1767         kq->kq_refcnt++;
1768         KQ_UNLOCK(kq);
1769
1770         return error;
1771 }
1772
1773 static void
1774 kqueue_release(struct kqueue *kq, int locked)
1775 {
1776         if (locked)
1777                 KQ_OWNED(kq);
1778         else
1779                 KQ_LOCK(kq);
1780         kq->kq_refcnt--;
1781         if (kq->kq_refcnt == 1)
1782                 wakeup(&kq->kq_refcnt);
1783         if (!locked)
1784                 KQ_UNLOCK(kq);
1785 }
1786
1787 void
1788 kqueue_drain_schedtask(void)
1789 {
1790         taskqueue_quiesce(taskqueue_kqueue_ctx);
1791 }
1792
1793 static void
1794 kqueue_schedtask(struct kqueue *kq)
1795 {
1796         struct thread *td;
1797
1798         KQ_OWNED(kq);
1799         KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN),
1800             ("scheduling kqueue task while draining"));
1801
1802         if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) {
1803                 taskqueue_enqueue(taskqueue_kqueue_ctx, &kq->kq_task);
1804                 kq->kq_state |= KQ_TASKSCHED;
1805                 td = curthread;
1806                 thread_lock(td);
1807                 td->td_flags |= TDF_ASTPENDING | TDF_KQTICKLED;
1808                 thread_unlock(td);
1809         }
1810 }
1811
1812 /*
1813  * Expand the kq to make sure we have storage for fops/ident pair.
1814  *
1815  * Return 0 on success (or no work necessary), return errno on failure.
1816  */
1817 static int
1818 kqueue_expand(struct kqueue *kq, const struct filterops *fops, uintptr_t ident,
1819     int mflag)
1820 {
1821         struct klist *list, *tmp_knhash, *to_free;
1822         u_long tmp_knhashmask;
1823         int error, fd, size;
1824
1825         KQ_NOTOWNED(kq);
1826
1827         error = 0;
1828         to_free = NULL;
1829         if (fops->f_isfd) {
1830                 fd = ident;
1831                 if (kq->kq_knlistsize <= fd) {
1832                         size = kq->kq_knlistsize;
1833                         while (size <= fd)
1834                                 size += KQEXTENT;
1835                         list = malloc(size * sizeof(*list), M_KQUEUE, mflag);
1836                         if (list == NULL)
1837                                 return ENOMEM;
1838                         KQ_LOCK(kq);
1839                         if ((kq->kq_state & KQ_CLOSING) != 0) {
1840                                 to_free = list;
1841                                 error = EBADF;
1842                         } else if (kq->kq_knlistsize > fd) {
1843                                 to_free = list;
1844                         } else {
1845                                 if (kq->kq_knlist != NULL) {
1846                                         bcopy(kq->kq_knlist, list,
1847                                             kq->kq_knlistsize * sizeof(*list));
1848                                         to_free = kq->kq_knlist;
1849                                         kq->kq_knlist = NULL;
1850                                 }
1851                                 bzero((caddr_t)list +
1852                                     kq->kq_knlistsize * sizeof(*list),
1853                                     (size - kq->kq_knlistsize) * sizeof(*list));
1854                                 kq->kq_knlistsize = size;
1855                                 kq->kq_knlist = list;
1856                         }
1857                         KQ_UNLOCK(kq);
1858                 }
1859         } else {
1860                 if (kq->kq_knhashmask == 0) {
1861                         tmp_knhash = hashinit_flags(KN_HASHSIZE, M_KQUEUE,
1862                             &tmp_knhashmask, (mflag & M_WAITOK) != 0 ?
1863                             HASH_WAITOK : HASH_NOWAIT);
1864                         if (tmp_knhash == NULL)
1865                                 return (ENOMEM);
1866                         KQ_LOCK(kq);
1867                         if ((kq->kq_state & KQ_CLOSING) != 0) {
1868                                 to_free = tmp_knhash;
1869                                 error = EBADF;
1870                         } else if (kq->kq_knhashmask == 0) {
1871                                 kq->kq_knhash = tmp_knhash;
1872                                 kq->kq_knhashmask = tmp_knhashmask;
1873                         } else {
1874                                 to_free = tmp_knhash;
1875                         }
1876                         KQ_UNLOCK(kq);
1877                 }
1878         }
1879         free(to_free, M_KQUEUE);
1880
1881         KQ_NOTOWNED(kq);
1882         return (error);
1883 }
1884
1885 static void
1886 kqueue_task(void *arg, int pending)
1887 {
1888         struct kqueue *kq;
1889         int haskqglobal;
1890
1891         haskqglobal = 0;
1892         kq = arg;
1893
1894         KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
1895         KQ_LOCK(kq);
1896
1897         KNOTE_LOCKED(&kq->kq_sel.si_note, 0);
1898
1899         kq->kq_state &= ~KQ_TASKSCHED;
1900         if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) {
1901                 wakeup(&kq->kq_state);
1902         }
1903         KQ_UNLOCK(kq);
1904         KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
1905 }
1906
1907 /*
1908  * Scan, update kn_data (if not ONESHOT), and copyout triggered events.
1909  * We treat KN_MARKER knotes as if they are in flux.
1910  */
1911 static int
1912 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops,
1913     const struct timespec *tsp, struct kevent *keva, struct thread *td)
1914 {
1915         struct kevent *kevp;
1916         struct knote *kn, *marker;
1917         struct knlist *knl;
1918         sbintime_t asbt, rsbt;
1919         int count, error, haskqglobal, influx, nkev, touch;
1920
1921         count = maxevents;
1922         nkev = 0;
1923         error = 0;
1924         haskqglobal = 0;
1925
1926         if (maxevents == 0)
1927                 goto done_nl;
1928         if (maxevents < 0) {
1929                 error = EINVAL;
1930                 goto done_nl;
1931         }
1932
1933         rsbt = 0;
1934         if (tsp != NULL) {
1935                 if (!timespecvalid_interval(tsp)) {
1936                         error = EINVAL;
1937                         goto done_nl;
1938                 }
1939                 if (timespecisset(tsp)) {
1940                         if (tsp->tv_sec <= INT32_MAX) {
1941                                 rsbt = tstosbt(*tsp);
1942                                 if (TIMESEL(&asbt, rsbt))
1943                                         asbt += tc_tick_sbt;
1944                                 if (asbt <= SBT_MAX - rsbt)
1945                                         asbt += rsbt;
1946                                 else
1947                                         asbt = 0;
1948                                 rsbt >>= tc_precexp;
1949                         } else
1950                                 asbt = 0;
1951                 } else
1952                         asbt = -1;
1953         } else
1954                 asbt = 0;
1955         marker = knote_alloc(M_WAITOK);
1956         marker->kn_status = KN_MARKER;
1957         KQ_LOCK(kq);
1958
1959 retry:
1960         kevp = keva;
1961         if (kq->kq_count == 0) {
1962                 if (asbt == -1) {
1963                         error = EWOULDBLOCK;
1964                 } else {
1965                         kq->kq_state |= KQ_SLEEP;
1966                         error = msleep_sbt(kq, &kq->kq_lock, PSOCK | PCATCH,
1967                             "kqread", asbt, rsbt, C_ABSOLUTE);
1968                 }
1969                 if (error == 0)
1970                         goto retry;
1971                 /* don't restart after signals... */
1972                 if (error == ERESTART)
1973                         error = EINTR;
1974                 else if (error == EWOULDBLOCK)
1975                         error = 0;
1976                 goto done;
1977         }
1978
1979         TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe);
1980         influx = 0;
1981         while (count) {
1982                 KQ_OWNED(kq);
1983                 kn = TAILQ_FIRST(&kq->kq_head);
1984
1985                 if ((kn->kn_status == KN_MARKER && kn != marker) ||
1986                     kn_in_flux(kn)) {
1987                         if (influx) {
1988                                 influx = 0;
1989                                 KQ_FLUX_WAKEUP(kq);
1990                         }
1991                         kq->kq_state |= KQ_FLUXWAIT;
1992                         error = msleep(kq, &kq->kq_lock, PSOCK,
1993                             "kqflxwt", 0);
1994                         continue;
1995                 }
1996
1997                 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
1998                 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) {
1999                         kn->kn_status &= ~KN_QUEUED;
2000                         kq->kq_count--;
2001                         continue;
2002                 }
2003                 if (kn == marker) {
2004                         KQ_FLUX_WAKEUP(kq);
2005                         if (count == maxevents)
2006                                 goto retry;
2007                         goto done;
2008                 }
2009                 KASSERT(!kn_in_flux(kn),
2010                     ("knote %p is unexpectedly in flux", kn));
2011
2012                 if ((kn->kn_flags & EV_DROP) == EV_DROP) {
2013                         kn->kn_status &= ~KN_QUEUED;
2014                         kn_enter_flux(kn);
2015                         kq->kq_count--;
2016                         KQ_UNLOCK(kq);
2017                         /*
2018                          * We don't need to lock the list since we've
2019                          * marked it as in flux.
2020                          */
2021                         knote_drop(kn, td);
2022                         KQ_LOCK(kq);
2023                         continue;
2024                 } else if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) {
2025                         kn->kn_status &= ~KN_QUEUED;
2026                         kn_enter_flux(kn);
2027                         kq->kq_count--;
2028                         KQ_UNLOCK(kq);
2029                         /*
2030                          * We don't need to lock the list since we've
2031                          * marked the knote as being in flux.
2032                          */
2033                         *kevp = kn->kn_kevent;
2034                         knote_drop(kn, td);
2035                         KQ_LOCK(kq);
2036                         kn = NULL;
2037                 } else {
2038                         kn->kn_status |= KN_SCAN;
2039                         kn_enter_flux(kn);
2040                         KQ_UNLOCK(kq);
2041                         if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE)
2042                                 KQ_GLOBAL_LOCK(&kq_global, haskqglobal);
2043                         knl = kn_list_lock(kn);
2044                         if (kn->kn_fop->f_event(kn, 0) == 0) {
2045                                 KQ_LOCK(kq);
2046                                 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
2047                                 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE |
2048                                     KN_SCAN);
2049                                 kn_leave_flux(kn);
2050                                 kq->kq_count--;
2051                                 kn_list_unlock(knl);
2052                                 influx = 1;
2053                                 continue;
2054                         }
2055                         touch = (!kn->kn_fop->f_isfd &&
2056                             kn->kn_fop->f_touch != NULL);
2057                         if (touch)
2058                                 kn->kn_fop->f_touch(kn, kevp, EVENT_PROCESS);
2059                         else
2060                                 *kevp = kn->kn_kevent;
2061                         KQ_LOCK(kq);
2062                         KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal);
2063                         if (kn->kn_flags & (EV_CLEAR | EV_DISPATCH)) {
2064                                 /*
2065                                  * Manually clear knotes who weren't
2066                                  * 'touch'ed.
2067                                  */
2068                                 if (touch == 0 && kn->kn_flags & EV_CLEAR) {
2069                                         kn->kn_data = 0;
2070                                         kn->kn_fflags = 0;
2071                                 }
2072                                 if (kn->kn_flags & EV_DISPATCH)
2073                                         kn->kn_status |= KN_DISABLED;
2074                                 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
2075                                 kq->kq_count--;
2076                         } else
2077                                 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
2078
2079                         kn->kn_status &= ~KN_SCAN;
2080                         kn_leave_flux(kn);
2081                         kn_list_unlock(knl);
2082                         influx = 1;
2083                 }
2084
2085                 /* we are returning a copy to the user */
2086                 kevp++;
2087                 nkev++;
2088                 count--;
2089
2090                 if (nkev == KQ_NEVENTS) {
2091                         influx = 0;
2092                         KQ_UNLOCK_FLUX(kq);
2093                         error = k_ops->k_copyout(k_ops->arg, keva, nkev);
2094                         nkev = 0;
2095                         kevp = keva;
2096                         KQ_LOCK(kq);
2097                         if (error)
2098                                 break;
2099                 }
2100         }
2101         TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe);
2102 done:
2103         KQ_OWNED(kq);
2104         KQ_UNLOCK_FLUX(kq);
2105         knote_free(marker);
2106 done_nl:
2107         KQ_NOTOWNED(kq);
2108         if (nkev != 0)
2109                 error = k_ops->k_copyout(k_ops->arg, keva, nkev);
2110         td->td_retval[0] = maxevents - count;
2111         return (error);
2112 }
2113
2114 /*ARGSUSED*/
2115 static int
2116 kqueue_ioctl(struct file *fp, u_long cmd, void *data,
2117         struct ucred *active_cred, struct thread *td)
2118 {
2119         /*
2120          * Enabling sigio causes two major problems:
2121          * 1) infinite recursion:
2122          * Synopsys: kevent is being used to track signals and have FIOASYNC
2123          * set.  On receipt of a signal this will cause a kqueue to recurse
2124          * into itself over and over.  Sending the sigio causes the kqueue
2125          * to become ready, which in turn posts sigio again, forever.
2126          * Solution: this can be solved by setting a flag in the kqueue that
2127          * we have a SIGIO in progress.
2128          * 2) locking problems:
2129          * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts
2130          * us above the proc and pgrp locks.
2131          * Solution: Post a signal using an async mechanism, being sure to
2132          * record a generation count in the delivery so that we do not deliver
2133          * a signal to the wrong process.
2134          *
2135          * Note, these two mechanisms are somewhat mutually exclusive!
2136          */
2137 #if 0
2138         struct kqueue *kq;
2139
2140         kq = fp->f_data;
2141         switch (cmd) {
2142         case FIOASYNC:
2143                 if (*(int *)data) {
2144                         kq->kq_state |= KQ_ASYNC;
2145                 } else {
2146                         kq->kq_state &= ~KQ_ASYNC;
2147                 }
2148                 return (0);
2149
2150         case FIOSETOWN:
2151                 return (fsetown(*(int *)data, &kq->kq_sigio));
2152
2153         case FIOGETOWN:
2154                 *(int *)data = fgetown(&kq->kq_sigio);
2155                 return (0);
2156         }
2157 #endif
2158
2159         return (ENOTTY);
2160 }
2161
2162 /*ARGSUSED*/
2163 static int
2164 kqueue_poll(struct file *fp, int events, struct ucred *active_cred,
2165         struct thread *td)
2166 {
2167         struct kqueue *kq;
2168         int revents = 0;
2169         int error;
2170
2171         if ((error = kqueue_acquire(fp, &kq)))
2172                 return POLLERR;
2173
2174         KQ_LOCK(kq);
2175         if (events & (POLLIN | POLLRDNORM)) {
2176                 if (kq->kq_count) {
2177                         revents |= events & (POLLIN | POLLRDNORM);
2178                 } else {
2179                         selrecord(td, &kq->kq_sel);
2180                         if (SEL_WAITING(&kq->kq_sel))
2181                                 kq->kq_state |= KQ_SEL;
2182                 }
2183         }
2184         kqueue_release(kq, 1);
2185         KQ_UNLOCK(kq);
2186         return (revents);
2187 }
2188
2189 /*ARGSUSED*/
2190 static int
2191 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
2192         struct thread *td)
2193 {
2194
2195         bzero((void *)st, sizeof *st);
2196         /*
2197          * We no longer return kq_count because the unlocked value is useless.
2198          * If you spent all this time getting the count, why not spend your
2199          * syscall better by calling kevent?
2200          *
2201          * XXX - This is needed for libc_r.
2202          */
2203         st->st_mode = S_IFIFO;
2204         return (0);
2205 }
2206
2207 static void
2208 kqueue_drain(struct kqueue *kq, struct thread *td)
2209 {
2210         struct knote *kn;
2211         int i;
2212
2213         KQ_LOCK(kq);
2214
2215         KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING,
2216             ("kqueue already closing"));
2217         kq->kq_state |= KQ_CLOSING;
2218         if (kq->kq_refcnt > 1)
2219                 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0);
2220
2221         KASSERT(kq->kq_refcnt == 1, ("other refs are out there!"));
2222
2223         KASSERT(knlist_empty(&kq->kq_sel.si_note),
2224             ("kqueue's knlist not empty"));
2225
2226         for (i = 0; i < kq->kq_knlistsize; i++) {
2227                 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) {
2228                         if (kn_in_flux(kn)) {
2229                                 kq->kq_state |= KQ_FLUXWAIT;
2230                                 msleep(kq, &kq->kq_lock, PSOCK, "kqclo1", 0);
2231                                 continue;
2232                         }
2233                         kn_enter_flux(kn);
2234                         KQ_UNLOCK(kq);
2235                         knote_drop(kn, td);
2236                         KQ_LOCK(kq);
2237                 }
2238         }
2239         if (kq->kq_knhashmask != 0) {
2240                 for (i = 0; i <= kq->kq_knhashmask; i++) {
2241                         while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) {
2242                                 if (kn_in_flux(kn)) {
2243                                         kq->kq_state |= KQ_FLUXWAIT;
2244                                         msleep(kq, &kq->kq_lock, PSOCK,
2245                                                "kqclo2", 0);
2246                                         continue;
2247                                 }
2248                                 kn_enter_flux(kn);
2249                                 KQ_UNLOCK(kq);
2250                                 knote_drop(kn, td);
2251                                 KQ_LOCK(kq);
2252                         }
2253                 }
2254         }
2255
2256         if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) {
2257                 kq->kq_state |= KQ_TASKDRAIN;
2258                 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0);
2259         }
2260
2261         if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
2262                 selwakeuppri(&kq->kq_sel, PSOCK);
2263                 if (!SEL_WAITING(&kq->kq_sel))
2264                         kq->kq_state &= ~KQ_SEL;
2265         }
2266
2267         KQ_UNLOCK(kq);
2268 }
2269
2270 static void
2271 kqueue_destroy(struct kqueue *kq)
2272 {
2273
2274         KASSERT(kq->kq_fdp == NULL,
2275             ("kqueue still attached to a file descriptor"));
2276         seldrain(&kq->kq_sel);
2277         knlist_destroy(&kq->kq_sel.si_note);
2278         mtx_destroy(&kq->kq_lock);
2279
2280         if (kq->kq_knhash != NULL)
2281                 free(kq->kq_knhash, M_KQUEUE);
2282         if (kq->kq_knlist != NULL)
2283                 free(kq->kq_knlist, M_KQUEUE);
2284
2285         funsetown(&kq->kq_sigio);
2286 }
2287
2288 /*ARGSUSED*/
2289 static int
2290 kqueue_close(struct file *fp, struct thread *td)
2291 {
2292         struct kqueue *kq = fp->f_data;
2293         struct filedesc *fdp;
2294         int error;
2295         int filedesc_unlock;
2296
2297         if ((error = kqueue_acquire(fp, &kq)))
2298                 return error;
2299         kqueue_drain(kq, td);
2300
2301         /*
2302          * We could be called due to the knote_drop() doing fdrop(),
2303          * called from kqueue_register().  In this case the global
2304          * lock is owned, and filedesc sx is locked before, to not
2305          * take the sleepable lock after non-sleepable.
2306          */
2307         fdp = kq->kq_fdp;
2308         kq->kq_fdp = NULL;
2309         if (!sx_xlocked(FILEDESC_LOCK(fdp))) {
2310                 FILEDESC_XLOCK(fdp);
2311                 filedesc_unlock = 1;
2312         } else
2313                 filedesc_unlock = 0;
2314         TAILQ_REMOVE(&fdp->fd_kqlist, kq, kq_list);
2315         if (filedesc_unlock)
2316                 FILEDESC_XUNLOCK(fdp);
2317
2318         kqueue_destroy(kq);
2319         chgkqcnt(kq->kq_cred->cr_ruidinfo, -1, 0);
2320         crfree(kq->kq_cred);
2321         free(kq, M_KQUEUE);
2322         fp->f_data = NULL;
2323
2324         return (0);
2325 }
2326
2327 static int
2328 kqueue_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
2329 {
2330         struct kqueue *kq = fp->f_data;
2331
2332         kif->kf_type = KF_TYPE_KQUEUE;
2333         kif->kf_un.kf_kqueue.kf_kqueue_addr = (uintptr_t)kq;
2334         kif->kf_un.kf_kqueue.kf_kqueue_count = kq->kq_count;
2335         kif->kf_un.kf_kqueue.kf_kqueue_state = kq->kq_state;
2336         return (0);
2337 }
2338
2339 static void
2340 kqueue_wakeup(struct kqueue *kq)
2341 {
2342         KQ_OWNED(kq);
2343
2344         if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) {
2345                 kq->kq_state &= ~KQ_SLEEP;
2346                 wakeup(kq);
2347         }
2348         if ((kq->kq_state & KQ_SEL) == KQ_SEL) {
2349                 selwakeuppri(&kq->kq_sel, PSOCK);
2350                 if (!SEL_WAITING(&kq->kq_sel))
2351                         kq->kq_state &= ~KQ_SEL;
2352         }
2353         if (!knlist_empty(&kq->kq_sel.si_note))
2354                 kqueue_schedtask(kq);
2355         if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) {
2356                 pgsigio(&kq->kq_sigio, SIGIO, 0);
2357         }
2358 }
2359
2360 /*
2361  * Walk down a list of knotes, activating them if their event has triggered.
2362  *
2363  * There is a possibility to optimize in the case of one kq watching another.
2364  * Instead of scheduling a task to wake it up, you could pass enough state
2365  * down the chain to make up the parent kqueue.  Make this code functional
2366  * first.
2367  */
2368 void
2369 knote(struct knlist *list, long hint, int lockflags)
2370 {
2371         struct kqueue *kq;
2372         struct knote *kn, *tkn;
2373         int error;
2374
2375         if (list == NULL)
2376                 return;
2377
2378         KNL_ASSERT_LOCK(list, lockflags & KNF_LISTLOCKED);
2379
2380         if ((lockflags & KNF_LISTLOCKED) == 0)
2381                 list->kl_lock(list->kl_lockarg); 
2382
2383         /*
2384          * If we unlock the list lock (and enter influx), we can
2385          * eliminate the kqueue scheduling, but this will introduce
2386          * four lock/unlock's for each knote to test.  Also, marker
2387          * would be needed to keep iteration position, since filters
2388          * or other threads could remove events.
2389          */
2390         SLIST_FOREACH_SAFE(kn, &list->kl_list, kn_selnext, tkn) {
2391                 kq = kn->kn_kq;
2392                 KQ_LOCK(kq);
2393                 if (kn_in_flux(kn) && (kn->kn_status & KN_SCAN) == 0) {
2394                         /*
2395                          * Do not process the influx notes, except for
2396                          * the influx coming from the kq unlock in the
2397                          * kqueue_scan().  In the later case, we do
2398                          * not interfere with the scan, since the code
2399                          * fragment in kqueue_scan() locks the knlist,
2400                          * and cannot proceed until we finished.
2401                          */
2402                         KQ_UNLOCK(kq);
2403                 } else if ((lockflags & KNF_NOKQLOCK) != 0) {
2404                         kn_enter_flux(kn);
2405                         KQ_UNLOCK(kq);
2406                         error = kn->kn_fop->f_event(kn, hint);
2407                         KQ_LOCK(kq);
2408                         kn_leave_flux(kn);
2409                         if (error)
2410                                 KNOTE_ACTIVATE(kn, 1);
2411                         KQ_UNLOCK_FLUX(kq);
2412                 } else {
2413                         if (kn->kn_fop->f_event(kn, hint))
2414                                 KNOTE_ACTIVATE(kn, 1);
2415                         KQ_UNLOCK(kq);
2416                 }
2417         }
2418         if ((lockflags & KNF_LISTLOCKED) == 0)
2419                 list->kl_unlock(list->kl_lockarg); 
2420 }
2421
2422 /*
2423  * add a knote to a knlist
2424  */
2425 void
2426 knlist_add(struct knlist *knl, struct knote *kn, int islocked)
2427 {
2428
2429         KNL_ASSERT_LOCK(knl, islocked);
2430         KQ_NOTOWNED(kn->kn_kq);
2431         KASSERT(kn_in_flux(kn), ("knote %p not in flux", kn));
2432         KASSERT((kn->kn_status & KN_DETACHED) != 0,
2433             ("knote %p was not detached", kn));
2434         if (!islocked)
2435                 knl->kl_lock(knl->kl_lockarg);
2436         SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext);
2437         if (!islocked)
2438                 knl->kl_unlock(knl->kl_lockarg);
2439         KQ_LOCK(kn->kn_kq);
2440         kn->kn_knlist = knl;
2441         kn->kn_status &= ~KN_DETACHED;
2442         KQ_UNLOCK(kn->kn_kq);
2443 }
2444
2445 static void
2446 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked,
2447     int kqislocked)
2448 {
2449
2450         KASSERT(!kqislocked || knlislocked, ("kq locked w/o knl locked"));
2451         KNL_ASSERT_LOCK(knl, knlislocked);
2452         mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED);
2453         KASSERT(kqislocked || kn_in_flux(kn), ("knote %p not in flux", kn));
2454         KASSERT((kn->kn_status & KN_DETACHED) == 0,
2455             ("knote %p was already detached", kn));
2456         if (!knlislocked)
2457                 knl->kl_lock(knl->kl_lockarg);
2458         SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext);
2459         kn->kn_knlist = NULL;
2460         if (!knlislocked)
2461                 kn_list_unlock(knl);
2462         if (!kqislocked)
2463                 KQ_LOCK(kn->kn_kq);
2464         kn->kn_status |= KN_DETACHED;
2465         if (!kqislocked)
2466                 KQ_UNLOCK(kn->kn_kq);
2467 }
2468
2469 /*
2470  * remove knote from the specified knlist
2471  */
2472 void
2473 knlist_remove(struct knlist *knl, struct knote *kn, int islocked)
2474 {
2475
2476         knlist_remove_kq(knl, kn, islocked, 0);
2477 }
2478
2479 int
2480 knlist_empty(struct knlist *knl)
2481 {
2482
2483         KNL_ASSERT_LOCKED(knl);
2484         return (SLIST_EMPTY(&knl->kl_list));
2485 }
2486
2487 static struct mtx knlist_lock;
2488 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects",
2489     MTX_DEF);
2490 static void knlist_mtx_lock(void *arg);
2491 static void knlist_mtx_unlock(void *arg);
2492
2493 static void
2494 knlist_mtx_lock(void *arg)
2495 {
2496
2497         mtx_lock((struct mtx *)arg);
2498 }
2499
2500 static void
2501 knlist_mtx_unlock(void *arg)
2502 {
2503
2504         mtx_unlock((struct mtx *)arg);
2505 }
2506
2507 static void
2508 knlist_mtx_assert_lock(void *arg, int what)
2509 {
2510
2511         if (what == LA_LOCKED)
2512                 mtx_assert((struct mtx *)arg, MA_OWNED);
2513         else
2514                 mtx_assert((struct mtx *)arg, MA_NOTOWNED);
2515 }
2516
2517 static void
2518 knlist_rw_rlock(void *arg)
2519 {
2520
2521         rw_rlock((struct rwlock *)arg);
2522 }
2523
2524 static void
2525 knlist_rw_runlock(void *arg)
2526 {
2527
2528         rw_runlock((struct rwlock *)arg);
2529 }
2530
2531 static void
2532 knlist_rw_assert_lock(void *arg, int what)
2533 {
2534
2535         if (what == LA_LOCKED)
2536                 rw_assert((struct rwlock *)arg, RA_LOCKED);
2537         else
2538                 rw_assert((struct rwlock *)arg, RA_UNLOCKED);
2539 }
2540
2541 void
2542 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *),
2543     void (*kl_unlock)(void *),
2544     void (*kl_assert_lock)(void *, int))
2545 {
2546
2547         if (lock == NULL)
2548                 knl->kl_lockarg = &knlist_lock;
2549         else
2550                 knl->kl_lockarg = lock;
2551
2552         if (kl_lock == NULL)
2553                 knl->kl_lock = knlist_mtx_lock;
2554         else
2555                 knl->kl_lock = kl_lock;
2556         if (kl_unlock == NULL)
2557                 knl->kl_unlock = knlist_mtx_unlock;
2558         else
2559                 knl->kl_unlock = kl_unlock;
2560         if (kl_assert_lock == NULL)
2561                 knl->kl_assert_lock = knlist_mtx_assert_lock;
2562         else
2563                 knl->kl_assert_lock = kl_assert_lock;
2564
2565         knl->kl_autodestroy = 0;
2566         SLIST_INIT(&knl->kl_list);
2567 }
2568
2569 void
2570 knlist_init_mtx(struct knlist *knl, struct mtx *lock)
2571 {
2572
2573         knlist_init(knl, lock, NULL, NULL, NULL);
2574 }
2575
2576 struct knlist *
2577 knlist_alloc(struct mtx *lock)
2578 {
2579         struct knlist *knl;
2580
2581         knl = malloc(sizeof(struct knlist), M_KQUEUE, M_WAITOK);
2582         knlist_init_mtx(knl, lock);
2583         return (knl);
2584 }
2585
2586 void
2587 knlist_init_rw_reader(struct knlist *knl, struct rwlock *lock)
2588 {
2589
2590         knlist_init(knl, lock, knlist_rw_rlock, knlist_rw_runlock,
2591             knlist_rw_assert_lock);
2592 }
2593
2594 void
2595 knlist_destroy(struct knlist *knl)
2596 {
2597
2598         KASSERT(KNLIST_EMPTY(knl),
2599             ("destroying knlist %p with knotes on it", knl));
2600 }
2601
2602 void
2603 knlist_detach(struct knlist *knl)
2604 {
2605
2606         KNL_ASSERT_LOCKED(knl);
2607         knl->kl_autodestroy = 1;
2608         if (knlist_empty(knl)) {
2609                 knlist_destroy(knl);
2610                 free(knl, M_KQUEUE);
2611         }
2612 }
2613
2614 /*
2615  * Even if we are locked, we may need to drop the lock to allow any influx
2616  * knotes time to "settle".
2617  */
2618 void
2619 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn)
2620 {
2621         struct knote *kn, *kn2;
2622         struct kqueue *kq;
2623
2624         KASSERT(!knl->kl_autodestroy, ("cleardel for autodestroy %p", knl));
2625         if (islocked)
2626                 KNL_ASSERT_LOCKED(knl);
2627         else {
2628                 KNL_ASSERT_UNLOCKED(knl);
2629 again:          /* need to reacquire lock since we have dropped it */
2630                 knl->kl_lock(knl->kl_lockarg);
2631         }
2632
2633         SLIST_FOREACH_SAFE(kn, &knl->kl_list, kn_selnext, kn2) {
2634                 kq = kn->kn_kq;
2635                 KQ_LOCK(kq);
2636                 if (kn_in_flux(kn)) {
2637                         KQ_UNLOCK(kq);
2638                         continue;
2639                 }
2640                 knlist_remove_kq(knl, kn, 1, 1);
2641                 if (killkn) {
2642                         kn_enter_flux(kn);
2643                         KQ_UNLOCK(kq);
2644                         knote_drop_detached(kn, td);
2645                 } else {
2646                         /* Make sure cleared knotes disappear soon */
2647                         kn->kn_flags |= EV_EOF | EV_ONESHOT;
2648                         KQ_UNLOCK(kq);
2649                 }
2650                 kq = NULL;
2651         }
2652
2653         if (!SLIST_EMPTY(&knl->kl_list)) {
2654                 /* there are still in flux knotes remaining */
2655                 kn = SLIST_FIRST(&knl->kl_list);
2656                 kq = kn->kn_kq;
2657                 KQ_LOCK(kq);
2658                 KASSERT(kn_in_flux(kn), ("knote removed w/o list lock"));
2659                 knl->kl_unlock(knl->kl_lockarg);
2660                 kq->kq_state |= KQ_FLUXWAIT;
2661                 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0);
2662                 kq = NULL;
2663                 goto again;
2664         }
2665
2666         if (islocked)
2667                 KNL_ASSERT_LOCKED(knl);
2668         else {
2669                 knl->kl_unlock(knl->kl_lockarg);
2670                 KNL_ASSERT_UNLOCKED(knl);
2671         }
2672 }
2673
2674 /*
2675  * Remove all knotes referencing a specified fd must be called with FILEDESC
2676  * lock.  This prevents a race where a new fd comes along and occupies the
2677  * entry and we attach a knote to the fd.
2678  */
2679 void
2680 knote_fdclose(struct thread *td, int fd)
2681 {
2682         struct filedesc *fdp = td->td_proc->p_fd;
2683         struct kqueue *kq;
2684         struct knote *kn;
2685         int influx;
2686
2687         FILEDESC_XLOCK_ASSERT(fdp);
2688
2689         /*
2690          * We shouldn't have to worry about new kevents appearing on fd
2691          * since filedesc is locked.
2692          */
2693         TAILQ_FOREACH(kq, &fdp->fd_kqlist, kq_list) {
2694                 KQ_LOCK(kq);
2695
2696 again:
2697                 influx = 0;
2698                 while (kq->kq_knlistsize > fd &&
2699                     (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) {
2700                         if (kn_in_flux(kn)) {
2701                                 /* someone else might be waiting on our knote */
2702                                 if (influx)
2703                                         wakeup(kq);
2704                                 kq->kq_state |= KQ_FLUXWAIT;
2705                                 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0);
2706                                 goto again;
2707                         }
2708                         kn_enter_flux(kn);
2709                         KQ_UNLOCK(kq);
2710                         influx = 1;
2711                         knote_drop(kn, td);
2712                         KQ_LOCK(kq);
2713                 }
2714                 KQ_UNLOCK_FLUX(kq);
2715         }
2716 }
2717
2718 static int
2719 knote_attach(struct knote *kn, struct kqueue *kq)
2720 {
2721         struct klist *list;
2722
2723         KASSERT(kn_in_flux(kn), ("knote %p not marked influx", kn));
2724         KQ_OWNED(kq);
2725
2726         if ((kq->kq_state & KQ_CLOSING) != 0)
2727                 return (EBADF);
2728         if (kn->kn_fop->f_isfd) {
2729                 if (kn->kn_id >= kq->kq_knlistsize)
2730                         return (ENOMEM);
2731                 list = &kq->kq_knlist[kn->kn_id];
2732         } else {
2733                 if (kq->kq_knhash == NULL)
2734                         return (ENOMEM);
2735                 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2736         }
2737         SLIST_INSERT_HEAD(list, kn, kn_link);
2738         return (0);
2739 }
2740
2741 static void
2742 knote_drop(struct knote *kn, struct thread *td)
2743 {
2744
2745         if ((kn->kn_status & KN_DETACHED) == 0)
2746                 kn->kn_fop->f_detach(kn);
2747         knote_drop_detached(kn, td);
2748 }
2749
2750 static void
2751 knote_drop_detached(struct knote *kn, struct thread *td)
2752 {
2753         struct kqueue *kq;
2754         struct klist *list;
2755
2756         kq = kn->kn_kq;
2757
2758         KASSERT((kn->kn_status & KN_DETACHED) != 0,
2759             ("knote %p still attached", kn));
2760         KQ_NOTOWNED(kq);
2761
2762         KQ_LOCK(kq);
2763         KASSERT(kn->kn_influx == 1,
2764             ("knote_drop called on %p with influx %d", kn, kn->kn_influx));
2765
2766         if (kn->kn_fop->f_isfd)
2767                 list = &kq->kq_knlist[kn->kn_id];
2768         else
2769                 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
2770
2771         if (!SLIST_EMPTY(list))
2772                 SLIST_REMOVE(list, kn, knote, kn_link);
2773         if (kn->kn_status & KN_QUEUED)
2774                 knote_dequeue(kn);
2775         KQ_UNLOCK_FLUX(kq);
2776
2777         if (kn->kn_fop->f_isfd) {
2778                 fdrop(kn->kn_fp, td);
2779                 kn->kn_fp = NULL;
2780         }
2781         kqueue_fo_release(kn->kn_kevent.filter);
2782         kn->kn_fop = NULL;
2783         knote_free(kn);
2784 }
2785
2786 static void
2787 knote_enqueue(struct knote *kn)
2788 {
2789         struct kqueue *kq = kn->kn_kq;
2790
2791         KQ_OWNED(kn->kn_kq);
2792         KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
2793
2794         TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe);
2795         kn->kn_status |= KN_QUEUED;
2796         kq->kq_count++;
2797         kqueue_wakeup(kq);
2798 }
2799
2800 static void
2801 knote_dequeue(struct knote *kn)
2802 {
2803         struct kqueue *kq = kn->kn_kq;
2804
2805         KQ_OWNED(kn->kn_kq);
2806         KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
2807
2808         TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe);
2809         kn->kn_status &= ~KN_QUEUED;
2810         kq->kq_count--;
2811 }
2812
2813 static void
2814 knote_init(void)
2815 {
2816
2817         knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL,
2818             NULL, NULL, UMA_ALIGN_PTR, 0);
2819 }
2820 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL);
2821
2822 static struct knote *
2823 knote_alloc(int mflag)
2824 {
2825
2826         return (uma_zalloc(knote_zone, mflag | M_ZERO));
2827 }
2828
2829 static void
2830 knote_free(struct knote *kn)
2831 {
2832
2833         uma_zfree(knote_zone, kn);
2834 }
2835
2836 /*
2837  * Register the kev w/ the kq specified by fd.
2838  */
2839 int
2840 kqfd_register(int fd, struct kevent *kev, struct thread *td, int mflag)
2841 {
2842         struct kqueue *kq;
2843         struct file *fp;
2844         cap_rights_t rights;
2845         int error;
2846
2847         error = fget(td, fd, cap_rights_init_one(&rights, CAP_KQUEUE_CHANGE),
2848             &fp);
2849         if (error != 0)
2850                 return (error);
2851         if ((error = kqueue_acquire(fp, &kq)) != 0)
2852                 goto noacquire;
2853
2854         error = kqueue_register(kq, kev, td, mflag);
2855         kqueue_release(kq, 0);
2856
2857 noacquire:
2858         fdrop(fp, td);
2859         return (error);
2860 }