xref: /dflybsd-src/sys/kern/kern_event.c (revision f3834e0d94a74d42752caa878a0d9060f94545d2)
1 /*-
2  * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/kern/kern_event.c,v 1.2.2.10 2004/04/04 07:03:14 cperciva Exp $
27  * $DragonFly: src/sys/kern/kern_event.c,v 1.33 2007/02/03 17:05:57 corecode Exp $
28  */
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/proc.h>
34 #include <sys/malloc.h>
35 #include <sys/unistd.h>
36 #include <sys/file.h>
37 #include <sys/lock.h>
38 #include <sys/fcntl.h>
39 #include <sys/queue.h>
40 #include <sys/event.h>
41 #include <sys/eventvar.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/stat.h>
46 #include <sys/sysctl.h>
47 #include <sys/sysproto.h>
48 #include <sys/thread.h>
49 #include <sys/uio.h>
50 #include <sys/signalvar.h>
51 #include <sys/filio.h>
52 #include <sys/ktr.h>
53 
54 #include <sys/thread2.h>
55 #include <sys/file2.h>
56 #include <sys/mplock2.h>
57 
58 #include <vm/vm_zone.h>
59 
60 /*
61  * Global token for kqueue subsystem
62  */
63 struct lwkt_token kq_token = LWKT_TOKEN_INITIALIZER(kq_token);
64 SYSCTL_LONG(_lwkt, OID_AUTO, kq_collisions,
65     CTLFLAG_RW, &kq_token.t_collisions, 0,
66     "Collision counter of kq_token");
67 
68 MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system");
69 
70 struct kevent_copyin_args {
71 	struct kevent_args	*ka;
72 	int			pchanges;
73 };
74 
75 static int	kqueue_sleep(struct kqueue *kq, struct timespec *tsp);
76 static int	kqueue_scan(struct kqueue *kq, struct kevent *kevp, int count,
77 		    struct knote *marker);
78 static int 	kqueue_read(struct file *fp, struct uio *uio,
79 		    struct ucred *cred, int flags);
80 static int	kqueue_write(struct file *fp, struct uio *uio,
81 		    struct ucred *cred, int flags);
82 static int	kqueue_ioctl(struct file *fp, u_long com, caddr_t data,
83 		    struct ucred *cred, struct sysmsg *msg);
84 static int 	kqueue_kqfilter(struct file *fp, struct knote *kn);
85 static int 	kqueue_stat(struct file *fp, struct stat *st,
86 		    struct ucred *cred);
87 static int 	kqueue_close(struct file *fp);
88 static void	kqueue_wakeup(struct kqueue *kq);
89 static int	filter_attach(struct knote *kn);
90 static int	filter_event(struct knote *kn, long hint);
91 
92 /*
93  * MPSAFE
94  */
95 static struct fileops kqueueops = {
96 	.fo_read = kqueue_read,
97 	.fo_write = kqueue_write,
98 	.fo_ioctl = kqueue_ioctl,
99 	.fo_kqfilter = kqueue_kqfilter,
100 	.fo_stat = kqueue_stat,
101 	.fo_close = kqueue_close,
102 	.fo_shutdown = nofo_shutdown
103 };
104 
105 static void 	knote_attach(struct knote *kn);
106 static void 	knote_drop(struct knote *kn);
107 static void	knote_detach_and_drop(struct knote *kn);
108 static void 	knote_enqueue(struct knote *kn);
109 static void 	knote_dequeue(struct knote *kn);
110 static void 	knote_init(void);
111 static struct 	knote *knote_alloc(void);
112 static void 	knote_free(struct knote *kn);
113 
114 static void	filt_kqdetach(struct knote *kn);
115 static int	filt_kqueue(struct knote *kn, long hint);
116 static int	filt_procattach(struct knote *kn);
117 static void	filt_procdetach(struct knote *kn);
118 static int	filt_proc(struct knote *kn, long hint);
119 static int	filt_fileattach(struct knote *kn);
120 static void	filt_timerexpire(void *knx);
121 static int	filt_timerattach(struct knote *kn);
122 static void	filt_timerdetach(struct knote *kn);
123 static int	filt_timer(struct knote *kn, long hint);
124 
125 static struct filterops file_filtops =
126 	{ FILTEROP_ISFD, filt_fileattach, NULL, NULL };
127 static struct filterops kqread_filtops =
128 	{ FILTEROP_ISFD, NULL, filt_kqdetach, filt_kqueue };
129 static struct filterops proc_filtops =
130 	{ 0, filt_procattach, filt_procdetach, filt_proc };
131 static struct filterops timer_filtops =
132 	{ 0, filt_timerattach, filt_timerdetach, filt_timer };
133 
134 static vm_zone_t	knote_zone;
135 static int 		kq_ncallouts = 0;
136 static int 		kq_calloutmax = (4 * 1024);
137 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW,
138     &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue");
139 static int		kq_checkloop = 1000000;
140 SYSCTL_INT(_kern, OID_AUTO, kq_checkloop, CTLFLAG_RW,
141     &kq_checkloop, 0, "Maximum number of callouts allocated for kqueue");
142 
143 #define KNOTE_ACTIVATE(kn) do { 					\
144 	kn->kn_status |= KN_ACTIVE;					\
145 	if ((kn->kn_status & (KN_QUEUED | KN_DISABLED)) == 0)		\
146 		knote_enqueue(kn);					\
147 } while(0)
148 
149 #define	KN_HASHSIZE		64		/* XXX should be tunable */
150 #define KN_HASH(val, mask)	(((val) ^ (val >> 8)) & (mask))
151 
152 extern struct filterops aio_filtops;
153 extern struct filterops sig_filtops;
154 
155 /*
156  * Table for for all system-defined filters.
157  */
158 static struct filterops *sysfilt_ops[] = {
159 	&file_filtops,			/* EVFILT_READ */
160 	&file_filtops,			/* EVFILT_WRITE */
161 	&aio_filtops,			/* EVFILT_AIO */
162 	&file_filtops,			/* EVFILT_VNODE */
163 	&proc_filtops,			/* EVFILT_PROC */
164 	&sig_filtops,			/* EVFILT_SIGNAL */
165 	&timer_filtops,			/* EVFILT_TIMER */
166 	&file_filtops,			/* EVFILT_EXCEPT */
167 };
168 
169 static int
170 filt_fileattach(struct knote *kn)
171 {
172 	return (fo_kqfilter(kn->kn_fp, kn));
173 }
174 
175 /*
176  * MPSAFE
177  */
178 static int
179 kqueue_kqfilter(struct file *fp, struct knote *kn)
180 {
181 	struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
182 
183 	if (kn->kn_filter != EVFILT_READ)
184 		return (EOPNOTSUPP);
185 
186 	kn->kn_fop = &kqread_filtops;
187 	knote_insert(&kq->kq_kqinfo.ki_note, kn);
188 	return (0);
189 }
190 
191 static void
192 filt_kqdetach(struct knote *kn)
193 {
194 	struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
195 
196 	knote_remove(&kq->kq_kqinfo.ki_note, kn);
197 }
198 
199 /*ARGSUSED*/
200 static int
201 filt_kqueue(struct knote *kn, long hint)
202 {
203 	struct kqueue *kq = (struct kqueue *)kn->kn_fp->f_data;
204 
205 	kn->kn_data = kq->kq_count;
206 	return (kn->kn_data > 0);
207 }
208 
209 static int
210 filt_procattach(struct knote *kn)
211 {
212 	struct proc *p;
213 	int immediate;
214 
215 	immediate = 0;
216 	lwkt_gettoken(&proc_token);
217 	p = pfind(kn->kn_id);
218 	if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) {
219 		p = zpfind(kn->kn_id);
220 		immediate = 1;
221 	}
222 	if (p == NULL) {
223 		lwkt_reltoken(&proc_token);
224 		return (ESRCH);
225 	}
226 	if (!PRISON_CHECK(curthread->td_ucred, p->p_ucred)) {
227 		lwkt_reltoken(&proc_token);
228 		return (EACCES);
229 	}
230 
231 	kn->kn_ptr.p_proc = p;
232 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
233 
234 	/*
235 	 * internal flag indicating registration done by kernel
236 	 */
237 	if (kn->kn_flags & EV_FLAG1) {
238 		kn->kn_data = kn->kn_sdata;		/* ppid */
239 		kn->kn_fflags = NOTE_CHILD;
240 		kn->kn_flags &= ~EV_FLAG1;
241 	}
242 
243 	knote_insert(&p->p_klist, kn);
244 
245 	/*
246 	 * Immediately activate any exit notes if the target process is a
247 	 * zombie.  This is necessary to handle the case where the target
248 	 * process, e.g. a child, dies before the kevent is negistered.
249 	 */
250 	if (immediate && filt_proc(kn, NOTE_EXIT))
251 		KNOTE_ACTIVATE(kn);
252 	lwkt_reltoken(&proc_token);
253 
254 	return (0);
255 }
256 
257 /*
258  * The knote may be attached to a different process, which may exit,
259  * leaving nothing for the knote to be attached to.  So when the process
260  * exits, the knote is marked as DETACHED and also flagged as ONESHOT so
261  * it will be deleted when read out.  However, as part of the knote deletion,
262  * this routine is called, so a check is needed to avoid actually performing
263  * a detach, because the original process does not exist any more.
264  */
265 static void
266 filt_procdetach(struct knote *kn)
267 {
268 	struct proc *p;
269 
270 	if (kn->kn_status & KN_DETACHED)
271 		return;
272 	/* XXX locking? take proc_token here? */
273 	p = kn->kn_ptr.p_proc;
274 	knote_remove(&p->p_klist, kn);
275 }
276 
277 static int
278 filt_proc(struct knote *kn, long hint)
279 {
280 	u_int event;
281 
282 	/*
283 	 * mask off extra data
284 	 */
285 	event = (u_int)hint & NOTE_PCTRLMASK;
286 
287 	/*
288 	 * if the user is interested in this event, record it.
289 	 */
290 	if (kn->kn_sfflags & event)
291 		kn->kn_fflags |= event;
292 
293 	/*
294 	 * Process is gone, so flag the event as finished.  Detach the
295 	 * knote from the process now because the process will be poof,
296 	 * gone later on.
297 	 */
298 	if (event == NOTE_EXIT) {
299 		struct proc *p = kn->kn_ptr.p_proc;
300 		if ((kn->kn_status & KN_DETACHED) == 0) {
301 			knote_remove(&p->p_klist, kn);
302 			kn->kn_status |= KN_DETACHED;
303 			kn->kn_data = p->p_xstat;
304 			kn->kn_ptr.p_proc = NULL;
305 		}
306 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
307 		return (1);
308 	}
309 
310 	/*
311 	 * process forked, and user wants to track the new process,
312 	 * so attach a new knote to it, and immediately report an
313 	 * event with the parent's pid.
314 	 */
315 	if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) {
316 		struct kevent kev;
317 		int error;
318 
319 		/*
320 		 * register knote with new process.
321 		 */
322 		kev.ident = hint & NOTE_PDATAMASK;	/* pid */
323 		kev.filter = kn->kn_filter;
324 		kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1;
325 		kev.fflags = kn->kn_sfflags;
326 		kev.data = kn->kn_id;			/* parent */
327 		kev.udata = kn->kn_kevent.udata;	/* preserve udata */
328 		error = kqueue_register(kn->kn_kq, &kev);
329 		if (error)
330 			kn->kn_fflags |= NOTE_TRACKERR;
331 	}
332 
333 	return (kn->kn_fflags != 0);
334 }
335 
336 /*
337  * The callout interlocks with callout_stop() (or should), so the
338  * knote should still be a valid structure.  However the timeout
339  * can race a deletion so if KN_DELETING is set we just don't touch
340  * the knote.
341  */
342 static void
343 filt_timerexpire(void *knx)
344 {
345 	struct knote *kn = knx;
346 	struct callout *calloutp;
347 	struct timeval tv;
348 	int tticks;
349 
350 	lwkt_gettoken(&kq_token);
351 	if ((kn->kn_status & KN_DELETING) == 0) {
352 		kn->kn_data++;
353 		KNOTE_ACTIVATE(kn);
354 
355 		if ((kn->kn_flags & EV_ONESHOT) == 0) {
356 			tv.tv_sec = kn->kn_sdata / 1000;
357 			tv.tv_usec = (kn->kn_sdata % 1000) * 1000;
358 			tticks = tvtohz_high(&tv);
359 			calloutp = (struct callout *)kn->kn_hook;
360 			callout_reset(calloutp, tticks, filt_timerexpire, kn);
361 		}
362 	}
363 	lwkt_reltoken(&kq_token);
364 }
365 
366 /*
367  * data contains amount of time to sleep, in milliseconds
368  */
369 static int
370 filt_timerattach(struct knote *kn)
371 {
372 	struct callout *calloutp;
373 	struct timeval tv;
374 	int tticks;
375 
376 	if (kq_ncallouts >= kq_calloutmax)
377 		return (ENOMEM);
378 	kq_ncallouts++;
379 
380 	tv.tv_sec = kn->kn_sdata / 1000;
381 	tv.tv_usec = (kn->kn_sdata % 1000) * 1000;
382 	tticks = tvtohz_high(&tv);
383 
384 	kn->kn_flags |= EV_CLEAR;		/* automatically set */
385 	MALLOC(calloutp, struct callout *, sizeof(*calloutp),
386 	    M_KQUEUE, M_WAITOK);
387 	callout_init(calloutp);
388 	kn->kn_hook = (caddr_t)calloutp;
389 	callout_reset(calloutp, tticks, filt_timerexpire, kn);
390 
391 	return (0);
392 }
393 
394 static void
395 filt_timerdetach(struct knote *kn)
396 {
397 	struct callout *calloutp;
398 
399 	calloutp = (struct callout *)kn->kn_hook;
400 	callout_stop(calloutp);
401 	FREE(calloutp, M_KQUEUE);
402 	kq_ncallouts--;
403 }
404 
405 static int
406 filt_timer(struct knote *kn, long hint)
407 {
408 
409 	return (kn->kn_data != 0);
410 }
411 
412 /*
413  * Acquire a knote, return non-zero on success, 0 on failure.
414  *
415  * If we cannot acquire the knote we sleep and return 0.  The knote
416  * may be stale on return in this case and the caller must restart
417  * whatever loop they are in.
418  */
419 static __inline
420 int
421 knote_acquire(struct knote *kn)
422 {
423 	if (kn->kn_status & KN_PROCESSING) {
424 		kn->kn_status |= KN_WAITING | KN_REPROCESS;
425 		tsleep(kn, 0, "kqepts", hz);
426 		/* knote may be stale now */
427 		return(0);
428 	}
429 	kn->kn_status |= KN_PROCESSING;
430 	return(1);
431 }
432 
433 /*
434  * Release an acquired knote, clearing KN_PROCESSING and handling any
435  * KN_REPROCESS events.
436  *
437  * Non-zero is returned if the knote is destroyed.
438  */
439 static __inline
440 int
441 knote_release(struct knote *kn)
442 {
443 	while (kn->kn_status & KN_REPROCESS) {
444 		kn->kn_status &= ~KN_REPROCESS;
445 		if (kn->kn_status & KN_WAITING) {
446 			kn->kn_status &= ~KN_WAITING;
447 			wakeup(kn);
448 		}
449 		if (kn->kn_status & KN_DELETING) {
450 			knote_detach_and_drop(kn);
451 			return(1);
452 			/* NOT REACHED */
453 		}
454 		if (filter_event(kn, 0))
455 			KNOTE_ACTIVATE(kn);
456 	}
457 	kn->kn_status &= ~KN_PROCESSING;
458 	return(0);
459 }
460 
461 /*
462  * Initialize a kqueue.
463  *
464  * NOTE: The lwp/proc code initializes a kqueue for select/poll ops.
465  *
466  * MPSAFE
467  */
468 void
469 kqueue_init(struct kqueue *kq, struct filedesc *fdp)
470 {
471 	TAILQ_INIT(&kq->kq_knpend);
472 	TAILQ_INIT(&kq->kq_knlist);
473 	kq->kq_count = 0;
474 	kq->kq_fdp = fdp;
475 	SLIST_INIT(&kq->kq_kqinfo.ki_note);
476 }
477 
478 /*
479  * Terminate a kqueue.  Freeing the actual kq itself is left up to the
480  * caller (it might be embedded in a lwp so we don't do it here).
481  *
482  * The kq's knlist must be completely eradicated so block on any
483  * processing races.
484  */
485 void
486 kqueue_terminate(struct kqueue *kq)
487 {
488 	struct knote *kn;
489 
490 	lwkt_gettoken(&kq_token);
491 	while ((kn = TAILQ_FIRST(&kq->kq_knlist)) != NULL) {
492 		if (knote_acquire(kn))
493 			knote_detach_and_drop(kn);
494 	}
495 	if (kq->kq_knhash) {
496 		kfree(kq->kq_knhash, M_KQUEUE);
497 		kq->kq_knhash = NULL;
498 		kq->kq_knhashmask = 0;
499 	}
500 	lwkt_reltoken(&kq_token);
501 }
502 
503 /*
504  * MPSAFE
505  */
506 int
507 sys_kqueue(struct kqueue_args *uap)
508 {
509 	struct thread *td = curthread;
510 	struct kqueue *kq;
511 	struct file *fp;
512 	int fd, error;
513 
514 	error = falloc(td->td_lwp, &fp, &fd);
515 	if (error)
516 		return (error);
517 	fp->f_flag = FREAD | FWRITE;
518 	fp->f_type = DTYPE_KQUEUE;
519 	fp->f_ops = &kqueueops;
520 
521 	kq = kmalloc(sizeof(struct kqueue), M_KQUEUE, M_WAITOK | M_ZERO);
522 	kqueue_init(kq, td->td_proc->p_fd);
523 	fp->f_data = kq;
524 
525 	fsetfd(kq->kq_fdp, fp, fd);
526 	uap->sysmsg_result = fd;
527 	fdrop(fp);
528 	return (error);
529 }
530 
531 /*
532  * Copy 'count' items into the destination list pointed to by uap->eventlist.
533  */
534 static int
535 kevent_copyout(void *arg, struct kevent *kevp, int count, int *res)
536 {
537 	struct kevent_copyin_args *kap;
538 	int error;
539 
540 	kap = (struct kevent_copyin_args *)arg;
541 
542 	error = copyout(kevp, kap->ka->eventlist, count * sizeof(*kevp));
543 	if (error == 0) {
544 		kap->ka->eventlist += count;
545 		*res += count;
546 	} else {
547 		*res = -1;
548 	}
549 
550 	return (error);
551 }
552 
553 /*
554  * Copy at most 'max' items from the list pointed to by kap->changelist,
555  * return number of items in 'events'.
556  */
557 static int
558 kevent_copyin(void *arg, struct kevent *kevp, int max, int *events)
559 {
560 	struct kevent_copyin_args *kap;
561 	int error, count;
562 
563 	kap = (struct kevent_copyin_args *)arg;
564 
565 	count = min(kap->ka->nchanges - kap->pchanges, max);
566 	error = copyin(kap->ka->changelist, kevp, count * sizeof *kevp);
567 	if (error == 0) {
568 		kap->ka->changelist += count;
569 		kap->pchanges += count;
570 		*events = count;
571 	}
572 
573 	return (error);
574 }
575 
576 /*
577  * MPSAFE
578  */
579 int
580 kern_kevent(struct kqueue *kq, int nevents, int *res, void *uap,
581 	    k_copyin_fn kevent_copyinfn, k_copyout_fn kevent_copyoutfn,
582 	    struct timespec *tsp_in)
583 {
584 	struct kevent *kevp;
585 	struct timespec *tsp;
586 	int i, n, total, error, nerrors = 0;
587 	int lres;
588 	int limit = kq_checkloop;
589 	struct kevent kev[KQ_NEVENTS];
590 	struct knote marker;
591 
592 	tsp = tsp_in;
593 	*res = 0;
594 
595 	lwkt_gettoken(&kq_token);
596 	for ( ;; ) {
597 		n = 0;
598 		error = kevent_copyinfn(uap, kev, KQ_NEVENTS, &n);
599 		if (error)
600 			goto done;
601 		if (n == 0)
602 			break;
603 		for (i = 0; i < n; i++) {
604 			kevp = &kev[i];
605 			kevp->flags &= ~EV_SYSFLAGS;
606 			error = kqueue_register(kq, kevp);
607 
608 			/*
609 			 * If a registration returns an error we
610 			 * immediately post the error.  The kevent()
611 			 * call itself will fail with the error if
612 			 * no space is available for posting.
613 			 *
614 			 * Such errors normally bypass the timeout/blocking
615 			 * code.  However, if the copyoutfn function refuses
616 			 * to post the error (see sys_poll()), then we
617 			 * ignore it too.
618 			 */
619 			if (error) {
620 				kevp->flags = EV_ERROR;
621 				kevp->data = error;
622 				lres = *res;
623 				kevent_copyoutfn(uap, kevp, 1, res);
624 				if (lres != *res) {
625 					nevents--;
626 					nerrors++;
627 				}
628 			}
629 		}
630 	}
631 	if (nerrors) {
632 		error = 0;
633 		goto done;
634 	}
635 
636 	/*
637 	 * Acquire/wait for events - setup timeout
638 	 */
639 	if (tsp != NULL) {
640 		struct timespec ats;
641 
642 		if (tsp->tv_sec || tsp->tv_nsec) {
643 			nanouptime(&ats);
644 			timespecadd(tsp, &ats);		/* tsp = target time */
645 		}
646 	}
647 
648 	/*
649 	 * Loop as required.
650 	 *
651 	 * Collect as many events as we can. Sleeping on successive
652 	 * loops is disabled if copyoutfn has incremented (*res).
653 	 *
654 	 * The loop stops if an error occurs, all events have been
655 	 * scanned (the marker has been reached), or fewer than the
656 	 * maximum number of events is found.
657 	 *
658 	 * The copyoutfn function does not have to increment (*res) in
659 	 * order for the loop to continue.
660 	 *
661 	 * NOTE: doselect() usually passes 0x7FFFFFFF for nevents.
662 	 */
663 	total = 0;
664 	error = 0;
665 	marker.kn_filter = EVFILT_MARKER;
666 	marker.kn_status = KN_PROCESSING;
667 	TAILQ_INSERT_TAIL(&kq->kq_knpend, &marker, kn_tqe);
668 	while ((n = nevents - total) > 0) {
669 		if (n > KQ_NEVENTS)
670 			n = KQ_NEVENTS;
671 
672 		/*
673 		 * If no events are pending sleep until timeout (if any)
674 		 * or an event occurs.
675 		 *
676 		 * After the sleep completes the marker is moved to the
677 		 * end of the list, making any received events available
678 		 * to our scan.
679 		 */
680 		if (kq->kq_count == 0 && *res == 0) {
681 			error = kqueue_sleep(kq, tsp);
682 			if (error)
683 				break;
684 
685 			TAILQ_REMOVE(&kq->kq_knpend, &marker, kn_tqe);
686 			TAILQ_INSERT_TAIL(&kq->kq_knpend, &marker, kn_tqe);
687 		}
688 
689 		/*
690 		 * Process all received events
691 		 * Account for all non-spurious events in our total
692 		 */
693 		i = kqueue_scan(kq, kev, n, &marker);
694 		if (i) {
695 			lres = *res;
696 			error = kevent_copyoutfn(uap, kev, i, res);
697 			total += *res - lres;
698 			if (error)
699 				break;
700 		}
701 		if (limit && --limit == 0)
702 			panic("kqueue: checkloop failed i=%d", i);
703 
704 		/*
705 		 * Normally when fewer events are returned than requested
706 		 * we can stop.  However, if only spurious events were
707 		 * collected the copyout will not bump (*res) and we have
708 		 * to continue.
709 		 */
710 		if (i < n && *res)
711 			break;
712 
713 		/*
714 		 * Deal with an edge case where spurious events can cause
715 		 * a loop to occur without moving the marker.  This can
716 		 * prevent kqueue_scan() from picking up new events which
717 		 * race us.  We must be sure to move the marker for this
718 		 * case.
719 		 *
720 		 * NOTE: We do not want to move the marker if events
721 		 *	 were scanned because normal kqueue operations
722 		 *	 may reactivate events.  Moving the marker in
723 		 *	 that case could result in duplicates for the
724 		 *	 same event.
725 		 */
726 		if (i == 0) {
727 			TAILQ_REMOVE(&kq->kq_knpend, &marker, kn_tqe);
728 			TAILQ_INSERT_TAIL(&kq->kq_knpend, &marker, kn_tqe);
729 		}
730 	}
731 	TAILQ_REMOVE(&kq->kq_knpend, &marker, kn_tqe);
732 
733 	/* Timeouts do not return EWOULDBLOCK. */
734 	if (error == EWOULDBLOCK)
735 		error = 0;
736 
737 done:
738 	lwkt_reltoken(&kq_token);
739 	return (error);
740 }
741 
742 /*
743  * MPALMOSTSAFE
744  */
745 int
746 sys_kevent(struct kevent_args *uap)
747 {
748 	struct thread *td = curthread;
749 	struct proc *p = td->td_proc;
750 	struct timespec ts, *tsp;
751 	struct kqueue *kq;
752 	struct file *fp = NULL;
753 	struct kevent_copyin_args *kap, ka;
754 	int error;
755 
756 	if (uap->timeout) {
757 		error = copyin(uap->timeout, &ts, sizeof(ts));
758 		if (error)
759 			return (error);
760 		tsp = &ts;
761 	} else {
762 		tsp = NULL;
763 	}
764 
765 	fp = holdfp(p->p_fd, uap->fd, -1);
766 	if (fp == NULL)
767 		return (EBADF);
768 	if (fp->f_type != DTYPE_KQUEUE) {
769 		fdrop(fp);
770 		return (EBADF);
771 	}
772 
773 	kq = (struct kqueue *)fp->f_data;
774 
775 	kap = &ka;
776 	kap->ka = uap;
777 	kap->pchanges = 0;
778 
779 	error = kern_kevent(kq, uap->nevents, &uap->sysmsg_result, kap,
780 			    kevent_copyin, kevent_copyout, tsp);
781 
782 	fdrop(fp);
783 
784 	return (error);
785 }
786 
787 int
788 kqueue_register(struct kqueue *kq, struct kevent *kev)
789 {
790 	struct filedesc *fdp = kq->kq_fdp;
791 	struct filterops *fops;
792 	struct file *fp = NULL;
793 	struct knote *kn = NULL;
794 	int error = 0;
795 
796 	if (kev->filter < 0) {
797 		if (kev->filter + EVFILT_SYSCOUNT < 0)
798 			return (EINVAL);
799 		fops = sysfilt_ops[~kev->filter];	/* to 0-base index */
800 	} else {
801 		/*
802 		 * XXX
803 		 * filter attach routine is responsible for insuring that
804 		 * the identifier can be attached to it.
805 		 */
806 		kprintf("unknown filter: %d\n", kev->filter);
807 		return (EINVAL);
808 	}
809 
810 	lwkt_gettoken(&kq_token);
811 	if (fops->f_flags & FILTEROP_ISFD) {
812 		/* validate descriptor */
813 		fp = holdfp(fdp, kev->ident, -1);
814 		if (fp == NULL) {
815 			lwkt_reltoken(&kq_token);
816 			return (EBADF);
817 		}
818 
819 again1:
820 		SLIST_FOREACH(kn, &fp->f_klist, kn_link) {
821 			if (kn->kn_kq == kq &&
822 			    kn->kn_filter == kev->filter &&
823 			    kn->kn_id == kev->ident) {
824 				if (knote_acquire(kn) == 0)
825 					goto again1;
826 				break;
827 			}
828 		}
829 	} else {
830 		if (kq->kq_knhashmask) {
831 			struct klist *list;
832 
833 			list = &kq->kq_knhash[
834 			    KN_HASH((u_long)kev->ident, kq->kq_knhashmask)];
835 again2:
836 			SLIST_FOREACH(kn, list, kn_link) {
837 				if (kn->kn_id == kev->ident &&
838 				    kn->kn_filter == kev->filter) {
839 					if (knote_acquire(kn) == 0)
840 						goto again2;
841 					break;
842 				}
843 			}
844 		}
845 	}
846 
847 	/*
848 	 * NOTE: At this point if kn is non-NULL we will have acquired
849 	 *	 it and set KN_PROCESSING.
850 	 */
851 	if (kn == NULL && ((kev->flags & EV_ADD) == 0)) {
852 		error = ENOENT;
853 		goto done;
854 	}
855 
856 	/*
857 	 * kn now contains the matching knote, or NULL if no match
858 	 */
859 	if (kev->flags & EV_ADD) {
860 		if (kn == NULL) {
861 			kn = knote_alloc();
862 			if (kn == NULL) {
863 				error = ENOMEM;
864 				goto done;
865 			}
866 			kn->kn_fp = fp;
867 			kn->kn_kq = kq;
868 			kn->kn_fop = fops;
869 
870 			/*
871 			 * apply reference count to knote structure, and
872 			 * do not release it at the end of this routine.
873 			 */
874 			fp = NULL;
875 
876 			kn->kn_sfflags = kev->fflags;
877 			kn->kn_sdata = kev->data;
878 			kev->fflags = 0;
879 			kev->data = 0;
880 			kn->kn_kevent = *kev;
881 
882 			/*
883 			 * KN_PROCESSING prevents the knote from getting
884 			 * ripped out from under us while we are trying
885 			 * to attach it, in case the attach blocks.
886 			 */
887 			kn->kn_status = KN_PROCESSING;
888 			knote_attach(kn);
889 			if ((error = filter_attach(kn)) != 0) {
890 				kn->kn_status |= KN_DELETING | KN_REPROCESS;
891 				knote_drop(kn);
892 				goto done;
893 			}
894 
895 			/*
896 			 * Interlock against close races which either tried
897 			 * to remove our knote while we were blocked or missed
898 			 * it entirely prior to our attachment.  We do not
899 			 * want to end up with a knote on a closed descriptor.
900 			 */
901 			if ((fops->f_flags & FILTEROP_ISFD) &&
902 			    checkfdclosed(fdp, kev->ident, kn->kn_fp)) {
903 				kn->kn_status |= KN_DELETING | KN_REPROCESS;
904 			}
905 		} else {
906 			/*
907 			 * The user may change some filter values after the
908 			 * initial EV_ADD, but doing so will not reset any
909 			 * filter which have already been triggered.
910 			 */
911 			KKASSERT(kn->kn_status & KN_PROCESSING);
912 			kn->kn_sfflags = kev->fflags;
913 			kn->kn_sdata = kev->data;
914 			kn->kn_kevent.udata = kev->udata;
915 		}
916 
917 		/*
918 		 * Execute the filter event to immediately activate the
919 		 * knote if necessary.  If reprocessing events are pending
920 		 * due to blocking above we do not run the filter here
921 		 * but instead let knote_release() do it.  Otherwise we
922 		 * might run the filter on a deleted event.
923 		 */
924 		if ((kn->kn_status & KN_REPROCESS) == 0) {
925 			if (filter_event(kn, 0))
926 				KNOTE_ACTIVATE(kn);
927 		}
928 	} else if (kev->flags & EV_DELETE) {
929 		/*
930 		 * Delete the existing knote
931 		 */
932 		knote_detach_and_drop(kn);
933 		goto done;
934 	}
935 
936 	/*
937 	 * Disablement does not deactivate a knote here.
938 	 */
939 	if ((kev->flags & EV_DISABLE) &&
940 	    ((kn->kn_status & KN_DISABLED) == 0)) {
941 		kn->kn_status |= KN_DISABLED;
942 	}
943 
944 	/*
945 	 * Re-enablement may have to immediately enqueue an active knote.
946 	 */
947 	if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) {
948 		kn->kn_status &= ~KN_DISABLED;
949 		if ((kn->kn_status & KN_ACTIVE) &&
950 		    ((kn->kn_status & KN_QUEUED) == 0)) {
951 			knote_enqueue(kn);
952 		}
953 	}
954 
955 	/*
956 	 * Handle any required reprocessing
957 	 */
958 	knote_release(kn);
959 	/* kn may be invalid now */
960 
961 done:
962 	lwkt_reltoken(&kq_token);
963 	if (fp != NULL)
964 		fdrop(fp);
965 	return (error);
966 }
967 
968 /*
969  * Block as necessary until the target time is reached.
970  * If tsp is NULL we block indefinitely.  If tsp->ts_secs/nsecs are both
971  * 0 we do not block at all.
972  */
973 static int
974 kqueue_sleep(struct kqueue *kq, struct timespec *tsp)
975 {
976 	int error = 0;
977 
978 	if (tsp == NULL) {
979 		kq->kq_state |= KQ_SLEEP;
980 		error = tsleep(kq, PCATCH, "kqread", 0);
981 	} else if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) {
982 		error = EWOULDBLOCK;
983 	} else {
984 		struct timespec ats;
985 		struct timespec atx = *tsp;
986 		int timeout;
987 
988 		nanouptime(&ats);
989 		timespecsub(&atx, &ats);
990 		if (ats.tv_sec < 0) {
991 			error = EWOULDBLOCK;
992 		} else {
993 			timeout = atx.tv_sec > 24 * 60 * 60 ?
994 				24 * 60 * 60 * hz : tstohz_high(&atx);
995 			kq->kq_state |= KQ_SLEEP;
996 			error = tsleep(kq, PCATCH, "kqread", timeout);
997 		}
998 	}
999 
1000 	/* don't restart after signals... */
1001 	if (error == ERESTART)
1002 		return (EINTR);
1003 
1004 	return (error);
1005 }
1006 
1007 /*
1008  * Scan the kqueue, return the number of active events placed in kevp up
1009  * to count.
1010  *
1011  * Continuous mode events may get recycled, do not continue scanning past
1012  * marker unless no events have been collected.
1013  */
1014 static int
1015 kqueue_scan(struct kqueue *kq, struct kevent *kevp, int count,
1016             struct knote *marker)
1017 {
1018         struct knote *kn, local_marker;
1019         int total;
1020 
1021         total = 0;
1022 	local_marker.kn_filter = EVFILT_MARKER;
1023 	local_marker.kn_status = KN_PROCESSING;
1024 
1025 	/*
1026 	 * Collect events.
1027 	 */
1028 	TAILQ_INSERT_HEAD(&kq->kq_knpend, &local_marker, kn_tqe);
1029 	while (count) {
1030 		kn = TAILQ_NEXT(&local_marker, kn_tqe);
1031 		if (kn->kn_filter == EVFILT_MARKER) {
1032 			/* Marker reached, we are done */
1033 			if (kn == marker)
1034 				break;
1035 
1036 			/* Move local marker past some other threads marker */
1037 			kn = TAILQ_NEXT(kn, kn_tqe);
1038 			TAILQ_REMOVE(&kq->kq_knpend, &local_marker, kn_tqe);
1039 			TAILQ_INSERT_BEFORE(kn, &local_marker, kn_tqe);
1040 			continue;
1041 		}
1042 
1043 		/*
1044 		 * We can't skip a knote undergoing processing, otherwise
1045 		 * we risk not returning it when the user process expects
1046 		 * it should be returned.  Sleep and retry.
1047 		 */
1048 		if (knote_acquire(kn) == 0)
1049 			continue;
1050 
1051 		/*
1052 		 * Remove the event for processing.
1053 		 *
1054 		 * WARNING!  We must leave KN_QUEUED set to prevent the
1055 		 *	     event from being KNOTE_ACTIVATE()d while
1056 		 *	     the queue state is in limbo, in case we
1057 		 *	     block.
1058 		 *
1059 		 * WARNING!  We must set KN_PROCESSING to avoid races
1060 		 *	     against deletion or another thread's
1061 		 *	     processing.
1062 		 */
1063 		TAILQ_REMOVE(&kq->kq_knpend, kn, kn_tqe);
1064 		kq->kq_count--;
1065 
1066 		/*
1067 		 * We have to deal with an extremely important race against
1068 		 * file descriptor close()s here.  The file descriptor can
1069 		 * disappear MPSAFE, and there is a small window of
1070 		 * opportunity between that and the call to knote_fdclose().
1071 		 *
1072 		 * If we hit that window here while doselect or dopoll is
1073 		 * trying to delete a spurious event they will not be able
1074 		 * to match up the event against a knote and will go haywire.
1075 		 */
1076 		if ((kn->kn_fop->f_flags & FILTEROP_ISFD) &&
1077 		    checkfdclosed(kq->kq_fdp, kn->kn_kevent.ident, kn->kn_fp)) {
1078 			kn->kn_status |= KN_DELETING | KN_REPROCESS;
1079 		}
1080 
1081 		if (kn->kn_status & KN_DISABLED) {
1082 			/*
1083 			 * If disabled we ensure the event is not queued
1084 			 * but leave its active bit set.  On re-enablement
1085 			 * the event may be immediately triggered.
1086 			 */
1087 			kn->kn_status &= ~KN_QUEUED;
1088 		} else if ((kn->kn_flags & EV_ONESHOT) == 0 &&
1089 			   (kn->kn_status & KN_DELETING) == 0 &&
1090 			   filter_event(kn, 0) == 0) {
1091 			/*
1092 			 * If not running in one-shot mode and the event
1093 			 * is no longer present we ensure it is removed
1094 			 * from the queue and ignore it.
1095 			 */
1096 			kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1097 		} else {
1098 			/*
1099 			 * Post the event
1100 			 */
1101 			*kevp++ = kn->kn_kevent;
1102 			++total;
1103 			--count;
1104 
1105 			if (kn->kn_flags & EV_ONESHOT) {
1106 				kn->kn_status &= ~KN_QUEUED;
1107 				kn->kn_status |= KN_DELETING | KN_REPROCESS;
1108 			} else if (kn->kn_flags & EV_CLEAR) {
1109 				kn->kn_data = 0;
1110 				kn->kn_fflags = 0;
1111 				kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE);
1112 			} else {
1113 				TAILQ_INSERT_TAIL(&kq->kq_knpend, kn, kn_tqe);
1114 				kq->kq_count++;
1115 			}
1116 		}
1117 
1118 		/*
1119 		 * Handle any post-processing states
1120 		 */
1121 		knote_release(kn);
1122 	}
1123 	TAILQ_REMOVE(&kq->kq_knpend, &local_marker, kn_tqe);
1124 
1125 	return (total);
1126 }
1127 
1128 /*
1129  * XXX
1130  * This could be expanded to call kqueue_scan, if desired.
1131  *
1132  * MPSAFE
1133  */
1134 static int
1135 kqueue_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
1136 {
1137 	return (ENXIO);
1138 }
1139 
1140 /*
1141  * MPSAFE
1142  */
1143 static int
1144 kqueue_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
1145 {
1146 	return (ENXIO);
1147 }
1148 
1149 /*
1150  * MPALMOSTSAFE
1151  */
1152 static int
1153 kqueue_ioctl(struct file *fp, u_long com, caddr_t data,
1154 	     struct ucred *cred, struct sysmsg *msg)
1155 {
1156 	struct kqueue *kq;
1157 	int error;
1158 
1159 	lwkt_gettoken(&kq_token);
1160 	kq = (struct kqueue *)fp->f_data;
1161 
1162 	switch(com) {
1163 	case FIOASYNC:
1164 		if (*(int *)data)
1165 			kq->kq_state |= KQ_ASYNC;
1166 		else
1167 			kq->kq_state &= ~KQ_ASYNC;
1168 		error = 0;
1169 		break;
1170 	case FIOSETOWN:
1171 		error = fsetown(*(int *)data, &kq->kq_sigio);
1172 		break;
1173 	default:
1174 		error = ENOTTY;
1175 		break;
1176 	}
1177 	lwkt_reltoken(&kq_token);
1178 	return (error);
1179 }
1180 
1181 /*
1182  * MPSAFE
1183  */
1184 static int
1185 kqueue_stat(struct file *fp, struct stat *st, struct ucred *cred)
1186 {
1187 	struct kqueue *kq = (struct kqueue *)fp->f_data;
1188 
1189 	bzero((void *)st, sizeof(*st));
1190 	st->st_size = kq->kq_count;
1191 	st->st_blksize = sizeof(struct kevent);
1192 	st->st_mode = S_IFIFO;
1193 	return (0);
1194 }
1195 
1196 /*
1197  * MPSAFE
1198  */
1199 static int
1200 kqueue_close(struct file *fp)
1201 {
1202 	struct kqueue *kq = (struct kqueue *)fp->f_data;
1203 
1204 	kqueue_terminate(kq);
1205 
1206 	fp->f_data = NULL;
1207 	funsetown(kq->kq_sigio);
1208 
1209 	kfree(kq, M_KQUEUE);
1210 	return (0);
1211 }
1212 
1213 static void
1214 kqueue_wakeup(struct kqueue *kq)
1215 {
1216 	if (kq->kq_state & KQ_SLEEP) {
1217 		kq->kq_state &= ~KQ_SLEEP;
1218 		wakeup(kq);
1219 	}
1220 	KNOTE(&kq->kq_kqinfo.ki_note, 0);
1221 }
1222 
1223 /*
1224  * Calls filterops f_attach function, acquiring mplock if filter is not
1225  * marked as FILTEROP_MPSAFE.
1226  */
1227 static int
1228 filter_attach(struct knote *kn)
1229 {
1230 	int ret;
1231 
1232 	if (!(kn->kn_fop->f_flags & FILTEROP_MPSAFE)) {
1233 		get_mplock();
1234 		ret = kn->kn_fop->f_attach(kn);
1235 		rel_mplock();
1236 	} else {
1237 		ret = kn->kn_fop->f_attach(kn);
1238 	}
1239 
1240 	return (ret);
1241 }
1242 
1243 /*
1244  * Detach the knote and drop it, destroying the knote.
1245  *
1246  * Calls filterops f_detach function, acquiring mplock if filter is not
1247  * marked as FILTEROP_MPSAFE.
1248  */
1249 static void
1250 knote_detach_and_drop(struct knote *kn)
1251 {
1252 	kn->kn_status |= KN_DELETING | KN_REPROCESS;
1253 	if (kn->kn_fop->f_flags & FILTEROP_MPSAFE) {
1254 		kn->kn_fop->f_detach(kn);
1255 	} else {
1256 		get_mplock();
1257 		kn->kn_fop->f_detach(kn);
1258 		rel_mplock();
1259 	}
1260 	knote_drop(kn);
1261 }
1262 
1263 /*
1264  * Calls filterops f_event function, acquiring mplock if filter is not
1265  * marked as FILTEROP_MPSAFE.
1266  *
1267  * If the knote is in the middle of being created or deleted we cannot
1268  * safely call the filter op.
1269  */
1270 static int
1271 filter_event(struct knote *kn, long hint)
1272 {
1273 	int ret;
1274 
1275 	if (kn->kn_fop->f_flags & FILTEROP_MPSAFE) {
1276 		ret = kn->kn_fop->f_event(kn, hint);
1277 	} else {
1278 		get_mplock();
1279 		ret = kn->kn_fop->f_event(kn, hint);
1280 		rel_mplock();
1281 	}
1282 	return (ret);
1283 }
1284 
1285 /*
1286  * Walk down a list of knotes, activating them if their event has triggered.
1287  *
1288  * If we encounter any knotes which are undergoing processing we just mark
1289  * them for reprocessing and do not try to [re]activate the knote.  However,
1290  * if a hint is being passed we have to wait and that makes things a bit
1291  * sticky.
1292  */
1293 void
1294 knote(struct klist *list, long hint)
1295 {
1296 	struct knote *kn;
1297 
1298 	lwkt_gettoken(&kq_token);
1299 restart:
1300 	SLIST_FOREACH(kn, list, kn_next) {
1301 		if (kn->kn_status & KN_PROCESSING) {
1302 			/*
1303 			 * Someone else is processing the knote, ask the
1304 			 * other thread to reprocess it and don't mess
1305 			 * with it otherwise.
1306 			 */
1307 			if (hint == 0) {
1308 				kn->kn_status |= KN_REPROCESS;
1309 				continue;
1310 			}
1311 
1312 			/*
1313 			 * If the hint is non-zero we have to wait or risk
1314 			 * losing the state the caller is trying to update.
1315 			 *
1316 			 * XXX This is a real problem, certain process
1317 			 *     and signal filters will bump kn_data for
1318 			 *     already-processed notes more than once if
1319 			 *     we restart the list scan.  FIXME.
1320 			 */
1321 			kn->kn_status |= KN_WAITING | KN_REPROCESS;
1322 			tsleep(kn, 0, "knotec", hz);
1323 			goto restart;
1324 		}
1325 
1326 		/*
1327 		 * Become the reprocessing master ourselves.
1328 		 *
1329 		 * If hint is non-zer running the event is mandatory
1330 		 * when not deleting so do it whether reprocessing is
1331 		 * set or not.
1332 		 */
1333 		kn->kn_status |= KN_PROCESSING;
1334 		if ((kn->kn_status & KN_DELETING) == 0) {
1335 			if (filter_event(kn, hint))
1336 				KNOTE_ACTIVATE(kn);
1337 		}
1338 		if (knote_release(kn))
1339 			goto restart;
1340 	}
1341 	lwkt_reltoken(&kq_token);
1342 }
1343 
1344 /*
1345  * Insert knote at head of klist.
1346  *
1347  * This function may only be called via a filter function and thus
1348  * kq_token should already be held and marked for processing.
1349  */
1350 void
1351 knote_insert(struct klist *klist, struct knote *kn)
1352 {
1353 	KKASSERT(kn->kn_status & KN_PROCESSING);
1354 	ASSERT_LWKT_TOKEN_HELD(&kq_token);
1355 	SLIST_INSERT_HEAD(klist, kn, kn_next);
1356 }
1357 
1358 /*
1359  * Remove knote from a klist
1360  *
1361  * This function may only be called via a filter function and thus
1362  * kq_token should already be held and marked for processing.
1363  */
1364 void
1365 knote_remove(struct klist *klist, struct knote *kn)
1366 {
1367 	KKASSERT(kn->kn_status & KN_PROCESSING);
1368 	ASSERT_LWKT_TOKEN_HELD(&kq_token);
1369 	SLIST_REMOVE(klist, kn, knote, kn_next);
1370 }
1371 
1372 /*
1373  * Remove all knotes from a specified klist
1374  *
1375  * Only called from aio.
1376  */
1377 void
1378 knote_empty(struct klist *list)
1379 {
1380 	struct knote *kn;
1381 
1382 	lwkt_gettoken(&kq_token);
1383 	while ((kn = SLIST_FIRST(list)) != NULL) {
1384 		if (knote_acquire(kn))
1385 			knote_detach_and_drop(kn);
1386 	}
1387 	lwkt_reltoken(&kq_token);
1388 }
1389 
1390 void
1391 knote_assume_knotes(struct kqinfo *src, struct kqinfo *dst,
1392 		    struct filterops *ops, void *hook)
1393 {
1394 	struct knote *kn;
1395 
1396 	lwkt_gettoken(&kq_token);
1397 	while ((kn = SLIST_FIRST(&src->ki_note)) != NULL) {
1398 		if (knote_acquire(kn)) {
1399 			knote_remove(&src->ki_note, kn);
1400 			kn->kn_fop = ops;
1401 			kn->kn_hook = hook;
1402 			knote_insert(&dst->ki_note, kn);
1403 			knote_release(kn);
1404 			/* kn may be invalid now */
1405 		}
1406 	}
1407 	lwkt_reltoken(&kq_token);
1408 }
1409 
1410 /*
1411  * Remove all knotes referencing a specified fd
1412  */
1413 void
1414 knote_fdclose(struct file *fp, struct filedesc *fdp, int fd)
1415 {
1416 	struct knote *kn;
1417 
1418 	lwkt_gettoken(&kq_token);
1419 restart:
1420 	SLIST_FOREACH(kn, &fp->f_klist, kn_link) {
1421 		if (kn->kn_kq->kq_fdp == fdp && kn->kn_id == fd) {
1422 			if (knote_acquire(kn))
1423 				knote_detach_and_drop(kn);
1424 			goto restart;
1425 		}
1426 	}
1427 	lwkt_reltoken(&kq_token);
1428 }
1429 
1430 /*
1431  * Low level attach function.
1432  *
1433  * The knote should already be marked for processing.
1434  */
1435 static void
1436 knote_attach(struct knote *kn)
1437 {
1438 	struct klist *list;
1439 	struct kqueue *kq = kn->kn_kq;
1440 
1441 	if (kn->kn_fop->f_flags & FILTEROP_ISFD) {
1442 		KKASSERT(kn->kn_fp);
1443 		list = &kn->kn_fp->f_klist;
1444 	} else {
1445 		if (kq->kq_knhashmask == 0)
1446 			kq->kq_knhash = hashinit(KN_HASHSIZE, M_KQUEUE,
1447 						 &kq->kq_knhashmask);
1448 		list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1449 	}
1450 	SLIST_INSERT_HEAD(list, kn, kn_link);
1451 	TAILQ_INSERT_HEAD(&kq->kq_knlist, kn, kn_kqlink);
1452 }
1453 
1454 /*
1455  * Low level drop function.
1456  *
1457  * The knote should already be marked for processing.
1458  */
1459 static void
1460 knote_drop(struct knote *kn)
1461 {
1462 	struct kqueue *kq;
1463 	struct klist *list;
1464 
1465 	kq = kn->kn_kq;
1466 
1467 	if (kn->kn_fop->f_flags & FILTEROP_ISFD)
1468 		list = &kn->kn_fp->f_klist;
1469 	else
1470 		list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)];
1471 
1472 	SLIST_REMOVE(list, kn, knote, kn_link);
1473 	TAILQ_REMOVE(&kq->kq_knlist, kn, kn_kqlink);
1474 	if (kn->kn_status & KN_QUEUED)
1475 		knote_dequeue(kn);
1476 	if (kn->kn_fop->f_flags & FILTEROP_ISFD) {
1477 		fdrop(kn->kn_fp);
1478 		kn->kn_fp = NULL;
1479 	}
1480 	knote_free(kn);
1481 }
1482 
1483 /*
1484  * Low level enqueue function.
1485  *
1486  * The knote should already be marked for processing.
1487  */
1488 static void
1489 knote_enqueue(struct knote *kn)
1490 {
1491 	struct kqueue *kq = kn->kn_kq;
1492 
1493 	KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued"));
1494 	TAILQ_INSERT_TAIL(&kq->kq_knpend, kn, kn_tqe);
1495 	kn->kn_status |= KN_QUEUED;
1496 	++kq->kq_count;
1497 
1498 	/*
1499 	 * Send SIGIO on request (typically set up as a mailbox signal)
1500 	 */
1501 	if (kq->kq_sigio && (kq->kq_state & KQ_ASYNC) && kq->kq_count == 1)
1502 		pgsigio(kq->kq_sigio, SIGIO, 0);
1503 
1504 	kqueue_wakeup(kq);
1505 }
1506 
1507 /*
1508  * Low level dequeue function.
1509  *
1510  * The knote should already be marked for processing.
1511  */
1512 static void
1513 knote_dequeue(struct knote *kn)
1514 {
1515 	struct kqueue *kq = kn->kn_kq;
1516 
1517 	KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued"));
1518 	TAILQ_REMOVE(&kq->kq_knpend, kn, kn_tqe);
1519 	kn->kn_status &= ~KN_QUEUED;
1520 	kq->kq_count--;
1521 }
1522 
1523 static void
1524 knote_init(void)
1525 {
1526 	knote_zone = zinit("KNOTE", sizeof(struct knote), 0, 0, 1);
1527 }
1528 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL)
1529 
1530 static struct knote *
1531 knote_alloc(void)
1532 {
1533 	return ((struct knote *)zalloc(knote_zone));
1534 }
1535 
1536 static void
1537 knote_free(struct knote *kn)
1538 {
1539 	zfree(knote_zone, kn);
1540 }
1541