2 * Copyright (c) 2004, David Xu <davidxu@freebsd.org>
3 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
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.
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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_compat.h"
32 #include "opt_umtx_profiling.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
43 #include <sys/sched.h>
45 #include <sys/sysctl.h>
46 #include <sys/sysent.h>
47 #include <sys/systm.h>
48 #include <sys/sysproto.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/eventhandler.h>
54 #include <vm/vm_param.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_object.h>
59 #include <machine/cpu.h>
61 #ifdef COMPAT_FREEBSD32
62 #include <compat/freebsd32/freebsd32_proto.h>
66 #define _UMUTEX_WAIT 2
69 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
70 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
73 /* Priority inheritance mutex info. */
76 struct thread *pi_owner;
81 /* List entry to link umtx holding by thread */
82 TAILQ_ENTRY(umtx_pi) pi_link;
84 /* List entry in hash */
85 TAILQ_ENTRY(umtx_pi) pi_hashlink;
87 /* List for waiters */
88 TAILQ_HEAD(,umtx_q) pi_blocked;
90 /* Identify a userland lock object */
91 struct umtx_key pi_key;
94 /* A userland synchronous object user. */
96 /* Linked list for the hash. */
97 TAILQ_ENTRY(umtx_q) uq_link;
100 struct umtx_key uq_key;
104 #define UQF_UMTXQ 0x0001
106 /* The thread waits on. */
107 struct thread *uq_thread;
110 * Blocked on PI mutex. read can use chain lock
111 * or umtx_lock, write must have both chain lock and
112 * umtx_lock being hold.
114 struct umtx_pi *uq_pi_blocked;
116 /* On blocked list */
117 TAILQ_ENTRY(umtx_q) uq_lockq;
119 /* Thread contending with us */
120 TAILQ_HEAD(,umtx_pi) uq_pi_contested;
122 /* Inherited priority from PP mutex */
123 u_char uq_inherited_pri;
125 /* Spare queue ready to be reused */
126 struct umtxq_queue *uq_spare_queue;
128 /* The queue we on */
129 struct umtxq_queue *uq_cur_queue;
132 TAILQ_HEAD(umtxq_head, umtx_q);
134 /* Per-key wait-queue */
136 struct umtxq_head head;
138 LIST_ENTRY(umtxq_queue) link;
142 LIST_HEAD(umtxq_list, umtxq_queue);
144 /* Userland lock object's wait-queue chain */
146 /* Lock for this chain. */
149 /* List of sleep queues. */
150 struct umtxq_list uc_queue[2];
151 #define UMTX_SHARED_QUEUE 0
152 #define UMTX_EXCLUSIVE_QUEUE 1
154 LIST_HEAD(, umtxq_queue) uc_spare_queue;
159 /* Chain lock waiters */
162 /* All PI in the list */
163 TAILQ_HEAD(,umtx_pi) uc_pi_list;
165 #ifdef UMTX_PROFILING
171 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
174 * Don't propagate time-sharing priority, there is a security reason,
175 * a user can simply introduce PI-mutex, let thread A lock the mutex,
176 * and let another thread B block on the mutex, because B is
177 * sleeping, its priority will be boosted, this causes A's priority to
178 * be boosted via priority propagating too and will never be lowered even
179 * if it is using 100%CPU, this is unfair to other processes.
182 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
183 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
184 PRI_MAX_TIMESHARE : (td)->td_user_pri)
186 #define GOLDEN_RATIO_PRIME 2654404609U
187 #define UMTX_CHAINS 512
188 #define UMTX_SHIFTS (__WORD_BIT - 9)
190 #define GET_SHARE(flags) \
191 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
193 #define BUSY_SPINS 200
201 static uma_zone_t umtx_pi_zone;
202 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
203 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
204 static int umtx_pi_allocated;
206 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW, 0, "umtx debug");
207 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
208 &umtx_pi_allocated, 0, "Allocated umtx_pi");
210 #ifdef UMTX_PROFILING
211 static long max_length;
212 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
213 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD, 0, "umtx chain stats");
216 static void umtxq_sysinit(void *);
217 static void umtxq_hash(struct umtx_key *key);
218 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
219 static void umtxq_lock(struct umtx_key *key);
220 static void umtxq_unlock(struct umtx_key *key);
221 static void umtxq_busy(struct umtx_key *key);
222 static void umtxq_unbusy(struct umtx_key *key);
223 static void umtxq_insert_queue(struct umtx_q *uq, int q);
224 static void umtxq_remove_queue(struct umtx_q *uq, int q);
225 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *);
226 static int umtxq_count(struct umtx_key *key);
227 static struct umtx_pi *umtx_pi_alloc(int);
228 static void umtx_pi_free(struct umtx_pi *pi);
229 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags);
230 static void umtx_thread_cleanup(struct thread *td);
231 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
232 struct image_params *imgp __unused);
233 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
235 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
236 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
237 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
239 static struct mtx umtx_lock;
241 #ifdef UMTX_PROFILING
243 umtx_init_profiling(void)
245 struct sysctl_oid *chain_oid;
249 for (i = 0; i < UMTX_CHAINS; ++i) {
250 snprintf(chain_name, sizeof(chain_name), "%d", i);
251 chain_oid = SYSCTL_ADD_NODE(NULL,
252 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
253 chain_name, CTLFLAG_RD, NULL, "umtx hash stats");
254 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
255 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
256 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
257 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
262 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
266 struct umtxq_chain *uc;
267 u_int fract, i, j, tot, whole;
268 u_int sf0, sf1, sf2, sf3, sf4;
269 u_int si0, si1, si2, si3, si4;
270 u_int sw0, sw1, sw2, sw3, sw4;
272 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
273 for (i = 0; i < 2; i++) {
275 for (j = 0; j < UMTX_CHAINS; ++j) {
276 uc = &umtxq_chains[i][j];
277 mtx_lock(&uc->uc_lock);
278 tot += uc->max_length;
279 mtx_unlock(&uc->uc_lock);
282 sbuf_printf(&sb, "%u) Empty ", i);
284 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
285 si0 = si1 = si2 = si3 = si4 = 0;
286 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
287 for (j = 0; j < UMTX_CHAINS; j++) {
288 uc = &umtxq_chains[i][j];
289 mtx_lock(&uc->uc_lock);
290 whole = uc->max_length * 100;
291 mtx_unlock(&uc->uc_lock);
292 fract = (whole % tot) * 100;
293 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
297 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
302 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
307 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
312 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
319 sbuf_printf(&sb, "queue %u:\n", i);
320 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
322 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
324 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
326 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
328 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
334 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
340 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
342 struct umtxq_chain *uc;
347 error = sysctl_handle_int(oidp, &clear, 0, req);
348 if (error != 0 || req->newptr == NULL)
352 for (i = 0; i < 2; ++i) {
353 for (j = 0; j < UMTX_CHAINS; ++j) {
354 uc = &umtxq_chains[i][j];
355 mtx_lock(&uc->uc_lock);
358 mtx_unlock(&uc->uc_lock);
365 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
366 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
367 sysctl_debug_umtx_chains_clear, "I", "Clear umtx chains statistics");
368 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
369 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
370 sysctl_debug_umtx_chains_peaks, "A", "Highest peaks in chains max length");
374 umtxq_sysinit(void *arg __unused)
378 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
379 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
380 for (i = 0; i < 2; ++i) {
381 for (j = 0; j < UMTX_CHAINS; ++j) {
382 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
383 MTX_DEF | MTX_DUPOK);
384 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
385 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
386 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
387 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
388 umtxq_chains[i][j].uc_busy = 0;
389 umtxq_chains[i][j].uc_waiters = 0;
390 #ifdef UMTX_PROFILING
391 umtxq_chains[i][j].length = 0;
392 umtxq_chains[i][j].max_length = 0;
396 #ifdef UMTX_PROFILING
397 umtx_init_profiling();
399 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
400 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
401 EVENTHANDLER_PRI_ANY);
409 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
410 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX, M_WAITOK | M_ZERO);
411 TAILQ_INIT(&uq->uq_spare_queue->head);
412 TAILQ_INIT(&uq->uq_pi_contested);
413 uq->uq_inherited_pri = PRI_MAX;
418 umtxq_free(struct umtx_q *uq)
420 MPASS(uq->uq_spare_queue != NULL);
421 free(uq->uq_spare_queue, M_UMTX);
426 umtxq_hash(struct umtx_key *key)
428 unsigned n = (uintptr_t)key->info.both.a + key->info.both.b;
429 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
432 static inline struct umtxq_chain *
433 umtxq_getchain(struct umtx_key *key)
435 if (key->type <= TYPE_SEM)
436 return (&umtxq_chains[1][key->hash]);
437 return (&umtxq_chains[0][key->hash]);
444 umtxq_lock(struct umtx_key *key)
446 struct umtxq_chain *uc;
448 uc = umtxq_getchain(key);
449 mtx_lock(&uc->uc_lock);
456 umtxq_unlock(struct umtx_key *key)
458 struct umtxq_chain *uc;
460 uc = umtxq_getchain(key);
461 mtx_unlock(&uc->uc_lock);
465 * Set chain to busy state when following operation
466 * may be blocked (kernel mutex can not be used).
469 umtxq_busy(struct umtx_key *key)
471 struct umtxq_chain *uc;
473 uc = umtxq_getchain(key);
474 mtx_assert(&uc->uc_lock, MA_OWNED);
478 int count = BUSY_SPINS;
481 while (uc->uc_busy && --count > 0)
487 while (uc->uc_busy) {
489 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
500 umtxq_unbusy(struct umtx_key *key)
502 struct umtxq_chain *uc;
504 uc = umtxq_getchain(key);
505 mtx_assert(&uc->uc_lock, MA_OWNED);
506 KASSERT(uc->uc_busy != 0, ("not busy"));
513 umtxq_unbusy_unlocked(struct umtx_key *key)
521 static struct umtxq_queue *
522 umtxq_queue_lookup(struct umtx_key *key, int q)
524 struct umtxq_queue *uh;
525 struct umtxq_chain *uc;
527 uc = umtxq_getchain(key);
528 UMTXQ_LOCKED_ASSERT(uc);
529 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
530 if (umtx_key_match(&uh->key, key))
538 umtxq_insert_queue(struct umtx_q *uq, int q)
540 struct umtxq_queue *uh;
541 struct umtxq_chain *uc;
543 uc = umtxq_getchain(&uq->uq_key);
544 UMTXQ_LOCKED_ASSERT(uc);
545 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
546 uh = umtxq_queue_lookup(&uq->uq_key, q);
548 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
550 uh = uq->uq_spare_queue;
551 uh->key = uq->uq_key;
552 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
553 #ifdef UMTX_PROFILING
555 if (uc->length > uc->max_length) {
556 uc->max_length = uc->length;
557 if (uc->max_length > max_length)
558 max_length = uc->max_length;
562 uq->uq_spare_queue = NULL;
564 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
566 uq->uq_flags |= UQF_UMTXQ;
567 uq->uq_cur_queue = uh;
572 umtxq_remove_queue(struct umtx_q *uq, int q)
574 struct umtxq_chain *uc;
575 struct umtxq_queue *uh;
577 uc = umtxq_getchain(&uq->uq_key);
578 UMTXQ_LOCKED_ASSERT(uc);
579 if (uq->uq_flags & UQF_UMTXQ) {
580 uh = uq->uq_cur_queue;
581 TAILQ_REMOVE(&uh->head, uq, uq_link);
583 uq->uq_flags &= ~UQF_UMTXQ;
584 if (TAILQ_EMPTY(&uh->head)) {
585 KASSERT(uh->length == 0,
586 ("inconsistent umtxq_queue length"));
587 #ifdef UMTX_PROFILING
590 LIST_REMOVE(uh, link);
592 uh = LIST_FIRST(&uc->uc_spare_queue);
593 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
594 LIST_REMOVE(uh, link);
596 uq->uq_spare_queue = uh;
597 uq->uq_cur_queue = NULL;
602 * Check if there are multiple waiters
605 umtxq_count(struct umtx_key *key)
607 struct umtxq_chain *uc;
608 struct umtxq_queue *uh;
610 uc = umtxq_getchain(key);
611 UMTXQ_LOCKED_ASSERT(uc);
612 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
619 * Check if there are multiple PI waiters and returns first
623 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
625 struct umtxq_chain *uc;
626 struct umtxq_queue *uh;
629 uc = umtxq_getchain(key);
630 UMTXQ_LOCKED_ASSERT(uc);
631 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
633 *first = TAILQ_FIRST(&uh->head);
640 umtxq_check_susp(struct thread *td)
646 * The check for TDF_NEEDSUSPCHK is racy, but it is enough to
647 * eventually break the lockstep loop.
649 if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
654 if (P_SHOULDSTOP(p) ||
655 ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) {
656 if (p->p_flag & P_SINGLE_EXIT)
666 * Wake up threads waiting on an userland object.
670 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
672 struct umtxq_chain *uc;
673 struct umtxq_queue *uh;
678 uc = umtxq_getchain(key);
679 UMTXQ_LOCKED_ASSERT(uc);
680 uh = umtxq_queue_lookup(key, q);
682 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
683 umtxq_remove_queue(uq, q);
694 * Wake up specified thread.
697 umtxq_signal_thread(struct umtx_q *uq)
699 struct umtxq_chain *uc;
701 uc = umtxq_getchain(&uq->uq_key);
702 UMTXQ_LOCKED_ASSERT(uc);
708 tstohz(const struct timespec *tsp)
712 TIMESPEC_TO_TIMEVAL(&tv, tsp);
717 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
718 const struct timespec *timeout)
721 timo->clockid = clockid;
723 kern_clock_gettime(curthread, clockid, &timo->end);
724 timo->cur = timo->end;
725 timespecadd(&timo->end, timeout);
727 timo->end = *timeout;
728 kern_clock_gettime(curthread, clockid, &timo->cur);
733 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
736 abs_timeout_init(timo, umtxtime->_clockid,
737 (umtxtime->_flags & UMTX_ABSTIME) != 0,
738 &umtxtime->_timeout);
742 abs_timeout_update(struct abs_timeout *timo)
744 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
748 abs_timeout_gethz(struct abs_timeout *timo)
752 if (timespeccmp(&timo->end, &timo->cur, <=))
755 timespecsub(&tts, &timo->cur);
756 return (tstohz(&tts));
760 * Put thread into sleep state, before sleeping, check if
761 * thread was removed from umtx queue.
764 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
766 struct umtxq_chain *uc;
769 uc = umtxq_getchain(&uq->uq_key);
770 UMTXQ_LOCKED_ASSERT(uc);
772 if (!(uq->uq_flags & UQF_UMTXQ))
774 if (abstime != NULL) {
775 timo = abs_timeout_gethz(abstime);
780 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
781 if (error != EWOULDBLOCK) {
782 umtxq_lock(&uq->uq_key);
786 abs_timeout_update(abstime);
787 umtxq_lock(&uq->uq_key);
793 * Convert userspace address into unique logical address.
796 umtx_key_get(void *addr, int type, int share, struct umtx_key *key)
798 struct thread *td = curthread;
800 vm_map_entry_t entry;
806 if (share == THREAD_SHARE) {
808 key->info.private.vs = td->td_proc->p_vmspace;
809 key->info.private.addr = (uintptr_t)addr;
811 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
812 map = &td->td_proc->p_vmspace->vm_map;
813 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
814 &entry, &key->info.shared.object, &pindex, &prot,
815 &wired) != KERN_SUCCESS) {
819 if ((share == PROCESS_SHARE) ||
820 (share == AUTO_SHARE &&
821 VM_INHERIT_SHARE == entry->inheritance)) {
823 key->info.shared.offset = entry->offset + entry->start -
825 vm_object_reference(key->info.shared.object);
828 key->info.private.vs = td->td_proc->p_vmspace;
829 key->info.private.addr = (uintptr_t)addr;
831 vm_map_lookup_done(map, entry);
842 umtx_key_release(struct umtx_key *key)
845 vm_object_deallocate(key->info.shared.object);
849 * Fetch and compare value, sleep on the address if value is not changed.
852 do_wait(struct thread *td, void *addr, u_long id,
853 struct _umtx_time *timeout, int compat32, int is_private)
855 struct abs_timeout timo;
862 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
863 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
867 abs_timeout_init2(&timo, timeout);
869 umtxq_lock(&uq->uq_key);
871 umtxq_unlock(&uq->uq_key);
873 error = fueword(addr, &tmp);
877 error = fueword32(addr, &tmp32);
883 umtxq_lock(&uq->uq_key);
886 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
888 if ((uq->uq_flags & UQF_UMTXQ) == 0)
892 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
895 umtxq_unlock(&uq->uq_key);
896 umtx_key_release(&uq->uq_key);
897 if (error == ERESTART)
903 * Wake up threads sleeping on the specified address.
906 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
911 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
912 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
915 umtxq_signal(&key, n_wake);
917 umtx_key_release(&key);
922 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
925 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
926 struct _umtx_time *timeout, int mode)
928 struct abs_timeout timo;
930 uint32_t owner, old, id;
937 abs_timeout_init2(&timo, timeout);
940 * Care must be exercised when dealing with umtx structure. It
941 * can fault on any access.
944 rv = fueword32(&m->m_owner, &owner);
947 if (mode == _UMUTEX_WAIT) {
948 if (owner == UMUTEX_UNOWNED || owner == UMUTEX_CONTESTED)
952 * Try the uncontested case. This should be done in userland.
954 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
956 /* The address was invalid. */
960 /* The acquire succeeded. */
961 if (owner == UMUTEX_UNOWNED)
964 /* If no one owns it but it is contested try to acquire it. */
965 if (owner == UMUTEX_CONTESTED) {
966 rv = casueword32(&m->m_owner,
967 UMUTEX_CONTESTED, &owner,
968 id | UMUTEX_CONTESTED);
969 /* The address was invalid. */
973 if (owner == UMUTEX_CONTESTED)
976 rv = umtxq_check_susp(td);
980 /* If this failed the lock has changed, restart. */
985 if (mode == _UMUTEX_TRY)
989 * If we caught a signal, we have retried and now
995 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
996 GET_SHARE(flags), &uq->uq_key)) != 0)
999 umtxq_lock(&uq->uq_key);
1000 umtxq_busy(&uq->uq_key);
1002 umtxq_unlock(&uq->uq_key);
1005 * Set the contested bit so that a release in user space
1006 * knows to use the system call for unlock. If this fails
1007 * either some one else has acquired the lock or it has been
1010 rv = casueword32(&m->m_owner, owner, &old,
1011 owner | UMUTEX_CONTESTED);
1013 /* The address was invalid. */
1015 umtxq_lock(&uq->uq_key);
1017 umtxq_unbusy(&uq->uq_key);
1018 umtxq_unlock(&uq->uq_key);
1019 umtx_key_release(&uq->uq_key);
1024 * We set the contested bit, sleep. Otherwise the lock changed
1025 * and we need to retry or we lost a race to the thread
1026 * unlocking the umtx.
1028 umtxq_lock(&uq->uq_key);
1029 umtxq_unbusy(&uq->uq_key);
1031 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1034 umtxq_unlock(&uq->uq_key);
1035 umtx_key_release(&uq->uq_key);
1038 error = umtxq_check_susp(td);
1045 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1048 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags)
1050 struct umtx_key key;
1051 uint32_t owner, old, id;
1057 * Make sure we own this mtx.
1059 error = fueword32(&m->m_owner, &owner);
1063 if ((owner & ~UMUTEX_CONTESTED) != id)
1066 if ((owner & UMUTEX_CONTESTED) == 0) {
1067 error = casueword32(&m->m_owner, owner, &old, UMUTEX_UNOWNED);
1075 /* We should only ever be in here for contested locks */
1076 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1082 count = umtxq_count(&key);
1086 * When unlocking the umtx, it must be marked as unowned if
1087 * there is zero or one thread only waiting for it.
1088 * Otherwise, it must be marked as contested.
1090 error = casueword32(&m->m_owner, owner, &old,
1091 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1093 umtxq_signal(&key,1);
1096 umtx_key_release(&key);
1105 * Check if the mutex is available and wake up a waiter,
1106 * only for simple mutex.
1109 do_wake_umutex(struct thread *td, struct umutex *m)
1111 struct umtx_key key;
1117 error = fueword32(&m->m_owner, &owner);
1121 if ((owner & ~UMUTEX_CONTESTED) != 0)
1124 error = fueword32(&m->m_flags, &flags);
1128 /* We should only ever be in here for contested locks */
1129 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1135 count = umtxq_count(&key);
1139 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1146 if (error == 0 && count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1147 umtxq_signal(&key, 1);
1150 umtx_key_release(&key);
1155 * Check if the mutex has waiters and tries to fix contention bit.
1158 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1160 struct umtx_key key;
1161 uint32_t owner, old;
1166 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
1168 type = TYPE_NORMAL_UMUTEX;
1170 case UMUTEX_PRIO_INHERIT:
1171 type = TYPE_PI_UMUTEX;
1173 case UMUTEX_PRIO_PROTECT:
1174 type = TYPE_PP_UMUTEX;
1179 if ((error = umtx_key_get(m, type, GET_SHARE(flags),
1186 count = umtxq_count(&key);
1189 * Only repair contention bit if there is a waiter, this means the mutex
1190 * is still being referenced by userland code, otherwise don't update
1194 error = fueword32(&m->m_owner, &owner);
1197 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0) {
1198 error = casueword32(&m->m_owner, owner, &old,
1199 owner | UMUTEX_CONTESTED);
1207 error = umtxq_check_susp(td);
1211 } else if (count == 1) {
1212 error = fueword32(&m->m_owner, &owner);
1215 while (error == 0 && (owner & ~UMUTEX_CONTESTED) != 0 &&
1216 (owner & UMUTEX_CONTESTED) == 0) {
1217 error = casueword32(&m->m_owner, owner, &old,
1218 owner | UMUTEX_CONTESTED);
1226 error = umtxq_check_susp(td);
1232 if (error == EFAULT) {
1233 umtxq_signal(&key, INT_MAX);
1234 } else if (count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1235 umtxq_signal(&key, 1);
1238 umtx_key_release(&key);
1242 static inline struct umtx_pi *
1243 umtx_pi_alloc(int flags)
1247 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1248 TAILQ_INIT(&pi->pi_blocked);
1249 atomic_add_int(&umtx_pi_allocated, 1);
1254 umtx_pi_free(struct umtx_pi *pi)
1256 uma_zfree(umtx_pi_zone, pi);
1257 atomic_add_int(&umtx_pi_allocated, -1);
1261 * Adjust the thread's position on a pi_state after its priority has been
1265 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1267 struct umtx_q *uq, *uq1, *uq2;
1270 mtx_assert(&umtx_lock, MA_OWNED);
1277 * Check if the thread needs to be moved on the blocked chain.
1278 * It needs to be moved if either its priority is lower than
1279 * the previous thread or higher than the next thread.
1281 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1282 uq2 = TAILQ_NEXT(uq, uq_lockq);
1283 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1284 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1286 * Remove thread from blocked chain and determine where
1287 * it should be moved to.
1289 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1290 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1291 td1 = uq1->uq_thread;
1292 MPASS(td1->td_proc->p_magic == P_MAGIC);
1293 if (UPRI(td1) > UPRI(td))
1298 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1300 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1305 static struct umtx_pi *
1306 umtx_pi_next(struct umtx_pi *pi)
1308 struct umtx_q *uq_owner;
1310 if (pi->pi_owner == NULL)
1312 uq_owner = pi->pi_owner->td_umtxq;
1313 if (uq_owner == NULL)
1315 return (uq_owner->uq_pi_blocked);
1319 * Floyd's Cycle-Finding Algorithm.
1322 umtx_pi_check_loop(struct umtx_pi *pi)
1324 struct umtx_pi *pi1; /* fast iterator */
1326 mtx_assert(&umtx_lock, MA_OWNED);
1331 pi = umtx_pi_next(pi);
1334 pi1 = umtx_pi_next(pi1);
1337 pi1 = umtx_pi_next(pi1);
1347 * Propagate priority when a thread is blocked on POSIX
1351 umtx_propagate_priority(struct thread *td)
1357 mtx_assert(&umtx_lock, MA_OWNED);
1360 pi = uq->uq_pi_blocked;
1363 if (umtx_pi_check_loop(pi))
1368 if (td == NULL || td == curthread)
1371 MPASS(td->td_proc != NULL);
1372 MPASS(td->td_proc->p_magic == P_MAGIC);
1375 if (td->td_lend_user_pri > pri)
1376 sched_lend_user_prio(td, pri);
1384 * Pick up the lock that td is blocked on.
1387 pi = uq->uq_pi_blocked;
1390 /* Resort td on the list if needed. */
1391 umtx_pi_adjust_thread(pi, td);
1396 * Unpropagate priority for a PI mutex when a thread blocked on
1397 * it is interrupted by signal or resumed by others.
1400 umtx_repropagate_priority(struct umtx_pi *pi)
1402 struct umtx_q *uq, *uq_owner;
1403 struct umtx_pi *pi2;
1406 mtx_assert(&umtx_lock, MA_OWNED);
1408 if (umtx_pi_check_loop(pi))
1410 while (pi != NULL && pi->pi_owner != NULL) {
1412 uq_owner = pi->pi_owner->td_umtxq;
1414 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1415 uq = TAILQ_FIRST(&pi2->pi_blocked);
1417 if (pri > UPRI(uq->uq_thread))
1418 pri = UPRI(uq->uq_thread);
1422 if (pri > uq_owner->uq_inherited_pri)
1423 pri = uq_owner->uq_inherited_pri;
1424 thread_lock(pi->pi_owner);
1425 sched_lend_user_prio(pi->pi_owner, pri);
1426 thread_unlock(pi->pi_owner);
1427 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1428 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1433 * Insert a PI mutex into owned list.
1436 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1438 struct umtx_q *uq_owner;
1440 uq_owner = owner->td_umtxq;
1441 mtx_assert(&umtx_lock, MA_OWNED);
1442 if (pi->pi_owner != NULL)
1443 panic("pi_owner != NULL");
1444 pi->pi_owner = owner;
1445 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1450 * Disown a PI mutex, and remove it from the owned list.
1453 umtx_pi_disown(struct umtx_pi *pi)
1456 mtx_assert(&umtx_lock, MA_OWNED);
1457 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1458 pi->pi_owner = NULL;
1462 * Claim ownership of a PI mutex.
1465 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1469 mtx_lock(&umtx_lock);
1470 if (pi->pi_owner == owner) {
1471 mtx_unlock(&umtx_lock);
1475 if (pi->pi_owner != NULL) {
1477 * userland may have already messed the mutex, sigh.
1479 mtx_unlock(&umtx_lock);
1482 umtx_pi_setowner(pi, owner);
1483 uq = TAILQ_FIRST(&pi->pi_blocked);
1487 pri = UPRI(uq->uq_thread);
1489 if (pri < UPRI(owner))
1490 sched_lend_user_prio(owner, pri);
1491 thread_unlock(owner);
1493 mtx_unlock(&umtx_lock);
1498 * Adjust a thread's order position in its blocked PI mutex,
1499 * this may result new priority propagating process.
1502 umtx_pi_adjust(struct thread *td, u_char oldpri)
1508 mtx_lock(&umtx_lock);
1510 * Pick up the lock that td is blocked on.
1512 pi = uq->uq_pi_blocked;
1514 umtx_pi_adjust_thread(pi, td);
1515 umtx_repropagate_priority(pi);
1517 mtx_unlock(&umtx_lock);
1521 * Sleep on a PI mutex.
1524 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi,
1525 uint32_t owner, const char *wmesg, struct abs_timeout *timo)
1527 struct umtxq_chain *uc;
1528 struct thread *td, *td1;
1534 KASSERT(td == curthread, ("inconsistent uq_thread"));
1535 uc = umtxq_getchain(&uq->uq_key);
1536 UMTXQ_LOCKED_ASSERT(uc);
1537 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1539 mtx_lock(&umtx_lock);
1540 if (pi->pi_owner == NULL) {
1541 mtx_unlock(&umtx_lock);
1542 /* XXX Only look up thread in current process. */
1543 td1 = tdfind(owner, curproc->p_pid);
1544 mtx_lock(&umtx_lock);
1546 if (pi->pi_owner == NULL)
1547 umtx_pi_setowner(pi, td1);
1548 PROC_UNLOCK(td1->td_proc);
1552 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1553 pri = UPRI(uq1->uq_thread);
1559 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1561 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1563 uq->uq_pi_blocked = pi;
1565 td->td_flags |= TDF_UPIBLOCKED;
1567 umtx_propagate_priority(td);
1568 mtx_unlock(&umtx_lock);
1569 umtxq_unbusy(&uq->uq_key);
1571 error = umtxq_sleep(uq, wmesg, timo);
1574 mtx_lock(&umtx_lock);
1575 uq->uq_pi_blocked = NULL;
1577 td->td_flags &= ~TDF_UPIBLOCKED;
1579 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1580 umtx_repropagate_priority(pi);
1581 mtx_unlock(&umtx_lock);
1582 umtxq_unlock(&uq->uq_key);
1588 * Add reference count for a PI mutex.
1591 umtx_pi_ref(struct umtx_pi *pi)
1593 struct umtxq_chain *uc;
1595 uc = umtxq_getchain(&pi->pi_key);
1596 UMTXQ_LOCKED_ASSERT(uc);
1601 * Decrease reference count for a PI mutex, if the counter
1602 * is decreased to zero, its memory space is freed.
1605 umtx_pi_unref(struct umtx_pi *pi)
1607 struct umtxq_chain *uc;
1609 uc = umtxq_getchain(&pi->pi_key);
1610 UMTXQ_LOCKED_ASSERT(uc);
1611 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1612 if (--pi->pi_refcount == 0) {
1613 mtx_lock(&umtx_lock);
1614 if (pi->pi_owner != NULL)
1616 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1617 ("blocked queue not empty"));
1618 mtx_unlock(&umtx_lock);
1619 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1625 * Find a PI mutex in hash table.
1627 static struct umtx_pi *
1628 umtx_pi_lookup(struct umtx_key *key)
1630 struct umtxq_chain *uc;
1633 uc = umtxq_getchain(key);
1634 UMTXQ_LOCKED_ASSERT(uc);
1636 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1637 if (umtx_key_match(&pi->pi_key, key)) {
1645 * Insert a PI mutex into hash table.
1648 umtx_pi_insert(struct umtx_pi *pi)
1650 struct umtxq_chain *uc;
1652 uc = umtxq_getchain(&pi->pi_key);
1653 UMTXQ_LOCKED_ASSERT(uc);
1654 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1661 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1662 struct _umtx_time *timeout, int try)
1664 struct abs_timeout timo;
1666 struct umtx_pi *pi, *new_pi;
1667 uint32_t id, owner, old;
1673 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1677 if (timeout != NULL)
1678 abs_timeout_init2(&timo, timeout);
1680 umtxq_lock(&uq->uq_key);
1681 pi = umtx_pi_lookup(&uq->uq_key);
1683 new_pi = umtx_pi_alloc(M_NOWAIT);
1684 if (new_pi == NULL) {
1685 umtxq_unlock(&uq->uq_key);
1686 new_pi = umtx_pi_alloc(M_WAITOK);
1687 umtxq_lock(&uq->uq_key);
1688 pi = umtx_pi_lookup(&uq->uq_key);
1690 umtx_pi_free(new_pi);
1694 if (new_pi != NULL) {
1695 new_pi->pi_key = uq->uq_key;
1696 umtx_pi_insert(new_pi);
1701 umtxq_unlock(&uq->uq_key);
1704 * Care must be exercised when dealing with umtx structure. It
1705 * can fault on any access.
1709 * Try the uncontested case. This should be done in userland.
1711 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
1712 /* The address was invalid. */
1718 /* The acquire succeeded. */
1719 if (owner == UMUTEX_UNOWNED) {
1724 /* If no one owns it but it is contested try to acquire it. */
1725 if (owner == UMUTEX_CONTESTED) {
1726 rv = casueword32(&m->m_owner,
1727 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
1728 /* The address was invalid. */
1734 if (owner == UMUTEX_CONTESTED) {
1735 umtxq_lock(&uq->uq_key);
1736 umtxq_busy(&uq->uq_key);
1737 error = umtx_pi_claim(pi, td);
1738 umtxq_unbusy(&uq->uq_key);
1739 umtxq_unlock(&uq->uq_key);
1742 * Since we're going to return an
1743 * error, restore the m_owner to its
1744 * previous, unowned state to avoid
1745 * compounding the problem.
1747 (void)casuword32(&m->m_owner,
1748 id | UMUTEX_CONTESTED,
1754 error = umtxq_check_susp(td);
1758 /* If this failed the lock has changed, restart. */
1762 if ((owner & ~UMUTEX_CONTESTED) == id) {
1773 * If we caught a signal, we have retried and now
1779 umtxq_lock(&uq->uq_key);
1780 umtxq_busy(&uq->uq_key);
1781 umtxq_unlock(&uq->uq_key);
1784 * Set the contested bit so that a release in user space
1785 * knows to use the system call for unlock. If this fails
1786 * either some one else has acquired the lock or it has been
1789 rv = casueword32(&m->m_owner, owner, &old,
1790 owner | UMUTEX_CONTESTED);
1792 /* The address was invalid. */
1794 umtxq_unbusy_unlocked(&uq->uq_key);
1799 umtxq_lock(&uq->uq_key);
1801 * We set the contested bit, sleep. Otherwise the lock changed
1802 * and we need to retry or we lost a race to the thread
1803 * unlocking the umtx.
1806 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
1807 "umtxpi", timeout == NULL ? NULL : &timo);
1811 umtxq_unbusy(&uq->uq_key);
1812 umtxq_unlock(&uq->uq_key);
1815 error = umtxq_check_susp(td);
1820 umtxq_lock(&uq->uq_key);
1822 umtxq_unlock(&uq->uq_key);
1824 umtx_key_release(&uq->uq_key);
1829 * Unlock a PI mutex.
1832 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags)
1834 struct umtx_key key;
1835 struct umtx_q *uq_first, *uq_first2, *uq_me;
1836 struct umtx_pi *pi, *pi2;
1837 uint32_t owner, old, id;
1844 * Make sure we own this mtx.
1846 error = fueword32(&m->m_owner, &owner);
1850 if ((owner & ~UMUTEX_CONTESTED) != id)
1853 /* This should be done in userland */
1854 if ((owner & UMUTEX_CONTESTED) == 0) {
1855 error = casueword32(&m->m_owner, owner, &old, UMUTEX_UNOWNED);
1863 /* We should only ever be in here for contested locks */
1864 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1870 count = umtxq_count_pi(&key, &uq_first);
1871 if (uq_first != NULL) {
1872 mtx_lock(&umtx_lock);
1873 pi = uq_first->uq_pi_blocked;
1874 KASSERT(pi != NULL, ("pi == NULL?"));
1875 if (pi->pi_owner != td) {
1876 mtx_unlock(&umtx_lock);
1879 umtx_key_release(&key);
1880 /* userland messed the mutex */
1883 uq_me = td->td_umtxq;
1885 /* get highest priority thread which is still sleeping. */
1886 uq_first = TAILQ_FIRST(&pi->pi_blocked);
1887 while (uq_first != NULL &&
1888 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
1889 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
1892 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
1893 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
1894 if (uq_first2 != NULL) {
1895 if (pri > UPRI(uq_first2->uq_thread))
1896 pri = UPRI(uq_first2->uq_thread);
1900 sched_lend_user_prio(td, pri);
1902 mtx_unlock(&umtx_lock);
1904 umtxq_signal_thread(uq_first);
1906 pi = umtx_pi_lookup(&key);
1908 * A umtx_pi can exist if a signal or timeout removed the
1909 * last waiter from the umtxq, but there is still
1910 * a thread in do_lock_pi() holding the umtx_pi.
1914 * The umtx_pi can be unowned, such as when a thread
1915 * has just entered do_lock_pi(), allocated the
1916 * umtx_pi, and unlocked the umtxq.
1917 * If the current thread owns it, it must disown it.
1919 mtx_lock(&umtx_lock);
1920 if (pi->pi_owner == td)
1922 mtx_unlock(&umtx_lock);
1928 * When unlocking the umtx, it must be marked as unowned if
1929 * there is zero or one thread only waiting for it.
1930 * Otherwise, it must be marked as contested.
1932 error = casueword32(&m->m_owner, owner, &old,
1933 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1935 umtxq_unbusy_unlocked(&key);
1936 umtx_key_release(&key);
1948 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
1949 struct _umtx_time *timeout, int try)
1951 struct abs_timeout timo;
1952 struct umtx_q *uq, *uq2;
1956 int error, pri, old_inherited_pri, su, rv;
1960 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
1964 if (timeout != NULL)
1965 abs_timeout_init2(&timo, timeout);
1967 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
1969 old_inherited_pri = uq->uq_inherited_pri;
1970 umtxq_lock(&uq->uq_key);
1971 umtxq_busy(&uq->uq_key);
1972 umtxq_unlock(&uq->uq_key);
1974 rv = fueword32(&m->m_ceilings[0], &ceiling);
1979 ceiling = RTP_PRIO_MAX - ceiling;
1980 if (ceiling > RTP_PRIO_MAX) {
1985 mtx_lock(&umtx_lock);
1986 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
1987 mtx_unlock(&umtx_lock);
1991 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
1992 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
1994 if (uq->uq_inherited_pri < UPRI(td))
1995 sched_lend_user_prio(td, uq->uq_inherited_pri);
1998 mtx_unlock(&umtx_lock);
2000 rv = casueword32(&m->m_owner,
2001 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
2002 /* The address was invalid. */
2008 if (owner == UMUTEX_CONTESTED) {
2019 * If we caught a signal, we have retried and now
2025 umtxq_lock(&uq->uq_key);
2027 umtxq_unbusy(&uq->uq_key);
2028 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2031 umtxq_unlock(&uq->uq_key);
2033 mtx_lock(&umtx_lock);
2034 uq->uq_inherited_pri = old_inherited_pri;
2036 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2037 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2039 if (pri > UPRI(uq2->uq_thread))
2040 pri = UPRI(uq2->uq_thread);
2043 if (pri > uq->uq_inherited_pri)
2044 pri = uq->uq_inherited_pri;
2046 sched_lend_user_prio(td, pri);
2048 mtx_unlock(&umtx_lock);
2052 mtx_lock(&umtx_lock);
2053 uq->uq_inherited_pri = old_inherited_pri;
2055 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2056 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2058 if (pri > UPRI(uq2->uq_thread))
2059 pri = UPRI(uq2->uq_thread);
2062 if (pri > uq->uq_inherited_pri)
2063 pri = uq->uq_inherited_pri;
2065 sched_lend_user_prio(td, pri);
2067 mtx_unlock(&umtx_lock);
2071 umtxq_unbusy_unlocked(&uq->uq_key);
2072 umtx_key_release(&uq->uq_key);
2077 * Unlock a PP mutex.
2080 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags)
2082 struct umtx_key key;
2083 struct umtx_q *uq, *uq2;
2087 int error, pri, new_inherited_pri, su;
2091 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2094 * Make sure we own this mtx.
2096 error = fueword32(&m->m_owner, &owner);
2100 if ((owner & ~UMUTEX_CONTESTED) != id)
2103 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2108 new_inherited_pri = PRI_MAX;
2110 rceiling = RTP_PRIO_MAX - rceiling;
2111 if (rceiling > RTP_PRIO_MAX)
2113 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2116 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2123 * For priority protected mutex, always set unlocked state
2124 * to UMUTEX_CONTESTED, so that userland always enters kernel
2125 * to lock the mutex, it is necessary because thread priority
2126 * has to be adjusted for such mutex.
2128 error = suword32(&m->m_owner, UMUTEX_CONTESTED);
2132 umtxq_signal(&key, 1);
2139 mtx_lock(&umtx_lock);
2141 uq->uq_inherited_pri = new_inherited_pri;
2143 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2144 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2146 if (pri > UPRI(uq2->uq_thread))
2147 pri = UPRI(uq2->uq_thread);
2150 if (pri > uq->uq_inherited_pri)
2151 pri = uq->uq_inherited_pri;
2153 sched_lend_user_prio(td, pri);
2155 mtx_unlock(&umtx_lock);
2157 umtx_key_release(&key);
2162 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2163 uint32_t *old_ceiling)
2166 uint32_t save_ceiling;
2171 error = fueword32(&m->m_flags, &flags);
2174 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2176 if (ceiling > RTP_PRIO_MAX)
2180 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2184 umtxq_lock(&uq->uq_key);
2185 umtxq_busy(&uq->uq_key);
2186 umtxq_unlock(&uq->uq_key);
2188 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2194 rv = casueword32(&m->m_owner,
2195 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
2201 if (owner == UMUTEX_CONTESTED) {
2202 suword32(&m->m_ceilings[0], ceiling);
2203 suword32(&m->m_owner, UMUTEX_CONTESTED);
2208 if ((owner & ~UMUTEX_CONTESTED) == id) {
2209 suword32(&m->m_ceilings[0], ceiling);
2215 * If we caught a signal, we have retried and now
2222 * We set the contested bit, sleep. Otherwise the lock changed
2223 * and we need to retry or we lost a race to the thread
2224 * unlocking the umtx.
2226 umtxq_lock(&uq->uq_key);
2228 umtxq_unbusy(&uq->uq_key);
2229 error = umtxq_sleep(uq, "umtxpp", NULL);
2231 umtxq_unlock(&uq->uq_key);
2233 umtxq_lock(&uq->uq_key);
2235 umtxq_signal(&uq->uq_key, INT_MAX);
2236 umtxq_unbusy(&uq->uq_key);
2237 umtxq_unlock(&uq->uq_key);
2238 umtx_key_release(&uq->uq_key);
2239 if (error == 0 && old_ceiling != NULL)
2240 suword32(old_ceiling, save_ceiling);
2245 * Lock a userland POSIX mutex.
2248 do_lock_umutex(struct thread *td, struct umutex *m,
2249 struct _umtx_time *timeout, int mode)
2254 error = fueword32(&m->m_flags, &flags);
2258 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2260 error = do_lock_normal(td, m, flags, timeout, mode);
2262 case UMUTEX_PRIO_INHERIT:
2263 error = do_lock_pi(td, m, flags, timeout, mode);
2265 case UMUTEX_PRIO_PROTECT:
2266 error = do_lock_pp(td, m, flags, timeout, mode);
2271 if (timeout == NULL) {
2272 if (error == EINTR && mode != _UMUTEX_WAIT)
2275 /* Timed-locking is not restarted. */
2276 if (error == ERESTART)
2283 * Unlock a userland POSIX mutex.
2286 do_unlock_umutex(struct thread *td, struct umutex *m)
2291 error = fueword32(&m->m_flags, &flags);
2295 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2297 return (do_unlock_normal(td, m, flags));
2298 case UMUTEX_PRIO_INHERIT:
2299 return (do_unlock_pi(td, m, flags));
2300 case UMUTEX_PRIO_PROTECT:
2301 return (do_unlock_pp(td, m, flags));
2308 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2309 struct timespec *timeout, u_long wflags)
2311 struct abs_timeout timo;
2313 uint32_t flags, clockid, hasw;
2317 error = fueword32(&cv->c_flags, &flags);
2320 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2324 if ((wflags & CVWAIT_CLOCKID) != 0) {
2325 error = fueword32(&cv->c_clockid, &clockid);
2327 umtx_key_release(&uq->uq_key);
2330 if (clockid < CLOCK_REALTIME ||
2331 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2332 /* hmm, only HW clock id will work. */
2333 umtx_key_release(&uq->uq_key);
2337 clockid = CLOCK_REALTIME;
2340 umtxq_lock(&uq->uq_key);
2341 umtxq_busy(&uq->uq_key);
2343 umtxq_unlock(&uq->uq_key);
2346 * Set c_has_waiters to 1 before releasing user mutex, also
2347 * don't modify cache line when unnecessary.
2349 error = fueword32(&cv->c_has_waiters, &hasw);
2350 if (error == 0 && hasw == 0)
2351 suword32(&cv->c_has_waiters, 1);
2353 umtxq_unbusy_unlocked(&uq->uq_key);
2355 error = do_unlock_umutex(td, m);
2357 if (timeout != NULL)
2358 abs_timeout_init(&timo, clockid, ((wflags & CVWAIT_ABSTIME) != 0),
2361 umtxq_lock(&uq->uq_key);
2363 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2367 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2371 * This must be timeout,interrupted by signal or
2372 * surprious wakeup, clear c_has_waiter flag when
2375 umtxq_busy(&uq->uq_key);
2376 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2377 int oldlen = uq->uq_cur_queue->length;
2380 umtxq_unlock(&uq->uq_key);
2381 suword32(&cv->c_has_waiters, 0);
2382 umtxq_lock(&uq->uq_key);
2385 umtxq_unbusy(&uq->uq_key);
2386 if (error == ERESTART)
2390 umtxq_unlock(&uq->uq_key);
2391 umtx_key_release(&uq->uq_key);
2396 * Signal a userland condition variable.
2399 do_cv_signal(struct thread *td, struct ucond *cv)
2401 struct umtx_key key;
2402 int error, cnt, nwake;
2405 error = fueword32(&cv->c_flags, &flags);
2408 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2412 cnt = umtxq_count(&key);
2413 nwake = umtxq_signal(&key, 1);
2416 error = suword32(&cv->c_has_waiters, 0);
2423 umtx_key_release(&key);
2428 do_cv_broadcast(struct thread *td, struct ucond *cv)
2430 struct umtx_key key;
2434 error = fueword32(&cv->c_flags, &flags);
2437 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2442 umtxq_signal(&key, INT_MAX);
2445 error = suword32(&cv->c_has_waiters, 0);
2449 umtxq_unbusy_unlocked(&key);
2451 umtx_key_release(&key);
2456 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, struct _umtx_time *timeout)
2458 struct abs_timeout timo;
2460 uint32_t flags, wrflags;
2461 int32_t state, oldstate;
2462 int32_t blocked_readers;
2466 error = fueword32(&rwlock->rw_flags, &flags);
2469 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2473 if (timeout != NULL)
2474 abs_timeout_init2(&timo, timeout);
2476 wrflags = URWLOCK_WRITE_OWNER;
2477 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2478 wrflags |= URWLOCK_WRITE_WAITERS;
2481 rv = fueword32(&rwlock->rw_state, &state);
2483 umtx_key_release(&uq->uq_key);
2487 /* try to lock it */
2488 while (!(state & wrflags)) {
2489 if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) {
2490 umtx_key_release(&uq->uq_key);
2493 rv = casueword32(&rwlock->rw_state, state,
2494 &oldstate, state + 1);
2496 umtx_key_release(&uq->uq_key);
2499 if (oldstate == state) {
2500 umtx_key_release(&uq->uq_key);
2503 error = umtxq_check_susp(td);
2512 /* grab monitor lock */
2513 umtxq_lock(&uq->uq_key);
2514 umtxq_busy(&uq->uq_key);
2515 umtxq_unlock(&uq->uq_key);
2518 * re-read the state, in case it changed between the try-lock above
2519 * and the check below
2521 rv = fueword32(&rwlock->rw_state, &state);
2525 /* set read contention bit */
2526 while (error == 0 && (state & wrflags) &&
2527 !(state & URWLOCK_READ_WAITERS)) {
2528 rv = casueword32(&rwlock->rw_state, state,
2529 &oldstate, state | URWLOCK_READ_WAITERS);
2534 if (oldstate == state)
2537 error = umtxq_check_susp(td);
2542 umtxq_unbusy_unlocked(&uq->uq_key);
2546 /* state is changed while setting flags, restart */
2547 if (!(state & wrflags)) {
2548 umtxq_unbusy_unlocked(&uq->uq_key);
2549 error = umtxq_check_susp(td);
2556 /* contention bit is set, before sleeping, increase read waiter count */
2557 rv = fueword32(&rwlock->rw_blocked_readers,
2560 umtxq_unbusy_unlocked(&uq->uq_key);
2564 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2566 while (state & wrflags) {
2567 umtxq_lock(&uq->uq_key);
2569 umtxq_unbusy(&uq->uq_key);
2571 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2574 umtxq_busy(&uq->uq_key);
2576 umtxq_unlock(&uq->uq_key);
2579 rv = fueword32(&rwlock->rw_state, &state);
2586 /* decrease read waiter count, and may clear read contention bit */
2587 rv = fueword32(&rwlock->rw_blocked_readers,
2590 umtxq_unbusy_unlocked(&uq->uq_key);
2594 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2595 if (blocked_readers == 1) {
2596 rv = fueword32(&rwlock->rw_state, &state);
2599 while (error == 0) {
2600 rv = casueword32(&rwlock->rw_state, state,
2601 &oldstate, state & ~URWLOCK_READ_WAITERS);
2606 if (oldstate == state)
2609 error = umtxq_check_susp(td);
2613 umtxq_unbusy_unlocked(&uq->uq_key);
2617 umtx_key_release(&uq->uq_key);
2618 if (error == ERESTART)
2624 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2626 struct abs_timeout timo;
2629 int32_t state, oldstate;
2630 int32_t blocked_writers;
2631 int32_t blocked_readers;
2635 error = fueword32(&rwlock->rw_flags, &flags);
2638 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2642 if (timeout != NULL)
2643 abs_timeout_init2(&timo, timeout);
2645 blocked_readers = 0;
2647 rv = fueword32(&rwlock->rw_state, &state);
2649 umtx_key_release(&uq->uq_key);
2652 while (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2653 rv = casueword32(&rwlock->rw_state, state,
2654 &oldstate, state | URWLOCK_WRITE_OWNER);
2656 umtx_key_release(&uq->uq_key);
2659 if (oldstate == state) {
2660 umtx_key_release(&uq->uq_key);
2664 error = umtxq_check_susp(td);
2670 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) &&
2671 blocked_readers != 0) {
2672 umtxq_lock(&uq->uq_key);
2673 umtxq_busy(&uq->uq_key);
2674 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE);
2675 umtxq_unbusy(&uq->uq_key);
2676 umtxq_unlock(&uq->uq_key);
2682 /* grab monitor lock */
2683 umtxq_lock(&uq->uq_key);
2684 umtxq_busy(&uq->uq_key);
2685 umtxq_unlock(&uq->uq_key);
2688 * re-read the state, in case it changed between the try-lock above
2689 * and the check below
2691 rv = fueword32(&rwlock->rw_state, &state);
2695 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
2696 URWLOCK_READER_COUNT(state) != 0) &&
2697 (state & URWLOCK_WRITE_WAITERS) == 0) {
2698 rv = casueword32(&rwlock->rw_state, state,
2699 &oldstate, state | URWLOCK_WRITE_WAITERS);
2704 if (oldstate == state)
2707 error = umtxq_check_susp(td);
2712 umtxq_unbusy_unlocked(&uq->uq_key);
2716 if (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2717 umtxq_unbusy_unlocked(&uq->uq_key);
2718 error = umtxq_check_susp(td);
2724 rv = fueword32(&rwlock->rw_blocked_writers,
2727 umtxq_unbusy_unlocked(&uq->uq_key);
2731 suword32(&rwlock->rw_blocked_writers, blocked_writers+1);
2733 while ((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) {
2734 umtxq_lock(&uq->uq_key);
2735 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2736 umtxq_unbusy(&uq->uq_key);
2738 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
2741 umtxq_busy(&uq->uq_key);
2742 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2743 umtxq_unlock(&uq->uq_key);
2746 rv = fueword32(&rwlock->rw_state, &state);
2753 rv = fueword32(&rwlock->rw_blocked_writers,
2756 umtxq_unbusy_unlocked(&uq->uq_key);
2760 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
2761 if (blocked_writers == 1) {
2762 rv = fueword32(&rwlock->rw_state, &state);
2764 umtxq_unbusy_unlocked(&uq->uq_key);
2769 rv = casueword32(&rwlock->rw_state, state,
2770 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
2775 if (oldstate == state)
2778 error = umtxq_check_susp(td);
2780 * We are leaving the URWLOCK_WRITE_WAITERS
2781 * behind, but this should not harm the
2787 rv = fueword32(&rwlock->rw_blocked_readers,
2790 umtxq_unbusy_unlocked(&uq->uq_key);
2795 blocked_readers = 0;
2797 umtxq_unbusy_unlocked(&uq->uq_key);
2800 umtx_key_release(&uq->uq_key);
2801 if (error == ERESTART)
2807 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
2811 int32_t state, oldstate;
2812 int error, rv, q, count;
2815 error = fueword32(&rwlock->rw_flags, &flags);
2818 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2822 error = fueword32(&rwlock->rw_state, &state);
2827 if (state & URWLOCK_WRITE_OWNER) {
2829 rv = casueword32(&rwlock->rw_state, state,
2830 &oldstate, state & ~URWLOCK_WRITE_OWNER);
2835 if (oldstate != state) {
2837 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
2841 error = umtxq_check_susp(td);
2847 } else if (URWLOCK_READER_COUNT(state) != 0) {
2849 rv = casueword32(&rwlock->rw_state, state,
2850 &oldstate, state - 1);
2855 if (oldstate != state) {
2857 if (URWLOCK_READER_COUNT(oldstate) == 0) {
2861 error = umtxq_check_susp(td);
2874 if (!(flags & URWLOCK_PREFER_READER)) {
2875 if (state & URWLOCK_WRITE_WAITERS) {
2877 q = UMTX_EXCLUSIVE_QUEUE;
2878 } else if (state & URWLOCK_READ_WAITERS) {
2880 q = UMTX_SHARED_QUEUE;
2883 if (state & URWLOCK_READ_WAITERS) {
2885 q = UMTX_SHARED_QUEUE;
2886 } else if (state & URWLOCK_WRITE_WAITERS) {
2888 q = UMTX_EXCLUSIVE_QUEUE;
2893 umtxq_lock(&uq->uq_key);
2894 umtxq_busy(&uq->uq_key);
2895 umtxq_signal_queue(&uq->uq_key, count, q);
2896 umtxq_unbusy(&uq->uq_key);
2897 umtxq_unlock(&uq->uq_key);
2900 umtx_key_release(&uq->uq_key);
2904 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
2906 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
2908 struct abs_timeout timo;
2910 uint32_t flags, count, count1;
2914 error = fueword32(&sem->_flags, &flags);
2917 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
2921 if (timeout != NULL)
2922 abs_timeout_init2(&timo, timeout);
2924 umtxq_lock(&uq->uq_key);
2925 umtxq_busy(&uq->uq_key);
2927 umtxq_unlock(&uq->uq_key);
2928 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
2930 rv = fueword32(&sem->_count, &count);
2931 if (rv == -1 || count != 0) {
2932 umtxq_lock(&uq->uq_key);
2933 umtxq_unbusy(&uq->uq_key);
2935 umtxq_unlock(&uq->uq_key);
2936 umtx_key_release(&uq->uq_key);
2937 return (rv == -1 ? EFAULT : 0);
2939 umtxq_lock(&uq->uq_key);
2940 umtxq_unbusy(&uq->uq_key);
2942 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
2944 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2948 /* A relative timeout cannot be restarted. */
2949 if (error == ERESTART && timeout != NULL &&
2950 (timeout->_flags & UMTX_ABSTIME) == 0)
2953 umtxq_unlock(&uq->uq_key);
2954 umtx_key_release(&uq->uq_key);
2959 * Signal a userland semaphore.
2962 do_sem_wake(struct thread *td, struct _usem *sem)
2964 struct umtx_key key;
2968 error = fueword32(&sem->_flags, &flags);
2971 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
2975 cnt = umtxq_count(&key);
2977 umtxq_signal(&key, 1);
2979 * Check if count is greater than 0, this means the memory is
2980 * still being referenced by user code, so we can safely
2981 * update _has_waiters flag.
2985 error = suword32(&sem->_has_waiters, 0);
2993 umtx_key_release(&key);
2999 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3001 struct abs_timeout timo;
3003 uint32_t count, flags;
3007 flags = fuword32(&sem->_flags);
3008 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3012 if (timeout != NULL)
3013 abs_timeout_init2(&timo, timeout);
3015 umtxq_lock(&uq->uq_key);
3016 umtxq_busy(&uq->uq_key);
3018 umtxq_unlock(&uq->uq_key);
3019 rv = fueword32(&sem->_count, &count);
3021 umtxq_lock(&uq->uq_key);
3022 umtxq_unbusy(&uq->uq_key);
3024 umtxq_unlock(&uq->uq_key);
3025 umtx_key_release(&uq->uq_key);
3029 if (USEM_COUNT(count) != 0) {
3030 umtxq_lock(&uq->uq_key);
3031 umtxq_unbusy(&uq->uq_key);
3033 umtxq_unlock(&uq->uq_key);
3034 umtx_key_release(&uq->uq_key);
3037 if (count == USEM_HAS_WAITERS)
3039 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3041 umtxq_lock(&uq->uq_key);
3042 umtxq_unbusy(&uq->uq_key);
3044 umtxq_unlock(&uq->uq_key);
3045 umtx_key_release(&uq->uq_key);
3051 umtxq_lock(&uq->uq_key);
3052 umtxq_unbusy(&uq->uq_key);
3054 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3056 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3060 /* A relative timeout cannot be restarted. */
3061 if (error == ERESTART && timeout != NULL &&
3062 (timeout->_flags & UMTX_ABSTIME) == 0)
3065 umtxq_unlock(&uq->uq_key);
3066 umtx_key_release(&uq->uq_key);
3071 * Signal a userland semaphore.
3074 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3076 struct umtx_key key;
3078 uint32_t count, flags;
3080 rv = fueword32(&sem->_flags, &flags);
3083 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3087 cnt = umtxq_count(&key);
3089 umtxq_signal(&key, 1);
3092 * If this was the last sleeping thread, clear the waiters
3097 rv = fueword32(&sem->_count, &count);
3098 while (rv != -1 && count & USEM_HAS_WAITERS)
3099 rv = casueword32(&sem->_count, count, &count,
3100 count & ~USEM_HAS_WAITERS);
3108 umtx_key_release(&key);
3113 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3117 error = copyin(addr, tsp, sizeof(struct timespec));
3119 if (tsp->tv_sec < 0 ||
3120 tsp->tv_nsec >= 1000000000 ||
3128 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3132 if (size <= sizeof(struct timespec)) {
3133 tp->_clockid = CLOCK_REALTIME;
3135 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3137 error = copyin(addr, tp, sizeof(struct _umtx_time));
3140 if (tp->_timeout.tv_sec < 0 ||
3141 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3147 __umtx_op_unimpl(struct thread *td, struct _umtx_op_args *uap)
3150 return (EOPNOTSUPP);
3154 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3156 struct _umtx_time timeout, *tm_p;
3159 if (uap->uaddr2 == NULL)
3162 error = umtx_copyin_umtx_time(
3163 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3168 return do_wait(td, uap->obj, uap->val, tm_p, 0, 0);
3172 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3174 struct _umtx_time timeout, *tm_p;
3177 if (uap->uaddr2 == NULL)
3180 error = umtx_copyin_umtx_time(
3181 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3186 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3190 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3192 struct _umtx_time *tm_p, timeout;
3195 if (uap->uaddr2 == NULL)
3198 error = umtx_copyin_umtx_time(
3199 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3204 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3208 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3210 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3213 #define BATCH_SIZE 128
3215 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3217 int count = uap->val;
3218 void *uaddrs[BATCH_SIZE];
3219 char **upp = (char **)uap->obj;
3226 if (tocopy > BATCH_SIZE)
3227 tocopy = BATCH_SIZE;
3228 error = copyin(upp+pos, uaddrs, tocopy * sizeof(char *));
3231 for (i = 0; i < tocopy; ++i)
3232 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3240 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3242 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3246 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3248 struct _umtx_time *tm_p, timeout;
3251 /* Allow a null timespec (wait forever). */
3252 if (uap->uaddr2 == NULL)
3255 error = umtx_copyin_umtx_time(
3256 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3261 return do_lock_umutex(td, uap->obj, tm_p, 0);
3265 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3267 return do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY);
3271 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3273 struct _umtx_time *tm_p, timeout;
3276 /* Allow a null timespec (wait forever). */
3277 if (uap->uaddr2 == NULL)
3280 error = umtx_copyin_umtx_time(
3281 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3286 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3290 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3292 return do_wake_umutex(td, uap->obj);
3296 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3298 return do_unlock_umutex(td, uap->obj);
3302 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3304 return do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1);
3308 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3310 struct timespec *ts, timeout;
3313 /* Allow a null timespec (wait forever). */
3314 if (uap->uaddr2 == NULL)
3317 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3322 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3326 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3328 return do_cv_signal(td, uap->obj);
3332 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3334 return do_cv_broadcast(td, uap->obj);
3338 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3340 struct _umtx_time timeout;
3343 /* Allow a null timespec (wait forever). */
3344 if (uap->uaddr2 == NULL) {
3345 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3347 error = umtx_copyin_umtx_time(uap->uaddr2,
3348 (size_t)uap->uaddr1, &timeout);
3351 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3357 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3359 struct _umtx_time timeout;
3362 /* Allow a null timespec (wait forever). */
3363 if (uap->uaddr2 == NULL) {
3364 error = do_rw_wrlock(td, uap->obj, 0);
3366 error = umtx_copyin_umtx_time(uap->uaddr2,
3367 (size_t)uap->uaddr1, &timeout);
3371 error = do_rw_wrlock(td, uap->obj, &timeout);
3377 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3379 return do_rw_unlock(td, uap->obj);
3382 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3384 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3386 struct _umtx_time *tm_p, timeout;
3389 /* Allow a null timespec (wait forever). */
3390 if (uap->uaddr2 == NULL)
3393 error = umtx_copyin_umtx_time(
3394 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3399 return (do_sem_wait(td, uap->obj, tm_p));
3403 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3405 return do_sem_wake(td, uap->obj);
3410 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3412 return do_wake2_umutex(td, uap->obj, uap->val);
3416 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap)
3418 struct _umtx_time *tm_p, timeout;
3421 /* Allow a null timespec (wait forever). */
3422 if (uap->uaddr2 == NULL)
3425 error = umtx_copyin_umtx_time(
3426 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3431 return (do_sem2_wait(td, uap->obj, tm_p));
3435 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap)
3437 return do_sem2_wake(td, uap->obj);
3440 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
3442 static _umtx_op_func op_table[] = {
3443 __umtx_op_unimpl, /* UMTX_OP_RESERVED0 */
3444 __umtx_op_unimpl, /* UMTX_OP_RESERVED1 */
3445 __umtx_op_wait, /* UMTX_OP_WAIT */
3446 __umtx_op_wake, /* UMTX_OP_WAKE */
3447 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_TRYLOCK */
3448 __umtx_op_lock_umutex, /* UMTX_OP_MUTEX_LOCK */
3449 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3450 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3451 __umtx_op_cv_wait, /* UMTX_OP_CV_WAIT*/
3452 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3453 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3454 __umtx_op_wait_uint, /* UMTX_OP_WAIT_UINT */
3455 __umtx_op_rw_rdlock, /* UMTX_OP_RW_RDLOCK */
3456 __umtx_op_rw_wrlock, /* UMTX_OP_RW_WRLOCK */
3457 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3458 __umtx_op_wait_uint_private, /* UMTX_OP_WAIT_UINT_PRIVATE */
3459 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3460 __umtx_op_wait_umutex, /* UMTX_OP_MUTEX_WAIT */
3461 __umtx_op_wake_umutex, /* UMTX_OP_MUTEX_WAKE */
3462 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3463 __umtx_op_sem_wait, /* UMTX_OP_SEM_WAIT */
3464 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3466 __umtx_op_unimpl, /* UMTX_OP_SEM_WAIT */
3467 __umtx_op_unimpl, /* UMTX_OP_SEM_WAKE */
3469 __umtx_op_nwake_private, /* UMTX_OP_NWAKE_PRIVATE */
3470 __umtx_op_wake2_umutex, /* UMTX_OP_MUTEX_WAKE2 */
3471 __umtx_op_sem2_wait, /* UMTX_OP_SEM2_WAIT */
3472 __umtx_op_sem2_wake, /* UMTX_OP_SEM2_WAKE */
3476 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
3478 if ((unsigned)uap->op < UMTX_OP_MAX)
3479 return (*op_table[uap->op])(td, uap);
3483 #ifdef COMPAT_FREEBSD32
3490 struct umtx_time32 {
3491 struct timespec32 timeout;
3497 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
3499 struct timespec32 ts32;
3502 error = copyin(addr, &ts32, sizeof(struct timespec32));
3504 if (ts32.tv_sec < 0 ||
3505 ts32.tv_nsec >= 1000000000 ||
3509 tsp->tv_sec = ts32.tv_sec;
3510 tsp->tv_nsec = ts32.tv_nsec;
3517 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
3519 struct umtx_time32 t32;
3522 t32.clockid = CLOCK_REALTIME;
3524 if (size <= sizeof(struct timespec32))
3525 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
3527 error = copyin(addr, &t32, sizeof(struct umtx_time32));
3530 if (t32.timeout.tv_sec < 0 ||
3531 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
3533 tp->_timeout.tv_sec = t32.timeout.tv_sec;
3534 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
3535 tp->_flags = t32.flags;
3536 tp->_clockid = t32.clockid;
3541 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3543 struct _umtx_time *tm_p, timeout;
3546 if (uap->uaddr2 == NULL)
3549 error = umtx_copyin_umtx_time32(uap->uaddr2,
3550 (size_t)uap->uaddr1, &timeout);
3555 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3559 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3561 struct _umtx_time *tm_p, timeout;
3564 /* Allow a null timespec (wait forever). */
3565 if (uap->uaddr2 == NULL)
3568 error = umtx_copyin_umtx_time(uap->uaddr2,
3569 (size_t)uap->uaddr1, &timeout);
3574 return do_lock_umutex(td, uap->obj, tm_p, 0);
3578 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3580 struct _umtx_time *tm_p, timeout;
3583 /* Allow a null timespec (wait forever). */
3584 if (uap->uaddr2 == NULL)
3587 error = umtx_copyin_umtx_time32(uap->uaddr2,
3588 (size_t)uap->uaddr1, &timeout);
3593 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3597 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3599 struct timespec *ts, timeout;
3602 /* Allow a null timespec (wait forever). */
3603 if (uap->uaddr2 == NULL)
3606 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3611 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3615 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3617 struct _umtx_time timeout;
3620 /* Allow a null timespec (wait forever). */
3621 if (uap->uaddr2 == NULL) {
3622 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3624 error = umtx_copyin_umtx_time32(uap->uaddr2,
3625 (size_t)uap->uaddr1, &timeout);
3628 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3634 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3636 struct _umtx_time timeout;
3639 /* Allow a null timespec (wait forever). */
3640 if (uap->uaddr2 == NULL) {
3641 error = do_rw_wrlock(td, uap->obj, 0);
3643 error = umtx_copyin_umtx_time32(uap->uaddr2,
3644 (size_t)uap->uaddr1, &timeout);
3647 error = do_rw_wrlock(td, uap->obj, &timeout);
3653 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3655 struct _umtx_time *tm_p, timeout;
3658 if (uap->uaddr2 == NULL)
3661 error = umtx_copyin_umtx_time32(
3662 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
3667 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3670 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3672 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3674 struct _umtx_time *tm_p, timeout;
3677 /* Allow a null timespec (wait forever). */
3678 if (uap->uaddr2 == NULL)
3681 error = umtx_copyin_umtx_time32(uap->uaddr2,
3682 (size_t)uap->uaddr1, &timeout);
3687 return (do_sem_wait(td, uap->obj, tm_p));
3692 __umtx_op_sem2_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3694 struct _umtx_time *tm_p, timeout;
3697 /* Allow a null timespec (wait forever). */
3698 if (uap->uaddr2 == NULL)
3701 error = umtx_copyin_umtx_time32(uap->uaddr2,
3702 (size_t)uap->uaddr1, &timeout);
3707 return (do_sem2_wait(td, uap->obj, tm_p));
3711 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
3713 int count = uap->val;
3714 uint32_t uaddrs[BATCH_SIZE];
3715 uint32_t **upp = (uint32_t **)uap->obj;
3722 if (tocopy > BATCH_SIZE)
3723 tocopy = BATCH_SIZE;
3724 error = copyin(upp+pos, uaddrs, tocopy * sizeof(uint32_t));
3727 for (i = 0; i < tocopy; ++i)
3728 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
3736 static _umtx_op_func op_table_compat32[] = {
3737 __umtx_op_unimpl, /* UMTX_OP_RESERVED0 */
3738 __umtx_op_unimpl, /* UMTX_OP_RESERVED1 */
3739 __umtx_op_wait_compat32, /* UMTX_OP_WAIT */
3740 __umtx_op_wake, /* UMTX_OP_WAKE */
3741 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_LOCK */
3742 __umtx_op_lock_umutex_compat32, /* UMTX_OP_MUTEX_TRYLOCK */
3743 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3744 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3745 __umtx_op_cv_wait_compat32, /* UMTX_OP_CV_WAIT*/
3746 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3747 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3748 __umtx_op_wait_compat32, /* UMTX_OP_WAIT_UINT */
3749 __umtx_op_rw_rdlock_compat32, /* UMTX_OP_RW_RDLOCK */
3750 __umtx_op_rw_wrlock_compat32, /* UMTX_OP_RW_WRLOCK */
3751 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3752 __umtx_op_wait_uint_private_compat32, /* UMTX_OP_WAIT_UINT_PRIVATE */
3753 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3754 __umtx_op_wait_umutex_compat32, /* UMTX_OP_MUTEX_WAIT */
3755 __umtx_op_wake_umutex, /* UMTX_OP_MUTEX_WAKE */
3756 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3757 __umtx_op_sem_wait_compat32, /* UMTX_OP_SEM_WAIT */
3758 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3760 __umtx_op_unimpl, /* UMTX_OP_SEM_WAIT */
3761 __umtx_op_unimpl, /* UMTX_OP_SEM_WAKE */
3763 __umtx_op_nwake_private32, /* UMTX_OP_NWAKE_PRIVATE */
3764 __umtx_op_wake2_umutex, /* UMTX_OP_MUTEX_WAKE2 */
3765 __umtx_op_sem2_wait_compat32, /* UMTX_OP_SEM2_WAIT */
3766 __umtx_op_sem2_wake, /* UMTX_OP_SEM2_WAKE */
3770 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
3772 if ((unsigned)uap->op < UMTX_OP_MAX)
3773 return (*op_table_compat32[uap->op])(td,
3774 (struct _umtx_op_args *)uap);
3780 umtx_thread_init(struct thread *td)
3782 td->td_umtxq = umtxq_alloc();
3783 td->td_umtxq->uq_thread = td;
3787 umtx_thread_fini(struct thread *td)
3789 umtxq_free(td->td_umtxq);
3793 * It will be called when new thread is created, e.g fork().
3796 umtx_thread_alloc(struct thread *td)
3801 uq->uq_inherited_pri = PRI_MAX;
3803 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
3804 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
3805 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
3806 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
3813 umtx_exec_hook(void *arg __unused, struct proc *p __unused,
3814 struct image_params *imgp __unused)
3816 umtx_thread_cleanup(curthread);
3820 * thread_exit() hook.
3823 umtx_thread_exit(struct thread *td)
3825 umtx_thread_cleanup(td);
3829 * clean up umtx data.
3832 umtx_thread_cleanup(struct thread *td)
3837 if ((uq = td->td_umtxq) == NULL)
3840 mtx_lock(&umtx_lock);
3841 uq->uq_inherited_pri = PRI_MAX;
3842 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
3843 pi->pi_owner = NULL;
3844 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
3846 mtx_unlock(&umtx_lock);
3848 sched_lend_user_prio(td, PRI_MAX);