1 /*- 2 * Copyright (c) 1999,2000,2001 Jonathan Lemon <jlemon@FreeBSD.org> 3 * Copyright 2004 John-Mark Gurney <jmg@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include "opt_ktrace.h" 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/lock.h> 37 #include <sys/mutex.h> 38 #include <sys/proc.h> 39 #include <sys/malloc.h> 40 #include <sys/unistd.h> 41 #include <sys/file.h> 42 #include <sys/filedesc.h> 43 #include <sys/filio.h> 44 #include <sys/fcntl.h> 45 #include <sys/kthread.h> 46 #include <sys/selinfo.h> 47 #include <sys/queue.h> 48 #include <sys/event.h> 49 #include <sys/eventvar.h> 50 #include <sys/poll.h> 51 #include <sys/protosw.h> 52 #include <sys/sigio.h> 53 #include <sys/signalvar.h> 54 #include <sys/socket.h> 55 #include <sys/socketvar.h> 56 #include <sys/stat.h> 57 #include <sys/sysctl.h> 58 #include <sys/sysproto.h> 59 #include <sys/syscallsubr.h> 60 #include <sys/taskqueue.h> 61 #include <sys/uio.h> 62 #ifdef KTRACE 63 #include <sys/ktrace.h> 64 #endif 65 66 #include <vm/uma.h> 67 68 static MALLOC_DEFINE(M_KQUEUE, "kqueue", "memory for kqueue system"); 69 70 /* 71 * This lock is used if multiple kq locks are required. This possibly 72 * should be made into a per proc lock. 73 */ 74 static struct mtx kq_global; 75 MTX_SYSINIT(kq_global, &kq_global, "kqueue order", MTX_DEF); 76 #define KQ_GLOBAL_LOCK(lck, haslck) do { \ 77 if (!haslck) \ 78 mtx_lock(lck); \ 79 haslck = 1; \ 80 } while (0) 81 #define KQ_GLOBAL_UNLOCK(lck, haslck) do { \ 82 if (haslck) \ 83 mtx_unlock(lck); \ 84 haslck = 0; \ 85 } while (0) 86 87 TASKQUEUE_DEFINE_THREAD(kqueue); 88 89 static int kevent_copyout(void *arg, struct kevent *kevp, int count); 90 static int kevent_copyin(void *arg, struct kevent *kevp, int count); 91 static int kqueue_register(struct kqueue *kq, struct kevent *kev, 92 struct thread *td, int waitok); 93 static int kqueue_acquire(struct file *fp, struct kqueue **kqp); 94 static void kqueue_release(struct kqueue *kq, int locked); 95 static int kqueue_expand(struct kqueue *kq, struct filterops *fops, 96 uintptr_t ident, int waitok); 97 static void kqueue_task(void *arg, int pending); 98 static int kqueue_scan(struct kqueue *kq, int maxevents, 99 struct kevent_copyops *k_ops, 100 const struct timespec *timeout, 101 struct kevent *keva, struct thread *td); 102 static void kqueue_wakeup(struct kqueue *kq); 103 static struct filterops *kqueue_fo_find(int filt); 104 static void kqueue_fo_release(int filt); 105 106 static fo_rdwr_t kqueue_read; 107 static fo_rdwr_t kqueue_write; 108 static fo_truncate_t kqueue_truncate; 109 static fo_ioctl_t kqueue_ioctl; 110 static fo_poll_t kqueue_poll; 111 static fo_kqfilter_t kqueue_kqfilter; 112 static fo_stat_t kqueue_stat; 113 static fo_close_t kqueue_close; 114 115 static struct fileops kqueueops = { 116 .fo_read = kqueue_read, 117 .fo_write = kqueue_write, 118 .fo_truncate = kqueue_truncate, 119 .fo_ioctl = kqueue_ioctl, 120 .fo_poll = kqueue_poll, 121 .fo_kqfilter = kqueue_kqfilter, 122 .fo_stat = kqueue_stat, 123 .fo_close = kqueue_close, 124 }; 125 126 static int knote_attach(struct knote *kn, struct kqueue *kq); 127 static void knote_drop(struct knote *kn, struct thread *td); 128 static void knote_enqueue(struct knote *kn); 129 static void knote_dequeue(struct knote *kn); 130 static void knote_init(void); 131 static struct knote *knote_alloc(int waitok); 132 static void knote_free(struct knote *kn); 133 134 static void filt_kqdetach(struct knote *kn); 135 static int filt_kqueue(struct knote *kn, long hint); 136 static int filt_procattach(struct knote *kn); 137 static void filt_procdetach(struct knote *kn); 138 static int filt_proc(struct knote *kn, long hint); 139 static int filt_fileattach(struct knote *kn); 140 static void filt_timerexpire(void *knx); 141 static int filt_timerattach(struct knote *kn); 142 static void filt_timerdetach(struct knote *kn); 143 static int filt_timer(struct knote *kn, long hint); 144 145 static struct filterops file_filtops = 146 { 1, filt_fileattach, NULL, NULL }; 147 static struct filterops kqread_filtops = 148 { 1, NULL, filt_kqdetach, filt_kqueue }; 149 /* XXX - move to kern_proc.c? */ 150 static struct filterops proc_filtops = 151 { 0, filt_procattach, filt_procdetach, filt_proc }; 152 static struct filterops timer_filtops = 153 { 0, filt_timerattach, filt_timerdetach, filt_timer }; 154 155 static uma_zone_t knote_zone; 156 static int kq_ncallouts = 0; 157 static int kq_calloutmax = (4 * 1024); 158 SYSCTL_INT(_kern, OID_AUTO, kq_calloutmax, CTLFLAG_RW, 159 &kq_calloutmax, 0, "Maximum number of callouts allocated for kqueue"); 160 161 /* XXX - ensure not KN_INFLUX?? */ 162 #define KNOTE_ACTIVATE(kn, islock) do { \ 163 if ((islock)) \ 164 mtx_assert(&(kn)->kn_kq->kq_lock, MA_OWNED); \ 165 else \ 166 KQ_LOCK((kn)->kn_kq); \ 167 (kn)->kn_status |= KN_ACTIVE; \ 168 if (((kn)->kn_status & (KN_QUEUED | KN_DISABLED)) == 0) \ 169 knote_enqueue((kn)); \ 170 if (!(islock)) \ 171 KQ_UNLOCK((kn)->kn_kq); \ 172 } while(0) 173 #define KQ_LOCK(kq) do { \ 174 mtx_lock(&(kq)->kq_lock); \ 175 } while (0) 176 #define KQ_FLUX_WAKEUP(kq) do { \ 177 if (((kq)->kq_state & KQ_FLUXWAIT) == KQ_FLUXWAIT) { \ 178 (kq)->kq_state &= ~KQ_FLUXWAIT; \ 179 wakeup((kq)); \ 180 } \ 181 } while (0) 182 #define KQ_UNLOCK_FLUX(kq) do { \ 183 KQ_FLUX_WAKEUP(kq); \ 184 mtx_unlock(&(kq)->kq_lock); \ 185 } while (0) 186 #define KQ_UNLOCK(kq) do { \ 187 mtx_unlock(&(kq)->kq_lock); \ 188 } while (0) 189 #define KQ_OWNED(kq) do { \ 190 mtx_assert(&(kq)->kq_lock, MA_OWNED); \ 191 } while (0) 192 #define KQ_NOTOWNED(kq) do { \ 193 mtx_assert(&(kq)->kq_lock, MA_NOTOWNED); \ 194 } while (0) 195 #define KN_LIST_LOCK(kn) do { \ 196 if (kn->kn_knlist != NULL) \ 197 kn->kn_knlist->kl_lock(kn->kn_knlist->kl_lockarg); \ 198 } while (0) 199 #define KN_LIST_UNLOCK(kn) do { \ 200 if (kn->kn_knlist != NULL) \ 201 kn->kn_knlist->kl_unlock(kn->kn_knlist->kl_lockarg); \ 202 } while (0) 203 #define KNL_ASSERT_LOCK(knl, islocked) do { \ 204 if (islocked) \ 205 KNL_ASSERT_LOCKED(knl); \ 206 else \ 207 KNL_ASSERT_UNLOCKED(knl); \ 208 } while (0) 209 #ifdef INVARIANTS 210 #define KNL_ASSERT_LOCKED(knl) do { \ 211 if (!knl->kl_locked((knl)->kl_lockarg)) \ 212 panic("knlist not locked, but should be"); \ 213 } while (0) 214 #define KNL_ASSERT_UNLOCKED(knl) do { \ 215 if (knl->kl_locked((knl)->kl_lockarg)) \ 216 panic("knlist locked, but should not be"); \ 217 } while (0) 218 #else /* !INVARIANTS */ 219 #define KNL_ASSERT_LOCKED(knl) do {} while(0) 220 #define KNL_ASSERT_UNLOCKED(knl) do {} while (0) 221 #endif /* INVARIANTS */ 222 223 #define KN_HASHSIZE 64 /* XXX should be tunable */ 224 #define KN_HASH(val, mask) (((val) ^ (val >> 8)) & (mask)) 225 226 static int 227 filt_nullattach(struct knote *kn) 228 { 229 230 return (ENXIO); 231 }; 232 233 struct filterops null_filtops = 234 { 0, filt_nullattach, NULL, NULL }; 235 236 /* XXX - make SYSINIT to add these, and move into respective modules. */ 237 extern struct filterops sig_filtops; 238 extern struct filterops fs_filtops; 239 240 /* 241 * Table for for all system-defined filters. 242 */ 243 static struct mtx filterops_lock; 244 MTX_SYSINIT(kqueue_filterops, &filterops_lock, "protect sysfilt_ops", 245 MTX_DEF); 246 static struct { 247 struct filterops *for_fop; 248 int for_refcnt; 249 } sysfilt_ops[EVFILT_SYSCOUNT] = { 250 { &file_filtops }, /* EVFILT_READ */ 251 { &file_filtops }, /* EVFILT_WRITE */ 252 { &null_filtops }, /* EVFILT_AIO */ 253 { &file_filtops }, /* EVFILT_VNODE */ 254 { &proc_filtops }, /* EVFILT_PROC */ 255 { &sig_filtops }, /* EVFILT_SIGNAL */ 256 { &timer_filtops }, /* EVFILT_TIMER */ 257 { &file_filtops }, /* EVFILT_NETDEV */ 258 { &fs_filtops }, /* EVFILT_FS */ 259 { &null_filtops }, /* EVFILT_LIO */ 260 }; 261 262 /* 263 * Simple redirection for all cdevsw style objects to call their fo_kqfilter 264 * method. 265 */ 266 static int 267 filt_fileattach(struct knote *kn) 268 { 269 270 return (fo_kqfilter(kn->kn_fp, kn)); 271 } 272 273 /*ARGSUSED*/ 274 static int 275 kqueue_kqfilter(struct file *fp, struct knote *kn) 276 { 277 struct kqueue *kq = kn->kn_fp->f_data; 278 279 if (kn->kn_filter != EVFILT_READ) 280 return (EINVAL); 281 282 kn->kn_status |= KN_KQUEUE; 283 kn->kn_fop = &kqread_filtops; 284 knlist_add(&kq->kq_sel.si_note, kn, 0); 285 286 return (0); 287 } 288 289 static void 290 filt_kqdetach(struct knote *kn) 291 { 292 struct kqueue *kq = kn->kn_fp->f_data; 293 294 knlist_remove(&kq->kq_sel.si_note, kn, 0); 295 } 296 297 /*ARGSUSED*/ 298 static int 299 filt_kqueue(struct knote *kn, long hint) 300 { 301 struct kqueue *kq = kn->kn_fp->f_data; 302 303 kn->kn_data = kq->kq_count; 304 return (kn->kn_data > 0); 305 } 306 307 /* XXX - move to kern_proc.c? */ 308 static int 309 filt_procattach(struct knote *kn) 310 { 311 struct proc *p; 312 int immediate; 313 int error; 314 315 immediate = 0; 316 p = pfind(kn->kn_id); 317 if (p == NULL && (kn->kn_sfflags & NOTE_EXIT)) { 318 p = zpfind(kn->kn_id); 319 immediate = 1; 320 } else if (p != NULL && (p->p_flag & P_WEXIT)) { 321 immediate = 1; 322 } 323 324 if (p == NULL) 325 return (ESRCH); 326 if ((error = p_cansee(curthread, p))) 327 return (error); 328 329 kn->kn_ptr.p_proc = p; 330 kn->kn_flags |= EV_CLEAR; /* automatically set */ 331 332 /* 333 * internal flag indicating registration done by kernel 334 */ 335 if (kn->kn_flags & EV_FLAG1) { 336 kn->kn_data = kn->kn_sdata; /* ppid */ 337 kn->kn_fflags = NOTE_CHILD; 338 kn->kn_flags &= ~EV_FLAG1; 339 } 340 341 if (immediate == 0) 342 knlist_add(&p->p_klist, kn, 1); 343 344 /* 345 * Immediately activate any exit notes if the target process is a 346 * zombie. This is necessary to handle the case where the target 347 * process, e.g. a child, dies before the kevent is registered. 348 */ 349 if (immediate && filt_proc(kn, NOTE_EXIT)) 350 KNOTE_ACTIVATE(kn, 0); 351 352 PROC_UNLOCK(p); 353 354 return (0); 355 } 356 357 /* 358 * The knote may be attached to a different process, which may exit, 359 * leaving nothing for the knote to be attached to. So when the process 360 * exits, the knote is marked as DETACHED and also flagged as ONESHOT so 361 * it will be deleted when read out. However, as part of the knote deletion, 362 * this routine is called, so a check is needed to avoid actually performing 363 * a detach, because the original process does not exist any more. 364 */ 365 /* XXX - move to kern_proc.c? */ 366 static void 367 filt_procdetach(struct knote *kn) 368 { 369 struct proc *p; 370 371 p = kn->kn_ptr.p_proc; 372 knlist_remove(&p->p_klist, kn, 0); 373 kn->kn_ptr.p_proc = NULL; 374 } 375 376 /* XXX - move to kern_proc.c? */ 377 static int 378 filt_proc(struct knote *kn, long hint) 379 { 380 struct proc *p = kn->kn_ptr.p_proc; 381 u_int event; 382 383 /* 384 * mask off extra data 385 */ 386 event = (u_int)hint & NOTE_PCTRLMASK; 387 388 /* 389 * if the user is interested in this event, record it. 390 */ 391 if (kn->kn_sfflags & event) 392 kn->kn_fflags |= event; 393 394 /* 395 * process is gone, so flag the event as finished. 396 */ 397 if (event == NOTE_EXIT) { 398 if (!(kn->kn_status & KN_DETACHED)) 399 knlist_remove_inevent(&p->p_klist, kn); 400 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 401 kn->kn_data = p->p_xstat; 402 kn->kn_ptr.p_proc = NULL; 403 return (1); 404 } 405 406 /* 407 * process forked, and user wants to track the new process, 408 * so attach a new knote to it, and immediately report an 409 * event with the parent's pid. 410 */ 411 if ((event == NOTE_FORK) && (kn->kn_sfflags & NOTE_TRACK)) { 412 struct kevent kev; 413 int error; 414 415 /* 416 * register knote with new process. 417 */ 418 kev.ident = hint & NOTE_PDATAMASK; /* pid */ 419 kev.filter = kn->kn_filter; 420 kev.flags = kn->kn_flags | EV_ADD | EV_ENABLE | EV_FLAG1; 421 kev.fflags = kn->kn_sfflags; 422 kev.data = kn->kn_id; /* parent */ 423 kev.udata = kn->kn_kevent.udata; /* preserve udata */ 424 error = kqueue_register(kn->kn_kq, &kev, NULL, 0); 425 if (error) 426 kn->kn_fflags |= NOTE_TRACKERR; 427 } 428 429 return (kn->kn_fflags != 0); 430 } 431 432 static int 433 timertoticks(intptr_t data) 434 { 435 struct timeval tv; 436 int tticks; 437 438 tv.tv_sec = data / 1000; 439 tv.tv_usec = (data % 1000) * 1000; 440 tticks = tvtohz(&tv); 441 442 return tticks; 443 } 444 445 /* XXX - move to kern_timeout.c? */ 446 static void 447 filt_timerexpire(void *knx) 448 { 449 struct knote *kn = knx; 450 struct callout *calloutp; 451 452 kn->kn_data++; 453 KNOTE_ACTIVATE(kn, 0); /* XXX - handle locking */ 454 455 if ((kn->kn_flags & EV_ONESHOT) != EV_ONESHOT) { 456 calloutp = (struct callout *)kn->kn_hook; 457 callout_reset(calloutp, timertoticks(kn->kn_sdata), 458 filt_timerexpire, kn); 459 } 460 } 461 462 /* 463 * data contains amount of time to sleep, in milliseconds 464 */ 465 /* XXX - move to kern_timeout.c? */ 466 static int 467 filt_timerattach(struct knote *kn) 468 { 469 struct callout *calloutp; 470 471 atomic_add_int(&kq_ncallouts, 1); 472 473 if (kq_ncallouts >= kq_calloutmax) { 474 atomic_add_int(&kq_ncallouts, -1); 475 return (ENOMEM); 476 } 477 478 kn->kn_flags |= EV_CLEAR; /* automatically set */ 479 kn->kn_status &= ~KN_DETACHED; /* knlist_add usually sets it */ 480 MALLOC(calloutp, struct callout *, sizeof(*calloutp), 481 M_KQUEUE, M_WAITOK); 482 callout_init(calloutp, CALLOUT_MPSAFE); 483 kn->kn_hook = calloutp; 484 callout_reset(calloutp, timertoticks(kn->kn_sdata), filt_timerexpire, 485 kn); 486 487 return (0); 488 } 489 490 /* XXX - move to kern_timeout.c? */ 491 static void 492 filt_timerdetach(struct knote *kn) 493 { 494 struct callout *calloutp; 495 496 calloutp = (struct callout *)kn->kn_hook; 497 callout_drain(calloutp); 498 FREE(calloutp, M_KQUEUE); 499 atomic_add_int(&kq_ncallouts, -1); 500 kn->kn_status |= KN_DETACHED; /* knlist_remove usually clears it */ 501 } 502 503 /* XXX - move to kern_timeout.c? */ 504 static int 505 filt_timer(struct knote *kn, long hint) 506 { 507 508 return (kn->kn_data != 0); 509 } 510 511 int 512 kqueue(struct thread *td, struct kqueue_args *uap) 513 { 514 struct filedesc *fdp; 515 struct kqueue *kq; 516 struct file *fp; 517 int fd, error; 518 519 fdp = td->td_proc->p_fd; 520 error = falloc(td, &fp, &fd); 521 if (error) 522 goto done2; 523 524 /* An extra reference on `nfp' has been held for us by falloc(). */ 525 kq = malloc(sizeof *kq, M_KQUEUE, M_WAITOK | M_ZERO); 526 mtx_init(&kq->kq_lock, "kqueue", NULL, MTX_DEF|MTX_DUPOK); 527 TAILQ_INIT(&kq->kq_head); 528 kq->kq_fdp = fdp; 529 knlist_init(&kq->kq_sel.si_note, &kq->kq_lock, NULL, NULL, NULL); 530 TASK_INIT(&kq->kq_task, 0, kqueue_task, kq); 531 532 FILEDESC_XLOCK(fdp); 533 SLIST_INSERT_HEAD(&fdp->fd_kqlist, kq, kq_list); 534 FILEDESC_XUNLOCK(fdp); 535 536 finit(fp, FREAD | FWRITE, DTYPE_KQUEUE, kq, &kqueueops); 537 fdrop(fp, td); 538 539 td->td_retval[0] = fd; 540 done2: 541 return (error); 542 } 543 544 #ifndef _SYS_SYSPROTO_H_ 545 struct kevent_args { 546 int fd; 547 const struct kevent *changelist; 548 int nchanges; 549 struct kevent *eventlist; 550 int nevents; 551 const struct timespec *timeout; 552 }; 553 #endif 554 int 555 kevent(struct thread *td, struct kevent_args *uap) 556 { 557 struct timespec ts, *tsp; 558 struct kevent_copyops k_ops = { uap, 559 kevent_copyout, 560 kevent_copyin}; 561 int error; 562 #ifdef KTRACE 563 struct uio ktruio; 564 struct iovec ktriov; 565 struct uio *ktruioin = NULL; 566 struct uio *ktruioout = NULL; 567 #endif 568 569 if (uap->timeout != NULL) { 570 error = copyin(uap->timeout, &ts, sizeof(ts)); 571 if (error) 572 return (error); 573 tsp = &ts; 574 } else 575 tsp = NULL; 576 577 #ifdef KTRACE 578 if (KTRPOINT(td, KTR_GENIO)) { 579 ktriov.iov_base = uap->changelist; 580 ktriov.iov_len = uap->nchanges * sizeof(struct kevent); 581 ktruio = (struct uio){ .uio_iov = &ktriov, .uio_iovcnt = 1, 582 .uio_segflg = UIO_USERSPACE, .uio_rw = UIO_READ, 583 .uio_td = td }; 584 ktruioin = cloneuio(&ktruio); 585 ktriov.iov_base = uap->eventlist; 586 ktriov.iov_len = uap->nevents * sizeof(struct kevent); 587 ktruioout = cloneuio(&ktruio); 588 } 589 #endif 590 591 error = kern_kevent(td, uap->fd, uap->nchanges, uap->nevents, 592 &k_ops, tsp); 593 594 #ifdef KTRACE 595 if (ktruioin != NULL) { 596 ktruioin->uio_resid = uap->nchanges * sizeof(struct kevent); 597 ktrgenio(uap->fd, UIO_WRITE, ktruioin, 0); 598 ktruioout->uio_resid = td->td_retval[0] * sizeof(struct kevent); 599 ktrgenio(uap->fd, UIO_READ, ktruioout, error); 600 } 601 #endif 602 603 return (error); 604 } 605 606 /* 607 * Copy 'count' items into the destination list pointed to by uap->eventlist. 608 */ 609 static int 610 kevent_copyout(void *arg, struct kevent *kevp, int count) 611 { 612 struct kevent_args *uap; 613 int error; 614 615 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count)); 616 uap = (struct kevent_args *)arg; 617 618 error = copyout(kevp, uap->eventlist, count * sizeof *kevp); 619 if (error == 0) 620 uap->eventlist += count; 621 return (error); 622 } 623 624 /* 625 * Copy 'count' items from the list pointed to by uap->changelist. 626 */ 627 static int 628 kevent_copyin(void *arg, struct kevent *kevp, int count) 629 { 630 struct kevent_args *uap; 631 int error; 632 633 KASSERT(count <= KQ_NEVENTS, ("count (%d) > KQ_NEVENTS", count)); 634 uap = (struct kevent_args *)arg; 635 636 error = copyin(uap->changelist, kevp, count * sizeof *kevp); 637 if (error == 0) 638 uap->changelist += count; 639 return (error); 640 } 641 642 int 643 kern_kevent(struct thread *td, int fd, int nchanges, int nevents, 644 struct kevent_copyops *k_ops, const struct timespec *timeout) 645 { 646 struct kevent keva[KQ_NEVENTS]; 647 struct kevent *kevp, *changes; 648 struct kqueue *kq; 649 struct file *fp; 650 int i, n, nerrors, error; 651 652 if ((error = fget(td, fd, &fp)) != 0) 653 return (error); 654 if ((error = kqueue_acquire(fp, &kq)) != 0) 655 goto done_norel; 656 657 nerrors = 0; 658 659 while (nchanges > 0) { 660 n = nchanges > KQ_NEVENTS ? KQ_NEVENTS : nchanges; 661 error = k_ops->k_copyin(k_ops->arg, keva, n); 662 if (error) 663 goto done; 664 changes = keva; 665 for (i = 0; i < n; i++) { 666 kevp = &changes[i]; 667 if (!kevp->filter) 668 continue; 669 kevp->flags &= ~EV_SYSFLAGS; 670 error = kqueue_register(kq, kevp, td, 1); 671 if (error) { 672 if (nevents != 0) { 673 kevp->flags = EV_ERROR; 674 kevp->data = error; 675 (void) k_ops->k_copyout(k_ops->arg, 676 kevp, 1); 677 nevents--; 678 nerrors++; 679 } else { 680 goto done; 681 } 682 } 683 } 684 nchanges -= n; 685 } 686 if (nerrors) { 687 td->td_retval[0] = nerrors; 688 error = 0; 689 goto done; 690 } 691 692 error = kqueue_scan(kq, nevents, k_ops, timeout, keva, td); 693 done: 694 kqueue_release(kq, 0); 695 done_norel: 696 fdrop(fp, td); 697 return (error); 698 } 699 700 int 701 kqueue_add_filteropts(int filt, struct filterops *filtops) 702 { 703 int error; 704 705 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) { 706 printf( 707 "trying to add a filterop that is out of range: %d is beyond %d\n", 708 ~filt, EVFILT_SYSCOUNT); 709 return EINVAL; 710 } 711 mtx_lock(&filterops_lock); 712 if (sysfilt_ops[~filt].for_fop != &null_filtops && 713 sysfilt_ops[~filt].for_fop != NULL) 714 error = EEXIST; 715 else { 716 sysfilt_ops[~filt].for_fop = filtops; 717 sysfilt_ops[~filt].for_refcnt = 0; 718 } 719 mtx_unlock(&filterops_lock); 720 721 return (0); 722 } 723 724 int 725 kqueue_del_filteropts(int filt) 726 { 727 int error; 728 729 error = 0; 730 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) 731 return EINVAL; 732 733 mtx_lock(&filterops_lock); 734 if (sysfilt_ops[~filt].for_fop == &null_filtops || 735 sysfilt_ops[~filt].for_fop == NULL) 736 error = EINVAL; 737 else if (sysfilt_ops[~filt].for_refcnt != 0) 738 error = EBUSY; 739 else { 740 sysfilt_ops[~filt].for_fop = &null_filtops; 741 sysfilt_ops[~filt].for_refcnt = 0; 742 } 743 mtx_unlock(&filterops_lock); 744 745 return error; 746 } 747 748 static struct filterops * 749 kqueue_fo_find(int filt) 750 { 751 752 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) 753 return NULL; 754 755 mtx_lock(&filterops_lock); 756 sysfilt_ops[~filt].for_refcnt++; 757 if (sysfilt_ops[~filt].for_fop == NULL) 758 sysfilt_ops[~filt].for_fop = &null_filtops; 759 mtx_unlock(&filterops_lock); 760 761 return sysfilt_ops[~filt].for_fop; 762 } 763 764 static void 765 kqueue_fo_release(int filt) 766 { 767 768 if (filt > 0 || filt + EVFILT_SYSCOUNT < 0) 769 return; 770 771 mtx_lock(&filterops_lock); 772 KASSERT(sysfilt_ops[~filt].for_refcnt > 0, 773 ("filter object refcount not valid on release")); 774 sysfilt_ops[~filt].for_refcnt--; 775 mtx_unlock(&filterops_lock); 776 } 777 778 /* 779 * A ref to kq (obtained via kqueue_acquire) must be held. waitok will 780 * influence if memory allocation should wait. Make sure it is 0 if you 781 * hold any mutexes. 782 */ 783 static int 784 kqueue_register(struct kqueue *kq, struct kevent *kev, struct thread *td, int waitok) 785 { 786 struct filterops *fops; 787 struct file *fp; 788 struct knote *kn, *tkn; 789 int error, filt, event; 790 int haskqglobal; 791 792 fp = NULL; 793 kn = NULL; 794 error = 0; 795 haskqglobal = 0; 796 797 filt = kev->filter; 798 fops = kqueue_fo_find(filt); 799 if (fops == NULL) 800 return EINVAL; 801 802 tkn = knote_alloc(waitok); /* prevent waiting with locks */ 803 804 findkn: 805 if (fops->f_isfd) { 806 KASSERT(td != NULL, ("td is NULL")); 807 error = fget(td, kev->ident, &fp); 808 if (error) 809 goto done; 810 811 if ((kev->flags & EV_ADD) == EV_ADD && kqueue_expand(kq, fops, 812 kev->ident, 0) != 0) { 813 /* try again */ 814 fdrop(fp, td); 815 fp = NULL; 816 error = kqueue_expand(kq, fops, kev->ident, waitok); 817 if (error) 818 goto done; 819 goto findkn; 820 } 821 822 if (fp->f_type == DTYPE_KQUEUE) { 823 /* 824 * if we add some inteligence about what we are doing, 825 * we should be able to support events on ourselves. 826 * We need to know when we are doing this to prevent 827 * getting both the knlist lock and the kq lock since 828 * they are the same thing. 829 */ 830 if (fp->f_data == kq) { 831 error = EINVAL; 832 goto done; 833 } 834 835 KQ_GLOBAL_LOCK(&kq_global, haskqglobal); 836 } 837 838 KQ_LOCK(kq); 839 if (kev->ident < kq->kq_knlistsize) { 840 SLIST_FOREACH(kn, &kq->kq_knlist[kev->ident], kn_link) 841 if (kev->filter == kn->kn_filter) 842 break; 843 } 844 } else { 845 if ((kev->flags & EV_ADD) == EV_ADD) 846 kqueue_expand(kq, fops, kev->ident, waitok); 847 848 KQ_LOCK(kq); 849 if (kq->kq_knhashmask != 0) { 850 struct klist *list; 851 852 list = &kq->kq_knhash[ 853 KN_HASH((u_long)kev->ident, kq->kq_knhashmask)]; 854 SLIST_FOREACH(kn, list, kn_link) 855 if (kev->ident == kn->kn_id && 856 kev->filter == kn->kn_filter) 857 break; 858 } 859 } 860 861 /* knote is in the process of changing, wait for it to stablize. */ 862 if (kn != NULL && (kn->kn_status & KN_INFLUX) == KN_INFLUX) { 863 if (fp != NULL) { 864 fdrop(fp, td); 865 fp = NULL; 866 } 867 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal); 868 kq->kq_state |= KQ_FLUXWAIT; 869 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqflxwt", 0); 870 goto findkn; 871 } 872 873 if (kn == NULL && ((kev->flags & EV_ADD) == 0)) { 874 KQ_UNLOCK(kq); 875 error = ENOENT; 876 goto done; 877 } 878 879 /* 880 * kn now contains the matching knote, or NULL if no match 881 */ 882 if (kev->flags & EV_ADD) { 883 if (kn == NULL) { 884 kn = tkn; 885 tkn = NULL; 886 if (kn == NULL) { 887 KQ_UNLOCK(kq); 888 error = ENOMEM; 889 goto done; 890 } 891 kn->kn_fp = fp; 892 kn->kn_kq = kq; 893 kn->kn_fop = fops; 894 /* 895 * apply reference counts to knote structure, and 896 * do not release it at the end of this routine. 897 */ 898 fops = NULL; 899 fp = NULL; 900 901 kn->kn_sfflags = kev->fflags; 902 kn->kn_sdata = kev->data; 903 kev->fflags = 0; 904 kev->data = 0; 905 kn->kn_kevent = *kev; 906 kn->kn_kevent.flags &= ~(EV_ADD | EV_DELETE | 907 EV_ENABLE | EV_DISABLE); 908 kn->kn_status = KN_INFLUX|KN_DETACHED; 909 910 error = knote_attach(kn, kq); 911 KQ_UNLOCK(kq); 912 if (error != 0) { 913 tkn = kn; 914 goto done; 915 } 916 917 if ((error = kn->kn_fop->f_attach(kn)) != 0) { 918 knote_drop(kn, td); 919 goto done; 920 } 921 KN_LIST_LOCK(kn); 922 } else { 923 /* 924 * The user may change some filter values after the 925 * initial EV_ADD, but doing so will not reset any 926 * filter which has already been triggered. 927 */ 928 kn->kn_status |= KN_INFLUX; 929 KQ_UNLOCK(kq); 930 KN_LIST_LOCK(kn); 931 kn->kn_sfflags = kev->fflags; 932 kn->kn_sdata = kev->data; 933 kn->kn_kevent.udata = kev->udata; 934 } 935 936 /* 937 * We can get here with kn->kn_knlist == NULL. 938 * This can happen when the initial attach event decides that 939 * the event is "completed" already. i.e. filt_procattach 940 * is called on a zombie process. It will call filt_proc 941 * which will remove it from the list, and NULL kn_knlist. 942 */ 943 event = kn->kn_fop->f_event(kn, 0); 944 KQ_LOCK(kq); 945 if (event) 946 KNOTE_ACTIVATE(kn, 1); 947 kn->kn_status &= ~KN_INFLUX; 948 KN_LIST_UNLOCK(kn); 949 } else if (kev->flags & EV_DELETE) { 950 kn->kn_status |= KN_INFLUX; 951 KQ_UNLOCK(kq); 952 if (!(kn->kn_status & KN_DETACHED)) 953 kn->kn_fop->f_detach(kn); 954 knote_drop(kn, td); 955 goto done; 956 } 957 958 if ((kev->flags & EV_DISABLE) && 959 ((kn->kn_status & KN_DISABLED) == 0)) { 960 kn->kn_status |= KN_DISABLED; 961 } 962 963 if ((kev->flags & EV_ENABLE) && (kn->kn_status & KN_DISABLED)) { 964 kn->kn_status &= ~KN_DISABLED; 965 if ((kn->kn_status & KN_ACTIVE) && 966 ((kn->kn_status & KN_QUEUED) == 0)) 967 knote_enqueue(kn); 968 } 969 KQ_UNLOCK_FLUX(kq); 970 971 done: 972 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal); 973 if (fp != NULL) 974 fdrop(fp, td); 975 if (tkn != NULL) 976 knote_free(tkn); 977 if (fops != NULL) 978 kqueue_fo_release(filt); 979 return (error); 980 } 981 982 static int 983 kqueue_acquire(struct file *fp, struct kqueue **kqp) 984 { 985 int error; 986 struct kqueue *kq; 987 988 error = 0; 989 990 kq = fp->f_data; 991 if (fp->f_type != DTYPE_KQUEUE || kq == NULL) 992 return (EBADF); 993 *kqp = kq; 994 KQ_LOCK(kq); 995 if ((kq->kq_state & KQ_CLOSING) == KQ_CLOSING) { 996 KQ_UNLOCK(kq); 997 return (EBADF); 998 } 999 kq->kq_refcnt++; 1000 KQ_UNLOCK(kq); 1001 1002 return error; 1003 } 1004 1005 static void 1006 kqueue_release(struct kqueue *kq, int locked) 1007 { 1008 if (locked) 1009 KQ_OWNED(kq); 1010 else 1011 KQ_LOCK(kq); 1012 kq->kq_refcnt--; 1013 if (kq->kq_refcnt == 1) 1014 wakeup(&kq->kq_refcnt); 1015 if (!locked) 1016 KQ_UNLOCK(kq); 1017 } 1018 1019 static void 1020 kqueue_schedtask(struct kqueue *kq) 1021 { 1022 1023 KQ_OWNED(kq); 1024 KASSERT(((kq->kq_state & KQ_TASKDRAIN) != KQ_TASKDRAIN), 1025 ("scheduling kqueue task while draining")); 1026 1027 if ((kq->kq_state & KQ_TASKSCHED) != KQ_TASKSCHED) { 1028 taskqueue_enqueue(taskqueue_kqueue, &kq->kq_task); 1029 kq->kq_state |= KQ_TASKSCHED; 1030 } 1031 } 1032 1033 /* 1034 * Expand the kq to make sure we have storage for fops/ident pair. 1035 * 1036 * Return 0 on success (or no work necessary), return errno on failure. 1037 * 1038 * Not calling hashinit w/ waitok (proper malloc flag) should be safe. 1039 * If kqueue_register is called from a non-fd context, there usually/should 1040 * be no locks held. 1041 */ 1042 static int 1043 kqueue_expand(struct kqueue *kq, struct filterops *fops, uintptr_t ident, 1044 int waitok) 1045 { 1046 struct klist *list, *tmp_knhash; 1047 u_long tmp_knhashmask; 1048 int size; 1049 int fd; 1050 int mflag = waitok ? M_WAITOK : M_NOWAIT; 1051 1052 KQ_NOTOWNED(kq); 1053 1054 if (fops->f_isfd) { 1055 fd = ident; 1056 if (kq->kq_knlistsize <= fd) { 1057 size = kq->kq_knlistsize; 1058 while (size <= fd) 1059 size += KQEXTENT; 1060 MALLOC(list, struct klist *, 1061 size * sizeof list, M_KQUEUE, mflag); 1062 if (list == NULL) 1063 return ENOMEM; 1064 KQ_LOCK(kq); 1065 if (kq->kq_knlistsize > fd) { 1066 FREE(list, M_KQUEUE); 1067 list = NULL; 1068 } else { 1069 if (kq->kq_knlist != NULL) { 1070 bcopy(kq->kq_knlist, list, 1071 kq->kq_knlistsize * sizeof list); 1072 FREE(kq->kq_knlist, M_KQUEUE); 1073 kq->kq_knlist = NULL; 1074 } 1075 bzero((caddr_t)list + 1076 kq->kq_knlistsize * sizeof list, 1077 (size - kq->kq_knlistsize) * sizeof list); 1078 kq->kq_knlistsize = size; 1079 kq->kq_knlist = list; 1080 } 1081 KQ_UNLOCK(kq); 1082 } 1083 } else { 1084 if (kq->kq_knhashmask == 0) { 1085 tmp_knhash = hashinit(KN_HASHSIZE, M_KQUEUE, 1086 &tmp_knhashmask); 1087 if (tmp_knhash == NULL) 1088 return ENOMEM; 1089 KQ_LOCK(kq); 1090 if (kq->kq_knhashmask == 0) { 1091 kq->kq_knhash = tmp_knhash; 1092 kq->kq_knhashmask = tmp_knhashmask; 1093 } else { 1094 free(tmp_knhash, M_KQUEUE); 1095 } 1096 KQ_UNLOCK(kq); 1097 } 1098 } 1099 1100 KQ_NOTOWNED(kq); 1101 return 0; 1102 } 1103 1104 static void 1105 kqueue_task(void *arg, int pending) 1106 { 1107 struct kqueue *kq; 1108 int haskqglobal; 1109 1110 haskqglobal = 0; 1111 kq = arg; 1112 1113 KQ_GLOBAL_LOCK(&kq_global, haskqglobal); 1114 KQ_LOCK(kq); 1115 1116 KNOTE_LOCKED(&kq->kq_sel.si_note, 0); 1117 1118 kq->kq_state &= ~KQ_TASKSCHED; 1119 if ((kq->kq_state & KQ_TASKDRAIN) == KQ_TASKDRAIN) { 1120 wakeup(&kq->kq_state); 1121 } 1122 KQ_UNLOCK(kq); 1123 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal); 1124 } 1125 1126 /* 1127 * Scan, update kn_data (if not ONESHOT), and copyout triggered events. 1128 * We treat KN_MARKER knotes as if they are INFLUX. 1129 */ 1130 static int 1131 kqueue_scan(struct kqueue *kq, int maxevents, struct kevent_copyops *k_ops, 1132 const struct timespec *tsp, struct kevent *keva, struct thread *td) 1133 { 1134 struct kevent *kevp; 1135 struct timeval atv, rtv, ttv; 1136 struct knote *kn, *marker; 1137 int count, timeout, nkev, error; 1138 int haskqglobal; 1139 1140 count = maxevents; 1141 nkev = 0; 1142 error = 0; 1143 haskqglobal = 0; 1144 1145 if (maxevents == 0) 1146 goto done_nl; 1147 1148 if (tsp != NULL) { 1149 TIMESPEC_TO_TIMEVAL(&atv, tsp); 1150 if (itimerfix(&atv)) { 1151 error = EINVAL; 1152 goto done_nl; 1153 } 1154 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0) 1155 timeout = -1; 1156 else 1157 timeout = atv.tv_sec > 24 * 60 * 60 ? 1158 24 * 60 * 60 * hz : tvtohz(&atv); 1159 getmicrouptime(&rtv); 1160 timevaladd(&atv, &rtv); 1161 } else { 1162 atv.tv_sec = 0; 1163 atv.tv_usec = 0; 1164 timeout = 0; 1165 } 1166 marker = knote_alloc(1); 1167 if (marker == NULL) { 1168 error = ENOMEM; 1169 goto done_nl; 1170 } 1171 marker->kn_status = KN_MARKER; 1172 KQ_LOCK(kq); 1173 goto start; 1174 1175 retry: 1176 if (atv.tv_sec || atv.tv_usec) { 1177 getmicrouptime(&rtv); 1178 if (timevalcmp(&rtv, &atv, >=)) 1179 goto done; 1180 ttv = atv; 1181 timevalsub(&ttv, &rtv); 1182 timeout = ttv.tv_sec > 24 * 60 * 60 ? 1183 24 * 60 * 60 * hz : tvtohz(&ttv); 1184 } 1185 1186 start: 1187 kevp = keva; 1188 if (kq->kq_count == 0) { 1189 if (timeout < 0) { 1190 error = EWOULDBLOCK; 1191 } else { 1192 kq->kq_state |= KQ_SLEEP; 1193 error = msleep(kq, &kq->kq_lock, PSOCK | PCATCH, 1194 "kqread", timeout); 1195 } 1196 if (error == 0) 1197 goto retry; 1198 /* don't restart after signals... */ 1199 if (error == ERESTART) 1200 error = EINTR; 1201 else if (error == EWOULDBLOCK) 1202 error = 0; 1203 goto done; 1204 } 1205 1206 TAILQ_INSERT_TAIL(&kq->kq_head, marker, kn_tqe); 1207 while (count) { 1208 KQ_OWNED(kq); 1209 kn = TAILQ_FIRST(&kq->kq_head); 1210 1211 if ((kn->kn_status == KN_MARKER && kn != marker) || 1212 (kn->kn_status & KN_INFLUX) == KN_INFLUX) { 1213 kq->kq_state |= KQ_FLUXWAIT; 1214 error = msleep(kq, &kq->kq_lock, PSOCK, 1215 "kqflxwt", 0); 1216 continue; 1217 } 1218 1219 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe); 1220 if ((kn->kn_status & KN_DISABLED) == KN_DISABLED) { 1221 kn->kn_status &= ~KN_QUEUED; 1222 kq->kq_count--; 1223 continue; 1224 } 1225 if (kn == marker) { 1226 KQ_FLUX_WAKEUP(kq); 1227 if (count == maxevents) 1228 goto retry; 1229 goto done; 1230 } 1231 KASSERT((kn->kn_status & KN_INFLUX) == 0, 1232 ("KN_INFLUX set when not suppose to be")); 1233 1234 if ((kn->kn_flags & EV_ONESHOT) == EV_ONESHOT) { 1235 kn->kn_status &= ~KN_QUEUED; 1236 kn->kn_status |= KN_INFLUX; 1237 kq->kq_count--; 1238 KQ_UNLOCK(kq); 1239 /* 1240 * We don't need to lock the list since we've marked 1241 * it _INFLUX. 1242 */ 1243 *kevp = kn->kn_kevent; 1244 if (!(kn->kn_status & KN_DETACHED)) 1245 kn->kn_fop->f_detach(kn); 1246 knote_drop(kn, td); 1247 KQ_LOCK(kq); 1248 kn = NULL; 1249 } else { 1250 kn->kn_status |= KN_INFLUX; 1251 KQ_UNLOCK(kq); 1252 if ((kn->kn_status & KN_KQUEUE) == KN_KQUEUE) 1253 KQ_GLOBAL_LOCK(&kq_global, haskqglobal); 1254 KN_LIST_LOCK(kn); 1255 if (kn->kn_fop->f_event(kn, 0) == 0) { 1256 KQ_LOCK(kq); 1257 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal); 1258 kn->kn_status &= 1259 ~(KN_QUEUED | KN_ACTIVE | KN_INFLUX); 1260 kq->kq_count--; 1261 KN_LIST_UNLOCK(kn); 1262 continue; 1263 } 1264 *kevp = kn->kn_kevent; 1265 KQ_LOCK(kq); 1266 KQ_GLOBAL_UNLOCK(&kq_global, haskqglobal); 1267 if (kn->kn_flags & EV_CLEAR) { 1268 kn->kn_data = 0; 1269 kn->kn_fflags = 0; 1270 kn->kn_status &= ~(KN_QUEUED | KN_ACTIVE); 1271 kq->kq_count--; 1272 } else 1273 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe); 1274 1275 kn->kn_status &= ~(KN_INFLUX); 1276 KN_LIST_UNLOCK(kn); 1277 } 1278 1279 /* we are returning a copy to the user */ 1280 kevp++; 1281 nkev++; 1282 count--; 1283 1284 if (nkev == KQ_NEVENTS) { 1285 KQ_UNLOCK_FLUX(kq); 1286 error = k_ops->k_copyout(k_ops->arg, keva, nkev); 1287 nkev = 0; 1288 kevp = keva; 1289 KQ_LOCK(kq); 1290 if (error) 1291 break; 1292 } 1293 } 1294 TAILQ_REMOVE(&kq->kq_head, marker, kn_tqe); 1295 done: 1296 KQ_OWNED(kq); 1297 KQ_UNLOCK_FLUX(kq); 1298 knote_free(marker); 1299 done_nl: 1300 KQ_NOTOWNED(kq); 1301 if (nkev != 0) 1302 error = k_ops->k_copyout(k_ops->arg, keva, nkev); 1303 td->td_retval[0] = maxevents - count; 1304 return (error); 1305 } 1306 1307 /* 1308 * XXX 1309 * This could be expanded to call kqueue_scan, if desired. 1310 */ 1311 /*ARGSUSED*/ 1312 static int 1313 kqueue_read(struct file *fp, struct uio *uio, struct ucred *active_cred, 1314 int flags, struct thread *td) 1315 { 1316 return (ENXIO); 1317 } 1318 1319 /*ARGSUSED*/ 1320 static int 1321 kqueue_write(struct file *fp, struct uio *uio, struct ucred *active_cred, 1322 int flags, struct thread *td) 1323 { 1324 return (ENXIO); 1325 } 1326 1327 /*ARGSUSED*/ 1328 static int 1329 kqueue_truncate(struct file *fp, off_t length, struct ucred *active_cred, 1330 struct thread *td) 1331 { 1332 1333 return (EINVAL); 1334 } 1335 1336 /*ARGSUSED*/ 1337 static int 1338 kqueue_ioctl(struct file *fp, u_long cmd, void *data, 1339 struct ucred *active_cred, struct thread *td) 1340 { 1341 /* 1342 * Enabling sigio causes two major problems: 1343 * 1) infinite recursion: 1344 * Synopsys: kevent is being used to track signals and have FIOASYNC 1345 * set. On receipt of a signal this will cause a kqueue to recurse 1346 * into itself over and over. Sending the sigio causes the kqueue 1347 * to become ready, which in turn posts sigio again, forever. 1348 * Solution: this can be solved by setting a flag in the kqueue that 1349 * we have a SIGIO in progress. 1350 * 2) locking problems: 1351 * Synopsys: Kqueue is a leaf subsystem, but adding signalling puts 1352 * us above the proc and pgrp locks. 1353 * Solution: Post a signal using an async mechanism, being sure to 1354 * record a generation count in the delivery so that we do not deliver 1355 * a signal to the wrong process. 1356 * 1357 * Note, these two mechanisms are somewhat mutually exclusive! 1358 */ 1359 #if 0 1360 struct kqueue *kq; 1361 1362 kq = fp->f_data; 1363 switch (cmd) { 1364 case FIOASYNC: 1365 if (*(int *)data) { 1366 kq->kq_state |= KQ_ASYNC; 1367 } else { 1368 kq->kq_state &= ~KQ_ASYNC; 1369 } 1370 return (0); 1371 1372 case FIOSETOWN: 1373 return (fsetown(*(int *)data, &kq->kq_sigio)); 1374 1375 case FIOGETOWN: 1376 *(int *)data = fgetown(&kq->kq_sigio); 1377 return (0); 1378 } 1379 #endif 1380 1381 return (ENOTTY); 1382 } 1383 1384 /*ARGSUSED*/ 1385 static int 1386 kqueue_poll(struct file *fp, int events, struct ucred *active_cred, 1387 struct thread *td) 1388 { 1389 struct kqueue *kq; 1390 int revents = 0; 1391 int error; 1392 1393 if ((error = kqueue_acquire(fp, &kq))) 1394 return POLLERR; 1395 1396 KQ_LOCK(kq); 1397 if (events & (POLLIN | POLLRDNORM)) { 1398 if (kq->kq_count) { 1399 revents |= events & (POLLIN | POLLRDNORM); 1400 } else { 1401 selrecord(td, &kq->kq_sel); 1402 if (SEL_WAITING(&kq->kq_sel)) 1403 kq->kq_state |= KQ_SEL; 1404 } 1405 } 1406 kqueue_release(kq, 1); 1407 KQ_UNLOCK(kq); 1408 return (revents); 1409 } 1410 1411 /*ARGSUSED*/ 1412 static int 1413 kqueue_stat(struct file *fp, struct stat *st, struct ucred *active_cred, 1414 struct thread *td) 1415 { 1416 1417 bzero((void *)st, sizeof *st); 1418 /* 1419 * We no longer return kq_count because the unlocked value is useless. 1420 * If you spent all this time getting the count, why not spend your 1421 * syscall better by calling kevent? 1422 * 1423 * XXX - This is needed for libc_r. 1424 */ 1425 st->st_mode = S_IFIFO; 1426 return (0); 1427 } 1428 1429 /*ARGSUSED*/ 1430 static int 1431 kqueue_close(struct file *fp, struct thread *td) 1432 { 1433 struct kqueue *kq = fp->f_data; 1434 struct filedesc *fdp; 1435 struct knote *kn; 1436 int i; 1437 int error; 1438 1439 if ((error = kqueue_acquire(fp, &kq))) 1440 return error; 1441 1442 KQ_LOCK(kq); 1443 1444 KASSERT((kq->kq_state & KQ_CLOSING) != KQ_CLOSING, 1445 ("kqueue already closing")); 1446 kq->kq_state |= KQ_CLOSING; 1447 if (kq->kq_refcnt > 1) 1448 msleep(&kq->kq_refcnt, &kq->kq_lock, PSOCK, "kqclose", 0); 1449 1450 KASSERT(kq->kq_refcnt == 1, ("other refs are out there!")); 1451 fdp = kq->kq_fdp; 1452 1453 KASSERT(knlist_empty(&kq->kq_sel.si_note), 1454 ("kqueue's knlist not empty")); 1455 1456 for (i = 0; i < kq->kq_knlistsize; i++) { 1457 while ((kn = SLIST_FIRST(&kq->kq_knlist[i])) != NULL) { 1458 KASSERT((kn->kn_status & KN_INFLUX) == 0, 1459 ("KN_INFLUX set when not suppose to be")); 1460 kn->kn_status |= KN_INFLUX; 1461 KQ_UNLOCK(kq); 1462 if (!(kn->kn_status & KN_DETACHED)) 1463 kn->kn_fop->f_detach(kn); 1464 knote_drop(kn, td); 1465 KQ_LOCK(kq); 1466 } 1467 } 1468 if (kq->kq_knhashmask != 0) { 1469 for (i = 0; i <= kq->kq_knhashmask; i++) { 1470 while ((kn = SLIST_FIRST(&kq->kq_knhash[i])) != NULL) { 1471 KASSERT((kn->kn_status & KN_INFLUX) == 0, 1472 ("KN_INFLUX set when not suppose to be")); 1473 kn->kn_status |= KN_INFLUX; 1474 KQ_UNLOCK(kq); 1475 if (!(kn->kn_status & KN_DETACHED)) 1476 kn->kn_fop->f_detach(kn); 1477 knote_drop(kn, td); 1478 KQ_LOCK(kq); 1479 } 1480 } 1481 } 1482 1483 if ((kq->kq_state & KQ_TASKSCHED) == KQ_TASKSCHED) { 1484 kq->kq_state |= KQ_TASKDRAIN; 1485 msleep(&kq->kq_state, &kq->kq_lock, PSOCK, "kqtqdr", 0); 1486 } 1487 1488 if ((kq->kq_state & KQ_SEL) == KQ_SEL) { 1489 selwakeuppri(&kq->kq_sel, PSOCK); 1490 if (!SEL_WAITING(&kq->kq_sel)) 1491 kq->kq_state &= ~KQ_SEL; 1492 } 1493 1494 KQ_UNLOCK(kq); 1495 1496 FILEDESC_XLOCK(fdp); 1497 SLIST_REMOVE(&fdp->fd_kqlist, kq, kqueue, kq_list); 1498 FILEDESC_XUNLOCK(fdp); 1499 1500 knlist_destroy(&kq->kq_sel.si_note); 1501 mtx_destroy(&kq->kq_lock); 1502 kq->kq_fdp = NULL; 1503 1504 if (kq->kq_knhash != NULL) 1505 free(kq->kq_knhash, M_KQUEUE); 1506 if (kq->kq_knlist != NULL) 1507 free(kq->kq_knlist, M_KQUEUE); 1508 1509 funsetown(&kq->kq_sigio); 1510 free(kq, M_KQUEUE); 1511 fp->f_data = NULL; 1512 1513 return (0); 1514 } 1515 1516 static void 1517 kqueue_wakeup(struct kqueue *kq) 1518 { 1519 KQ_OWNED(kq); 1520 1521 if ((kq->kq_state & KQ_SLEEP) == KQ_SLEEP) { 1522 kq->kq_state &= ~KQ_SLEEP; 1523 wakeup(kq); 1524 } 1525 if ((kq->kq_state & KQ_SEL) == KQ_SEL) { 1526 selwakeuppri(&kq->kq_sel, PSOCK); 1527 if (!SEL_WAITING(&kq->kq_sel)) 1528 kq->kq_state &= ~KQ_SEL; 1529 } 1530 if (!knlist_empty(&kq->kq_sel.si_note)) 1531 kqueue_schedtask(kq); 1532 if ((kq->kq_state & KQ_ASYNC) == KQ_ASYNC) { 1533 pgsigio(&kq->kq_sigio, SIGIO, 0); 1534 } 1535 } 1536 1537 /* 1538 * Walk down a list of knotes, activating them if their event has triggered. 1539 * 1540 * There is a possibility to optimize in the case of one kq watching another. 1541 * Instead of scheduling a task to wake it up, you could pass enough state 1542 * down the chain to make up the parent kqueue. Make this code functional 1543 * first. 1544 */ 1545 void 1546 knote(struct knlist *list, long hint, int islocked) 1547 { 1548 struct kqueue *kq; 1549 struct knote *kn; 1550 1551 if (list == NULL) 1552 return; 1553 1554 KNL_ASSERT_LOCK(list, islocked); 1555 1556 if (!islocked) 1557 list->kl_lock(list->kl_lockarg); 1558 1559 /* 1560 * If we unlock the list lock (and set KN_INFLUX), we can eliminate 1561 * the kqueue scheduling, but this will introduce four 1562 * lock/unlock's for each knote to test. If we do, continue to use 1563 * SLIST_FOREACH, SLIST_FOREACH_SAFE is not safe in our case, it is 1564 * only safe if you want to remove the current item, which we are 1565 * not doing. 1566 */ 1567 SLIST_FOREACH(kn, &list->kl_list, kn_selnext) { 1568 kq = kn->kn_kq; 1569 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) { 1570 KQ_LOCK(kq); 1571 if ((kn->kn_status & KN_INFLUX) != KN_INFLUX) { 1572 kn->kn_status |= KN_HASKQLOCK; 1573 if (kn->kn_fop->f_event(kn, hint)) 1574 KNOTE_ACTIVATE(kn, 1); 1575 kn->kn_status &= ~KN_HASKQLOCK; 1576 } 1577 KQ_UNLOCK(kq); 1578 } 1579 kq = NULL; 1580 } 1581 if (!islocked) 1582 list->kl_unlock(list->kl_lockarg); 1583 } 1584 1585 /* 1586 * add a knote to a knlist 1587 */ 1588 void 1589 knlist_add(struct knlist *knl, struct knote *kn, int islocked) 1590 { 1591 KNL_ASSERT_LOCK(knl, islocked); 1592 KQ_NOTOWNED(kn->kn_kq); 1593 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == 1594 (KN_INFLUX|KN_DETACHED), ("knote not KN_INFLUX and KN_DETACHED")); 1595 if (!islocked) 1596 knl->kl_lock(knl->kl_lockarg); 1597 SLIST_INSERT_HEAD(&knl->kl_list, kn, kn_selnext); 1598 if (!islocked) 1599 knl->kl_unlock(knl->kl_lockarg); 1600 KQ_LOCK(kn->kn_kq); 1601 kn->kn_knlist = knl; 1602 kn->kn_status &= ~KN_DETACHED; 1603 KQ_UNLOCK(kn->kn_kq); 1604 } 1605 1606 static void 1607 knlist_remove_kq(struct knlist *knl, struct knote *kn, int knlislocked, int kqislocked) 1608 { 1609 KASSERT(!(!!kqislocked && !knlislocked), ("kq locked w/o knl locked")); 1610 KNL_ASSERT_LOCK(knl, knlislocked); 1611 mtx_assert(&kn->kn_kq->kq_lock, kqislocked ? MA_OWNED : MA_NOTOWNED); 1612 if (!kqislocked) 1613 KASSERT((kn->kn_status & (KN_INFLUX|KN_DETACHED)) == KN_INFLUX, 1614 ("knlist_remove called w/o knote being KN_INFLUX or already removed")); 1615 if (!knlislocked) 1616 knl->kl_lock(knl->kl_lockarg); 1617 SLIST_REMOVE(&knl->kl_list, kn, knote, kn_selnext); 1618 kn->kn_knlist = NULL; 1619 if (!knlislocked) 1620 knl->kl_unlock(knl->kl_lockarg); 1621 if (!kqislocked) 1622 KQ_LOCK(kn->kn_kq); 1623 kn->kn_status |= KN_DETACHED; 1624 if (!kqislocked) 1625 KQ_UNLOCK(kn->kn_kq); 1626 } 1627 1628 /* 1629 * remove all knotes from a specified klist 1630 */ 1631 void 1632 knlist_remove(struct knlist *knl, struct knote *kn, int islocked) 1633 { 1634 1635 knlist_remove_kq(knl, kn, islocked, 0); 1636 } 1637 1638 /* 1639 * remove knote from a specified klist while in f_event handler. 1640 */ 1641 void 1642 knlist_remove_inevent(struct knlist *knl, struct knote *kn) 1643 { 1644 1645 knlist_remove_kq(knl, kn, 1, 1646 (kn->kn_status & KN_HASKQLOCK) == KN_HASKQLOCK); 1647 } 1648 1649 int 1650 knlist_empty(struct knlist *knl) 1651 { 1652 KNL_ASSERT_LOCKED(knl); 1653 return SLIST_EMPTY(&knl->kl_list); 1654 } 1655 1656 static struct mtx knlist_lock; 1657 MTX_SYSINIT(knlist_lock, &knlist_lock, "knlist lock for lockless objects", 1658 MTX_DEF); 1659 static void knlist_mtx_lock(void *arg); 1660 static void knlist_mtx_unlock(void *arg); 1661 static int knlist_mtx_locked(void *arg); 1662 1663 static void 1664 knlist_mtx_lock(void *arg) 1665 { 1666 mtx_lock((struct mtx *)arg); 1667 } 1668 1669 static void 1670 knlist_mtx_unlock(void *arg) 1671 { 1672 mtx_unlock((struct mtx *)arg); 1673 } 1674 1675 static int 1676 knlist_mtx_locked(void *arg) 1677 { 1678 return (mtx_owned((struct mtx *)arg)); 1679 } 1680 1681 void 1682 knlist_init(struct knlist *knl, void *lock, void (*kl_lock)(void *), 1683 void (*kl_unlock)(void *), int (*kl_locked)(void *)) 1684 { 1685 1686 if (lock == NULL) 1687 knl->kl_lockarg = &knlist_lock; 1688 else 1689 knl->kl_lockarg = lock; 1690 1691 if (kl_lock == NULL) 1692 knl->kl_lock = knlist_mtx_lock; 1693 else 1694 knl->kl_lock = kl_lock; 1695 if (kl_unlock == NULL) 1696 knl->kl_unlock = knlist_mtx_unlock; 1697 else 1698 knl->kl_unlock = kl_unlock; 1699 if (kl_locked == NULL) 1700 knl->kl_locked = knlist_mtx_locked; 1701 else 1702 knl->kl_locked = kl_locked; 1703 1704 SLIST_INIT(&knl->kl_list); 1705 } 1706 1707 void 1708 knlist_destroy(struct knlist *knl) 1709 { 1710 1711 #ifdef INVARIANTS 1712 /* 1713 * if we run across this error, we need to find the offending 1714 * driver and have it call knlist_clear. 1715 */ 1716 if (!SLIST_EMPTY(&knl->kl_list)) 1717 printf("WARNING: destroying knlist w/ knotes on it!\n"); 1718 #endif 1719 1720 knl->kl_lockarg = knl->kl_lock = knl->kl_unlock = NULL; 1721 SLIST_INIT(&knl->kl_list); 1722 } 1723 1724 /* 1725 * Even if we are locked, we may need to drop the lock to allow any influx 1726 * knotes time to "settle". 1727 */ 1728 void 1729 knlist_cleardel(struct knlist *knl, struct thread *td, int islocked, int killkn) 1730 { 1731 struct knote *kn, *kn2; 1732 struct kqueue *kq; 1733 1734 if (islocked) 1735 KNL_ASSERT_LOCKED(knl); 1736 else { 1737 KNL_ASSERT_UNLOCKED(knl); 1738 again: /* need to reacquire lock since we have dropped it */ 1739 knl->kl_lock(knl->kl_lockarg); 1740 } 1741 1742 SLIST_FOREACH_SAFE(kn, &knl->kl_list, kn_selnext, kn2) { 1743 kq = kn->kn_kq; 1744 KQ_LOCK(kq); 1745 if ((kn->kn_status & KN_INFLUX)) { 1746 KQ_UNLOCK(kq); 1747 continue; 1748 } 1749 knlist_remove_kq(knl, kn, 1, 1); 1750 if (killkn) { 1751 kn->kn_status |= KN_INFLUX | KN_DETACHED; 1752 KQ_UNLOCK(kq); 1753 knote_drop(kn, td); 1754 } else { 1755 /* Make sure cleared knotes disappear soon */ 1756 kn->kn_flags |= (EV_EOF | EV_ONESHOT); 1757 KQ_UNLOCK(kq); 1758 } 1759 kq = NULL; 1760 } 1761 1762 if (!SLIST_EMPTY(&knl->kl_list)) { 1763 /* there are still KN_INFLUX remaining */ 1764 kn = SLIST_FIRST(&knl->kl_list); 1765 kq = kn->kn_kq; 1766 KQ_LOCK(kq); 1767 KASSERT(kn->kn_status & KN_INFLUX, 1768 ("knote removed w/o list lock")); 1769 knl->kl_unlock(knl->kl_lockarg); 1770 kq->kq_state |= KQ_FLUXWAIT; 1771 msleep(kq, &kq->kq_lock, PSOCK | PDROP, "kqkclr", 0); 1772 kq = NULL; 1773 goto again; 1774 } 1775 1776 if (islocked) 1777 KNL_ASSERT_LOCKED(knl); 1778 else { 1779 knl->kl_unlock(knl->kl_lockarg); 1780 KNL_ASSERT_UNLOCKED(knl); 1781 } 1782 } 1783 1784 /* 1785 * Remove all knotes referencing a specified fd must be called with FILEDESC 1786 * lock. This prevents a race where a new fd comes along and occupies the 1787 * entry and we attach a knote to the fd. 1788 */ 1789 void 1790 knote_fdclose(struct thread *td, int fd) 1791 { 1792 struct filedesc *fdp = td->td_proc->p_fd; 1793 struct kqueue *kq; 1794 struct knote *kn; 1795 int influx; 1796 1797 FILEDESC_XLOCK_ASSERT(fdp); 1798 1799 /* 1800 * We shouldn't have to worry about new kevents appearing on fd 1801 * since filedesc is locked. 1802 */ 1803 SLIST_FOREACH(kq, &fdp->fd_kqlist, kq_list) { 1804 KQ_LOCK(kq); 1805 1806 again: 1807 influx = 0; 1808 while (kq->kq_knlistsize > fd && 1809 (kn = SLIST_FIRST(&kq->kq_knlist[fd])) != NULL) { 1810 if (kn->kn_status & KN_INFLUX) { 1811 /* someone else might be waiting on our knote */ 1812 if (influx) 1813 wakeup(kq); 1814 kq->kq_state |= KQ_FLUXWAIT; 1815 msleep(kq, &kq->kq_lock, PSOCK, "kqflxwt", 0); 1816 goto again; 1817 } 1818 kn->kn_status |= KN_INFLUX; 1819 KQ_UNLOCK(kq); 1820 if (!(kn->kn_status & KN_DETACHED)) 1821 kn->kn_fop->f_detach(kn); 1822 knote_drop(kn, td); 1823 influx = 1; 1824 KQ_LOCK(kq); 1825 } 1826 KQ_UNLOCK_FLUX(kq); 1827 } 1828 } 1829 1830 static int 1831 knote_attach(struct knote *kn, struct kqueue *kq) 1832 { 1833 struct klist *list; 1834 1835 KASSERT(kn->kn_status & KN_INFLUX, ("knote not marked INFLUX")); 1836 KQ_OWNED(kq); 1837 1838 if (kn->kn_fop->f_isfd) { 1839 if (kn->kn_id >= kq->kq_knlistsize) 1840 return ENOMEM; 1841 list = &kq->kq_knlist[kn->kn_id]; 1842 } else { 1843 if (kq->kq_knhash == NULL) 1844 return ENOMEM; 1845 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)]; 1846 } 1847 1848 SLIST_INSERT_HEAD(list, kn, kn_link); 1849 1850 return 0; 1851 } 1852 1853 /* 1854 * knote must already have been detached using the f_detach method. 1855 * no lock need to be held, it is assumed that the KN_INFLUX flag is set 1856 * to prevent other removal. 1857 */ 1858 static void 1859 knote_drop(struct knote *kn, struct thread *td) 1860 { 1861 struct kqueue *kq; 1862 struct klist *list; 1863 1864 kq = kn->kn_kq; 1865 1866 KQ_NOTOWNED(kq); 1867 KASSERT((kn->kn_status & KN_INFLUX) == KN_INFLUX, 1868 ("knote_drop called without KN_INFLUX set in kn_status")); 1869 1870 KQ_LOCK(kq); 1871 if (kn->kn_fop->f_isfd) 1872 list = &kq->kq_knlist[kn->kn_id]; 1873 else 1874 list = &kq->kq_knhash[KN_HASH(kn->kn_id, kq->kq_knhashmask)]; 1875 1876 if (!SLIST_EMPTY(list)) 1877 SLIST_REMOVE(list, kn, knote, kn_link); 1878 if (kn->kn_status & KN_QUEUED) 1879 knote_dequeue(kn); 1880 KQ_UNLOCK_FLUX(kq); 1881 1882 if (kn->kn_fop->f_isfd) { 1883 fdrop(kn->kn_fp, td); 1884 kn->kn_fp = NULL; 1885 } 1886 kqueue_fo_release(kn->kn_kevent.filter); 1887 kn->kn_fop = NULL; 1888 knote_free(kn); 1889 } 1890 1891 static void 1892 knote_enqueue(struct knote *kn) 1893 { 1894 struct kqueue *kq = kn->kn_kq; 1895 1896 KQ_OWNED(kn->kn_kq); 1897 KASSERT((kn->kn_status & KN_QUEUED) == 0, ("knote already queued")); 1898 1899 TAILQ_INSERT_TAIL(&kq->kq_head, kn, kn_tqe); 1900 kn->kn_status |= KN_QUEUED; 1901 kq->kq_count++; 1902 kqueue_wakeup(kq); 1903 } 1904 1905 static void 1906 knote_dequeue(struct knote *kn) 1907 { 1908 struct kqueue *kq = kn->kn_kq; 1909 1910 KQ_OWNED(kn->kn_kq); 1911 KASSERT(kn->kn_status & KN_QUEUED, ("knote not queued")); 1912 1913 TAILQ_REMOVE(&kq->kq_head, kn, kn_tqe); 1914 kn->kn_status &= ~KN_QUEUED; 1915 kq->kq_count--; 1916 } 1917 1918 static void 1919 knote_init(void) 1920 { 1921 1922 knote_zone = uma_zcreate("KNOTE", sizeof(struct knote), NULL, NULL, 1923 NULL, NULL, UMA_ALIGN_PTR, 0); 1924 } 1925 SYSINIT(knote, SI_SUB_PSEUDO, SI_ORDER_ANY, knote_init, NULL) 1926 1927 static struct knote * 1928 knote_alloc(int waitok) 1929 { 1930 return ((struct knote *)uma_zalloc(knote_zone, 1931 (waitok ? M_WAITOK : M_NOWAIT)|M_ZERO)); 1932 } 1933 1934 static void 1935 knote_free(struct knote *kn) 1936 { 1937 if (kn != NULL) 1938 uma_zfree(knote_zone, kn); 1939 } 1940 1941 /* 1942 * Register the kev w/ the kq specified by fd. 1943 */ 1944 int 1945 kqfd_register(int fd, struct kevent *kev, struct thread *td, int waitok) 1946 { 1947 struct kqueue *kq; 1948 struct file *fp; 1949 int error; 1950 1951 if ((error = fget(td, fd, &fp)) != 0) 1952 return (error); 1953 if ((error = kqueue_acquire(fp, &kq)) != 0) 1954 goto noacquire; 1955 1956 error = kqueue_register(kq, kev, td, waitok); 1957 1958 kqueue_release(kq, 0); 1959 1960 noacquire: 1961 fdrop(fp, td); 1962 1963 return error; 1964 } 1965