kern_umtx.c revision 232144
1/*- 2 * Copyright (c) 2004, David Xu <davidxu@freebsd.org> 3 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice unmodified, this list of conditions, and the following 11 * disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: head/sys/kern/kern_umtx.c 232144 2012-02-25 02:12:17Z davidxu $"); 30 31#include "opt_compat.h" 32#include <sys/param.h> 33#include <sys/kernel.h> 34#include <sys/limits.h> 35#include <sys/lock.h> 36#include <sys/malloc.h> 37#include <sys/mutex.h> 38#include <sys/priv.h> 39#include <sys/proc.h> 40#include <sys/sched.h> 41#include <sys/smp.h> 42#include <sys/sysctl.h> 43#include <sys/sysent.h> 44#include <sys/systm.h> 45#include <sys/sysproto.h> 46#include <sys/syscallsubr.h> 47#include <sys/eventhandler.h> 48#include <sys/umtx.h> 49 50#include <vm/vm.h> 51#include <vm/vm_param.h> 52#include <vm/pmap.h> 53#include <vm/vm_map.h> 54#include <vm/vm_object.h> 55 56#include <machine/cpu.h> 57 58#ifdef COMPAT_FREEBSD32 59#include <compat/freebsd32/freebsd32_proto.h> 60#endif 61 62#define _UMUTEX_TRY 1 63#define _UMUTEX_WAIT 2 64 65/* Priority inheritance mutex info. */ 66struct umtx_pi { 67 /* Owner thread */ 68 struct thread *pi_owner; 69 70 /* Reference count */ 71 int pi_refcount; 72 73 /* List entry to link umtx holding by thread */ 74 TAILQ_ENTRY(umtx_pi) pi_link; 75 76 /* List entry in hash */ 77 TAILQ_ENTRY(umtx_pi) pi_hashlink; 78 79 /* List for waiters */ 80 TAILQ_HEAD(,umtx_q) pi_blocked; 81 82 /* Identify a userland lock object */ 83 struct umtx_key pi_key; 84}; 85 86/* A userland synchronous object user. */ 87struct umtx_q { 88 /* Linked list for the hash. */ 89 TAILQ_ENTRY(umtx_q) uq_link; 90 91 /* Umtx key. */ 92 struct umtx_key uq_key; 93 94 /* Umtx flags. */ 95 int uq_flags; 96#define UQF_UMTXQ 0x0001 97 98 /* The thread waits on. */ 99 struct thread *uq_thread; 100 101 /* 102 * Blocked on PI mutex. read can use chain lock 103 * or umtx_lock, write must have both chain lock and 104 * umtx_lock being hold. 105 */ 106 struct umtx_pi *uq_pi_blocked; 107 108 /* On blocked list */ 109 TAILQ_ENTRY(umtx_q) uq_lockq; 110 111 /* Thread contending with us */ 112 TAILQ_HEAD(,umtx_pi) uq_pi_contested; 113 114 /* Inherited priority from PP mutex */ 115 u_char uq_inherited_pri; 116 117 /* Spare queue ready to be reused */ 118 struct umtxq_queue *uq_spare_queue; 119 120 /* The queue we on */ 121 struct umtxq_queue *uq_cur_queue; 122}; 123 124TAILQ_HEAD(umtxq_head, umtx_q); 125 126/* Per-key wait-queue */ 127struct umtxq_queue { 128 struct umtxq_head head; 129 struct umtx_key key; 130 LIST_ENTRY(umtxq_queue) link; 131 int length; 132}; 133 134LIST_HEAD(umtxq_list, umtxq_queue); 135 136/* Userland lock object's wait-queue chain */ 137struct umtxq_chain { 138 /* Lock for this chain. */ 139 struct mtx uc_lock; 140 141 /* List of sleep queues. */ 142 struct umtxq_list uc_queue[2]; 143#define UMTX_SHARED_QUEUE 0 144#define UMTX_EXCLUSIVE_QUEUE 1 145 146 LIST_HEAD(, umtxq_queue) uc_spare_queue; 147 148 /* Busy flag */ 149 char uc_busy; 150 151 /* Chain lock waiters */ 152 int uc_waiters; 153 154 /* All PI in the list */ 155 TAILQ_HEAD(,umtx_pi) uc_pi_list; 156 157}; 158 159#define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED) 160#define UMTXQ_BUSY_ASSERT(uc) KASSERT(&(uc)->uc_busy, ("umtx chain is not busy")) 161 162/* 163 * Don't propagate time-sharing priority, there is a security reason, 164 * a user can simply introduce PI-mutex, let thread A lock the mutex, 165 * and let another thread B block on the mutex, because B is 166 * sleeping, its priority will be boosted, this causes A's priority to 167 * be boosted via priority propagating too and will never be lowered even 168 * if it is using 100%CPU, this is unfair to other processes. 169 */ 170 171#define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\ 172 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\ 173 PRI_MAX_TIMESHARE : (td)->td_user_pri) 174 175#define GOLDEN_RATIO_PRIME 2654404609U 176#define UMTX_CHAINS 512 177#define UMTX_SHIFTS (__WORD_BIT - 9) 178 179#define GET_SHARE(flags) \ 180 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE) 181 182#define BUSY_SPINS 200 183 184static uma_zone_t umtx_pi_zone; 185static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS]; 186static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory"); 187static int umtx_pi_allocated; 188 189static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW, 0, "umtx debug"); 190SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD, 191 &umtx_pi_allocated, 0, "Allocated umtx_pi"); 192 193static void umtxq_sysinit(void *); 194static void umtxq_hash(struct umtx_key *key); 195static struct umtxq_chain *umtxq_getchain(struct umtx_key *key); 196static void umtxq_lock(struct umtx_key *key); 197static void umtxq_unlock(struct umtx_key *key); 198static void umtxq_busy(struct umtx_key *key); 199static void umtxq_unbusy(struct umtx_key *key); 200static void umtxq_insert_queue(struct umtx_q *uq, int q); 201static void umtxq_remove_queue(struct umtx_q *uq, int q); 202static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, int timo); 203static int umtxq_count(struct umtx_key *key); 204static struct umtx_pi *umtx_pi_alloc(int); 205static void umtx_pi_free(struct umtx_pi *pi); 206static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags); 207static void umtx_thread_cleanup(struct thread *td); 208static void umtx_exec_hook(void *arg __unused, struct proc *p __unused, 209 struct image_params *imgp __unused); 210SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL); 211 212#define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE) 213#define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE) 214#define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE) 215 216static struct mtx umtx_lock; 217 218static void 219umtxq_sysinit(void *arg __unused) 220{ 221 int i, j; 222 223 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi), 224 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 225 for (i = 0; i < 2; ++i) { 226 for (j = 0; j < UMTX_CHAINS; ++j) { 227 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL, 228 MTX_DEF | MTX_DUPOK); 229 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]); 230 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]); 231 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue); 232 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list); 233 umtxq_chains[i][j].uc_busy = 0; 234 umtxq_chains[i][j].uc_waiters = 0; 235 } 236 } 237 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_SPIN); 238 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL, 239 EVENTHANDLER_PRI_ANY); 240} 241 242struct umtx_q * 243umtxq_alloc(void) 244{ 245 struct umtx_q *uq; 246 247 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO); 248 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX, M_WAITOK | M_ZERO); 249 TAILQ_INIT(&uq->uq_spare_queue->head); 250 TAILQ_INIT(&uq->uq_pi_contested); 251 uq->uq_inherited_pri = PRI_MAX; 252 return (uq); 253} 254 255void 256umtxq_free(struct umtx_q *uq) 257{ 258 MPASS(uq->uq_spare_queue != NULL); 259 free(uq->uq_spare_queue, M_UMTX); 260 free(uq, M_UMTX); 261} 262 263static inline void 264umtxq_hash(struct umtx_key *key) 265{ 266 unsigned n = (uintptr_t)key->info.both.a + key->info.both.b; 267 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS; 268} 269 270static inline struct umtxq_chain * 271umtxq_getchain(struct umtx_key *key) 272{ 273 if (key->type <= TYPE_SEM) 274 return (&umtxq_chains[1][key->hash]); 275 return (&umtxq_chains[0][key->hash]); 276} 277 278/* 279 * Lock a chain. 280 */ 281static inline void 282umtxq_lock(struct umtx_key *key) 283{ 284 struct umtxq_chain *uc; 285 286 uc = umtxq_getchain(key); 287 mtx_lock(&uc->uc_lock); 288} 289 290/* 291 * Unlock a chain. 292 */ 293static inline void 294umtxq_unlock(struct umtx_key *key) 295{ 296 struct umtxq_chain *uc; 297 298 uc = umtxq_getchain(key); 299 mtx_unlock(&uc->uc_lock); 300} 301 302/* 303 * Set chain to busy state when following operation 304 * may be blocked (kernel mutex can not be used). 305 */ 306static inline void 307umtxq_busy(struct umtx_key *key) 308{ 309 struct umtxq_chain *uc; 310 311 uc = umtxq_getchain(key); 312 mtx_assert(&uc->uc_lock, MA_OWNED); 313 if (uc->uc_busy) { 314#ifdef SMP 315 if (smp_cpus > 1) { 316 int count = BUSY_SPINS; 317 if (count > 0) { 318 umtxq_unlock(key); 319 while (uc->uc_busy && --count > 0) 320 cpu_spinwait(); 321 umtxq_lock(key); 322 } 323 } 324#endif 325 while (uc->uc_busy) { 326 uc->uc_waiters++; 327 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0); 328 uc->uc_waiters--; 329 } 330 } 331 uc->uc_busy = 1; 332} 333 334/* 335 * Unbusy a chain. 336 */ 337static inline void 338umtxq_unbusy(struct umtx_key *key) 339{ 340 struct umtxq_chain *uc; 341 342 uc = umtxq_getchain(key); 343 mtx_assert(&uc->uc_lock, MA_OWNED); 344 KASSERT(uc->uc_busy != 0, ("not busy")); 345 uc->uc_busy = 0; 346 if (uc->uc_waiters) 347 wakeup_one(uc); 348} 349 350static struct umtxq_queue * 351umtxq_queue_lookup(struct umtx_key *key, int q) 352{ 353 struct umtxq_queue *uh; 354 struct umtxq_chain *uc; 355 356 uc = umtxq_getchain(key); 357 UMTXQ_LOCKED_ASSERT(uc); 358 LIST_FOREACH(uh, &uc->uc_queue[q], link) { 359 if (umtx_key_match(&uh->key, key)) 360 return (uh); 361 } 362 363 return (NULL); 364} 365 366static inline void 367umtxq_insert_queue(struct umtx_q *uq, int q) 368{ 369 struct umtxq_queue *uh; 370 struct umtxq_chain *uc; 371 372 uc = umtxq_getchain(&uq->uq_key); 373 UMTXQ_LOCKED_ASSERT(uc); 374 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue")); 375 uh = umtxq_queue_lookup(&uq->uq_key, q); 376 if (uh != NULL) { 377 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link); 378 } else { 379 uh = uq->uq_spare_queue; 380 uh->key = uq->uq_key; 381 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link); 382 } 383 uq->uq_spare_queue = NULL; 384 385 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link); 386 uh->length++; 387 uq->uq_flags |= UQF_UMTXQ; 388 uq->uq_cur_queue = uh; 389 return; 390} 391 392static inline void 393umtxq_remove_queue(struct umtx_q *uq, int q) 394{ 395 struct umtxq_chain *uc; 396 struct umtxq_queue *uh; 397 398 uc = umtxq_getchain(&uq->uq_key); 399 UMTXQ_LOCKED_ASSERT(uc); 400 if (uq->uq_flags & UQF_UMTXQ) { 401 uh = uq->uq_cur_queue; 402 TAILQ_REMOVE(&uh->head, uq, uq_link); 403 uh->length--; 404 uq->uq_flags &= ~UQF_UMTXQ; 405 if (TAILQ_EMPTY(&uh->head)) { 406 KASSERT(uh->length == 0, 407 ("inconsistent umtxq_queue length")); 408 LIST_REMOVE(uh, link); 409 } else { 410 uh = LIST_FIRST(&uc->uc_spare_queue); 411 KASSERT(uh != NULL, ("uc_spare_queue is empty")); 412 LIST_REMOVE(uh, link); 413 } 414 uq->uq_spare_queue = uh; 415 uq->uq_cur_queue = NULL; 416 } 417} 418 419/* 420 * Check if there are multiple waiters 421 */ 422static int 423umtxq_count(struct umtx_key *key) 424{ 425 struct umtxq_chain *uc; 426 struct umtxq_queue *uh; 427 428 uc = umtxq_getchain(key); 429 UMTXQ_LOCKED_ASSERT(uc); 430 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE); 431 if (uh != NULL) 432 return (uh->length); 433 return (0); 434} 435 436/* 437 * Check if there are multiple PI waiters and returns first 438 * waiter. 439 */ 440static int 441umtxq_count_pi(struct umtx_key *key, struct umtx_q **first) 442{ 443 struct umtxq_chain *uc; 444 struct umtxq_queue *uh; 445 446 *first = NULL; 447 uc = umtxq_getchain(key); 448 UMTXQ_LOCKED_ASSERT(uc); 449 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE); 450 if (uh != NULL) { 451 *first = TAILQ_FIRST(&uh->head); 452 return (uh->length); 453 } 454 return (0); 455} 456 457/* 458 * Wake up threads waiting on an userland object. 459 */ 460 461static int 462umtxq_signal_queue(struct umtx_key *key, int n_wake, int q) 463{ 464 struct umtxq_chain *uc; 465 struct umtxq_queue *uh; 466 struct umtx_q *uq; 467 int ret; 468 469 ret = 0; 470 uc = umtxq_getchain(key); 471 UMTXQ_LOCKED_ASSERT(uc); 472 uh = umtxq_queue_lookup(key, q); 473 if (uh != NULL) { 474 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) { 475 umtxq_remove_queue(uq, q); 476 wakeup(uq); 477 if (++ret >= n_wake) 478 return (ret); 479 } 480 } 481 return (ret); 482} 483 484 485/* 486 * Wake up specified thread. 487 */ 488static inline void 489umtxq_signal_thread(struct umtx_q *uq) 490{ 491 struct umtxq_chain *uc; 492 493 uc = umtxq_getchain(&uq->uq_key); 494 UMTXQ_LOCKED_ASSERT(uc); 495 umtxq_remove(uq); 496 wakeup(uq); 497} 498 499/* 500 * Put thread into sleep state, before sleeping, check if 501 * thread was removed from umtx queue. 502 */ 503static inline int 504umtxq_sleep(struct umtx_q *uq, const char *wmesg, int timo) 505{ 506 struct umtxq_chain *uc; 507 int error; 508 509 uc = umtxq_getchain(&uq->uq_key); 510 UMTXQ_LOCKED_ASSERT(uc); 511 if (!(uq->uq_flags & UQF_UMTXQ)) 512 return (0); 513 error = msleep(uq, &uc->uc_lock, PCATCH, wmesg, timo); 514 if (error == EWOULDBLOCK) 515 error = ETIMEDOUT; 516 return (error); 517} 518 519/* 520 * Convert userspace address into unique logical address. 521 */ 522int 523umtx_key_get(void *addr, int type, int share, struct umtx_key *key) 524{ 525 struct thread *td = curthread; 526 vm_map_t map; 527 vm_map_entry_t entry; 528 vm_pindex_t pindex; 529 vm_prot_t prot; 530 boolean_t wired; 531 532 key->type = type; 533 if (share == THREAD_SHARE) { 534 key->shared = 0; 535 key->info.private.vs = td->td_proc->p_vmspace; 536 key->info.private.addr = (uintptr_t)addr; 537 } else { 538 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE); 539 map = &td->td_proc->p_vmspace->vm_map; 540 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE, 541 &entry, &key->info.shared.object, &pindex, &prot, 542 &wired) != KERN_SUCCESS) { 543 return EFAULT; 544 } 545 546 if ((share == PROCESS_SHARE) || 547 (share == AUTO_SHARE && 548 VM_INHERIT_SHARE == entry->inheritance)) { 549 key->shared = 1; 550 key->info.shared.offset = entry->offset + entry->start - 551 (vm_offset_t)addr; 552 vm_object_reference(key->info.shared.object); 553 } else { 554 key->shared = 0; 555 key->info.private.vs = td->td_proc->p_vmspace; 556 key->info.private.addr = (uintptr_t)addr; 557 } 558 vm_map_lookup_done(map, entry); 559 } 560 561 umtxq_hash(key); 562 return (0); 563} 564 565/* 566 * Release key. 567 */ 568void 569umtx_key_release(struct umtx_key *key) 570{ 571 if (key->shared) 572 vm_object_deallocate(key->info.shared.object); 573} 574 575/* 576 * Lock a umtx object. 577 */ 578static int 579_do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id, int timo) 580{ 581 struct umtx_q *uq; 582 u_long owner; 583 u_long old; 584 int error = 0; 585 586 uq = td->td_umtxq; 587 588 /* 589 * Care must be exercised when dealing with umtx structure. It 590 * can fault on any access. 591 */ 592 for (;;) { 593 /* 594 * Try the uncontested case. This should be done in userland. 595 */ 596 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id); 597 598 /* The acquire succeeded. */ 599 if (owner == UMTX_UNOWNED) 600 return (0); 601 602 /* The address was invalid. */ 603 if (owner == -1) 604 return (EFAULT); 605 606 /* If no one owns it but it is contested try to acquire it. */ 607 if (owner == UMTX_CONTESTED) { 608 owner = casuword(&umtx->u_owner, 609 UMTX_CONTESTED, id | UMTX_CONTESTED); 610 611 if (owner == UMTX_CONTESTED) 612 return (0); 613 614 /* The address was invalid. */ 615 if (owner == -1) 616 return (EFAULT); 617 618 /* If this failed the lock has changed, restart. */ 619 continue; 620 } 621 622 /* 623 * If we caught a signal, we have retried and now 624 * exit immediately. 625 */ 626 if (error != 0) 627 return (error); 628 629 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, 630 AUTO_SHARE, &uq->uq_key)) != 0) 631 return (error); 632 633 umtxq_lock(&uq->uq_key); 634 umtxq_busy(&uq->uq_key); 635 umtxq_insert(uq); 636 umtxq_unbusy(&uq->uq_key); 637 umtxq_unlock(&uq->uq_key); 638 639 /* 640 * Set the contested bit so that a release in user space 641 * knows to use the system call for unlock. If this fails 642 * either some one else has acquired the lock or it has been 643 * released. 644 */ 645 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED); 646 647 /* The address was invalid. */ 648 if (old == -1) { 649 umtxq_lock(&uq->uq_key); 650 umtxq_remove(uq); 651 umtxq_unlock(&uq->uq_key); 652 umtx_key_release(&uq->uq_key); 653 return (EFAULT); 654 } 655 656 /* 657 * We set the contested bit, sleep. Otherwise the lock changed 658 * and we need to retry or we lost a race to the thread 659 * unlocking the umtx. 660 */ 661 umtxq_lock(&uq->uq_key); 662 if (old == owner) 663 error = umtxq_sleep(uq, "umtx", timo); 664 umtxq_remove(uq); 665 umtxq_unlock(&uq->uq_key); 666 umtx_key_release(&uq->uq_key); 667 } 668 669 return (0); 670} 671 672/* 673 * Lock a umtx object. 674 */ 675static int 676do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id, 677 struct timespec *timeout) 678{ 679 struct timespec ts, ts2, ts3; 680 struct timeval tv; 681 int error; 682 683 if (timeout == NULL) { 684 error = _do_lock_umtx(td, umtx, id, 0); 685 /* Mutex locking is restarted if it is interrupted. */ 686 if (error == EINTR) 687 error = ERESTART; 688 } else { 689 getnanouptime(&ts); 690 timespecadd(&ts, timeout); 691 TIMESPEC_TO_TIMEVAL(&tv, timeout); 692 for (;;) { 693 error = _do_lock_umtx(td, umtx, id, tvtohz(&tv)); 694 if (error != ETIMEDOUT) 695 break; 696 getnanouptime(&ts2); 697 if (timespeccmp(&ts2, &ts, >=)) { 698 error = ETIMEDOUT; 699 break; 700 } 701 ts3 = ts; 702 timespecsub(&ts3, &ts2); 703 TIMESPEC_TO_TIMEVAL(&tv, &ts3); 704 } 705 /* Timed-locking is not restarted. */ 706 if (error == ERESTART) 707 error = EINTR; 708 } 709 return (error); 710} 711 712/* 713 * Unlock a umtx object. 714 */ 715static int 716do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id) 717{ 718 struct umtx_key key; 719 u_long owner; 720 u_long old; 721 int error; 722 int count; 723 724 /* 725 * Make sure we own this mtx. 726 */ 727 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner)); 728 if (owner == -1) 729 return (EFAULT); 730 731 if ((owner & ~UMTX_CONTESTED) != id) 732 return (EPERM); 733 734 /* This should be done in userland */ 735 if ((owner & UMTX_CONTESTED) == 0) { 736 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED); 737 if (old == -1) 738 return (EFAULT); 739 if (old == owner) 740 return (0); 741 owner = old; 742 } 743 744 /* We should only ever be in here for contested locks */ 745 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE, 746 &key)) != 0) 747 return (error); 748 749 umtxq_lock(&key); 750 umtxq_busy(&key); 751 count = umtxq_count(&key); 752 umtxq_unlock(&key); 753 754 /* 755 * When unlocking the umtx, it must be marked as unowned if 756 * there is zero or one thread only waiting for it. 757 * Otherwise, it must be marked as contested. 758 */ 759 old = casuword(&umtx->u_owner, owner, 760 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED); 761 umtxq_lock(&key); 762 umtxq_signal(&key,1); 763 umtxq_unbusy(&key); 764 umtxq_unlock(&key); 765 umtx_key_release(&key); 766 if (old == -1) 767 return (EFAULT); 768 if (old != owner) 769 return (EINVAL); 770 return (0); 771} 772 773#ifdef COMPAT_FREEBSD32 774 775/* 776 * Lock a umtx object. 777 */ 778static int 779_do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id, int timo) 780{ 781 struct umtx_q *uq; 782 uint32_t owner; 783 uint32_t old; 784 int error = 0; 785 786 uq = td->td_umtxq; 787 788 /* 789 * Care must be exercised when dealing with umtx structure. It 790 * can fault on any access. 791 */ 792 for (;;) { 793 /* 794 * Try the uncontested case. This should be done in userland. 795 */ 796 owner = casuword32(m, UMUTEX_UNOWNED, id); 797 798 /* The acquire succeeded. */ 799 if (owner == UMUTEX_UNOWNED) 800 return (0); 801 802 /* The address was invalid. */ 803 if (owner == -1) 804 return (EFAULT); 805 806 /* If no one owns it but it is contested try to acquire it. */ 807 if (owner == UMUTEX_CONTESTED) { 808 owner = casuword32(m, 809 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED); 810 if (owner == UMUTEX_CONTESTED) 811 return (0); 812 813 /* The address was invalid. */ 814 if (owner == -1) 815 return (EFAULT); 816 817 /* If this failed the lock has changed, restart. */ 818 continue; 819 } 820 821 /* 822 * If we caught a signal, we have retried and now 823 * exit immediately. 824 */ 825 if (error != 0) 826 return (error); 827 828 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, 829 AUTO_SHARE, &uq->uq_key)) != 0) 830 return (error); 831 832 umtxq_lock(&uq->uq_key); 833 umtxq_busy(&uq->uq_key); 834 umtxq_insert(uq); 835 umtxq_unbusy(&uq->uq_key); 836 umtxq_unlock(&uq->uq_key); 837 838 /* 839 * Set the contested bit so that a release in user space 840 * knows to use the system call for unlock. If this fails 841 * either some one else has acquired the lock or it has been 842 * released. 843 */ 844 old = casuword32(m, owner, owner | UMUTEX_CONTESTED); 845 846 /* The address was invalid. */ 847 if (old == -1) { 848 umtxq_lock(&uq->uq_key); 849 umtxq_remove(uq); 850 umtxq_unlock(&uq->uq_key); 851 umtx_key_release(&uq->uq_key); 852 return (EFAULT); 853 } 854 855 /* 856 * We set the contested bit, sleep. Otherwise the lock changed 857 * and we need to retry or we lost a race to the thread 858 * unlocking the umtx. 859 */ 860 umtxq_lock(&uq->uq_key); 861 if (old == owner) 862 error = umtxq_sleep(uq, "umtx", timo); 863 umtxq_remove(uq); 864 umtxq_unlock(&uq->uq_key); 865 umtx_key_release(&uq->uq_key); 866 } 867 868 return (0); 869} 870 871/* 872 * Lock a umtx object. 873 */ 874static int 875do_lock_umtx32(struct thread *td, void *m, uint32_t id, 876 struct timespec *timeout) 877{ 878 struct timespec ts, ts2, ts3; 879 struct timeval tv; 880 int error; 881 882 if (timeout == NULL) { 883 error = _do_lock_umtx32(td, m, id, 0); 884 /* Mutex locking is restarted if it is interrupted. */ 885 if (error == EINTR) 886 error = ERESTART; 887 } else { 888 getnanouptime(&ts); 889 timespecadd(&ts, timeout); 890 TIMESPEC_TO_TIMEVAL(&tv, timeout); 891 for (;;) { 892 error = _do_lock_umtx32(td, m, id, tvtohz(&tv)); 893 if (error != ETIMEDOUT) 894 break; 895 getnanouptime(&ts2); 896 if (timespeccmp(&ts2, &ts, >=)) { 897 error = ETIMEDOUT; 898 break; 899 } 900 ts3 = ts; 901 timespecsub(&ts3, &ts2); 902 TIMESPEC_TO_TIMEVAL(&tv, &ts3); 903 } 904 /* Timed-locking is not restarted. */ 905 if (error == ERESTART) 906 error = EINTR; 907 } 908 return (error); 909} 910 911/* 912 * Unlock a umtx object. 913 */ 914static int 915do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id) 916{ 917 struct umtx_key key; 918 uint32_t owner; 919 uint32_t old; 920 int error; 921 int count; 922 923 /* 924 * Make sure we own this mtx. 925 */ 926 owner = fuword32(m); 927 if (owner == -1) 928 return (EFAULT); 929 930 if ((owner & ~UMUTEX_CONTESTED) != id) 931 return (EPERM); 932 933 /* This should be done in userland */ 934 if ((owner & UMUTEX_CONTESTED) == 0) { 935 old = casuword32(m, owner, UMUTEX_UNOWNED); 936 if (old == -1) 937 return (EFAULT); 938 if (old == owner) 939 return (0); 940 owner = old; 941 } 942 943 /* We should only ever be in here for contested locks */ 944 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE, 945 &key)) != 0) 946 return (error); 947 948 umtxq_lock(&key); 949 umtxq_busy(&key); 950 count = umtxq_count(&key); 951 umtxq_unlock(&key); 952 953 /* 954 * When unlocking the umtx, it must be marked as unowned if 955 * there is zero or one thread only waiting for it. 956 * Otherwise, it must be marked as contested. 957 */ 958 old = casuword32(m, owner, 959 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED); 960 umtxq_lock(&key); 961 umtxq_signal(&key,1); 962 umtxq_unbusy(&key); 963 umtxq_unlock(&key); 964 umtx_key_release(&key); 965 if (old == -1) 966 return (EFAULT); 967 if (old != owner) 968 return (EINVAL); 969 return (0); 970} 971#endif 972 973static inline int 974tstohz(const struct timespec *tsp) 975{ 976 struct timeval tv; 977 978 TIMESPEC_TO_TIMEVAL(&tv, tsp); 979 return tvtohz(&tv); 980} 981 982/* 983 * Fetch and compare value, sleep on the address if value is not changed. 984 */ 985static int 986do_wait(struct thread *td, void *addr, u_long id, 987 struct _umtx_time *timeout, int compat32, int is_private) 988{ 989 struct umtx_q *uq; 990 struct timespec ets, cts, tts; 991 u_long tmp; 992 int error = 0; 993 994 uq = td->td_umtxq; 995 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT, 996 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0) 997 return (error); 998 999 umtxq_lock(&uq->uq_key); 1000 umtxq_insert(uq); 1001 umtxq_unlock(&uq->uq_key); 1002 if (compat32 == 0) 1003 tmp = fuword(addr); 1004 else 1005 tmp = (unsigned int)fuword32(addr); 1006 if (tmp != id) { 1007 umtxq_lock(&uq->uq_key); 1008 umtxq_remove(uq); 1009 umtxq_unlock(&uq->uq_key); 1010 } else if (timeout == NULL) { 1011 umtxq_lock(&uq->uq_key); 1012 error = umtxq_sleep(uq, "uwait", 0); 1013 umtxq_remove(uq); 1014 umtxq_unlock(&uq->uq_key); 1015 } else { 1016 kern_clock_gettime(td, timeout->_clockid, &cts); 1017 if ((timeout->_flags & UMTX_ABSTIME) == 0) { 1018 ets = cts; 1019 timespecadd(&ets, &timeout->_timeout); 1020 } else { 1021 ets = timeout->_timeout; 1022 } 1023 umtxq_lock(&uq->uq_key); 1024 for (;;) { 1025 if (timespeccmp(&cts, &ets, >=)) { 1026 error = ETIMEDOUT; 1027 break; 1028 } 1029 tts = ets; 1030 timespecsub(&tts, &cts); 1031 error = umtxq_sleep(uq, "uwait", tstohz(&tts)); 1032 if (!(uq->uq_flags & UQF_UMTXQ)) { 1033 error = 0; 1034 break; 1035 } 1036 if (error != ETIMEDOUT) 1037 break; 1038 umtxq_unlock(&uq->uq_key); 1039 kern_clock_gettime(td, timeout->_clockid, &cts); 1040 umtxq_lock(&uq->uq_key); 1041 } 1042 umtxq_remove(uq); 1043 umtxq_unlock(&uq->uq_key); 1044 } 1045 umtx_key_release(&uq->uq_key); 1046 if (error == ERESTART) 1047 error = EINTR; 1048 return (error); 1049} 1050 1051/* 1052 * Wake up threads sleeping on the specified address. 1053 */ 1054int 1055kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private) 1056{ 1057 struct umtx_key key; 1058 int ret; 1059 1060 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT, 1061 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0) 1062 return (ret); 1063 umtxq_lock(&key); 1064 ret = umtxq_signal(&key, n_wake); 1065 umtxq_unlock(&key); 1066 umtx_key_release(&key); 1067 return (0); 1068} 1069 1070/* 1071 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex. 1072 */ 1073static int 1074_do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags, int timo, 1075 int mode) 1076{ 1077 struct umtx_q *uq; 1078 uint32_t owner, old, id; 1079 int error = 0; 1080 1081 id = td->td_tid; 1082 uq = td->td_umtxq; 1083 1084 /* 1085 * Care must be exercised when dealing with umtx structure. It 1086 * can fault on any access. 1087 */ 1088 for (;;) { 1089 owner = fuword32(__DEVOLATILE(void *, &m->m_owner)); 1090 if (mode == _UMUTEX_WAIT) { 1091 if (owner == UMUTEX_UNOWNED || owner == UMUTEX_CONTESTED) 1092 return (0); 1093 } else { 1094 /* 1095 * Try the uncontested case. This should be done in userland. 1096 */ 1097 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id); 1098 1099 /* The acquire succeeded. */ 1100 if (owner == UMUTEX_UNOWNED) 1101 return (0); 1102 1103 /* The address was invalid. */ 1104 if (owner == -1) 1105 return (EFAULT); 1106 1107 /* If no one owns it but it is contested try to acquire it. */ 1108 if (owner == UMUTEX_CONTESTED) { 1109 owner = casuword32(&m->m_owner, 1110 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED); 1111 1112 if (owner == UMUTEX_CONTESTED) 1113 return (0); 1114 1115 /* The address was invalid. */ 1116 if (owner == -1) 1117 return (EFAULT); 1118 1119 /* If this failed the lock has changed, restart. */ 1120 continue; 1121 } 1122 } 1123 1124 if ((flags & UMUTEX_ERROR_CHECK) != 0 && 1125 (owner & ~UMUTEX_CONTESTED) == id) 1126 return (EDEADLK); 1127 1128 if (mode == _UMUTEX_TRY) 1129 return (EBUSY); 1130 1131 /* 1132 * If we caught a signal, we have retried and now 1133 * exit immediately. 1134 */ 1135 if (error != 0) 1136 return (error); 1137 1138 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, 1139 GET_SHARE(flags), &uq->uq_key)) != 0) 1140 return (error); 1141 1142 umtxq_lock(&uq->uq_key); 1143 umtxq_busy(&uq->uq_key); 1144 umtxq_insert(uq); 1145 umtxq_unlock(&uq->uq_key); 1146 1147 /* 1148 * Set the contested bit so that a release in user space 1149 * knows to use the system call for unlock. If this fails 1150 * either some one else has acquired the lock or it has been 1151 * released. 1152 */ 1153 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED); 1154 1155 /* The address was invalid. */ 1156 if (old == -1) { 1157 umtxq_lock(&uq->uq_key); 1158 umtxq_remove(uq); 1159 umtxq_unbusy(&uq->uq_key); 1160 umtxq_unlock(&uq->uq_key); 1161 umtx_key_release(&uq->uq_key); 1162 return (EFAULT); 1163 } 1164 1165 /* 1166 * We set the contested bit, sleep. Otherwise the lock changed 1167 * and we need to retry or we lost a race to the thread 1168 * unlocking the umtx. 1169 */ 1170 umtxq_lock(&uq->uq_key); 1171 umtxq_unbusy(&uq->uq_key); 1172 if (old == owner) 1173 error = umtxq_sleep(uq, "umtxn", timo); 1174 umtxq_remove(uq); 1175 umtxq_unlock(&uq->uq_key); 1176 umtx_key_release(&uq->uq_key); 1177 } 1178 1179 return (0); 1180} 1181 1182/* 1183 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex. 1184 */ 1185/* 1186 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex. 1187 */ 1188static int 1189do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags) 1190{ 1191 struct umtx_key key; 1192 uint32_t owner, old, id; 1193 int error; 1194 int count; 1195 1196 id = td->td_tid; 1197 /* 1198 * Make sure we own this mtx. 1199 */ 1200 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner)); 1201 if (owner == -1) 1202 return (EFAULT); 1203 1204 if ((owner & ~UMUTEX_CONTESTED) != id) 1205 return (EPERM); 1206 1207 if ((owner & UMUTEX_CONTESTED) == 0) { 1208 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED); 1209 if (old == -1) 1210 return (EFAULT); 1211 if (old == owner) 1212 return (0); 1213 owner = old; 1214 } 1215 1216 /* We should only ever be in here for contested locks */ 1217 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags), 1218 &key)) != 0) 1219 return (error); 1220 1221 umtxq_lock(&key); 1222 umtxq_busy(&key); 1223 count = umtxq_count(&key); 1224 umtxq_unlock(&key); 1225 1226 /* 1227 * When unlocking the umtx, it must be marked as unowned if 1228 * there is zero or one thread only waiting for it. 1229 * Otherwise, it must be marked as contested. 1230 */ 1231 old = casuword32(&m->m_owner, owner, 1232 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED); 1233 umtxq_lock(&key); 1234 umtxq_signal(&key,1); 1235 umtxq_unbusy(&key); 1236 umtxq_unlock(&key); 1237 umtx_key_release(&key); 1238 if (old == -1) 1239 return (EFAULT); 1240 if (old != owner) 1241 return (EINVAL); 1242 return (0); 1243} 1244 1245/* 1246 * Check if the mutex is available and wake up a waiter, 1247 * only for simple mutex. 1248 */ 1249static int 1250do_wake_umutex(struct thread *td, struct umutex *m) 1251{ 1252 struct umtx_key key; 1253 uint32_t owner; 1254 uint32_t flags; 1255 int error; 1256 int count; 1257 1258 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner)); 1259 if (owner == -1) 1260 return (EFAULT); 1261 1262 if ((owner & ~UMUTEX_CONTESTED) != 0) 1263 return (0); 1264 1265 flags = fuword32(&m->m_flags); 1266 1267 /* We should only ever be in here for contested locks */ 1268 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags), 1269 &key)) != 0) 1270 return (error); 1271 1272 umtxq_lock(&key); 1273 umtxq_busy(&key); 1274 count = umtxq_count(&key); 1275 umtxq_unlock(&key); 1276 1277 if (count <= 1) 1278 owner = casuword32(&m->m_owner, UMUTEX_CONTESTED, UMUTEX_UNOWNED); 1279 1280 umtxq_lock(&key); 1281 if (count != 0 && (owner & ~UMUTEX_CONTESTED) == 0) 1282 umtxq_signal(&key, 1); 1283 umtxq_unbusy(&key); 1284 umtxq_unlock(&key); 1285 umtx_key_release(&key); 1286 return (0); 1287} 1288 1289static inline struct umtx_pi * 1290umtx_pi_alloc(int flags) 1291{ 1292 struct umtx_pi *pi; 1293 1294 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags); 1295 TAILQ_INIT(&pi->pi_blocked); 1296 atomic_add_int(&umtx_pi_allocated, 1); 1297 return (pi); 1298} 1299 1300static inline void 1301umtx_pi_free(struct umtx_pi *pi) 1302{ 1303 uma_zfree(umtx_pi_zone, pi); 1304 atomic_add_int(&umtx_pi_allocated, -1); 1305} 1306 1307/* 1308 * Adjust the thread's position on a pi_state after its priority has been 1309 * changed. 1310 */ 1311static int 1312umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td) 1313{ 1314 struct umtx_q *uq, *uq1, *uq2; 1315 struct thread *td1; 1316 1317 mtx_assert(&umtx_lock, MA_OWNED); 1318 if (pi == NULL) 1319 return (0); 1320 1321 uq = td->td_umtxq; 1322 1323 /* 1324 * Check if the thread needs to be moved on the blocked chain. 1325 * It needs to be moved if either its priority is lower than 1326 * the previous thread or higher than the next thread. 1327 */ 1328 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq); 1329 uq2 = TAILQ_NEXT(uq, uq_lockq); 1330 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) || 1331 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) { 1332 /* 1333 * Remove thread from blocked chain and determine where 1334 * it should be moved to. 1335 */ 1336 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq); 1337 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) { 1338 td1 = uq1->uq_thread; 1339 MPASS(td1->td_proc->p_magic == P_MAGIC); 1340 if (UPRI(td1) > UPRI(td)) 1341 break; 1342 } 1343 1344 if (uq1 == NULL) 1345 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq); 1346 else 1347 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq); 1348 } 1349 return (1); 1350} 1351 1352/* 1353 * Propagate priority when a thread is blocked on POSIX 1354 * PI mutex. 1355 */ 1356static void 1357umtx_propagate_priority(struct thread *td) 1358{ 1359 struct umtx_q *uq; 1360 struct umtx_pi *pi; 1361 int pri; 1362 1363 mtx_assert(&umtx_lock, MA_OWNED); 1364 pri = UPRI(td); 1365 uq = td->td_umtxq; 1366 pi = uq->uq_pi_blocked; 1367 if (pi == NULL) 1368 return; 1369 1370 for (;;) { 1371 td = pi->pi_owner; 1372 if (td == NULL || td == curthread) 1373 return; 1374 1375 MPASS(td->td_proc != NULL); 1376 MPASS(td->td_proc->p_magic == P_MAGIC); 1377 1378 thread_lock(td); 1379 if (td->td_lend_user_pri > pri) 1380 sched_lend_user_prio(td, pri); 1381 else { 1382 thread_unlock(td); 1383 break; 1384 } 1385 thread_unlock(td); 1386 1387 /* 1388 * Pick up the lock that td is blocked on. 1389 */ 1390 uq = td->td_umtxq; 1391 pi = uq->uq_pi_blocked; 1392 if (pi == NULL) 1393 break; 1394 /* Resort td on the list if needed. */ 1395 umtx_pi_adjust_thread(pi, td); 1396 } 1397} 1398 1399/* 1400 * Unpropagate priority for a PI mutex when a thread blocked on 1401 * it is interrupted by signal or resumed by others. 1402 */ 1403static void 1404umtx_repropagate_priority(struct umtx_pi *pi) 1405{ 1406 struct umtx_q *uq, *uq_owner; 1407 struct umtx_pi *pi2; 1408 int pri; 1409 1410 mtx_assert(&umtx_lock, MA_OWNED); 1411 1412 while (pi != NULL && pi->pi_owner != NULL) { 1413 pri = PRI_MAX; 1414 uq_owner = pi->pi_owner->td_umtxq; 1415 1416 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) { 1417 uq = TAILQ_FIRST(&pi2->pi_blocked); 1418 if (uq != NULL) { 1419 if (pri > UPRI(uq->uq_thread)) 1420 pri = UPRI(uq->uq_thread); 1421 } 1422 } 1423 1424 if (pri > uq_owner->uq_inherited_pri) 1425 pri = uq_owner->uq_inherited_pri; 1426 thread_lock(pi->pi_owner); 1427 sched_lend_user_prio(pi->pi_owner, pri); 1428 thread_unlock(pi->pi_owner); 1429 if ((pi = uq_owner->uq_pi_blocked) != NULL) 1430 umtx_pi_adjust_thread(pi, uq_owner->uq_thread); 1431 } 1432} 1433 1434/* 1435 * Insert a PI mutex into owned list. 1436 */ 1437static void 1438umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner) 1439{ 1440 struct umtx_q *uq_owner; 1441 1442 uq_owner = owner->td_umtxq; 1443 mtx_assert(&umtx_lock, MA_OWNED); 1444 if (pi->pi_owner != NULL) 1445 panic("pi_ower != NULL"); 1446 pi->pi_owner = owner; 1447 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link); 1448} 1449 1450/* 1451 * Claim ownership of a PI mutex. 1452 */ 1453static int 1454umtx_pi_claim(struct umtx_pi *pi, struct thread *owner) 1455{ 1456 struct umtx_q *uq, *uq_owner; 1457 1458 uq_owner = owner->td_umtxq; 1459 mtx_lock_spin(&umtx_lock); 1460 if (pi->pi_owner == owner) { 1461 mtx_unlock_spin(&umtx_lock); 1462 return (0); 1463 } 1464 1465 if (pi->pi_owner != NULL) { 1466 /* 1467 * userland may have already messed the mutex, sigh. 1468 */ 1469 mtx_unlock_spin(&umtx_lock); 1470 return (EPERM); 1471 } 1472 umtx_pi_setowner(pi, owner); 1473 uq = TAILQ_FIRST(&pi->pi_blocked); 1474 if (uq != NULL) { 1475 int pri; 1476 1477 pri = UPRI(uq->uq_thread); 1478 thread_lock(owner); 1479 if (pri < UPRI(owner)) 1480 sched_lend_user_prio(owner, pri); 1481 thread_unlock(owner); 1482 } 1483 mtx_unlock_spin(&umtx_lock); 1484 return (0); 1485} 1486 1487/* 1488 * Adjust a thread's order position in its blocked PI mutex, 1489 * this may result new priority propagating process. 1490 */ 1491void 1492umtx_pi_adjust(struct thread *td, u_char oldpri) 1493{ 1494 struct umtx_q *uq; 1495 struct umtx_pi *pi; 1496 1497 uq = td->td_umtxq; 1498 mtx_lock_spin(&umtx_lock); 1499 /* 1500 * Pick up the lock that td is blocked on. 1501 */ 1502 pi = uq->uq_pi_blocked; 1503 if (pi != NULL) { 1504 umtx_pi_adjust_thread(pi, td); 1505 umtx_repropagate_priority(pi); 1506 } 1507 mtx_unlock_spin(&umtx_lock); 1508} 1509 1510/* 1511 * Sleep on a PI mutex. 1512 */ 1513static int 1514umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, 1515 uint32_t owner, const char *wmesg, int timo) 1516{ 1517 struct umtxq_chain *uc; 1518 struct thread *td, *td1; 1519 struct umtx_q *uq1; 1520 int pri; 1521 int error = 0; 1522 1523 td = uq->uq_thread; 1524 KASSERT(td == curthread, ("inconsistent uq_thread")); 1525 uc = umtxq_getchain(&uq->uq_key); 1526 UMTXQ_LOCKED_ASSERT(uc); 1527 UMTXQ_BUSY_ASSERT(uc); 1528 umtxq_insert(uq); 1529 mtx_lock_spin(&umtx_lock); 1530 if (pi->pi_owner == NULL) { 1531 mtx_unlock_spin(&umtx_lock); 1532 /* XXX Only look up thread in current process. */ 1533 td1 = tdfind(owner, curproc->p_pid); 1534 mtx_lock_spin(&umtx_lock); 1535 if (td1 != NULL) { 1536 if (pi->pi_owner == NULL) 1537 umtx_pi_setowner(pi, td1); 1538 PROC_UNLOCK(td1->td_proc); 1539 } 1540 } 1541 1542 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) { 1543 pri = UPRI(uq1->uq_thread); 1544 if (pri > UPRI(td)) 1545 break; 1546 } 1547 1548 if (uq1 != NULL) 1549 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq); 1550 else 1551 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq); 1552 1553 uq->uq_pi_blocked = pi; 1554 thread_lock(td); 1555 td->td_flags |= TDF_UPIBLOCKED; 1556 thread_unlock(td); 1557 umtx_propagate_priority(td); 1558 mtx_unlock_spin(&umtx_lock); 1559 umtxq_unbusy(&uq->uq_key); 1560 1561 if (uq->uq_flags & UQF_UMTXQ) { 1562 error = msleep(uq, &uc->uc_lock, PCATCH, wmesg, timo); 1563 if (error == EWOULDBLOCK) 1564 error = ETIMEDOUT; 1565 if (uq->uq_flags & UQF_UMTXQ) { 1566 umtxq_remove(uq); 1567 } 1568 } 1569 mtx_lock_spin(&umtx_lock); 1570 uq->uq_pi_blocked = NULL; 1571 thread_lock(td); 1572 td->td_flags &= ~TDF_UPIBLOCKED; 1573 thread_unlock(td); 1574 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq); 1575 umtx_repropagate_priority(pi); 1576 mtx_unlock_spin(&umtx_lock); 1577 umtxq_unlock(&uq->uq_key); 1578 1579 return (error); 1580} 1581 1582/* 1583 * Add reference count for a PI mutex. 1584 */ 1585static void 1586umtx_pi_ref(struct umtx_pi *pi) 1587{ 1588 struct umtxq_chain *uc; 1589 1590 uc = umtxq_getchain(&pi->pi_key); 1591 UMTXQ_LOCKED_ASSERT(uc); 1592 pi->pi_refcount++; 1593} 1594 1595/* 1596 * Decrease reference count for a PI mutex, if the counter 1597 * is decreased to zero, its memory space is freed. 1598 */ 1599static void 1600umtx_pi_unref(struct umtx_pi *pi) 1601{ 1602 struct umtxq_chain *uc; 1603 1604 uc = umtxq_getchain(&pi->pi_key); 1605 UMTXQ_LOCKED_ASSERT(uc); 1606 KASSERT(pi->pi_refcount > 0, ("invalid reference count")); 1607 if (--pi->pi_refcount == 0) { 1608 mtx_lock_spin(&umtx_lock); 1609 if (pi->pi_owner != NULL) { 1610 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, 1611 pi, pi_link); 1612 pi->pi_owner = NULL; 1613 } 1614 KASSERT(TAILQ_EMPTY(&pi->pi_blocked), 1615 ("blocked queue not empty")); 1616 mtx_unlock_spin(&umtx_lock); 1617 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink); 1618 umtx_pi_free(pi); 1619 } 1620} 1621 1622/* 1623 * Find a PI mutex in hash table. 1624 */ 1625static struct umtx_pi * 1626umtx_pi_lookup(struct umtx_key *key) 1627{ 1628 struct umtxq_chain *uc; 1629 struct umtx_pi *pi; 1630 1631 uc = umtxq_getchain(key); 1632 UMTXQ_LOCKED_ASSERT(uc); 1633 1634 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) { 1635 if (umtx_key_match(&pi->pi_key, key)) { 1636 return (pi); 1637 } 1638 } 1639 return (NULL); 1640} 1641 1642/* 1643 * Insert a PI mutex into hash table. 1644 */ 1645static inline void 1646umtx_pi_insert(struct umtx_pi *pi) 1647{ 1648 struct umtxq_chain *uc; 1649 1650 uc = umtxq_getchain(&pi->pi_key); 1651 UMTXQ_LOCKED_ASSERT(uc); 1652 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink); 1653} 1654 1655/* 1656 * Lock a PI mutex. 1657 */ 1658static int 1659_do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags, int timo, 1660 int try) 1661{ 1662 struct umtx_q *uq; 1663 struct umtx_pi *pi, *new_pi; 1664 uint32_t id, owner, old; 1665 int error; 1666 1667 id = td->td_tid; 1668 uq = td->td_umtxq; 1669 1670 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags), 1671 &uq->uq_key)) != 0) 1672 return (error); 1673 umtxq_lock(&uq->uq_key); 1674 pi = umtx_pi_lookup(&uq->uq_key); 1675 if (pi == NULL) { 1676 new_pi = umtx_pi_alloc(M_NOWAIT); 1677 if (new_pi == NULL) { 1678 umtxq_unlock(&uq->uq_key); 1679 new_pi = umtx_pi_alloc(M_WAITOK); 1680 umtxq_lock(&uq->uq_key); 1681 pi = umtx_pi_lookup(&uq->uq_key); 1682 if (pi != NULL) { 1683 umtx_pi_free(new_pi); 1684 new_pi = NULL; 1685 } 1686 } 1687 if (new_pi != NULL) { 1688 new_pi->pi_key = uq->uq_key; 1689 umtx_pi_insert(new_pi); 1690 pi = new_pi; 1691 } 1692 } 1693 umtx_pi_ref(pi); 1694 umtxq_unlock(&uq->uq_key); 1695 1696 /* 1697 * Care must be exercised when dealing with umtx structure. It 1698 * can fault on any access. 1699 */ 1700 for (;;) { 1701 /* 1702 * Try the uncontested case. This should be done in userland. 1703 */ 1704 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id); 1705 1706 /* The acquire succeeded. */ 1707 if (owner == UMUTEX_UNOWNED) { 1708 error = 0; 1709 break; 1710 } 1711 1712 /* The address was invalid. */ 1713 if (owner == -1) { 1714 error = EFAULT; 1715 break; 1716 } 1717 1718 /* If no one owns it but it is contested try to acquire it. */ 1719 if (owner == UMUTEX_CONTESTED) { 1720 owner = casuword32(&m->m_owner, 1721 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED); 1722 1723 if (owner == UMUTEX_CONTESTED) { 1724 umtxq_lock(&uq->uq_key); 1725 umtxq_busy(&uq->uq_key); 1726 error = umtx_pi_claim(pi, td); 1727 umtxq_unbusy(&uq->uq_key); 1728 umtxq_unlock(&uq->uq_key); 1729 break; 1730 } 1731 1732 /* The address was invalid. */ 1733 if (owner == -1) { 1734 error = EFAULT; 1735 break; 1736 } 1737 1738 /* If this failed the lock has changed, restart. */ 1739 continue; 1740 } 1741 1742 if ((flags & UMUTEX_ERROR_CHECK) != 0 && 1743 (owner & ~UMUTEX_CONTESTED) == id) { 1744 error = EDEADLK; 1745 break; 1746 } 1747 1748 if (try != 0) { 1749 error = EBUSY; 1750 break; 1751 } 1752 1753 /* 1754 * If we caught a signal, we have retried and now 1755 * exit immediately. 1756 */ 1757 if (error != 0) 1758 break; 1759 1760 umtxq_lock(&uq->uq_key); 1761 umtxq_busy(&uq->uq_key); 1762 umtxq_unlock(&uq->uq_key); 1763 1764 /* 1765 * Set the contested bit so that a release in user space 1766 * knows to use the system call for unlock. If this fails 1767 * either some one else has acquired the lock or it has been 1768 * released. 1769 */ 1770 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED); 1771 1772 /* The address was invalid. */ 1773 if (old == -1) { 1774 umtxq_lock(&uq->uq_key); 1775 umtxq_unbusy(&uq->uq_key); 1776 umtxq_unlock(&uq->uq_key); 1777 error = EFAULT; 1778 break; 1779 } 1780 1781 umtxq_lock(&uq->uq_key); 1782 /* 1783 * We set the contested bit, sleep. Otherwise the lock changed 1784 * and we need to retry or we lost a race to the thread 1785 * unlocking the umtx. 1786 */ 1787 if (old == owner) 1788 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED, 1789 "umtxpi", timo); 1790 else { 1791 umtxq_unbusy(&uq->uq_key); 1792 umtxq_unlock(&uq->uq_key); 1793 } 1794 } 1795 1796 umtxq_lock(&uq->uq_key); 1797 umtx_pi_unref(pi); 1798 umtxq_unlock(&uq->uq_key); 1799 1800 umtx_key_release(&uq->uq_key); 1801 return (error); 1802} 1803 1804/* 1805 * Unlock a PI mutex. 1806 */ 1807static int 1808do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags) 1809{ 1810 struct umtx_key key; 1811 struct umtx_q *uq_first, *uq_first2, *uq_me; 1812 struct umtx_pi *pi, *pi2; 1813 uint32_t owner, old, id; 1814 int error; 1815 int count; 1816 int pri; 1817 1818 id = td->td_tid; 1819 /* 1820 * Make sure we own this mtx. 1821 */ 1822 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner)); 1823 if (owner == -1) 1824 return (EFAULT); 1825 1826 if ((owner & ~UMUTEX_CONTESTED) != id) 1827 return (EPERM); 1828 1829 /* This should be done in userland */ 1830 if ((owner & UMUTEX_CONTESTED) == 0) { 1831 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED); 1832 if (old == -1) 1833 return (EFAULT); 1834 if (old == owner) 1835 return (0); 1836 owner = old; 1837 } 1838 1839 /* We should only ever be in here for contested locks */ 1840 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags), 1841 &key)) != 0) 1842 return (error); 1843 1844 umtxq_lock(&key); 1845 umtxq_busy(&key); 1846 count = umtxq_count_pi(&key, &uq_first); 1847 if (uq_first != NULL) { 1848 mtx_lock_spin(&umtx_lock); 1849 pi = uq_first->uq_pi_blocked; 1850 KASSERT(pi != NULL, ("pi == NULL?")); 1851 if (pi->pi_owner != curthread) { 1852 mtx_unlock_spin(&umtx_lock); 1853 umtxq_unbusy(&key); 1854 umtxq_unlock(&key); 1855 umtx_key_release(&key); 1856 /* userland messed the mutex */ 1857 return (EPERM); 1858 } 1859 uq_me = curthread->td_umtxq; 1860 pi->pi_owner = NULL; 1861 TAILQ_REMOVE(&uq_me->uq_pi_contested, pi, pi_link); 1862 /* get highest priority thread which is still sleeping. */ 1863 uq_first = TAILQ_FIRST(&pi->pi_blocked); 1864 while (uq_first != NULL && 1865 (uq_first->uq_flags & UQF_UMTXQ) == 0) { 1866 uq_first = TAILQ_NEXT(uq_first, uq_lockq); 1867 } 1868 pri = PRI_MAX; 1869 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) { 1870 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked); 1871 if (uq_first2 != NULL) { 1872 if (pri > UPRI(uq_first2->uq_thread)) 1873 pri = UPRI(uq_first2->uq_thread); 1874 } 1875 } 1876 thread_lock(curthread); 1877 sched_lend_user_prio(curthread, pri); 1878 thread_unlock(curthread); 1879 mtx_unlock_spin(&umtx_lock); 1880 if (uq_first) 1881 umtxq_signal_thread(uq_first); 1882 } 1883 umtxq_unlock(&key); 1884 1885 /* 1886 * When unlocking the umtx, it must be marked as unowned if 1887 * there is zero or one thread only waiting for it. 1888 * Otherwise, it must be marked as contested. 1889 */ 1890 old = casuword32(&m->m_owner, owner, 1891 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED); 1892 1893 umtxq_lock(&key); 1894 umtxq_unbusy(&key); 1895 umtxq_unlock(&key); 1896 umtx_key_release(&key); 1897 if (old == -1) 1898 return (EFAULT); 1899 if (old != owner) 1900 return (EINVAL); 1901 return (0); 1902} 1903 1904/* 1905 * Lock a PP mutex. 1906 */ 1907static int 1908_do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags, int timo, 1909 int try) 1910{ 1911 struct umtx_q *uq, *uq2; 1912 struct umtx_pi *pi; 1913 uint32_t ceiling; 1914 uint32_t owner, id; 1915 int error, pri, old_inherited_pri, su; 1916 1917 id = td->td_tid; 1918 uq = td->td_umtxq; 1919 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags), 1920 &uq->uq_key)) != 0) 1921 return (error); 1922 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0); 1923 for (;;) { 1924 old_inherited_pri = uq->uq_inherited_pri; 1925 umtxq_lock(&uq->uq_key); 1926 umtxq_busy(&uq->uq_key); 1927 umtxq_unlock(&uq->uq_key); 1928 1929 ceiling = RTP_PRIO_MAX - fuword32(&m->m_ceilings[0]); 1930 if (ceiling > RTP_PRIO_MAX) { 1931 error = EINVAL; 1932 goto out; 1933 } 1934 1935 mtx_lock_spin(&umtx_lock); 1936 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) { 1937 mtx_unlock_spin(&umtx_lock); 1938 error = EINVAL; 1939 goto out; 1940 } 1941 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) { 1942 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling; 1943 thread_lock(td); 1944 if (uq->uq_inherited_pri < UPRI(td)) 1945 sched_lend_user_prio(td, uq->uq_inherited_pri); 1946 thread_unlock(td); 1947 } 1948 mtx_unlock_spin(&umtx_lock); 1949 1950 owner = casuword32(&m->m_owner, 1951 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED); 1952 1953 if (owner == UMUTEX_CONTESTED) { 1954 error = 0; 1955 break; 1956 } 1957 1958 /* The address was invalid. */ 1959 if (owner == -1) { 1960 error = EFAULT; 1961 break; 1962 } 1963 1964 if ((flags & UMUTEX_ERROR_CHECK) != 0 && 1965 (owner & ~UMUTEX_CONTESTED) == id) { 1966 error = EDEADLK; 1967 break; 1968 } 1969 1970 if (try != 0) { 1971 error = EBUSY; 1972 break; 1973 } 1974 1975 /* 1976 * If we caught a signal, we have retried and now 1977 * exit immediately. 1978 */ 1979 if (error != 0) 1980 break; 1981 1982 umtxq_lock(&uq->uq_key); 1983 umtxq_insert(uq); 1984 umtxq_unbusy(&uq->uq_key); 1985 error = umtxq_sleep(uq, "umtxpp", timo); 1986 umtxq_remove(uq); 1987 umtxq_unlock(&uq->uq_key); 1988 1989 mtx_lock_spin(&umtx_lock); 1990 uq->uq_inherited_pri = old_inherited_pri; 1991 pri = PRI_MAX; 1992 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) { 1993 uq2 = TAILQ_FIRST(&pi->pi_blocked); 1994 if (uq2 != NULL) { 1995 if (pri > UPRI(uq2->uq_thread)) 1996 pri = UPRI(uq2->uq_thread); 1997 } 1998 } 1999 if (pri > uq->uq_inherited_pri) 2000 pri = uq->uq_inherited_pri; 2001 thread_lock(td); 2002 sched_lend_user_prio(td, pri); 2003 thread_unlock(td); 2004 mtx_unlock_spin(&umtx_lock); 2005 } 2006 2007 if (error != 0) { 2008 mtx_lock_spin(&umtx_lock); 2009 uq->uq_inherited_pri = old_inherited_pri; 2010 pri = PRI_MAX; 2011 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) { 2012 uq2 = TAILQ_FIRST(&pi->pi_blocked); 2013 if (uq2 != NULL) { 2014 if (pri > UPRI(uq2->uq_thread)) 2015 pri = UPRI(uq2->uq_thread); 2016 } 2017 } 2018 if (pri > uq->uq_inherited_pri) 2019 pri = uq->uq_inherited_pri; 2020 thread_lock(td); 2021 sched_lend_user_prio(td, pri); 2022 thread_unlock(td); 2023 mtx_unlock_spin(&umtx_lock); 2024 } 2025 2026out: 2027 umtxq_lock(&uq->uq_key); 2028 umtxq_unbusy(&uq->uq_key); 2029 umtxq_unlock(&uq->uq_key); 2030 umtx_key_release(&uq->uq_key); 2031 return (error); 2032} 2033 2034/* 2035 * Unlock a PP mutex. 2036 */ 2037static int 2038do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags) 2039{ 2040 struct umtx_key key; 2041 struct umtx_q *uq, *uq2; 2042 struct umtx_pi *pi; 2043 uint32_t owner, id; 2044 uint32_t rceiling; 2045 int error, pri, new_inherited_pri, su; 2046 2047 id = td->td_tid; 2048 uq = td->td_umtxq; 2049 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0); 2050 2051 /* 2052 * Make sure we own this mtx. 2053 */ 2054 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner)); 2055 if (owner == -1) 2056 return (EFAULT); 2057 2058 if ((owner & ~UMUTEX_CONTESTED) != id) 2059 return (EPERM); 2060 2061 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t)); 2062 if (error != 0) 2063 return (error); 2064 2065 if (rceiling == -1) 2066 new_inherited_pri = PRI_MAX; 2067 else { 2068 rceiling = RTP_PRIO_MAX - rceiling; 2069 if (rceiling > RTP_PRIO_MAX) 2070 return (EINVAL); 2071 new_inherited_pri = PRI_MIN_REALTIME + rceiling; 2072 } 2073 2074 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags), 2075 &key)) != 0) 2076 return (error); 2077 umtxq_lock(&key); 2078 umtxq_busy(&key); 2079 umtxq_unlock(&key); 2080 /* 2081 * For priority protected mutex, always set unlocked state 2082 * to UMUTEX_CONTESTED, so that userland always enters kernel 2083 * to lock the mutex, it is necessary because thread priority 2084 * has to be adjusted for such mutex. 2085 */ 2086 error = suword32(__DEVOLATILE(uint32_t *, &m->m_owner), 2087 UMUTEX_CONTESTED); 2088 2089 umtxq_lock(&key); 2090 if (error == 0) 2091 umtxq_signal(&key, 1); 2092 umtxq_unbusy(&key); 2093 umtxq_unlock(&key); 2094 2095 if (error == -1) 2096 error = EFAULT; 2097 else { 2098 mtx_lock_spin(&umtx_lock); 2099 if (su != 0) 2100 uq->uq_inherited_pri = new_inherited_pri; 2101 pri = PRI_MAX; 2102 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) { 2103 uq2 = TAILQ_FIRST(&pi->pi_blocked); 2104 if (uq2 != NULL) { 2105 if (pri > UPRI(uq2->uq_thread)) 2106 pri = UPRI(uq2->uq_thread); 2107 } 2108 } 2109 if (pri > uq->uq_inherited_pri) 2110 pri = uq->uq_inherited_pri; 2111 thread_lock(td); 2112 sched_lend_user_prio(td, pri); 2113 thread_unlock(td); 2114 mtx_unlock_spin(&umtx_lock); 2115 } 2116 umtx_key_release(&key); 2117 return (error); 2118} 2119 2120static int 2121do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling, 2122 uint32_t *old_ceiling) 2123{ 2124 struct umtx_q *uq; 2125 uint32_t save_ceiling; 2126 uint32_t owner, id; 2127 uint32_t flags; 2128 int error; 2129 2130 flags = fuword32(&m->m_flags); 2131 if ((flags & UMUTEX_PRIO_PROTECT) == 0) 2132 return (EINVAL); 2133 if (ceiling > RTP_PRIO_MAX) 2134 return (EINVAL); 2135 id = td->td_tid; 2136 uq = td->td_umtxq; 2137 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags), 2138 &uq->uq_key)) != 0) 2139 return (error); 2140 for (;;) { 2141 umtxq_lock(&uq->uq_key); 2142 umtxq_busy(&uq->uq_key); 2143 umtxq_unlock(&uq->uq_key); 2144 2145 save_ceiling = fuword32(&m->m_ceilings[0]); 2146 2147 owner = casuword32(&m->m_owner, 2148 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED); 2149 2150 if (owner == UMUTEX_CONTESTED) { 2151 suword32(&m->m_ceilings[0], ceiling); 2152 suword32(__DEVOLATILE(uint32_t *, &m->m_owner), 2153 UMUTEX_CONTESTED); 2154 error = 0; 2155 break; 2156 } 2157 2158 /* The address was invalid. */ 2159 if (owner == -1) { 2160 error = EFAULT; 2161 break; 2162 } 2163 2164 if ((owner & ~UMUTEX_CONTESTED) == id) { 2165 suword32(&m->m_ceilings[0], ceiling); 2166 error = 0; 2167 break; 2168 } 2169 2170 /* 2171 * If we caught a signal, we have retried and now 2172 * exit immediately. 2173 */ 2174 if (error != 0) 2175 break; 2176 2177 /* 2178 * We set the contested bit, sleep. Otherwise the lock changed 2179 * and we need to retry or we lost a race to the thread 2180 * unlocking the umtx. 2181 */ 2182 umtxq_lock(&uq->uq_key); 2183 umtxq_insert(uq); 2184 umtxq_unbusy(&uq->uq_key); 2185 error = umtxq_sleep(uq, "umtxpp", 0); 2186 umtxq_remove(uq); 2187 umtxq_unlock(&uq->uq_key); 2188 } 2189 umtxq_lock(&uq->uq_key); 2190 if (error == 0) 2191 umtxq_signal(&uq->uq_key, INT_MAX); 2192 umtxq_unbusy(&uq->uq_key); 2193 umtxq_unlock(&uq->uq_key); 2194 umtx_key_release(&uq->uq_key); 2195 if (error == 0 && old_ceiling != NULL) 2196 suword32(old_ceiling, save_ceiling); 2197 return (error); 2198} 2199 2200static int 2201_do_lock_umutex(struct thread *td, struct umutex *m, int flags, int timo, 2202 int mode) 2203{ 2204 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) { 2205 case 0: 2206 return (_do_lock_normal(td, m, flags, timo, mode)); 2207 case UMUTEX_PRIO_INHERIT: 2208 return (_do_lock_pi(td, m, flags, timo, mode)); 2209 case UMUTEX_PRIO_PROTECT: 2210 return (_do_lock_pp(td, m, flags, timo, mode)); 2211 } 2212 return (EINVAL); 2213} 2214 2215/* 2216 * Lock a userland POSIX mutex. 2217 */ 2218static int 2219do_lock_umutex(struct thread *td, struct umutex *m, 2220 struct _umtx_time *timeout, int mode) 2221{ 2222 struct timespec cts, ets, tts; 2223 uint32_t flags; 2224 int error; 2225 2226 flags = fuword32(&m->m_flags); 2227 if (flags == -1) 2228 return (EFAULT); 2229 2230 if (timeout == NULL) { 2231 error = _do_lock_umutex(td, m, flags, 0, mode); 2232 /* Mutex locking is restarted if it is interrupted. */ 2233 if (error == EINTR && mode != _UMUTEX_WAIT) 2234 error = ERESTART; 2235 } else { 2236 kern_clock_gettime(td, timeout->_clockid, &cts); 2237 if ((timeout->_flags & UMTX_ABSTIME) == 0) { 2238 ets = cts; 2239 timespecadd(&ets, &timeout->_timeout); 2240 tts = timeout->_timeout; 2241 } else { 2242 ets = timeout->_timeout; 2243 tts = timeout->_timeout; 2244 timespecsub(&tts, &cts); 2245 } 2246 for (;;) { 2247 error = _do_lock_umutex(td, m, flags, tstohz(&tts), mode); 2248 if (error != ETIMEDOUT) 2249 break; 2250 kern_clock_gettime(td, timeout->_clockid, &cts); 2251 if (timespeccmp(&cts, &ets, >=)) 2252 break; 2253 tts = ets; 2254 timespecsub(&tts, &cts); 2255 } 2256 /* Timed-locking is not restarted. */ 2257 if (error == ERESTART) 2258 error = EINTR; 2259 } 2260 return (error); 2261} 2262 2263/* 2264 * Unlock a userland POSIX mutex. 2265 */ 2266static int 2267do_unlock_umutex(struct thread *td, struct umutex *m) 2268{ 2269 uint32_t flags; 2270 2271 flags = fuword32(&m->m_flags); 2272 if (flags == -1) 2273 return (EFAULT); 2274 2275 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) { 2276 case 0: 2277 return (do_unlock_normal(td, m, flags)); 2278 case UMUTEX_PRIO_INHERIT: 2279 return (do_unlock_pi(td, m, flags)); 2280 case UMUTEX_PRIO_PROTECT: 2281 return (do_unlock_pp(td, m, flags)); 2282 } 2283 2284 return (EINVAL); 2285} 2286 2287static int 2288do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m, 2289 struct timespec *timeout, u_long wflags) 2290{ 2291 struct umtx_q *uq; 2292 struct timeval tv; 2293 struct timespec cts, ets, tts; 2294 uint32_t flags; 2295 uint32_t clockid; 2296 int error; 2297 2298 uq = td->td_umtxq; 2299 flags = fuword32(&cv->c_flags); 2300 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key); 2301 if (error != 0) 2302 return (error); 2303 2304 if ((wflags & CVWAIT_CLOCKID) != 0) { 2305 clockid = fuword32(&cv->c_clockid); 2306 if (clockid < CLOCK_REALTIME || 2307 clockid >= CLOCK_THREAD_CPUTIME_ID) { 2308 /* hmm, only HW clock id will work. */ 2309 return (EINVAL); 2310 } 2311 } else { 2312 clockid = CLOCK_REALTIME; 2313 } 2314 2315 umtxq_lock(&uq->uq_key); 2316 umtxq_busy(&uq->uq_key); 2317 umtxq_insert(uq); 2318 umtxq_unlock(&uq->uq_key); 2319 2320 /* 2321 * Set c_has_waiters to 1 before releasing user mutex, also 2322 * don't modify cache line when unnecessary. 2323 */ 2324 if (fuword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters)) == 0) 2325 suword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters), 1); 2326 2327 umtxq_lock(&uq->uq_key); 2328 umtxq_unbusy(&uq->uq_key); 2329 umtxq_unlock(&uq->uq_key); 2330 2331 error = do_unlock_umutex(td, m); 2332 2333 umtxq_lock(&uq->uq_key); 2334 if (error == 0) { 2335 if (timeout == NULL) { 2336 error = umtxq_sleep(uq, "ucond", 0); 2337 } else { 2338 if ((wflags & CVWAIT_ABSTIME) == 0) { 2339 kern_clock_gettime(td, clockid, &ets); 2340 timespecadd(&ets, timeout); 2341 tts = *timeout; 2342 } else { /* absolute time */ 2343 ets = *timeout; 2344 tts = *timeout; 2345 kern_clock_gettime(td, clockid, &cts); 2346 timespecsub(&tts, &cts); 2347 } 2348 TIMESPEC_TO_TIMEVAL(&tv, &tts); 2349 for (;;) { 2350 error = umtxq_sleep(uq, "ucond", tvtohz(&tv)); 2351 if (error != ETIMEDOUT) 2352 break; 2353 kern_clock_gettime(td, clockid, &cts); 2354 if (timespeccmp(&cts, &ets, >=)) { 2355 error = ETIMEDOUT; 2356 break; 2357 } 2358 tts = ets; 2359 timespecsub(&tts, &cts); 2360 TIMESPEC_TO_TIMEVAL(&tv, &tts); 2361 } 2362 } 2363 } 2364 2365 if ((uq->uq_flags & UQF_UMTXQ) == 0) 2366 error = 0; 2367 else { 2368 /* 2369 * This must be timeout,interrupted by signal or 2370 * surprious wakeup, clear c_has_waiter flag when 2371 * necessary. 2372 */ 2373 umtxq_busy(&uq->uq_key); 2374 if ((uq->uq_flags & UQF_UMTXQ) != 0) { 2375 int oldlen = uq->uq_cur_queue->length; 2376 umtxq_remove(uq); 2377 if (oldlen == 1) { 2378 umtxq_unlock(&uq->uq_key); 2379 suword32( 2380 __DEVOLATILE(uint32_t *, 2381 &cv->c_has_waiters), 0); 2382 umtxq_lock(&uq->uq_key); 2383 } 2384 } 2385 umtxq_unbusy(&uq->uq_key); 2386 if (error == ERESTART) 2387 error = EINTR; 2388 } 2389 2390 umtxq_unlock(&uq->uq_key); 2391 umtx_key_release(&uq->uq_key); 2392 return (error); 2393} 2394 2395/* 2396 * Signal a userland condition variable. 2397 */ 2398static int 2399do_cv_signal(struct thread *td, struct ucond *cv) 2400{ 2401 struct umtx_key key; 2402 int error, cnt, nwake; 2403 uint32_t flags; 2404 2405 flags = fuword32(&cv->c_flags); 2406 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0) 2407 return (error); 2408 umtxq_lock(&key); 2409 umtxq_busy(&key); 2410 cnt = umtxq_count(&key); 2411 nwake = umtxq_signal(&key, 1); 2412 if (cnt <= nwake) { 2413 umtxq_unlock(&key); 2414 error = suword32( 2415 __DEVOLATILE(uint32_t *, &cv->c_has_waiters), 0); 2416 umtxq_lock(&key); 2417 } 2418 umtxq_unbusy(&key); 2419 umtxq_unlock(&key); 2420 umtx_key_release(&key); 2421 return (error); 2422} 2423 2424static int 2425do_cv_broadcast(struct thread *td, struct ucond *cv) 2426{ 2427 struct umtx_key key; 2428 int error; 2429 uint32_t flags; 2430 2431 flags = fuword32(&cv->c_flags); 2432 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0) 2433 return (error); 2434 2435 umtxq_lock(&key); 2436 umtxq_busy(&key); 2437 umtxq_signal(&key, INT_MAX); 2438 umtxq_unlock(&key); 2439 2440 error = suword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters), 0); 2441 2442 umtxq_lock(&key); 2443 umtxq_unbusy(&key); 2444 umtxq_unlock(&key); 2445 2446 umtx_key_release(&key); 2447 return (error); 2448} 2449 2450static int 2451do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, int timo) 2452{ 2453 struct umtx_q *uq; 2454 uint32_t flags, wrflags; 2455 int32_t state, oldstate; 2456 int32_t blocked_readers; 2457 int error; 2458 2459 uq = td->td_umtxq; 2460 flags = fuword32(&rwlock->rw_flags); 2461 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key); 2462 if (error != 0) 2463 return (error); 2464 2465 wrflags = URWLOCK_WRITE_OWNER; 2466 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER)) 2467 wrflags |= URWLOCK_WRITE_WAITERS; 2468 2469 for (;;) { 2470 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state)); 2471 /* try to lock it */ 2472 while (!(state & wrflags)) { 2473 if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) { 2474 umtx_key_release(&uq->uq_key); 2475 return (EAGAIN); 2476 } 2477 oldstate = casuword32(&rwlock->rw_state, state, state + 1); 2478 if (oldstate == state) { 2479 umtx_key_release(&uq->uq_key); 2480 return (0); 2481 } 2482 state = oldstate; 2483 } 2484 2485 if (error) 2486 break; 2487 2488 /* grab monitor lock */ 2489 umtxq_lock(&uq->uq_key); 2490 umtxq_busy(&uq->uq_key); 2491 umtxq_unlock(&uq->uq_key); 2492 2493 /* 2494 * re-read the state, in case it changed between the try-lock above 2495 * and the check below 2496 */ 2497 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state)); 2498 2499 /* set read contention bit */ 2500 while ((state & wrflags) && !(state & URWLOCK_READ_WAITERS)) { 2501 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_READ_WAITERS); 2502 if (oldstate == state) 2503 goto sleep; 2504 state = oldstate; 2505 } 2506 2507 /* state is changed while setting flags, restart */ 2508 if (!(state & wrflags)) { 2509 umtxq_lock(&uq->uq_key); 2510 umtxq_unbusy(&uq->uq_key); 2511 umtxq_unlock(&uq->uq_key); 2512 continue; 2513 } 2514 2515sleep: 2516 /* contention bit is set, before sleeping, increase read waiter count */ 2517 blocked_readers = fuword32(&rwlock->rw_blocked_readers); 2518 suword32(&rwlock->rw_blocked_readers, blocked_readers+1); 2519 2520 while (state & wrflags) { 2521 umtxq_lock(&uq->uq_key); 2522 umtxq_insert(uq); 2523 umtxq_unbusy(&uq->uq_key); 2524 2525 error = umtxq_sleep(uq, "urdlck", timo); 2526 2527 umtxq_busy(&uq->uq_key); 2528 umtxq_remove(uq); 2529 umtxq_unlock(&uq->uq_key); 2530 if (error) 2531 break; 2532 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state)); 2533 } 2534 2535 /* decrease read waiter count, and may clear read contention bit */ 2536 blocked_readers = fuword32(&rwlock->rw_blocked_readers); 2537 suword32(&rwlock->rw_blocked_readers, blocked_readers-1); 2538 if (blocked_readers == 1) { 2539 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state)); 2540 for (;;) { 2541 oldstate = casuword32(&rwlock->rw_state, state, 2542 state & ~URWLOCK_READ_WAITERS); 2543 if (oldstate == state) 2544 break; 2545 state = oldstate; 2546 } 2547 } 2548 2549 umtxq_lock(&uq->uq_key); 2550 umtxq_unbusy(&uq->uq_key); 2551 umtxq_unlock(&uq->uq_key); 2552 } 2553 umtx_key_release(&uq->uq_key); 2554 return (error); 2555} 2556 2557static int 2558do_rw_rdlock2(struct thread *td, void *obj, long val, struct timespec *timeout) 2559{ 2560 struct timespec ts, ts2, ts3; 2561 struct timeval tv; 2562 int error; 2563 2564 getnanouptime(&ts); 2565 timespecadd(&ts, timeout); 2566 TIMESPEC_TO_TIMEVAL(&tv, timeout); 2567 for (;;) { 2568 error = do_rw_rdlock(td, obj, val, tvtohz(&tv)); 2569 if (error != ETIMEDOUT) 2570 break; 2571 getnanouptime(&ts2); 2572 if (timespeccmp(&ts2, &ts, >=)) { 2573 error = ETIMEDOUT; 2574 break; 2575 } 2576 ts3 = ts; 2577 timespecsub(&ts3, &ts2); 2578 TIMESPEC_TO_TIMEVAL(&tv, &ts3); 2579 } 2580 if (error == ERESTART) 2581 error = EINTR; 2582 return (error); 2583} 2584 2585static int 2586do_rw_wrlock(struct thread *td, struct urwlock *rwlock, int timo) 2587{ 2588 struct umtx_q *uq; 2589 uint32_t flags; 2590 int32_t state, oldstate; 2591 int32_t blocked_writers; 2592 int32_t blocked_readers; 2593 int error; 2594 2595 uq = td->td_umtxq; 2596 flags = fuword32(&rwlock->rw_flags); 2597 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key); 2598 if (error != 0) 2599 return (error); 2600 2601 blocked_readers = 0; 2602 for (;;) { 2603 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state)); 2604 while (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) { 2605 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_WRITE_OWNER); 2606 if (oldstate == state) { 2607 umtx_key_release(&uq->uq_key); 2608 return (0); 2609 } 2610 state = oldstate; 2611 } 2612 2613 if (error) { 2614 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) && 2615 blocked_readers != 0) { 2616 umtxq_lock(&uq->uq_key); 2617 umtxq_busy(&uq->uq_key); 2618 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE); 2619 umtxq_unbusy(&uq->uq_key); 2620 umtxq_unlock(&uq->uq_key); 2621 } 2622 2623 break; 2624 } 2625 2626 /* grab monitor lock */ 2627 umtxq_lock(&uq->uq_key); 2628 umtxq_busy(&uq->uq_key); 2629 umtxq_unlock(&uq->uq_key); 2630 2631 /* 2632 * re-read the state, in case it changed between the try-lock above 2633 * and the check below 2634 */ 2635 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state)); 2636 2637 while (((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) && 2638 (state & URWLOCK_WRITE_WAITERS) == 0) { 2639 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_WRITE_WAITERS); 2640 if (oldstate == state) 2641 goto sleep; 2642 state = oldstate; 2643 } 2644 2645 if (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) { 2646 umtxq_lock(&uq->uq_key); 2647 umtxq_unbusy(&uq->uq_key); 2648 umtxq_unlock(&uq->uq_key); 2649 continue; 2650 } 2651sleep: 2652 blocked_writers = fuword32(&rwlock->rw_blocked_writers); 2653 suword32(&rwlock->rw_blocked_writers, blocked_writers+1); 2654 2655 while ((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) { 2656 umtxq_lock(&uq->uq_key); 2657 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE); 2658 umtxq_unbusy(&uq->uq_key); 2659 2660 error = umtxq_sleep(uq, "uwrlck", timo); 2661 2662 umtxq_busy(&uq->uq_key); 2663 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE); 2664 umtxq_unlock(&uq->uq_key); 2665 if (error) 2666 break; 2667 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state)); 2668 } 2669 2670 blocked_writers = fuword32(&rwlock->rw_blocked_writers); 2671 suword32(&rwlock->rw_blocked_writers, blocked_writers-1); 2672 if (blocked_writers == 1) { 2673 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state)); 2674 for (;;) { 2675 oldstate = casuword32(&rwlock->rw_state, state, 2676 state & ~URWLOCK_WRITE_WAITERS); 2677 if (oldstate == state) 2678 break; 2679 state = oldstate; 2680 } 2681 blocked_readers = fuword32(&rwlock->rw_blocked_readers); 2682 } else 2683 blocked_readers = 0; 2684 2685 umtxq_lock(&uq->uq_key); 2686 umtxq_unbusy(&uq->uq_key); 2687 umtxq_unlock(&uq->uq_key); 2688 } 2689 2690 umtx_key_release(&uq->uq_key); 2691 return (error); 2692} 2693 2694static int 2695do_rw_wrlock2(struct thread *td, void *obj, struct timespec *timeout) 2696{ 2697 struct timespec ts, ts2, ts3; 2698 struct timeval tv; 2699 int error; 2700 2701 getnanouptime(&ts); 2702 timespecadd(&ts, timeout); 2703 TIMESPEC_TO_TIMEVAL(&tv, timeout); 2704 for (;;) { 2705 error = do_rw_wrlock(td, obj, tvtohz(&tv)); 2706 if (error != ETIMEDOUT) 2707 break; 2708 getnanouptime(&ts2); 2709 if (timespeccmp(&ts2, &ts, >=)) { 2710 error = ETIMEDOUT; 2711 break; 2712 } 2713 ts3 = ts; 2714 timespecsub(&ts3, &ts2); 2715 TIMESPEC_TO_TIMEVAL(&tv, &ts3); 2716 } 2717 if (error == ERESTART) 2718 error = EINTR; 2719 return (error); 2720} 2721 2722static int 2723do_rw_unlock(struct thread *td, struct urwlock *rwlock) 2724{ 2725 struct umtx_q *uq; 2726 uint32_t flags; 2727 int32_t state, oldstate; 2728 int error, q, count; 2729 2730 uq = td->td_umtxq; 2731 flags = fuword32(&rwlock->rw_flags); 2732 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key); 2733 if (error != 0) 2734 return (error); 2735 2736 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state)); 2737 if (state & URWLOCK_WRITE_OWNER) { 2738 for (;;) { 2739 oldstate = casuword32(&rwlock->rw_state, state, 2740 state & ~URWLOCK_WRITE_OWNER); 2741 if (oldstate != state) { 2742 state = oldstate; 2743 if (!(oldstate & URWLOCK_WRITE_OWNER)) { 2744 error = EPERM; 2745 goto out; 2746 } 2747 } else 2748 break; 2749 } 2750 } else if (URWLOCK_READER_COUNT(state) != 0) { 2751 for (;;) { 2752 oldstate = casuword32(&rwlock->rw_state, state, 2753 state - 1); 2754 if (oldstate != state) { 2755 state = oldstate; 2756 if (URWLOCK_READER_COUNT(oldstate) == 0) { 2757 error = EPERM; 2758 goto out; 2759 } 2760 } 2761 else 2762 break; 2763 } 2764 } else { 2765 error = EPERM; 2766 goto out; 2767 } 2768 2769 count = 0; 2770 2771 if (!(flags & URWLOCK_PREFER_READER)) { 2772 if (state & URWLOCK_WRITE_WAITERS) { 2773 count = 1; 2774 q = UMTX_EXCLUSIVE_QUEUE; 2775 } else if (state & URWLOCK_READ_WAITERS) { 2776 count = INT_MAX; 2777 q = UMTX_SHARED_QUEUE; 2778 } 2779 } else { 2780 if (state & URWLOCK_READ_WAITERS) { 2781 count = INT_MAX; 2782 q = UMTX_SHARED_QUEUE; 2783 } else if (state & URWLOCK_WRITE_WAITERS) { 2784 count = 1; 2785 q = UMTX_EXCLUSIVE_QUEUE; 2786 } 2787 } 2788 2789 if (count) { 2790 umtxq_lock(&uq->uq_key); 2791 umtxq_busy(&uq->uq_key); 2792 umtxq_signal_queue(&uq->uq_key, count, q); 2793 umtxq_unbusy(&uq->uq_key); 2794 umtxq_unlock(&uq->uq_key); 2795 } 2796out: 2797 umtx_key_release(&uq->uq_key); 2798 return (error); 2799} 2800 2801static int 2802do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout) 2803{ 2804 struct umtx_q *uq; 2805 struct timespec cts, ets, tts; 2806 uint32_t flags, count; 2807 int error; 2808 2809 uq = td->td_umtxq; 2810 flags = fuword32(&sem->_flags); 2811 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key); 2812 if (error != 0) 2813 return (error); 2814 umtxq_lock(&uq->uq_key); 2815 umtxq_busy(&uq->uq_key); 2816 umtxq_insert(uq); 2817 umtxq_unlock(&uq->uq_key); 2818 2819 casuword32(__DEVOLATILE(uint32_t *, &sem->_has_waiters), 0, 1); 2820 rmb(); 2821 count = fuword32(__DEVOLATILE(uint32_t *, &sem->_count)); 2822 if (count != 0) { 2823 umtxq_lock(&uq->uq_key); 2824 umtxq_unbusy(&uq->uq_key); 2825 umtxq_remove(uq); 2826 umtxq_unlock(&uq->uq_key); 2827 umtx_key_release(&uq->uq_key); 2828 return (0); 2829 } 2830 2831 umtxq_lock(&uq->uq_key); 2832 umtxq_unbusy(&uq->uq_key); 2833 2834 if (timeout == NULL) { 2835 error = umtxq_sleep(uq, "usem", 0); 2836 } else { 2837 umtxq_unlock(&uq->uq_key); 2838 kern_clock_gettime(td, timeout->_clockid, &cts); 2839 if ((timeout->_flags & UMTX_ABSTIME) == 0) { 2840 ets = cts; 2841 timespecadd(&ets, &timeout->_timeout); 2842 } else { 2843 ets = timeout->_timeout; 2844 } 2845 umtxq_lock(&uq->uq_key); 2846 for (;;) { 2847 if (timespeccmp(&cts, &ets, >=)) { 2848 error = ETIMEDOUT; 2849 break; 2850 } 2851 tts = ets; 2852 timespecsub(&tts, &cts); 2853 error = umtxq_sleep(uq, "usem", tstohz(&tts)); 2854 if (error != ETIMEDOUT) 2855 break; 2856 umtxq_unlock(&uq->uq_key); 2857 kern_clock_gettime(td, timeout->_clockid, &cts); 2858 umtxq_lock(&uq->uq_key); 2859 } 2860 } 2861 2862 if ((uq->uq_flags & UQF_UMTXQ) == 0) 2863 error = 0; 2864 else { 2865 umtxq_remove(uq); 2866 if (error == ERESTART) 2867 error = EINTR; 2868 } 2869 umtxq_unlock(&uq->uq_key); 2870 umtx_key_release(&uq->uq_key); 2871 return (error); 2872} 2873 2874/* 2875 * Signal a userland condition variable. 2876 */ 2877static int 2878do_sem_wake(struct thread *td, struct _usem *sem) 2879{ 2880 struct umtx_key key; 2881 int error, cnt, nwake; 2882 uint32_t flags; 2883 2884 flags = fuword32(&sem->_flags); 2885 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0) 2886 return (error); 2887 umtxq_lock(&key); 2888 umtxq_busy(&key); 2889 cnt = umtxq_count(&key); 2890 nwake = umtxq_signal(&key, 1); 2891 if (cnt <= nwake) { 2892 umtxq_unlock(&key); 2893 error = suword32( 2894 __DEVOLATILE(uint32_t *, &sem->_has_waiters), 0); 2895 umtxq_lock(&key); 2896 } 2897 umtxq_unbusy(&key); 2898 umtxq_unlock(&key); 2899 umtx_key_release(&key); 2900 return (error); 2901} 2902 2903int 2904sys__umtx_lock(struct thread *td, struct _umtx_lock_args *uap) 2905 /* struct umtx *umtx */ 2906{ 2907 return _do_lock_umtx(td, uap->umtx, td->td_tid, 0); 2908} 2909 2910int 2911sys__umtx_unlock(struct thread *td, struct _umtx_unlock_args *uap) 2912 /* struct umtx *umtx */ 2913{ 2914 return do_unlock_umtx(td, uap->umtx, td->td_tid); 2915} 2916 2917inline int 2918umtx_copyin_timeout(const void *addr, struct timespec *tsp) 2919{ 2920 int error; 2921 2922 error = copyin(addr, tsp, sizeof(struct timespec)); 2923 if (error == 0) { 2924 if (tsp->tv_sec < 0 || 2925 tsp->tv_nsec >= 1000000000 || 2926 tsp->tv_nsec < 0) 2927 error = EINVAL; 2928 } 2929 return (error); 2930} 2931 2932static inline int 2933umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp) 2934{ 2935 int error; 2936 2937 tp->_clockid = CLOCK_REALTIME; 2938 tp->_flags = 0; 2939 if (size <= sizeof(struct timespec)) 2940 error = copyin(addr, &tp->_timeout, sizeof(struct timespec)); 2941 else 2942 error = copyin(addr, tp, sizeof(struct _umtx_time)); 2943 if (error != 0) 2944 return (error); 2945 if (tp->_timeout.tv_sec < 0 || 2946 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0) 2947 return (EINVAL); 2948 return (0); 2949} 2950 2951static int 2952__umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap) 2953{ 2954 struct timespec *ts, timeout; 2955 int error; 2956 2957 /* Allow a null timespec (wait forever). */ 2958 if (uap->uaddr2 == NULL) 2959 ts = NULL; 2960 else { 2961 error = umtx_copyin_timeout(uap->uaddr2, &timeout); 2962 if (error != 0) 2963 return (error); 2964 ts = &timeout; 2965 } 2966 return (do_lock_umtx(td, uap->obj, uap->val, ts)); 2967} 2968 2969static int 2970__umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap) 2971{ 2972 return (do_unlock_umtx(td, uap->obj, uap->val)); 2973} 2974 2975static int 2976__umtx_op_wait(struct thread *td, struct _umtx_op_args *uap) 2977{ 2978 struct _umtx_time timeout, *tm_p; 2979 int error; 2980 2981 if (uap->uaddr2 == NULL) 2982 tm_p = NULL; 2983 else { 2984 error = umtx_copyin_umtx_time( 2985 uap->uaddr2, (size_t)uap->uaddr1, &timeout); 2986 if (error != 0) 2987 return (error); 2988 tm_p = &timeout; 2989 } 2990 return do_wait(td, uap->obj, uap->val, tm_p, 0, 0); 2991} 2992 2993static int 2994__umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap) 2995{ 2996 struct _umtx_time timeout, *tm_p; 2997 int error; 2998 2999 if (uap->uaddr2 == NULL) 3000 tm_p = NULL; 3001 else { 3002 error = umtx_copyin_umtx_time( 3003 uap->uaddr2, (size_t)uap->uaddr1, &timeout); 3004 if (error != 0) 3005 return (error); 3006 tm_p = &timeout; 3007 } 3008 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0); 3009} 3010 3011static int 3012__umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap) 3013{ 3014 struct _umtx_time *tm_p, timeout; 3015 int error; 3016 3017 if (uap->uaddr2 == NULL) 3018 tm_p = NULL; 3019 else { 3020 error = umtx_copyin_umtx_time( 3021 uap->uaddr2, (size_t)uap->uaddr1, &timeout); 3022 if (error != 0) 3023 return (error); 3024 tm_p = &timeout; 3025 } 3026 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1); 3027} 3028 3029static int 3030__umtx_op_wake(struct thread *td, struct _umtx_op_args *uap) 3031{ 3032 return (kern_umtx_wake(td, uap->obj, uap->val, 0)); 3033} 3034 3035#define BATCH_SIZE 128 3036static int 3037__umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap) 3038{ 3039 int count = uap->val; 3040 void *uaddrs[BATCH_SIZE]; 3041 char **upp = (char **)uap->obj; 3042 int tocopy; 3043 int error = 0; 3044 int i, pos = 0; 3045 3046 while (count > 0) { 3047 tocopy = count; 3048 if (tocopy > BATCH_SIZE) 3049 tocopy = BATCH_SIZE; 3050 error = copyin(upp+pos, uaddrs, tocopy * sizeof(char *)); 3051 if (error != 0) 3052 break; 3053 for (i = 0; i < tocopy; ++i) 3054 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1); 3055 count -= tocopy; 3056 pos += tocopy; 3057 } 3058 return (error); 3059} 3060 3061static int 3062__umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap) 3063{ 3064 return (kern_umtx_wake(td, uap->obj, uap->val, 1)); 3065} 3066 3067static int 3068__umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap) 3069{ 3070 struct _umtx_time *tm_p, timeout; 3071 int error; 3072 3073 /* Allow a null timespec (wait forever). */ 3074 if (uap->uaddr2 == NULL) 3075 tm_p = NULL; 3076 else { 3077 error = umtx_copyin_umtx_time( 3078 uap->uaddr2, (size_t)uap->uaddr1, &timeout); 3079 if (error != 0) 3080 return (error); 3081 tm_p = &timeout; 3082 } 3083 return do_lock_umutex(td, uap->obj, tm_p, 0); 3084} 3085 3086static int 3087__umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap) 3088{ 3089 return do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY); 3090} 3091 3092static int 3093__umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap) 3094{ 3095 struct _umtx_time *tm_p, timeout; 3096 int error; 3097 3098 /* Allow a null timespec (wait forever). */ 3099 if (uap->uaddr2 == NULL) 3100 tm_p = NULL; 3101 else { 3102 error = umtx_copyin_umtx_time( 3103 uap->uaddr2, (size_t)uap->uaddr1, &timeout); 3104 if (error != 0) 3105 return (error); 3106 tm_p = &timeout; 3107 } 3108 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT); 3109} 3110 3111static int 3112__umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap) 3113{ 3114 return do_wake_umutex(td, uap->obj); 3115} 3116 3117static int 3118__umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap) 3119{ 3120 return do_unlock_umutex(td, uap->obj); 3121} 3122 3123static int 3124__umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap) 3125{ 3126 return do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1); 3127} 3128 3129static int 3130__umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap) 3131{ 3132 struct timespec *ts, timeout; 3133 int error; 3134 3135 /* Allow a null timespec (wait forever). */ 3136 if (uap->uaddr2 == NULL) 3137 ts = NULL; 3138 else { 3139 error = umtx_copyin_timeout(uap->uaddr2, &timeout); 3140 if (error != 0) 3141 return (error); 3142 ts = &timeout; 3143 } 3144 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val)); 3145} 3146 3147static int 3148__umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap) 3149{ 3150 return do_cv_signal(td, uap->obj); 3151} 3152 3153static int 3154__umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap) 3155{ 3156 return do_cv_broadcast(td, uap->obj); 3157} 3158 3159static int 3160__umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap) 3161{ 3162 struct timespec timeout; 3163 int error; 3164 3165 /* Allow a null timespec (wait forever). */ 3166 if (uap->uaddr2 == NULL) { 3167 error = do_rw_rdlock(td, uap->obj, uap->val, 0); 3168 } else { 3169 error = umtx_copyin_timeout(uap->uaddr2, &timeout); 3170 if (error != 0) 3171 return (error); 3172 error = do_rw_rdlock2(td, uap->obj, uap->val, &timeout); 3173 } 3174 return (error); 3175} 3176 3177static int 3178__umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap) 3179{ 3180 struct timespec timeout; 3181 int error; 3182 3183 /* Allow a null timespec (wait forever). */ 3184 if (uap->uaddr2 == NULL) { 3185 error = do_rw_wrlock(td, uap->obj, 0); 3186 } else { 3187 error = umtx_copyin_timeout(uap->uaddr2, &timeout); 3188 if (error != 0) 3189 return (error); 3190 3191 error = do_rw_wrlock2(td, uap->obj, &timeout); 3192 } 3193 return (error); 3194} 3195 3196static int 3197__umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap) 3198{ 3199 return do_rw_unlock(td, uap->obj); 3200} 3201 3202static int 3203__umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap) 3204{ 3205 struct _umtx_time *tm_p, timeout; 3206 int error; 3207 3208 /* Allow a null timespec (wait forever). */ 3209 if (uap->uaddr2 == NULL) 3210 tm_p = NULL; 3211 else { 3212 error = umtx_copyin_umtx_time( 3213 uap->uaddr2, (size_t)uap->uaddr1, &timeout); 3214 if (error != 0) 3215 return (error); 3216 tm_p = &timeout; 3217 } 3218 return (do_sem_wait(td, uap->obj, tm_p)); 3219} 3220 3221static int 3222__umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap) 3223{ 3224 return do_sem_wake(td, uap->obj); 3225} 3226 3227typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap); 3228 3229static _umtx_op_func op_table[] = { 3230 __umtx_op_lock_umtx, /* UMTX_OP_LOCK */ 3231 __umtx_op_unlock_umtx, /* UMTX_OP_UNLOCK */ 3232 __umtx_op_wait, /* UMTX_OP_WAIT */ 3233 __umtx_op_wake, /* UMTX_OP_WAKE */ 3234 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_TRYLOCK */ 3235 __umtx_op_lock_umutex, /* UMTX_OP_MUTEX_LOCK */ 3236 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */ 3237 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */ 3238 __umtx_op_cv_wait, /* UMTX_OP_CV_WAIT*/ 3239 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */ 3240 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */ 3241 __umtx_op_wait_uint, /* UMTX_OP_WAIT_UINT */ 3242 __umtx_op_rw_rdlock, /* UMTX_OP_RW_RDLOCK */ 3243 __umtx_op_rw_wrlock, /* UMTX_OP_RW_WRLOCK */ 3244 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */ 3245 __umtx_op_wait_uint_private, /* UMTX_OP_WAIT_UINT_PRIVATE */ 3246 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */ 3247 __umtx_op_wait_umutex, /* UMTX_OP_UMUTEX_WAIT */ 3248 __umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */ 3249 __umtx_op_sem_wait, /* UMTX_OP_SEM_WAIT */ 3250 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */ 3251 __umtx_op_nwake_private /* UMTX_OP_NWAKE_PRIVATE */ 3252}; 3253 3254int 3255sys__umtx_op(struct thread *td, struct _umtx_op_args *uap) 3256{ 3257 if ((unsigned)uap->op < UMTX_OP_MAX) 3258 return (*op_table[uap->op])(td, uap); 3259 return (EINVAL); 3260} 3261 3262#ifdef COMPAT_FREEBSD32 3263int 3264freebsd32_umtx_lock(struct thread *td, struct freebsd32_umtx_lock_args *uap) 3265 /* struct umtx *umtx */ 3266{ 3267 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL)); 3268} 3269 3270int 3271freebsd32_umtx_unlock(struct thread *td, struct freebsd32_umtx_unlock_args *uap) 3272 /* struct umtx *umtx */ 3273{ 3274 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid)); 3275} 3276 3277struct timespec32 { 3278 uint32_t tv_sec; 3279 uint32_t tv_nsec; 3280}; 3281 3282struct umtx_time32 { 3283 struct timespec32 timeout; 3284 uint32_t flags; 3285 uint32_t clockid; 3286}; 3287 3288static inline int 3289umtx_copyin_timeout32(void *addr, struct timespec *tsp) 3290{ 3291 struct timespec32 ts32; 3292 int error; 3293 3294 error = copyin(addr, &ts32, sizeof(struct timespec32)); 3295 if (error == 0) { 3296 if (ts32.tv_sec < 0 || 3297 ts32.tv_nsec >= 1000000000 || 3298 ts32.tv_nsec < 0) 3299 error = EINVAL; 3300 else { 3301 tsp->tv_sec = ts32.tv_sec; 3302 tsp->tv_nsec = ts32.tv_nsec; 3303 } 3304 } 3305 return (error); 3306} 3307 3308static inline int 3309umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp) 3310{ 3311 struct umtx_time32 t32; 3312 int error; 3313 3314 t32.clockid = CLOCK_REALTIME; 3315 t32.flags = 0; 3316 if (size <= sizeof(struct timespec32)) 3317 error = copyin(addr, &t32.timeout, sizeof(struct timespec32)); 3318 else 3319 error = copyin(addr, &t32, sizeof(struct umtx_time32)); 3320 if (error != 0) 3321 return (error); 3322 if (t32.timeout.tv_sec < 0 || 3323 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0) 3324 return (EINVAL); 3325 tp->_timeout.tv_sec = t32.timeout.tv_sec; 3326 tp->_timeout.tv_nsec = t32.timeout.tv_nsec; 3327 tp->_flags = t32.flags; 3328 tp->_clockid = t32.clockid; 3329 return (0); 3330} 3331 3332static int 3333__umtx_op_lock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap) 3334{ 3335 struct timespec *ts, timeout; 3336 int error; 3337 3338 /* Allow a null timespec (wait forever). */ 3339 if (uap->uaddr2 == NULL) 3340 ts = NULL; 3341 else { 3342 error = umtx_copyin_timeout32(uap->uaddr2, &timeout); 3343 if (error != 0) 3344 return (error); 3345 ts = &timeout; 3346 } 3347 return (do_lock_umtx32(td, uap->obj, uap->val, ts)); 3348} 3349 3350static int 3351__umtx_op_unlock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap) 3352{ 3353 return (do_unlock_umtx32(td, uap->obj, (uint32_t)uap->val)); 3354} 3355 3356static int 3357__umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap) 3358{ 3359 struct _umtx_time *tm_p, timeout; 3360 int error; 3361 3362 if (uap->uaddr2 == NULL) 3363 tm_p = NULL; 3364 else { 3365 error = umtx_copyin_umtx_time32(uap->uaddr2, 3366 (size_t)uap->uaddr1, &timeout); 3367 if (error != 0) 3368 return (error); 3369 tm_p = &timeout; 3370 } 3371 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0); 3372} 3373 3374static int 3375__umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap) 3376{ 3377 struct _umtx_time *tm_p, timeout; 3378 int error; 3379 3380 /* Allow a null timespec (wait forever). */ 3381 if (uap->uaddr2 == NULL) 3382 tm_p = NULL; 3383 else { 3384 error = umtx_copyin_umtx_time(uap->uaddr2, 3385 (size_t)uap->uaddr1, &timeout); 3386 if (error != 0) 3387 return (error); 3388 tm_p = &timeout; 3389 } 3390 return do_lock_umutex(td, uap->obj, tm_p, 0); 3391} 3392 3393static int 3394__umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap) 3395{ 3396 struct _umtx_time *tm_p, timeout; 3397 int error; 3398 3399 /* Allow a null timespec (wait forever). */ 3400 if (uap->uaddr2 == NULL) 3401 tm_p = NULL; 3402 else { 3403 error = umtx_copyin_umtx_time32(uap->uaddr2, 3404 (size_t)uap->uaddr1, &timeout); 3405 if (error != 0) 3406 return (error); 3407 tm_p = &timeout; 3408 } 3409 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT); 3410} 3411 3412static int 3413__umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap) 3414{ 3415 struct timespec *ts, timeout; 3416 int error; 3417 3418 /* Allow a null timespec (wait forever). */ 3419 if (uap->uaddr2 == NULL) 3420 ts = NULL; 3421 else { 3422 error = umtx_copyin_timeout32(uap->uaddr2, &timeout); 3423 if (error != 0) 3424 return (error); 3425 ts = &timeout; 3426 } 3427 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val)); 3428} 3429 3430static int 3431__umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap) 3432{ 3433 struct timespec timeout; 3434 int error; 3435 3436 /* Allow a null timespec (wait forever). */ 3437 if (uap->uaddr2 == NULL) { 3438 error = do_rw_rdlock(td, uap->obj, uap->val, 0); 3439 } else { 3440 error = umtx_copyin_timeout32(uap->uaddr2, &timeout); 3441 if (error != 0) 3442 return (error); 3443 error = do_rw_rdlock2(td, uap->obj, uap->val, &timeout); 3444 } 3445 return (error); 3446} 3447 3448static int 3449__umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap) 3450{ 3451 struct timespec timeout; 3452 int error; 3453 3454 /* Allow a null timespec (wait forever). */ 3455 if (uap->uaddr2 == NULL) { 3456 error = do_rw_wrlock(td, uap->obj, 0); 3457 } else { 3458 error = umtx_copyin_timeout32(uap->uaddr2, &timeout); 3459 if (error != 0) 3460 return (error); 3461 3462 error = do_rw_wrlock2(td, uap->obj, &timeout); 3463 } 3464 return (error); 3465} 3466 3467static int 3468__umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap) 3469{ 3470 struct _umtx_time *tm_p, timeout; 3471 int error; 3472 3473 if (uap->uaddr2 == NULL) 3474 tm_p = NULL; 3475 else { 3476 error = umtx_copyin_umtx_time32( 3477 uap->uaddr2, (size_t)uap->uaddr1,&timeout); 3478 if (error != 0) 3479 return (error); 3480 tm_p = &timeout; 3481 } 3482 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1); 3483} 3484 3485static int 3486__umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap) 3487{ 3488 struct _umtx_time *tm_p, timeout; 3489 int error; 3490 3491 /* Allow a null timespec (wait forever). */ 3492 if (uap->uaddr2 == NULL) 3493 tm_p = NULL; 3494 else { 3495 error = umtx_copyin_umtx_time32(uap->uaddr2, 3496 (size_t)uap->uaddr1, &timeout); 3497 if (error != 0) 3498 return (error); 3499 tm_p = &timeout; 3500 } 3501 return (do_sem_wait(td, uap->obj, tm_p)); 3502} 3503 3504static int 3505__umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap) 3506{ 3507 int count = uap->val; 3508 uint32_t uaddrs[BATCH_SIZE]; 3509 uint32_t **upp = (uint32_t **)uap->obj; 3510 int tocopy; 3511 int error = 0; 3512 int i, pos = 0; 3513 3514 while (count > 0) { 3515 tocopy = count; 3516 if (tocopy > BATCH_SIZE) 3517 tocopy = BATCH_SIZE; 3518 error = copyin(upp+pos, uaddrs, tocopy * sizeof(uint32_t)); 3519 if (error != 0) 3520 break; 3521 for (i = 0; i < tocopy; ++i) 3522 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i], 3523 INT_MAX, 1); 3524 count -= tocopy; 3525 pos += tocopy; 3526 } 3527 return (error); 3528} 3529 3530static _umtx_op_func op_table_compat32[] = { 3531 __umtx_op_lock_umtx_compat32, /* UMTX_OP_LOCK */ 3532 __umtx_op_unlock_umtx_compat32, /* UMTX_OP_UNLOCK */ 3533 __umtx_op_wait_compat32, /* UMTX_OP_WAIT */ 3534 __umtx_op_wake, /* UMTX_OP_WAKE */ 3535 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_LOCK */ 3536 __umtx_op_lock_umutex_compat32, /* UMTX_OP_MUTEX_TRYLOCK */ 3537 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */ 3538 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */ 3539 __umtx_op_cv_wait_compat32, /* UMTX_OP_CV_WAIT*/ 3540 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */ 3541 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */ 3542 __umtx_op_wait_compat32, /* UMTX_OP_WAIT_UINT */ 3543 __umtx_op_rw_rdlock_compat32, /* UMTX_OP_RW_RDLOCK */ 3544 __umtx_op_rw_wrlock_compat32, /* UMTX_OP_RW_WRLOCK */ 3545 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */ 3546 __umtx_op_wait_uint_private_compat32, /* UMTX_OP_WAIT_UINT_PRIVATE */ 3547 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */ 3548 __umtx_op_wait_umutex_compat32, /* UMTX_OP_UMUTEX_WAIT */ 3549 __umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */ 3550 __umtx_op_sem_wait_compat32, /* UMTX_OP_SEM_WAIT */ 3551 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */ 3552 __umtx_op_nwake_private32 /* UMTX_OP_NWAKE_PRIVATE */ 3553}; 3554 3555int 3556freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap) 3557{ 3558 if ((unsigned)uap->op < UMTX_OP_MAX) 3559 return (*op_table_compat32[uap->op])(td, 3560 (struct _umtx_op_args *)uap); 3561 return (EINVAL); 3562} 3563#endif 3564 3565void 3566umtx_thread_init(struct thread *td) 3567{ 3568 td->td_umtxq = umtxq_alloc(); 3569 td->td_umtxq->uq_thread = td; 3570} 3571 3572void 3573umtx_thread_fini(struct thread *td) 3574{ 3575 umtxq_free(td->td_umtxq); 3576} 3577 3578/* 3579 * It will be called when new thread is created, e.g fork(). 3580 */ 3581void 3582umtx_thread_alloc(struct thread *td) 3583{ 3584 struct umtx_q *uq; 3585 3586 uq = td->td_umtxq; 3587 uq->uq_inherited_pri = PRI_MAX; 3588 3589 KASSERT(uq->uq_flags == 0, ("uq_flags != 0")); 3590 KASSERT(uq->uq_thread == td, ("uq_thread != td")); 3591 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL")); 3592 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty")); 3593} 3594 3595/* 3596 * exec() hook. 3597 */ 3598static void 3599umtx_exec_hook(void *arg __unused, struct proc *p __unused, 3600 struct image_params *imgp __unused) 3601{ 3602 umtx_thread_cleanup(curthread); 3603} 3604 3605/* 3606 * thread_exit() hook. 3607 */ 3608void 3609umtx_thread_exit(struct thread *td) 3610{ 3611 umtx_thread_cleanup(td); 3612} 3613 3614/* 3615 * clean up umtx data. 3616 */ 3617static void 3618umtx_thread_cleanup(struct thread *td) 3619{ 3620 struct umtx_q *uq; 3621 struct umtx_pi *pi; 3622 3623 if ((uq = td->td_umtxq) == NULL) 3624 return; 3625 3626 mtx_lock_spin(&umtx_lock); 3627 uq->uq_inherited_pri = PRI_MAX; 3628 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) { 3629 pi->pi_owner = NULL; 3630 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link); 3631 } 3632 mtx_unlock_spin(&umtx_lock); 3633 thread_lock(td); 3634 sched_lend_user_prio(td, PRI_MAX); 3635 thread_unlock(td); 3636} 3637