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