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 <sys/cdefs.h> 42 __FBSDID("$FreeBSD$"); 43 44 #include "namespace.h" 45 #include <stdlib.h> 46 #include <errno.h> 47 #include <string.h> 48 #include <sys/param.h> 49 #include <sys/queue.h> 50 #include <pthread.h> 51 #include <pthread_np.h> 52 #include "un-namespace.h" 53 54 #include "thr_private.h" 55 56 _Static_assert(sizeof(struct pthread_mutex) <= PAGE_SIZE, 57 "pthread_mutex is too large for off-page"); 58 59 /* 60 * For adaptive mutexes, how many times to spin doing trylock2 61 * before entering the kernel to block 62 */ 63 #define MUTEX_ADAPTIVE_SPINS 2000 64 65 /* 66 * Prototypes 67 */ 68 int __pthread_mutex_timedlock(pthread_mutex_t * __restrict mutex, 69 const struct timespec * __restrict abstime); 70 int _pthread_mutex_getspinloops_np(pthread_mutex_t *mutex, int *count); 71 int _pthread_mutex_setspinloops_np(pthread_mutex_t *mutex, int count); 72 int __pthread_mutex_setspinloops_np(pthread_mutex_t *mutex, int count); 73 int _pthread_mutex_setyieldloops_np(pthread_mutex_t *mutex, int count); 74 int _pthread_mutex_getyieldloops_np(pthread_mutex_t *mutex, int *count); 75 int __pthread_mutex_setyieldloops_np(pthread_mutex_t *mutex, int count); 76 77 static int mutex_self_trylock(pthread_mutex_t); 78 static int mutex_self_lock(pthread_mutex_t, 79 const struct timespec *abstime); 80 static int mutex_unlock_common(struct pthread_mutex *, bool, int *); 81 static int mutex_lock_sleep(struct pthread *, pthread_mutex_t, 82 const struct timespec *); 83 static void mutex_init_robust(struct pthread *curthread); 84 static int mutex_qidx(struct pthread_mutex *m); 85 static bool is_robust_mutex(struct pthread_mutex *m); 86 static bool is_pshared_mutex(struct pthread_mutex *m); 87 88 __weak_reference(__Tthr_mutex_init, pthread_mutex_init); 89 __weak_reference(__Tthr_mutex_init, __pthread_mutex_init); 90 __strong_reference(__Tthr_mutex_init, _pthread_mutex_init); 91 __weak_reference(__Tthr_mutex_lock, pthread_mutex_lock); 92 __weak_reference(__Tthr_mutex_lock, __pthread_mutex_lock); 93 __strong_reference(__Tthr_mutex_lock, _pthread_mutex_lock); 94 __weak_reference(__pthread_mutex_timedlock, pthread_mutex_timedlock); 95 __strong_reference(__pthread_mutex_timedlock, _pthread_mutex_timedlock); 96 __weak_reference(__Tthr_mutex_trylock, pthread_mutex_trylock); 97 __weak_reference(__Tthr_mutex_trylock, __pthread_mutex_trylock); 98 __strong_reference(__Tthr_mutex_trylock, _pthread_mutex_trylock); 99 __weak_reference(_Tthr_mutex_consistent, pthread_mutex_consistent); 100 __weak_reference(_Tthr_mutex_consistent, _pthread_mutex_consistent); 101 __strong_reference(_Tthr_mutex_consistent, __pthread_mutex_consistent); 102 103 /* Single underscore versions provided for libc internal usage: */ 104 /* No difference between libc and application usage of these: */ 105 __weak_reference(_thr_mutex_destroy, pthread_mutex_destroy); 106 __weak_reference(_thr_mutex_destroy, _pthread_mutex_destroy); 107 __weak_reference(_thr_mutex_unlock, pthread_mutex_unlock); 108 __weak_reference(_thr_mutex_unlock, _pthread_mutex_unlock); 109 110 __weak_reference(_pthread_mutex_getprioceiling, pthread_mutex_getprioceiling); 111 __weak_reference(_pthread_mutex_setprioceiling, pthread_mutex_setprioceiling); 112 113 __weak_reference(__pthread_mutex_setspinloops_np, pthread_mutex_setspinloops_np); 114 __strong_reference(__pthread_mutex_setspinloops_np, _pthread_mutex_setspinloops_np); 115 __weak_reference(_pthread_mutex_getspinloops_np, pthread_mutex_getspinloops_np); 116 117 __weak_reference(__pthread_mutex_setyieldloops_np, pthread_mutex_setyieldloops_np); 118 __strong_reference(__pthread_mutex_setyieldloops_np, _pthread_mutex_setyieldloops_np); 119 __weak_reference(_pthread_mutex_getyieldloops_np, pthread_mutex_getyieldloops_np); 120 __weak_reference(_pthread_mutex_isowned_np, pthread_mutex_isowned_np); 121 122 static void 123 mutex_init_link(struct pthread_mutex *m) 124 { 125 126 #if defined(_PTHREADS_INVARIANTS) 127 m->m_qe.tqe_prev = NULL; 128 m->m_qe.tqe_next = NULL; 129 m->m_pqe.tqe_prev = NULL; 130 m->m_pqe.tqe_next = NULL; 131 #endif 132 } 133 134 static void 135 mutex_assert_is_owned(struct pthread_mutex *m __unused) 136 { 137 138 #if defined(_PTHREADS_INVARIANTS) 139 if (__predict_false(m->m_qe.tqe_prev == NULL)) 140 PANIC("mutex %p own %#x is not on list %p %p", 141 m, m->m_lock.m_owner, m->m_qe.tqe_prev, m->m_qe.tqe_next); 142 #endif 143 } 144 145 static void 146 mutex_assert_not_owned(struct pthread *curthread __unused, 147 struct pthread_mutex *m __unused) 148 { 149 150 #if defined(_PTHREADS_INVARIANTS) 151 if (__predict_false(m->m_qe.tqe_prev != NULL || 152 m->m_qe.tqe_next != NULL)) 153 PANIC("mutex %p own %#x is on list %p %p", 154 m, m->m_lock.m_owner, m->m_qe.tqe_prev, m->m_qe.tqe_next); 155 if (__predict_false(is_robust_mutex(m) && 156 (m->m_lock.m_rb_lnk != 0 || m->m_rb_prev != NULL || 157 (is_pshared_mutex(m) && curthread->robust_list == 158 (uintptr_t)&m->m_lock) || 159 (!is_pshared_mutex(m) && curthread->priv_robust_list == 160 (uintptr_t)&m->m_lock)))) 161 PANIC( 162 "mutex %p own %#x is on robust linkage %p %p head %p phead %p", 163 m, m->m_lock.m_owner, (void *)m->m_lock.m_rb_lnk, 164 m->m_rb_prev, (void *)curthread->robust_list, 165 (void *)curthread->priv_robust_list); 166 #endif 167 } 168 169 static bool 170 is_pshared_mutex(struct pthread_mutex *m) 171 { 172 173 return ((m->m_lock.m_flags & USYNC_PROCESS_SHARED) != 0); 174 } 175 176 static bool 177 is_robust_mutex(struct pthread_mutex *m) 178 { 179 180 return ((m->m_lock.m_flags & UMUTEX_ROBUST) != 0); 181 } 182 183 int 184 _mutex_enter_robust(struct pthread *curthread, struct pthread_mutex *m) 185 { 186 187 #if defined(_PTHREADS_INVARIANTS) 188 if (__predict_false(curthread->inact_mtx != 0)) 189 PANIC("inact_mtx enter"); 190 #endif 191 if (!is_robust_mutex(m)) 192 return (0); 193 194 mutex_init_robust(curthread); 195 curthread->inact_mtx = (uintptr_t)&m->m_lock; 196 return (1); 197 } 198 199 void 200 _mutex_leave_robust(struct pthread *curthread, struct pthread_mutex *m __unused) 201 { 202 203 #if defined(_PTHREADS_INVARIANTS) 204 if (__predict_false(curthread->inact_mtx != (uintptr_t)&m->m_lock)) 205 PANIC("inact_mtx leave"); 206 #endif 207 curthread->inact_mtx = 0; 208 } 209 210 static int 211 mutex_check_attr(const struct pthread_mutex_attr *attr) 212 { 213 214 if (attr->m_type < PTHREAD_MUTEX_ERRORCHECK || 215 attr->m_type >= PTHREAD_MUTEX_TYPE_MAX) 216 return (EINVAL); 217 if (attr->m_protocol < PTHREAD_PRIO_NONE || 218 attr->m_protocol > PTHREAD_PRIO_PROTECT) 219 return (EINVAL); 220 return (0); 221 } 222 223 static void 224 mutex_init_robust(struct pthread *curthread) 225 { 226 struct umtx_robust_lists_params rb; 227 228 if (curthread == NULL) 229 curthread = _get_curthread(); 230 if (curthread->robust_inited) 231 return; 232 rb.robust_list_offset = (uintptr_t)&curthread->robust_list; 233 rb.robust_priv_list_offset = (uintptr_t)&curthread->priv_robust_list; 234 rb.robust_inact_offset = (uintptr_t)&curthread->inact_mtx; 235 _umtx_op(NULL, UMTX_OP_ROBUST_LISTS, sizeof(rb), &rb, NULL); 236 curthread->robust_inited = 1; 237 } 238 239 static void 240 mutex_init_body(struct pthread_mutex *pmutex, 241 const struct pthread_mutex_attr *attr) 242 { 243 244 pmutex->m_flags = attr->m_type; 245 pmutex->m_count = 0; 246 pmutex->m_spinloops = 0; 247 pmutex->m_yieldloops = 0; 248 mutex_init_link(pmutex); 249 switch (attr->m_protocol) { 250 case PTHREAD_PRIO_NONE: 251 pmutex->m_lock.m_owner = UMUTEX_UNOWNED; 252 pmutex->m_lock.m_flags = 0; 253 break; 254 case PTHREAD_PRIO_INHERIT: 255 pmutex->m_lock.m_owner = UMUTEX_UNOWNED; 256 pmutex->m_lock.m_flags = UMUTEX_PRIO_INHERIT; 257 break; 258 case PTHREAD_PRIO_PROTECT: 259 pmutex->m_lock.m_owner = UMUTEX_CONTESTED; 260 pmutex->m_lock.m_flags = UMUTEX_PRIO_PROTECT; 261 pmutex->m_lock.m_ceilings[0] = attr->m_ceiling; 262 break; 263 } 264 if (attr->m_pshared == PTHREAD_PROCESS_SHARED) 265 pmutex->m_lock.m_flags |= USYNC_PROCESS_SHARED; 266 if (attr->m_robust == PTHREAD_MUTEX_ROBUST) { 267 mutex_init_robust(NULL); 268 pmutex->m_lock.m_flags |= UMUTEX_ROBUST; 269 } 270 if (PMUTEX_TYPE(pmutex->m_flags) == PTHREAD_MUTEX_ADAPTIVE_NP) { 271 pmutex->m_spinloops = 272 _thr_spinloops ? _thr_spinloops: MUTEX_ADAPTIVE_SPINS; 273 pmutex->m_yieldloops = _thr_yieldloops; 274 } 275 } 276 277 static int 278 mutex_init(pthread_mutex_t *mutex, 279 const struct pthread_mutex_attr *mutex_attr, 280 void *(calloc_cb)(size_t, size_t)) 281 { 282 const struct pthread_mutex_attr *attr; 283 struct pthread_mutex *pmutex; 284 int error; 285 286 if (mutex_attr == NULL) { 287 attr = &_pthread_mutexattr_default; 288 } else { 289 attr = mutex_attr; 290 error = mutex_check_attr(attr); 291 if (error != 0) 292 return (error); 293 } 294 if ((pmutex = (pthread_mutex_t) 295 calloc_cb(1, sizeof(struct pthread_mutex))) == NULL) 296 return (ENOMEM); 297 mutex_init_body(pmutex, attr); 298 *mutex = pmutex; 299 return (0); 300 } 301 302 static int 303 init_static(struct pthread *thread, pthread_mutex_t *mutex) 304 { 305 int ret; 306 307 THR_LOCK_ACQUIRE(thread, &_mutex_static_lock); 308 309 if (*mutex == THR_MUTEX_INITIALIZER) 310 ret = mutex_init(mutex, &_pthread_mutexattr_default, 311 __thr_calloc); 312 else if (*mutex == THR_ADAPTIVE_MUTEX_INITIALIZER) 313 ret = mutex_init(mutex, &_pthread_mutexattr_adaptive_default, 314 __thr_calloc); 315 else 316 ret = 0; 317 THR_LOCK_RELEASE(thread, &_mutex_static_lock); 318 319 return (ret); 320 } 321 322 static void 323 set_inherited_priority(struct pthread *curthread, struct pthread_mutex *m) 324 { 325 struct pthread_mutex *m2; 326 327 m2 = TAILQ_LAST(&curthread->mq[mutex_qidx(m)], mutex_queue); 328 if (m2 != NULL) 329 m->m_lock.m_ceilings[1] = m2->m_lock.m_ceilings[0]; 330 else 331 m->m_lock.m_ceilings[1] = -1; 332 } 333 334 static void 335 shared_mutex_init(struct pthread_mutex *pmtx, const struct 336 pthread_mutex_attr *mutex_attr) 337 { 338 static const struct pthread_mutex_attr foobar_mutex_attr = { 339 .m_type = PTHREAD_MUTEX_DEFAULT, 340 .m_protocol = PTHREAD_PRIO_NONE, 341 .m_ceiling = 0, 342 .m_pshared = PTHREAD_PROCESS_SHARED, 343 .m_robust = PTHREAD_MUTEX_STALLED, 344 }; 345 bool done; 346 347 /* 348 * Hack to allow multiple pthread_mutex_init() calls on the 349 * same process-shared mutex. We rely on kernel allocating 350 * zeroed offpage for the mutex, i.e. the 351 * PMUTEX_INITSTAGE_ALLOC value must be zero. 352 */ 353 for (done = false; !done;) { 354 switch (pmtx->m_ps) { 355 case PMUTEX_INITSTAGE_DONE: 356 atomic_thread_fence_acq(); 357 done = true; 358 break; 359 case PMUTEX_INITSTAGE_ALLOC: 360 if (atomic_cmpset_int(&pmtx->m_ps, 361 PMUTEX_INITSTAGE_ALLOC, PMUTEX_INITSTAGE_BUSY)) { 362 if (mutex_attr == NULL) 363 mutex_attr = &foobar_mutex_attr; 364 mutex_init_body(pmtx, mutex_attr); 365 atomic_store_rel_int(&pmtx->m_ps, 366 PMUTEX_INITSTAGE_DONE); 367 done = true; 368 } 369 break; 370 case PMUTEX_INITSTAGE_BUSY: 371 _pthread_yield(); 372 break; 373 default: 374 PANIC("corrupted offpage"); 375 break; 376 } 377 } 378 } 379 380 int 381 __Tthr_mutex_init(pthread_mutex_t * __restrict mutex, 382 const pthread_mutexattr_t * __restrict mutex_attr) 383 { 384 struct pthread_mutex *pmtx; 385 int ret; 386 387 if (mutex_attr != NULL) { 388 ret = mutex_check_attr(*mutex_attr); 389 if (ret != 0) 390 return (ret); 391 } 392 if (mutex_attr == NULL || 393 (*mutex_attr)->m_pshared == PTHREAD_PROCESS_PRIVATE) { 394 __thr_malloc_init(); 395 return (mutex_init(mutex, mutex_attr ? *mutex_attr : NULL, 396 __thr_calloc)); 397 } 398 pmtx = __thr_pshared_offpage(__DECONST(void *, mutex), 1); 399 if (pmtx == NULL) 400 return (EFAULT); 401 *mutex = THR_PSHARED_PTR; 402 shared_mutex_init(pmtx, *mutex_attr); 403 return (0); 404 } 405 406 /* This function is used internally by malloc. */ 407 int 408 _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex, 409 void *(calloc_cb)(size_t, size_t)) 410 { 411 static const struct pthread_mutex_attr attr = { 412 .m_type = PTHREAD_MUTEX_NORMAL, 413 .m_protocol = PTHREAD_PRIO_NONE, 414 .m_ceiling = 0, 415 .m_pshared = PTHREAD_PROCESS_PRIVATE, 416 .m_robust = PTHREAD_MUTEX_STALLED, 417 }; 418 int ret; 419 420 ret = mutex_init(mutex, &attr, calloc_cb); 421 if (ret == 0) 422 (*mutex)->m_flags |= PMUTEX_FLAG_PRIVATE; 423 return (ret); 424 } 425 426 /* 427 * Fix mutex ownership for child process. 428 * 429 * Process private mutex ownership is transmitted from the forking 430 * thread to the child process. 431 * 432 * Process shared mutex should not be inherited because owner is 433 * forking thread which is in parent process, they are removed from 434 * the owned mutex list. 435 */ 436 static void 437 queue_fork(struct pthread *curthread, struct mutex_queue *q, 438 struct mutex_queue *qp, uint bit) 439 { 440 struct pthread_mutex *m; 441 442 TAILQ_INIT(q); 443 TAILQ_FOREACH(m, qp, m_pqe) { 444 TAILQ_INSERT_TAIL(q, m, m_qe); 445 m->m_lock.m_owner = TID(curthread) | bit; 446 } 447 } 448 449 void 450 _mutex_fork(struct pthread *curthread) 451 { 452 453 queue_fork(curthread, &curthread->mq[TMQ_NORM], 454 &curthread->mq[TMQ_NORM_PRIV], 0); 455 queue_fork(curthread, &curthread->mq[TMQ_NORM_PP], 456 &curthread->mq[TMQ_NORM_PP_PRIV], UMUTEX_CONTESTED); 457 queue_fork(curthread, &curthread->mq[TMQ_ROBUST_PP], 458 &curthread->mq[TMQ_ROBUST_PP_PRIV], UMUTEX_CONTESTED); 459 curthread->robust_list = 0; 460 } 461 462 int 463 _thr_mutex_destroy(pthread_mutex_t *mutex) 464 { 465 pthread_mutex_t m, m1; 466 int ret; 467 468 m = *mutex; 469 if (m < THR_MUTEX_DESTROYED) { 470 ret = 0; 471 } else if (m == THR_MUTEX_DESTROYED) { 472 ret = EINVAL; 473 } else { 474 if (m == THR_PSHARED_PTR) { 475 m1 = __thr_pshared_offpage(mutex, 0); 476 if (m1 != NULL) { 477 if ((uint32_t)m1->m_lock.m_owner != 478 UMUTEX_RB_OWNERDEAD) { 479 mutex_assert_not_owned( 480 _get_curthread(), m1); 481 } 482 __thr_pshared_destroy(mutex); 483 } 484 *mutex = THR_MUTEX_DESTROYED; 485 return (0); 486 } 487 if (PMUTEX_OWNER_ID(m) != 0 && 488 (uint32_t)m->m_lock.m_owner != UMUTEX_RB_NOTRECOV) { 489 ret = EBUSY; 490 } else { 491 *mutex = THR_MUTEX_DESTROYED; 492 mutex_assert_not_owned(_get_curthread(), m); 493 __thr_free(m); 494 ret = 0; 495 } 496 } 497 498 return (ret); 499 } 500 501 static int 502 mutex_qidx(struct pthread_mutex *m) 503 { 504 505 if ((m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0) 506 return (TMQ_NORM); 507 return (is_robust_mutex(m) ? TMQ_ROBUST_PP : TMQ_NORM_PP); 508 } 509 510 /* 511 * Both enqueue_mutex() and dequeue_mutex() operate on the 512 * thread-private linkage of the locked mutexes and on the robust 513 * linkage. 514 * 515 * Robust list, as seen by kernel, must be consistent even in the case 516 * of thread termination at arbitrary moment. Since either enqueue or 517 * dequeue for list walked by kernel consists of rewriting a single 518 * forward pointer, it is safe. On the other hand, rewrite of the 519 * back pointer is not atomic WRT the forward one, but kernel does not 520 * care. 521 */ 522 static void 523 enqueue_mutex(struct pthread *curthread, struct pthread_mutex *m, 524 int error) 525 { 526 struct pthread_mutex *m1; 527 uintptr_t *rl; 528 int qidx; 529 530 /* Add to the list of owned mutexes: */ 531 if (error != EOWNERDEAD) 532 mutex_assert_not_owned(curthread, m); 533 qidx = mutex_qidx(m); 534 TAILQ_INSERT_TAIL(&curthread->mq[qidx], m, m_qe); 535 if (!is_pshared_mutex(m)) 536 TAILQ_INSERT_TAIL(&curthread->mq[qidx + 1], m, m_pqe); 537 if (is_robust_mutex(m)) { 538 rl = is_pshared_mutex(m) ? &curthread->robust_list : 539 &curthread->priv_robust_list; 540 m->m_rb_prev = NULL; 541 if (*rl != 0) { 542 m1 = __containerof((void *)*rl, 543 struct pthread_mutex, m_lock); 544 m->m_lock.m_rb_lnk = (uintptr_t)&m1->m_lock; 545 m1->m_rb_prev = m; 546 } else { 547 m1 = NULL; 548 m->m_lock.m_rb_lnk = 0; 549 } 550 *rl = (uintptr_t)&m->m_lock; 551 } 552 } 553 554 static void 555 dequeue_mutex(struct pthread *curthread, struct pthread_mutex *m) 556 { 557 struct pthread_mutex *mp, *mn; 558 int qidx; 559 560 mutex_assert_is_owned(m); 561 qidx = mutex_qidx(m); 562 if (is_robust_mutex(m)) { 563 mp = m->m_rb_prev; 564 if (mp == NULL) { 565 if (is_pshared_mutex(m)) { 566 curthread->robust_list = m->m_lock.m_rb_lnk; 567 } else { 568 curthread->priv_robust_list = 569 m->m_lock.m_rb_lnk; 570 } 571 } else { 572 mp->m_lock.m_rb_lnk = m->m_lock.m_rb_lnk; 573 } 574 if (m->m_lock.m_rb_lnk != 0) { 575 mn = __containerof((void *)m->m_lock.m_rb_lnk, 576 struct pthread_mutex, m_lock); 577 mn->m_rb_prev = m->m_rb_prev; 578 } 579 m->m_lock.m_rb_lnk = 0; 580 m->m_rb_prev = NULL; 581 } 582 TAILQ_REMOVE(&curthread->mq[qidx], m, m_qe); 583 if (!is_pshared_mutex(m)) 584 TAILQ_REMOVE(&curthread->mq[qidx + 1], m, m_pqe); 585 if ((m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) != 0) 586 set_inherited_priority(curthread, m); 587 mutex_init_link(m); 588 } 589 590 static int 591 check_and_init_mutex(pthread_mutex_t *mutex, struct pthread_mutex **m) 592 { 593 int ret; 594 595 *m = *mutex; 596 ret = 0; 597 if (*m == THR_PSHARED_PTR) { 598 *m = __thr_pshared_offpage(mutex, 0); 599 if (*m == NULL) 600 ret = EINVAL; 601 else 602 shared_mutex_init(*m, NULL); 603 } else if (__predict_false(*m <= THR_MUTEX_DESTROYED)) { 604 if (*m == THR_MUTEX_DESTROYED) { 605 ret = EINVAL; 606 } else { 607 ret = init_static(_get_curthread(), mutex); 608 if (ret == 0) 609 *m = *mutex; 610 } 611 } 612 return (ret); 613 } 614 615 int 616 __Tthr_mutex_trylock(pthread_mutex_t *mutex) 617 { 618 struct pthread *curthread; 619 struct pthread_mutex *m; 620 uint32_t id; 621 int ret, robust; 622 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 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 (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 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