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