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);
1306 * Propagate priority when a thread is blocked on POSIX
1310 umtx_propagate_priority(struct thread *td)
1316 mtx_assert(&umtx_lock, MA_OWNED);
1319 pi = uq->uq_pi_blocked;
1325 if (td == NULL || td == curthread)
1328 MPASS(td->td_proc != NULL);
1329 MPASS(td->td_proc->p_magic == P_MAGIC);
1332 if (td->td_lend_user_pri > pri)
1333 sched_lend_user_prio(td, pri);
1341 * Pick up the lock that td is blocked on.
1344 pi = uq->uq_pi_blocked;
1347 /* Resort td on the list if needed. */
1348 umtx_pi_adjust_thread(pi, td);
1353 * Unpropagate priority for a PI mutex when a thread blocked on
1354 * it is interrupted by signal or resumed by others.
1357 umtx_repropagate_priority(struct umtx_pi *pi)
1359 struct umtx_q *uq, *uq_owner;
1360 struct umtx_pi *pi2;
1363 mtx_assert(&umtx_lock, MA_OWNED);
1365 while (pi != NULL && pi->pi_owner != NULL) {
1367 uq_owner = pi->pi_owner->td_umtxq;
1369 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1370 uq = TAILQ_FIRST(&pi2->pi_blocked);
1372 if (pri > UPRI(uq->uq_thread))
1373 pri = UPRI(uq->uq_thread);
1377 if (pri > uq_owner->uq_inherited_pri)
1378 pri = uq_owner->uq_inherited_pri;
1379 thread_lock(pi->pi_owner);
1380 sched_lend_user_prio(pi->pi_owner, pri);
1381 thread_unlock(pi->pi_owner);
1382 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1383 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1388 * Insert a PI mutex into owned list.
1391 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1393 struct umtx_q *uq_owner;
1395 uq_owner = owner->td_umtxq;
1396 mtx_assert(&umtx_lock, MA_OWNED);
1397 if (pi->pi_owner != NULL)
1398 panic("pi_ower != NULL");
1399 pi->pi_owner = owner;
1400 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1404 * Claim ownership of a PI mutex.
1407 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1409 struct umtx_q *uq, *uq_owner;
1411 uq_owner = owner->td_umtxq;
1412 mtx_lock_spin(&umtx_lock);
1413 if (pi->pi_owner == owner) {
1414 mtx_unlock_spin(&umtx_lock);
1418 if (pi->pi_owner != NULL) {
1420 * userland may have already messed the mutex, sigh.
1422 mtx_unlock_spin(&umtx_lock);
1425 umtx_pi_setowner(pi, owner);
1426 uq = TAILQ_FIRST(&pi->pi_blocked);
1430 pri = UPRI(uq->uq_thread);
1432 if (pri < UPRI(owner))
1433 sched_lend_user_prio(owner, pri);
1434 thread_unlock(owner);
1436 mtx_unlock_spin(&umtx_lock);
1441 * Adjust a thread's order position in its blocked PI mutex,
1442 * this may result new priority propagating process.
1445 umtx_pi_adjust(struct thread *td, u_char oldpri)
1451 mtx_lock_spin(&umtx_lock);
1453 * Pick up the lock that td is blocked on.
1455 pi = uq->uq_pi_blocked;
1457 umtx_pi_adjust_thread(pi, td);
1458 umtx_repropagate_priority(pi);
1460 mtx_unlock_spin(&umtx_lock);
1464 * Sleep on a PI mutex.
1467 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi,
1468 uint32_t owner, const char *wmesg, struct abs_timeout *timo)
1470 struct umtxq_chain *uc;
1471 struct thread *td, *td1;
1477 KASSERT(td == curthread, ("inconsistent uq_thread"));
1478 uc = umtxq_getchain(&uq->uq_key);
1479 UMTXQ_LOCKED_ASSERT(uc);
1480 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1482 mtx_lock_spin(&umtx_lock);
1483 if (pi->pi_owner == NULL) {
1484 mtx_unlock_spin(&umtx_lock);
1485 /* XXX Only look up thread in current process. */
1486 td1 = tdfind(owner, curproc->p_pid);
1487 mtx_lock_spin(&umtx_lock);
1489 if (pi->pi_owner == NULL)
1490 umtx_pi_setowner(pi, td1);
1491 PROC_UNLOCK(td1->td_proc);
1495 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1496 pri = UPRI(uq1->uq_thread);
1502 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1504 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1506 uq->uq_pi_blocked = pi;
1508 td->td_flags |= TDF_UPIBLOCKED;
1510 umtx_propagate_priority(td);
1511 mtx_unlock_spin(&umtx_lock);
1512 umtxq_unbusy(&uq->uq_key);
1514 error = umtxq_sleep(uq, wmesg, timo);
1517 mtx_lock_spin(&umtx_lock);
1518 uq->uq_pi_blocked = NULL;
1520 td->td_flags &= ~TDF_UPIBLOCKED;
1522 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1523 umtx_repropagate_priority(pi);
1524 mtx_unlock_spin(&umtx_lock);
1525 umtxq_unlock(&uq->uq_key);
1531 * Add reference count for a PI mutex.
1534 umtx_pi_ref(struct umtx_pi *pi)
1536 struct umtxq_chain *uc;
1538 uc = umtxq_getchain(&pi->pi_key);
1539 UMTXQ_LOCKED_ASSERT(uc);
1544 * Decrease reference count for a PI mutex, if the counter
1545 * is decreased to zero, its memory space is freed.
1548 umtx_pi_unref(struct umtx_pi *pi)
1550 struct umtxq_chain *uc;
1552 uc = umtxq_getchain(&pi->pi_key);
1553 UMTXQ_LOCKED_ASSERT(uc);
1554 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1555 if (--pi->pi_refcount == 0) {
1556 mtx_lock_spin(&umtx_lock);
1557 if (pi->pi_owner != NULL) {
1558 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested,
1560 pi->pi_owner = NULL;
1562 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1563 ("blocked queue not empty"));
1564 mtx_unlock_spin(&umtx_lock);
1565 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1571 * Find a PI mutex in hash table.
1573 static struct umtx_pi *
1574 umtx_pi_lookup(struct umtx_key *key)
1576 struct umtxq_chain *uc;
1579 uc = umtxq_getchain(key);
1580 UMTXQ_LOCKED_ASSERT(uc);
1582 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1583 if (umtx_key_match(&pi->pi_key, key)) {
1591 * Insert a PI mutex into hash table.
1594 umtx_pi_insert(struct umtx_pi *pi)
1596 struct umtxq_chain *uc;
1598 uc = umtxq_getchain(&pi->pi_key);
1599 UMTXQ_LOCKED_ASSERT(uc);
1600 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1607 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1608 struct _umtx_time *timeout, int try)
1610 struct abs_timeout timo;
1612 struct umtx_pi *pi, *new_pi;
1613 uint32_t id, owner, old;
1619 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1623 if (timeout != NULL)
1624 abs_timeout_init2(&timo, timeout);
1626 umtxq_lock(&uq->uq_key);
1627 pi = umtx_pi_lookup(&uq->uq_key);
1629 new_pi = umtx_pi_alloc(M_NOWAIT);
1630 if (new_pi == NULL) {
1631 umtxq_unlock(&uq->uq_key);
1632 new_pi = umtx_pi_alloc(M_WAITOK);
1633 umtxq_lock(&uq->uq_key);
1634 pi = umtx_pi_lookup(&uq->uq_key);
1636 umtx_pi_free(new_pi);
1640 if (new_pi != NULL) {
1641 new_pi->pi_key = uq->uq_key;
1642 umtx_pi_insert(new_pi);
1647 umtxq_unlock(&uq->uq_key);
1650 * Care must be exercised when dealing with umtx structure. It
1651 * can fault on any access.
1655 * Try the uncontested case. This should be done in userland.
1657 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
1658 /* The address was invalid. */
1664 /* The acquire succeeded. */
1665 if (owner == UMUTEX_UNOWNED) {
1670 /* If no one owns it but it is contested try to acquire it. */
1671 if (owner == UMUTEX_CONTESTED) {
1672 rv = casueword32(&m->m_owner,
1673 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
1674 /* The address was invalid. */
1680 if (owner == UMUTEX_CONTESTED) {
1681 umtxq_lock(&uq->uq_key);
1682 umtxq_busy(&uq->uq_key);
1683 error = umtx_pi_claim(pi, td);
1684 umtxq_unbusy(&uq->uq_key);
1685 umtxq_unlock(&uq->uq_key);
1689 error = umtxq_check_susp(td);
1693 /* If this failed the lock has changed, restart. */
1703 * If we caught a signal, we have retried and now
1709 umtxq_lock(&uq->uq_key);
1710 umtxq_busy(&uq->uq_key);
1711 umtxq_unlock(&uq->uq_key);
1714 * Set the contested bit so that a release in user space
1715 * knows to use the system call for unlock. If this fails
1716 * either some one else has acquired the lock or it has been
1719 rv = casueword32(&m->m_owner, owner, &old,
1720 owner | UMUTEX_CONTESTED);
1722 /* The address was invalid. */
1724 umtxq_unbusy_unlocked(&uq->uq_key);
1729 umtxq_lock(&uq->uq_key);
1731 * We set the contested bit, sleep. Otherwise the lock changed
1732 * and we need to retry or we lost a race to the thread
1733 * unlocking the umtx.
1736 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
1737 "umtxpi", timeout == NULL ? NULL : &timo);
1741 umtxq_unbusy(&uq->uq_key);
1742 umtxq_unlock(&uq->uq_key);
1745 error = umtxq_check_susp(td);
1750 umtxq_lock(&uq->uq_key);
1752 umtxq_unlock(&uq->uq_key);
1754 umtx_key_release(&uq->uq_key);
1759 * Unlock a PI mutex.
1762 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags)
1764 struct umtx_key key;
1765 struct umtx_q *uq_first, *uq_first2, *uq_me;
1766 struct umtx_pi *pi, *pi2;
1767 uint32_t owner, old, id;
1774 * Make sure we own this mtx.
1776 error = fueword32(&m->m_owner, &owner);
1780 if ((owner & ~UMUTEX_CONTESTED) != id)
1783 /* This should be done in userland */
1784 if ((owner & UMUTEX_CONTESTED) == 0) {
1785 error = casueword32(&m->m_owner, owner, &old, UMUTEX_UNOWNED);
1793 /* We should only ever be in here for contested locks */
1794 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1800 count = umtxq_count_pi(&key, &uq_first);
1801 if (uq_first != NULL) {
1802 mtx_lock_spin(&umtx_lock);
1803 pi = uq_first->uq_pi_blocked;
1804 KASSERT(pi != NULL, ("pi == NULL?"));
1805 if (pi->pi_owner != curthread) {
1806 mtx_unlock_spin(&umtx_lock);
1809 umtx_key_release(&key);
1810 /* userland messed the mutex */
1813 uq_me = curthread->td_umtxq;
1814 pi->pi_owner = NULL;
1815 TAILQ_REMOVE(&uq_me->uq_pi_contested, pi, pi_link);
1816 /* get highest priority thread which is still sleeping. */
1817 uq_first = TAILQ_FIRST(&pi->pi_blocked);
1818 while (uq_first != NULL &&
1819 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
1820 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
1823 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
1824 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
1825 if (uq_first2 != NULL) {
1826 if (pri > UPRI(uq_first2->uq_thread))
1827 pri = UPRI(uq_first2->uq_thread);
1830 thread_lock(curthread);
1831 sched_lend_user_prio(curthread, pri);
1832 thread_unlock(curthread);
1833 mtx_unlock_spin(&umtx_lock);
1835 umtxq_signal_thread(uq_first);
1840 * When unlocking the umtx, it must be marked as unowned if
1841 * there is zero or one thread only waiting for it.
1842 * Otherwise, it must be marked as contested.
1844 error = casueword32(&m->m_owner, owner, &old,
1845 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1847 umtxq_unbusy_unlocked(&key);
1848 umtx_key_release(&key);
1860 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
1861 struct _umtx_time *timeout, int try)
1863 struct abs_timeout timo;
1864 struct umtx_q *uq, *uq2;
1868 int error, pri, old_inherited_pri, su, rv;
1872 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
1876 if (timeout != NULL)
1877 abs_timeout_init2(&timo, timeout);
1879 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
1881 old_inherited_pri = uq->uq_inherited_pri;
1882 umtxq_lock(&uq->uq_key);
1883 umtxq_busy(&uq->uq_key);
1884 umtxq_unlock(&uq->uq_key);
1886 rv = fueword32(&m->m_ceilings[0], &ceiling);
1891 ceiling = RTP_PRIO_MAX - ceiling;
1892 if (ceiling > RTP_PRIO_MAX) {
1897 mtx_lock_spin(&umtx_lock);
1898 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
1899 mtx_unlock_spin(&umtx_lock);
1903 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
1904 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
1906 if (uq->uq_inherited_pri < UPRI(td))
1907 sched_lend_user_prio(td, uq->uq_inherited_pri);
1910 mtx_unlock_spin(&umtx_lock);
1912 rv = casueword32(&m->m_owner,
1913 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
1914 /* The address was invalid. */
1920 if (owner == UMUTEX_CONTESTED) {
1931 * If we caught a signal, we have retried and now
1937 umtxq_lock(&uq->uq_key);
1939 umtxq_unbusy(&uq->uq_key);
1940 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
1943 umtxq_unlock(&uq->uq_key);
1945 mtx_lock_spin(&umtx_lock);
1946 uq->uq_inherited_pri = old_inherited_pri;
1948 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
1949 uq2 = TAILQ_FIRST(&pi->pi_blocked);
1951 if (pri > UPRI(uq2->uq_thread))
1952 pri = UPRI(uq2->uq_thread);
1955 if (pri > uq->uq_inherited_pri)
1956 pri = uq->uq_inherited_pri;
1958 sched_lend_user_prio(td, pri);
1960 mtx_unlock_spin(&umtx_lock);
1964 mtx_lock_spin(&umtx_lock);
1965 uq->uq_inherited_pri = old_inherited_pri;
1967 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
1968 uq2 = TAILQ_FIRST(&pi->pi_blocked);
1970 if (pri > UPRI(uq2->uq_thread))
1971 pri = UPRI(uq2->uq_thread);
1974 if (pri > uq->uq_inherited_pri)
1975 pri = uq->uq_inherited_pri;
1977 sched_lend_user_prio(td, pri);
1979 mtx_unlock_spin(&umtx_lock);
1983 umtxq_unbusy_unlocked(&uq->uq_key);
1984 umtx_key_release(&uq->uq_key);
1989 * Unlock a PP mutex.
1992 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags)
1994 struct umtx_key key;
1995 struct umtx_q *uq, *uq2;
1999 int error, pri, new_inherited_pri, su;
2003 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2006 * Make sure we own this mtx.
2008 error = fueword32(&m->m_owner, &owner);
2012 if ((owner & ~UMUTEX_CONTESTED) != id)
2015 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2020 new_inherited_pri = PRI_MAX;
2022 rceiling = RTP_PRIO_MAX - rceiling;
2023 if (rceiling > RTP_PRIO_MAX)
2025 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2028 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2035 * For priority protected mutex, always set unlocked state
2036 * to UMUTEX_CONTESTED, so that userland always enters kernel
2037 * to lock the mutex, it is necessary because thread priority
2038 * has to be adjusted for such mutex.
2040 error = suword32(&m->m_owner, UMUTEX_CONTESTED);
2044 umtxq_signal(&key, 1);
2051 mtx_lock_spin(&umtx_lock);
2053 uq->uq_inherited_pri = new_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_spin(&umtx_lock);
2069 umtx_key_release(&key);
2074 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2075 uint32_t *old_ceiling)
2078 uint32_t save_ceiling;
2083 error = fueword32(&m->m_flags, &flags);
2086 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2088 if (ceiling > RTP_PRIO_MAX)
2092 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2096 umtxq_lock(&uq->uq_key);
2097 umtxq_busy(&uq->uq_key);
2098 umtxq_unlock(&uq->uq_key);
2100 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2106 rv = casueword32(&m->m_owner,
2107 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
2113 if (owner == UMUTEX_CONTESTED) {
2114 suword32(&m->m_ceilings[0], ceiling);
2115 suword32(&m->m_owner, UMUTEX_CONTESTED);
2120 if ((owner & ~UMUTEX_CONTESTED) == id) {
2121 suword32(&m->m_ceilings[0], ceiling);
2127 * If we caught a signal, we have retried and now
2134 * We set the contested bit, sleep. Otherwise the lock changed
2135 * and we need to retry or we lost a race to the thread
2136 * unlocking the umtx.
2138 umtxq_lock(&uq->uq_key);
2140 umtxq_unbusy(&uq->uq_key);
2141 error = umtxq_sleep(uq, "umtxpp", NULL);
2143 umtxq_unlock(&uq->uq_key);
2145 umtxq_lock(&uq->uq_key);
2147 umtxq_signal(&uq->uq_key, INT_MAX);
2148 umtxq_unbusy(&uq->uq_key);
2149 umtxq_unlock(&uq->uq_key);
2150 umtx_key_release(&uq->uq_key);
2151 if (error == 0 && old_ceiling != NULL)
2152 suword32(old_ceiling, save_ceiling);
2157 * Lock a userland POSIX mutex.
2160 do_lock_umutex(struct thread *td, struct umutex *m,
2161 struct _umtx_time *timeout, int mode)
2166 error = fueword32(&m->m_flags, &flags);
2170 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2172 error = do_lock_normal(td, m, flags, timeout, mode);
2174 case UMUTEX_PRIO_INHERIT:
2175 error = do_lock_pi(td, m, flags, timeout, mode);
2177 case UMUTEX_PRIO_PROTECT:
2178 error = do_lock_pp(td, m, flags, timeout, mode);
2183 if (timeout == NULL) {
2184 if (error == EINTR && mode != _UMUTEX_WAIT)
2187 /* Timed-locking is not restarted. */
2188 if (error == ERESTART)
2195 * Unlock a userland POSIX mutex.
2198 do_unlock_umutex(struct thread *td, struct umutex *m)
2203 error = fueword32(&m->m_flags, &flags);
2207 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2209 return (do_unlock_normal(td, m, flags));
2210 case UMUTEX_PRIO_INHERIT:
2211 return (do_unlock_pi(td, m, flags));
2212 case UMUTEX_PRIO_PROTECT:
2213 return (do_unlock_pp(td, m, flags));
2220 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2221 struct timespec *timeout, u_long wflags)
2223 struct abs_timeout timo;
2225 uint32_t flags, clockid, hasw;
2229 error = fueword32(&cv->c_flags, &flags);
2232 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2236 if ((wflags & CVWAIT_CLOCKID) != 0) {
2237 error = fueword32(&cv->c_clockid, &clockid);
2239 umtx_key_release(&uq->uq_key);
2242 if (clockid < CLOCK_REALTIME ||
2243 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2244 /* hmm, only HW clock id will work. */
2245 umtx_key_release(&uq->uq_key);
2249 clockid = CLOCK_REALTIME;
2252 umtxq_lock(&uq->uq_key);
2253 umtxq_busy(&uq->uq_key);
2255 umtxq_unlock(&uq->uq_key);
2258 * Set c_has_waiters to 1 before releasing user mutex, also
2259 * don't modify cache line when unnecessary.
2261 error = fueword32(&cv->c_has_waiters, &hasw);
2262 if (error == 0 && hasw == 0)
2263 suword32(&cv->c_has_waiters, 1);
2265 umtxq_unbusy_unlocked(&uq->uq_key);
2267 error = do_unlock_umutex(td, m);
2269 if (timeout != NULL)
2270 abs_timeout_init(&timo, clockid, ((wflags & CVWAIT_ABSTIME) != 0),
2273 umtxq_lock(&uq->uq_key);
2275 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2279 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2283 * This must be timeout,interrupted by signal or
2284 * surprious wakeup, clear c_has_waiter flag when
2287 umtxq_busy(&uq->uq_key);
2288 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2289 int oldlen = uq->uq_cur_queue->length;
2292 umtxq_unlock(&uq->uq_key);
2293 suword32(&cv->c_has_waiters, 0);
2294 umtxq_lock(&uq->uq_key);
2297 umtxq_unbusy(&uq->uq_key);
2298 if (error == ERESTART)
2302 umtxq_unlock(&uq->uq_key);
2303 umtx_key_release(&uq->uq_key);
2308 * Signal a userland condition variable.
2311 do_cv_signal(struct thread *td, struct ucond *cv)
2313 struct umtx_key key;
2314 int error, cnt, nwake;
2317 error = fueword32(&cv->c_flags, &flags);
2320 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2324 cnt = umtxq_count(&key);
2325 nwake = umtxq_signal(&key, 1);
2328 error = suword32(&cv->c_has_waiters, 0);
2335 umtx_key_release(&key);
2340 do_cv_broadcast(struct thread *td, struct ucond *cv)
2342 struct umtx_key key;
2346 error = fueword32(&cv->c_flags, &flags);
2349 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2354 umtxq_signal(&key, INT_MAX);
2357 error = suword32(&cv->c_has_waiters, 0);
2361 umtxq_unbusy_unlocked(&key);
2363 umtx_key_release(&key);
2368 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, struct _umtx_time *timeout)
2370 struct abs_timeout timo;
2372 uint32_t flags, wrflags;
2373 int32_t state, oldstate;
2374 int32_t blocked_readers;
2378 error = fueword32(&rwlock->rw_flags, &flags);
2381 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2385 if (timeout != NULL)
2386 abs_timeout_init2(&timo, timeout);
2388 wrflags = URWLOCK_WRITE_OWNER;
2389 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2390 wrflags |= URWLOCK_WRITE_WAITERS;
2393 rv = fueword32(&rwlock->rw_state, &state);
2395 umtx_key_release(&uq->uq_key);
2399 /* try to lock it */
2400 while (!(state & wrflags)) {
2401 if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) {
2402 umtx_key_release(&uq->uq_key);
2405 rv = casueword32(&rwlock->rw_state, state,
2406 &oldstate, state + 1);
2408 umtx_key_release(&uq->uq_key);
2411 if (oldstate == state) {
2412 umtx_key_release(&uq->uq_key);
2415 error = umtxq_check_susp(td);
2424 /* grab monitor lock */
2425 umtxq_lock(&uq->uq_key);
2426 umtxq_busy(&uq->uq_key);
2427 umtxq_unlock(&uq->uq_key);
2430 * re-read the state, in case it changed between the try-lock above
2431 * and the check below
2433 rv = fueword32(&rwlock->rw_state, &state);
2437 /* set read contention bit */
2438 while (error == 0 && (state & wrflags) &&
2439 !(state & URWLOCK_READ_WAITERS)) {
2440 rv = casueword32(&rwlock->rw_state, state,
2441 &oldstate, state | URWLOCK_READ_WAITERS);
2446 if (oldstate == state)
2449 error = umtxq_check_susp(td);
2454 umtxq_unbusy_unlocked(&uq->uq_key);
2458 /* state is changed while setting flags, restart */
2459 if (!(state & wrflags)) {
2460 umtxq_unbusy_unlocked(&uq->uq_key);
2461 error = umtxq_check_susp(td);
2468 /* contention bit is set, before sleeping, increase read waiter count */
2469 rv = fueword32(&rwlock->rw_blocked_readers,
2472 umtxq_unbusy_unlocked(&uq->uq_key);
2476 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2478 while (state & wrflags) {
2479 umtxq_lock(&uq->uq_key);
2481 umtxq_unbusy(&uq->uq_key);
2483 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2486 umtxq_busy(&uq->uq_key);
2488 umtxq_unlock(&uq->uq_key);
2491 rv = fueword32(&rwlock->rw_state, &state);
2498 /* decrease read waiter count, and may clear read contention bit */
2499 rv = fueword32(&rwlock->rw_blocked_readers,
2502 umtxq_unbusy_unlocked(&uq->uq_key);
2506 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2507 if (blocked_readers == 1) {
2508 rv = fueword32(&rwlock->rw_state, &state);
2511 while (error == 0) {
2512 rv = casueword32(&rwlock->rw_state, state,
2513 &oldstate, state & ~URWLOCK_READ_WAITERS);
2518 if (oldstate == state)
2521 error = umtxq_check_susp(td);
2525 umtxq_unbusy_unlocked(&uq->uq_key);
2529 umtx_key_release(&uq->uq_key);
2530 if (error == ERESTART)
2536 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2538 struct abs_timeout timo;
2541 int32_t state, oldstate;
2542 int32_t blocked_writers;
2543 int32_t blocked_readers;
2547 error = fueword32(&rwlock->rw_flags, &flags);
2550 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2554 if (timeout != NULL)
2555 abs_timeout_init2(&timo, timeout);
2557 blocked_readers = 0;
2559 rv = fueword32(&rwlock->rw_state, &state);
2561 umtx_key_release(&uq->uq_key);
2564 while (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2565 rv = casueword32(&rwlock->rw_state, state,
2566 &oldstate, state | URWLOCK_WRITE_OWNER);
2568 umtx_key_release(&uq->uq_key);
2571 if (oldstate == state) {
2572 umtx_key_release(&uq->uq_key);
2576 error = umtxq_check_susp(td);
2582 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) &&
2583 blocked_readers != 0) {
2584 umtxq_lock(&uq->uq_key);
2585 umtxq_busy(&uq->uq_key);
2586 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE);
2587 umtxq_unbusy(&uq->uq_key);
2588 umtxq_unlock(&uq->uq_key);
2594 /* grab monitor lock */
2595 umtxq_lock(&uq->uq_key);
2596 umtxq_busy(&uq->uq_key);
2597 umtxq_unlock(&uq->uq_key);
2600 * re-read the state, in case it changed between the try-lock above
2601 * and the check below
2603 rv = fueword32(&rwlock->rw_state, &state);
2607 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
2608 URWLOCK_READER_COUNT(state) != 0) &&
2609 (state & URWLOCK_WRITE_WAITERS) == 0) {
2610 rv = casueword32(&rwlock->rw_state, state,
2611 &oldstate, state | URWLOCK_WRITE_WAITERS);
2616 if (oldstate == state)
2619 error = umtxq_check_susp(td);
2624 umtxq_unbusy_unlocked(&uq->uq_key);
2628 if (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2629 umtxq_unbusy_unlocked(&uq->uq_key);
2630 error = umtxq_check_susp(td);
2636 rv = fueword32(&rwlock->rw_blocked_writers,
2639 umtxq_unbusy_unlocked(&uq->uq_key);
2643 suword32(&rwlock->rw_blocked_writers, blocked_writers+1);
2645 while ((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) {
2646 umtxq_lock(&uq->uq_key);
2647 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2648 umtxq_unbusy(&uq->uq_key);
2650 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
2653 umtxq_busy(&uq->uq_key);
2654 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2655 umtxq_unlock(&uq->uq_key);
2658 rv = fueword32(&rwlock->rw_state, &state);
2665 rv = fueword32(&rwlock->rw_blocked_writers,
2668 umtxq_unbusy_unlocked(&uq->uq_key);
2672 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
2673 if (blocked_writers == 1) {
2674 rv = fueword32(&rwlock->rw_state, &state);
2676 umtxq_unbusy_unlocked(&uq->uq_key);
2681 rv = casueword32(&rwlock->rw_state, state,
2682 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
2687 if (oldstate == state)
2690 error = umtxq_check_susp(td);
2692 * We are leaving the URWLOCK_WRITE_WAITERS
2693 * behind, but this should not harm the
2699 rv = fueword32(&rwlock->rw_blocked_readers,
2702 umtxq_unbusy_unlocked(&uq->uq_key);
2707 blocked_readers = 0;
2709 umtxq_unbusy_unlocked(&uq->uq_key);
2712 umtx_key_release(&uq->uq_key);
2713 if (error == ERESTART)
2719 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
2723 int32_t state, oldstate;
2724 int error, rv, q, count;
2727 error = fueword32(&rwlock->rw_flags, &flags);
2730 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2734 error = fueword32(&rwlock->rw_state, &state);
2739 if (state & URWLOCK_WRITE_OWNER) {
2741 rv = casueword32(&rwlock->rw_state, state,
2742 &oldstate, state & ~URWLOCK_WRITE_OWNER);
2747 if (oldstate != state) {
2749 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
2753 error = umtxq_check_susp(td);
2759 } else if (URWLOCK_READER_COUNT(state) != 0) {
2761 rv = casueword32(&rwlock->rw_state, state,
2762 &oldstate, state - 1);
2767 if (oldstate != state) {
2769 if (URWLOCK_READER_COUNT(oldstate) == 0) {
2773 error = umtxq_check_susp(td);
2786 if (!(flags & URWLOCK_PREFER_READER)) {
2787 if (state & URWLOCK_WRITE_WAITERS) {
2789 q = UMTX_EXCLUSIVE_QUEUE;
2790 } else if (state & URWLOCK_READ_WAITERS) {
2792 q = UMTX_SHARED_QUEUE;
2795 if (state & URWLOCK_READ_WAITERS) {
2797 q = UMTX_SHARED_QUEUE;
2798 } else if (state & URWLOCK_WRITE_WAITERS) {
2800 q = UMTX_EXCLUSIVE_QUEUE;
2805 umtxq_lock(&uq->uq_key);
2806 umtxq_busy(&uq->uq_key);
2807 umtxq_signal_queue(&uq->uq_key, count, q);
2808 umtxq_unbusy(&uq->uq_key);
2809 umtxq_unlock(&uq->uq_key);
2812 umtx_key_release(&uq->uq_key);
2816 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
2818 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
2820 struct abs_timeout timo;
2822 uint32_t flags, count, count1;
2826 error = fueword32(&sem->_flags, &flags);
2829 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
2833 if (timeout != NULL)
2834 abs_timeout_init2(&timo, timeout);
2836 umtxq_lock(&uq->uq_key);
2837 umtxq_busy(&uq->uq_key);
2839 umtxq_unlock(&uq->uq_key);
2840 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
2842 rv = fueword32(&sem->_count, &count);
2843 if (rv == -1 || count != 0) {
2844 umtxq_lock(&uq->uq_key);
2845 umtxq_unbusy(&uq->uq_key);
2847 umtxq_unlock(&uq->uq_key);
2848 umtx_key_release(&uq->uq_key);
2849 return (rv == -1 ? EFAULT : 0);
2851 umtxq_lock(&uq->uq_key);
2852 umtxq_unbusy(&uq->uq_key);
2854 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
2856 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2860 /* A relative timeout cannot be restarted. */
2861 if (error == ERESTART && timeout != NULL &&
2862 (timeout->_flags & UMTX_ABSTIME) == 0)
2865 umtxq_unlock(&uq->uq_key);
2866 umtx_key_release(&uq->uq_key);
2871 * Signal a userland semaphore.
2874 do_sem_wake(struct thread *td, struct _usem *sem)
2876 struct umtx_key key;
2880 error = fueword32(&sem->_flags, &flags);
2883 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
2887 cnt = umtxq_count(&key);
2889 umtxq_signal(&key, 1);
2891 * Check if count is greater than 0, this means the memory is
2892 * still being referenced by user code, so we can safely
2893 * update _has_waiters flag.
2897 error = suword32(&sem->_has_waiters, 0);
2905 umtx_key_release(&key);
2911 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
2913 struct abs_timeout timo;
2915 uint32_t count, flags;
2919 flags = fuword32(&sem->_flags);
2920 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
2924 if (timeout != NULL)
2925 abs_timeout_init2(&timo, timeout);
2927 umtxq_lock(&uq->uq_key);
2928 umtxq_busy(&uq->uq_key);
2930 umtxq_unlock(&uq->uq_key);
2931 rv = fueword32(&sem->_count, &count);
2933 umtxq_lock(&uq->uq_key);
2934 umtxq_unbusy(&uq->uq_key);
2936 umtxq_unlock(&uq->uq_key);
2937 umtx_key_release(&uq->uq_key);
2941 if (USEM_COUNT(count) != 0) {
2942 umtxq_lock(&uq->uq_key);
2943 umtxq_unbusy(&uq->uq_key);
2945 umtxq_unlock(&uq->uq_key);
2946 umtx_key_release(&uq->uq_key);
2949 if (count == USEM_HAS_WAITERS)
2951 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
2953 umtxq_lock(&uq->uq_key);
2954 umtxq_unbusy(&uq->uq_key);
2956 umtxq_unlock(&uq->uq_key);
2957 umtx_key_release(&uq->uq_key);
2963 umtxq_lock(&uq->uq_key);
2964 umtxq_unbusy(&uq->uq_key);
2966 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
2968 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2972 /* A relative timeout cannot be restarted. */
2973 if (error == ERESTART && timeout != NULL &&
2974 (timeout->_flags & UMTX_ABSTIME) == 0)
2977 umtxq_unlock(&uq->uq_key);
2978 umtx_key_release(&uq->uq_key);
2983 * Signal a userland semaphore.
2986 do_sem2_wake(struct thread *td, struct _usem2 *sem)
2988 struct umtx_key key;
2990 uint32_t count, flags;
2992 rv = fueword32(&sem->_flags, &flags);
2995 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
2999 cnt = umtxq_count(&key);
3001 umtxq_signal(&key, 1);
3004 * If this was the last sleeping thread, clear the waiters
3009 rv = fueword32(&sem->_count, &count);
3010 while (rv != -1 && count & USEM_HAS_WAITERS)
3011 rv = casueword32(&sem->_count, count, &count,
3012 count & ~USEM_HAS_WAITERS);
3020 umtx_key_release(&key);
3025 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3029 error = copyin(addr, tsp, sizeof(struct timespec));
3031 if (tsp->tv_sec < 0 ||
3032 tsp->tv_nsec >= 1000000000 ||
3040 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3044 if (size <= sizeof(struct timespec)) {
3045 tp->_clockid = CLOCK_REALTIME;
3047 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3049 error = copyin(addr, tp, sizeof(struct _umtx_time));
3052 if (tp->_timeout.tv_sec < 0 ||
3053 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3059 __umtx_op_unimpl(struct thread *td, struct _umtx_op_args *uap)
3062 return (EOPNOTSUPP);
3066 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3068 struct _umtx_time timeout, *tm_p;
3071 if (uap->uaddr2 == NULL)
3074 error = umtx_copyin_umtx_time(
3075 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3080 return do_wait(td, uap->obj, uap->val, tm_p, 0, 0);
3084 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3086 struct _umtx_time timeout, *tm_p;
3089 if (uap->uaddr2 == NULL)
3092 error = umtx_copyin_umtx_time(
3093 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3098 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3102 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3104 struct _umtx_time *tm_p, timeout;
3107 if (uap->uaddr2 == NULL)
3110 error = umtx_copyin_umtx_time(
3111 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3116 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3120 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3122 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3125 #define BATCH_SIZE 128
3127 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3129 int count = uap->val;
3130 void *uaddrs[BATCH_SIZE];
3131 char **upp = (char **)uap->obj;
3138 if (tocopy > BATCH_SIZE)
3139 tocopy = BATCH_SIZE;
3140 error = copyin(upp+pos, uaddrs, tocopy * sizeof(char *));
3143 for (i = 0; i < tocopy; ++i)
3144 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3152 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3154 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3158 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3160 struct _umtx_time *tm_p, timeout;
3163 /* Allow a null timespec (wait forever). */
3164 if (uap->uaddr2 == NULL)
3167 error = umtx_copyin_umtx_time(
3168 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3173 return do_lock_umutex(td, uap->obj, tm_p, 0);
3177 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3179 return do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY);
3183 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3185 struct _umtx_time *tm_p, timeout;
3188 /* Allow a null timespec (wait forever). */
3189 if (uap->uaddr2 == NULL)
3192 error = umtx_copyin_umtx_time(
3193 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3198 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3202 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3204 return do_wake_umutex(td, uap->obj);
3208 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3210 return do_unlock_umutex(td, uap->obj);
3214 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3216 return do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1);
3220 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3222 struct timespec *ts, timeout;
3225 /* Allow a null timespec (wait forever). */
3226 if (uap->uaddr2 == NULL)
3229 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3234 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3238 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3240 return do_cv_signal(td, uap->obj);
3244 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3246 return do_cv_broadcast(td, uap->obj);
3250 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3252 struct _umtx_time timeout;
3255 /* Allow a null timespec (wait forever). */
3256 if (uap->uaddr2 == NULL) {
3257 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3259 error = umtx_copyin_umtx_time(uap->uaddr2,
3260 (size_t)uap->uaddr1, &timeout);
3263 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3269 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3271 struct _umtx_time timeout;
3274 /* Allow a null timespec (wait forever). */
3275 if (uap->uaddr2 == NULL) {
3276 error = do_rw_wrlock(td, uap->obj, 0);
3278 error = umtx_copyin_umtx_time(uap->uaddr2,
3279 (size_t)uap->uaddr1, &timeout);
3283 error = do_rw_wrlock(td, uap->obj, &timeout);
3289 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3291 return do_rw_unlock(td, uap->obj);
3294 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3296 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3298 struct _umtx_time *tm_p, timeout;
3301 /* Allow a null timespec (wait forever). */
3302 if (uap->uaddr2 == NULL)
3305 error = umtx_copyin_umtx_time(
3306 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3311 return (do_sem_wait(td, uap->obj, tm_p));
3315 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3317 return do_sem_wake(td, uap->obj);
3322 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3324 return do_wake2_umutex(td, uap->obj, uap->val);
3328 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap)
3330 struct _umtx_time *tm_p, timeout;
3333 /* Allow a null timespec (wait forever). */
3334 if (uap->uaddr2 == NULL)
3337 error = umtx_copyin_umtx_time(
3338 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3343 return (do_sem2_wait(td, uap->obj, tm_p));
3347 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap)
3349 return do_sem2_wake(td, uap->obj);
3352 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
3354 static _umtx_op_func op_table[] = {
3355 __umtx_op_unimpl, /* UMTX_OP_RESERVED0 */
3356 __umtx_op_unimpl, /* UMTX_OP_RESERVED1 */
3357 __umtx_op_wait, /* UMTX_OP_WAIT */
3358 __umtx_op_wake, /* UMTX_OP_WAKE */
3359 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_TRYLOCK */
3360 __umtx_op_lock_umutex, /* UMTX_OP_MUTEX_LOCK */
3361 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3362 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3363 __umtx_op_cv_wait, /* UMTX_OP_CV_WAIT*/
3364 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3365 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3366 __umtx_op_wait_uint, /* UMTX_OP_WAIT_UINT */
3367 __umtx_op_rw_rdlock, /* UMTX_OP_RW_RDLOCK */
3368 __umtx_op_rw_wrlock, /* UMTX_OP_RW_WRLOCK */
3369 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3370 __umtx_op_wait_uint_private, /* UMTX_OP_WAIT_UINT_PRIVATE */
3371 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3372 __umtx_op_wait_umutex, /* UMTX_OP_MUTEX_WAIT */
3373 __umtx_op_wake_umutex, /* UMTX_OP_MUTEX_WAKE */
3374 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3375 __umtx_op_sem_wait, /* UMTX_OP_SEM_WAIT */
3376 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3378 __umtx_op_unimpl, /* UMTX_OP_SEM_WAIT */
3379 __umtx_op_unimpl, /* UMTX_OP_SEM_WAKE */
3381 __umtx_op_nwake_private, /* UMTX_OP_NWAKE_PRIVATE */
3382 __umtx_op_wake2_umutex, /* UMTX_OP_MUTEX_WAKE2 */
3383 __umtx_op_sem2_wait, /* UMTX_OP_SEM2_WAIT */
3384 __umtx_op_sem2_wake, /* UMTX_OP_SEM2_WAKE */
3388 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
3390 if ((unsigned)uap->op < UMTX_OP_MAX)
3391 return (*op_table[uap->op])(td, uap);
3395 #ifdef COMPAT_FREEBSD32
3402 struct umtx_time32 {
3403 struct timespec32 timeout;
3409 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
3411 struct timespec32 ts32;
3414 error = copyin(addr, &ts32, sizeof(struct timespec32));
3416 if (ts32.tv_sec < 0 ||
3417 ts32.tv_nsec >= 1000000000 ||
3421 tsp->tv_sec = ts32.tv_sec;
3422 tsp->tv_nsec = ts32.tv_nsec;
3429 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
3431 struct umtx_time32 t32;
3434 t32.clockid = CLOCK_REALTIME;
3436 if (size <= sizeof(struct timespec32))
3437 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
3439 error = copyin(addr, &t32, sizeof(struct umtx_time32));
3442 if (t32.timeout.tv_sec < 0 ||
3443 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
3445 tp->_timeout.tv_sec = t32.timeout.tv_sec;
3446 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
3447 tp->_flags = t32.flags;
3448 tp->_clockid = t32.clockid;
3453 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3455 struct _umtx_time *tm_p, timeout;
3458 if (uap->uaddr2 == NULL)
3461 error = umtx_copyin_umtx_time32(uap->uaddr2,
3462 (size_t)uap->uaddr1, &timeout);
3467 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3471 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3473 struct _umtx_time *tm_p, timeout;
3476 /* Allow a null timespec (wait forever). */
3477 if (uap->uaddr2 == NULL)
3480 error = umtx_copyin_umtx_time(uap->uaddr2,
3481 (size_t)uap->uaddr1, &timeout);
3486 return do_lock_umutex(td, uap->obj, tm_p, 0);
3490 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3492 struct _umtx_time *tm_p, timeout;
3495 /* Allow a null timespec (wait forever). */
3496 if (uap->uaddr2 == NULL)
3499 error = umtx_copyin_umtx_time32(uap->uaddr2,
3500 (size_t)uap->uaddr1, &timeout);
3505 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3509 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3511 struct timespec *ts, timeout;
3514 /* Allow a null timespec (wait forever). */
3515 if (uap->uaddr2 == NULL)
3518 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3523 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3527 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3529 struct _umtx_time timeout;
3532 /* Allow a null timespec (wait forever). */
3533 if (uap->uaddr2 == NULL) {
3534 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3536 error = umtx_copyin_umtx_time32(uap->uaddr2,
3537 (size_t)uap->uaddr1, &timeout);
3540 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3546 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3548 struct _umtx_time timeout;
3551 /* Allow a null timespec (wait forever). */
3552 if (uap->uaddr2 == NULL) {
3553 error = do_rw_wrlock(td, uap->obj, 0);
3555 error = umtx_copyin_umtx_time32(uap->uaddr2,
3556 (size_t)uap->uaddr1, &timeout);
3559 error = do_rw_wrlock(td, uap->obj, &timeout);
3565 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3567 struct _umtx_time *tm_p, timeout;
3570 if (uap->uaddr2 == NULL)
3573 error = umtx_copyin_umtx_time32(
3574 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
3579 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3582 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3584 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3586 struct _umtx_time *tm_p, timeout;
3589 /* Allow a null timespec (wait forever). */
3590 if (uap->uaddr2 == NULL)
3593 error = umtx_copyin_umtx_time32(uap->uaddr2,
3594 (size_t)uap->uaddr1, &timeout);
3599 return (do_sem_wait(td, uap->obj, tm_p));
3604 __umtx_op_sem2_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3606 struct _umtx_time *tm_p, timeout;
3609 /* Allow a null timespec (wait forever). */
3610 if (uap->uaddr2 == NULL)
3613 error = umtx_copyin_umtx_time32(uap->uaddr2,
3614 (size_t)uap->uaddr1, &timeout);
3619 return (do_sem2_wait(td, uap->obj, tm_p));
3623 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
3625 int count = uap->val;
3626 uint32_t uaddrs[BATCH_SIZE];
3627 uint32_t **upp = (uint32_t **)uap->obj;
3634 if (tocopy > BATCH_SIZE)
3635 tocopy = BATCH_SIZE;
3636 error = copyin(upp+pos, uaddrs, tocopy * sizeof(uint32_t));
3639 for (i = 0; i < tocopy; ++i)
3640 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
3648 static _umtx_op_func op_table_compat32[] = {
3649 __umtx_op_unimpl, /* UMTX_OP_RESERVED0 */
3650 __umtx_op_unimpl, /* UMTX_OP_RESERVED1 */
3651 __umtx_op_wait_compat32, /* UMTX_OP_WAIT */
3652 __umtx_op_wake, /* UMTX_OP_WAKE */
3653 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_LOCK */
3654 __umtx_op_lock_umutex_compat32, /* UMTX_OP_MUTEX_TRYLOCK */
3655 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3656 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3657 __umtx_op_cv_wait_compat32, /* UMTX_OP_CV_WAIT*/
3658 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3659 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3660 __umtx_op_wait_compat32, /* UMTX_OP_WAIT_UINT */
3661 __umtx_op_rw_rdlock_compat32, /* UMTX_OP_RW_RDLOCK */
3662 __umtx_op_rw_wrlock_compat32, /* UMTX_OP_RW_WRLOCK */
3663 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3664 __umtx_op_wait_uint_private_compat32, /* UMTX_OP_WAIT_UINT_PRIVATE */
3665 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3666 __umtx_op_wait_umutex_compat32, /* UMTX_OP_MUTEX_WAIT */
3667 __umtx_op_wake_umutex, /* UMTX_OP_MUTEX_WAKE */
3668 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3669 __umtx_op_sem_wait_compat32, /* UMTX_OP_SEM_WAIT */
3670 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3672 __umtx_op_unimpl, /* UMTX_OP_SEM_WAIT */
3673 __umtx_op_unimpl, /* UMTX_OP_SEM_WAKE */
3675 __umtx_op_nwake_private32, /* UMTX_OP_NWAKE_PRIVATE */
3676 __umtx_op_wake2_umutex, /* UMTX_OP_MUTEX_WAKE2 */
3677 __umtx_op_sem2_wait_compat32, /* UMTX_OP_SEM2_WAIT */
3678 __umtx_op_sem2_wake, /* UMTX_OP_SEM2_WAKE */
3682 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
3684 if ((unsigned)uap->op < UMTX_OP_MAX)
3685 return (*op_table_compat32[uap->op])(td,
3686 (struct _umtx_op_args *)uap);
3692 umtx_thread_init(struct thread *td)
3694 td->td_umtxq = umtxq_alloc();
3695 td->td_umtxq->uq_thread = td;
3699 umtx_thread_fini(struct thread *td)
3701 umtxq_free(td->td_umtxq);
3705 * It will be called when new thread is created, e.g fork().
3708 umtx_thread_alloc(struct thread *td)
3713 uq->uq_inherited_pri = PRI_MAX;
3715 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
3716 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
3717 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
3718 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
3725 umtx_exec_hook(void *arg __unused, struct proc *p __unused,
3726 struct image_params *imgp __unused)
3728 umtx_thread_cleanup(curthread);
3732 * thread_exit() hook.
3735 umtx_thread_exit(struct thread *td)
3737 umtx_thread_cleanup(td);
3741 * clean up umtx data.
3744 umtx_thread_cleanup(struct thread *td)
3749 if ((uq = td->td_umtxq) == NULL)
3752 mtx_lock_spin(&umtx_lock);
3753 uq->uq_inherited_pri = PRI_MAX;
3754 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
3755 pi->pi_owner = NULL;
3756 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
3758 mtx_unlock_spin(&umtx_lock);
3760 sched_lend_user_prio(td, PRI_MAX);