xref: /freebsd/lib/libthr/thread/thr_mutex.c (revision 6c05f3a74f30934ee60919cc97e16ec69b542b06)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
5  * Copyright (c) 2006 David Xu <davidxu@freebsd.org>.
6  * Copyright (c) 2015, 2016 The FreeBSD Foundation
7  *
8  * All rights reserved.
9  *
10  * Portions of this software were developed by Konstantin Belousov
11  * under sponsorship from the FreeBSD Foundation.
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. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by John Birrell.
24  * 4. Neither the name of the author nor the names of any co-contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 #include "namespace.h"
42 #include <stdlib.h>
43 #include <errno.h>
44 #include <string.h>
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <pthread.h>
48 #include <pthread_np.h>
49 #include "un-namespace.h"
50 
51 #include "thr_private.h"
52 
53 _Static_assert(sizeof(struct pthread_mutex) <= THR_PAGE_SIZE_MIN,
54     "pthread_mutex is too large for off-page");
55 
56 /*
57  * For adaptive mutexes, how many times to spin doing trylock2
58  * before entering the kernel to block
59  */
60 #define MUTEX_ADAPTIVE_SPINS	2000
61 
62 /*
63  * Prototypes
64  */
65 int	__pthread_mutex_timedlock(pthread_mutex_t * __restrict mutex,
66 		const struct timespec * __restrict abstime);
67 int	_pthread_mutex_getspinloops_np(pthread_mutex_t *mutex, int *count);
68 int	_pthread_mutex_setspinloops_np(pthread_mutex_t *mutex, int count);
69 int	__pthread_mutex_setspinloops_np(pthread_mutex_t *mutex, int count);
70 int	_pthread_mutex_setyieldloops_np(pthread_mutex_t *mutex, int count);
71 int	_pthread_mutex_getyieldloops_np(pthread_mutex_t *mutex, int *count);
72 int	__pthread_mutex_setyieldloops_np(pthread_mutex_t *mutex, int count);
73 
74 static int	mutex_self_trylock(pthread_mutex_t);
75 static int	mutex_self_lock(pthread_mutex_t,
76 				const struct timespec *abstime);
77 static int	mutex_unlock_common(struct pthread_mutex *, bool, int *);
78 static int	mutex_lock_sleep(struct pthread *, pthread_mutex_t,
79 				const struct timespec *);
80 static void	mutex_init_robust(struct pthread *curthread);
81 static int	mutex_qidx(struct pthread_mutex *m);
82 static bool	is_robust_mutex(struct pthread_mutex *m);
83 static bool	is_pshared_mutex(struct pthread_mutex *m);
84 
85 __weak_reference(__Tthr_mutex_init, pthread_mutex_init);
86 __weak_reference(__Tthr_mutex_init, __pthread_mutex_init);
87 __strong_reference(__Tthr_mutex_init, _pthread_mutex_init);
88 __weak_reference(__Tthr_mutex_lock, pthread_mutex_lock);
89 __weak_reference(__Tthr_mutex_lock, __pthread_mutex_lock);
90 __strong_reference(__Tthr_mutex_lock, _pthread_mutex_lock);
91 __weak_reference(__pthread_mutex_timedlock, pthread_mutex_timedlock);
92 __strong_reference(__pthread_mutex_timedlock, _pthread_mutex_timedlock);
93 __weak_reference(__Tthr_mutex_trylock, pthread_mutex_trylock);
94 __weak_reference(__Tthr_mutex_trylock, __pthread_mutex_trylock);
95 __strong_reference(__Tthr_mutex_trylock, _pthread_mutex_trylock);
96 __weak_reference(_Tthr_mutex_consistent, pthread_mutex_consistent);
97 __weak_reference(_Tthr_mutex_consistent, _pthread_mutex_consistent);
98 __strong_reference(_Tthr_mutex_consistent, __pthread_mutex_consistent);
99 
100 /* Single underscore versions provided for libc internal usage: */
101 /* No difference between libc and application usage of these: */
102 __weak_reference(_thr_mutex_destroy, pthread_mutex_destroy);
103 __weak_reference(_thr_mutex_destroy, _pthread_mutex_destroy);
104 __weak_reference(_thr_mutex_unlock, pthread_mutex_unlock);
105 __weak_reference(_thr_mutex_unlock, _pthread_mutex_unlock);
106 
107 __weak_reference(_pthread_mutex_getprioceiling, pthread_mutex_getprioceiling);
108 __weak_reference(_pthread_mutex_setprioceiling, pthread_mutex_setprioceiling);
109 
110 __weak_reference(__pthread_mutex_setspinloops_np, pthread_mutex_setspinloops_np);
111 __strong_reference(__pthread_mutex_setspinloops_np, _pthread_mutex_setspinloops_np);
112 __weak_reference(_pthread_mutex_getspinloops_np, pthread_mutex_getspinloops_np);
113 
114 __weak_reference(__pthread_mutex_setyieldloops_np, pthread_mutex_setyieldloops_np);
115 __strong_reference(__pthread_mutex_setyieldloops_np, _pthread_mutex_setyieldloops_np);
116 __weak_reference(_pthread_mutex_getyieldloops_np, pthread_mutex_getyieldloops_np);
117 __weak_reference(_pthread_mutex_isowned_np, pthread_mutex_isowned_np);
118 
119 static void
120 mutex_init_link(struct pthread_mutex *m __unused)
121 {
122 
123 #if defined(_PTHREADS_INVARIANTS)
124 	m->m_qe.tqe_prev = NULL;
125 	m->m_qe.tqe_next = NULL;
126 	m->m_pqe.tqe_prev = NULL;
127 	m->m_pqe.tqe_next = NULL;
128 #endif
129 }
130 
131 static void
132 mutex_assert_is_owned(struct pthread_mutex *m __unused)
133 {
134 
135 #if defined(_PTHREADS_INVARIANTS)
136 	if (__predict_false(m->m_qe.tqe_prev == NULL))
137 		PANIC("mutex %p own %#x is not on list %p %p",
138 		    m, m->m_lock.m_owner, m->m_qe.tqe_prev, m->m_qe.tqe_next);
139 #endif
140 }
141 
142 static void
143 mutex_assert_not_owned(struct pthread *curthread __unused,
144     struct pthread_mutex *m __unused)
145 {
146 
147 #if defined(_PTHREADS_INVARIANTS)
148 	if (__predict_false(m->m_qe.tqe_prev != NULL ||
149 	    m->m_qe.tqe_next != NULL))
150 		PANIC("mutex %p own %#x is on list %p %p",
151 		    m, m->m_lock.m_owner, m->m_qe.tqe_prev, m->m_qe.tqe_next);
152 	if (__predict_false(is_robust_mutex(m) &&
153 	    (m->m_lock.m_rb_lnk != 0 || m->m_rb_prev != NULL ||
154 	    (is_pshared_mutex(m) && curthread->robust_list ==
155 	    (uintptr_t)&m->m_lock) ||
156 	    (!is_pshared_mutex(m) && curthread->priv_robust_list ==
157 	    (uintptr_t)&m->m_lock))))
158 		PANIC(
159     "mutex %p own %#x is on robust linkage %p %p head %p phead %p",
160 		    m, m->m_lock.m_owner, (void *)m->m_lock.m_rb_lnk,
161 		    m->m_rb_prev, (void *)curthread->robust_list,
162 		    (void *)curthread->priv_robust_list);
163 #endif
164 }
165 
166 static bool
167 is_pshared_mutex(struct pthread_mutex *m)
168 {
169 
170 	return ((m->m_lock.m_flags & USYNC_PROCESS_SHARED) != 0);
171 }
172 
173 static bool
174 is_robust_mutex(struct pthread_mutex *m)
175 {
176 
177 	return ((m->m_lock.m_flags & UMUTEX_ROBUST) != 0);
178 }
179 
180 int
181 _mutex_enter_robust(struct pthread *curthread, struct pthread_mutex *m)
182 {
183 
184 #if defined(_PTHREADS_INVARIANTS)
185 	if (__predict_false(curthread->inact_mtx != 0))
186 		PANIC("inact_mtx enter");
187 #endif
188 	if (!is_robust_mutex(m))
189 		return (0);
190 
191 	mutex_init_robust(curthread);
192 	curthread->inact_mtx = (uintptr_t)&m->m_lock;
193 	return (1);
194 }
195 
196 void
197 _mutex_leave_robust(struct pthread *curthread, struct pthread_mutex *m __unused)
198 {
199 
200 #if defined(_PTHREADS_INVARIANTS)
201 	if (__predict_false(curthread->inact_mtx != (uintptr_t)&m->m_lock))
202 		PANIC("inact_mtx leave");
203 #endif
204 	curthread->inact_mtx = 0;
205 }
206 
207 static int
208 mutex_check_attr(const struct pthread_mutex_attr *attr)
209 {
210 
211 	if (attr->m_type < PTHREAD_MUTEX_ERRORCHECK ||
212 	    attr->m_type >= PTHREAD_MUTEX_TYPE_MAX)
213 		return (EINVAL);
214 	if (attr->m_protocol < PTHREAD_PRIO_NONE ||
215 	    attr->m_protocol > PTHREAD_PRIO_PROTECT)
216 		return (EINVAL);
217 	return (0);
218 }
219 
220 static void
221 mutex_init_robust(struct pthread *curthread)
222 {
223 	struct umtx_robust_lists_params rb;
224 
225 	if (curthread == NULL)
226 		curthread = _get_curthread();
227 	if (curthread->robust_inited)
228 		return;
229 	rb.robust_list_offset = (uintptr_t)&curthread->robust_list;
230 	rb.robust_priv_list_offset = (uintptr_t)&curthread->priv_robust_list;
231 	rb.robust_inact_offset = (uintptr_t)&curthread->inact_mtx;
232 	_umtx_op(NULL, UMTX_OP_ROBUST_LISTS, sizeof(rb), &rb, NULL);
233 	curthread->robust_inited = 1;
234 }
235 
236 static void
237 mutex_init_body(struct pthread_mutex *pmutex,
238     const struct pthread_mutex_attr *attr)
239 {
240 
241 	pmutex->m_flags = attr->m_type;
242 	pmutex->m_count = 0;
243 	pmutex->m_spinloops = 0;
244 	pmutex->m_yieldloops = 0;
245 	mutex_init_link(pmutex);
246 	switch (attr->m_protocol) {
247 	case PTHREAD_PRIO_NONE:
248 		pmutex->m_lock.m_owner = UMUTEX_UNOWNED;
249 		pmutex->m_lock.m_flags = 0;
250 		break;
251 	case PTHREAD_PRIO_INHERIT:
252 		pmutex->m_lock.m_owner = UMUTEX_UNOWNED;
253 		pmutex->m_lock.m_flags = UMUTEX_PRIO_INHERIT;
254 		break;
255 	case PTHREAD_PRIO_PROTECT:
256 		pmutex->m_lock.m_owner = UMUTEX_CONTESTED;
257 		pmutex->m_lock.m_flags = UMUTEX_PRIO_PROTECT;
258 		pmutex->m_lock.m_ceilings[0] = attr->m_ceiling;
259 		break;
260 	}
261 	if (attr->m_pshared == PTHREAD_PROCESS_SHARED)
262 		pmutex->m_lock.m_flags |= USYNC_PROCESS_SHARED;
263 	if (attr->m_robust == PTHREAD_MUTEX_ROBUST) {
264 		mutex_init_robust(NULL);
265 		pmutex->m_lock.m_flags |= UMUTEX_ROBUST;
266 	}
267 	if (PMUTEX_TYPE(pmutex->m_flags) == PTHREAD_MUTEX_ADAPTIVE_NP) {
268 		pmutex->m_spinloops =
269 		    _thr_spinloops ? _thr_spinloops: MUTEX_ADAPTIVE_SPINS;
270 		pmutex->m_yieldloops = _thr_yieldloops;
271 	}
272 }
273 
274 static int
275 mutex_init(pthread_mutex_t *mutex,
276     const struct pthread_mutex_attr *mutex_attr,
277     void *(calloc_cb)(size_t, size_t))
278 {
279 	const struct pthread_mutex_attr *attr;
280 	struct pthread_mutex *pmutex;
281 	int error;
282 
283 	if (mutex_attr == NULL) {
284 		attr = &_pthread_mutexattr_default;
285 	} else {
286 		attr = mutex_attr;
287 		error = mutex_check_attr(attr);
288 		if (error != 0)
289 			return (error);
290 	}
291 	if ((pmutex = (pthread_mutex_t)calloc_cb(1,
292 	    sizeof(struct pthread_mutex))) == NULL)
293 		return (ENOMEM);
294 	mutex_init_body(pmutex, attr);
295 	*mutex = pmutex;
296 	return (0);
297 }
298 
299 static int
300 init_static(struct pthread *thread, pthread_mutex_t *mutex)
301 {
302 	int ret;
303 
304 	THR_LOCK_ACQUIRE(thread, &_mutex_static_lock);
305 
306 	if (*mutex == THR_MUTEX_INITIALIZER)
307 		ret = mutex_init(mutex, &_pthread_mutexattr_default,
308 		    __thr_calloc);
309 	else if (*mutex == THR_ADAPTIVE_MUTEX_INITIALIZER)
310 		ret = mutex_init(mutex, &_pthread_mutexattr_adaptive_default,
311 		    __thr_calloc);
312 	else
313 		ret = 0;
314 	THR_LOCK_RELEASE(thread, &_mutex_static_lock);
315 
316 	return (ret);
317 }
318 
319 static void
320 set_inherited_priority(struct pthread *curthread, struct pthread_mutex *m)
321 {
322 	struct pthread_mutex *m2;
323 
324 	m2 = TAILQ_LAST(&curthread->mq[mutex_qidx(m)], mutex_queue);
325 	if (m2 != NULL)
326 		m->m_lock.m_ceilings[1] = m2->m_lock.m_ceilings[0];
327 	else
328 		m->m_lock.m_ceilings[1] = -1;
329 }
330 
331 static void
332 shared_mutex_init(struct pthread_mutex *pmtx, const struct
333     pthread_mutex_attr *mutex_attr)
334 {
335 	static const struct pthread_mutex_attr foobar_mutex_attr = {
336 		.m_type = PTHREAD_MUTEX_DEFAULT,
337 		.m_protocol = PTHREAD_PRIO_NONE,
338 		.m_ceiling = 0,
339 		.m_pshared = PTHREAD_PROCESS_SHARED,
340 		.m_robust = PTHREAD_MUTEX_STALLED,
341 	};
342 	bool done;
343 
344 	/*
345 	 * Hack to allow multiple pthread_mutex_init() calls on the
346 	 * same process-shared mutex.  We rely on kernel allocating
347 	 * zeroed offpage for the mutex, i.e. the
348 	 * PMUTEX_INITSTAGE_ALLOC value must be zero.
349 	 */
350 	for (done = false; !done;) {
351 		switch (pmtx->m_ps) {
352 		case PMUTEX_INITSTAGE_DONE:
353 			atomic_thread_fence_acq();
354 			done = true;
355 			break;
356 		case PMUTEX_INITSTAGE_ALLOC:
357 			if (atomic_cmpset_int(&pmtx->m_ps,
358 			    PMUTEX_INITSTAGE_ALLOC, PMUTEX_INITSTAGE_BUSY)) {
359 				if (mutex_attr == NULL)
360 					mutex_attr = &foobar_mutex_attr;
361 				mutex_init_body(pmtx, mutex_attr);
362 				atomic_store_rel_int(&pmtx->m_ps,
363 				    PMUTEX_INITSTAGE_DONE);
364 				done = true;
365 			}
366 			break;
367 		case PMUTEX_INITSTAGE_BUSY:
368 			_pthread_yield();
369 			break;
370 		default:
371 			PANIC("corrupted offpage");
372 			break;
373 		}
374 	}
375 }
376 
377 int
378 __Tthr_mutex_init(pthread_mutex_t * __restrict mutex,
379     const pthread_mutexattr_t * __restrict mutex_attr)
380 {
381 	struct pthread_mutex *pmtx;
382 	int ret;
383 
384 	_thr_check_init();
385 
386 	if (mutex_attr != NULL) {
387 		ret = mutex_check_attr(*mutex_attr);
388 		if (ret != 0)
389 			return (ret);
390 	}
391 	if (mutex_attr == NULL ||
392 	    (*mutex_attr)->m_pshared == PTHREAD_PROCESS_PRIVATE) {
393 		__thr_malloc_init();
394 		return (mutex_init(mutex, mutex_attr ? *mutex_attr : NULL,
395 		    __thr_calloc));
396 	}
397 	pmtx = __thr_pshared_offpage(__DECONST(void *, mutex), 1);
398 	if (pmtx == NULL)
399 		return (EFAULT);
400 	*mutex = THR_PSHARED_PTR;
401 	shared_mutex_init(pmtx, *mutex_attr);
402 	return (0);
403 }
404 
405 /* This function is used internally by malloc. */
406 int
407 _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex,
408     void *(calloc_cb)(size_t, size_t))
409 {
410 	static const struct pthread_mutex_attr attr = {
411 		.m_type = PTHREAD_MUTEX_NORMAL,
412 		.m_protocol = PTHREAD_PRIO_NONE,
413 		.m_ceiling = 0,
414 		.m_pshared = PTHREAD_PROCESS_PRIVATE,
415 		.m_robust = PTHREAD_MUTEX_STALLED,
416 	};
417 	int ret;
418 
419 	ret = mutex_init(mutex, &attr, calloc_cb);
420 	if (ret == 0)
421 		(*mutex)->m_flags |= PMUTEX_FLAG_PRIVATE;
422 	return (ret);
423 }
424 
425 /*
426  * Fix mutex ownership for child process.
427  *
428  * Process private mutex ownership is transmitted from the forking
429  * thread to the child process.
430  *
431  * Process shared mutex should not be inherited because owner is
432  * forking thread which is in parent process, they are removed from
433  * the owned mutex list.
434  */
435 static void
436 queue_fork(struct pthread *curthread, struct mutex_queue *q,
437     struct mutex_queue *qp, uint bit)
438 {
439 	struct pthread_mutex *m;
440 
441 	TAILQ_INIT(q);
442 	TAILQ_FOREACH(m, qp, m_pqe) {
443 		TAILQ_INSERT_TAIL(q, m, m_qe);
444 		m->m_lock.m_owner = TID(curthread) | bit;
445 	}
446 }
447 
448 void
449 _mutex_fork(struct pthread *curthread)
450 {
451 
452 	queue_fork(curthread, &curthread->mq[TMQ_NORM],
453 	    &curthread->mq[TMQ_NORM_PRIV], 0);
454 	queue_fork(curthread, &curthread->mq[TMQ_NORM_PP],
455 	    &curthread->mq[TMQ_NORM_PP_PRIV], UMUTEX_CONTESTED);
456 	queue_fork(curthread, &curthread->mq[TMQ_ROBUST_PP],
457 	    &curthread->mq[TMQ_ROBUST_PP_PRIV], UMUTEX_CONTESTED);
458 	curthread->robust_list = 0;
459 }
460 
461 int
462 _thr_mutex_destroy(pthread_mutex_t *mutex)
463 {
464 	pthread_mutex_t m, m1;
465 	int ret;
466 
467 	m = *mutex;
468 	if (m < THR_MUTEX_DESTROYED) {
469 		ret = 0;
470 	} else if (m == THR_MUTEX_DESTROYED) {
471 		ret = EINVAL;
472 	} else {
473 		if (m == THR_PSHARED_PTR) {
474 			m1 = __thr_pshared_offpage(mutex, 0);
475 			if (m1 != NULL) {
476 				if ((uint32_t)m1->m_lock.m_owner !=
477 				    UMUTEX_RB_OWNERDEAD) {
478 					mutex_assert_not_owned(
479 					    _get_curthread(), m1);
480 				}
481 				__thr_pshared_destroy(mutex);
482 			}
483 			*mutex = THR_MUTEX_DESTROYED;
484 			return (0);
485 		}
486 		if (PMUTEX_OWNER_ID(m) != 0 &&
487 		    (uint32_t)m->m_lock.m_owner != UMUTEX_RB_NOTRECOV) {
488 			ret = EBUSY;
489 		} else {
490 			*mutex = THR_MUTEX_DESTROYED;
491 			mutex_assert_not_owned(_get_curthread(), m);
492 			__thr_free(m);
493 			ret = 0;
494 		}
495 	}
496 
497 	return (ret);
498 }
499 
500 static int
501 mutex_qidx(struct pthread_mutex *m)
502 {
503 
504 	if ((m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0)
505 		return (TMQ_NORM);
506 	return (is_robust_mutex(m) ? TMQ_ROBUST_PP : TMQ_NORM_PP);
507 }
508 
509 /*
510  * Both enqueue_mutex() and dequeue_mutex() operate on the
511  * thread-private linkage of the locked mutexes and on the robust
512  * linkage.
513  *
514  * Robust list, as seen by kernel, must be consistent even in the case
515  * of thread termination at arbitrary moment.  Since either enqueue or
516  * dequeue for list walked by kernel consists of rewriting a single
517  * forward pointer, it is safe.  On the other hand, rewrite of the
518  * back pointer is not atomic WRT the forward one, but kernel does not
519  * care.
520  */
521 static void
522 enqueue_mutex(struct pthread *curthread, struct pthread_mutex *m,
523     int error)
524 {
525 	struct pthread_mutex *m1;
526 	uintptr_t *rl;
527 	int qidx;
528 
529 	/* Add to the list of owned mutexes: */
530 	if (error != EOWNERDEAD)
531 		mutex_assert_not_owned(curthread, m);
532 	qidx = mutex_qidx(m);
533 	TAILQ_INSERT_TAIL(&curthread->mq[qidx], m, m_qe);
534 	if (!is_pshared_mutex(m))
535 		TAILQ_INSERT_TAIL(&curthread->mq[qidx + 1], m, m_pqe);
536 	if (is_robust_mutex(m)) {
537 		rl = is_pshared_mutex(m) ? &curthread->robust_list :
538 		    &curthread->priv_robust_list;
539 		m->m_rb_prev = NULL;
540 		if (*rl != 0) {
541 			m1 = __containerof((void *)*rl,
542 			    struct pthread_mutex, m_lock);
543 			m->m_lock.m_rb_lnk = (uintptr_t)&m1->m_lock;
544 			m1->m_rb_prev = m;
545 		} else {
546 			m1 = NULL;
547 			m->m_lock.m_rb_lnk = 0;
548 		}
549 		*rl = (uintptr_t)&m->m_lock;
550 	}
551 }
552 
553 static void
554 dequeue_mutex(struct pthread *curthread, struct pthread_mutex *m)
555 {
556 	struct pthread_mutex *mp, *mn;
557 	int qidx;
558 
559 	mutex_assert_is_owned(m);
560 	qidx = mutex_qidx(m);
561 	if (is_robust_mutex(m)) {
562 		mp = m->m_rb_prev;
563 		if (mp == NULL) {
564 			if (is_pshared_mutex(m)) {
565 				curthread->robust_list = m->m_lock.m_rb_lnk;
566 			} else {
567 				curthread->priv_robust_list =
568 				    m->m_lock.m_rb_lnk;
569 			}
570 		} else {
571 			mp->m_lock.m_rb_lnk = m->m_lock.m_rb_lnk;
572 		}
573 		if (m->m_lock.m_rb_lnk != 0) {
574 			mn = __containerof((void *)m->m_lock.m_rb_lnk,
575 			    struct pthread_mutex, m_lock);
576 			mn->m_rb_prev = m->m_rb_prev;
577 		}
578 		m->m_lock.m_rb_lnk = 0;
579 		m->m_rb_prev = NULL;
580 	}
581 	TAILQ_REMOVE(&curthread->mq[qidx], m, m_qe);
582 	if (!is_pshared_mutex(m))
583 		TAILQ_REMOVE(&curthread->mq[qidx + 1], m, m_pqe);
584 	if ((m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) != 0)
585 		set_inherited_priority(curthread, m);
586 	mutex_init_link(m);
587 }
588 
589 static int
590 check_and_init_mutex(pthread_mutex_t *mutex, struct pthread_mutex **m)
591 {
592 	int ret;
593 
594 	*m = *mutex;
595 	ret = 0;
596 	if (__predict_false(*m == THR_PSHARED_PTR)) {
597 		*m = __thr_pshared_offpage(mutex, 0);
598 		if (*m == NULL)
599 			ret = EINVAL;
600 		else
601 			shared_mutex_init(*m, NULL);
602 	} else if (__predict_false(*m <= THR_MUTEX_DESTROYED)) {
603 		if (*m == THR_MUTEX_DESTROYED) {
604 			ret = EINVAL;
605 		} else {
606 			ret = init_static(_get_curthread(), mutex);
607 			if (ret == 0)
608 				*m = *mutex;
609 		}
610 	}
611 	return (ret);
612 }
613 
614 int
615 __Tthr_mutex_trylock(pthread_mutex_t *mutex)
616 {
617 	struct pthread *curthread;
618 	struct pthread_mutex *m;
619 	uint32_t id;
620 	int ret, robust;
621 
622 	_thr_check_init();
623 	ret = check_and_init_mutex(mutex, &m);
624 	if (ret != 0)
625 		return (ret);
626 	curthread = _get_curthread();
627 	id = TID(curthread);
628 	if (m->m_flags & PMUTEX_FLAG_PRIVATE)
629 		THR_CRITICAL_ENTER(curthread);
630 	robust = _mutex_enter_robust(curthread, m);
631 	ret = _thr_umutex_trylock(&m->m_lock, id);
632 	if (__predict_true(ret == 0) || ret == EOWNERDEAD) {
633 		enqueue_mutex(curthread, m, ret);
634 		if (ret == EOWNERDEAD)
635 			m->m_lock.m_flags |= UMUTEX_NONCONSISTENT;
636 	} else if (PMUTEX_OWNER_ID(m) == id) {
637 		ret = mutex_self_trylock(m);
638 	} /* else {} */
639 	if (robust)
640 		_mutex_leave_robust(curthread, m);
641 	if (ret != 0 && ret != EOWNERDEAD &&
642 	    (m->m_flags & PMUTEX_FLAG_PRIVATE) != 0)
643 		THR_CRITICAL_LEAVE(curthread);
644 	return (ret);
645 }
646 
647 static int
648 mutex_lock_sleep(struct pthread *curthread, struct pthread_mutex *m,
649     const struct timespec *abstime)
650 {
651 	uint32_t id, owner;
652 	int count, ret;
653 
654 	id = TID(curthread);
655 	if (PMUTEX_OWNER_ID(m) == id)
656 		return (mutex_self_lock(m, abstime));
657 
658 	/*
659 	 * For adaptive mutexes, spin for a bit in the expectation
660 	 * that if the application requests this mutex type then
661 	 * the lock is likely to be released quickly and it is
662 	 * faster than entering the kernel
663 	 */
664 	if (__predict_false((m->m_lock.m_flags & (UMUTEX_PRIO_PROTECT |
665 	    UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST | UMUTEX_NONCONSISTENT)) != 0))
666 		goto sleep_in_kernel;
667 
668 	if (!_thr_is_smp)
669 		goto yield_loop;
670 
671 	count = m->m_spinloops;
672 	while (count--) {
673 		owner = m->m_lock.m_owner;
674 		if ((owner & ~UMUTEX_CONTESTED) == 0) {
675 			if (atomic_cmpset_acq_32(&m->m_lock.m_owner, owner,
676 			    id | owner)) {
677 				ret = 0;
678 				goto done;
679 			}
680 		}
681 		CPU_SPINWAIT;
682 	}
683 
684 yield_loop:
685 	count = m->m_yieldloops;
686 	while (count--) {
687 		_sched_yield();
688 		owner = m->m_lock.m_owner;
689 		if ((owner & ~UMUTEX_CONTESTED) == 0) {
690 			if (atomic_cmpset_acq_32(&m->m_lock.m_owner, owner,
691 			    id | owner)) {
692 				ret = 0;
693 				goto done;
694 			}
695 		}
696 	}
697 
698 sleep_in_kernel:
699 	if (abstime == NULL)
700 		ret = __thr_umutex_lock(&m->m_lock, id);
701 	else if (__predict_false(abstime->tv_nsec < 0 ||
702 	    abstime->tv_nsec >= 1000000000))
703 		ret = EINVAL;
704 	else
705 		ret = __thr_umutex_timedlock(&m->m_lock, id, abstime);
706 done:
707 	if (ret == 0 || ret == EOWNERDEAD) {
708 		enqueue_mutex(curthread, m, ret);
709 		if (ret == EOWNERDEAD)
710 			m->m_lock.m_flags |= UMUTEX_NONCONSISTENT;
711 	}
712 	return (ret);
713 }
714 
715 static __always_inline int
716 mutex_lock_common(struct pthread_mutex *m, const struct timespec *abstime,
717     bool cvattach, bool rb_onlist)
718 {
719 	struct pthread *curthread;
720 	int ret, robust;
721 
722 	robust = 0;  /* pacify gcc */
723 	curthread  = _get_curthread();
724 	if (!cvattach && m->m_flags & PMUTEX_FLAG_PRIVATE)
725 		THR_CRITICAL_ENTER(curthread);
726 	if (!rb_onlist)
727 		robust = _mutex_enter_robust(curthread, m);
728 	ret = _thr_umutex_trylock2(&m->m_lock, TID(curthread));
729 	if (__predict_true(ret == 0) || ret == EOWNERDEAD) {
730 		enqueue_mutex(curthread, m, ret);
731 		if (ret == EOWNERDEAD)
732 			m->m_lock.m_flags |= UMUTEX_NONCONSISTENT;
733 	} else {
734 		ret = mutex_lock_sleep(curthread, m, abstime);
735 	}
736 	if (!rb_onlist && robust)
737 		_mutex_leave_robust(curthread, m);
738 	if (ret != 0 && ret != EOWNERDEAD &&
739 	    (m->m_flags & PMUTEX_FLAG_PRIVATE) != 0 && !cvattach)
740 		THR_CRITICAL_LEAVE(curthread);
741 	return (ret);
742 }
743 
744 int
745 __Tthr_mutex_lock(pthread_mutex_t *mutex)
746 {
747 	struct pthread_mutex *m;
748 	int ret;
749 
750 	_thr_check_init();
751 	ret = check_and_init_mutex(mutex, &m);
752 	if (ret == 0)
753 		ret = mutex_lock_common(m, NULL, false, false);
754 	return (ret);
755 }
756 
757 int
758 __pthread_mutex_timedlock(pthread_mutex_t * __restrict mutex,
759     const struct timespec * __restrict abstime)
760 {
761 	struct pthread_mutex *m;
762 	int ret;
763 
764 	_thr_check_init();
765 	ret = check_and_init_mutex(mutex, &m);
766 	if (ret == 0)
767 		ret = mutex_lock_common(m, abstime, false, false);
768 	return (ret);
769 }
770 
771 int
772 _thr_mutex_unlock(pthread_mutex_t *mutex)
773 {
774 	struct pthread_mutex *mp;
775 
776 	if (*mutex == THR_PSHARED_PTR) {
777 		mp = __thr_pshared_offpage(mutex, 0);
778 		if (mp == NULL)
779 			return (EINVAL);
780 		shared_mutex_init(mp, NULL);
781 	} else {
782 		mp = *mutex;
783 	}
784 	return (mutex_unlock_common(mp, false, NULL));
785 }
786 
787 int
788 _mutex_cv_lock(struct pthread_mutex *m, int count, bool rb_onlist)
789 {
790 	int error;
791 
792 	error = mutex_lock_common(m, NULL, true, rb_onlist);
793 	if (error == 0 || error == EOWNERDEAD)
794 		m->m_count = count;
795 	return (error);
796 }
797 
798 int
799 _mutex_cv_unlock(struct pthread_mutex *m, int *count, int *defer)
800 {
801 
802 	/*
803 	 * Clear the count in case this is a recursive mutex.
804 	 */
805 	*count = m->m_count;
806 	m->m_count = 0;
807 	(void)mutex_unlock_common(m, true, defer);
808         return (0);
809 }
810 
811 int
812 _mutex_cv_attach(struct pthread_mutex *m, int count)
813 {
814 	struct pthread *curthread;
815 
816 	curthread = _get_curthread();
817 	enqueue_mutex(curthread, m, 0);
818 	m->m_count = count;
819 	return (0);
820 }
821 
822 int
823 _mutex_cv_detach(struct pthread_mutex *mp, int *recurse)
824 {
825 	struct pthread *curthread;
826 	int deferred, error;
827 
828 	curthread = _get_curthread();
829 	if ((error = _mutex_owned(curthread, mp)) != 0)
830 		return (error);
831 
832 	/*
833 	 * Clear the count in case this is a recursive mutex.
834 	 */
835 	*recurse = mp->m_count;
836 	mp->m_count = 0;
837 	dequeue_mutex(curthread, mp);
838 
839 	/* Will this happen in real-world ? */
840         if ((mp->m_flags & PMUTEX_FLAG_DEFERRED) != 0) {
841 		deferred = 1;
842 		mp->m_flags &= ~PMUTEX_FLAG_DEFERRED;
843 	} else
844 		deferred = 0;
845 
846 	if (deferred)  {
847 		_thr_wake_all(curthread->defer_waiters,
848 		    curthread->nwaiter_defer);
849 		curthread->nwaiter_defer = 0;
850 	}
851 	return (0);
852 }
853 
854 static int
855 mutex_self_trylock(struct pthread_mutex *m)
856 {
857 	int ret;
858 
859 	switch (PMUTEX_TYPE(m->m_flags)) {
860 	case PTHREAD_MUTEX_ERRORCHECK:
861 	case PTHREAD_MUTEX_NORMAL:
862 	case PTHREAD_MUTEX_ADAPTIVE_NP:
863 		ret = EBUSY;
864 		break;
865 
866 	case PTHREAD_MUTEX_RECURSIVE:
867 		/* Increment the lock count: */
868 		if (m->m_count + 1 > 0) {
869 			m->m_count++;
870 			ret = 0;
871 		} else
872 			ret = EAGAIN;
873 		break;
874 
875 	default:
876 		/* Trap invalid mutex types; */
877 		ret = EINVAL;
878 	}
879 
880 	return (ret);
881 }
882 
883 static int
884 mutex_self_lock(struct pthread_mutex *m, const struct timespec *abstime)
885 {
886 	struct timespec	ts1, ts2;
887 	int ret;
888 
889 	switch (PMUTEX_TYPE(m->m_flags)) {
890 	case PTHREAD_MUTEX_ERRORCHECK:
891 	case PTHREAD_MUTEX_ADAPTIVE_NP:
892 		if (abstime) {
893 			if (abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
894 			    abstime->tv_nsec >= 1000000000) {
895 				ret = EINVAL;
896 			} else {
897 				clock_gettime(CLOCK_REALTIME, &ts1);
898 				TIMESPEC_SUB(&ts2, abstime, &ts1);
899 				__sys_nanosleep(&ts2, NULL);
900 				ret = ETIMEDOUT;
901 			}
902 		} else {
903 			/*
904 			 * POSIX specifies that mutexes should return
905 			 * EDEADLK if a recursive lock is detected.
906 			 */
907 			ret = EDEADLK;
908 		}
909 		break;
910 
911 	case PTHREAD_MUTEX_NORMAL:
912 		/*
913 		 * What SS2 define as a 'normal' mutex.  Intentionally
914 		 * deadlock on attempts to get a lock you already own.
915 		 */
916 		ret = 0;
917 		if (abstime) {
918 			if (abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
919 			    abstime->tv_nsec >= 1000000000) {
920 				ret = EINVAL;
921 			} else {
922 				clock_gettime(CLOCK_REALTIME, &ts1);
923 				TIMESPEC_SUB(&ts2, abstime, &ts1);
924 				__sys_nanosleep(&ts2, NULL);
925 				ret = ETIMEDOUT;
926 			}
927 		} else {
928 			ts1.tv_sec = 30;
929 			ts1.tv_nsec = 0;
930 			for (;;)
931 				__sys_nanosleep(&ts1, NULL);
932 		}
933 		break;
934 
935 	case PTHREAD_MUTEX_RECURSIVE:
936 		/* Increment the lock count: */
937 		if (m->m_count + 1 > 0) {
938 			m->m_count++;
939 			ret = 0;
940 		} else
941 			ret = EAGAIN;
942 		break;
943 
944 	default:
945 		/* Trap invalid mutex types; */
946 		ret = EINVAL;
947 	}
948 
949 	return (ret);
950 }
951 
952 static __always_inline int
953 mutex_unlock_common(struct pthread_mutex *m, bool cv, int *mtx_defer)
954 {
955 	struct pthread *curthread;
956 	uint32_t id;
957 	int deferred, error, private, robust;
958 
959 	if (__predict_false(m <= THR_MUTEX_DESTROYED)) {
960 		if (m == THR_MUTEX_DESTROYED)
961 			return (EINVAL);
962 		return (EPERM);
963 	}
964 
965 	curthread = _get_curthread();
966 	id = TID(curthread);
967 
968 	/*
969 	 * Check if the running thread is not the owner of the mutex.
970 	 */
971 	if (__predict_false(PMUTEX_OWNER_ID(m) != id))
972 		return (EPERM);
973 
974 	error = 0;
975 	private = (m->m_flags & PMUTEX_FLAG_PRIVATE) != 0;
976 	if (__predict_false(PMUTEX_TYPE(m->m_flags) ==
977 	    PTHREAD_MUTEX_RECURSIVE && m->m_count > 0)) {
978 		m->m_count--;
979 	} else {
980 		if ((m->m_flags & PMUTEX_FLAG_DEFERRED) != 0) {
981 			deferred = 1;
982 			m->m_flags &= ~PMUTEX_FLAG_DEFERRED;
983         	} else
984 			deferred = 0;
985 
986 		robust = _mutex_enter_robust(curthread, m);
987 		dequeue_mutex(curthread, m);
988 		error = _thr_umutex_unlock2(&m->m_lock, id, mtx_defer);
989 		if (deferred)  {
990 			if (mtx_defer == NULL) {
991 				_thr_wake_all(curthread->defer_waiters,
992 				    curthread->nwaiter_defer);
993 				curthread->nwaiter_defer = 0;
994 			} else
995 				*mtx_defer = 1;
996 		}
997 		if (robust)
998 			_mutex_leave_robust(curthread, m);
999 	}
1000 	if (!cv && private)
1001 		THR_CRITICAL_LEAVE(curthread);
1002 	return (error);
1003 }
1004 
1005 int
1006 _pthread_mutex_getprioceiling(const pthread_mutex_t * __restrict mutex,
1007     int * __restrict prioceiling)
1008 {
1009 	struct pthread_mutex *m;
1010 
1011 	if (*mutex == THR_PSHARED_PTR) {
1012 		m = __thr_pshared_offpage(__DECONST(void *, mutex), 0);
1013 		if (m == NULL)
1014 			return (EINVAL);
1015 		shared_mutex_init(m, NULL);
1016 	} else {
1017 		m = *mutex;
1018 		if (m <= THR_MUTEX_DESTROYED)
1019 			return (EINVAL);
1020 	}
1021 	if ((m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0)
1022 		return (EINVAL);
1023 	*prioceiling = m->m_lock.m_ceilings[0];
1024 	return (0);
1025 }
1026 
1027 int
1028 _pthread_mutex_setprioceiling(pthread_mutex_t * __restrict mutex,
1029     int ceiling, int * __restrict old_ceiling)
1030 {
1031 	struct pthread *curthread;
1032 	struct pthread_mutex *m, *m1, *m2;
1033 	struct mutex_queue *q, *qp;
1034 	int qidx, ret;
1035 
1036 	if (*mutex == THR_PSHARED_PTR) {
1037 		m = __thr_pshared_offpage(mutex, 0);
1038 		if (m == NULL)
1039 			return (EINVAL);
1040 		shared_mutex_init(m, NULL);
1041 	} else {
1042 		m = *mutex;
1043 		if (m <= THR_MUTEX_DESTROYED)
1044 			return (EINVAL);
1045 	}
1046 	if ((m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0)
1047 		return (EINVAL);
1048 
1049 	ret = __thr_umutex_set_ceiling(&m->m_lock, ceiling, old_ceiling);
1050 	if (ret != 0)
1051 		return (ret);
1052 
1053 	curthread = _get_curthread();
1054 	if (PMUTEX_OWNER_ID(m) == TID(curthread)) {
1055 		mutex_assert_is_owned(m);
1056 		m1 = TAILQ_PREV(m, mutex_queue, m_qe);
1057 		m2 = TAILQ_NEXT(m, m_qe);
1058 		if ((m1 != NULL && m1->m_lock.m_ceilings[0] > (u_int)ceiling) ||
1059 		    (m2 != NULL && m2->m_lock.m_ceilings[0] < (u_int)ceiling)) {
1060 			qidx = mutex_qidx(m);
1061 			q = &curthread->mq[qidx];
1062 			qp = &curthread->mq[qidx + 1];
1063 			TAILQ_REMOVE(q, m, m_qe);
1064 			if (!is_pshared_mutex(m))
1065 				TAILQ_REMOVE(qp, m, m_pqe);
1066 			TAILQ_FOREACH(m2, q, m_qe) {
1067 				if (m2->m_lock.m_ceilings[0] > (u_int)ceiling) {
1068 					TAILQ_INSERT_BEFORE(m2, m, m_qe);
1069 					if (!is_pshared_mutex(m)) {
1070 						while (m2 != NULL &&
1071 						    is_pshared_mutex(m2)) {
1072 							m2 = TAILQ_PREV(m2,
1073 							    mutex_queue, m_qe);
1074 						}
1075 						if (m2 == NULL) {
1076 							TAILQ_INSERT_HEAD(qp,
1077 							    m, m_pqe);
1078 						} else {
1079 							TAILQ_INSERT_BEFORE(m2,
1080 							    m, m_pqe);
1081 						}
1082 					}
1083 					return (0);
1084 				}
1085 			}
1086 			TAILQ_INSERT_TAIL(q, m, m_qe);
1087 			if (!is_pshared_mutex(m))
1088 				TAILQ_INSERT_TAIL(qp, m, m_pqe);
1089 		}
1090 	}
1091 	return (0);
1092 }
1093 
1094 int
1095 _pthread_mutex_getspinloops_np(pthread_mutex_t *mutex, int *count)
1096 {
1097 	struct pthread_mutex *m;
1098 	int ret;
1099 
1100 	ret = check_and_init_mutex(mutex, &m);
1101 	if (ret == 0)
1102 		*count = m->m_spinloops;
1103 	return (ret);
1104 }
1105 
1106 int
1107 __pthread_mutex_setspinloops_np(pthread_mutex_t *mutex, int count)
1108 {
1109 	struct pthread_mutex *m;
1110 	int ret;
1111 
1112 	ret = check_and_init_mutex(mutex, &m);
1113 	if (ret == 0)
1114 		m->m_spinloops = count;
1115 	return (ret);
1116 }
1117 
1118 int
1119 _pthread_mutex_getyieldloops_np(pthread_mutex_t *mutex, int *count)
1120 {
1121 	struct pthread_mutex *m;
1122 	int ret;
1123 
1124 	ret = check_and_init_mutex(mutex, &m);
1125 	if (ret == 0)
1126 		*count = m->m_yieldloops;
1127 	return (ret);
1128 }
1129 
1130 int
1131 __pthread_mutex_setyieldloops_np(pthread_mutex_t *mutex, int count)
1132 {
1133 	struct pthread_mutex *m;
1134 	int ret;
1135 
1136 	ret = check_and_init_mutex(mutex, &m);
1137 	if (ret == 0)
1138 		m->m_yieldloops = count;
1139 	return (0);
1140 }
1141 
1142 int
1143 _pthread_mutex_isowned_np(pthread_mutex_t *mutex)
1144 {
1145 	struct pthread_mutex *m;
1146 
1147 	if (*mutex == THR_PSHARED_PTR) {
1148 		m = __thr_pshared_offpage(mutex, 0);
1149 		if (m == NULL)
1150 			return (0);
1151 		shared_mutex_init(m, NULL);
1152 	} else {
1153 		m = *mutex;
1154 		if (m <= THR_MUTEX_DESTROYED)
1155 			return (0);
1156 	}
1157 	return (PMUTEX_OWNER_ID(m) == TID(_get_curthread()));
1158 }
1159 
1160 int
1161 _mutex_owned(struct pthread *curthread, const struct pthread_mutex *mp)
1162 {
1163 
1164 	if (__predict_false(mp <= THR_MUTEX_DESTROYED)) {
1165 		if (mp == THR_MUTEX_DESTROYED)
1166 			return (EINVAL);
1167 		return (EPERM);
1168 	}
1169 	if (PMUTEX_OWNER_ID(mp) != TID(curthread))
1170 		return (EPERM);
1171 	return (0);
1172 }
1173 
1174 int
1175 _Tthr_mutex_consistent(pthread_mutex_t *mutex)
1176 {
1177 	struct pthread_mutex *m;
1178 	struct pthread *curthread;
1179 
1180 	if (*mutex == THR_PSHARED_PTR) {
1181 		m = __thr_pshared_offpage(mutex, 0);
1182 		if (m == NULL)
1183 			return (EINVAL);
1184 		shared_mutex_init(m, NULL);
1185 	} else {
1186 		m = *mutex;
1187 		if (m <= THR_MUTEX_DESTROYED)
1188 			return (EINVAL);
1189 	}
1190 	curthread = _get_curthread();
1191 	if ((m->m_lock.m_flags & (UMUTEX_ROBUST | UMUTEX_NONCONSISTENT)) !=
1192 	    (UMUTEX_ROBUST | UMUTEX_NONCONSISTENT))
1193 		return (EINVAL);
1194 	if (PMUTEX_OWNER_ID(m) != TID(curthread))
1195 		return (EPERM);
1196 	m->m_lock.m_flags &= ~UMUTEX_NONCONSISTENT;
1197 	return (0);
1198 }
1199