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_SPIN);
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 ret = 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_ower != NULL");
1444 pi->pi_owner = owner;
1445 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1449 * Claim ownership of a PI mutex.
1452 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1454 struct umtx_q *uq, *uq_owner;
1456 uq_owner = owner->td_umtxq;
1457 mtx_lock_spin(&umtx_lock);
1458 if (pi->pi_owner == owner) {
1459 mtx_unlock_spin(&umtx_lock);
1463 if (pi->pi_owner != NULL) {
1465 * userland may have already messed the mutex, sigh.
1467 mtx_unlock_spin(&umtx_lock);
1470 umtx_pi_setowner(pi, owner);
1471 uq = TAILQ_FIRST(&pi->pi_blocked);
1475 pri = UPRI(uq->uq_thread);
1477 if (pri < UPRI(owner))
1478 sched_lend_user_prio(owner, pri);
1479 thread_unlock(owner);
1481 mtx_unlock_spin(&umtx_lock);
1486 * Adjust a thread's order position in its blocked PI mutex,
1487 * this may result new priority propagating process.
1490 umtx_pi_adjust(struct thread *td, u_char oldpri)
1496 mtx_lock_spin(&umtx_lock);
1498 * Pick up the lock that td is blocked on.
1500 pi = uq->uq_pi_blocked;
1502 umtx_pi_adjust_thread(pi, td);
1503 umtx_repropagate_priority(pi);
1505 mtx_unlock_spin(&umtx_lock);
1509 * Sleep on a PI mutex.
1512 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi,
1513 uint32_t owner, const char *wmesg, struct abs_timeout *timo)
1515 struct umtxq_chain *uc;
1516 struct thread *td, *td1;
1522 KASSERT(td == curthread, ("inconsistent uq_thread"));
1523 uc = umtxq_getchain(&uq->uq_key);
1524 UMTXQ_LOCKED_ASSERT(uc);
1525 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1527 mtx_lock_spin(&umtx_lock);
1528 if (pi->pi_owner == NULL) {
1529 mtx_unlock_spin(&umtx_lock);
1530 /* XXX Only look up thread in current process. */
1531 td1 = tdfind(owner, curproc->p_pid);
1532 mtx_lock_spin(&umtx_lock);
1534 if (pi->pi_owner == NULL)
1535 umtx_pi_setowner(pi, td1);
1536 PROC_UNLOCK(td1->td_proc);
1540 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1541 pri = UPRI(uq1->uq_thread);
1547 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1549 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1551 uq->uq_pi_blocked = pi;
1553 td->td_flags |= TDF_UPIBLOCKED;
1555 umtx_propagate_priority(td);
1556 mtx_unlock_spin(&umtx_lock);
1557 umtxq_unbusy(&uq->uq_key);
1559 error = umtxq_sleep(uq, wmesg, timo);
1562 mtx_lock_spin(&umtx_lock);
1563 uq->uq_pi_blocked = NULL;
1565 td->td_flags &= ~TDF_UPIBLOCKED;
1567 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1568 umtx_repropagate_priority(pi);
1569 mtx_unlock_spin(&umtx_lock);
1570 umtxq_unlock(&uq->uq_key);
1576 * Add reference count for a PI mutex.
1579 umtx_pi_ref(struct umtx_pi *pi)
1581 struct umtxq_chain *uc;
1583 uc = umtxq_getchain(&pi->pi_key);
1584 UMTXQ_LOCKED_ASSERT(uc);
1589 * Decrease reference count for a PI mutex, if the counter
1590 * is decreased to zero, its memory space is freed.
1593 umtx_pi_unref(struct umtx_pi *pi)
1595 struct umtxq_chain *uc;
1597 uc = umtxq_getchain(&pi->pi_key);
1598 UMTXQ_LOCKED_ASSERT(uc);
1599 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1600 if (--pi->pi_refcount == 0) {
1601 mtx_lock_spin(&umtx_lock);
1602 if (pi->pi_owner != NULL) {
1603 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested,
1605 pi->pi_owner = NULL;
1607 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1608 ("blocked queue not empty"));
1609 mtx_unlock_spin(&umtx_lock);
1610 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1616 * Find a PI mutex in hash table.
1618 static struct umtx_pi *
1619 umtx_pi_lookup(struct umtx_key *key)
1621 struct umtxq_chain *uc;
1624 uc = umtxq_getchain(key);
1625 UMTXQ_LOCKED_ASSERT(uc);
1627 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1628 if (umtx_key_match(&pi->pi_key, key)) {
1636 * Insert a PI mutex into hash table.
1639 umtx_pi_insert(struct umtx_pi *pi)
1641 struct umtxq_chain *uc;
1643 uc = umtxq_getchain(&pi->pi_key);
1644 UMTXQ_LOCKED_ASSERT(uc);
1645 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1652 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1653 struct _umtx_time *timeout, int try)
1655 struct abs_timeout timo;
1657 struct umtx_pi *pi, *new_pi;
1658 uint32_t id, owner, old;
1664 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1668 if (timeout != NULL)
1669 abs_timeout_init2(&timo, timeout);
1671 umtxq_lock(&uq->uq_key);
1672 pi = umtx_pi_lookup(&uq->uq_key);
1674 new_pi = umtx_pi_alloc(M_NOWAIT);
1675 if (new_pi == NULL) {
1676 umtxq_unlock(&uq->uq_key);
1677 new_pi = umtx_pi_alloc(M_WAITOK);
1678 umtxq_lock(&uq->uq_key);
1679 pi = umtx_pi_lookup(&uq->uq_key);
1681 umtx_pi_free(new_pi);
1685 if (new_pi != NULL) {
1686 new_pi->pi_key = uq->uq_key;
1687 umtx_pi_insert(new_pi);
1692 umtxq_unlock(&uq->uq_key);
1695 * Care must be exercised when dealing with umtx structure. It
1696 * can fault on any access.
1700 * Try the uncontested case. This should be done in userland.
1702 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
1703 /* The address was invalid. */
1709 /* The acquire succeeded. */
1710 if (owner == UMUTEX_UNOWNED) {
1715 /* If no one owns it but it is contested try to acquire it. */
1716 if (owner == UMUTEX_CONTESTED) {
1717 rv = casueword32(&m->m_owner,
1718 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
1719 /* The address was invalid. */
1725 if (owner == UMUTEX_CONTESTED) {
1726 umtxq_lock(&uq->uq_key);
1727 umtxq_busy(&uq->uq_key);
1728 error = umtx_pi_claim(pi, td);
1729 umtxq_unbusy(&uq->uq_key);
1730 umtxq_unlock(&uq->uq_key);
1734 error = umtxq_check_susp(td);
1738 /* If this failed the lock has changed, restart. */
1742 if ((owner & ~UMUTEX_CONTESTED) == id) {
1753 * If we caught a signal, we have retried and now
1759 umtxq_lock(&uq->uq_key);
1760 umtxq_busy(&uq->uq_key);
1761 umtxq_unlock(&uq->uq_key);
1764 * Set the contested bit so that a release in user space
1765 * knows to use the system call for unlock. If this fails
1766 * either some one else has acquired the lock or it has been
1769 rv = casueword32(&m->m_owner, owner, &old,
1770 owner | UMUTEX_CONTESTED);
1772 /* The address was invalid. */
1774 umtxq_unbusy_unlocked(&uq->uq_key);
1779 umtxq_lock(&uq->uq_key);
1781 * We set the contested bit, sleep. Otherwise the lock changed
1782 * and we need to retry or we lost a race to the thread
1783 * unlocking the umtx.
1786 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
1787 "umtxpi", timeout == NULL ? NULL : &timo);
1791 umtxq_unbusy(&uq->uq_key);
1792 umtxq_unlock(&uq->uq_key);
1795 error = umtxq_check_susp(td);
1800 umtxq_lock(&uq->uq_key);
1802 umtxq_unlock(&uq->uq_key);
1804 umtx_key_release(&uq->uq_key);
1809 * Unlock a PI mutex.
1812 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags)
1814 struct umtx_key key;
1815 struct umtx_q *uq_first, *uq_first2, *uq_me;
1816 struct umtx_pi *pi, *pi2;
1817 uint32_t owner, old, id;
1824 * Make sure we own this mtx.
1826 error = fueword32(&m->m_owner, &owner);
1830 if ((owner & ~UMUTEX_CONTESTED) != id)
1833 /* This should be done in userland */
1834 if ((owner & UMUTEX_CONTESTED) == 0) {
1835 error = casueword32(&m->m_owner, owner, &old, UMUTEX_UNOWNED);
1843 /* We should only ever be in here for contested locks */
1844 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1850 count = umtxq_count_pi(&key, &uq_first);
1851 if (uq_first != NULL) {
1852 mtx_lock_spin(&umtx_lock);
1853 pi = uq_first->uq_pi_blocked;
1854 KASSERT(pi != NULL, ("pi == NULL?"));
1855 if (pi->pi_owner != curthread) {
1856 mtx_unlock_spin(&umtx_lock);
1859 umtx_key_release(&key);
1860 /* userland messed the mutex */
1863 uq_me = curthread->td_umtxq;
1864 pi->pi_owner = NULL;
1865 TAILQ_REMOVE(&uq_me->uq_pi_contested, pi, pi_link);
1866 /* get highest priority thread which is still sleeping. */
1867 uq_first = TAILQ_FIRST(&pi->pi_blocked);
1868 while (uq_first != NULL &&
1869 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
1870 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
1873 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
1874 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
1875 if (uq_first2 != NULL) {
1876 if (pri > UPRI(uq_first2->uq_thread))
1877 pri = UPRI(uq_first2->uq_thread);
1880 thread_lock(curthread);
1881 sched_lend_user_prio(curthread, pri);
1882 thread_unlock(curthread);
1883 mtx_unlock_spin(&umtx_lock);
1885 umtxq_signal_thread(uq_first);
1890 * When unlocking the umtx, it must be marked as unowned if
1891 * there is zero or one thread only waiting for it.
1892 * Otherwise, it must be marked as contested.
1894 error = casueword32(&m->m_owner, owner, &old,
1895 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1897 umtxq_unbusy_unlocked(&key);
1898 umtx_key_release(&key);
1910 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
1911 struct _umtx_time *timeout, int try)
1913 struct abs_timeout timo;
1914 struct umtx_q *uq, *uq2;
1918 int error, pri, old_inherited_pri, su, rv;
1922 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
1926 if (timeout != NULL)
1927 abs_timeout_init2(&timo, timeout);
1929 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
1931 old_inherited_pri = uq->uq_inherited_pri;
1932 umtxq_lock(&uq->uq_key);
1933 umtxq_busy(&uq->uq_key);
1934 umtxq_unlock(&uq->uq_key);
1936 rv = fueword32(&m->m_ceilings[0], &ceiling);
1941 ceiling = RTP_PRIO_MAX - ceiling;
1942 if (ceiling > RTP_PRIO_MAX) {
1947 mtx_lock_spin(&umtx_lock);
1948 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
1949 mtx_unlock_spin(&umtx_lock);
1953 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
1954 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
1956 if (uq->uq_inherited_pri < UPRI(td))
1957 sched_lend_user_prio(td, uq->uq_inherited_pri);
1960 mtx_unlock_spin(&umtx_lock);
1962 rv = casueword32(&m->m_owner,
1963 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
1964 /* The address was invalid. */
1970 if (owner == UMUTEX_CONTESTED) {
1981 * If we caught a signal, we have retried and now
1987 umtxq_lock(&uq->uq_key);
1989 umtxq_unbusy(&uq->uq_key);
1990 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
1993 umtxq_unlock(&uq->uq_key);
1995 mtx_lock_spin(&umtx_lock);
1996 uq->uq_inherited_pri = old_inherited_pri;
1998 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
1999 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2001 if (pri > UPRI(uq2->uq_thread))
2002 pri = UPRI(uq2->uq_thread);
2005 if (pri > uq->uq_inherited_pri)
2006 pri = uq->uq_inherited_pri;
2008 sched_lend_user_prio(td, pri);
2010 mtx_unlock_spin(&umtx_lock);
2014 mtx_lock_spin(&umtx_lock);
2015 uq->uq_inherited_pri = old_inherited_pri;
2017 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2018 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2020 if (pri > UPRI(uq2->uq_thread))
2021 pri = UPRI(uq2->uq_thread);
2024 if (pri > uq->uq_inherited_pri)
2025 pri = uq->uq_inherited_pri;
2027 sched_lend_user_prio(td, pri);
2029 mtx_unlock_spin(&umtx_lock);
2033 umtxq_unbusy_unlocked(&uq->uq_key);
2034 umtx_key_release(&uq->uq_key);
2039 * Unlock a PP mutex.
2042 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags)
2044 struct umtx_key key;
2045 struct umtx_q *uq, *uq2;
2049 int error, pri, new_inherited_pri, su;
2053 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2056 * Make sure we own this mtx.
2058 error = fueword32(&m->m_owner, &owner);
2062 if ((owner & ~UMUTEX_CONTESTED) != id)
2065 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2070 new_inherited_pri = PRI_MAX;
2072 rceiling = RTP_PRIO_MAX - rceiling;
2073 if (rceiling > RTP_PRIO_MAX)
2075 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2078 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2085 * For priority protected mutex, always set unlocked state
2086 * to UMUTEX_CONTESTED, so that userland always enters kernel
2087 * to lock the mutex, it is necessary because thread priority
2088 * has to be adjusted for such mutex.
2090 error = suword32(&m->m_owner, UMUTEX_CONTESTED);
2094 umtxq_signal(&key, 1);
2101 mtx_lock_spin(&umtx_lock);
2103 uq->uq_inherited_pri = new_inherited_pri;
2105 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2106 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2108 if (pri > UPRI(uq2->uq_thread))
2109 pri = UPRI(uq2->uq_thread);
2112 if (pri > uq->uq_inherited_pri)
2113 pri = uq->uq_inherited_pri;
2115 sched_lend_user_prio(td, pri);
2117 mtx_unlock_spin(&umtx_lock);
2119 umtx_key_release(&key);
2124 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2125 uint32_t *old_ceiling)
2128 uint32_t save_ceiling;
2133 error = fueword32(&m->m_flags, &flags);
2136 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2138 if (ceiling > RTP_PRIO_MAX)
2142 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2146 umtxq_lock(&uq->uq_key);
2147 umtxq_busy(&uq->uq_key);
2148 umtxq_unlock(&uq->uq_key);
2150 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2156 rv = casueword32(&m->m_owner,
2157 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
2163 if (owner == UMUTEX_CONTESTED) {
2164 suword32(&m->m_ceilings[0], ceiling);
2165 suword32(&m->m_owner, UMUTEX_CONTESTED);
2170 if ((owner & ~UMUTEX_CONTESTED) == id) {
2171 suword32(&m->m_ceilings[0], ceiling);
2177 * If we caught a signal, we have retried and now
2184 * We set the contested bit, sleep. Otherwise the lock changed
2185 * and we need to retry or we lost a race to the thread
2186 * unlocking the umtx.
2188 umtxq_lock(&uq->uq_key);
2190 umtxq_unbusy(&uq->uq_key);
2191 error = umtxq_sleep(uq, "umtxpp", NULL);
2193 umtxq_unlock(&uq->uq_key);
2195 umtxq_lock(&uq->uq_key);
2197 umtxq_signal(&uq->uq_key, INT_MAX);
2198 umtxq_unbusy(&uq->uq_key);
2199 umtxq_unlock(&uq->uq_key);
2200 umtx_key_release(&uq->uq_key);
2201 if (error == 0 && old_ceiling != NULL)
2202 suword32(old_ceiling, save_ceiling);
2207 * Lock a userland POSIX mutex.
2210 do_lock_umutex(struct thread *td, struct umutex *m,
2211 struct _umtx_time *timeout, int mode)
2216 error = fueword32(&m->m_flags, &flags);
2220 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2222 error = do_lock_normal(td, m, flags, timeout, mode);
2224 case UMUTEX_PRIO_INHERIT:
2225 error = do_lock_pi(td, m, flags, timeout, mode);
2227 case UMUTEX_PRIO_PROTECT:
2228 error = do_lock_pp(td, m, flags, timeout, mode);
2233 if (timeout == NULL) {
2234 if (error == EINTR && mode != _UMUTEX_WAIT)
2237 /* Timed-locking is not restarted. */
2238 if (error == ERESTART)
2245 * Unlock a userland POSIX mutex.
2248 do_unlock_umutex(struct thread *td, struct umutex *m)
2253 error = fueword32(&m->m_flags, &flags);
2257 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2259 return (do_unlock_normal(td, m, flags));
2260 case UMUTEX_PRIO_INHERIT:
2261 return (do_unlock_pi(td, m, flags));
2262 case UMUTEX_PRIO_PROTECT:
2263 return (do_unlock_pp(td, m, flags));
2270 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2271 struct timespec *timeout, u_long wflags)
2273 struct abs_timeout timo;
2275 uint32_t flags, clockid, hasw;
2279 error = fueword32(&cv->c_flags, &flags);
2282 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2286 if ((wflags & CVWAIT_CLOCKID) != 0) {
2287 error = fueword32(&cv->c_clockid, &clockid);
2289 umtx_key_release(&uq->uq_key);
2292 if (clockid < CLOCK_REALTIME ||
2293 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2294 /* hmm, only HW clock id will work. */
2295 umtx_key_release(&uq->uq_key);
2299 clockid = CLOCK_REALTIME;
2302 umtxq_lock(&uq->uq_key);
2303 umtxq_busy(&uq->uq_key);
2305 umtxq_unlock(&uq->uq_key);
2308 * Set c_has_waiters to 1 before releasing user mutex, also
2309 * don't modify cache line when unnecessary.
2311 error = fueword32(&cv->c_has_waiters, &hasw);
2312 if (error == 0 && hasw == 0)
2313 suword32(&cv->c_has_waiters, 1);
2315 umtxq_unbusy_unlocked(&uq->uq_key);
2317 error = do_unlock_umutex(td, m);
2319 if (timeout != NULL)
2320 abs_timeout_init(&timo, clockid, ((wflags & CVWAIT_ABSTIME) != 0),
2323 umtxq_lock(&uq->uq_key);
2325 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2329 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2333 * This must be timeout,interrupted by signal or
2334 * surprious wakeup, clear c_has_waiter flag when
2337 umtxq_busy(&uq->uq_key);
2338 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2339 int oldlen = uq->uq_cur_queue->length;
2342 umtxq_unlock(&uq->uq_key);
2343 suword32(&cv->c_has_waiters, 0);
2344 umtxq_lock(&uq->uq_key);
2347 umtxq_unbusy(&uq->uq_key);
2348 if (error == ERESTART)
2352 umtxq_unlock(&uq->uq_key);
2353 umtx_key_release(&uq->uq_key);
2358 * Signal a userland condition variable.
2361 do_cv_signal(struct thread *td, struct ucond *cv)
2363 struct umtx_key key;
2364 int error, cnt, nwake;
2367 error = fueword32(&cv->c_flags, &flags);
2370 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2374 cnt = umtxq_count(&key);
2375 nwake = umtxq_signal(&key, 1);
2378 error = suword32(&cv->c_has_waiters, 0);
2385 umtx_key_release(&key);
2390 do_cv_broadcast(struct thread *td, struct ucond *cv)
2392 struct umtx_key key;
2396 error = fueword32(&cv->c_flags, &flags);
2399 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2404 umtxq_signal(&key, INT_MAX);
2407 error = suword32(&cv->c_has_waiters, 0);
2411 umtxq_unbusy_unlocked(&key);
2413 umtx_key_release(&key);
2418 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, struct _umtx_time *timeout)
2420 struct abs_timeout timo;
2422 uint32_t flags, wrflags;
2423 int32_t state, oldstate;
2424 int32_t blocked_readers;
2428 error = fueword32(&rwlock->rw_flags, &flags);
2431 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2435 if (timeout != NULL)
2436 abs_timeout_init2(&timo, timeout);
2438 wrflags = URWLOCK_WRITE_OWNER;
2439 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2440 wrflags |= URWLOCK_WRITE_WAITERS;
2443 rv = fueword32(&rwlock->rw_state, &state);
2445 umtx_key_release(&uq->uq_key);
2449 /* try to lock it */
2450 while (!(state & wrflags)) {
2451 if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) {
2452 umtx_key_release(&uq->uq_key);
2455 rv = casueword32(&rwlock->rw_state, state,
2456 &oldstate, state + 1);
2458 umtx_key_release(&uq->uq_key);
2461 if (oldstate == state) {
2462 umtx_key_release(&uq->uq_key);
2465 error = umtxq_check_susp(td);
2474 /* grab monitor lock */
2475 umtxq_lock(&uq->uq_key);
2476 umtxq_busy(&uq->uq_key);
2477 umtxq_unlock(&uq->uq_key);
2480 * re-read the state, in case it changed between the try-lock above
2481 * and the check below
2483 rv = fueword32(&rwlock->rw_state, &state);
2487 /* set read contention bit */
2488 while (error == 0 && (state & wrflags) &&
2489 !(state & URWLOCK_READ_WAITERS)) {
2490 rv = casueword32(&rwlock->rw_state, state,
2491 &oldstate, state | URWLOCK_READ_WAITERS);
2496 if (oldstate == state)
2499 error = umtxq_check_susp(td);
2504 umtxq_unbusy_unlocked(&uq->uq_key);
2508 /* state is changed while setting flags, restart */
2509 if (!(state & wrflags)) {
2510 umtxq_unbusy_unlocked(&uq->uq_key);
2511 error = umtxq_check_susp(td);
2518 /* contention bit is set, before sleeping, increase read waiter count */
2519 rv = fueword32(&rwlock->rw_blocked_readers,
2522 umtxq_unbusy_unlocked(&uq->uq_key);
2526 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2528 while (state & wrflags) {
2529 umtxq_lock(&uq->uq_key);
2531 umtxq_unbusy(&uq->uq_key);
2533 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2536 umtxq_busy(&uq->uq_key);
2538 umtxq_unlock(&uq->uq_key);
2541 rv = fueword32(&rwlock->rw_state, &state);
2548 /* decrease read waiter count, and may clear read contention bit */
2549 rv = fueword32(&rwlock->rw_blocked_readers,
2552 umtxq_unbusy_unlocked(&uq->uq_key);
2556 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2557 if (blocked_readers == 1) {
2558 rv = fueword32(&rwlock->rw_state, &state);
2561 while (error == 0) {
2562 rv = casueword32(&rwlock->rw_state, state,
2563 &oldstate, state & ~URWLOCK_READ_WAITERS);
2568 if (oldstate == state)
2571 error = umtxq_check_susp(td);
2575 umtxq_unbusy_unlocked(&uq->uq_key);
2579 umtx_key_release(&uq->uq_key);
2580 if (error == ERESTART)
2586 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2588 struct abs_timeout timo;
2591 int32_t state, oldstate;
2592 int32_t blocked_writers;
2593 int32_t blocked_readers;
2597 error = fueword32(&rwlock->rw_flags, &flags);
2600 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2604 if (timeout != NULL)
2605 abs_timeout_init2(&timo, timeout);
2607 blocked_readers = 0;
2609 rv = fueword32(&rwlock->rw_state, &state);
2611 umtx_key_release(&uq->uq_key);
2614 while (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2615 rv = casueword32(&rwlock->rw_state, state,
2616 &oldstate, state | URWLOCK_WRITE_OWNER);
2618 umtx_key_release(&uq->uq_key);
2621 if (oldstate == state) {
2622 umtx_key_release(&uq->uq_key);
2626 error = umtxq_check_susp(td);
2632 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) &&
2633 blocked_readers != 0) {
2634 umtxq_lock(&uq->uq_key);
2635 umtxq_busy(&uq->uq_key);
2636 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE);
2637 umtxq_unbusy(&uq->uq_key);
2638 umtxq_unlock(&uq->uq_key);
2644 /* grab monitor lock */
2645 umtxq_lock(&uq->uq_key);
2646 umtxq_busy(&uq->uq_key);
2647 umtxq_unlock(&uq->uq_key);
2650 * re-read the state, in case it changed between the try-lock above
2651 * and the check below
2653 rv = fueword32(&rwlock->rw_state, &state);
2657 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
2658 URWLOCK_READER_COUNT(state) != 0) &&
2659 (state & URWLOCK_WRITE_WAITERS) == 0) {
2660 rv = casueword32(&rwlock->rw_state, state,
2661 &oldstate, state | URWLOCK_WRITE_WAITERS);
2666 if (oldstate == state)
2669 error = umtxq_check_susp(td);
2674 umtxq_unbusy_unlocked(&uq->uq_key);
2678 if (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2679 umtxq_unbusy_unlocked(&uq->uq_key);
2680 error = umtxq_check_susp(td);
2686 rv = fueword32(&rwlock->rw_blocked_writers,
2689 umtxq_unbusy_unlocked(&uq->uq_key);
2693 suword32(&rwlock->rw_blocked_writers, blocked_writers+1);
2695 while ((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) {
2696 umtxq_lock(&uq->uq_key);
2697 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2698 umtxq_unbusy(&uq->uq_key);
2700 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
2703 umtxq_busy(&uq->uq_key);
2704 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2705 umtxq_unlock(&uq->uq_key);
2708 rv = fueword32(&rwlock->rw_state, &state);
2715 rv = fueword32(&rwlock->rw_blocked_writers,
2718 umtxq_unbusy_unlocked(&uq->uq_key);
2722 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
2723 if (blocked_writers == 1) {
2724 rv = fueword32(&rwlock->rw_state, &state);
2726 umtxq_unbusy_unlocked(&uq->uq_key);
2731 rv = casueword32(&rwlock->rw_state, state,
2732 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
2737 if (oldstate == state)
2740 error = umtxq_check_susp(td);
2742 * We are leaving the URWLOCK_WRITE_WAITERS
2743 * behind, but this should not harm the
2749 rv = fueword32(&rwlock->rw_blocked_readers,
2752 umtxq_unbusy_unlocked(&uq->uq_key);
2757 blocked_readers = 0;
2759 umtxq_unbusy_unlocked(&uq->uq_key);
2762 umtx_key_release(&uq->uq_key);
2763 if (error == ERESTART)
2769 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
2773 int32_t state, oldstate;
2774 int error, rv, q, count;
2777 error = fueword32(&rwlock->rw_flags, &flags);
2780 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2784 error = fueword32(&rwlock->rw_state, &state);
2789 if (state & URWLOCK_WRITE_OWNER) {
2791 rv = casueword32(&rwlock->rw_state, state,
2792 &oldstate, state & ~URWLOCK_WRITE_OWNER);
2797 if (oldstate != state) {
2799 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
2803 error = umtxq_check_susp(td);
2809 } else if (URWLOCK_READER_COUNT(state) != 0) {
2811 rv = casueword32(&rwlock->rw_state, state,
2812 &oldstate, state - 1);
2817 if (oldstate != state) {
2819 if (URWLOCK_READER_COUNT(oldstate) == 0) {
2823 error = umtxq_check_susp(td);
2836 if (!(flags & URWLOCK_PREFER_READER)) {
2837 if (state & URWLOCK_WRITE_WAITERS) {
2839 q = UMTX_EXCLUSIVE_QUEUE;
2840 } else if (state & URWLOCK_READ_WAITERS) {
2842 q = UMTX_SHARED_QUEUE;
2845 if (state & URWLOCK_READ_WAITERS) {
2847 q = UMTX_SHARED_QUEUE;
2848 } else if (state & URWLOCK_WRITE_WAITERS) {
2850 q = UMTX_EXCLUSIVE_QUEUE;
2855 umtxq_lock(&uq->uq_key);
2856 umtxq_busy(&uq->uq_key);
2857 umtxq_signal_queue(&uq->uq_key, count, q);
2858 umtxq_unbusy(&uq->uq_key);
2859 umtxq_unlock(&uq->uq_key);
2862 umtx_key_release(&uq->uq_key);
2866 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
2868 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
2870 struct abs_timeout timo;
2872 uint32_t flags, count, count1;
2876 error = fueword32(&sem->_flags, &flags);
2879 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
2883 if (timeout != NULL)
2884 abs_timeout_init2(&timo, timeout);
2886 umtxq_lock(&uq->uq_key);
2887 umtxq_busy(&uq->uq_key);
2889 umtxq_unlock(&uq->uq_key);
2890 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
2892 rv = fueword32(&sem->_count, &count);
2893 if (rv == -1 || count != 0) {
2894 umtxq_lock(&uq->uq_key);
2895 umtxq_unbusy(&uq->uq_key);
2897 umtxq_unlock(&uq->uq_key);
2898 umtx_key_release(&uq->uq_key);
2899 return (rv == -1 ? EFAULT : 0);
2901 umtxq_lock(&uq->uq_key);
2902 umtxq_unbusy(&uq->uq_key);
2904 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
2906 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2910 /* A relative timeout cannot be restarted. */
2911 if (error == ERESTART && timeout != NULL &&
2912 (timeout->_flags & UMTX_ABSTIME) == 0)
2915 umtxq_unlock(&uq->uq_key);
2916 umtx_key_release(&uq->uq_key);
2921 * Signal a userland semaphore.
2924 do_sem_wake(struct thread *td, struct _usem *sem)
2926 struct umtx_key key;
2930 error = fueword32(&sem->_flags, &flags);
2933 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
2937 cnt = umtxq_count(&key);
2939 umtxq_signal(&key, 1);
2941 * Check if count is greater than 0, this means the memory is
2942 * still being referenced by user code, so we can safely
2943 * update _has_waiters flag.
2947 error = suword32(&sem->_has_waiters, 0);
2955 umtx_key_release(&key);
2961 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
2963 struct abs_timeout timo;
2965 uint32_t count, flags;
2969 flags = fuword32(&sem->_flags);
2970 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
2974 if (timeout != NULL)
2975 abs_timeout_init2(&timo, timeout);
2977 umtxq_lock(&uq->uq_key);
2978 umtxq_busy(&uq->uq_key);
2980 umtxq_unlock(&uq->uq_key);
2981 rv = fueword32(&sem->_count, &count);
2983 umtxq_lock(&uq->uq_key);
2984 umtxq_unbusy(&uq->uq_key);
2986 umtxq_unlock(&uq->uq_key);
2987 umtx_key_release(&uq->uq_key);
2991 if (USEM_COUNT(count) != 0) {
2992 umtxq_lock(&uq->uq_key);
2993 umtxq_unbusy(&uq->uq_key);
2995 umtxq_unlock(&uq->uq_key);
2996 umtx_key_release(&uq->uq_key);
2999 if (count == USEM_HAS_WAITERS)
3001 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3003 umtxq_lock(&uq->uq_key);
3004 umtxq_unbusy(&uq->uq_key);
3006 umtxq_unlock(&uq->uq_key);
3007 umtx_key_release(&uq->uq_key);
3013 umtxq_lock(&uq->uq_key);
3014 umtxq_unbusy(&uq->uq_key);
3016 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3018 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3022 /* A relative timeout cannot be restarted. */
3023 if (error == ERESTART && timeout != NULL &&
3024 (timeout->_flags & UMTX_ABSTIME) == 0)
3027 umtxq_unlock(&uq->uq_key);
3028 umtx_key_release(&uq->uq_key);
3033 * Signal a userland semaphore.
3036 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3038 struct umtx_key key;
3040 uint32_t count, flags;
3042 rv = fueword32(&sem->_flags, &flags);
3045 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3049 cnt = umtxq_count(&key);
3051 umtxq_signal(&key, 1);
3054 * If this was the last sleeping thread, clear the waiters
3059 rv = fueword32(&sem->_count, &count);
3060 while (rv != -1 && count & USEM_HAS_WAITERS)
3061 rv = casueword32(&sem->_count, count, &count,
3062 count & ~USEM_HAS_WAITERS);
3070 umtx_key_release(&key);
3075 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3079 error = copyin(addr, tsp, sizeof(struct timespec));
3081 if (tsp->tv_sec < 0 ||
3082 tsp->tv_nsec >= 1000000000 ||
3090 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3094 if (size <= sizeof(struct timespec)) {
3095 tp->_clockid = CLOCK_REALTIME;
3097 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3099 error = copyin(addr, tp, sizeof(struct _umtx_time));
3102 if (tp->_timeout.tv_sec < 0 ||
3103 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3109 __umtx_op_unimpl(struct thread *td, struct _umtx_op_args *uap)
3112 return (EOPNOTSUPP);
3116 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3118 struct _umtx_time timeout, *tm_p;
3121 if (uap->uaddr2 == NULL)
3124 error = umtx_copyin_umtx_time(
3125 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3130 return do_wait(td, uap->obj, uap->val, tm_p, 0, 0);
3134 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3136 struct _umtx_time timeout, *tm_p;
3139 if (uap->uaddr2 == NULL)
3142 error = umtx_copyin_umtx_time(
3143 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3148 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3152 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3154 struct _umtx_time *tm_p, timeout;
3157 if (uap->uaddr2 == NULL)
3160 error = umtx_copyin_umtx_time(
3161 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3166 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3170 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3172 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3175 #define BATCH_SIZE 128
3177 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3179 int count = uap->val;
3180 void *uaddrs[BATCH_SIZE];
3181 char **upp = (char **)uap->obj;
3188 if (tocopy > BATCH_SIZE)
3189 tocopy = BATCH_SIZE;
3190 error = copyin(upp+pos, uaddrs, tocopy * sizeof(char *));
3193 for (i = 0; i < tocopy; ++i)
3194 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3202 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3204 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3208 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3210 struct _umtx_time *tm_p, timeout;
3213 /* Allow a null timespec (wait forever). */
3214 if (uap->uaddr2 == NULL)
3217 error = umtx_copyin_umtx_time(
3218 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3223 return do_lock_umutex(td, uap->obj, tm_p, 0);
3227 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3229 return do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY);
3233 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3235 struct _umtx_time *tm_p, timeout;
3238 /* Allow a null timespec (wait forever). */
3239 if (uap->uaddr2 == NULL)
3242 error = umtx_copyin_umtx_time(
3243 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3248 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3252 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3254 return do_wake_umutex(td, uap->obj);
3258 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3260 return do_unlock_umutex(td, uap->obj);
3264 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3266 return do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1);
3270 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3272 struct timespec *ts, timeout;
3275 /* Allow a null timespec (wait forever). */
3276 if (uap->uaddr2 == NULL)
3279 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3284 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3288 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3290 return do_cv_signal(td, uap->obj);
3294 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3296 return do_cv_broadcast(td, uap->obj);
3300 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3302 struct _umtx_time timeout;
3305 /* Allow a null timespec (wait forever). */
3306 if (uap->uaddr2 == NULL) {
3307 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3309 error = umtx_copyin_umtx_time(uap->uaddr2,
3310 (size_t)uap->uaddr1, &timeout);
3313 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3319 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3321 struct _umtx_time timeout;
3324 /* Allow a null timespec (wait forever). */
3325 if (uap->uaddr2 == NULL) {
3326 error = do_rw_wrlock(td, uap->obj, 0);
3328 error = umtx_copyin_umtx_time(uap->uaddr2,
3329 (size_t)uap->uaddr1, &timeout);
3333 error = do_rw_wrlock(td, uap->obj, &timeout);
3339 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3341 return do_rw_unlock(td, uap->obj);
3344 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3346 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3348 struct _umtx_time *tm_p, timeout;
3351 /* Allow a null timespec (wait forever). */
3352 if (uap->uaddr2 == NULL)
3355 error = umtx_copyin_umtx_time(
3356 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3361 return (do_sem_wait(td, uap->obj, tm_p));
3365 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3367 return do_sem_wake(td, uap->obj);
3372 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3374 return do_wake2_umutex(td, uap->obj, uap->val);
3378 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap)
3380 struct _umtx_time *tm_p, timeout;
3383 /* Allow a null timespec (wait forever). */
3384 if (uap->uaddr2 == NULL)
3387 error = umtx_copyin_umtx_time(
3388 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3393 return (do_sem2_wait(td, uap->obj, tm_p));
3397 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap)
3399 return do_sem2_wake(td, uap->obj);
3402 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
3404 static _umtx_op_func op_table[] = {
3405 __umtx_op_unimpl, /* UMTX_OP_RESERVED0 */
3406 __umtx_op_unimpl, /* UMTX_OP_RESERVED1 */
3407 __umtx_op_wait, /* UMTX_OP_WAIT */
3408 __umtx_op_wake, /* UMTX_OP_WAKE */
3409 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_TRYLOCK */
3410 __umtx_op_lock_umutex, /* UMTX_OP_MUTEX_LOCK */
3411 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3412 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3413 __umtx_op_cv_wait, /* UMTX_OP_CV_WAIT*/
3414 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3415 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3416 __umtx_op_wait_uint, /* UMTX_OP_WAIT_UINT */
3417 __umtx_op_rw_rdlock, /* UMTX_OP_RW_RDLOCK */
3418 __umtx_op_rw_wrlock, /* UMTX_OP_RW_WRLOCK */
3419 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3420 __umtx_op_wait_uint_private, /* UMTX_OP_WAIT_UINT_PRIVATE */
3421 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3422 __umtx_op_wait_umutex, /* UMTX_OP_MUTEX_WAIT */
3423 __umtx_op_wake_umutex, /* UMTX_OP_MUTEX_WAKE */
3424 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3425 __umtx_op_sem_wait, /* UMTX_OP_SEM_WAIT */
3426 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3428 __umtx_op_unimpl, /* UMTX_OP_SEM_WAIT */
3429 __umtx_op_unimpl, /* UMTX_OP_SEM_WAKE */
3431 __umtx_op_nwake_private, /* UMTX_OP_NWAKE_PRIVATE */
3432 __umtx_op_wake2_umutex, /* UMTX_OP_MUTEX_WAKE2 */
3433 __umtx_op_sem2_wait, /* UMTX_OP_SEM2_WAIT */
3434 __umtx_op_sem2_wake, /* UMTX_OP_SEM2_WAKE */
3438 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
3440 if ((unsigned)uap->op < UMTX_OP_MAX)
3441 return (*op_table[uap->op])(td, uap);
3445 #ifdef COMPAT_FREEBSD32
3452 struct umtx_time32 {
3453 struct timespec32 timeout;
3459 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
3461 struct timespec32 ts32;
3464 error = copyin(addr, &ts32, sizeof(struct timespec32));
3466 if (ts32.tv_sec < 0 ||
3467 ts32.tv_nsec >= 1000000000 ||
3471 tsp->tv_sec = ts32.tv_sec;
3472 tsp->tv_nsec = ts32.tv_nsec;
3479 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
3481 struct umtx_time32 t32;
3484 t32.clockid = CLOCK_REALTIME;
3486 if (size <= sizeof(struct timespec32))
3487 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
3489 error = copyin(addr, &t32, sizeof(struct umtx_time32));
3492 if (t32.timeout.tv_sec < 0 ||
3493 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
3495 tp->_timeout.tv_sec = t32.timeout.tv_sec;
3496 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
3497 tp->_flags = t32.flags;
3498 tp->_clockid = t32.clockid;
3503 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3505 struct _umtx_time *tm_p, timeout;
3508 if (uap->uaddr2 == NULL)
3511 error = umtx_copyin_umtx_time32(uap->uaddr2,
3512 (size_t)uap->uaddr1, &timeout);
3517 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3521 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3523 struct _umtx_time *tm_p, timeout;
3526 /* Allow a null timespec (wait forever). */
3527 if (uap->uaddr2 == NULL)
3530 error = umtx_copyin_umtx_time(uap->uaddr2,
3531 (size_t)uap->uaddr1, &timeout);
3536 return do_lock_umutex(td, uap->obj, tm_p, 0);
3540 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3542 struct _umtx_time *tm_p, timeout;
3545 /* Allow a null timespec (wait forever). */
3546 if (uap->uaddr2 == NULL)
3549 error = umtx_copyin_umtx_time32(uap->uaddr2,
3550 (size_t)uap->uaddr1, &timeout);
3555 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3559 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3561 struct timespec *ts, timeout;
3564 /* Allow a null timespec (wait forever). */
3565 if (uap->uaddr2 == NULL)
3568 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3573 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3577 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3579 struct _umtx_time timeout;
3582 /* Allow a null timespec (wait forever). */
3583 if (uap->uaddr2 == NULL) {
3584 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3586 error = umtx_copyin_umtx_time32(uap->uaddr2,
3587 (size_t)uap->uaddr1, &timeout);
3590 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3596 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3598 struct _umtx_time timeout;
3601 /* Allow a null timespec (wait forever). */
3602 if (uap->uaddr2 == NULL) {
3603 error = do_rw_wrlock(td, uap->obj, 0);
3605 error = umtx_copyin_umtx_time32(uap->uaddr2,
3606 (size_t)uap->uaddr1, &timeout);
3609 error = do_rw_wrlock(td, uap->obj, &timeout);
3615 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3617 struct _umtx_time *tm_p, timeout;
3620 if (uap->uaddr2 == NULL)
3623 error = umtx_copyin_umtx_time32(
3624 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
3629 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3632 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3634 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3636 struct _umtx_time *tm_p, timeout;
3639 /* Allow a null timespec (wait forever). */
3640 if (uap->uaddr2 == NULL)
3643 error = umtx_copyin_umtx_time32(uap->uaddr2,
3644 (size_t)uap->uaddr1, &timeout);
3649 return (do_sem_wait(td, uap->obj, tm_p));
3654 __umtx_op_sem2_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3656 struct _umtx_time *tm_p, timeout;
3659 /* Allow a null timespec (wait forever). */
3660 if (uap->uaddr2 == NULL)
3663 error = umtx_copyin_umtx_time32(uap->uaddr2,
3664 (size_t)uap->uaddr1, &timeout);
3669 return (do_sem2_wait(td, uap->obj, tm_p));
3673 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
3675 int count = uap->val;
3676 uint32_t uaddrs[BATCH_SIZE];
3677 uint32_t **upp = (uint32_t **)uap->obj;
3684 if (tocopy > BATCH_SIZE)
3685 tocopy = BATCH_SIZE;
3686 error = copyin(upp+pos, uaddrs, tocopy * sizeof(uint32_t));
3689 for (i = 0; i < tocopy; ++i)
3690 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
3698 static _umtx_op_func op_table_compat32[] = {
3699 __umtx_op_unimpl, /* UMTX_OP_RESERVED0 */
3700 __umtx_op_unimpl, /* UMTX_OP_RESERVED1 */
3701 __umtx_op_wait_compat32, /* UMTX_OP_WAIT */
3702 __umtx_op_wake, /* UMTX_OP_WAKE */
3703 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_LOCK */
3704 __umtx_op_lock_umutex_compat32, /* UMTX_OP_MUTEX_TRYLOCK */
3705 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3706 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3707 __umtx_op_cv_wait_compat32, /* UMTX_OP_CV_WAIT*/
3708 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3709 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3710 __umtx_op_wait_compat32, /* UMTX_OP_WAIT_UINT */
3711 __umtx_op_rw_rdlock_compat32, /* UMTX_OP_RW_RDLOCK */
3712 __umtx_op_rw_wrlock_compat32, /* UMTX_OP_RW_WRLOCK */
3713 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3714 __umtx_op_wait_uint_private_compat32, /* UMTX_OP_WAIT_UINT_PRIVATE */
3715 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3716 __umtx_op_wait_umutex_compat32, /* UMTX_OP_MUTEX_WAIT */
3717 __umtx_op_wake_umutex, /* UMTX_OP_MUTEX_WAKE */
3718 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3719 __umtx_op_sem_wait_compat32, /* UMTX_OP_SEM_WAIT */
3720 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3722 __umtx_op_unimpl, /* UMTX_OP_SEM_WAIT */
3723 __umtx_op_unimpl, /* UMTX_OP_SEM_WAKE */
3725 __umtx_op_nwake_private32, /* UMTX_OP_NWAKE_PRIVATE */
3726 __umtx_op_wake2_umutex, /* UMTX_OP_MUTEX_WAKE2 */
3727 __umtx_op_sem2_wait_compat32, /* UMTX_OP_SEM2_WAIT */
3728 __umtx_op_sem2_wake, /* UMTX_OP_SEM2_WAKE */
3732 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
3734 if ((unsigned)uap->op < UMTX_OP_MAX)
3735 return (*op_table_compat32[uap->op])(td,
3736 (struct _umtx_op_args *)uap);
3742 umtx_thread_init(struct thread *td)
3744 td->td_umtxq = umtxq_alloc();
3745 td->td_umtxq->uq_thread = td;
3749 umtx_thread_fini(struct thread *td)
3751 umtxq_free(td->td_umtxq);
3755 * It will be called when new thread is created, e.g fork().
3758 umtx_thread_alloc(struct thread *td)
3763 uq->uq_inherited_pri = PRI_MAX;
3765 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
3766 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
3767 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
3768 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
3775 umtx_exec_hook(void *arg __unused, struct proc *p __unused,
3776 struct image_params *imgp __unused)
3778 umtx_thread_cleanup(curthread);
3782 * thread_exit() hook.
3785 umtx_thread_exit(struct thread *td)
3787 umtx_thread_cleanup(td);
3791 * clean up umtx data.
3794 umtx_thread_cleanup(struct thread *td)
3799 if ((uq = td->td_umtxq) == NULL)
3802 mtx_lock_spin(&umtx_lock);
3803 uq->uq_inherited_pri = PRI_MAX;
3804 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
3805 pi->pi_owner = NULL;
3806 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
3808 mtx_unlock_spin(&umtx_lock);
3810 sched_lend_user_prio(td, PRI_MAX);