2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015, 2016 The FreeBSD Foundation
5 * Copyright (c) 2004, David Xu <davidxu@freebsd.org>
6 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
9 * Portions of this software were developed by Konstantin Belousov
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice unmodified, this list of conditions, and the following
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_umtx_profiling.h"
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
43 #include <sys/filedesc.h>
44 #include <sys/limits.h>
46 #include <sys/malloc.h>
48 #include <sys/mutex.h>
51 #include <sys/resource.h>
52 #include <sys/resourcevar.h>
53 #include <sys/rwlock.h>
55 #include <sys/sched.h>
57 #include <sys/sysctl.h>
58 #include <sys/systm.h>
59 #include <sys/sysproto.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/taskqueue.h>
63 #include <sys/eventhandler.h>
65 #include <sys/umtxvar.h>
67 #include <security/mac/mac_framework.h>
70 #include <vm/vm_param.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_object.h>
75 #include <machine/atomic.h>
76 #include <machine/cpu.h>
78 #include <compat/freebsd32/freebsd32.h>
79 #ifdef COMPAT_FREEBSD32
80 #include <compat/freebsd32/freebsd32_proto.h>
84 #define _UMUTEX_WAIT 2
87 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
88 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
91 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
93 #define UMTXQ_ASSERT_LOCKED_BUSY(key) do { \
94 struct umtxq_chain *uc; \
96 uc = umtxq_getchain(key); \
97 mtx_assert(&uc->uc_lock, MA_OWNED); \
98 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy")); \
101 #define UMTXQ_ASSERT_LOCKED_BUSY(key) do {} while (0)
105 * Don't propagate time-sharing priority, there is a security reason,
106 * a user can simply introduce PI-mutex, let thread A lock the mutex,
107 * and let another thread B block on the mutex, because B is
108 * sleeping, its priority will be boosted, this causes A's priority to
109 * be boosted via priority propagating too and will never be lowered even
110 * if it is using 100%CPU, this is unfair to other processes.
113 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
114 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
115 PRI_MAX_TIMESHARE : (td)->td_user_pri)
117 #define GOLDEN_RATIO_PRIME 2654404609U
119 #define UMTX_CHAINS 512
121 #define UMTX_SHIFTS (__WORD_BIT - 9)
123 #define GET_SHARE(flags) \
124 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
126 #define BUSY_SPINS 200
128 struct umtx_copyops {
129 int (*copyin_timeout)(const void *uaddr, struct timespec *tsp);
130 int (*copyin_umtx_time)(const void *uaddr, size_t size,
131 struct _umtx_time *tp);
132 int (*copyin_robust_lists)(const void *uaddr, size_t size,
133 struct umtx_robust_lists_params *rbp);
134 int (*copyout_timeout)(void *uaddr, size_t size,
135 struct timespec *tsp);
136 const size_t timespec_sz;
137 const size_t umtx_time_sz;
141 _Static_assert(sizeof(struct umutex) == sizeof(struct umutex32), "umutex32");
142 _Static_assert(__offsetof(struct umutex, m_spare[0]) ==
143 __offsetof(struct umutex32, m_spare[0]), "m_spare32");
145 int umtx_shm_vnobj_persistent = 0;
146 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_vnode_persistent, CTLFLAG_RWTUN,
147 &umtx_shm_vnobj_persistent, 0,
148 "False forces destruction of umtx attached to file, on last close");
149 static int umtx_max_rb = 1000;
150 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_max_robust, CTLFLAG_RWTUN,
152 "Maximum number of robust mutexes allowed for each thread");
154 static uma_zone_t umtx_pi_zone;
155 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
156 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
157 static int umtx_pi_allocated;
159 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
161 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
162 &umtx_pi_allocated, 0, "Allocated umtx_pi");
163 static int umtx_verbose_rb = 1;
164 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
168 #ifdef UMTX_PROFILING
169 static long max_length;
170 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
171 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
175 static inline void umtx_abs_timeout_init2(struct umtx_abs_timeout *timo,
176 const struct _umtx_time *umtxtime);
178 static void umtx_shm_init(void);
179 static void umtxq_sysinit(void *);
180 static void umtxq_hash(struct umtx_key *key);
181 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
183 static void umtx_thread_cleanup(struct thread *td);
184 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
186 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
188 static struct mtx umtx_lock;
190 #ifdef UMTX_PROFILING
192 umtx_init_profiling(void)
194 struct sysctl_oid *chain_oid;
198 for (i = 0; i < UMTX_CHAINS; ++i) {
199 snprintf(chain_name, sizeof(chain_name), "%d", i);
200 chain_oid = SYSCTL_ADD_NODE(NULL,
201 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
202 chain_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
204 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
205 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
206 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
207 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
212 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
216 struct umtxq_chain *uc;
217 u_int fract, i, j, tot, whole;
218 u_int sf0, sf1, sf2, sf3, sf4;
219 u_int si0, si1, si2, si3, si4;
220 u_int sw0, sw1, sw2, sw3, sw4;
222 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
223 for (i = 0; i < 2; i++) {
225 for (j = 0; j < UMTX_CHAINS; ++j) {
226 uc = &umtxq_chains[i][j];
227 mtx_lock(&uc->uc_lock);
228 tot += uc->max_length;
229 mtx_unlock(&uc->uc_lock);
232 sbuf_printf(&sb, "%u) Empty ", i);
234 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
235 si0 = si1 = si2 = si3 = si4 = 0;
236 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
237 for (j = 0; j < UMTX_CHAINS; j++) {
238 uc = &umtxq_chains[i][j];
239 mtx_lock(&uc->uc_lock);
240 whole = uc->max_length * 100;
241 mtx_unlock(&uc->uc_lock);
242 fract = (whole % tot) * 100;
243 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
247 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
252 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
257 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
262 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
269 sbuf_printf(&sb, "queue %u:\n", i);
270 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
272 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
274 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
276 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
278 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
284 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
290 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
292 struct umtxq_chain *uc;
297 error = sysctl_handle_int(oidp, &clear, 0, req);
298 if (error != 0 || req->newptr == NULL)
302 for (i = 0; i < 2; ++i) {
303 for (j = 0; j < UMTX_CHAINS; ++j) {
304 uc = &umtxq_chains[i][j];
305 mtx_lock(&uc->uc_lock);
308 mtx_unlock(&uc->uc_lock);
315 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
316 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
317 sysctl_debug_umtx_chains_clear, "I",
318 "Clear umtx chains statistics");
319 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
320 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
321 sysctl_debug_umtx_chains_peaks, "A",
322 "Highest peaks in chains max length");
326 umtxq_sysinit(void *arg __unused)
330 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
331 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
332 for (i = 0; i < 2; ++i) {
333 for (j = 0; j < UMTX_CHAINS; ++j) {
334 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
335 MTX_DEF | MTX_DUPOK);
336 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
337 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
338 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
339 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
340 umtxq_chains[i][j].uc_busy = 0;
341 umtxq_chains[i][j].uc_waiters = 0;
342 #ifdef UMTX_PROFILING
343 umtxq_chains[i][j].length = 0;
344 umtxq_chains[i][j].max_length = 0;
348 #ifdef UMTX_PROFILING
349 umtx_init_profiling();
351 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
360 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
361 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
363 TAILQ_INIT(&uq->uq_spare_queue->head);
364 TAILQ_INIT(&uq->uq_pi_contested);
365 uq->uq_inherited_pri = PRI_MAX;
370 umtxq_free(struct umtx_q *uq)
373 MPASS(uq->uq_spare_queue != NULL);
374 free(uq->uq_spare_queue, M_UMTX);
379 umtxq_hash(struct umtx_key *key)
383 n = (uintptr_t)key->info.both.a + key->info.both.b;
384 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
388 umtxq_getchain(struct umtx_key *key)
391 if (key->type <= TYPE_SEM)
392 return (&umtxq_chains[1][key->hash]);
393 return (&umtxq_chains[0][key->hash]);
397 * Set chain to busy state when following operation
398 * may be blocked (kernel mutex can not be used).
401 umtxq_busy(struct umtx_key *key)
403 struct umtxq_chain *uc;
405 uc = umtxq_getchain(key);
406 mtx_assert(&uc->uc_lock, MA_OWNED);
410 int count = BUSY_SPINS;
413 while (uc->uc_busy && --count > 0)
419 while (uc->uc_busy) {
421 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
432 umtxq_unbusy(struct umtx_key *key)
434 struct umtxq_chain *uc;
436 uc = umtxq_getchain(key);
437 mtx_assert(&uc->uc_lock, MA_OWNED);
438 KASSERT(uc->uc_busy != 0, ("not busy"));
445 umtxq_unbusy_unlocked(struct umtx_key *key)
453 static struct umtxq_queue *
454 umtxq_queue_lookup(struct umtx_key *key, int q)
456 struct umtxq_queue *uh;
457 struct umtxq_chain *uc;
459 uc = umtxq_getchain(key);
460 UMTXQ_LOCKED_ASSERT(uc);
461 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
462 if (umtx_key_match(&uh->key, key))
470 umtxq_insert_queue(struct umtx_q *uq, int q)
472 struct umtxq_queue *uh;
473 struct umtxq_chain *uc;
475 uc = umtxq_getchain(&uq->uq_key);
476 UMTXQ_LOCKED_ASSERT(uc);
477 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
478 uh = umtxq_queue_lookup(&uq->uq_key, q);
480 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
482 uh = uq->uq_spare_queue;
483 uh->key = uq->uq_key;
484 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
485 #ifdef UMTX_PROFILING
487 if (uc->length > uc->max_length) {
488 uc->max_length = uc->length;
489 if (uc->max_length > max_length)
490 max_length = uc->max_length;
494 uq->uq_spare_queue = NULL;
496 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
498 uq->uq_flags |= UQF_UMTXQ;
499 uq->uq_cur_queue = uh;
504 umtxq_remove_queue(struct umtx_q *uq, int q)
506 struct umtxq_chain *uc;
507 struct umtxq_queue *uh;
509 uc = umtxq_getchain(&uq->uq_key);
510 UMTXQ_LOCKED_ASSERT(uc);
511 if (uq->uq_flags & UQF_UMTXQ) {
512 uh = uq->uq_cur_queue;
513 TAILQ_REMOVE(&uh->head, uq, uq_link);
515 uq->uq_flags &= ~UQF_UMTXQ;
516 if (TAILQ_EMPTY(&uh->head)) {
517 KASSERT(uh->length == 0,
518 ("inconsistent umtxq_queue length"));
519 #ifdef UMTX_PROFILING
522 LIST_REMOVE(uh, link);
524 uh = LIST_FIRST(&uc->uc_spare_queue);
525 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
526 LIST_REMOVE(uh, link);
528 uq->uq_spare_queue = uh;
529 uq->uq_cur_queue = NULL;
534 * Check if there are multiple waiters
537 umtxq_count(struct umtx_key *key)
539 struct umtxq_queue *uh;
541 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
542 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
549 * Check if there are multiple PI waiters and returns first
553 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
555 struct umtxq_queue *uh;
558 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
559 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
561 *first = TAILQ_FIRST(&uh->head);
568 * Wake up threads waiting on an userland object by a bit mask.
571 umtxq_signal_mask(struct umtx_key *key, int n_wake, u_int bitset)
573 struct umtxq_queue *uh;
574 struct umtx_q *uq, *uq_temp;
578 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
579 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
582 TAILQ_FOREACH_SAFE(uq, &uh->head, uq_link, uq_temp) {
583 if ((uq->uq_bitset & bitset) == 0)
585 umtxq_remove_queue(uq, UMTX_SHARED_QUEUE);
594 * Wake up threads waiting on an userland object.
598 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
600 struct umtxq_queue *uh;
605 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
606 uh = umtxq_queue_lookup(key, q);
608 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
609 umtxq_remove_queue(uq, q);
619 * Wake up specified thread.
622 umtxq_signal_thread(struct umtx_q *uq)
625 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
631 * Wake up a maximum of n_wake threads that are waiting on an userland
632 * object identified by key. The remaining threads are removed from queue
633 * identified by key and added to the queue identified by key2 (requeued).
634 * The n_requeue specifies an upper limit on the number of threads that
635 * are requeued to the second queue.
638 umtxq_requeue(struct umtx_key *key, int n_wake, struct umtx_key *key2,
641 struct umtxq_queue *uh;
642 struct umtx_q *uq, *uq_temp;
646 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
647 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key2));
648 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
651 TAILQ_FOREACH_SAFE(uq, &uh->head, uq_link, uq_temp) {
652 if (++ret <= n_wake) {
659 if (ret - n_wake == n_requeue)
667 tstohz(const struct timespec *tsp)
671 TIMESPEC_TO_TIMEVAL(&tv, tsp);
676 umtx_abs_timeout_init(struct umtx_abs_timeout *timo, int clockid,
677 int absolute, const struct timespec *timeout)
680 timo->clockid = clockid;
682 timo->is_abs_real = false;
683 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
684 timespecadd(&timo->cur, timeout, &timo->end);
686 timo->end = *timeout;
687 timo->is_abs_real = clockid == CLOCK_REALTIME ||
688 clockid == CLOCK_REALTIME_FAST ||
689 clockid == CLOCK_REALTIME_PRECISE ||
690 clockid == CLOCK_SECOND;
695 umtx_abs_timeout_init2(struct umtx_abs_timeout *timo,
696 const struct _umtx_time *umtxtime)
699 umtx_abs_timeout_init(timo, umtxtime->_clockid,
700 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
704 umtx_abs_timeout_enforce_min(sbintime_t *sbt)
706 sbintime_t when, mint;
708 mint = curproc->p_umtx_min_timeout;
709 if (__predict_false(mint != 0)) {
710 when = sbinuptime() + mint;
717 umtx_abs_timeout_getsbt(struct umtx_abs_timeout *timo, sbintime_t *sbt,
720 struct bintime bt, bbt;
724 switch (timo->clockid) {
726 /* Clocks that can be converted into absolute time. */
728 case CLOCK_REALTIME_PRECISE:
729 case CLOCK_REALTIME_FAST:
730 case CLOCK_MONOTONIC:
731 case CLOCK_MONOTONIC_PRECISE:
732 case CLOCK_MONOTONIC_FAST:
734 case CLOCK_UPTIME_PRECISE:
735 case CLOCK_UPTIME_FAST:
737 timespec2bintime(&timo->end, &bt);
738 switch (timo->clockid) {
740 case CLOCK_REALTIME_PRECISE:
741 case CLOCK_REALTIME_FAST:
743 getboottimebin(&bbt);
744 bintime_sub(&bt, &bbt);
749 if (bt.sec >= (SBT_MAX >> 32)) {
755 umtx_abs_timeout_enforce_min(sbt);
758 * Check if the absolute time should be aligned to
759 * avoid firing multiple timer events in non-periodic
762 switch (timo->clockid) {
763 case CLOCK_REALTIME_FAST:
764 case CLOCK_MONOTONIC_FAST:
765 case CLOCK_UPTIME_FAST:
766 rem = *sbt % tc_tick_sbt;
767 if (__predict_true(rem != 0))
768 *sbt += tc_tick_sbt - rem;
772 if (__predict_true(rem != 0))
773 *sbt += SBT_1S - rem;
779 /* Clocks that has to be periodically polled. */
782 case CLOCK_THREAD_CPUTIME_ID:
783 case CLOCK_PROCESS_CPUTIME_ID:
785 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
786 if (timespeccmp(&timo->end, &timo->cur, <=))
788 timespecsub(&timo->end, &timo->cur, &tts);
789 *sbt = tick_sbt * tstohz(&tts);
790 *flags = C_HARDCLOCK;
796 umtx_unlock_val(uint32_t flags, bool rb)
800 return (UMUTEX_RB_OWNERDEAD);
801 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
802 return (UMUTEX_RB_NOTRECOV);
804 return (UMUTEX_UNOWNED);
809 * Put thread into sleep state, before sleeping, check if
810 * thread was removed from umtx queue.
813 umtxq_sleep(struct umtx_q *uq, const char *wmesg,
814 struct umtx_abs_timeout *timo)
816 struct umtxq_chain *uc;
818 int error, flags = 0;
820 uc = umtxq_getchain(&uq->uq_key);
821 UMTXQ_LOCKED_ASSERT(uc);
823 if (!(uq->uq_flags & UQF_UMTXQ)) {
828 if (timo->is_abs_real)
829 curthread->td_rtcgen =
830 atomic_load_acq_int(&rtc_generation);
831 error = umtx_abs_timeout_getsbt(timo, &sbt, &flags);
835 error = msleep_sbt(uq, &uc->uc_lock, PCATCH | PDROP, wmesg,
837 uc = umtxq_getchain(&uq->uq_key);
838 mtx_lock(&uc->uc_lock);
839 if (error == EINTR || error == ERESTART)
841 if (error == EWOULDBLOCK && (flags & C_ABSOLUTE) != 0) {
847 curthread->td_rtcgen = 0;
852 * Convert userspace address into unique logical address.
855 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
857 struct thread *td = curthread;
859 vm_map_entry_t entry;
865 if (share == THREAD_SHARE) {
867 key->info.private.vs = td->td_proc->p_vmspace;
868 key->info.private.addr = (uintptr_t)addr;
870 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
871 map = &td->td_proc->p_vmspace->vm_map;
872 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
873 &entry, &key->info.shared.object, &pindex, &prot,
874 &wired) != KERN_SUCCESS) {
878 if ((share == PROCESS_SHARE) ||
879 (share == AUTO_SHARE &&
880 VM_INHERIT_SHARE == entry->inheritance)) {
882 key->info.shared.offset = (vm_offset_t)addr -
883 entry->start + entry->offset;
884 vm_object_reference(key->info.shared.object);
887 key->info.private.vs = td->td_proc->p_vmspace;
888 key->info.private.addr = (uintptr_t)addr;
890 vm_map_lookup_done(map, entry);
901 umtx_key_release(struct umtx_key *key)
904 vm_object_deallocate(key->info.shared.object);
907 #ifdef COMPAT_FREEBSD10
909 * Lock a umtx object.
912 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
913 const struct timespec *timeout)
915 struct umtx_abs_timeout timo;
923 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
926 * Care must be exercised when dealing with umtx structure. It
927 * can fault on any access.
931 * Try the uncontested case. This should be done in userland.
933 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
935 /* The acquire succeeded. */
936 if (owner == UMTX_UNOWNED)
939 /* The address was invalid. */
943 /* If no one owns it but it is contested try to acquire it. */
944 if (owner == UMTX_CONTESTED) {
945 owner = casuword(&umtx->u_owner,
946 UMTX_CONTESTED, id | UMTX_CONTESTED);
948 if (owner == UMTX_CONTESTED)
951 /* The address was invalid. */
955 error = thread_check_susp(td, false);
959 /* If this failed the lock has changed, restart. */
964 * If we caught a signal, we have retried and now
970 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
971 AUTO_SHARE, &uq->uq_key)) != 0)
974 umtxq_lock(&uq->uq_key);
975 umtxq_busy(&uq->uq_key);
977 umtxq_unbusy(&uq->uq_key);
978 umtxq_unlock(&uq->uq_key);
981 * Set the contested bit so that a release in user space
982 * knows to use the system call for unlock. If this fails
983 * either some one else has acquired the lock or it has been
986 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
988 /* The address was invalid. */
990 umtxq_lock(&uq->uq_key);
992 umtxq_unlock(&uq->uq_key);
993 umtx_key_release(&uq->uq_key);
998 * We set the contested bit, sleep. Otherwise the lock changed
999 * and we need to retry or we lost a race to the thread
1000 * unlocking the umtx.
1002 umtxq_lock(&uq->uq_key);
1004 error = umtxq_sleep(uq, "umtx", timeout == NULL ? NULL :
1007 umtxq_unlock(&uq->uq_key);
1008 umtx_key_release(&uq->uq_key);
1011 error = thread_check_susp(td, false);
1014 if (timeout == NULL) {
1015 /* Mutex locking is restarted if it is interrupted. */
1019 /* Timed-locking is not restarted. */
1020 if (error == ERESTART)
1027 * Unlock a umtx object.
1030 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
1032 struct umtx_key key;
1039 * Make sure we own this mtx.
1041 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
1045 if ((owner & ~UMTX_CONTESTED) != id)
1048 /* This should be done in userland */
1049 if ((owner & UMTX_CONTESTED) == 0) {
1050 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
1058 /* We should only ever be in here for contested locks */
1059 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1065 count = umtxq_count(&key);
1069 * When unlocking the umtx, it must be marked as unowned if
1070 * there is zero or one thread only waiting for it.
1071 * Otherwise, it must be marked as contested.
1073 old = casuword(&umtx->u_owner, owner,
1074 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
1076 umtxq_signal(&key,1);
1079 umtx_key_release(&key);
1087 #ifdef COMPAT_FREEBSD32
1090 * Lock a umtx object.
1093 do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id,
1094 const struct timespec *timeout)
1096 struct umtx_abs_timeout timo;
1104 if (timeout != NULL)
1105 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
1108 * Care must be exercised when dealing with umtx structure. It
1109 * can fault on any access.
1113 * Try the uncontested case. This should be done in userland.
1115 owner = casuword32(m, UMUTEX_UNOWNED, id);
1117 /* The acquire succeeded. */
1118 if (owner == UMUTEX_UNOWNED)
1121 /* The address was invalid. */
1125 /* If no one owns it but it is contested try to acquire it. */
1126 if (owner == UMUTEX_CONTESTED) {
1127 owner = casuword32(m,
1128 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1129 if (owner == UMUTEX_CONTESTED)
1132 /* The address was invalid. */
1136 error = thread_check_susp(td, false);
1140 /* If this failed the lock has changed, restart. */
1145 * If we caught a signal, we have retried and now
1151 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
1152 AUTO_SHARE, &uq->uq_key)) != 0)
1155 umtxq_lock(&uq->uq_key);
1156 umtxq_busy(&uq->uq_key);
1158 umtxq_unbusy(&uq->uq_key);
1159 umtxq_unlock(&uq->uq_key);
1162 * Set the contested bit so that a release in user space
1163 * knows to use the system call for unlock. If this fails
1164 * either some one else has acquired the lock or it has been
1167 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
1169 /* The address was invalid. */
1171 umtxq_lock(&uq->uq_key);
1173 umtxq_unlock(&uq->uq_key);
1174 umtx_key_release(&uq->uq_key);
1179 * We set the contested bit, sleep. Otherwise the lock changed
1180 * and we need to retry or we lost a race to the thread
1181 * unlocking the umtx.
1183 umtxq_lock(&uq->uq_key);
1185 error = umtxq_sleep(uq, "umtx", timeout == NULL ?
1188 umtxq_unlock(&uq->uq_key);
1189 umtx_key_release(&uq->uq_key);
1192 error = thread_check_susp(td, false);
1195 if (timeout == NULL) {
1196 /* Mutex locking is restarted if it is interrupted. */
1200 /* Timed-locking is not restarted. */
1201 if (error == ERESTART)
1208 * Unlock a umtx object.
1211 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
1213 struct umtx_key key;
1220 * Make sure we own this mtx.
1222 owner = fuword32(m);
1226 if ((owner & ~UMUTEX_CONTESTED) != id)
1229 /* This should be done in userland */
1230 if ((owner & UMUTEX_CONTESTED) == 0) {
1231 old = casuword32(m, owner, UMUTEX_UNOWNED);
1239 /* We should only ever be in here for contested locks */
1240 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1246 count = umtxq_count(&key);
1250 * When unlocking the umtx, it must be marked as unowned if
1251 * there is zero or one thread only waiting for it.
1252 * Otherwise, it must be marked as contested.
1254 old = casuword32(m, owner,
1255 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1257 umtxq_signal(&key,1);
1260 umtx_key_release(&key);
1267 #endif /* COMPAT_FREEBSD32 */
1268 #endif /* COMPAT_FREEBSD10 */
1271 * Fetch and compare value, sleep on the address if value is not changed.
1274 do_wait(struct thread *td, void *addr, u_long id,
1275 struct _umtx_time *timeout, int compat32, int is_private)
1277 struct umtx_abs_timeout timo;
1284 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
1285 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
1288 if (timeout != NULL)
1289 umtx_abs_timeout_init2(&timo, timeout);
1291 umtxq_lock(&uq->uq_key);
1293 umtxq_unlock(&uq->uq_key);
1294 if (compat32 == 0) {
1295 error = fueword(addr, &tmp);
1299 error = fueword32(addr, &tmp32);
1305 umtxq_lock(&uq->uq_key);
1308 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
1310 if ((uq->uq_flags & UQF_UMTXQ) == 0)
1314 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
1317 umtxq_unlock(&uq->uq_key);
1318 umtx_key_release(&uq->uq_key);
1319 if (error == ERESTART)
1325 * Wake up threads sleeping on the specified address.
1328 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1330 struct umtx_key key;
1333 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1334 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1337 umtxq_signal(&key, n_wake);
1339 umtx_key_release(&key);
1344 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1347 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1348 struct _umtx_time *timeout, int mode)
1350 struct umtx_abs_timeout timo;
1352 uint32_t owner, old, id;
1358 if (timeout != NULL)
1359 umtx_abs_timeout_init2(&timo, timeout);
1362 * Care must be exercised when dealing with umtx structure. It
1363 * can fault on any access.
1366 rv = fueword32(&m->m_owner, &owner);
1369 if (mode == _UMUTEX_WAIT) {
1370 if (owner == UMUTEX_UNOWNED ||
1371 owner == UMUTEX_CONTESTED ||
1372 owner == UMUTEX_RB_OWNERDEAD ||
1373 owner == UMUTEX_RB_NOTRECOV)
1377 * Robust mutex terminated. Kernel duty is to
1378 * return EOWNERDEAD to the userspace. The
1379 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1380 * by the common userspace code.
1382 if (owner == UMUTEX_RB_OWNERDEAD) {
1383 rv = casueword32(&m->m_owner,
1384 UMUTEX_RB_OWNERDEAD, &owner,
1385 id | UMUTEX_CONTESTED);
1389 MPASS(owner == UMUTEX_RB_OWNERDEAD);
1390 return (EOWNERDEAD); /* success */
1393 rv = thread_check_susp(td, false);
1398 if (owner == UMUTEX_RB_NOTRECOV)
1399 return (ENOTRECOVERABLE);
1402 * Try the uncontested case. This should be
1405 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1407 /* The address was invalid. */
1411 /* The acquire succeeded. */
1413 MPASS(owner == UMUTEX_UNOWNED);
1418 * If no one owns it but it is contested try
1422 if (owner == UMUTEX_CONTESTED) {
1423 rv = casueword32(&m->m_owner,
1424 UMUTEX_CONTESTED, &owner,
1425 id | UMUTEX_CONTESTED);
1426 /* The address was invalid. */
1430 MPASS(owner == UMUTEX_CONTESTED);
1434 rv = thread_check_susp(td, false);
1440 * If this failed the lock has
1446 /* rv == 1 but not contested, likely store failure */
1447 rv = thread_check_susp(td, false);
1452 if (mode == _UMUTEX_TRY)
1456 * If we caught a signal, we have retried and now
1462 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1463 GET_SHARE(flags), &uq->uq_key)) != 0)
1466 umtxq_lock(&uq->uq_key);
1467 umtxq_busy(&uq->uq_key);
1469 umtxq_unlock(&uq->uq_key);
1472 * Set the contested bit so that a release in user space
1473 * knows to use the system call for unlock. If this fails
1474 * either some one else has acquired the lock or it has been
1477 rv = casueword32(&m->m_owner, owner, &old,
1478 owner | UMUTEX_CONTESTED);
1480 /* The address was invalid or casueword failed to store. */
1481 if (rv == -1 || rv == 1) {
1482 umtxq_lock(&uq->uq_key);
1484 umtxq_unbusy(&uq->uq_key);
1485 umtxq_unlock(&uq->uq_key);
1486 umtx_key_release(&uq->uq_key);
1490 rv = thread_check_susp(td, false);
1498 * We set the contested bit, sleep. Otherwise the lock changed
1499 * and we need to retry or we lost a race to the thread
1500 * unlocking the umtx.
1502 umtxq_lock(&uq->uq_key);
1503 umtxq_unbusy(&uq->uq_key);
1504 MPASS(old == owner);
1505 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1508 umtxq_unlock(&uq->uq_key);
1509 umtx_key_release(&uq->uq_key);
1512 error = thread_check_susp(td, false);
1519 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1522 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1524 struct umtx_key key;
1525 uint32_t owner, old, id, newlock;
1532 * Make sure we own this mtx.
1534 error = fueword32(&m->m_owner, &owner);
1538 if ((owner & ~UMUTEX_CONTESTED) != id)
1541 newlock = umtx_unlock_val(flags, rb);
1542 if ((owner & UMUTEX_CONTESTED) == 0) {
1543 error = casueword32(&m->m_owner, owner, &old, newlock);
1547 error = thread_check_susp(td, false);
1552 MPASS(old == owner);
1556 /* We should only ever be in here for contested locks */
1557 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1563 count = umtxq_count(&key);
1567 * When unlocking the umtx, it must be marked as unowned if
1568 * there is zero or one thread only waiting for it.
1569 * Otherwise, it must be marked as contested.
1572 newlock |= UMUTEX_CONTESTED;
1573 error = casueword32(&m->m_owner, owner, &old, newlock);
1575 umtxq_signal(&key, 1);
1578 umtx_key_release(&key);
1584 error = thread_check_susp(td, false);
1593 * Check if the mutex is available and wake up a waiter,
1594 * only for simple mutex.
1597 do_wake_umutex(struct thread *td, struct umutex *m)
1599 struct umtx_key key;
1606 error = fueword32(&m->m_owner, &owner);
1610 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1611 owner != UMUTEX_RB_NOTRECOV)
1614 error = fueword32(&m->m_flags, &flags);
1618 /* We should only ever be in here for contested locks */
1619 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1625 count = umtxq_count(&key);
1628 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1629 owner != UMUTEX_RB_NOTRECOV) {
1630 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1634 } else if (error == 1) {
1638 umtx_key_release(&key);
1639 error = thread_check_susp(td, false);
1647 if (error == 0 && count != 0) {
1648 MPASS((owner & ~UMUTEX_CONTESTED) == 0 ||
1649 owner == UMUTEX_RB_OWNERDEAD ||
1650 owner == UMUTEX_RB_NOTRECOV);
1651 umtxq_signal(&key, 1);
1655 umtx_key_release(&key);
1660 * Check if the mutex has waiters and tries to fix contention bit.
1663 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1665 struct umtx_key key;
1666 uint32_t owner, old;
1671 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1675 type = TYPE_NORMAL_UMUTEX;
1677 case UMUTEX_PRIO_INHERIT:
1678 type = TYPE_PI_UMUTEX;
1680 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1681 type = TYPE_PI_ROBUST_UMUTEX;
1683 case UMUTEX_PRIO_PROTECT:
1684 type = TYPE_PP_UMUTEX;
1686 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1687 type = TYPE_PP_ROBUST_UMUTEX;
1692 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1698 count = umtxq_count(&key);
1701 error = fueword32(&m->m_owner, &owner);
1706 * Only repair contention bit if there is a waiter, this means
1707 * the mutex is still being referenced by userland code,
1708 * otherwise don't update any memory.
1710 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0 &&
1711 (count > 1 || (count == 1 && (owner & ~UMUTEX_CONTESTED) != 0))) {
1712 error = casueword32(&m->m_owner, owner, &old,
1713 owner | UMUTEX_CONTESTED);
1719 MPASS(old == owner);
1723 error = thread_check_susp(td, false);
1727 if (error == EFAULT) {
1728 umtxq_signal(&key, INT_MAX);
1729 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1730 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1731 umtxq_signal(&key, 1);
1734 umtx_key_release(&key);
1739 umtx_pi_alloc(int flags)
1743 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1744 TAILQ_INIT(&pi->pi_blocked);
1745 atomic_add_int(&umtx_pi_allocated, 1);
1750 umtx_pi_free(struct umtx_pi *pi)
1752 uma_zfree(umtx_pi_zone, pi);
1753 atomic_add_int(&umtx_pi_allocated, -1);
1757 * Adjust the thread's position on a pi_state after its priority has been
1761 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1763 struct umtx_q *uq, *uq1, *uq2;
1766 mtx_assert(&umtx_lock, MA_OWNED);
1773 * Check if the thread needs to be moved on the blocked chain.
1774 * It needs to be moved if either its priority is lower than
1775 * the previous thread or higher than the next thread.
1777 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1778 uq2 = TAILQ_NEXT(uq, uq_lockq);
1779 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1780 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1782 * Remove thread from blocked chain and determine where
1783 * it should be moved to.
1785 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1786 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1787 td1 = uq1->uq_thread;
1788 MPASS(td1->td_proc->p_magic == P_MAGIC);
1789 if (UPRI(td1) > UPRI(td))
1794 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1796 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1801 static struct umtx_pi *
1802 umtx_pi_next(struct umtx_pi *pi)
1804 struct umtx_q *uq_owner;
1806 if (pi->pi_owner == NULL)
1808 uq_owner = pi->pi_owner->td_umtxq;
1809 if (uq_owner == NULL)
1811 return (uq_owner->uq_pi_blocked);
1815 * Floyd's Cycle-Finding Algorithm.
1818 umtx_pi_check_loop(struct umtx_pi *pi)
1820 struct umtx_pi *pi1; /* fast iterator */
1822 mtx_assert(&umtx_lock, MA_OWNED);
1827 pi = umtx_pi_next(pi);
1830 pi1 = umtx_pi_next(pi1);
1833 pi1 = umtx_pi_next(pi1);
1843 * Propagate priority when a thread is blocked on POSIX
1847 umtx_propagate_priority(struct thread *td)
1853 mtx_assert(&umtx_lock, MA_OWNED);
1856 pi = uq->uq_pi_blocked;
1859 if (umtx_pi_check_loop(pi))
1864 if (td == NULL || td == curthread)
1867 MPASS(td->td_proc != NULL);
1868 MPASS(td->td_proc->p_magic == P_MAGIC);
1871 if (td->td_lend_user_pri > pri)
1872 sched_lend_user_prio(td, pri);
1880 * Pick up the lock that td is blocked on.
1883 pi = uq->uq_pi_blocked;
1886 /* Resort td on the list if needed. */
1887 umtx_pi_adjust_thread(pi, td);
1892 * Unpropagate priority for a PI mutex when a thread blocked on
1893 * it is interrupted by signal or resumed by others.
1896 umtx_repropagate_priority(struct umtx_pi *pi)
1898 struct umtx_q *uq, *uq_owner;
1899 struct umtx_pi *pi2;
1902 mtx_assert(&umtx_lock, MA_OWNED);
1904 if (umtx_pi_check_loop(pi))
1906 while (pi != NULL && pi->pi_owner != NULL) {
1908 uq_owner = pi->pi_owner->td_umtxq;
1910 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1911 uq = TAILQ_FIRST(&pi2->pi_blocked);
1913 if (pri > UPRI(uq->uq_thread))
1914 pri = UPRI(uq->uq_thread);
1918 if (pri > uq_owner->uq_inherited_pri)
1919 pri = uq_owner->uq_inherited_pri;
1920 thread_lock(pi->pi_owner);
1921 sched_lend_user_prio(pi->pi_owner, pri);
1922 thread_unlock(pi->pi_owner);
1923 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1924 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1929 * Insert a PI mutex into owned list.
1932 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1934 struct umtx_q *uq_owner;
1936 uq_owner = owner->td_umtxq;
1937 mtx_assert(&umtx_lock, MA_OWNED);
1938 MPASS(pi->pi_owner == NULL);
1939 pi->pi_owner = owner;
1940 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1944 * Disown a PI mutex, and remove it from the owned list.
1947 umtx_pi_disown(struct umtx_pi *pi)
1950 mtx_assert(&umtx_lock, MA_OWNED);
1951 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1952 pi->pi_owner = NULL;
1956 * Claim ownership of a PI mutex.
1959 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1964 mtx_lock(&umtx_lock);
1965 if (pi->pi_owner == owner) {
1966 mtx_unlock(&umtx_lock);
1970 if (pi->pi_owner != NULL) {
1972 * userland may have already messed the mutex, sigh.
1974 mtx_unlock(&umtx_lock);
1977 umtx_pi_setowner(pi, owner);
1978 uq = TAILQ_FIRST(&pi->pi_blocked);
1980 pri = UPRI(uq->uq_thread);
1982 if (pri < UPRI(owner))
1983 sched_lend_user_prio(owner, pri);
1984 thread_unlock(owner);
1986 mtx_unlock(&umtx_lock);
1991 * Adjust a thread's order position in its blocked PI mutex,
1992 * this may result new priority propagating process.
1995 umtx_pi_adjust(struct thread *td, u_char oldpri)
2001 mtx_lock(&umtx_lock);
2003 * Pick up the lock that td is blocked on.
2005 pi = uq->uq_pi_blocked;
2007 umtx_pi_adjust_thread(pi, td);
2008 umtx_repropagate_priority(pi);
2010 mtx_unlock(&umtx_lock);
2014 * Sleep on a PI mutex.
2017 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
2018 const char *wmesg, struct umtx_abs_timeout *timo, bool shared)
2020 struct thread *td, *td1;
2024 struct umtxq_chain *uc;
2026 uc = umtxq_getchain(&pi->pi_key);
2030 KASSERT(td == curthread, ("inconsistent uq_thread"));
2031 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
2032 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
2034 mtx_lock(&umtx_lock);
2035 if (pi->pi_owner == NULL) {
2036 mtx_unlock(&umtx_lock);
2037 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
2038 mtx_lock(&umtx_lock);
2040 if (pi->pi_owner == NULL)
2041 umtx_pi_setowner(pi, td1);
2042 PROC_UNLOCK(td1->td_proc);
2046 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
2047 pri = UPRI(uq1->uq_thread);
2053 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
2055 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
2057 uq->uq_pi_blocked = pi;
2059 td->td_flags |= TDF_UPIBLOCKED;
2061 umtx_propagate_priority(td);
2062 mtx_unlock(&umtx_lock);
2063 umtxq_unbusy(&uq->uq_key);
2065 error = umtxq_sleep(uq, wmesg, timo);
2068 mtx_lock(&umtx_lock);
2069 uq->uq_pi_blocked = NULL;
2071 td->td_flags &= ~TDF_UPIBLOCKED;
2073 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
2074 umtx_repropagate_priority(pi);
2075 mtx_unlock(&umtx_lock);
2076 umtxq_unlock(&uq->uq_key);
2082 * Add reference count for a PI mutex.
2085 umtx_pi_ref(struct umtx_pi *pi)
2088 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
2093 * Decrease reference count for a PI mutex, if the counter
2094 * is decreased to zero, its memory space is freed.
2097 umtx_pi_unref(struct umtx_pi *pi)
2099 struct umtxq_chain *uc;
2101 uc = umtxq_getchain(&pi->pi_key);
2102 UMTXQ_LOCKED_ASSERT(uc);
2103 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
2104 if (--pi->pi_refcount == 0) {
2105 mtx_lock(&umtx_lock);
2106 if (pi->pi_owner != NULL)
2108 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
2109 ("blocked queue not empty"));
2110 mtx_unlock(&umtx_lock);
2111 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
2117 * Find a PI mutex in hash table.
2120 umtx_pi_lookup(struct umtx_key *key)
2122 struct umtxq_chain *uc;
2125 uc = umtxq_getchain(key);
2126 UMTXQ_LOCKED_ASSERT(uc);
2128 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
2129 if (umtx_key_match(&pi->pi_key, key)) {
2137 * Insert a PI mutex into hash table.
2140 umtx_pi_insert(struct umtx_pi *pi)
2142 struct umtxq_chain *uc;
2144 uc = umtxq_getchain(&pi->pi_key);
2145 UMTXQ_LOCKED_ASSERT(uc);
2146 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
2150 * Drop a PI mutex and wakeup a top waiter.
2153 umtx_pi_drop(struct thread *td, struct umtx_key *key, bool rb, int *count)
2155 struct umtx_q *uq_first, *uq_first2, *uq_me;
2156 struct umtx_pi *pi, *pi2;
2159 UMTXQ_ASSERT_LOCKED_BUSY(key);
2160 *count = umtxq_count_pi(key, &uq_first);
2161 if (uq_first != NULL) {
2162 mtx_lock(&umtx_lock);
2163 pi = uq_first->uq_pi_blocked;
2164 KASSERT(pi != NULL, ("pi == NULL?"));
2165 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2166 mtx_unlock(&umtx_lock);
2167 /* userland messed the mutex */
2170 uq_me = td->td_umtxq;
2171 if (pi->pi_owner == td)
2173 /* get highest priority thread which is still sleeping. */
2174 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2175 while (uq_first != NULL &&
2176 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2177 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2180 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2181 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2182 if (uq_first2 != NULL) {
2183 if (pri > UPRI(uq_first2->uq_thread))
2184 pri = UPRI(uq_first2->uq_thread);
2188 sched_lend_user_prio(td, pri);
2190 mtx_unlock(&umtx_lock);
2192 umtxq_signal_thread(uq_first);
2194 pi = umtx_pi_lookup(key);
2196 * A umtx_pi can exist if a signal or timeout removed the
2197 * last waiter from the umtxq, but there is still
2198 * a thread in do_lock_pi() holding the umtx_pi.
2202 * The umtx_pi can be unowned, such as when a thread
2203 * has just entered do_lock_pi(), allocated the
2204 * umtx_pi, and unlocked the umtxq.
2205 * If the current thread owns it, it must disown it.
2207 mtx_lock(&umtx_lock);
2208 if (pi->pi_owner == td)
2210 mtx_unlock(&umtx_lock);
2220 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
2221 struct _umtx_time *timeout, int try)
2223 struct umtx_abs_timeout timo;
2225 struct umtx_pi *pi, *new_pi;
2226 uint32_t id, old_owner, owner, old;
2232 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2233 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2237 if (timeout != NULL)
2238 umtx_abs_timeout_init2(&timo, timeout);
2240 umtxq_lock(&uq->uq_key);
2241 pi = umtx_pi_lookup(&uq->uq_key);
2243 new_pi = umtx_pi_alloc(M_NOWAIT);
2244 if (new_pi == NULL) {
2245 umtxq_unlock(&uq->uq_key);
2246 new_pi = umtx_pi_alloc(M_WAITOK);
2247 umtxq_lock(&uq->uq_key);
2248 pi = umtx_pi_lookup(&uq->uq_key);
2250 umtx_pi_free(new_pi);
2254 if (new_pi != NULL) {
2255 new_pi->pi_key = uq->uq_key;
2256 umtx_pi_insert(new_pi);
2261 umtxq_unlock(&uq->uq_key);
2264 * Care must be exercised when dealing with umtx structure. It
2265 * can fault on any access.
2269 * Try the uncontested case. This should be done in userland.
2271 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
2272 /* The address was invalid. */
2277 /* The acquire succeeded. */
2279 MPASS(owner == UMUTEX_UNOWNED);
2284 if (owner == UMUTEX_RB_NOTRECOV) {
2285 error = ENOTRECOVERABLE;
2290 * Nobody owns it, but the acquire failed. This can happen
2291 * with ll/sc atomics.
2293 if (owner == UMUTEX_UNOWNED) {
2294 error = thread_check_susp(td, true);
2301 * Avoid overwriting a possible error from sleep due
2302 * to the pending signal with suspension check result.
2305 error = thread_check_susp(td, true);
2310 /* If no one owns it but it is contested try to acquire it. */
2311 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
2313 rv = casueword32(&m->m_owner, owner, &owner,
2314 id | UMUTEX_CONTESTED);
2315 /* The address was invalid. */
2322 error = thread_check_susp(td, true);
2328 * If this failed the lock could
2335 MPASS(owner == old_owner);
2336 umtxq_lock(&uq->uq_key);
2337 umtxq_busy(&uq->uq_key);
2338 error = umtx_pi_claim(pi, td);
2339 umtxq_unbusy(&uq->uq_key);
2340 umtxq_unlock(&uq->uq_key);
2343 * Since we're going to return an
2344 * error, restore the m_owner to its
2345 * previous, unowned state to avoid
2346 * compounding the problem.
2348 (void)casuword32(&m->m_owner,
2349 id | UMUTEX_CONTESTED, old_owner);
2351 if (error == 0 && old_owner == UMUTEX_RB_OWNERDEAD)
2356 if ((owner & ~UMUTEX_CONTESTED) == id) {
2367 * If we caught a signal, we have retried and now
2373 umtxq_lock(&uq->uq_key);
2374 umtxq_busy(&uq->uq_key);
2375 umtxq_unlock(&uq->uq_key);
2378 * Set the contested bit so that a release in user space
2379 * knows to use the system call for unlock. If this fails
2380 * either some one else has acquired the lock or it has been
2383 rv = casueword32(&m->m_owner, owner, &old, owner |
2386 /* The address was invalid. */
2388 umtxq_unbusy_unlocked(&uq->uq_key);
2393 umtxq_unbusy_unlocked(&uq->uq_key);
2394 error = thread_check_susp(td, true);
2399 * The lock changed and we need to retry or we
2400 * lost a race to the thread unlocking the
2401 * umtx. Note that the UMUTEX_RB_OWNERDEAD
2402 * value for owner is impossible there.
2407 umtxq_lock(&uq->uq_key);
2409 /* We set the contested bit, sleep. */
2410 MPASS(old == owner);
2411 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
2412 "umtxpi", timeout == NULL ? NULL : &timo,
2413 (flags & USYNC_PROCESS_SHARED) != 0);
2417 error = thread_check_susp(td, false);
2422 umtxq_lock(&uq->uq_key);
2424 umtxq_unlock(&uq->uq_key);
2426 umtx_key_release(&uq->uq_key);
2431 * Unlock a PI mutex.
2434 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2436 struct umtx_key key;
2437 uint32_t id, new_owner, old, owner;
2444 * Make sure we own this mtx.
2446 error = fueword32(&m->m_owner, &owner);
2450 if ((owner & ~UMUTEX_CONTESTED) != id)
2453 new_owner = umtx_unlock_val(flags, rb);
2455 /* This should be done in userland */
2456 if ((owner & UMUTEX_CONTESTED) == 0) {
2457 error = casueword32(&m->m_owner, owner, &old, new_owner);
2461 error = thread_check_susp(td, true);
2471 /* We should only ever be in here for contested locks */
2472 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2473 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2479 error = umtx_pi_drop(td, &key, rb, &count);
2483 umtx_key_release(&key);
2484 /* userland messed the mutex */
2490 * When unlocking the umtx, it must be marked as unowned if
2491 * there is zero or one thread only waiting for it.
2492 * Otherwise, it must be marked as contested.
2496 new_owner |= UMUTEX_CONTESTED;
2498 error = casueword32(&m->m_owner, owner, &old, new_owner);
2500 error = thread_check_susp(td, false);
2504 umtxq_unbusy_unlocked(&key);
2505 umtx_key_release(&key);
2508 if (error == 0 && old != owner)
2517 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2518 struct _umtx_time *timeout, int try)
2520 struct umtx_abs_timeout timo;
2521 struct umtx_q *uq, *uq2;
2525 int error, pri, old_inherited_pri, su, rv;
2529 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2530 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2534 if (timeout != NULL)
2535 umtx_abs_timeout_init2(&timo, timeout);
2537 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2539 old_inherited_pri = uq->uq_inherited_pri;
2540 umtxq_lock(&uq->uq_key);
2541 umtxq_busy(&uq->uq_key);
2542 umtxq_unlock(&uq->uq_key);
2544 rv = fueword32(&m->m_ceilings[0], &ceiling);
2549 ceiling = RTP_PRIO_MAX - ceiling;
2550 if (ceiling > RTP_PRIO_MAX) {
2555 mtx_lock(&umtx_lock);
2556 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2557 mtx_unlock(&umtx_lock);
2561 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2562 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2564 if (uq->uq_inherited_pri < UPRI(td))
2565 sched_lend_user_prio(td, uq->uq_inherited_pri);
2568 mtx_unlock(&umtx_lock);
2570 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2571 id | UMUTEX_CONTESTED);
2572 /* The address was invalid. */
2578 MPASS(owner == UMUTEX_CONTESTED);
2583 if (owner == UMUTEX_RB_OWNERDEAD) {
2584 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2585 &owner, id | UMUTEX_CONTESTED);
2591 MPASS(owner == UMUTEX_RB_OWNERDEAD);
2592 error = EOWNERDEAD; /* success */
2597 * rv == 1, only check for suspension if we
2598 * did not already catched a signal. If we
2599 * get an error from the check, the same
2600 * condition is checked by the umtxq_sleep()
2601 * call below, so we should obliterate the
2602 * error to not skip the last loop iteration.
2605 error = thread_check_susp(td, false);
2614 } else if (owner == UMUTEX_RB_NOTRECOV) {
2615 error = ENOTRECOVERABLE;
2622 * If we caught a signal, we have retried and now
2628 umtxq_lock(&uq->uq_key);
2630 umtxq_unbusy(&uq->uq_key);
2631 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2634 umtxq_unlock(&uq->uq_key);
2636 mtx_lock(&umtx_lock);
2637 uq->uq_inherited_pri = old_inherited_pri;
2639 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2640 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2642 if (pri > UPRI(uq2->uq_thread))
2643 pri = UPRI(uq2->uq_thread);
2646 if (pri > uq->uq_inherited_pri)
2647 pri = uq->uq_inherited_pri;
2649 sched_lend_user_prio(td, pri);
2651 mtx_unlock(&umtx_lock);
2654 if (error != 0 && error != EOWNERDEAD) {
2655 mtx_lock(&umtx_lock);
2656 uq->uq_inherited_pri = old_inherited_pri;
2658 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2659 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2661 if (pri > UPRI(uq2->uq_thread))
2662 pri = UPRI(uq2->uq_thread);
2665 if (pri > uq->uq_inherited_pri)
2666 pri = uq->uq_inherited_pri;
2668 sched_lend_user_prio(td, pri);
2670 mtx_unlock(&umtx_lock);
2674 umtxq_unbusy_unlocked(&uq->uq_key);
2675 umtx_key_release(&uq->uq_key);
2680 * Unlock a PP mutex.
2683 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2685 struct umtx_key key;
2686 struct umtx_q *uq, *uq2;
2688 uint32_t id, owner, rceiling;
2689 int error, pri, new_inherited_pri, su;
2693 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2696 * Make sure we own this mtx.
2698 error = fueword32(&m->m_owner, &owner);
2702 if ((owner & ~UMUTEX_CONTESTED) != id)
2705 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2710 new_inherited_pri = PRI_MAX;
2712 rceiling = RTP_PRIO_MAX - rceiling;
2713 if (rceiling > RTP_PRIO_MAX)
2715 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2718 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2719 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2726 * For priority protected mutex, always set unlocked state
2727 * to UMUTEX_CONTESTED, so that userland always enters kernel
2728 * to lock the mutex, it is necessary because thread priority
2729 * has to be adjusted for such mutex.
2731 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2736 umtxq_signal(&key, 1);
2743 mtx_lock(&umtx_lock);
2745 uq->uq_inherited_pri = new_inherited_pri;
2747 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2748 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2750 if (pri > UPRI(uq2->uq_thread))
2751 pri = UPRI(uq2->uq_thread);
2754 if (pri > uq->uq_inherited_pri)
2755 pri = uq->uq_inherited_pri;
2757 sched_lend_user_prio(td, pri);
2759 mtx_unlock(&umtx_lock);
2761 umtx_key_release(&key);
2766 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2767 uint32_t *old_ceiling)
2770 uint32_t flags, id, owner, save_ceiling;
2773 error = fueword32(&m->m_flags, &flags);
2776 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2778 if (ceiling > RTP_PRIO_MAX)
2782 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2783 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2787 umtxq_lock(&uq->uq_key);
2788 umtxq_busy(&uq->uq_key);
2789 umtxq_unlock(&uq->uq_key);
2791 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2797 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2798 id | UMUTEX_CONTESTED);
2805 MPASS(owner == UMUTEX_CONTESTED);
2806 rv = suword32(&m->m_ceilings[0], ceiling);
2807 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2808 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2812 if ((owner & ~UMUTEX_CONTESTED) == id) {
2813 rv = suword32(&m->m_ceilings[0], ceiling);
2814 error = rv == 0 ? 0 : EFAULT;
2818 if (owner == UMUTEX_RB_OWNERDEAD) {
2821 } else if (owner == UMUTEX_RB_NOTRECOV) {
2822 error = ENOTRECOVERABLE;
2827 * If we caught a signal, we have retried and now
2834 * We set the contested bit, sleep. Otherwise the lock changed
2835 * and we need to retry or we lost a race to the thread
2836 * unlocking the umtx.
2838 umtxq_lock(&uq->uq_key);
2840 umtxq_unbusy(&uq->uq_key);
2841 error = umtxq_sleep(uq, "umtxpp", NULL);
2843 umtxq_unlock(&uq->uq_key);
2845 umtxq_lock(&uq->uq_key);
2847 umtxq_signal(&uq->uq_key, INT_MAX);
2848 umtxq_unbusy(&uq->uq_key);
2849 umtxq_unlock(&uq->uq_key);
2850 umtx_key_release(&uq->uq_key);
2851 if (error == 0 && old_ceiling != NULL) {
2852 rv = suword32(old_ceiling, save_ceiling);
2853 error = rv == 0 ? 0 : EFAULT;
2859 * Lock a userland POSIX mutex.
2862 do_lock_umutex(struct thread *td, struct umutex *m,
2863 struct _umtx_time *timeout, int mode)
2868 error = fueword32(&m->m_flags, &flags);
2872 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2874 error = do_lock_normal(td, m, flags, timeout, mode);
2876 case UMUTEX_PRIO_INHERIT:
2877 error = do_lock_pi(td, m, flags, timeout, mode);
2879 case UMUTEX_PRIO_PROTECT:
2880 error = do_lock_pp(td, m, flags, timeout, mode);
2885 if (timeout == NULL) {
2886 if (error == EINTR && mode != _UMUTEX_WAIT)
2889 /* Timed-locking is not restarted. */
2890 if (error == ERESTART)
2897 * Unlock a userland POSIX mutex.
2900 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2905 error = fueword32(&m->m_flags, &flags);
2909 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2911 return (do_unlock_normal(td, m, flags, rb));
2912 case UMUTEX_PRIO_INHERIT:
2913 return (do_unlock_pi(td, m, flags, rb));
2914 case UMUTEX_PRIO_PROTECT:
2915 return (do_unlock_pp(td, m, flags, rb));
2922 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2923 struct timespec *timeout, u_long wflags)
2925 struct umtx_abs_timeout timo;
2927 uint32_t flags, clockid, hasw;
2931 error = fueword32(&cv->c_flags, &flags);
2934 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2938 if ((wflags & CVWAIT_CLOCKID) != 0) {
2939 error = fueword32(&cv->c_clockid, &clockid);
2941 umtx_key_release(&uq->uq_key);
2944 if (clockid < CLOCK_REALTIME ||
2945 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2946 /* hmm, only HW clock id will work. */
2947 umtx_key_release(&uq->uq_key);
2951 clockid = CLOCK_REALTIME;
2954 umtxq_lock(&uq->uq_key);
2955 umtxq_busy(&uq->uq_key);
2957 umtxq_unlock(&uq->uq_key);
2960 * Set c_has_waiters to 1 before releasing user mutex, also
2961 * don't modify cache line when unnecessary.
2963 error = fueword32(&cv->c_has_waiters, &hasw);
2964 if (error == 0 && hasw == 0)
2965 suword32(&cv->c_has_waiters, 1);
2967 umtxq_unbusy_unlocked(&uq->uq_key);
2969 error = do_unlock_umutex(td, m, false);
2971 if (timeout != NULL)
2972 umtx_abs_timeout_init(&timo, clockid,
2973 (wflags & CVWAIT_ABSTIME) != 0, timeout);
2975 umtxq_lock(&uq->uq_key);
2977 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2981 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2985 * This must be timeout,interrupted by signal or
2986 * surprious wakeup, clear c_has_waiter flag when
2989 umtxq_busy(&uq->uq_key);
2990 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2991 int oldlen = uq->uq_cur_queue->length;
2994 umtxq_unlock(&uq->uq_key);
2995 suword32(&cv->c_has_waiters, 0);
2996 umtxq_lock(&uq->uq_key);
2999 umtxq_unbusy(&uq->uq_key);
3000 if (error == ERESTART)
3004 umtxq_unlock(&uq->uq_key);
3005 umtx_key_release(&uq->uq_key);
3010 * Signal a userland condition variable.
3013 do_cv_signal(struct thread *td, struct ucond *cv)
3015 struct umtx_key key;
3016 int error, cnt, nwake;
3019 error = fueword32(&cv->c_flags, &flags);
3022 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
3026 cnt = umtxq_count(&key);
3027 nwake = umtxq_signal(&key, 1);
3030 error = suword32(&cv->c_has_waiters, 0);
3037 umtx_key_release(&key);
3042 do_cv_broadcast(struct thread *td, struct ucond *cv)
3044 struct umtx_key key;
3048 error = fueword32(&cv->c_flags, &flags);
3051 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
3056 umtxq_signal(&key, INT_MAX);
3059 error = suword32(&cv->c_has_waiters, 0);
3063 umtxq_unbusy_unlocked(&key);
3065 umtx_key_release(&key);
3070 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
3071 struct _umtx_time *timeout)
3073 struct umtx_abs_timeout timo;
3075 uint32_t flags, wrflags;
3076 int32_t state, oldstate;
3077 int32_t blocked_readers;
3078 int error, error1, rv;
3081 error = fueword32(&rwlock->rw_flags, &flags);
3084 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3088 if (timeout != NULL)
3089 umtx_abs_timeout_init2(&timo, timeout);
3091 wrflags = URWLOCK_WRITE_OWNER;
3092 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
3093 wrflags |= URWLOCK_WRITE_WAITERS;
3096 rv = fueword32(&rwlock->rw_state, &state);
3098 umtx_key_release(&uq->uq_key);
3102 /* try to lock it */
3103 while (!(state & wrflags)) {
3104 if (__predict_false(URWLOCK_READER_COUNT(state) ==
3105 URWLOCK_MAX_READERS)) {
3106 umtx_key_release(&uq->uq_key);
3109 rv = casueword32(&rwlock->rw_state, state,
3110 &oldstate, state + 1);
3112 umtx_key_release(&uq->uq_key);
3116 MPASS(oldstate == state);
3117 umtx_key_release(&uq->uq_key);
3120 error = thread_check_susp(td, true);
3129 /* grab monitor lock */
3130 umtxq_lock(&uq->uq_key);
3131 umtxq_busy(&uq->uq_key);
3132 umtxq_unlock(&uq->uq_key);
3135 * re-read the state, in case it changed between the try-lock above
3136 * and the check below
3138 rv = fueword32(&rwlock->rw_state, &state);
3142 /* set read contention bit */
3143 while (error == 0 && (state & wrflags) &&
3144 !(state & URWLOCK_READ_WAITERS)) {
3145 rv = casueword32(&rwlock->rw_state, state,
3146 &oldstate, state | URWLOCK_READ_WAITERS);
3152 MPASS(oldstate == state);
3156 error = thread_check_susp(td, false);
3161 umtxq_unbusy_unlocked(&uq->uq_key);
3165 /* state is changed while setting flags, restart */
3166 if (!(state & wrflags)) {
3167 umtxq_unbusy_unlocked(&uq->uq_key);
3168 error = thread_check_susp(td, true);
3176 * Contention bit is set, before sleeping, increase
3177 * read waiter count.
3179 rv = fueword32(&rwlock->rw_blocked_readers,
3182 umtxq_unbusy_unlocked(&uq->uq_key);
3186 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
3188 while (state & wrflags) {
3189 umtxq_lock(&uq->uq_key);
3191 umtxq_unbusy(&uq->uq_key);
3193 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
3196 umtxq_busy(&uq->uq_key);
3198 umtxq_unlock(&uq->uq_key);
3201 rv = fueword32(&rwlock->rw_state, &state);
3208 /* decrease read waiter count, and may clear read contention bit */
3209 rv = fueword32(&rwlock->rw_blocked_readers,
3212 umtxq_unbusy_unlocked(&uq->uq_key);
3216 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
3217 if (blocked_readers == 1) {
3218 rv = fueword32(&rwlock->rw_state, &state);
3220 umtxq_unbusy_unlocked(&uq->uq_key);
3225 rv = casueword32(&rwlock->rw_state, state,
3226 &oldstate, state & ~URWLOCK_READ_WAITERS);
3232 MPASS(oldstate == state);
3236 error1 = thread_check_susp(td, false);
3245 umtxq_unbusy_unlocked(&uq->uq_key);
3249 umtx_key_release(&uq->uq_key);
3250 if (error == ERESTART)
3256 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
3258 struct umtx_abs_timeout timo;
3261 int32_t state, oldstate;
3262 int32_t blocked_writers;
3263 int32_t blocked_readers;
3264 int error, error1, rv;
3267 error = fueword32(&rwlock->rw_flags, &flags);
3270 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3274 if (timeout != NULL)
3275 umtx_abs_timeout_init2(&timo, timeout);
3277 blocked_readers = 0;
3279 rv = fueword32(&rwlock->rw_state, &state);
3281 umtx_key_release(&uq->uq_key);
3284 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
3285 URWLOCK_READER_COUNT(state) == 0) {
3286 rv = casueword32(&rwlock->rw_state, state,
3287 &oldstate, state | URWLOCK_WRITE_OWNER);
3289 umtx_key_release(&uq->uq_key);
3293 MPASS(oldstate == state);
3294 umtx_key_release(&uq->uq_key);
3298 error = thread_check_susp(td, true);
3304 if ((state & (URWLOCK_WRITE_OWNER |
3305 URWLOCK_WRITE_WAITERS)) == 0 &&
3306 blocked_readers != 0) {
3307 umtxq_lock(&uq->uq_key);
3308 umtxq_busy(&uq->uq_key);
3309 umtxq_signal_queue(&uq->uq_key, INT_MAX,
3311 umtxq_unbusy(&uq->uq_key);
3312 umtxq_unlock(&uq->uq_key);
3318 /* grab monitor lock */
3319 umtxq_lock(&uq->uq_key);
3320 umtxq_busy(&uq->uq_key);
3321 umtxq_unlock(&uq->uq_key);
3324 * Re-read the state, in case it changed between the
3325 * try-lock above and the check below.
3327 rv = fueword32(&rwlock->rw_state, &state);
3331 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
3332 URWLOCK_READER_COUNT(state) != 0) &&
3333 (state & URWLOCK_WRITE_WAITERS) == 0) {
3334 rv = casueword32(&rwlock->rw_state, state,
3335 &oldstate, state | URWLOCK_WRITE_WAITERS);
3341 MPASS(oldstate == state);
3345 error = thread_check_susp(td, false);
3350 umtxq_unbusy_unlocked(&uq->uq_key);
3354 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
3355 URWLOCK_READER_COUNT(state) == 0) {
3356 umtxq_unbusy_unlocked(&uq->uq_key);
3357 error = thread_check_susp(td, false);
3363 rv = fueword32(&rwlock->rw_blocked_writers,
3366 umtxq_unbusy_unlocked(&uq->uq_key);
3370 suword32(&rwlock->rw_blocked_writers, blocked_writers + 1);
3372 while ((state & URWLOCK_WRITE_OWNER) ||
3373 URWLOCK_READER_COUNT(state) != 0) {
3374 umtxq_lock(&uq->uq_key);
3375 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3376 umtxq_unbusy(&uq->uq_key);
3378 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
3381 umtxq_busy(&uq->uq_key);
3382 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3383 umtxq_unlock(&uq->uq_key);
3386 rv = fueword32(&rwlock->rw_state, &state);
3393 rv = fueword32(&rwlock->rw_blocked_writers,
3396 umtxq_unbusy_unlocked(&uq->uq_key);
3400 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
3401 if (blocked_writers == 1) {
3402 rv = fueword32(&rwlock->rw_state, &state);
3404 umtxq_unbusy_unlocked(&uq->uq_key);
3409 rv = casueword32(&rwlock->rw_state, state,
3410 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3416 MPASS(oldstate == state);
3420 error1 = thread_check_susp(td, false);
3422 * We are leaving the URWLOCK_WRITE_WAITERS
3423 * behind, but this should not harm the
3432 rv = fueword32(&rwlock->rw_blocked_readers,
3435 umtxq_unbusy_unlocked(&uq->uq_key);
3440 blocked_readers = 0;
3442 umtxq_unbusy_unlocked(&uq->uq_key);
3445 umtx_key_release(&uq->uq_key);
3446 if (error == ERESTART)
3452 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3456 int32_t state, oldstate;
3457 int error, rv, q, count;
3460 error = fueword32(&rwlock->rw_flags, &flags);
3463 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3467 error = fueword32(&rwlock->rw_state, &state);
3472 if (state & URWLOCK_WRITE_OWNER) {
3474 rv = casueword32(&rwlock->rw_state, state,
3475 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3482 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3486 error = thread_check_susp(td, true);
3492 } else if (URWLOCK_READER_COUNT(state) != 0) {
3494 rv = casueword32(&rwlock->rw_state, state,
3495 &oldstate, state - 1);
3502 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3506 error = thread_check_susp(td, true);
3519 if (!(flags & URWLOCK_PREFER_READER)) {
3520 if (state & URWLOCK_WRITE_WAITERS) {
3522 q = UMTX_EXCLUSIVE_QUEUE;
3523 } else if (state & URWLOCK_READ_WAITERS) {
3525 q = UMTX_SHARED_QUEUE;
3528 if (state & URWLOCK_READ_WAITERS) {
3530 q = UMTX_SHARED_QUEUE;
3531 } else if (state & URWLOCK_WRITE_WAITERS) {
3533 q = UMTX_EXCLUSIVE_QUEUE;
3538 umtxq_lock(&uq->uq_key);
3539 umtxq_busy(&uq->uq_key);
3540 umtxq_signal_queue(&uq->uq_key, count, q);
3541 umtxq_unbusy(&uq->uq_key);
3542 umtxq_unlock(&uq->uq_key);
3545 umtx_key_release(&uq->uq_key);
3549 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3551 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3553 struct umtx_abs_timeout timo;
3555 uint32_t flags, count, count1;
3559 error = fueword32(&sem->_flags, &flags);
3562 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3566 if (timeout != NULL)
3567 umtx_abs_timeout_init2(&timo, timeout);
3570 umtxq_lock(&uq->uq_key);
3571 umtxq_busy(&uq->uq_key);
3573 umtxq_unlock(&uq->uq_key);
3574 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3576 rv1 = fueword32(&sem->_count, &count);
3577 if (rv == -1 || rv1 == -1 || count != 0 || (rv == 1 && count1 == 0)) {
3579 suword32(&sem->_has_waiters, 0);
3580 umtxq_lock(&uq->uq_key);
3581 umtxq_unbusy(&uq->uq_key);
3583 umtxq_unlock(&uq->uq_key);
3584 if (rv == -1 || rv1 == -1) {
3592 MPASS(rv == 1 && count1 == 0);
3593 rv = thread_check_susp(td, true);
3599 umtxq_lock(&uq->uq_key);
3600 umtxq_unbusy(&uq->uq_key);
3602 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3604 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3608 /* A relative timeout cannot be restarted. */
3609 if (error == ERESTART && timeout != NULL &&
3610 (timeout->_flags & UMTX_ABSTIME) == 0)
3613 umtxq_unlock(&uq->uq_key);
3615 umtx_key_release(&uq->uq_key);
3620 * Signal a userland semaphore.
3623 do_sem_wake(struct thread *td, struct _usem *sem)
3625 struct umtx_key key;
3629 error = fueword32(&sem->_flags, &flags);
3632 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3636 cnt = umtxq_count(&key);
3639 * Check if count is greater than 0, this means the memory is
3640 * still being referenced by user code, so we can safely
3641 * update _has_waiters flag.
3645 error = suword32(&sem->_has_waiters, 0);
3650 umtxq_signal(&key, 1);
3654 umtx_key_release(&key);
3660 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3662 struct umtx_abs_timeout timo;
3664 uint32_t count, flags;
3668 flags = fuword32(&sem->_flags);
3669 if (timeout != NULL)
3670 umtx_abs_timeout_init2(&timo, timeout);
3673 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3676 umtxq_lock(&uq->uq_key);
3677 umtxq_busy(&uq->uq_key);
3679 umtxq_unlock(&uq->uq_key);
3680 rv = fueword32(&sem->_count, &count);
3682 umtxq_lock(&uq->uq_key);
3683 umtxq_unbusy(&uq->uq_key);
3685 umtxq_unlock(&uq->uq_key);
3686 umtx_key_release(&uq->uq_key);
3690 if (USEM_COUNT(count) != 0) {
3691 umtxq_lock(&uq->uq_key);
3692 umtxq_unbusy(&uq->uq_key);
3694 umtxq_unlock(&uq->uq_key);
3695 umtx_key_release(&uq->uq_key);
3698 if (count == USEM_HAS_WAITERS)
3700 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3703 umtxq_lock(&uq->uq_key);
3704 umtxq_unbusy(&uq->uq_key);
3706 umtxq_unlock(&uq->uq_key);
3707 umtx_key_release(&uq->uq_key);
3710 rv = thread_check_susp(td, true);
3715 umtxq_lock(&uq->uq_key);
3716 umtxq_unbusy(&uq->uq_key);
3718 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3720 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3724 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3725 /* A relative timeout cannot be restarted. */
3726 if (error == ERESTART)
3728 if (error == EINTR) {
3729 kern_clock_gettime(curthread, timo.clockid,
3731 timespecsub(&timo.end, &timo.cur,
3732 &timeout->_timeout);
3736 umtxq_unlock(&uq->uq_key);
3737 umtx_key_release(&uq->uq_key);
3742 * Signal a userland semaphore.
3745 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3747 struct umtx_key key;
3749 uint32_t count, flags;
3751 rv = fueword32(&sem->_flags, &flags);
3754 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3758 cnt = umtxq_count(&key);
3761 * If this was the last sleeping thread, clear the waiters
3766 rv = fueword32(&sem->_count, &count);
3767 while (rv != -1 && count & USEM_HAS_WAITERS) {
3768 rv = casueword32(&sem->_count, count, &count,
3769 count & ~USEM_HAS_WAITERS);
3771 rv = thread_check_susp(td, true);
3784 umtxq_signal(&key, 1);
3788 umtx_key_release(&key);
3792 #ifdef COMPAT_FREEBSD10
3794 freebsd10__umtx_lock(struct thread *td, struct freebsd10__umtx_lock_args *uap)
3796 return (do_lock_umtx(td, uap->umtx, td->td_tid, 0));
3800 freebsd10__umtx_unlock(struct thread *td,
3801 struct freebsd10__umtx_unlock_args *uap)
3803 return (do_unlock_umtx(td, uap->umtx, td->td_tid));
3808 umtx_copyin_timeout(const void *uaddr, struct timespec *tsp)
3812 error = copyin(uaddr, tsp, sizeof(*tsp));
3814 if (!timespecvalid_interval(tsp))
3821 umtx_copyin_umtx_time(const void *uaddr, size_t size, struct _umtx_time *tp)
3825 if (size <= sizeof(tp->_timeout)) {
3826 tp->_clockid = CLOCK_REALTIME;
3828 error = copyin(uaddr, &tp->_timeout, sizeof(tp->_timeout));
3830 error = copyin(uaddr, tp, sizeof(*tp));
3833 if (!timespecvalid_interval(&tp->_timeout))
3839 umtx_copyin_robust_lists(const void *uaddr, size_t size,
3840 struct umtx_robust_lists_params *rb)
3843 if (size > sizeof(*rb))
3845 return (copyin(uaddr, rb, size));
3849 umtx_copyout_timeout(void *uaddr, size_t sz, struct timespec *tsp)
3853 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
3854 * and we're only called if sz >= sizeof(timespec) as supplied in the
3857 KASSERT(sz >= sizeof(*tsp),
3858 ("umtx_copyops specifies incorrect sizes"));
3860 return (copyout(tsp, uaddr, sizeof(*tsp)));
3863 #ifdef COMPAT_FREEBSD10
3865 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap,
3866 const struct umtx_copyops *ops)
3868 struct timespec *ts, timeout;
3871 /* Allow a null timespec (wait forever). */
3872 if (uap->uaddr2 == NULL)
3875 error = ops->copyin_timeout(uap->uaddr2, &timeout);
3880 #ifdef COMPAT_FREEBSD32
3882 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
3884 return (do_lock_umtx(td, uap->obj, uap->val, ts));
3888 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap,
3889 const struct umtx_copyops *ops)
3891 #ifdef COMPAT_FREEBSD32
3893 return (do_unlock_umtx32(td, uap->obj, uap->val));
3895 return (do_unlock_umtx(td, uap->obj, uap->val));
3897 #endif /* COMPAT_FREEBSD10 */
3899 #if !defined(COMPAT_FREEBSD10)
3901 __umtx_op_unimpl(struct thread *td __unused, struct _umtx_op_args *uap __unused,
3902 const struct umtx_copyops *ops __unused)
3904 return (EOPNOTSUPP);
3906 #endif /* COMPAT_FREEBSD10 */
3909 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap,
3910 const struct umtx_copyops *ops)
3912 struct _umtx_time timeout, *tm_p;
3915 if (uap->uaddr2 == NULL)
3918 error = ops->copyin_umtx_time(
3919 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3924 return (do_wait(td, uap->obj, uap->val, tm_p, ops->compat32, 0));
3928 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap,
3929 const struct umtx_copyops *ops)
3931 struct _umtx_time timeout, *tm_p;
3934 if (uap->uaddr2 == NULL)
3937 error = ops->copyin_umtx_time(
3938 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3943 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3947 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap,
3948 const struct umtx_copyops *ops)
3950 struct _umtx_time *tm_p, timeout;
3953 if (uap->uaddr2 == NULL)
3956 error = ops->copyin_umtx_time(
3957 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3962 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3966 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap,
3967 const struct umtx_copyops *ops __unused)
3970 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3973 #define BATCH_SIZE 128
3975 __umtx_op_nwake_private_native(struct thread *td, struct _umtx_op_args *uap)
3977 char *uaddrs[BATCH_SIZE], **upp;
3978 int count, error, i, pos, tocopy;
3980 upp = (char **)uap->obj;
3982 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3984 tocopy = MIN(count, BATCH_SIZE);
3985 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3988 for (i = 0; i < tocopy; ++i) {
3989 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3997 __umtx_op_nwake_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3999 uint32_t uaddrs[BATCH_SIZE], *upp;
4000 int count, error, i, pos, tocopy;
4002 upp = (uint32_t *)uap->obj;
4004 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
4006 tocopy = MIN(count, BATCH_SIZE);
4007 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
4010 for (i = 0; i < tocopy; ++i) {
4011 kern_umtx_wake(td, (void *)(uintptr_t)uaddrs[i],
4020 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap,
4021 const struct umtx_copyops *ops)
4025 return (__umtx_op_nwake_private_compat32(td, uap));
4026 return (__umtx_op_nwake_private_native(td, uap));
4030 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap,
4031 const struct umtx_copyops *ops __unused)
4034 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
4038 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap,
4039 const struct umtx_copyops *ops)
4041 struct _umtx_time *tm_p, timeout;
4044 /* Allow a null timespec (wait forever). */
4045 if (uap->uaddr2 == NULL)
4048 error = ops->copyin_umtx_time(
4049 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4054 return (do_lock_umutex(td, uap->obj, tm_p, 0));
4058 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap,
4059 const struct umtx_copyops *ops __unused)
4062 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
4066 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap,
4067 const struct umtx_copyops *ops)
4069 struct _umtx_time *tm_p, timeout;
4072 /* Allow a null timespec (wait forever). */
4073 if (uap->uaddr2 == NULL)
4076 error = ops->copyin_umtx_time(
4077 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4082 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
4086 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap,
4087 const struct umtx_copyops *ops __unused)
4090 return (do_wake_umutex(td, uap->obj));
4094 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap,
4095 const struct umtx_copyops *ops __unused)
4098 return (do_unlock_umutex(td, uap->obj, false));
4102 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap,
4103 const struct umtx_copyops *ops __unused)
4106 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
4110 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap,
4111 const struct umtx_copyops *ops)
4113 struct timespec *ts, timeout;
4116 /* Allow a null timespec (wait forever). */
4117 if (uap->uaddr2 == NULL)
4120 error = ops->copyin_timeout(uap->uaddr2, &timeout);
4125 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
4129 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap,
4130 const struct umtx_copyops *ops __unused)
4133 return (do_cv_signal(td, uap->obj));
4137 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap,
4138 const struct umtx_copyops *ops __unused)
4141 return (do_cv_broadcast(td, uap->obj));
4145 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap,
4146 const struct umtx_copyops *ops)
4148 struct _umtx_time timeout;
4151 /* Allow a null timespec (wait forever). */
4152 if (uap->uaddr2 == NULL) {
4153 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4155 error = ops->copyin_umtx_time(uap->uaddr2,
4156 (size_t)uap->uaddr1, &timeout);
4159 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4165 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap,
4166 const struct umtx_copyops *ops)
4168 struct _umtx_time timeout;
4171 /* Allow a null timespec (wait forever). */
4172 if (uap->uaddr2 == NULL) {
4173 error = do_rw_wrlock(td, uap->obj, 0);
4175 error = ops->copyin_umtx_time(uap->uaddr2,
4176 (size_t)uap->uaddr1, &timeout);
4180 error = do_rw_wrlock(td, uap->obj, &timeout);
4186 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap,
4187 const struct umtx_copyops *ops __unused)
4190 return (do_rw_unlock(td, uap->obj));
4193 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4195 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap,
4196 const struct umtx_copyops *ops)
4198 struct _umtx_time *tm_p, timeout;
4201 /* Allow a null timespec (wait forever). */
4202 if (uap->uaddr2 == NULL)
4205 error = ops->copyin_umtx_time(
4206 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4211 return (do_sem_wait(td, uap->obj, tm_p));
4215 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap,
4216 const struct umtx_copyops *ops __unused)
4219 return (do_sem_wake(td, uap->obj));
4224 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap,
4225 const struct umtx_copyops *ops __unused)
4228 return (do_wake2_umutex(td, uap->obj, uap->val));
4232 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap,
4233 const struct umtx_copyops *ops)
4235 struct _umtx_time *tm_p, timeout;
4239 /* Allow a null timespec (wait forever). */
4240 if (uap->uaddr2 == NULL) {
4244 uasize = (size_t)uap->uaddr1;
4245 error = ops->copyin_umtx_time(uap->uaddr2, uasize, &timeout);
4250 error = do_sem2_wait(td, uap->obj, tm_p);
4251 if (error == EINTR && uap->uaddr2 != NULL &&
4252 (timeout._flags & UMTX_ABSTIME) == 0 &&
4253 uasize >= ops->umtx_time_sz + ops->timespec_sz) {
4254 error = ops->copyout_timeout(
4255 (void *)((uintptr_t)uap->uaddr2 + ops->umtx_time_sz),
4256 uasize - ops->umtx_time_sz, &timeout._timeout);
4266 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap,
4267 const struct umtx_copyops *ops __unused)
4270 return (do_sem2_wake(td, uap->obj));
4273 #define USHM_OBJ_UMTX(o) \
4274 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
4276 #define USHMF_REG_LINKED 0x0001
4277 #define USHMF_OBJ_LINKED 0x0002
4278 struct umtx_shm_reg {
4279 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
4280 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
4281 struct umtx_key ushm_key;
4282 struct ucred *ushm_cred;
4283 struct shmfd *ushm_obj;
4288 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
4289 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
4291 static uma_zone_t umtx_shm_reg_zone;
4292 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
4293 static struct mtx umtx_shm_lock;
4294 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
4295 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
4297 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
4300 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
4302 struct umtx_shm_reg_head d;
4303 struct umtx_shm_reg *reg, *reg1;
4306 mtx_lock(&umtx_shm_lock);
4307 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
4308 mtx_unlock(&umtx_shm_lock);
4309 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
4310 TAILQ_REMOVE(&d, reg, ushm_reg_link);
4311 umtx_shm_free_reg(reg);
4315 static struct task umtx_shm_reg_delfree_task =
4316 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
4318 static struct umtx_shm_reg *
4319 umtx_shm_find_reg_locked(const struct umtx_key *key)
4321 struct umtx_shm_reg *reg;
4322 struct umtx_shm_reg_head *reg_head;
4324 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
4325 mtx_assert(&umtx_shm_lock, MA_OWNED);
4326 reg_head = &umtx_shm_registry[key->hash];
4327 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
4328 KASSERT(reg->ushm_key.shared,
4329 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
4330 if (reg->ushm_key.info.shared.object ==
4331 key->info.shared.object &&
4332 reg->ushm_key.info.shared.offset ==
4333 key->info.shared.offset) {
4334 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
4335 KASSERT(reg->ushm_refcnt > 0,
4336 ("reg %p refcnt 0 onlist", reg));
4337 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
4338 ("reg %p not linked", reg));
4346 static struct umtx_shm_reg *
4347 umtx_shm_find_reg(const struct umtx_key *key)
4349 struct umtx_shm_reg *reg;
4351 mtx_lock(&umtx_shm_lock);
4352 reg = umtx_shm_find_reg_locked(key);
4353 mtx_unlock(&umtx_shm_lock);
4358 umtx_shm_free_reg(struct umtx_shm_reg *reg)
4361 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
4362 crfree(reg->ushm_cred);
4363 shm_drop(reg->ushm_obj);
4364 uma_zfree(umtx_shm_reg_zone, reg);
4368 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
4372 mtx_assert(&umtx_shm_lock, MA_OWNED);
4373 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
4375 res = reg->ushm_refcnt == 0;
4377 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
4378 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
4379 reg, ushm_reg_link);
4380 reg->ushm_flags &= ~USHMF_REG_LINKED;
4382 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
4383 LIST_REMOVE(reg, ushm_obj_link);
4384 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
4391 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
4397 object = reg->ushm_obj->shm_object;
4398 VM_OBJECT_WLOCK(object);
4399 vm_object_set_flag(object, OBJ_UMTXDEAD);
4400 VM_OBJECT_WUNLOCK(object);
4402 mtx_lock(&umtx_shm_lock);
4403 dofree = umtx_shm_unref_reg_locked(reg, force);
4404 mtx_unlock(&umtx_shm_lock);
4406 umtx_shm_free_reg(reg);
4410 umtx_shm_object_init(vm_object_t object)
4413 LIST_INIT(USHM_OBJ_UMTX(object));
4417 umtx_shm_object_terminated(vm_object_t object)
4419 struct umtx_shm_reg *reg, *reg1;
4422 if (LIST_EMPTY(USHM_OBJ_UMTX(object)))
4426 mtx_lock(&umtx_shm_lock);
4427 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
4428 if (umtx_shm_unref_reg_locked(reg, true)) {
4429 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
4434 mtx_unlock(&umtx_shm_lock);
4436 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
4440 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
4441 struct umtx_shm_reg **res)
4443 struct umtx_shm_reg *reg, *reg1;
4447 reg = umtx_shm_find_reg(key);
4452 cred = td->td_ucred;
4453 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
4455 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
4456 reg->ushm_refcnt = 1;
4457 bcopy(key, ®->ushm_key, sizeof(*key));
4458 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR, false);
4459 reg->ushm_cred = crhold(cred);
4460 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
4462 umtx_shm_free_reg(reg);
4465 mtx_lock(&umtx_shm_lock);
4466 reg1 = umtx_shm_find_reg_locked(key);
4468 mtx_unlock(&umtx_shm_lock);
4469 umtx_shm_free_reg(reg);
4474 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
4475 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
4477 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
4478 mtx_unlock(&umtx_shm_lock);
4484 umtx_shm_alive(struct thread *td, void *addr)
4487 vm_map_entry_t entry;
4494 map = &td->td_proc->p_vmspace->vm_map;
4495 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
4496 &object, &pindex, &prot, &wired);
4497 if (res != KERN_SUCCESS)
4502 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
4503 vm_map_lookup_done(map, entry);
4512 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
4513 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
4514 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
4515 for (i = 0; i < nitems(umtx_shm_registry); i++)
4516 TAILQ_INIT(&umtx_shm_registry[i]);
4520 umtx_shm(struct thread *td, void *addr, u_int flags)
4522 struct umtx_key key;
4523 struct umtx_shm_reg *reg;
4527 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
4528 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
4530 if ((flags & UMTX_SHM_ALIVE) != 0)
4531 return (umtx_shm_alive(td, addr));
4532 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
4535 KASSERT(key.shared == 1, ("non-shared key"));
4536 if ((flags & UMTX_SHM_CREAT) != 0) {
4537 error = umtx_shm_create_reg(td, &key, ®);
4539 reg = umtx_shm_find_reg(&key);
4543 umtx_key_release(&key);
4546 KASSERT(reg != NULL, ("no reg"));
4547 if ((flags & UMTX_SHM_DESTROY) != 0) {
4548 umtx_shm_unref_reg(reg, true);
4552 error = mac_posixshm_check_open(td->td_ucred,
4553 reg->ushm_obj, FFLAGS(O_RDWR));
4556 error = shm_access(reg->ushm_obj, td->td_ucred,
4560 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
4562 shm_hold(reg->ushm_obj);
4563 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
4565 td->td_retval[0] = fd;
4569 umtx_shm_unref_reg(reg, false);
4574 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap,
4575 const struct umtx_copyops *ops __unused)
4578 return (umtx_shm(td, uap->uaddr1, uap->val));
4582 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap,
4583 const struct umtx_copyops *ops)
4585 struct umtx_robust_lists_params rb;
4588 if (ops->compat32) {
4589 if ((td->td_pflags2 & TDP2_COMPAT32RB) == 0 &&
4590 (td->td_rb_list != 0 || td->td_rbp_list != 0 ||
4591 td->td_rb_inact != 0))
4593 } else if ((td->td_pflags2 & TDP2_COMPAT32RB) != 0) {
4597 bzero(&rb, sizeof(rb));
4598 error = ops->copyin_robust_lists(uap->uaddr1, uap->val, &rb);
4603 td->td_pflags2 |= TDP2_COMPAT32RB;
4605 td->td_rb_list = rb.robust_list_offset;
4606 td->td_rbp_list = rb.robust_priv_list_offset;
4607 td->td_rb_inact = rb.robust_inact_offset;
4612 __umtx_op_get_min_timeout(struct thread *td, struct _umtx_op_args *uap,
4613 const struct umtx_copyops *ops)
4618 val = sbttons(td->td_proc->p_umtx_min_timeout);
4619 if (ops->compat32) {
4621 error = copyout(&val1, uap->uaddr1, sizeof(val1));
4623 error = copyout(&val, uap->uaddr1, sizeof(val));
4629 __umtx_op_set_min_timeout(struct thread *td, struct _umtx_op_args *uap,
4630 const struct umtx_copyops *ops)
4634 td->td_proc->p_umtx_min_timeout = nstosbt(uap->val);
4638 #if defined(__i386__) || defined(__amd64__)
4640 * Provide the standard 32-bit definitions for x86, since native/compat32 use a
4641 * 32-bit time_t there. Other architectures just need the i386 definitions
4642 * along with their standard compat32.
4644 struct timespecx32 {
4649 struct umtx_timex32 {
4650 struct timespecx32 _timeout;
4656 #define timespeci386 timespec32
4657 #define umtx_timei386 umtx_time32
4659 #else /* !__i386__ && !__amd64__ */
4660 /* 32-bit architectures can emulate i386, so define these almost everywhere. */
4661 struct timespeci386 {
4666 struct umtx_timei386 {
4667 struct timespeci386 _timeout;
4672 #if defined(__LP64__)
4673 #define timespecx32 timespec32
4674 #define umtx_timex32 umtx_time32
4679 umtx_copyin_robust_lists32(const void *uaddr, size_t size,
4680 struct umtx_robust_lists_params *rbp)
4682 struct umtx_robust_lists_params_compat32 rb32;
4685 if (size > sizeof(rb32))
4687 bzero(&rb32, sizeof(rb32));
4688 error = copyin(uaddr, &rb32, size);
4691 CP(rb32, *rbp, robust_list_offset);
4692 CP(rb32, *rbp, robust_priv_list_offset);
4693 CP(rb32, *rbp, robust_inact_offset);
4699 umtx_copyin_timeouti386(const void *uaddr, struct timespec *tsp)
4701 struct timespeci386 ts32;
4704 error = copyin(uaddr, &ts32, sizeof(ts32));
4706 if (!timespecvalid_interval(&ts32))
4709 CP(ts32, *tsp, tv_sec);
4710 CP(ts32, *tsp, tv_nsec);
4717 umtx_copyin_umtx_timei386(const void *uaddr, size_t size, struct _umtx_time *tp)
4719 struct umtx_timei386 t32;
4722 t32._clockid = CLOCK_REALTIME;
4724 if (size <= sizeof(t32._timeout))
4725 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4727 error = copyin(uaddr, &t32, sizeof(t32));
4730 if (!timespecvalid_interval(&t32._timeout))
4732 TS_CP(t32, *tp, _timeout);
4733 CP(t32, *tp, _flags);
4734 CP(t32, *tp, _clockid);
4739 umtx_copyout_timeouti386(void *uaddr, size_t sz, struct timespec *tsp)
4741 struct timespeci386 remain32 = {
4742 .tv_sec = tsp->tv_sec,
4743 .tv_nsec = tsp->tv_nsec,
4747 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4748 * and we're only called if sz >= sizeof(timespec) as supplied in the
4751 KASSERT(sz >= sizeof(remain32),
4752 ("umtx_copyops specifies incorrect sizes"));
4754 return (copyout(&remain32, uaddr, sizeof(remain32)));
4756 #endif /* !__i386__ */
4758 #if defined(__i386__) || defined(__LP64__)
4760 umtx_copyin_timeoutx32(const void *uaddr, struct timespec *tsp)
4762 struct timespecx32 ts32;
4765 error = copyin(uaddr, &ts32, sizeof(ts32));
4767 if (!timespecvalid_interval(&ts32))
4770 CP(ts32, *tsp, tv_sec);
4771 CP(ts32, *tsp, tv_nsec);
4778 umtx_copyin_umtx_timex32(const void *uaddr, size_t size, struct _umtx_time *tp)
4780 struct umtx_timex32 t32;
4783 t32._clockid = CLOCK_REALTIME;
4785 if (size <= sizeof(t32._timeout))
4786 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4788 error = copyin(uaddr, &t32, sizeof(t32));
4791 if (!timespecvalid_interval(&t32._timeout))
4793 TS_CP(t32, *tp, _timeout);
4794 CP(t32, *tp, _flags);
4795 CP(t32, *tp, _clockid);
4800 umtx_copyout_timeoutx32(void *uaddr, size_t sz, struct timespec *tsp)
4802 struct timespecx32 remain32 = {
4803 .tv_sec = tsp->tv_sec,
4804 .tv_nsec = tsp->tv_nsec,
4808 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4809 * and we're only called if sz >= sizeof(timespec) as supplied in the
4812 KASSERT(sz >= sizeof(remain32),
4813 ("umtx_copyops specifies incorrect sizes"));
4815 return (copyout(&remain32, uaddr, sizeof(remain32)));
4817 #endif /* __i386__ || __LP64__ */
4819 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap,
4820 const struct umtx_copyops *umtx_ops);
4822 static const _umtx_op_func op_table[] = {
4823 #ifdef COMPAT_FREEBSD10
4824 [UMTX_OP_LOCK] = __umtx_op_lock_umtx,
4825 [UMTX_OP_UNLOCK] = __umtx_op_unlock_umtx,
4827 [UMTX_OP_LOCK] = __umtx_op_unimpl,
4828 [UMTX_OP_UNLOCK] = __umtx_op_unimpl,
4830 [UMTX_OP_WAIT] = __umtx_op_wait,
4831 [UMTX_OP_WAKE] = __umtx_op_wake,
4832 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4833 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
4834 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4835 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4836 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
4837 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4838 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4839 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
4840 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
4841 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
4842 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4843 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4844 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4845 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4846 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4847 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4848 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4849 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4851 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4852 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4854 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4855 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4856 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4857 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4858 [UMTX_OP_SHM] = __umtx_op_shm,
4859 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4860 [UMTX_OP_GET_MIN_TIMEOUT] = __umtx_op_get_min_timeout,
4861 [UMTX_OP_SET_MIN_TIMEOUT] = __umtx_op_set_min_timeout,
4864 static const struct umtx_copyops umtx_native_ops = {
4865 .copyin_timeout = umtx_copyin_timeout,
4866 .copyin_umtx_time = umtx_copyin_umtx_time,
4867 .copyin_robust_lists = umtx_copyin_robust_lists,
4868 .copyout_timeout = umtx_copyout_timeout,
4869 .timespec_sz = sizeof(struct timespec),
4870 .umtx_time_sz = sizeof(struct _umtx_time),
4874 static const struct umtx_copyops umtx_native_opsi386 = {
4875 .copyin_timeout = umtx_copyin_timeouti386,
4876 .copyin_umtx_time = umtx_copyin_umtx_timei386,
4877 .copyin_robust_lists = umtx_copyin_robust_lists32,
4878 .copyout_timeout = umtx_copyout_timeouti386,
4879 .timespec_sz = sizeof(struct timespeci386),
4880 .umtx_time_sz = sizeof(struct umtx_timei386),
4885 #if defined(__i386__) || defined(__LP64__)
4886 /* i386 can emulate other 32-bit archs, too! */
4887 static const struct umtx_copyops umtx_native_opsx32 = {
4888 .copyin_timeout = umtx_copyin_timeoutx32,
4889 .copyin_umtx_time = umtx_copyin_umtx_timex32,
4890 .copyin_robust_lists = umtx_copyin_robust_lists32,
4891 .copyout_timeout = umtx_copyout_timeoutx32,
4892 .timespec_sz = sizeof(struct timespecx32),
4893 .umtx_time_sz = sizeof(struct umtx_timex32),
4897 #ifdef COMPAT_FREEBSD32
4899 #define umtx_native_ops32 umtx_native_opsi386
4901 #define umtx_native_ops32 umtx_native_opsx32
4903 #endif /* COMPAT_FREEBSD32 */
4904 #endif /* __i386__ || __LP64__ */
4906 #define UMTX_OP__FLAGS (UMTX_OP__32BIT | UMTX_OP__I386)
4909 kern__umtx_op(struct thread *td, void *obj, int op, unsigned long val,
4910 void *uaddr1, void *uaddr2, const struct umtx_copyops *ops)
4912 struct _umtx_op_args uap = {
4914 .op = op & ~UMTX_OP__FLAGS,
4920 if ((uap.op >= nitems(op_table)))
4922 return ((*op_table[uap.op])(td, &uap, ops));
4926 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4928 static const struct umtx_copyops *umtx_ops;
4930 umtx_ops = &umtx_native_ops;
4932 if ((uap->op & (UMTX_OP__32BIT | UMTX_OP__I386)) != 0) {
4933 if ((uap->op & UMTX_OP__I386) != 0)
4934 umtx_ops = &umtx_native_opsi386;
4936 umtx_ops = &umtx_native_opsx32;
4938 #elif !defined(__i386__)
4939 /* We consider UMTX_OP__32BIT a nop on !i386 ILP32. */
4940 if ((uap->op & UMTX_OP__I386) != 0)
4941 umtx_ops = &umtx_native_opsi386;
4943 /* Likewise, UMTX_OP__I386 is a nop on i386. */
4944 if ((uap->op & UMTX_OP__32BIT) != 0)
4945 umtx_ops = &umtx_native_opsx32;
4947 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr1,
4948 uap->uaddr2, umtx_ops));
4951 #ifdef COMPAT_FREEBSD32
4952 #ifdef COMPAT_FREEBSD10
4954 freebsd10_freebsd32__umtx_lock(struct thread *td,
4955 struct freebsd10_freebsd32__umtx_lock_args *uap)
4957 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
4961 freebsd10_freebsd32__umtx_unlock(struct thread *td,
4962 struct freebsd10_freebsd32__umtx_unlock_args *uap)
4964 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
4966 #endif /* COMPAT_FREEBSD10 */
4969 freebsd32__umtx_op(struct thread *td, struct freebsd32__umtx_op_args *uap)
4972 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr1,
4973 uap->uaddr2, &umtx_native_ops32));
4975 #endif /* COMPAT_FREEBSD32 */
4978 umtx_thread_init(struct thread *td)
4981 td->td_umtxq = umtxq_alloc();
4982 td->td_umtxq->uq_thread = td;
4986 umtx_thread_fini(struct thread *td)
4989 umtxq_free(td->td_umtxq);
4993 * It will be called when new thread is created, e.g fork().
4996 umtx_thread_alloc(struct thread *td)
5001 uq->uq_inherited_pri = PRI_MAX;
5003 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
5004 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
5005 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
5006 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
5012 * Clear robust lists for all process' threads, not delaying the
5013 * cleanup to thread exit, since the relevant address space is
5014 * destroyed right now.
5017 umtx_exec(struct proc *p)
5021 KASSERT(p == curproc, ("need curproc"));
5022 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
5023 (p->p_flag & P_STOPPED_SINGLE) != 0,
5024 ("curproc must be single-threaded"));
5026 * There is no need to lock the list as only this thread can be
5029 FOREACH_THREAD_IN_PROC(p, td) {
5030 KASSERT(td == curthread ||
5031 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
5032 ("running thread %p %p", p, td));
5033 umtx_thread_cleanup(td);
5034 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
5037 p->p_umtx_min_timeout = 0;
5044 umtx_thread_exit(struct thread *td)
5047 umtx_thread_cleanup(td);
5051 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res, bool compat32)
5058 error = fueword32((void *)ptr, &res32);
5062 error = fueword((void *)ptr, &res1);
5072 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list,
5075 struct umutex32 m32;
5078 memcpy(&m32, m, sizeof(m32));
5079 *rb_list = m32.m_rb_lnk;
5081 *rb_list = m->m_rb_lnk;
5086 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact,
5092 KASSERT(td->td_proc == curproc, ("need current vmspace"));
5093 error = copyin((void *)rbp, &m, sizeof(m));
5096 if (rb_list != NULL)
5097 umtx_read_rb_list(td, &m, rb_list, compat32);
5098 if ((m.m_flags & UMUTEX_ROBUST) == 0)
5100 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
5101 /* inact is cleared after unlock, allow the inconsistency */
5102 return (inact ? 0 : EINVAL);
5103 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
5107 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
5108 const char *name, bool compat32)
5116 error = umtx_read_uptr(td, rb_list, &rbp, compat32);
5117 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
5118 if (rbp == *rb_inact) {
5123 error = umtx_handle_rb(td, rbp, &rbp, inact, compat32);
5125 if (i == umtx_max_rb && umtx_verbose_rb) {
5126 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
5127 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
5129 if (error != 0 && umtx_verbose_rb) {
5130 uprintf("comm %s pid %d: handling %srb error %d\n",
5131 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
5136 * Clean up umtx data.
5139 umtx_thread_cleanup(struct thread *td)
5147 * Disown pi mutexes.
5151 if (uq->uq_inherited_pri != PRI_MAX ||
5152 !TAILQ_EMPTY(&uq->uq_pi_contested)) {
5153 mtx_lock(&umtx_lock);
5154 uq->uq_inherited_pri = PRI_MAX;
5155 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
5156 pi->pi_owner = NULL;
5157 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
5159 mtx_unlock(&umtx_lock);
5161 sched_lend_user_prio_cond(td, PRI_MAX);
5164 compat32 = (td->td_pflags2 & TDP2_COMPAT32RB) != 0;
5165 td->td_pflags2 &= ~TDP2_COMPAT32RB;
5167 if (td->td_rb_inact == 0 && td->td_rb_list == 0 && td->td_rbp_list == 0)
5171 * Handle terminated robust mutexes. Must be done after
5172 * robust pi disown, otherwise unlock could see unowned
5175 rb_inact = td->td_rb_inact;
5177 (void)umtx_read_uptr(td, rb_inact, &rb_inact, compat32);
5178 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "", compat32);
5179 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ", compat32);
5181 (void)umtx_handle_rb(td, rb_inact, NULL, true, compat32);