1 /* $OpenBSD: kern_synch.c,v 1.171 2020/10/23 20:28:09 cheloha Exp $ */ 2 /* $NetBSD: kern_synch.c,v 1.37 1996/04/22 01:38:37 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1990, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)kern_synch.c 8.6 (Berkeley) 1/21/94 38 */ 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/proc.h> 43 #include <sys/kernel.h> 44 #include <sys/signalvar.h> 45 #include <sys/resourcevar.h> 46 #include <sys/sched.h> 47 #include <sys/timeout.h> 48 #include <sys/mount.h> 49 #include <sys/syscallargs.h> 50 #include <sys/pool.h> 51 #include <sys/refcnt.h> 52 #include <sys/atomic.h> 53 #include <sys/witness.h> 54 #include <sys/tracepoint.h> 55 56 #include <ddb/db_output.h> 57 58 #include <machine/spinlock.h> 59 60 #ifdef DIAGNOSTIC 61 #include <sys/syslog.h> 62 #endif 63 64 #ifdef KTRACE 65 #include <sys/ktrace.h> 66 #endif 67 68 int thrsleep(struct proc *, struct sys___thrsleep_args *); 69 int thrsleep_unlock(void *); 70 71 /* 72 * We're only looking at 7 bits of the address; everything is 73 * aligned to 4, lots of things are aligned to greater powers 74 * of 2. Shift right by 8, i.e. drop the bottom 256 worth. 75 */ 76 #define TABLESIZE 128 77 #define LOOKUP(x) (((long)(x) >> 8) & (TABLESIZE - 1)) 78 TAILQ_HEAD(slpque,proc) slpque[TABLESIZE]; 79 80 void 81 sleep_queue_init(void) 82 { 83 int i; 84 85 for (i = 0; i < TABLESIZE; i++) 86 TAILQ_INIT(&slpque[i]); 87 } 88 89 90 /* 91 * During autoconfiguration or after a panic, a sleep will simply 92 * lower the priority briefly to allow interrupts, then return. 93 * The priority to be used (safepri) is machine-dependent, thus this 94 * value is initialized and maintained in the machine-dependent layers. 95 * This priority will typically be 0, or the lowest priority 96 * that is safe for use on the interrupt stack; it can be made 97 * higher to block network software interrupts after panics. 98 */ 99 extern int safepri; 100 101 /* 102 * General sleep call. Suspends the current process until a wakeup is 103 * performed on the specified identifier. The process will then be made 104 * runnable with the specified priority. Sleeps at most timo/hz seconds 105 * (0 means no timeout). If pri includes PCATCH flag, signals are checked 106 * before and after sleeping, else signals are not checked. Returns 0 if 107 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a 108 * signal needs to be delivered, ERESTART is returned if the current system 109 * call should be restarted if possible, and EINTR is returned if the system 110 * call should be interrupted by the signal (return EINTR). 111 */ 112 int 113 tsleep(const volatile void *ident, int priority, const char *wmesg, int timo) 114 { 115 struct sleep_state sls; 116 #ifdef MULTIPROCESSOR 117 int hold_count; 118 #endif 119 120 KASSERT((priority & ~(PRIMASK | PCATCH)) == 0); 121 122 #ifdef MULTIPROCESSOR 123 KASSERT(timo || _kernel_lock_held()); 124 #endif 125 126 #ifdef DDB 127 if (cold == 2) 128 db_stack_dump(); 129 #endif 130 if (cold || panicstr) { 131 int s; 132 /* 133 * After a panic, or during autoconfiguration, 134 * just give interrupts a chance, then just return; 135 * don't run any other procs or panic below, 136 * in case this is the idle process and already asleep. 137 */ 138 s = splhigh(); 139 splx(safepri); 140 #ifdef MULTIPROCESSOR 141 if (_kernel_lock_held()) { 142 hold_count = __mp_release_all(&kernel_lock); 143 __mp_acquire_count(&kernel_lock, hold_count); 144 } 145 #endif 146 splx(s); 147 return (0); 148 } 149 150 sleep_setup(&sls, ident, priority, wmesg); 151 sleep_setup_timeout(&sls, timo); 152 sleep_setup_signal(&sls); 153 154 return sleep_finish_all(&sls, 1); 155 } 156 157 int 158 tsleep_nsec(const volatile void *ident, int priority, const char *wmesg, 159 uint64_t nsecs) 160 { 161 uint64_t to_ticks; 162 163 if (nsecs == INFSLP) 164 return tsleep(ident, priority, wmesg, 0); 165 #ifdef DIAGNOSTIC 166 if (nsecs == 0) { 167 log(LOG_WARNING, 168 "%s: %s[%d]: %s: trying to sleep zero nanoseconds\n", 169 __func__, curproc->p_p->ps_comm, curproc->p_p->ps_pid, 170 wmesg); 171 } 172 #endif 173 /* 174 * We want to sleep at least nsecs nanoseconds worth of ticks. 175 * 176 * - Clamp nsecs to prevent arithmetic overflow. 177 * 178 * - Round nsecs up to account for any nanoseconds that do not 179 * divide evenly into tick_nsec, otherwise we'll lose them to 180 * integer division in the next step. We add (tick_nsec - 1) 181 * to keep from introducing a spurious tick if there are no 182 * such nanoseconds, i.e. nsecs % tick_nsec == 0. 183 * 184 * - Divide the rounded value to a count of ticks. We divide 185 * by (tick_nsec + 1) to discard the extra tick introduced if, 186 * before rounding, nsecs % tick_nsec == 1. 187 * 188 * - Finally, add a tick to the result. We need to wait out 189 * the current tick before we can begin counting our interval, 190 * as we do not know how much time has elapsed since the 191 * current tick began. 192 */ 193 nsecs = MIN(nsecs, UINT64_MAX - tick_nsec); 194 to_ticks = (nsecs + tick_nsec - 1) / (tick_nsec + 1) + 1; 195 if (to_ticks > INT_MAX) 196 to_ticks = INT_MAX; 197 return tsleep(ident, priority, wmesg, (int)to_ticks); 198 } 199 200 /* 201 * Same as tsleep, but if we have a mutex provided, then once we've 202 * entered the sleep queue we drop the mutex. After sleeping we re-lock. 203 */ 204 int 205 msleep(const volatile void *ident, struct mutex *mtx, int priority, 206 const char *wmesg, int timo) 207 { 208 struct sleep_state sls; 209 int error, spl; 210 #ifdef MULTIPROCESSOR 211 int hold_count; 212 #endif 213 214 KASSERT((priority & ~(PRIMASK | PCATCH | PNORELOCK)) == 0); 215 KASSERT(mtx != NULL); 216 217 if (priority & PCATCH) 218 KERNEL_ASSERT_LOCKED(); 219 220 if (cold || panicstr) { 221 /* 222 * After a panic, or during autoconfiguration, 223 * just give interrupts a chance, then just return; 224 * don't run any other procs or panic below, 225 * in case this is the idle process and already asleep. 226 */ 227 spl = MUTEX_OLDIPL(mtx); 228 MUTEX_OLDIPL(mtx) = safepri; 229 mtx_leave(mtx); 230 #ifdef MULTIPROCESSOR 231 if (_kernel_lock_held()) { 232 hold_count = __mp_release_all(&kernel_lock); 233 __mp_acquire_count(&kernel_lock, hold_count); 234 } 235 #endif 236 if ((priority & PNORELOCK) == 0) { 237 mtx_enter(mtx); 238 MUTEX_OLDIPL(mtx) = spl; 239 } else 240 splx(spl); 241 return (0); 242 } 243 244 sleep_setup(&sls, ident, priority, wmesg); 245 sleep_setup_timeout(&sls, timo); 246 247 /* XXX - We need to make sure that the mutex doesn't 248 * unblock splsched. This can be made a bit more 249 * correct when the sched_lock is a mutex. 250 */ 251 spl = MUTEX_OLDIPL(mtx); 252 MUTEX_OLDIPL(mtx) = splsched(); 253 mtx_leave(mtx); 254 /* signal may stop the process, release mutex before that */ 255 sleep_setup_signal(&sls); 256 257 error = sleep_finish_all(&sls, 1); 258 259 if ((priority & PNORELOCK) == 0) { 260 mtx_enter(mtx); 261 MUTEX_OLDIPL(mtx) = spl; /* put the ipl back */ 262 } else 263 splx(spl); 264 265 return error; 266 } 267 268 int 269 msleep_nsec(const volatile void *ident, struct mutex *mtx, int priority, 270 const char *wmesg, uint64_t nsecs) 271 { 272 uint64_t to_ticks; 273 274 if (nsecs == INFSLP) 275 return msleep(ident, mtx, priority, wmesg, 0); 276 #ifdef DIAGNOSTIC 277 if (nsecs == 0) { 278 log(LOG_WARNING, 279 "%s: %s[%d]: %s: trying to sleep zero nanoseconds\n", 280 __func__, curproc->p_p->ps_comm, curproc->p_p->ps_pid, 281 wmesg); 282 } 283 #endif 284 nsecs = MIN(nsecs, UINT64_MAX - tick_nsec); 285 to_ticks = (nsecs + tick_nsec - 1) / (tick_nsec + 1) + 1; 286 if (to_ticks > INT_MAX) 287 to_ticks = INT_MAX; 288 return msleep(ident, mtx, priority, wmesg, (int)to_ticks); 289 } 290 291 /* 292 * Same as tsleep, but if we have a rwlock provided, then once we've 293 * entered the sleep queue we drop the it. After sleeping we re-lock. 294 */ 295 int 296 rwsleep(const volatile void *ident, struct rwlock *rwl, int priority, 297 const char *wmesg, int timo) 298 { 299 struct sleep_state sls; 300 int error, status; 301 302 KASSERT((priority & ~(PRIMASK | PCATCH | PNORELOCK)) == 0); 303 rw_assert_anylock(rwl); 304 status = rw_status(rwl); 305 306 sleep_setup(&sls, ident, priority, wmesg); 307 sleep_setup_timeout(&sls, timo); 308 309 rw_exit(rwl); 310 /* signal may stop the process, release rwlock before that */ 311 sleep_setup_signal(&sls); 312 313 error = sleep_finish_all(&sls, 1); 314 315 if ((priority & PNORELOCK) == 0) 316 rw_enter(rwl, status); 317 318 return error; 319 } 320 321 int 322 rwsleep_nsec(const volatile void *ident, struct rwlock *rwl, int priority, 323 const char *wmesg, uint64_t nsecs) 324 { 325 uint64_t to_ticks; 326 327 if (nsecs == INFSLP) 328 return rwsleep(ident, rwl, priority, wmesg, 0); 329 #ifdef DIAGNOSTIC 330 if (nsecs == 0) { 331 log(LOG_WARNING, 332 "%s: %s[%d]: %s: trying to sleep zero nanoseconds\n", 333 __func__, curproc->p_p->ps_comm, curproc->p_p->ps_pid, 334 wmesg); 335 } 336 #endif 337 nsecs = MIN(nsecs, UINT64_MAX - tick_nsec); 338 to_ticks = (nsecs + tick_nsec - 1) / (tick_nsec + 1) + 1; 339 if (to_ticks > INT_MAX) 340 to_ticks = INT_MAX; 341 return rwsleep(ident, rwl, priority, wmesg, (int)to_ticks); 342 } 343 344 void 345 sleep_setup(struct sleep_state *sls, const volatile void *ident, int prio, 346 const char *wmesg) 347 { 348 struct proc *p = curproc; 349 350 #ifdef DIAGNOSTIC 351 if (p->p_flag & P_CANTSLEEP) 352 panic("sleep: %s failed insomnia", p->p_p->ps_comm); 353 if (ident == NULL) 354 panic("tsleep: no ident"); 355 if (p->p_stat != SONPROC) 356 panic("tsleep: not SONPROC"); 357 #endif 358 359 sls->sls_catch = prio & PCATCH; 360 sls->sls_do_sleep = 1; 361 sls->sls_locked = 0; 362 sls->sls_sig = 0; 363 sls->sls_unwind = 0; 364 sls->sls_timeout = 0; 365 366 /* 367 * The kernel has to be locked for signal processing. 368 * This is done here and not in sleep_setup_signal() because 369 * KERNEL_LOCK() has to be taken before SCHED_LOCK(). 370 */ 371 if (sls->sls_catch != 0) { 372 KERNEL_LOCK(); 373 sls->sls_locked = 1; 374 } 375 376 SCHED_LOCK(sls->sls_s); 377 378 TRACEPOINT(sched, sleep, NULL); 379 380 p->p_wchan = ident; 381 p->p_wmesg = wmesg; 382 p->p_slptime = 0; 383 p->p_slppri = prio & PRIMASK; 384 TAILQ_INSERT_TAIL(&slpque[LOOKUP(ident)], p, p_runq); 385 } 386 387 int 388 sleep_finish_all(struct sleep_state *sls, int do_sleep) 389 { 390 int error, error1; 391 392 sleep_finish(sls, do_sleep); 393 error1 = sleep_finish_timeout(sls); 394 error = sleep_finish_signal(sls); 395 396 /* Signal errors are higher priority than timeouts. */ 397 if (error == 0 && error1 != 0) 398 error = error1; 399 400 return error; 401 } 402 403 void 404 sleep_finish(struct sleep_state *sls, int do_sleep) 405 { 406 struct proc *p = curproc; 407 408 if (sls->sls_do_sleep && do_sleep) { 409 p->p_stat = SSLEEP; 410 p->p_ru.ru_nvcsw++; 411 SCHED_ASSERT_LOCKED(); 412 mi_switch(); 413 } else if (!do_sleep) { 414 unsleep(p); 415 } 416 417 #ifdef DIAGNOSTIC 418 if (p->p_stat != SONPROC) 419 panic("sleep_finish !SONPROC"); 420 #endif 421 422 p->p_cpu->ci_schedstate.spc_curpriority = p->p_usrpri; 423 SCHED_UNLOCK(sls->sls_s); 424 425 /* 426 * Even though this belongs to the signal handling part of sleep, 427 * we need to clear it before the ktrace. 428 */ 429 atomic_clearbits_int(&p->p_flag, P_SINTR); 430 } 431 432 void 433 sleep_setup_timeout(struct sleep_state *sls, int timo) 434 { 435 struct proc *p = curproc; 436 437 KASSERT((p->p_flag & P_TIMEOUT) == 0); 438 439 if (timo) { 440 sls->sls_timeout = 1; 441 timeout_add(&p->p_sleep_to, timo); 442 } 443 } 444 445 int 446 sleep_finish_timeout(struct sleep_state *sls) 447 { 448 struct proc *p = curproc; 449 450 if (sls->sls_timeout) { 451 if (p->p_flag & P_TIMEOUT) { 452 atomic_clearbits_int(&p->p_flag, P_TIMEOUT); 453 return (EWOULDBLOCK); 454 } else { 455 /* This must not sleep. */ 456 timeout_del_barrier(&p->p_sleep_to); 457 KASSERT((p->p_flag & P_TIMEOUT) == 0); 458 } 459 } 460 461 return (0); 462 } 463 464 void 465 sleep_setup_signal(struct sleep_state *sls) 466 { 467 struct proc *p = curproc; 468 469 if (sls->sls_catch == 0) 470 return; 471 472 /* sleep_setup() has locked the kernel. */ 473 KERNEL_ASSERT_LOCKED(); 474 475 /* 476 * We put ourselves on the sleep queue and start our timeout before 477 * calling single_thread_check or CURSIG, as we could stop there, and 478 * a wakeup or a SIGCONT (or both) could occur while we were stopped. 479 * A SIGCONT would cause us to be marked as SSLEEP without resuming us, 480 * thus we must be ready for sleep when CURSIG is called. If the 481 * wakeup happens while we're stopped, p->p_wchan will be 0 upon 482 * return from single_thread_check or CURSIG. In that case we should 483 * not go to sleep. If single_thread_check returns an error we need 484 * to unwind immediately. That's achieved by saving the return value 485 * in sls->sl_unwind and checking it later in sleep_finish_signal. 486 */ 487 atomic_setbits_int(&p->p_flag, P_SINTR); 488 if ((sls->sls_unwind = single_thread_check(p, 1)) != 0 || 489 (sls->sls_sig = CURSIG(p)) != 0) { 490 unsleep(p); 491 p->p_stat = SONPROC; 492 sls->sls_do_sleep = 0; 493 } else if (p->p_wchan == 0) { 494 sls->sls_catch = 0; 495 sls->sls_do_sleep = 0; 496 } 497 } 498 499 int 500 sleep_finish_signal(struct sleep_state *sls) 501 { 502 struct proc *p = curproc; 503 int error = 0; 504 505 if (sls->sls_catch != 0) { 506 KERNEL_ASSERT_LOCKED(); 507 508 if (sls->sls_unwind != 0 || 509 (sls->sls_unwind = single_thread_check(p, 1)) != 0) 510 error = sls->sls_unwind; 511 else if (sls->sls_sig != 0 || 512 (sls->sls_sig = CURSIG(p)) != 0) { 513 if (p->p_p->ps_sigacts->ps_sigintr & 514 sigmask(sls->sls_sig)) 515 error = EINTR; 516 else 517 error = ERESTART; 518 } 519 } 520 521 if (sls->sls_locked) 522 KERNEL_UNLOCK(); 523 524 return (error); 525 } 526 527 int 528 wakeup_proc(struct proc *p, const volatile void *chan) 529 { 530 int s, awakened = 0; 531 532 SCHED_LOCK(s); 533 if (p->p_wchan != NULL && 534 ((chan == NULL) || (p->p_wchan == chan))) { 535 awakened = 1; 536 if (p->p_stat == SSLEEP) 537 setrunnable(p); 538 else 539 unsleep(p); 540 } 541 SCHED_UNLOCK(s); 542 543 return awakened; 544 } 545 546 /* 547 * Implement timeout for tsleep. 548 * If process hasn't been awakened (wchan non-zero), 549 * set timeout flag and undo the sleep. If proc 550 * is stopped, just unsleep so it will remain stopped. 551 */ 552 void 553 endtsleep(void *arg) 554 { 555 struct proc *p = arg; 556 int s; 557 558 SCHED_LOCK(s); 559 if (wakeup_proc(p, NULL)) 560 atomic_setbits_int(&p->p_flag, P_TIMEOUT); 561 SCHED_UNLOCK(s); 562 } 563 564 /* 565 * Remove a process from its wait queue 566 */ 567 void 568 unsleep(struct proc *p) 569 { 570 SCHED_ASSERT_LOCKED(); 571 572 if (p->p_wchan != NULL) { 573 TAILQ_REMOVE(&slpque[LOOKUP(p->p_wchan)], p, p_runq); 574 p->p_wchan = NULL; 575 TRACEPOINT(sched, wakeup, p->p_tid, p->p_p->ps_pid); 576 } 577 } 578 579 /* 580 * Make a number of processes sleeping on the specified identifier runnable. 581 */ 582 void 583 wakeup_n(const volatile void *ident, int n) 584 { 585 struct slpque *qp; 586 struct proc *p; 587 struct proc *pnext; 588 int s; 589 590 SCHED_LOCK(s); 591 qp = &slpque[LOOKUP(ident)]; 592 for (p = TAILQ_FIRST(qp); p != NULL && n != 0; p = pnext) { 593 pnext = TAILQ_NEXT(p, p_runq); 594 #ifdef DIAGNOSTIC 595 /* 596 * If the rwlock passed to rwsleep() is contended, the 597 * CPU will end up calling wakeup() between sleep_setup() 598 * and sleep_finish(). 599 */ 600 if (p == curproc) { 601 KASSERT(p->p_stat == SONPROC); 602 continue; 603 } 604 if (p->p_stat != SSLEEP && p->p_stat != SSTOP) 605 panic("wakeup: p_stat is %d", (int)p->p_stat); 606 #endif 607 if (wakeup_proc(p, ident)) 608 --n; 609 } 610 SCHED_UNLOCK(s); 611 } 612 613 /* 614 * Make all processes sleeping on the specified identifier runnable. 615 */ 616 void 617 wakeup(const volatile void *chan) 618 { 619 wakeup_n(chan, -1); 620 } 621 622 int 623 sys_sched_yield(struct proc *p, void *v, register_t *retval) 624 { 625 struct proc *q; 626 uint8_t newprio; 627 int s; 628 629 SCHED_LOCK(s); 630 /* 631 * If one of the threads of a multi-threaded process called 632 * sched_yield(2), drop its priority to ensure its siblings 633 * can make some progress. 634 */ 635 newprio = p->p_usrpri; 636 TAILQ_FOREACH(q, &p->p_p->ps_threads, p_thr_link) 637 newprio = max(newprio, q->p_runpri); 638 setrunqueue(p->p_cpu, p, newprio); 639 p->p_ru.ru_nvcsw++; 640 mi_switch(); 641 SCHED_UNLOCK(s); 642 643 return (0); 644 } 645 646 int 647 thrsleep_unlock(void *lock) 648 { 649 static _atomic_lock_t unlocked = _ATOMIC_LOCK_UNLOCKED; 650 _atomic_lock_t *atomiclock = lock; 651 652 if (!lock) 653 return 0; 654 655 return copyout(&unlocked, atomiclock, sizeof(unlocked)); 656 } 657 658 struct tslpentry { 659 TAILQ_ENTRY(tslpentry) tslp_link; 660 long tslp_ident; 661 }; 662 663 /* thrsleep queue shared between processes */ 664 static struct tslpqueue thrsleep_queue = TAILQ_HEAD_INITIALIZER(thrsleep_queue); 665 static struct rwlock thrsleep_lock = RWLOCK_INITIALIZER("thrsleeplk"); 666 667 int 668 thrsleep(struct proc *p, struct sys___thrsleep_args *v) 669 { 670 struct sys___thrsleep_args /* { 671 syscallarg(const volatile void *) ident; 672 syscallarg(clockid_t) clock_id; 673 syscallarg(const struct timespec *) tp; 674 syscallarg(void *) lock; 675 syscallarg(const int *) abort; 676 } */ *uap = v; 677 long ident = (long)SCARG(uap, ident); 678 struct tslpentry entry; 679 struct tslpqueue *queue; 680 struct rwlock *qlock; 681 struct timespec *tsp = (struct timespec *)SCARG(uap, tp); 682 void *lock = SCARG(uap, lock); 683 uint64_t nsecs = INFSLP; 684 int abort = 0, error; 685 clockid_t clock_id = SCARG(uap, clock_id); 686 687 if (ident == 0) 688 return (EINVAL); 689 if (tsp != NULL) { 690 struct timespec now; 691 692 if ((error = clock_gettime(p, clock_id, &now))) 693 return (error); 694 #ifdef KTRACE 695 if (KTRPOINT(p, KTR_STRUCT)) 696 ktrabstimespec(p, tsp); 697 #endif 698 699 if (timespeccmp(tsp, &now, <=)) { 700 /* already passed: still do the unlock */ 701 if ((error = thrsleep_unlock(lock))) 702 return (error); 703 return (EWOULDBLOCK); 704 } 705 706 timespecsub(tsp, &now, tsp); 707 nsecs = MIN(TIMESPEC_TO_NSEC(tsp), MAXTSLP); 708 } 709 710 if (ident == -1) { 711 queue = &thrsleep_queue; 712 qlock = &thrsleep_lock; 713 } else { 714 queue = &p->p_p->ps_tslpqueue; 715 qlock = &p->p_p->ps_lock; 716 } 717 718 /* Interlock with wakeup. */ 719 entry.tslp_ident = ident; 720 rw_enter_write(qlock); 721 TAILQ_INSERT_TAIL(queue, &entry, tslp_link); 722 rw_exit_write(qlock); 723 724 error = thrsleep_unlock(lock); 725 726 if (error == 0 && SCARG(uap, abort) != NULL) 727 error = copyin(SCARG(uap, abort), &abort, sizeof(abort)); 728 729 rw_enter_write(qlock); 730 if (error != 0) 731 goto out; 732 if (abort != 0) { 733 error = EINTR; 734 goto out; 735 } 736 if (entry.tslp_ident != 0) { 737 error = rwsleep_nsec(&entry, qlock, PWAIT|PCATCH, "thrsleep", 738 nsecs); 739 } 740 741 out: 742 if (entry.tslp_ident != 0) 743 TAILQ_REMOVE(queue, &entry, tslp_link); 744 rw_exit_write(qlock); 745 746 if (error == ERESTART) 747 error = ECANCELED; 748 749 return (error); 750 751 } 752 753 int 754 sys___thrsleep(struct proc *p, void *v, register_t *retval) 755 { 756 struct sys___thrsleep_args /* { 757 syscallarg(const volatile void *) ident; 758 syscallarg(clockid_t) clock_id; 759 syscallarg(struct timespec *) tp; 760 syscallarg(void *) lock; 761 syscallarg(const int *) abort; 762 } */ *uap = v; 763 struct timespec ts; 764 int error; 765 766 if (SCARG(uap, tp) != NULL) { 767 if ((error = copyin(SCARG(uap, tp), &ts, sizeof(ts)))) { 768 *retval = error; 769 return 0; 770 } 771 if (!timespecisvalid(&ts)) { 772 *retval = EINVAL; 773 return 0; 774 } 775 SCARG(uap, tp) = &ts; 776 } 777 778 *retval = thrsleep(p, uap); 779 return 0; 780 } 781 782 int 783 sys___thrwakeup(struct proc *p, void *v, register_t *retval) 784 { 785 struct sys___thrwakeup_args /* { 786 syscallarg(const volatile void *) ident; 787 syscallarg(int) n; 788 } */ *uap = v; 789 struct tslpentry *entry, *tmp; 790 struct tslpqueue *queue; 791 struct rwlock *qlock; 792 long ident = (long)SCARG(uap, ident); 793 int n = SCARG(uap, n); 794 int found = 0; 795 796 if (ident == 0) 797 *retval = EINVAL; 798 else { 799 if (ident == -1) { 800 queue = &thrsleep_queue; 801 qlock = &thrsleep_lock; 802 /* 803 * Wake up all waiters with ident -1. This is needed 804 * because ident -1 can be shared by multiple userspace 805 * lock state machines concurrently. The implementation 806 * has no way to direct the wakeup to a particular 807 * state machine. 808 */ 809 n = 0; 810 } else { 811 queue = &p->p_p->ps_tslpqueue; 812 qlock = &p->p_p->ps_lock; 813 } 814 815 rw_enter_write(qlock); 816 TAILQ_FOREACH_SAFE(entry, queue, tslp_link, tmp) { 817 if (entry->tslp_ident == ident) { 818 TAILQ_REMOVE(queue, entry, tslp_link); 819 entry->tslp_ident = 0; 820 wakeup_one(entry); 821 if (++found == n) 822 break; 823 } 824 } 825 rw_exit_write(qlock); 826 827 if (ident == -1) 828 *retval = 0; 829 else 830 *retval = found ? 0 : ESRCH; 831 } 832 833 return (0); 834 } 835 836 void 837 refcnt_init(struct refcnt *r) 838 { 839 r->refs = 1; 840 } 841 842 void 843 refcnt_take(struct refcnt *r) 844 { 845 #ifdef DIAGNOSTIC 846 u_int refcnt; 847 848 refcnt = atomic_inc_int_nv(&r->refs); 849 KASSERT(refcnt != 0); 850 #else 851 atomic_inc_int(&r->refs); 852 #endif 853 } 854 855 int 856 refcnt_rele(struct refcnt *r) 857 { 858 u_int refcnt; 859 860 refcnt = atomic_dec_int_nv(&r->refs); 861 KASSERT(refcnt != ~0); 862 863 return (refcnt == 0); 864 } 865 866 void 867 refcnt_rele_wake(struct refcnt *r) 868 { 869 if (refcnt_rele(r)) 870 wakeup_one(r); 871 } 872 873 void 874 refcnt_finalize(struct refcnt *r, const char *wmesg) 875 { 876 struct sleep_state sls; 877 u_int refcnt; 878 879 refcnt = atomic_dec_int_nv(&r->refs); 880 while (refcnt) { 881 sleep_setup(&sls, r, PWAIT, wmesg); 882 refcnt = r->refs; 883 sleep_finish(&sls, refcnt); 884 } 885 } 886 887 void 888 cond_init(struct cond *c) 889 { 890 c->c_wait = 1; 891 } 892 893 void 894 cond_signal(struct cond *c) 895 { 896 c->c_wait = 0; 897 898 wakeup_one(c); 899 } 900 901 void 902 cond_wait(struct cond *c, const char *wmesg) 903 { 904 struct sleep_state sls; 905 int wait; 906 907 wait = c->c_wait; 908 while (wait) { 909 sleep_setup(&sls, c, PWAIT, wmesg); 910 wait = c->c_wait; 911 sleep_finish(&sls, wait); 912 } 913 } 914