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)
172 #define UMTXQ_BUSY_ASSERT(uc) KASSERT(&(uc)->uc_busy, ("umtx chain is not busy"))
175 * Don't propagate time-sharing priority, there is a security reason,
176 * a user can simply introduce PI-mutex, let thread A lock the mutex,
177 * and let another thread B block on the mutex, because B is
178 * sleeping, its priority will be boosted, this causes A's priority to
179 * be boosted via priority propagating too and will never be lowered even
180 * if it is using 100%CPU, this is unfair to other processes.
183 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
184 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
185 PRI_MAX_TIMESHARE : (td)->td_user_pri)
187 #define GOLDEN_RATIO_PRIME 2654404609U
188 #define UMTX_CHAINS 512
189 #define UMTX_SHIFTS (__WORD_BIT - 9)
191 #define GET_SHARE(flags) \
192 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
194 #define BUSY_SPINS 200
202 static uma_zone_t umtx_pi_zone;
203 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
204 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
205 static int umtx_pi_allocated;
207 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW, 0, "umtx debug");
208 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
209 &umtx_pi_allocated, 0, "Allocated umtx_pi");
211 #ifdef UMTX_PROFILING
212 static long max_length;
213 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
214 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD, 0, "umtx chain stats");
217 static void umtxq_sysinit(void *);
218 static void umtxq_hash(struct umtx_key *key);
219 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
220 static void umtxq_lock(struct umtx_key *key);
221 static void umtxq_unlock(struct umtx_key *key);
222 static void umtxq_busy(struct umtx_key *key);
223 static void umtxq_unbusy(struct umtx_key *key);
224 static void umtxq_insert_queue(struct umtx_q *uq, int q);
225 static void umtxq_remove_queue(struct umtx_q *uq, int q);
226 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *);
227 static int umtxq_count(struct umtx_key *key);
228 static struct umtx_pi *umtx_pi_alloc(int);
229 static void umtx_pi_free(struct umtx_pi *pi);
230 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags);
231 static void umtx_thread_cleanup(struct thread *td);
232 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
233 struct image_params *imgp __unused);
234 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
236 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
237 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
238 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
240 static struct mtx umtx_lock;
242 #ifdef UMTX_PROFILING
244 umtx_init_profiling(void)
246 struct sysctl_oid *chain_oid;
250 for (i = 0; i < UMTX_CHAINS; ++i) {
251 snprintf(chain_name, sizeof(chain_name), "%d", i);
252 chain_oid = SYSCTL_ADD_NODE(NULL,
253 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
254 chain_name, CTLFLAG_RD, NULL, "umtx hash stats");
255 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
256 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
257 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
258 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
263 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
267 struct umtxq_chain *uc;
268 u_int fract, i, j, tot, whole;
269 u_int sf0, sf1, sf2, sf3, sf4;
270 u_int si0, si1, si2, si3, si4;
271 u_int sw0, sw1, sw2, sw3, sw4;
273 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
274 for (i = 0; i < 2; i++) {
276 for (j = 0; j < UMTX_CHAINS; ++j) {
277 uc = &umtxq_chains[i][j];
278 mtx_lock(&uc->uc_lock);
279 tot += uc->max_length;
280 mtx_unlock(&uc->uc_lock);
283 sbuf_printf(&sb, "%u) Empty ", i);
285 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
286 si0 = si1 = si2 = si3 = si4 = 0;
287 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
288 for (j = 0; j < UMTX_CHAINS; j++) {
289 uc = &umtxq_chains[i][j];
290 mtx_lock(&uc->uc_lock);
291 whole = uc->max_length * 100;
292 mtx_unlock(&uc->uc_lock);
293 fract = (whole % tot) * 100;
294 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
298 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
303 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
308 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
313 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
320 sbuf_printf(&sb, "queue %u:\n", i);
321 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
323 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
325 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
327 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
329 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
335 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
341 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
343 struct umtxq_chain *uc;
348 error = sysctl_handle_int(oidp, &clear, 0, req);
349 if (error != 0 || req->newptr == NULL)
353 for (i = 0; i < 2; ++i) {
354 for (j = 0; j < UMTX_CHAINS; ++j) {
355 uc = &umtxq_chains[i][j];
356 mtx_lock(&uc->uc_lock);
359 mtx_unlock(&uc->uc_lock);
366 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
367 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
368 sysctl_debug_umtx_chains_clear, "I", "Clear umtx chains statistics");
369 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
370 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
371 sysctl_debug_umtx_chains_peaks, "A", "Highest peaks in chains max length");
375 umtxq_sysinit(void *arg __unused)
379 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
380 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
381 for (i = 0; i < 2; ++i) {
382 for (j = 0; j < UMTX_CHAINS; ++j) {
383 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
384 MTX_DEF | MTX_DUPOK);
385 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
386 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
387 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
388 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
389 umtxq_chains[i][j].uc_busy = 0;
390 umtxq_chains[i][j].uc_waiters = 0;
391 #ifdef UMTX_PROFILING
392 umtxq_chains[i][j].length = 0;
393 umtxq_chains[i][j].max_length = 0;
397 #ifdef UMTX_PROFILING
398 umtx_init_profiling();
400 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_SPIN);
401 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
402 EVENTHANDLER_PRI_ANY);
410 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
411 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX, M_WAITOK | M_ZERO);
412 TAILQ_INIT(&uq->uq_spare_queue->head);
413 TAILQ_INIT(&uq->uq_pi_contested);
414 uq->uq_inherited_pri = PRI_MAX;
419 umtxq_free(struct umtx_q *uq)
421 MPASS(uq->uq_spare_queue != NULL);
422 free(uq->uq_spare_queue, M_UMTX);
427 umtxq_hash(struct umtx_key *key)
429 unsigned n = (uintptr_t)key->info.both.a + key->info.both.b;
430 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
433 static inline struct umtxq_chain *
434 umtxq_getchain(struct umtx_key *key)
436 if (key->type <= TYPE_SEM)
437 return (&umtxq_chains[1][key->hash]);
438 return (&umtxq_chains[0][key->hash]);
445 umtxq_lock(struct umtx_key *key)
447 struct umtxq_chain *uc;
449 uc = umtxq_getchain(key);
450 mtx_lock(&uc->uc_lock);
457 umtxq_unlock(struct umtx_key *key)
459 struct umtxq_chain *uc;
461 uc = umtxq_getchain(key);
462 mtx_unlock(&uc->uc_lock);
466 * Set chain to busy state when following operation
467 * may be blocked (kernel mutex can not be used).
470 umtxq_busy(struct umtx_key *key)
472 struct umtxq_chain *uc;
474 uc = umtxq_getchain(key);
475 mtx_assert(&uc->uc_lock, MA_OWNED);
479 int count = BUSY_SPINS;
482 while (uc->uc_busy && --count > 0)
488 while (uc->uc_busy) {
490 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
501 umtxq_unbusy(struct umtx_key *key)
503 struct umtxq_chain *uc;
505 uc = umtxq_getchain(key);
506 mtx_assert(&uc->uc_lock, MA_OWNED);
507 KASSERT(uc->uc_busy != 0, ("not busy"));
513 static struct umtxq_queue *
514 umtxq_queue_lookup(struct umtx_key *key, int q)
516 struct umtxq_queue *uh;
517 struct umtxq_chain *uc;
519 uc = umtxq_getchain(key);
520 UMTXQ_LOCKED_ASSERT(uc);
521 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
522 if (umtx_key_match(&uh->key, key))
530 umtxq_insert_queue(struct umtx_q *uq, int q)
532 struct umtxq_queue *uh;
533 struct umtxq_chain *uc;
535 uc = umtxq_getchain(&uq->uq_key);
536 UMTXQ_LOCKED_ASSERT(uc);
537 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
538 uh = umtxq_queue_lookup(&uq->uq_key, q);
540 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
542 uh = uq->uq_spare_queue;
543 uh->key = uq->uq_key;
544 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
545 #ifdef UMTX_PROFILING
547 if (uc->length > uc->max_length) {
548 uc->max_length = uc->length;
549 if (uc->max_length > max_length)
550 max_length = uc->max_length;
554 uq->uq_spare_queue = NULL;
556 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
558 uq->uq_flags |= UQF_UMTXQ;
559 uq->uq_cur_queue = uh;
564 umtxq_remove_queue(struct umtx_q *uq, int q)
566 struct umtxq_chain *uc;
567 struct umtxq_queue *uh;
569 uc = umtxq_getchain(&uq->uq_key);
570 UMTXQ_LOCKED_ASSERT(uc);
571 if (uq->uq_flags & UQF_UMTXQ) {
572 uh = uq->uq_cur_queue;
573 TAILQ_REMOVE(&uh->head, uq, uq_link);
575 uq->uq_flags &= ~UQF_UMTXQ;
576 if (TAILQ_EMPTY(&uh->head)) {
577 KASSERT(uh->length == 0,
578 ("inconsistent umtxq_queue length"));
579 #ifdef UMTX_PROFILING
582 LIST_REMOVE(uh, link);
584 uh = LIST_FIRST(&uc->uc_spare_queue);
585 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
586 LIST_REMOVE(uh, link);
588 uq->uq_spare_queue = uh;
589 uq->uq_cur_queue = NULL;
594 * Check if there are multiple waiters
597 umtxq_count(struct umtx_key *key)
599 struct umtxq_chain *uc;
600 struct umtxq_queue *uh;
602 uc = umtxq_getchain(key);
603 UMTXQ_LOCKED_ASSERT(uc);
604 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
611 * Check if there are multiple PI waiters and returns first
615 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
617 struct umtxq_chain *uc;
618 struct umtxq_queue *uh;
621 uc = umtxq_getchain(key);
622 UMTXQ_LOCKED_ASSERT(uc);
623 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
625 *first = TAILQ_FIRST(&uh->head);
632 umtxq_check_susp(struct thread *td)
638 * The check for TDF_NEEDSUSPCHK is racy, but it is enough to
639 * eventually break the lockstep loop.
641 if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
646 if (P_SHOULDSTOP(p) ||
647 ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) {
648 if (p->p_flag & P_SINGLE_EXIT)
658 * Wake up threads waiting on an userland object.
662 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
664 struct umtxq_chain *uc;
665 struct umtxq_queue *uh;
670 uc = umtxq_getchain(key);
671 UMTXQ_LOCKED_ASSERT(uc);
672 uh = umtxq_queue_lookup(key, q);
674 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
675 umtxq_remove_queue(uq, q);
686 * Wake up specified thread.
689 umtxq_signal_thread(struct umtx_q *uq)
691 struct umtxq_chain *uc;
693 uc = umtxq_getchain(&uq->uq_key);
694 UMTXQ_LOCKED_ASSERT(uc);
700 tstohz(const struct timespec *tsp)
704 TIMESPEC_TO_TIMEVAL(&tv, tsp);
709 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
710 const struct timespec *timeout)
713 timo->clockid = clockid;
715 kern_clock_gettime(curthread, clockid, &timo->end);
716 timo->cur = timo->end;
717 timespecadd(&timo->end, timeout);
719 timo->end = *timeout;
720 kern_clock_gettime(curthread, clockid, &timo->cur);
725 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
728 abs_timeout_init(timo, umtxtime->_clockid,
729 (umtxtime->_flags & UMTX_ABSTIME) != 0,
730 &umtxtime->_timeout);
734 abs_timeout_update(struct abs_timeout *timo)
736 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
740 abs_timeout_gethz(struct abs_timeout *timo)
744 if (timespeccmp(&timo->end, &timo->cur, <=))
747 timespecsub(&tts, &timo->cur);
748 return (tstohz(&tts));
752 * Put thread into sleep state, before sleeping, check if
753 * thread was removed from umtx queue.
756 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
758 struct umtxq_chain *uc;
761 uc = umtxq_getchain(&uq->uq_key);
762 UMTXQ_LOCKED_ASSERT(uc);
764 if (!(uq->uq_flags & UQF_UMTXQ))
766 if (abstime != NULL) {
767 timo = abs_timeout_gethz(abstime);
772 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
773 if (error != EWOULDBLOCK) {
774 umtxq_lock(&uq->uq_key);
778 abs_timeout_update(abstime);
779 umtxq_lock(&uq->uq_key);
785 * Convert userspace address into unique logical address.
788 umtx_key_get(void *addr, int type, int share, struct umtx_key *key)
790 struct thread *td = curthread;
792 vm_map_entry_t entry;
798 if (share == THREAD_SHARE) {
800 key->info.private.vs = td->td_proc->p_vmspace;
801 key->info.private.addr = (uintptr_t)addr;
803 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
804 map = &td->td_proc->p_vmspace->vm_map;
805 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
806 &entry, &key->info.shared.object, &pindex, &prot,
807 &wired) != KERN_SUCCESS) {
811 if ((share == PROCESS_SHARE) ||
812 (share == AUTO_SHARE &&
813 VM_INHERIT_SHARE == entry->inheritance)) {
815 key->info.shared.offset = entry->offset + entry->start -
817 vm_object_reference(key->info.shared.object);
820 key->info.private.vs = td->td_proc->p_vmspace;
821 key->info.private.addr = (uintptr_t)addr;
823 vm_map_lookup_done(map, entry);
834 umtx_key_release(struct umtx_key *key)
837 vm_object_deallocate(key->info.shared.object);
841 * Fetch and compare value, sleep on the address if value is not changed.
844 do_wait(struct thread *td, void *addr, u_long id,
845 struct _umtx_time *timeout, int compat32, int is_private)
847 struct abs_timeout timo;
853 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
854 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
858 abs_timeout_init2(&timo, timeout);
860 umtxq_lock(&uq->uq_key);
862 umtxq_unlock(&uq->uq_key);
866 tmp = (unsigned int)fuword32(addr);
867 umtxq_lock(&uq->uq_key);
869 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
871 if ((uq->uq_flags & UQF_UMTXQ) == 0)
875 umtxq_unlock(&uq->uq_key);
876 umtx_key_release(&uq->uq_key);
877 if (error == ERESTART)
883 * Wake up threads sleeping on the specified address.
886 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
891 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
892 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
895 ret = umtxq_signal(&key, n_wake);
897 umtx_key_release(&key);
902 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
905 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
906 struct _umtx_time *timeout, int mode)
908 struct abs_timeout timo;
910 uint32_t owner, old, id;
917 abs_timeout_init2(&timo, timeout);
920 * Care must be exercised when dealing with umtx structure. It
921 * can fault on any access.
924 owner = fuword32(__DEVOLATILE(void *, &m->m_owner));
925 if (mode == _UMUTEX_WAIT) {
926 if (owner == UMUTEX_UNOWNED || owner == UMUTEX_CONTESTED)
930 * Try the uncontested case. This should be done in userland.
932 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id);
934 /* The acquire succeeded. */
935 if (owner == UMUTEX_UNOWNED)
938 /* The address was invalid. */
942 /* If no one owns it but it is contested try to acquire it. */
943 if (owner == UMUTEX_CONTESTED) {
944 owner = casuword32(&m->m_owner,
945 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
947 if (owner == UMUTEX_CONTESTED)
950 /* The address was invalid. */
954 error = umtxq_check_susp(td);
958 /* If this failed the lock has changed, restart. */
963 if (mode == _UMUTEX_TRY)
967 * If we caught a signal, we have retried and now
973 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
974 GET_SHARE(flags), &uq->uq_key)) != 0)
977 umtxq_lock(&uq->uq_key);
978 umtxq_busy(&uq->uq_key);
980 umtxq_unlock(&uq->uq_key);
983 * Set the contested bit so that a release in user space
984 * knows to use the system call for unlock. If this fails
985 * either some one else has acquired the lock or it has been
988 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED);
990 /* The address was invalid. */
992 umtxq_lock(&uq->uq_key);
994 umtxq_unbusy(&uq->uq_key);
995 umtxq_unlock(&uq->uq_key);
996 umtx_key_release(&uq->uq_key);
1001 * We set the contested bit, sleep. Otherwise the lock changed
1002 * and we need to retry or we lost a race to the thread
1003 * unlocking the umtx.
1005 umtxq_lock(&uq->uq_key);
1006 umtxq_unbusy(&uq->uq_key);
1008 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1011 umtxq_unlock(&uq->uq_key);
1012 umtx_key_release(&uq->uq_key);
1015 error = umtxq_check_susp(td);
1022 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1025 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags)
1027 struct umtx_key key;
1028 uint32_t owner, old, id;
1034 * Make sure we own this mtx.
1036 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1040 if ((owner & ~UMUTEX_CONTESTED) != id)
1043 if ((owner & UMUTEX_CONTESTED) == 0) {
1044 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED);
1052 /* We should only ever be in here for contested locks */
1053 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1059 count = umtxq_count(&key);
1063 * When unlocking the umtx, it must be marked as unowned if
1064 * there is zero or one thread only waiting for it.
1065 * Otherwise, it must be marked as contested.
1067 old = casuword32(&m->m_owner, owner,
1068 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1070 umtxq_signal(&key,1);
1073 umtx_key_release(&key);
1082 * Check if the mutex is available and wake up a waiter,
1083 * only for simple mutex.
1086 do_wake_umutex(struct thread *td, struct umutex *m)
1088 struct umtx_key key;
1094 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1098 if ((owner & ~UMUTEX_CONTESTED) != 0)
1101 flags = fuword32(&m->m_flags);
1103 /* We should only ever be in here for contested locks */
1104 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1110 count = umtxq_count(&key);
1114 owner = casuword32(&m->m_owner, UMUTEX_CONTESTED, UMUTEX_UNOWNED);
1117 if (count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1118 umtxq_signal(&key, 1);
1121 umtx_key_release(&key);
1126 * Check if the mutex has waiters and tries to fix contention bit.
1129 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1131 struct umtx_key key;
1132 uint32_t owner, old;
1137 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
1139 type = TYPE_NORMAL_UMUTEX;
1141 case UMUTEX_PRIO_INHERIT:
1142 type = TYPE_PI_UMUTEX;
1144 case UMUTEX_PRIO_PROTECT:
1145 type = TYPE_PP_UMUTEX;
1150 if ((error = umtx_key_get(m, type, GET_SHARE(flags),
1157 count = umtxq_count(&key);
1160 * Only repair contention bit if there is a waiter, this means the mutex
1161 * is still being referenced by userland code, otherwise don't update
1165 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1166 while ((owner & UMUTEX_CONTESTED) ==0) {
1167 old = casuword32(&m->m_owner, owner,
1168 owner|UMUTEX_CONTESTED);
1174 error = umtxq_check_susp(td);
1178 } else if (count == 1) {
1179 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1180 while ((owner & ~UMUTEX_CONTESTED) != 0 &&
1181 (owner & UMUTEX_CONTESTED) == 0) {
1182 old = casuword32(&m->m_owner, owner,
1183 owner|UMUTEX_CONTESTED);
1189 error = umtxq_check_susp(td);
1197 umtxq_signal(&key, INT_MAX);
1199 else if (count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1200 umtxq_signal(&key, 1);
1203 umtx_key_release(&key);
1207 static inline struct umtx_pi *
1208 umtx_pi_alloc(int flags)
1212 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1213 TAILQ_INIT(&pi->pi_blocked);
1214 atomic_add_int(&umtx_pi_allocated, 1);
1219 umtx_pi_free(struct umtx_pi *pi)
1221 uma_zfree(umtx_pi_zone, pi);
1222 atomic_add_int(&umtx_pi_allocated, -1);
1226 * Adjust the thread's position on a pi_state after its priority has been
1230 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1232 struct umtx_q *uq, *uq1, *uq2;
1235 mtx_assert(&umtx_lock, MA_OWNED);
1242 * Check if the thread needs to be moved on the blocked chain.
1243 * It needs to be moved if either its priority is lower than
1244 * the previous thread or higher than the next thread.
1246 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1247 uq2 = TAILQ_NEXT(uq, uq_lockq);
1248 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1249 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1251 * Remove thread from blocked chain and determine where
1252 * it should be moved to.
1254 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1255 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1256 td1 = uq1->uq_thread;
1257 MPASS(td1->td_proc->p_magic == P_MAGIC);
1258 if (UPRI(td1) > UPRI(td))
1263 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1265 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1271 * Propagate priority when a thread is blocked on POSIX
1275 umtx_propagate_priority(struct thread *td)
1281 mtx_assert(&umtx_lock, MA_OWNED);
1284 pi = uq->uq_pi_blocked;
1290 if (td == NULL || td == curthread)
1293 MPASS(td->td_proc != NULL);
1294 MPASS(td->td_proc->p_magic == P_MAGIC);
1297 if (td->td_lend_user_pri > pri)
1298 sched_lend_user_prio(td, pri);
1306 * Pick up the lock that td is blocked on.
1309 pi = uq->uq_pi_blocked;
1312 /* Resort td on the list if needed. */
1313 umtx_pi_adjust_thread(pi, td);
1318 * Unpropagate priority for a PI mutex when a thread blocked on
1319 * it is interrupted by signal or resumed by others.
1322 umtx_repropagate_priority(struct umtx_pi *pi)
1324 struct umtx_q *uq, *uq_owner;
1325 struct umtx_pi *pi2;
1328 mtx_assert(&umtx_lock, MA_OWNED);
1330 while (pi != NULL && pi->pi_owner != NULL) {
1332 uq_owner = pi->pi_owner->td_umtxq;
1334 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1335 uq = TAILQ_FIRST(&pi2->pi_blocked);
1337 if (pri > UPRI(uq->uq_thread))
1338 pri = UPRI(uq->uq_thread);
1342 if (pri > uq_owner->uq_inherited_pri)
1343 pri = uq_owner->uq_inherited_pri;
1344 thread_lock(pi->pi_owner);
1345 sched_lend_user_prio(pi->pi_owner, pri);
1346 thread_unlock(pi->pi_owner);
1347 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1348 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1353 * Insert a PI mutex into owned list.
1356 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1358 struct umtx_q *uq_owner;
1360 uq_owner = owner->td_umtxq;
1361 mtx_assert(&umtx_lock, MA_OWNED);
1362 if (pi->pi_owner != NULL)
1363 panic("pi_ower != NULL");
1364 pi->pi_owner = owner;
1365 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1369 * Claim ownership of a PI mutex.
1372 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1374 struct umtx_q *uq, *uq_owner;
1376 uq_owner = owner->td_umtxq;
1377 mtx_lock_spin(&umtx_lock);
1378 if (pi->pi_owner == owner) {
1379 mtx_unlock_spin(&umtx_lock);
1383 if (pi->pi_owner != NULL) {
1385 * userland may have already messed the mutex, sigh.
1387 mtx_unlock_spin(&umtx_lock);
1390 umtx_pi_setowner(pi, owner);
1391 uq = TAILQ_FIRST(&pi->pi_blocked);
1395 pri = UPRI(uq->uq_thread);
1397 if (pri < UPRI(owner))
1398 sched_lend_user_prio(owner, pri);
1399 thread_unlock(owner);
1401 mtx_unlock_spin(&umtx_lock);
1406 * Adjust a thread's order position in its blocked PI mutex,
1407 * this may result new priority propagating process.
1410 umtx_pi_adjust(struct thread *td, u_char oldpri)
1416 mtx_lock_spin(&umtx_lock);
1418 * Pick up the lock that td is blocked on.
1420 pi = uq->uq_pi_blocked;
1422 umtx_pi_adjust_thread(pi, td);
1423 umtx_repropagate_priority(pi);
1425 mtx_unlock_spin(&umtx_lock);
1429 * Sleep on a PI mutex.
1432 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi,
1433 uint32_t owner, const char *wmesg, struct abs_timeout *timo)
1435 struct umtxq_chain *uc;
1436 struct thread *td, *td1;
1442 KASSERT(td == curthread, ("inconsistent uq_thread"));
1443 uc = umtxq_getchain(&uq->uq_key);
1444 UMTXQ_LOCKED_ASSERT(uc);
1445 UMTXQ_BUSY_ASSERT(uc);
1447 mtx_lock_spin(&umtx_lock);
1448 if (pi->pi_owner == NULL) {
1449 mtx_unlock_spin(&umtx_lock);
1450 /* XXX Only look up thread in current process. */
1451 td1 = tdfind(owner, curproc->p_pid);
1452 mtx_lock_spin(&umtx_lock);
1454 if (pi->pi_owner == NULL)
1455 umtx_pi_setowner(pi, td1);
1456 PROC_UNLOCK(td1->td_proc);
1460 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1461 pri = UPRI(uq1->uq_thread);
1467 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1469 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1471 uq->uq_pi_blocked = pi;
1473 td->td_flags |= TDF_UPIBLOCKED;
1475 umtx_propagate_priority(td);
1476 mtx_unlock_spin(&umtx_lock);
1477 umtxq_unbusy(&uq->uq_key);
1479 error = umtxq_sleep(uq, wmesg, timo);
1482 mtx_lock_spin(&umtx_lock);
1483 uq->uq_pi_blocked = NULL;
1485 td->td_flags &= ~TDF_UPIBLOCKED;
1487 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1488 umtx_repropagate_priority(pi);
1489 mtx_unlock_spin(&umtx_lock);
1490 umtxq_unlock(&uq->uq_key);
1496 * Add reference count for a PI mutex.
1499 umtx_pi_ref(struct umtx_pi *pi)
1501 struct umtxq_chain *uc;
1503 uc = umtxq_getchain(&pi->pi_key);
1504 UMTXQ_LOCKED_ASSERT(uc);
1509 * Decrease reference count for a PI mutex, if the counter
1510 * is decreased to zero, its memory space is freed.
1513 umtx_pi_unref(struct umtx_pi *pi)
1515 struct umtxq_chain *uc;
1517 uc = umtxq_getchain(&pi->pi_key);
1518 UMTXQ_LOCKED_ASSERT(uc);
1519 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1520 if (--pi->pi_refcount == 0) {
1521 mtx_lock_spin(&umtx_lock);
1522 if (pi->pi_owner != NULL) {
1523 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested,
1525 pi->pi_owner = NULL;
1527 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1528 ("blocked queue not empty"));
1529 mtx_unlock_spin(&umtx_lock);
1530 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1536 * Find a PI mutex in hash table.
1538 static struct umtx_pi *
1539 umtx_pi_lookup(struct umtx_key *key)
1541 struct umtxq_chain *uc;
1544 uc = umtxq_getchain(key);
1545 UMTXQ_LOCKED_ASSERT(uc);
1547 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1548 if (umtx_key_match(&pi->pi_key, key)) {
1556 * Insert a PI mutex into hash table.
1559 umtx_pi_insert(struct umtx_pi *pi)
1561 struct umtxq_chain *uc;
1563 uc = umtxq_getchain(&pi->pi_key);
1564 UMTXQ_LOCKED_ASSERT(uc);
1565 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1572 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1573 struct _umtx_time *timeout, int try)
1575 struct abs_timeout timo;
1577 struct umtx_pi *pi, *new_pi;
1578 uint32_t id, owner, old;
1584 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1588 if (timeout != NULL)
1589 abs_timeout_init2(&timo, timeout);
1591 umtxq_lock(&uq->uq_key);
1592 pi = umtx_pi_lookup(&uq->uq_key);
1594 new_pi = umtx_pi_alloc(M_NOWAIT);
1595 if (new_pi == NULL) {
1596 umtxq_unlock(&uq->uq_key);
1597 new_pi = umtx_pi_alloc(M_WAITOK);
1598 umtxq_lock(&uq->uq_key);
1599 pi = umtx_pi_lookup(&uq->uq_key);
1601 umtx_pi_free(new_pi);
1605 if (new_pi != NULL) {
1606 new_pi->pi_key = uq->uq_key;
1607 umtx_pi_insert(new_pi);
1612 umtxq_unlock(&uq->uq_key);
1615 * Care must be exercised when dealing with umtx structure. It
1616 * can fault on any access.
1620 * Try the uncontested case. This should be done in userland.
1622 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id);
1624 /* The acquire succeeded. */
1625 if (owner == UMUTEX_UNOWNED) {
1630 /* The address was invalid. */
1636 /* If no one owns it but it is contested try to acquire it. */
1637 if (owner == UMUTEX_CONTESTED) {
1638 owner = casuword32(&m->m_owner,
1639 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1641 if (owner == UMUTEX_CONTESTED) {
1642 umtxq_lock(&uq->uq_key);
1643 umtxq_busy(&uq->uq_key);
1644 error = umtx_pi_claim(pi, td);
1645 umtxq_unbusy(&uq->uq_key);
1646 umtxq_unlock(&uq->uq_key);
1650 /* The address was invalid. */
1656 error = umtxq_check_susp(td);
1660 /* If this failed the lock has changed, restart. */
1670 * If we caught a signal, we have retried and now
1676 umtxq_lock(&uq->uq_key);
1677 umtxq_busy(&uq->uq_key);
1678 umtxq_unlock(&uq->uq_key);
1681 * Set the contested bit so that a release in user space
1682 * knows to use the system call for unlock. If this fails
1683 * either some one else has acquired the lock or it has been
1686 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED);
1688 /* The address was invalid. */
1690 umtxq_lock(&uq->uq_key);
1691 umtxq_unbusy(&uq->uq_key);
1692 umtxq_unlock(&uq->uq_key);
1697 umtxq_lock(&uq->uq_key);
1699 * We set the contested bit, sleep. Otherwise the lock changed
1700 * and we need to retry or we lost a race to the thread
1701 * unlocking the umtx.
1704 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
1705 "umtxpi", timeout == NULL ? NULL : &timo);
1709 umtxq_unbusy(&uq->uq_key);
1710 umtxq_unlock(&uq->uq_key);
1713 error = umtxq_check_susp(td);
1718 umtxq_lock(&uq->uq_key);
1720 umtxq_unlock(&uq->uq_key);
1722 umtx_key_release(&uq->uq_key);
1727 * Unlock a PI mutex.
1730 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags)
1732 struct umtx_key key;
1733 struct umtx_q *uq_first, *uq_first2, *uq_me;
1734 struct umtx_pi *pi, *pi2;
1735 uint32_t owner, old, id;
1742 * Make sure we own this mtx.
1744 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1748 if ((owner & ~UMUTEX_CONTESTED) != id)
1751 /* This should be done in userland */
1752 if ((owner & UMUTEX_CONTESTED) == 0) {
1753 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED);
1761 /* We should only ever be in here for contested locks */
1762 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1768 count = umtxq_count_pi(&key, &uq_first);
1769 if (uq_first != NULL) {
1770 mtx_lock_spin(&umtx_lock);
1771 pi = uq_first->uq_pi_blocked;
1772 KASSERT(pi != NULL, ("pi == NULL?"));
1773 if (pi->pi_owner != curthread) {
1774 mtx_unlock_spin(&umtx_lock);
1777 umtx_key_release(&key);
1778 /* userland messed the mutex */
1781 uq_me = curthread->td_umtxq;
1782 pi->pi_owner = NULL;
1783 TAILQ_REMOVE(&uq_me->uq_pi_contested, pi, pi_link);
1784 /* get highest priority thread which is still sleeping. */
1785 uq_first = TAILQ_FIRST(&pi->pi_blocked);
1786 while (uq_first != NULL &&
1787 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
1788 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
1791 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
1792 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
1793 if (uq_first2 != NULL) {
1794 if (pri > UPRI(uq_first2->uq_thread))
1795 pri = UPRI(uq_first2->uq_thread);
1798 thread_lock(curthread);
1799 sched_lend_user_prio(curthread, pri);
1800 thread_unlock(curthread);
1801 mtx_unlock_spin(&umtx_lock);
1803 umtxq_signal_thread(uq_first);
1808 * When unlocking the umtx, it must be marked as unowned if
1809 * there is zero or one thread only waiting for it.
1810 * Otherwise, it must be marked as contested.
1812 old = casuword32(&m->m_owner, owner,
1813 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1818 umtx_key_release(&key);
1830 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
1831 struct _umtx_time *timeout, int try)
1833 struct abs_timeout timo;
1834 struct umtx_q *uq, *uq2;
1838 int error, pri, old_inherited_pri, su;
1842 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
1846 if (timeout != NULL)
1847 abs_timeout_init2(&timo, timeout);
1849 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
1851 old_inherited_pri = uq->uq_inherited_pri;
1852 umtxq_lock(&uq->uq_key);
1853 umtxq_busy(&uq->uq_key);
1854 umtxq_unlock(&uq->uq_key);
1856 ceiling = RTP_PRIO_MAX - fuword32(&m->m_ceilings[0]);
1857 if (ceiling > RTP_PRIO_MAX) {
1862 mtx_lock_spin(&umtx_lock);
1863 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
1864 mtx_unlock_spin(&umtx_lock);
1868 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
1869 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
1871 if (uq->uq_inherited_pri < UPRI(td))
1872 sched_lend_user_prio(td, uq->uq_inherited_pri);
1875 mtx_unlock_spin(&umtx_lock);
1877 owner = casuword32(&m->m_owner,
1878 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1880 if (owner == UMUTEX_CONTESTED) {
1885 /* The address was invalid. */
1897 * If we caught a signal, we have retried and now
1903 umtxq_lock(&uq->uq_key);
1905 umtxq_unbusy(&uq->uq_key);
1906 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
1909 umtxq_unlock(&uq->uq_key);
1911 mtx_lock_spin(&umtx_lock);
1912 uq->uq_inherited_pri = old_inherited_pri;
1914 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
1915 uq2 = TAILQ_FIRST(&pi->pi_blocked);
1917 if (pri > UPRI(uq2->uq_thread))
1918 pri = UPRI(uq2->uq_thread);
1921 if (pri > uq->uq_inherited_pri)
1922 pri = uq->uq_inherited_pri;
1924 sched_lend_user_prio(td, pri);
1926 mtx_unlock_spin(&umtx_lock);
1930 mtx_lock_spin(&umtx_lock);
1931 uq->uq_inherited_pri = old_inherited_pri;
1933 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
1934 uq2 = TAILQ_FIRST(&pi->pi_blocked);
1936 if (pri > UPRI(uq2->uq_thread))
1937 pri = UPRI(uq2->uq_thread);
1940 if (pri > uq->uq_inherited_pri)
1941 pri = uq->uq_inherited_pri;
1943 sched_lend_user_prio(td, pri);
1945 mtx_unlock_spin(&umtx_lock);
1949 umtxq_lock(&uq->uq_key);
1950 umtxq_unbusy(&uq->uq_key);
1951 umtxq_unlock(&uq->uq_key);
1952 umtx_key_release(&uq->uq_key);
1957 * Unlock a PP mutex.
1960 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags)
1962 struct umtx_key key;
1963 struct umtx_q *uq, *uq2;
1967 int error, pri, new_inherited_pri, su;
1971 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
1974 * Make sure we own this mtx.
1976 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1980 if ((owner & ~UMUTEX_CONTESTED) != id)
1983 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
1988 new_inherited_pri = PRI_MAX;
1990 rceiling = RTP_PRIO_MAX - rceiling;
1991 if (rceiling > RTP_PRIO_MAX)
1993 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
1996 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2003 * For priority protected mutex, always set unlocked state
2004 * to UMUTEX_CONTESTED, so that userland always enters kernel
2005 * to lock the mutex, it is necessary because thread priority
2006 * has to be adjusted for such mutex.
2008 error = suword32(__DEVOLATILE(uint32_t *, &m->m_owner),
2013 umtxq_signal(&key, 1);
2020 mtx_lock_spin(&umtx_lock);
2022 uq->uq_inherited_pri = new_inherited_pri;
2024 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2025 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2027 if (pri > UPRI(uq2->uq_thread))
2028 pri = UPRI(uq2->uq_thread);
2031 if (pri > uq->uq_inherited_pri)
2032 pri = uq->uq_inherited_pri;
2034 sched_lend_user_prio(td, pri);
2036 mtx_unlock_spin(&umtx_lock);
2038 umtx_key_release(&key);
2043 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2044 uint32_t *old_ceiling)
2047 uint32_t save_ceiling;
2052 flags = fuword32(&m->m_flags);
2053 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2055 if (ceiling > RTP_PRIO_MAX)
2059 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2063 umtxq_lock(&uq->uq_key);
2064 umtxq_busy(&uq->uq_key);
2065 umtxq_unlock(&uq->uq_key);
2067 save_ceiling = fuword32(&m->m_ceilings[0]);
2069 owner = casuword32(&m->m_owner,
2070 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
2072 if (owner == UMUTEX_CONTESTED) {
2073 suword32(&m->m_ceilings[0], ceiling);
2074 suword32(__DEVOLATILE(uint32_t *, &m->m_owner),
2080 /* The address was invalid. */
2086 if ((owner & ~UMUTEX_CONTESTED) == id) {
2087 suword32(&m->m_ceilings[0], ceiling);
2093 * If we caught a signal, we have retried and now
2100 * We set the contested bit, sleep. Otherwise the lock changed
2101 * and we need to retry or we lost a race to the thread
2102 * unlocking the umtx.
2104 umtxq_lock(&uq->uq_key);
2106 umtxq_unbusy(&uq->uq_key);
2107 error = umtxq_sleep(uq, "umtxpp", NULL);
2109 umtxq_unlock(&uq->uq_key);
2111 umtxq_lock(&uq->uq_key);
2113 umtxq_signal(&uq->uq_key, INT_MAX);
2114 umtxq_unbusy(&uq->uq_key);
2115 umtxq_unlock(&uq->uq_key);
2116 umtx_key_release(&uq->uq_key);
2117 if (error == 0 && old_ceiling != NULL)
2118 suword32(old_ceiling, save_ceiling);
2123 * Lock a userland POSIX mutex.
2126 do_lock_umutex(struct thread *td, struct umutex *m,
2127 struct _umtx_time *timeout, int mode)
2132 flags = fuword32(&m->m_flags);
2136 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2138 error = do_lock_normal(td, m, flags, timeout, mode);
2140 case UMUTEX_PRIO_INHERIT:
2141 error = do_lock_pi(td, m, flags, timeout, mode);
2143 case UMUTEX_PRIO_PROTECT:
2144 error = do_lock_pp(td, m, flags, timeout, mode);
2149 if (timeout == NULL) {
2150 if (error == EINTR && mode != _UMUTEX_WAIT)
2153 /* Timed-locking is not restarted. */
2154 if (error == ERESTART)
2161 * Unlock a userland POSIX mutex.
2164 do_unlock_umutex(struct thread *td, struct umutex *m)
2168 flags = fuword32(&m->m_flags);
2172 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2174 return (do_unlock_normal(td, m, flags));
2175 case UMUTEX_PRIO_INHERIT:
2176 return (do_unlock_pi(td, m, flags));
2177 case UMUTEX_PRIO_PROTECT:
2178 return (do_unlock_pp(td, m, flags));
2185 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2186 struct timespec *timeout, u_long wflags)
2188 struct abs_timeout timo;
2195 flags = fuword32(&cv->c_flags);
2196 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2200 if ((wflags & CVWAIT_CLOCKID) != 0) {
2201 clockid = fuword32(&cv->c_clockid);
2202 if (clockid < CLOCK_REALTIME ||
2203 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2204 /* hmm, only HW clock id will work. */
2208 clockid = CLOCK_REALTIME;
2211 umtxq_lock(&uq->uq_key);
2212 umtxq_busy(&uq->uq_key);
2214 umtxq_unlock(&uq->uq_key);
2217 * Set c_has_waiters to 1 before releasing user mutex, also
2218 * don't modify cache line when unnecessary.
2220 if (fuword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters)) == 0)
2221 suword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters), 1);
2223 umtxq_lock(&uq->uq_key);
2224 umtxq_unbusy(&uq->uq_key);
2225 umtxq_unlock(&uq->uq_key);
2227 error = do_unlock_umutex(td, m);
2229 if (timeout != NULL)
2230 abs_timeout_init(&timo, clockid, ((wflags & CVWAIT_ABSTIME) != 0),
2233 umtxq_lock(&uq->uq_key);
2235 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2239 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2243 * This must be timeout,interrupted by signal or
2244 * surprious wakeup, clear c_has_waiter flag when
2247 umtxq_busy(&uq->uq_key);
2248 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2249 int oldlen = uq->uq_cur_queue->length;
2252 umtxq_unlock(&uq->uq_key);
2254 __DEVOLATILE(uint32_t *,
2255 &cv->c_has_waiters), 0);
2256 umtxq_lock(&uq->uq_key);
2259 umtxq_unbusy(&uq->uq_key);
2260 if (error == ERESTART)
2264 umtxq_unlock(&uq->uq_key);
2265 umtx_key_release(&uq->uq_key);
2270 * Signal a userland condition variable.
2273 do_cv_signal(struct thread *td, struct ucond *cv)
2275 struct umtx_key key;
2276 int error, cnt, nwake;
2279 flags = fuword32(&cv->c_flags);
2280 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2284 cnt = umtxq_count(&key);
2285 nwake = umtxq_signal(&key, 1);
2289 __DEVOLATILE(uint32_t *, &cv->c_has_waiters), 0);
2294 umtx_key_release(&key);
2299 do_cv_broadcast(struct thread *td, struct ucond *cv)
2301 struct umtx_key key;
2305 flags = fuword32(&cv->c_flags);
2306 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2311 umtxq_signal(&key, INT_MAX);
2314 error = suword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters), 0);
2320 umtx_key_release(&key);
2325 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, struct _umtx_time *timeout)
2327 struct abs_timeout timo;
2329 uint32_t flags, wrflags;
2330 int32_t state, oldstate;
2331 int32_t blocked_readers;
2335 flags = fuword32(&rwlock->rw_flags);
2336 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2340 if (timeout != NULL)
2341 abs_timeout_init2(&timo, timeout);
2343 wrflags = URWLOCK_WRITE_OWNER;
2344 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2345 wrflags |= URWLOCK_WRITE_WAITERS;
2348 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2349 /* try to lock it */
2350 while (!(state & wrflags)) {
2351 if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) {
2352 umtx_key_release(&uq->uq_key);
2355 oldstate = casuword32(&rwlock->rw_state, state, state + 1);
2356 if (oldstate == -1) {
2357 umtx_key_release(&uq->uq_key);
2360 if (oldstate == state) {
2361 umtx_key_release(&uq->uq_key);
2364 error = umtxq_check_susp(td);
2373 /* grab monitor lock */
2374 umtxq_lock(&uq->uq_key);
2375 umtxq_busy(&uq->uq_key);
2376 umtxq_unlock(&uq->uq_key);
2379 * re-read the state, in case it changed between the try-lock above
2380 * and the check below
2382 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2384 /* set read contention bit */
2385 while ((state & wrflags) && !(state & URWLOCK_READ_WAITERS)) {
2386 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_READ_WAITERS);
2387 if (oldstate == -1) {
2391 if (oldstate == state)
2394 error = umtxq_check_susp(td);
2399 umtxq_lock(&uq->uq_key);
2400 umtxq_unbusy(&uq->uq_key);
2401 umtxq_unlock(&uq->uq_key);
2405 /* state is changed while setting flags, restart */
2406 if (!(state & wrflags)) {
2407 umtxq_lock(&uq->uq_key);
2408 umtxq_unbusy(&uq->uq_key);
2409 umtxq_unlock(&uq->uq_key);
2410 error = umtxq_check_susp(td);
2417 /* contention bit is set, before sleeping, increase read waiter count */
2418 blocked_readers = fuword32(&rwlock->rw_blocked_readers);
2419 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2421 while (state & wrflags) {
2422 umtxq_lock(&uq->uq_key);
2424 umtxq_unbusy(&uq->uq_key);
2426 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2429 umtxq_busy(&uq->uq_key);
2431 umtxq_unlock(&uq->uq_key);
2434 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2437 /* decrease read waiter count, and may clear read contention bit */
2438 blocked_readers = fuword32(&rwlock->rw_blocked_readers);
2439 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2440 if (blocked_readers == 1) {
2441 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2443 oldstate = casuword32(&rwlock->rw_state, state,
2444 state & ~URWLOCK_READ_WAITERS);
2445 if (oldstate == -1) {
2449 if (oldstate == state)
2452 error = umtxq_check_susp(td);
2458 umtxq_lock(&uq->uq_key);
2459 umtxq_unbusy(&uq->uq_key);
2460 umtxq_unlock(&uq->uq_key);
2464 umtx_key_release(&uq->uq_key);
2465 if (error == ERESTART)
2471 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2473 struct abs_timeout timo;
2476 int32_t state, oldstate;
2477 int32_t blocked_writers;
2478 int32_t blocked_readers;
2482 flags = fuword32(&rwlock->rw_flags);
2483 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2487 if (timeout != NULL)
2488 abs_timeout_init2(&timo, timeout);
2490 blocked_readers = 0;
2492 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2493 while (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2494 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_WRITE_OWNER);
2495 if (oldstate == -1) {
2496 umtx_key_release(&uq->uq_key);
2499 if (oldstate == state) {
2500 umtx_key_release(&uq->uq_key);
2504 error = umtxq_check_susp(td);
2510 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) &&
2511 blocked_readers != 0) {
2512 umtxq_lock(&uq->uq_key);
2513 umtxq_busy(&uq->uq_key);
2514 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE);
2515 umtxq_unbusy(&uq->uq_key);
2516 umtxq_unlock(&uq->uq_key);
2522 /* grab monitor lock */
2523 umtxq_lock(&uq->uq_key);
2524 umtxq_busy(&uq->uq_key);
2525 umtxq_unlock(&uq->uq_key);
2528 * re-read the state, in case it changed between the try-lock above
2529 * and the check below
2531 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2533 while (((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) &&
2534 (state & URWLOCK_WRITE_WAITERS) == 0) {
2535 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_WRITE_WAITERS);
2536 if (oldstate == -1) {
2540 if (oldstate == state)
2543 error = umtxq_check_susp(td);
2548 umtxq_lock(&uq->uq_key);
2549 umtxq_unbusy(&uq->uq_key);
2550 umtxq_unlock(&uq->uq_key);
2554 if (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2555 umtxq_lock(&uq->uq_key);
2556 umtxq_unbusy(&uq->uq_key);
2557 umtxq_unlock(&uq->uq_key);
2558 error = umtxq_check_susp(td);
2564 blocked_writers = fuword32(&rwlock->rw_blocked_writers);
2565 suword32(&rwlock->rw_blocked_writers, blocked_writers+1);
2567 while ((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) {
2568 umtxq_lock(&uq->uq_key);
2569 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2570 umtxq_unbusy(&uq->uq_key);
2572 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
2575 umtxq_busy(&uq->uq_key);
2576 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2577 umtxq_unlock(&uq->uq_key);
2580 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2583 blocked_writers = fuword32(&rwlock->rw_blocked_writers);
2584 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
2585 if (blocked_writers == 1) {
2586 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2588 oldstate = casuword32(&rwlock->rw_state, state,
2589 state & ~URWLOCK_WRITE_WAITERS);
2590 if (oldstate == -1) {
2594 if (oldstate == state)
2597 error = umtxq_check_susp(td);
2599 * We are leaving the URWLOCK_WRITE_WAITERS
2600 * behind, but this should not harm the
2606 blocked_readers = fuword32(&rwlock->rw_blocked_readers);
2608 blocked_readers = 0;
2610 umtxq_lock(&uq->uq_key);
2611 umtxq_unbusy(&uq->uq_key);
2612 umtxq_unlock(&uq->uq_key);
2615 umtx_key_release(&uq->uq_key);
2616 if (error == ERESTART)
2622 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
2626 int32_t state, oldstate;
2627 int error, q, count;
2630 flags = fuword32(&rwlock->rw_flags);
2631 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2635 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2636 if (state & URWLOCK_WRITE_OWNER) {
2638 oldstate = casuword32(&rwlock->rw_state, state,
2639 state & ~URWLOCK_WRITE_OWNER);
2640 if (oldstate == -1) {
2644 if (oldstate != state) {
2646 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
2650 error = umtxq_check_susp(td);
2656 } else if (URWLOCK_READER_COUNT(state) != 0) {
2658 oldstate = casuword32(&rwlock->rw_state, state,
2660 if (oldstate == -1) {
2664 if (oldstate != state) {
2666 if (URWLOCK_READER_COUNT(oldstate) == 0) {
2670 error = umtxq_check_susp(td);
2683 if (!(flags & URWLOCK_PREFER_READER)) {
2684 if (state & URWLOCK_WRITE_WAITERS) {
2686 q = UMTX_EXCLUSIVE_QUEUE;
2687 } else if (state & URWLOCK_READ_WAITERS) {
2689 q = UMTX_SHARED_QUEUE;
2692 if (state & URWLOCK_READ_WAITERS) {
2694 q = UMTX_SHARED_QUEUE;
2695 } else if (state & URWLOCK_WRITE_WAITERS) {
2697 q = UMTX_EXCLUSIVE_QUEUE;
2702 umtxq_lock(&uq->uq_key);
2703 umtxq_busy(&uq->uq_key);
2704 umtxq_signal_queue(&uq->uq_key, count, q);
2705 umtxq_unbusy(&uq->uq_key);
2706 umtxq_unlock(&uq->uq_key);
2709 umtx_key_release(&uq->uq_key);
2714 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
2716 struct abs_timeout timo;
2718 uint32_t flags, count;
2722 flags = fuword32(&sem->_flags);
2723 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
2727 if (timeout != NULL)
2728 abs_timeout_init2(&timo, timeout);
2730 umtxq_lock(&uq->uq_key);
2731 umtxq_busy(&uq->uq_key);
2733 umtxq_unlock(&uq->uq_key);
2734 casuword32(__DEVOLATILE(uint32_t *, &sem->_has_waiters), 0, 1);
2735 count = fuword32(__DEVOLATILE(uint32_t *, &sem->_count));
2737 umtxq_lock(&uq->uq_key);
2738 umtxq_unbusy(&uq->uq_key);
2740 umtxq_unlock(&uq->uq_key);
2741 umtx_key_release(&uq->uq_key);
2744 umtxq_lock(&uq->uq_key);
2745 umtxq_unbusy(&uq->uq_key);
2747 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
2749 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2753 /* A relative timeout cannot be restarted. */
2754 if (error == ERESTART && timeout != NULL &&
2755 (timeout->_flags & UMTX_ABSTIME) == 0)
2758 umtxq_unlock(&uq->uq_key);
2759 umtx_key_release(&uq->uq_key);
2764 * Signal a userland condition variable.
2767 do_sem_wake(struct thread *td, struct _usem *sem)
2769 struct umtx_key key;
2773 flags = fuword32(&sem->_flags);
2774 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
2778 cnt = umtxq_count(&key);
2780 umtxq_signal(&key, 1);
2782 * Check if count is greater than 0, this means the memory is
2783 * still being referenced by user code, so we can safely
2784 * update _has_waiters flag.
2789 __DEVOLATILE(uint32_t *, &sem->_has_waiters), 0);
2795 umtx_key_release(&key);
2800 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
2804 error = copyin(addr, tsp, sizeof(struct timespec));
2806 if (tsp->tv_sec < 0 ||
2807 tsp->tv_nsec >= 1000000000 ||
2815 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
2819 if (size <= sizeof(struct timespec)) {
2820 tp->_clockid = CLOCK_REALTIME;
2822 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
2824 error = copyin(addr, tp, sizeof(struct _umtx_time));
2827 if (tp->_timeout.tv_sec < 0 ||
2828 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
2834 __umtx_op_unimpl(struct thread *td, struct _umtx_op_args *uap)
2837 return (EOPNOTSUPP);
2841 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
2843 struct _umtx_time timeout, *tm_p;
2846 if (uap->uaddr2 == NULL)
2849 error = umtx_copyin_umtx_time(
2850 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
2855 return do_wait(td, uap->obj, uap->val, tm_p, 0, 0);
2859 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
2861 struct _umtx_time timeout, *tm_p;
2864 if (uap->uaddr2 == NULL)
2867 error = umtx_copyin_umtx_time(
2868 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
2873 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
2877 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
2879 struct _umtx_time *tm_p, timeout;
2882 if (uap->uaddr2 == NULL)
2885 error = umtx_copyin_umtx_time(
2886 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
2891 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
2895 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
2897 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
2900 #define BATCH_SIZE 128
2902 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
2904 int count = uap->val;
2905 void *uaddrs[BATCH_SIZE];
2906 char **upp = (char **)uap->obj;
2913 if (tocopy > BATCH_SIZE)
2914 tocopy = BATCH_SIZE;
2915 error = copyin(upp+pos, uaddrs, tocopy * sizeof(char *));
2918 for (i = 0; i < tocopy; ++i)
2919 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
2927 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
2929 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
2933 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
2935 struct _umtx_time *tm_p, timeout;
2938 /* Allow a null timespec (wait forever). */
2939 if (uap->uaddr2 == NULL)
2942 error = umtx_copyin_umtx_time(
2943 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
2948 return do_lock_umutex(td, uap->obj, tm_p, 0);
2952 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
2954 return do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY);
2958 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
2960 struct _umtx_time *tm_p, timeout;
2963 /* Allow a null timespec (wait forever). */
2964 if (uap->uaddr2 == NULL)
2967 error = umtx_copyin_umtx_time(
2968 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
2973 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
2977 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
2979 return do_wake_umutex(td, uap->obj);
2983 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
2985 return do_unlock_umutex(td, uap->obj);
2989 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
2991 return do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1);
2995 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
2997 struct timespec *ts, timeout;
3000 /* Allow a null timespec (wait forever). */
3001 if (uap->uaddr2 == NULL)
3004 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3009 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3013 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3015 return do_cv_signal(td, uap->obj);
3019 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3021 return do_cv_broadcast(td, uap->obj);
3025 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3027 struct _umtx_time timeout;
3030 /* Allow a null timespec (wait forever). */
3031 if (uap->uaddr2 == NULL) {
3032 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3034 error = umtx_copyin_umtx_time(uap->uaddr2,
3035 (size_t)uap->uaddr1, &timeout);
3038 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3044 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3046 struct _umtx_time timeout;
3049 /* Allow a null timespec (wait forever). */
3050 if (uap->uaddr2 == NULL) {
3051 error = do_rw_wrlock(td, uap->obj, 0);
3053 error = umtx_copyin_umtx_time(uap->uaddr2,
3054 (size_t)uap->uaddr1, &timeout);
3058 error = do_rw_wrlock(td, uap->obj, &timeout);
3064 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3066 return do_rw_unlock(td, uap->obj);
3070 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3072 struct _umtx_time *tm_p, timeout;
3075 /* Allow a null timespec (wait forever). */
3076 if (uap->uaddr2 == NULL)
3079 error = umtx_copyin_umtx_time(
3080 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3085 return (do_sem_wait(td, uap->obj, tm_p));
3089 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3091 return do_sem_wake(td, uap->obj);
3095 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3097 return do_wake2_umutex(td, uap->obj, uap->val);
3100 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
3102 static _umtx_op_func op_table[] = {
3103 __umtx_op_unimpl, /* UMTX_OP_RESERVED0 */
3104 __umtx_op_unimpl, /* UMTX_OP_RESERVED1 */
3105 __umtx_op_wait, /* UMTX_OP_WAIT */
3106 __umtx_op_wake, /* UMTX_OP_WAKE */
3107 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_TRYLOCK */
3108 __umtx_op_lock_umutex, /* UMTX_OP_MUTEX_LOCK */
3109 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3110 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3111 __umtx_op_cv_wait, /* UMTX_OP_CV_WAIT*/
3112 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3113 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3114 __umtx_op_wait_uint, /* UMTX_OP_WAIT_UINT */
3115 __umtx_op_rw_rdlock, /* UMTX_OP_RW_RDLOCK */
3116 __umtx_op_rw_wrlock, /* UMTX_OP_RW_WRLOCK */
3117 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3118 __umtx_op_wait_uint_private, /* UMTX_OP_WAIT_UINT_PRIVATE */
3119 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3120 __umtx_op_wait_umutex, /* UMTX_OP_MUTEX_WAIT */
3121 __umtx_op_wake_umutex, /* UMTX_OP_MUTEX_WAKE */
3122 __umtx_op_sem_wait, /* UMTX_OP_SEM_WAIT */
3123 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3124 __umtx_op_nwake_private, /* UMTX_OP_NWAKE_PRIVATE */
3125 __umtx_op_wake2_umutex /* UMTX_OP_MUTEX_WAKE2 */
3129 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
3131 if ((unsigned)uap->op < UMTX_OP_MAX)
3132 return (*op_table[uap->op])(td, uap);
3136 #ifdef COMPAT_FREEBSD32
3143 struct umtx_time32 {
3144 struct timespec32 timeout;
3150 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
3152 struct timespec32 ts32;
3155 error = copyin(addr, &ts32, sizeof(struct timespec32));
3157 if (ts32.tv_sec < 0 ||
3158 ts32.tv_nsec >= 1000000000 ||
3162 tsp->tv_sec = ts32.tv_sec;
3163 tsp->tv_nsec = ts32.tv_nsec;
3170 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
3172 struct umtx_time32 t32;
3175 t32.clockid = CLOCK_REALTIME;
3177 if (size <= sizeof(struct timespec32))
3178 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
3180 error = copyin(addr, &t32, sizeof(struct umtx_time32));
3183 if (t32.timeout.tv_sec < 0 ||
3184 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
3186 tp->_timeout.tv_sec = t32.timeout.tv_sec;
3187 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
3188 tp->_flags = t32.flags;
3189 tp->_clockid = t32.clockid;
3194 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3196 struct _umtx_time *tm_p, timeout;
3199 if (uap->uaddr2 == NULL)
3202 error = umtx_copyin_umtx_time32(uap->uaddr2,
3203 (size_t)uap->uaddr1, &timeout);
3208 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3212 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3214 struct _umtx_time *tm_p, timeout;
3217 /* Allow a null timespec (wait forever). */
3218 if (uap->uaddr2 == NULL)
3221 error = umtx_copyin_umtx_time(uap->uaddr2,
3222 (size_t)uap->uaddr1, &timeout);
3227 return do_lock_umutex(td, uap->obj, tm_p, 0);
3231 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3233 struct _umtx_time *tm_p, timeout;
3236 /* Allow a null timespec (wait forever). */
3237 if (uap->uaddr2 == NULL)
3240 error = umtx_copyin_umtx_time32(uap->uaddr2,
3241 (size_t)uap->uaddr1, &timeout);
3246 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3250 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3252 struct timespec *ts, timeout;
3255 /* Allow a null timespec (wait forever). */
3256 if (uap->uaddr2 == NULL)
3259 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3264 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3268 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3270 struct _umtx_time timeout;
3273 /* Allow a null timespec (wait forever). */
3274 if (uap->uaddr2 == NULL) {
3275 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3277 error = umtx_copyin_umtx_time32(uap->uaddr2,
3278 (size_t)uap->uaddr1, &timeout);
3281 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3287 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3289 struct _umtx_time timeout;
3292 /* Allow a null timespec (wait forever). */
3293 if (uap->uaddr2 == NULL) {
3294 error = do_rw_wrlock(td, uap->obj, 0);
3296 error = umtx_copyin_umtx_time32(uap->uaddr2,
3297 (size_t)uap->uaddr1, &timeout);
3300 error = do_rw_wrlock(td, uap->obj, &timeout);
3306 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3308 struct _umtx_time *tm_p, timeout;
3311 if (uap->uaddr2 == NULL)
3314 error = umtx_copyin_umtx_time32(
3315 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
3320 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3324 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3326 struct _umtx_time *tm_p, timeout;
3329 /* Allow a null timespec (wait forever). */
3330 if (uap->uaddr2 == NULL)
3333 error = umtx_copyin_umtx_time32(uap->uaddr2,
3334 (size_t)uap->uaddr1, &timeout);
3339 return (do_sem_wait(td, uap->obj, tm_p));
3343 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
3345 int count = uap->val;
3346 uint32_t uaddrs[BATCH_SIZE];
3347 uint32_t **upp = (uint32_t **)uap->obj;
3354 if (tocopy > BATCH_SIZE)
3355 tocopy = BATCH_SIZE;
3356 error = copyin(upp+pos, uaddrs, tocopy * sizeof(uint32_t));
3359 for (i = 0; i < tocopy; ++i)
3360 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
3368 static _umtx_op_func op_table_compat32[] = {
3369 __umtx_op_unimpl, /* UMTX_OP_RESERVED0 */
3370 __umtx_op_unimpl, /* UMTX_OP_RESERVED1 */
3371 __umtx_op_wait_compat32, /* UMTX_OP_WAIT */
3372 __umtx_op_wake, /* UMTX_OP_WAKE */
3373 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_LOCK */
3374 __umtx_op_lock_umutex_compat32, /* UMTX_OP_MUTEX_TRYLOCK */
3375 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3376 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3377 __umtx_op_cv_wait_compat32, /* UMTX_OP_CV_WAIT*/
3378 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3379 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3380 __umtx_op_wait_compat32, /* UMTX_OP_WAIT_UINT */
3381 __umtx_op_rw_rdlock_compat32, /* UMTX_OP_RW_RDLOCK */
3382 __umtx_op_rw_wrlock_compat32, /* UMTX_OP_RW_WRLOCK */
3383 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3384 __umtx_op_wait_uint_private_compat32, /* UMTX_OP_WAIT_UINT_PRIVATE */
3385 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3386 __umtx_op_wait_umutex_compat32, /* UMTX_OP_MUTEX_WAIT */
3387 __umtx_op_wake_umutex, /* UMTX_OP_MUTEX_WAKE */
3388 __umtx_op_sem_wait_compat32, /* UMTX_OP_SEM_WAIT */
3389 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3390 __umtx_op_nwake_private32, /* UMTX_OP_NWAKE_PRIVATE */
3391 __umtx_op_wake2_umutex /* UMTX_OP_MUTEX_WAKE2 */
3395 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
3397 if ((unsigned)uap->op < UMTX_OP_MAX)
3398 return (*op_table_compat32[uap->op])(td,
3399 (struct _umtx_op_args *)uap);
3405 umtx_thread_init(struct thread *td)
3407 td->td_umtxq = umtxq_alloc();
3408 td->td_umtxq->uq_thread = td;
3412 umtx_thread_fini(struct thread *td)
3414 umtxq_free(td->td_umtxq);
3418 * It will be called when new thread is created, e.g fork().
3421 umtx_thread_alloc(struct thread *td)
3426 uq->uq_inherited_pri = PRI_MAX;
3428 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
3429 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
3430 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
3431 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
3438 umtx_exec_hook(void *arg __unused, struct proc *p __unused,
3439 struct image_params *imgp __unused)
3441 umtx_thread_cleanup(curthread);
3445 * thread_exit() hook.
3448 umtx_thread_exit(struct thread *td)
3450 umtx_thread_cleanup(td);
3454 * clean up umtx data.
3457 umtx_thread_cleanup(struct thread *td)
3462 if ((uq = td->td_umtxq) == NULL)
3465 mtx_lock_spin(&umtx_lock);
3466 uq->uq_inherited_pri = PRI_MAX;
3467 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
3468 pi->pi_owner = NULL;
3469 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
3471 mtx_unlock_spin(&umtx_lock);
3473 sched_lend_user_prio(td, PRI_MAX);