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