2 * SPDX-License-Identifier: BSD-2-Clause
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 #include "opt_umtx_profiling.h"
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/fcntl.h>
41 #include <sys/filedesc.h>
42 #include <sys/limits.h>
44 #include <sys/malloc.h>
46 #include <sys/mutex.h>
49 #include <sys/resource.h>
50 #include <sys/resourcevar.h>
51 #include <sys/rwlock.h>
53 #include <sys/sched.h>
55 #include <sys/sysctl.h>
56 #include <sys/systm.h>
57 #include <sys/sysproto.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/taskqueue.h>
61 #include <sys/eventhandler.h>
63 #include <sys/umtxvar.h>
65 #include <security/mac/mac_framework.h>
68 #include <vm/vm_param.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
73 #include <machine/atomic.h>
74 #include <machine/cpu.h>
76 #include <compat/freebsd32/freebsd32.h>
77 #ifdef COMPAT_FREEBSD32
78 #include <compat/freebsd32/freebsd32_proto.h>
82 #define _UMUTEX_WAIT 2
85 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
86 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
89 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
91 #define UMTXQ_ASSERT_LOCKED_BUSY(key) do { \
92 struct umtxq_chain *uc; \
94 uc = umtxq_getchain(key); \
95 mtx_assert(&uc->uc_lock, MA_OWNED); \
96 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy")); \
99 #define UMTXQ_ASSERT_LOCKED_BUSY(key) do {} while (0)
103 * Don't propagate time-sharing priority, there is a security reason,
104 * a user can simply introduce PI-mutex, let thread A lock the mutex,
105 * and let another thread B block on the mutex, because B is
106 * sleeping, its priority will be boosted, this causes A's priority to
107 * be boosted via priority propagating too and will never be lowered even
108 * if it is using 100%CPU, this is unfair to other processes.
111 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
112 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
113 PRI_MAX_TIMESHARE : (td)->td_user_pri)
115 #define GOLDEN_RATIO_PRIME 2654404609U
117 #define UMTX_CHAINS 512
119 #define UMTX_SHIFTS (__WORD_BIT - 9)
121 #define GET_SHARE(flags) \
122 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
124 #define BUSY_SPINS 200
126 struct umtx_copyops {
127 int (*copyin_timeout)(const void *uaddr, struct timespec *tsp);
128 int (*copyin_umtx_time)(const void *uaddr, size_t size,
129 struct _umtx_time *tp);
130 int (*copyin_robust_lists)(const void *uaddr, size_t size,
131 struct umtx_robust_lists_params *rbp);
132 int (*copyout_timeout)(void *uaddr, size_t size,
133 struct timespec *tsp);
134 const size_t timespec_sz;
135 const size_t umtx_time_sz;
139 _Static_assert(sizeof(struct umutex) == sizeof(struct umutex32), "umutex32");
140 _Static_assert(__offsetof(struct umutex, m_spare[0]) ==
141 __offsetof(struct umutex32, m_spare[0]), "m_spare32");
143 int umtx_shm_vnobj_persistent = 0;
144 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_vnode_persistent, CTLFLAG_RWTUN,
145 &umtx_shm_vnobj_persistent, 0,
146 "False forces destruction of umtx attached to file, on last close");
147 static int umtx_max_rb = 1000;
148 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_max_robust, CTLFLAG_RWTUN,
150 "Maximum number of robust mutexes allowed for each thread");
152 static uma_zone_t umtx_pi_zone;
153 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
154 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
155 static int umtx_pi_allocated;
157 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
159 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
160 &umtx_pi_allocated, 0, "Allocated umtx_pi");
161 static int umtx_verbose_rb = 1;
162 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
166 #ifdef UMTX_PROFILING
167 static long max_length;
168 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
169 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
173 static inline void umtx_abs_timeout_init2(struct umtx_abs_timeout *timo,
174 const struct _umtx_time *umtxtime);
176 static void umtx_shm_init(void);
177 static void umtxq_sysinit(void *);
178 static void umtxq_hash(struct umtx_key *key);
179 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
181 static void umtx_thread_cleanup(struct thread *td);
182 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
184 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
186 static struct mtx umtx_lock;
188 #ifdef UMTX_PROFILING
190 umtx_init_profiling(void)
192 struct sysctl_oid *chain_oid;
196 for (i = 0; i < UMTX_CHAINS; ++i) {
197 snprintf(chain_name, sizeof(chain_name), "%d", i);
198 chain_oid = SYSCTL_ADD_NODE(NULL,
199 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
200 chain_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
202 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
203 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
204 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
205 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
210 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
214 struct umtxq_chain *uc;
215 u_int fract, i, j, tot, whole;
216 u_int sf0, sf1, sf2, sf3, sf4;
217 u_int si0, si1, si2, si3, si4;
218 u_int sw0, sw1, sw2, sw3, sw4;
220 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
221 for (i = 0; i < 2; i++) {
223 for (j = 0; j < UMTX_CHAINS; ++j) {
224 uc = &umtxq_chains[i][j];
225 mtx_lock(&uc->uc_lock);
226 tot += uc->max_length;
227 mtx_unlock(&uc->uc_lock);
230 sbuf_printf(&sb, "%u) Empty ", i);
232 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
233 si0 = si1 = si2 = si3 = si4 = 0;
234 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
235 for (j = 0; j < UMTX_CHAINS; j++) {
236 uc = &umtxq_chains[i][j];
237 mtx_lock(&uc->uc_lock);
238 whole = uc->max_length * 100;
239 mtx_unlock(&uc->uc_lock);
240 fract = (whole % tot) * 100;
241 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
245 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
250 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
255 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
260 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
267 sbuf_printf(&sb, "queue %u:\n", i);
268 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
270 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
272 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
274 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
276 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
282 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
288 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
290 struct umtxq_chain *uc;
295 error = sysctl_handle_int(oidp, &clear, 0, req);
296 if (error != 0 || req->newptr == NULL)
300 for (i = 0; i < 2; ++i) {
301 for (j = 0; j < UMTX_CHAINS; ++j) {
302 uc = &umtxq_chains[i][j];
303 mtx_lock(&uc->uc_lock);
306 mtx_unlock(&uc->uc_lock);
313 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
314 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
315 sysctl_debug_umtx_chains_clear, "I",
316 "Clear umtx chains statistics");
317 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
318 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
319 sysctl_debug_umtx_chains_peaks, "A",
320 "Highest peaks in chains max length");
324 umtxq_sysinit(void *arg __unused)
328 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
329 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
330 for (i = 0; i < 2; ++i) {
331 for (j = 0; j < UMTX_CHAINS; ++j) {
332 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
333 MTX_DEF | MTX_DUPOK);
334 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
335 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
336 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
337 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
338 umtxq_chains[i][j].uc_busy = 0;
339 umtxq_chains[i][j].uc_waiters = 0;
340 #ifdef UMTX_PROFILING
341 umtxq_chains[i][j].length = 0;
342 umtxq_chains[i][j].max_length = 0;
346 #ifdef UMTX_PROFILING
347 umtx_init_profiling();
349 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
358 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
359 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
361 TAILQ_INIT(&uq->uq_spare_queue->head);
362 TAILQ_INIT(&uq->uq_pi_contested);
363 uq->uq_inherited_pri = PRI_MAX;
368 umtxq_free(struct umtx_q *uq)
371 MPASS(uq->uq_spare_queue != NULL);
372 free(uq->uq_spare_queue, M_UMTX);
377 umtxq_hash(struct umtx_key *key)
381 n = (uintptr_t)key->info.both.a + key->info.both.b;
382 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
386 umtxq_getchain(struct umtx_key *key)
389 if (key->type <= TYPE_SEM)
390 return (&umtxq_chains[1][key->hash]);
391 return (&umtxq_chains[0][key->hash]);
395 * Set chain to busy state when following operation
396 * may be blocked (kernel mutex can not be used).
399 umtxq_busy(struct umtx_key *key)
401 struct umtxq_chain *uc;
403 uc = umtxq_getchain(key);
404 mtx_assert(&uc->uc_lock, MA_OWNED);
408 int count = BUSY_SPINS;
411 while (uc->uc_busy && --count > 0)
417 while (uc->uc_busy) {
419 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
430 umtxq_unbusy(struct umtx_key *key)
432 struct umtxq_chain *uc;
434 uc = umtxq_getchain(key);
435 mtx_assert(&uc->uc_lock, MA_OWNED);
436 KASSERT(uc->uc_busy != 0, ("not busy"));
443 umtxq_unbusy_unlocked(struct umtx_key *key)
451 static struct umtxq_queue *
452 umtxq_queue_lookup(struct umtx_key *key, int q)
454 struct umtxq_queue *uh;
455 struct umtxq_chain *uc;
457 uc = umtxq_getchain(key);
458 UMTXQ_LOCKED_ASSERT(uc);
459 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
460 if (umtx_key_match(&uh->key, key))
468 umtxq_insert_queue(struct umtx_q *uq, int q)
470 struct umtxq_queue *uh;
471 struct umtxq_chain *uc;
473 uc = umtxq_getchain(&uq->uq_key);
474 UMTXQ_LOCKED_ASSERT(uc);
475 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
476 uh = umtxq_queue_lookup(&uq->uq_key, q);
478 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
480 uh = uq->uq_spare_queue;
481 uh->key = uq->uq_key;
482 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
483 #ifdef UMTX_PROFILING
485 if (uc->length > uc->max_length) {
486 uc->max_length = uc->length;
487 if (uc->max_length > max_length)
488 max_length = uc->max_length;
492 uq->uq_spare_queue = NULL;
494 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
496 uq->uq_flags |= UQF_UMTXQ;
497 uq->uq_cur_queue = uh;
502 umtxq_remove_queue(struct umtx_q *uq, int q)
504 struct umtxq_chain *uc;
505 struct umtxq_queue *uh;
507 uc = umtxq_getchain(&uq->uq_key);
508 UMTXQ_LOCKED_ASSERT(uc);
509 if (uq->uq_flags & UQF_UMTXQ) {
510 uh = uq->uq_cur_queue;
511 TAILQ_REMOVE(&uh->head, uq, uq_link);
513 uq->uq_flags &= ~UQF_UMTXQ;
514 if (TAILQ_EMPTY(&uh->head)) {
515 KASSERT(uh->length == 0,
516 ("inconsistent umtxq_queue length"));
517 #ifdef UMTX_PROFILING
520 LIST_REMOVE(uh, link);
522 uh = LIST_FIRST(&uc->uc_spare_queue);
523 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
524 LIST_REMOVE(uh, link);
526 uq->uq_spare_queue = uh;
527 uq->uq_cur_queue = NULL;
532 * Check if there are multiple waiters
535 umtxq_count(struct umtx_key *key)
537 struct umtxq_queue *uh;
539 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
540 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
547 * Check if there are multiple PI waiters and returns first
551 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
553 struct umtxq_queue *uh;
556 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
557 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
559 *first = TAILQ_FIRST(&uh->head);
566 * Wake up threads waiting on an userland object by a bit mask.
569 umtxq_signal_mask(struct umtx_key *key, int n_wake, u_int bitset)
571 struct umtxq_queue *uh;
572 struct umtx_q *uq, *uq_temp;
576 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
577 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
580 TAILQ_FOREACH_SAFE(uq, &uh->head, uq_link, uq_temp) {
581 if ((uq->uq_bitset & bitset) == 0)
583 umtxq_remove_queue(uq, UMTX_SHARED_QUEUE);
592 * Wake up threads waiting on an userland object.
596 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
598 struct umtxq_queue *uh;
603 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
604 uh = umtxq_queue_lookup(key, q);
606 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
607 umtxq_remove_queue(uq, q);
617 * Wake up specified thread.
620 umtxq_signal_thread(struct umtx_q *uq)
623 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
629 * Wake up a maximum of n_wake threads that are waiting on an userland
630 * object identified by key. The remaining threads are removed from queue
631 * identified by key and added to the queue identified by key2 (requeued).
632 * The n_requeue specifies an upper limit on the number of threads that
633 * are requeued to the second queue.
636 umtxq_requeue(struct umtx_key *key, int n_wake, struct umtx_key *key2,
639 struct umtxq_queue *uh;
640 struct umtx_q *uq, *uq_temp;
644 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
645 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key2));
646 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
649 TAILQ_FOREACH_SAFE(uq, &uh->head, uq_link, uq_temp) {
650 if (++ret <= n_wake) {
657 if (ret - n_wake == n_requeue)
665 tstohz(const struct timespec *tsp)
669 TIMESPEC_TO_TIMEVAL(&tv, tsp);
674 umtx_abs_timeout_init(struct umtx_abs_timeout *timo, int clockid,
675 int absolute, const struct timespec *timeout)
678 timo->clockid = clockid;
680 timo->is_abs_real = false;
681 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
682 timespecadd(&timo->cur, timeout, &timo->end);
684 timo->end = *timeout;
685 timo->is_abs_real = clockid == CLOCK_REALTIME ||
686 clockid == CLOCK_REALTIME_FAST ||
687 clockid == CLOCK_REALTIME_PRECISE ||
688 clockid == CLOCK_SECOND;
693 umtx_abs_timeout_init2(struct umtx_abs_timeout *timo,
694 const struct _umtx_time *umtxtime)
697 umtx_abs_timeout_init(timo, umtxtime->_clockid,
698 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
702 umtx_abs_timeout_enforce_min(sbintime_t *sbt)
704 sbintime_t when, mint;
706 mint = curproc->p_umtx_min_timeout;
707 if (__predict_false(mint != 0)) {
708 when = sbinuptime() + mint;
715 umtx_abs_timeout_getsbt(struct umtx_abs_timeout *timo, sbintime_t *sbt,
718 struct bintime bt, bbt;
722 switch (timo->clockid) {
724 /* Clocks that can be converted into absolute time. */
726 case CLOCK_REALTIME_PRECISE:
727 case CLOCK_REALTIME_FAST:
728 case CLOCK_MONOTONIC:
729 case CLOCK_MONOTONIC_PRECISE:
730 case CLOCK_MONOTONIC_FAST:
732 case CLOCK_UPTIME_PRECISE:
733 case CLOCK_UPTIME_FAST:
735 timespec2bintime(&timo->end, &bt);
736 switch (timo->clockid) {
738 case CLOCK_REALTIME_PRECISE:
739 case CLOCK_REALTIME_FAST:
741 getboottimebin(&bbt);
742 bintime_sub(&bt, &bbt);
747 if (bt.sec >= (SBT_MAX >> 32)) {
753 umtx_abs_timeout_enforce_min(sbt);
756 * Check if the absolute time should be aligned to
757 * avoid firing multiple timer events in non-periodic
760 switch (timo->clockid) {
761 case CLOCK_REALTIME_FAST:
762 case CLOCK_MONOTONIC_FAST:
763 case CLOCK_UPTIME_FAST:
764 rem = *sbt % tc_tick_sbt;
765 if (__predict_true(rem != 0))
766 *sbt += tc_tick_sbt - rem;
770 if (__predict_true(rem != 0))
771 *sbt += SBT_1S - rem;
777 /* Clocks that has to be periodically polled. */
780 case CLOCK_THREAD_CPUTIME_ID:
781 case CLOCK_PROCESS_CPUTIME_ID:
783 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
784 if (timespeccmp(&timo->end, &timo->cur, <=))
786 timespecsub(&timo->end, &timo->cur, &tts);
787 *sbt = tick_sbt * tstohz(&tts);
788 *flags = C_HARDCLOCK;
794 umtx_unlock_val(uint32_t flags, bool rb)
798 return (UMUTEX_RB_OWNERDEAD);
799 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
800 return (UMUTEX_RB_NOTRECOV);
802 return (UMUTEX_UNOWNED);
807 * Put thread into sleep state, before sleeping, check if
808 * thread was removed from umtx queue.
811 umtxq_sleep(struct umtx_q *uq, const char *wmesg,
812 struct umtx_abs_timeout *timo)
814 struct umtxq_chain *uc;
816 int error, flags = 0;
818 uc = umtxq_getchain(&uq->uq_key);
819 UMTXQ_LOCKED_ASSERT(uc);
821 if (!(uq->uq_flags & UQF_UMTXQ)) {
826 if (timo->is_abs_real)
827 curthread->td_rtcgen =
828 atomic_load_acq_int(&rtc_generation);
829 error = umtx_abs_timeout_getsbt(timo, &sbt, &flags);
833 error = msleep_sbt(uq, &uc->uc_lock, PCATCH | PDROP, wmesg,
835 uc = umtxq_getchain(&uq->uq_key);
836 mtx_lock(&uc->uc_lock);
837 if (error == EINTR || error == ERESTART)
839 if (error == EWOULDBLOCK && (flags & C_ABSOLUTE) != 0) {
845 curthread->td_rtcgen = 0;
850 * Convert userspace address into unique logical address.
853 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
855 struct thread *td = curthread;
857 vm_map_entry_t entry;
863 if (share == THREAD_SHARE) {
865 key->info.private.vs = td->td_proc->p_vmspace;
866 key->info.private.addr = (uintptr_t)addr;
868 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
869 map = &td->td_proc->p_vmspace->vm_map;
870 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
871 &entry, &key->info.shared.object, &pindex, &prot,
872 &wired) != KERN_SUCCESS) {
876 if ((share == PROCESS_SHARE) ||
877 (share == AUTO_SHARE &&
878 VM_INHERIT_SHARE == entry->inheritance)) {
880 key->info.shared.offset = (vm_offset_t)addr -
881 entry->start + entry->offset;
882 vm_object_reference(key->info.shared.object);
885 key->info.private.vs = td->td_proc->p_vmspace;
886 key->info.private.addr = (uintptr_t)addr;
888 vm_map_lookup_done(map, entry);
899 umtx_key_release(struct umtx_key *key)
902 vm_object_deallocate(key->info.shared.object);
905 #ifdef COMPAT_FREEBSD10
907 * Lock a umtx object.
910 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
911 const struct timespec *timeout)
913 struct umtx_abs_timeout timo;
921 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
924 * Care must be exercised when dealing with umtx structure. It
925 * can fault on any access.
929 * Try the uncontested case. This should be done in userland.
931 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
933 /* The acquire succeeded. */
934 if (owner == UMTX_UNOWNED)
937 /* The address was invalid. */
941 /* If no one owns it but it is contested try to acquire it. */
942 if (owner == UMTX_CONTESTED) {
943 owner = casuword(&umtx->u_owner,
944 UMTX_CONTESTED, id | UMTX_CONTESTED);
946 if (owner == UMTX_CONTESTED)
949 /* The address was invalid. */
953 error = thread_check_susp(td, false);
957 /* If this failed the lock has changed, restart. */
962 * If we caught a signal, we have retried and now
968 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
969 AUTO_SHARE, &uq->uq_key)) != 0)
972 umtxq_lock(&uq->uq_key);
973 umtxq_busy(&uq->uq_key);
975 umtxq_unbusy(&uq->uq_key);
976 umtxq_unlock(&uq->uq_key);
979 * Set the contested bit so that a release in user space
980 * knows to use the system call for unlock. If this fails
981 * either some one else has acquired the lock or it has been
984 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
986 /* The address was invalid. */
988 umtxq_lock(&uq->uq_key);
990 umtxq_unlock(&uq->uq_key);
991 umtx_key_release(&uq->uq_key);
996 * We set the contested bit, sleep. Otherwise the lock changed
997 * and we need to retry or we lost a race to the thread
998 * unlocking the umtx.
1000 umtxq_lock(&uq->uq_key);
1002 error = umtxq_sleep(uq, "umtx", timeout == NULL ? NULL :
1005 umtxq_unlock(&uq->uq_key);
1006 umtx_key_release(&uq->uq_key);
1009 error = thread_check_susp(td, false);
1012 if (timeout == NULL) {
1013 /* Mutex locking is restarted if it is interrupted. */
1017 /* Timed-locking is not restarted. */
1018 if (error == ERESTART)
1025 * Unlock a umtx object.
1028 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
1030 struct umtx_key key;
1037 * Make sure we own this mtx.
1039 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
1043 if ((owner & ~UMTX_CONTESTED) != id)
1046 /* This should be done in userland */
1047 if ((owner & UMTX_CONTESTED) == 0) {
1048 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
1056 /* We should only ever be in here for contested locks */
1057 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1063 count = umtxq_count(&key);
1067 * When unlocking the umtx, it must be marked as unowned if
1068 * there is zero or one thread only waiting for it.
1069 * Otherwise, it must be marked as contested.
1071 old = casuword(&umtx->u_owner, owner,
1072 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
1074 umtxq_signal(&key,1);
1077 umtx_key_release(&key);
1085 #ifdef COMPAT_FREEBSD32
1088 * Lock a umtx object.
1091 do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id,
1092 const struct timespec *timeout)
1094 struct umtx_abs_timeout timo;
1102 if (timeout != NULL)
1103 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
1106 * Care must be exercised when dealing with umtx structure. It
1107 * can fault on any access.
1111 * Try the uncontested case. This should be done in userland.
1113 owner = casuword32(m, UMUTEX_UNOWNED, id);
1115 /* The acquire succeeded. */
1116 if (owner == UMUTEX_UNOWNED)
1119 /* The address was invalid. */
1123 /* If no one owns it but it is contested try to acquire it. */
1124 if (owner == UMUTEX_CONTESTED) {
1125 owner = casuword32(m,
1126 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1127 if (owner == UMUTEX_CONTESTED)
1130 /* The address was invalid. */
1134 error = thread_check_susp(td, false);
1138 /* If this failed the lock has changed, restart. */
1143 * If we caught a signal, we have retried and now
1149 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
1150 AUTO_SHARE, &uq->uq_key)) != 0)
1153 umtxq_lock(&uq->uq_key);
1154 umtxq_busy(&uq->uq_key);
1156 umtxq_unbusy(&uq->uq_key);
1157 umtxq_unlock(&uq->uq_key);
1160 * Set the contested bit so that a release in user space
1161 * knows to use the system call for unlock. If this fails
1162 * either some one else has acquired the lock or it has been
1165 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
1167 /* The address was invalid. */
1169 umtxq_lock(&uq->uq_key);
1171 umtxq_unlock(&uq->uq_key);
1172 umtx_key_release(&uq->uq_key);
1177 * We set the contested bit, sleep. Otherwise the lock changed
1178 * and we need to retry or we lost a race to the thread
1179 * unlocking the umtx.
1181 umtxq_lock(&uq->uq_key);
1183 error = umtxq_sleep(uq, "umtx", timeout == NULL ?
1186 umtxq_unlock(&uq->uq_key);
1187 umtx_key_release(&uq->uq_key);
1190 error = thread_check_susp(td, false);
1193 if (timeout == NULL) {
1194 /* Mutex locking is restarted if it is interrupted. */
1198 /* Timed-locking is not restarted. */
1199 if (error == ERESTART)
1206 * Unlock a umtx object.
1209 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
1211 struct umtx_key key;
1218 * Make sure we own this mtx.
1220 owner = fuword32(m);
1224 if ((owner & ~UMUTEX_CONTESTED) != id)
1227 /* This should be done in userland */
1228 if ((owner & UMUTEX_CONTESTED) == 0) {
1229 old = casuword32(m, owner, UMUTEX_UNOWNED);
1237 /* We should only ever be in here for contested locks */
1238 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1244 count = umtxq_count(&key);
1248 * When unlocking the umtx, it must be marked as unowned if
1249 * there is zero or one thread only waiting for it.
1250 * Otherwise, it must be marked as contested.
1252 old = casuword32(m, owner,
1253 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1255 umtxq_signal(&key,1);
1258 umtx_key_release(&key);
1265 #endif /* COMPAT_FREEBSD32 */
1266 #endif /* COMPAT_FREEBSD10 */
1269 * Fetch and compare value, sleep on the address if value is not changed.
1272 do_wait(struct thread *td, void *addr, u_long id,
1273 struct _umtx_time *timeout, int compat32, int is_private)
1275 struct umtx_abs_timeout timo;
1282 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
1283 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
1286 if (timeout != NULL)
1287 umtx_abs_timeout_init2(&timo, timeout);
1289 umtxq_lock(&uq->uq_key);
1291 umtxq_unlock(&uq->uq_key);
1292 if (compat32 == 0) {
1293 error = fueword(addr, &tmp);
1297 error = fueword32(addr, &tmp32);
1303 umtxq_lock(&uq->uq_key);
1306 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
1308 if ((uq->uq_flags & UQF_UMTXQ) == 0)
1312 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
1315 umtxq_unlock(&uq->uq_key);
1316 umtx_key_release(&uq->uq_key);
1317 if (error == ERESTART)
1323 * Wake up threads sleeping on the specified address.
1326 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1328 struct umtx_key key;
1331 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1332 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1335 umtxq_signal(&key, n_wake);
1337 umtx_key_release(&key);
1342 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1345 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1346 struct _umtx_time *timeout, int mode)
1348 struct umtx_abs_timeout timo;
1350 uint32_t owner, old, id;
1356 if (timeout != NULL)
1357 umtx_abs_timeout_init2(&timo, timeout);
1360 * Care must be exercised when dealing with umtx structure. It
1361 * can fault on any access.
1364 rv = fueword32(&m->m_owner, &owner);
1367 if (mode == _UMUTEX_WAIT) {
1368 if (owner == UMUTEX_UNOWNED ||
1369 owner == UMUTEX_CONTESTED ||
1370 owner == UMUTEX_RB_OWNERDEAD ||
1371 owner == UMUTEX_RB_NOTRECOV)
1375 * Robust mutex terminated. Kernel duty is to
1376 * return EOWNERDEAD to the userspace. The
1377 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1378 * by the common userspace code.
1380 if (owner == UMUTEX_RB_OWNERDEAD) {
1381 rv = casueword32(&m->m_owner,
1382 UMUTEX_RB_OWNERDEAD, &owner,
1383 id | UMUTEX_CONTESTED);
1387 MPASS(owner == UMUTEX_RB_OWNERDEAD);
1388 return (EOWNERDEAD); /* success */
1391 rv = thread_check_susp(td, false);
1396 if (owner == UMUTEX_RB_NOTRECOV)
1397 return (ENOTRECOVERABLE);
1400 * Try the uncontested case. This should be
1403 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1405 /* The address was invalid. */
1409 /* The acquire succeeded. */
1411 MPASS(owner == UMUTEX_UNOWNED);
1416 * If no one owns it but it is contested try
1420 if (owner == UMUTEX_CONTESTED) {
1421 rv = casueword32(&m->m_owner,
1422 UMUTEX_CONTESTED, &owner,
1423 id | UMUTEX_CONTESTED);
1424 /* The address was invalid. */
1428 MPASS(owner == UMUTEX_CONTESTED);
1432 rv = thread_check_susp(td, false);
1438 * If this failed the lock has
1444 /* rv == 1 but not contested, likely store failure */
1445 rv = thread_check_susp(td, false);
1450 if (mode == _UMUTEX_TRY)
1454 * If we caught a signal, we have retried and now
1460 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1461 GET_SHARE(flags), &uq->uq_key)) != 0)
1464 umtxq_lock(&uq->uq_key);
1465 umtxq_busy(&uq->uq_key);
1467 umtxq_unlock(&uq->uq_key);
1470 * Set the contested bit so that a release in user space
1471 * knows to use the system call for unlock. If this fails
1472 * either some one else has acquired the lock or it has been
1475 rv = casueword32(&m->m_owner, owner, &old,
1476 owner | UMUTEX_CONTESTED);
1478 /* The address was invalid or casueword failed to store. */
1479 if (rv == -1 || rv == 1) {
1480 umtxq_lock(&uq->uq_key);
1482 umtxq_unbusy(&uq->uq_key);
1483 umtxq_unlock(&uq->uq_key);
1484 umtx_key_release(&uq->uq_key);
1488 rv = thread_check_susp(td, false);
1496 * We set the contested bit, sleep. Otherwise the lock changed
1497 * and we need to retry or we lost a race to the thread
1498 * unlocking the umtx.
1500 umtxq_lock(&uq->uq_key);
1501 umtxq_unbusy(&uq->uq_key);
1502 MPASS(old == owner);
1503 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1506 umtxq_unlock(&uq->uq_key);
1507 umtx_key_release(&uq->uq_key);
1510 error = thread_check_susp(td, false);
1517 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1520 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1522 struct umtx_key key;
1523 uint32_t owner, old, id, newlock;
1530 * Make sure we own this mtx.
1532 error = fueword32(&m->m_owner, &owner);
1536 if ((owner & ~UMUTEX_CONTESTED) != id)
1539 newlock = umtx_unlock_val(flags, rb);
1540 if ((owner & UMUTEX_CONTESTED) == 0) {
1541 error = casueword32(&m->m_owner, owner, &old, newlock);
1545 error = thread_check_susp(td, false);
1550 MPASS(old == owner);
1554 /* We should only ever be in here for contested locks */
1555 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1561 count = umtxq_count(&key);
1565 * When unlocking the umtx, it must be marked as unowned if
1566 * there is zero or one thread only waiting for it.
1567 * Otherwise, it must be marked as contested.
1570 newlock |= UMUTEX_CONTESTED;
1571 error = casueword32(&m->m_owner, owner, &old, newlock);
1573 umtxq_signal(&key, 1);
1576 umtx_key_release(&key);
1582 error = thread_check_susp(td, false);
1591 * Check if the mutex is available and wake up a waiter,
1592 * only for simple mutex.
1595 do_wake_umutex(struct thread *td, struct umutex *m)
1597 struct umtx_key key;
1604 error = fueword32(&m->m_owner, &owner);
1608 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1609 owner != UMUTEX_RB_NOTRECOV)
1612 error = fueword32(&m->m_flags, &flags);
1616 /* We should only ever be in here for contested locks */
1617 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1623 count = umtxq_count(&key);
1626 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1627 owner != UMUTEX_RB_NOTRECOV) {
1628 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1632 } else if (error == 1) {
1636 umtx_key_release(&key);
1637 error = thread_check_susp(td, false);
1645 if (error == 0 && count != 0) {
1646 MPASS((owner & ~UMUTEX_CONTESTED) == 0 ||
1647 owner == UMUTEX_RB_OWNERDEAD ||
1648 owner == UMUTEX_RB_NOTRECOV);
1649 umtxq_signal(&key, 1);
1653 umtx_key_release(&key);
1658 * Check if the mutex has waiters and tries to fix contention bit.
1661 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1663 struct umtx_key key;
1664 uint32_t owner, old;
1669 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1673 type = TYPE_NORMAL_UMUTEX;
1675 case UMUTEX_PRIO_INHERIT:
1676 type = TYPE_PI_UMUTEX;
1678 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1679 type = TYPE_PI_ROBUST_UMUTEX;
1681 case UMUTEX_PRIO_PROTECT:
1682 type = TYPE_PP_UMUTEX;
1684 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1685 type = TYPE_PP_ROBUST_UMUTEX;
1690 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1696 count = umtxq_count(&key);
1699 error = fueword32(&m->m_owner, &owner);
1704 * Only repair contention bit if there is a waiter, this means
1705 * the mutex is still being referenced by userland code,
1706 * otherwise don't update any memory.
1708 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0 &&
1709 (count > 1 || (count == 1 && (owner & ~UMUTEX_CONTESTED) != 0))) {
1710 error = casueword32(&m->m_owner, owner, &old,
1711 owner | UMUTEX_CONTESTED);
1717 MPASS(old == owner);
1721 error = thread_check_susp(td, false);
1725 if (error == EFAULT) {
1726 umtxq_signal(&key, INT_MAX);
1727 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1728 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1729 umtxq_signal(&key, 1);
1732 umtx_key_release(&key);
1737 umtx_pi_alloc(int flags)
1741 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1742 TAILQ_INIT(&pi->pi_blocked);
1743 atomic_add_int(&umtx_pi_allocated, 1);
1748 umtx_pi_free(struct umtx_pi *pi)
1750 uma_zfree(umtx_pi_zone, pi);
1751 atomic_add_int(&umtx_pi_allocated, -1);
1755 * Adjust the thread's position on a pi_state after its priority has been
1759 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1761 struct umtx_q *uq, *uq1, *uq2;
1764 mtx_assert(&umtx_lock, MA_OWNED);
1771 * Check if the thread needs to be moved on the blocked chain.
1772 * It needs to be moved if either its priority is lower than
1773 * the previous thread or higher than the next thread.
1775 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1776 uq2 = TAILQ_NEXT(uq, uq_lockq);
1777 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1778 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1780 * Remove thread from blocked chain and determine where
1781 * it should be moved to.
1783 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1784 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1785 td1 = uq1->uq_thread;
1786 MPASS(td1->td_proc->p_magic == P_MAGIC);
1787 if (UPRI(td1) > UPRI(td))
1792 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1794 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1799 static struct umtx_pi *
1800 umtx_pi_next(struct umtx_pi *pi)
1802 struct umtx_q *uq_owner;
1804 if (pi->pi_owner == NULL)
1806 uq_owner = pi->pi_owner->td_umtxq;
1807 if (uq_owner == NULL)
1809 return (uq_owner->uq_pi_blocked);
1813 * Floyd's Cycle-Finding Algorithm.
1816 umtx_pi_check_loop(struct umtx_pi *pi)
1818 struct umtx_pi *pi1; /* fast iterator */
1820 mtx_assert(&umtx_lock, MA_OWNED);
1825 pi = umtx_pi_next(pi);
1828 pi1 = umtx_pi_next(pi1);
1831 pi1 = umtx_pi_next(pi1);
1841 * Propagate priority when a thread is blocked on POSIX
1845 umtx_propagate_priority(struct thread *td)
1851 mtx_assert(&umtx_lock, MA_OWNED);
1854 pi = uq->uq_pi_blocked;
1857 if (umtx_pi_check_loop(pi))
1862 if (td == NULL || td == curthread)
1865 MPASS(td->td_proc != NULL);
1866 MPASS(td->td_proc->p_magic == P_MAGIC);
1869 if (td->td_lend_user_pri > pri)
1870 sched_lend_user_prio(td, pri);
1878 * Pick up the lock that td is blocked on.
1881 pi = uq->uq_pi_blocked;
1884 /* Resort td on the list if needed. */
1885 umtx_pi_adjust_thread(pi, td);
1890 * Unpropagate priority for a PI mutex when a thread blocked on
1891 * it is interrupted by signal or resumed by others.
1894 umtx_repropagate_priority(struct umtx_pi *pi)
1896 struct umtx_q *uq, *uq_owner;
1897 struct umtx_pi *pi2;
1900 mtx_assert(&umtx_lock, MA_OWNED);
1902 if (umtx_pi_check_loop(pi))
1904 while (pi != NULL && pi->pi_owner != NULL) {
1906 uq_owner = pi->pi_owner->td_umtxq;
1908 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1909 uq = TAILQ_FIRST(&pi2->pi_blocked);
1911 if (pri > UPRI(uq->uq_thread))
1912 pri = UPRI(uq->uq_thread);
1916 if (pri > uq_owner->uq_inherited_pri)
1917 pri = uq_owner->uq_inherited_pri;
1918 thread_lock(pi->pi_owner);
1919 sched_lend_user_prio(pi->pi_owner, pri);
1920 thread_unlock(pi->pi_owner);
1921 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1922 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1927 * Insert a PI mutex into owned list.
1930 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1932 struct umtx_q *uq_owner;
1934 uq_owner = owner->td_umtxq;
1935 mtx_assert(&umtx_lock, MA_OWNED);
1936 MPASS(pi->pi_owner == NULL);
1937 pi->pi_owner = owner;
1938 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1942 * Disown a PI mutex, and remove it from the owned list.
1945 umtx_pi_disown(struct umtx_pi *pi)
1948 mtx_assert(&umtx_lock, MA_OWNED);
1949 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1950 pi->pi_owner = NULL;
1954 * Claim ownership of a PI mutex.
1957 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1962 mtx_lock(&umtx_lock);
1963 if (pi->pi_owner == owner) {
1964 mtx_unlock(&umtx_lock);
1968 if (pi->pi_owner != NULL) {
1970 * userland may have already messed the mutex, sigh.
1972 mtx_unlock(&umtx_lock);
1975 umtx_pi_setowner(pi, owner);
1976 uq = TAILQ_FIRST(&pi->pi_blocked);
1978 pri = UPRI(uq->uq_thread);
1980 if (pri < UPRI(owner))
1981 sched_lend_user_prio(owner, pri);
1982 thread_unlock(owner);
1984 mtx_unlock(&umtx_lock);
1989 * Adjust a thread's order position in its blocked PI mutex,
1990 * this may result new priority propagating process.
1993 umtx_pi_adjust(struct thread *td, u_char oldpri)
1999 mtx_lock(&umtx_lock);
2001 * Pick up the lock that td is blocked on.
2003 pi = uq->uq_pi_blocked;
2005 umtx_pi_adjust_thread(pi, td);
2006 umtx_repropagate_priority(pi);
2008 mtx_unlock(&umtx_lock);
2012 * Sleep on a PI mutex.
2015 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
2016 const char *wmesg, struct umtx_abs_timeout *timo, bool shared)
2018 struct thread *td, *td1;
2022 struct umtxq_chain *uc;
2024 uc = umtxq_getchain(&pi->pi_key);
2028 KASSERT(td == curthread, ("inconsistent uq_thread"));
2029 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
2030 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
2032 mtx_lock(&umtx_lock);
2033 if (pi->pi_owner == NULL) {
2034 mtx_unlock(&umtx_lock);
2035 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
2036 mtx_lock(&umtx_lock);
2038 if (pi->pi_owner == NULL)
2039 umtx_pi_setowner(pi, td1);
2040 PROC_UNLOCK(td1->td_proc);
2044 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
2045 pri = UPRI(uq1->uq_thread);
2051 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
2053 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
2055 uq->uq_pi_blocked = pi;
2057 td->td_flags |= TDF_UPIBLOCKED;
2059 umtx_propagate_priority(td);
2060 mtx_unlock(&umtx_lock);
2061 umtxq_unbusy(&uq->uq_key);
2063 error = umtxq_sleep(uq, wmesg, timo);
2066 mtx_lock(&umtx_lock);
2067 uq->uq_pi_blocked = NULL;
2069 td->td_flags &= ~TDF_UPIBLOCKED;
2071 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
2072 umtx_repropagate_priority(pi);
2073 mtx_unlock(&umtx_lock);
2074 umtxq_unlock(&uq->uq_key);
2080 * Add reference count for a PI mutex.
2083 umtx_pi_ref(struct umtx_pi *pi)
2086 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
2091 * Decrease reference count for a PI mutex, if the counter
2092 * is decreased to zero, its memory space is freed.
2095 umtx_pi_unref(struct umtx_pi *pi)
2097 struct umtxq_chain *uc;
2099 uc = umtxq_getchain(&pi->pi_key);
2100 UMTXQ_LOCKED_ASSERT(uc);
2101 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
2102 if (--pi->pi_refcount == 0) {
2103 mtx_lock(&umtx_lock);
2104 if (pi->pi_owner != NULL)
2106 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
2107 ("blocked queue not empty"));
2108 mtx_unlock(&umtx_lock);
2109 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
2115 * Find a PI mutex in hash table.
2118 umtx_pi_lookup(struct umtx_key *key)
2120 struct umtxq_chain *uc;
2123 uc = umtxq_getchain(key);
2124 UMTXQ_LOCKED_ASSERT(uc);
2126 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
2127 if (umtx_key_match(&pi->pi_key, key)) {
2135 * Insert a PI mutex into hash table.
2138 umtx_pi_insert(struct umtx_pi *pi)
2140 struct umtxq_chain *uc;
2142 uc = umtxq_getchain(&pi->pi_key);
2143 UMTXQ_LOCKED_ASSERT(uc);
2144 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
2148 * Drop a PI mutex and wakeup a top waiter.
2151 umtx_pi_drop(struct thread *td, struct umtx_key *key, bool rb, int *count)
2153 struct umtx_q *uq_first, *uq_first2, *uq_me;
2154 struct umtx_pi *pi, *pi2;
2157 UMTXQ_ASSERT_LOCKED_BUSY(key);
2158 *count = umtxq_count_pi(key, &uq_first);
2159 if (uq_first != NULL) {
2160 mtx_lock(&umtx_lock);
2161 pi = uq_first->uq_pi_blocked;
2162 KASSERT(pi != NULL, ("pi == NULL?"));
2163 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2164 mtx_unlock(&umtx_lock);
2165 /* userland messed the mutex */
2168 uq_me = td->td_umtxq;
2169 if (pi->pi_owner == td)
2171 /* get highest priority thread which is still sleeping. */
2172 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2173 while (uq_first != NULL &&
2174 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2175 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2178 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2179 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2180 if (uq_first2 != NULL) {
2181 if (pri > UPRI(uq_first2->uq_thread))
2182 pri = UPRI(uq_first2->uq_thread);
2186 sched_lend_user_prio(td, pri);
2188 mtx_unlock(&umtx_lock);
2190 umtxq_signal_thread(uq_first);
2192 pi = umtx_pi_lookup(key);
2194 * A umtx_pi can exist if a signal or timeout removed the
2195 * last waiter from the umtxq, but there is still
2196 * a thread in do_lock_pi() holding the umtx_pi.
2200 * The umtx_pi can be unowned, such as when a thread
2201 * has just entered do_lock_pi(), allocated the
2202 * umtx_pi, and unlocked the umtxq.
2203 * If the current thread owns it, it must disown it.
2205 mtx_lock(&umtx_lock);
2206 if (pi->pi_owner == td)
2208 mtx_unlock(&umtx_lock);
2218 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
2219 struct _umtx_time *timeout, int try)
2221 struct umtx_abs_timeout timo;
2223 struct umtx_pi *pi, *new_pi;
2224 uint32_t id, old_owner, owner, old;
2230 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2231 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2235 if (timeout != NULL)
2236 umtx_abs_timeout_init2(&timo, timeout);
2238 umtxq_lock(&uq->uq_key);
2239 pi = umtx_pi_lookup(&uq->uq_key);
2241 new_pi = umtx_pi_alloc(M_NOWAIT);
2242 if (new_pi == NULL) {
2243 umtxq_unlock(&uq->uq_key);
2244 new_pi = umtx_pi_alloc(M_WAITOK);
2245 umtxq_lock(&uq->uq_key);
2246 pi = umtx_pi_lookup(&uq->uq_key);
2248 umtx_pi_free(new_pi);
2252 if (new_pi != NULL) {
2253 new_pi->pi_key = uq->uq_key;
2254 umtx_pi_insert(new_pi);
2259 umtxq_unlock(&uq->uq_key);
2262 * Care must be exercised when dealing with umtx structure. It
2263 * can fault on any access.
2267 * Try the uncontested case. This should be done in userland.
2269 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
2270 /* The address was invalid. */
2275 /* The acquire succeeded. */
2277 MPASS(owner == UMUTEX_UNOWNED);
2282 if (owner == UMUTEX_RB_NOTRECOV) {
2283 error = ENOTRECOVERABLE;
2288 * Nobody owns it, but the acquire failed. This can happen
2289 * with ll/sc atomics.
2291 if (owner == UMUTEX_UNOWNED) {
2292 error = thread_check_susp(td, true);
2299 * Avoid overwriting a possible error from sleep due
2300 * to the pending signal with suspension check result.
2303 error = thread_check_susp(td, true);
2308 /* If no one owns it but it is contested try to acquire it. */
2309 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
2311 rv = casueword32(&m->m_owner, owner, &owner,
2312 id | UMUTEX_CONTESTED);
2313 /* The address was invalid. */
2320 error = thread_check_susp(td, true);
2326 * If this failed the lock could
2333 MPASS(owner == old_owner);
2334 umtxq_lock(&uq->uq_key);
2335 umtxq_busy(&uq->uq_key);
2336 error = umtx_pi_claim(pi, td);
2337 umtxq_unbusy(&uq->uq_key);
2338 umtxq_unlock(&uq->uq_key);
2341 * Since we're going to return an
2342 * error, restore the m_owner to its
2343 * previous, unowned state to avoid
2344 * compounding the problem.
2346 (void)casuword32(&m->m_owner,
2347 id | UMUTEX_CONTESTED, old_owner);
2349 if (error == 0 && old_owner == UMUTEX_RB_OWNERDEAD)
2354 if ((owner & ~UMUTEX_CONTESTED) == id) {
2365 * If we caught a signal, we have retried and now
2371 umtxq_lock(&uq->uq_key);
2372 umtxq_busy(&uq->uq_key);
2373 umtxq_unlock(&uq->uq_key);
2376 * Set the contested bit so that a release in user space
2377 * knows to use the system call for unlock. If this fails
2378 * either some one else has acquired the lock or it has been
2381 rv = casueword32(&m->m_owner, owner, &old, owner |
2384 /* The address was invalid. */
2386 umtxq_unbusy_unlocked(&uq->uq_key);
2391 umtxq_unbusy_unlocked(&uq->uq_key);
2392 error = thread_check_susp(td, true);
2397 * The lock changed and we need to retry or we
2398 * lost a race to the thread unlocking the
2399 * umtx. Note that the UMUTEX_RB_OWNERDEAD
2400 * value for owner is impossible there.
2405 umtxq_lock(&uq->uq_key);
2407 /* We set the contested bit, sleep. */
2408 MPASS(old == owner);
2409 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
2410 "umtxpi", timeout == NULL ? NULL : &timo,
2411 (flags & USYNC_PROCESS_SHARED) != 0);
2415 error = thread_check_susp(td, false);
2420 umtxq_lock(&uq->uq_key);
2422 umtxq_unlock(&uq->uq_key);
2424 umtx_key_release(&uq->uq_key);
2429 * Unlock a PI mutex.
2432 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2434 struct umtx_key key;
2435 uint32_t id, new_owner, old, owner;
2442 * Make sure we own this mtx.
2444 error = fueword32(&m->m_owner, &owner);
2448 if ((owner & ~UMUTEX_CONTESTED) != id)
2451 new_owner = umtx_unlock_val(flags, rb);
2453 /* This should be done in userland */
2454 if ((owner & UMUTEX_CONTESTED) == 0) {
2455 error = casueword32(&m->m_owner, owner, &old, new_owner);
2459 error = thread_check_susp(td, true);
2469 /* We should only ever be in here for contested locks */
2470 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2471 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2477 error = umtx_pi_drop(td, &key, rb, &count);
2481 umtx_key_release(&key);
2482 /* userland messed the mutex */
2488 * When unlocking the umtx, it must be marked as unowned if
2489 * there is zero or one thread only waiting for it.
2490 * Otherwise, it must be marked as contested.
2494 new_owner |= UMUTEX_CONTESTED;
2496 error = casueword32(&m->m_owner, owner, &old, new_owner);
2498 error = thread_check_susp(td, false);
2502 umtxq_unbusy_unlocked(&key);
2503 umtx_key_release(&key);
2506 if (error == 0 && old != owner)
2515 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2516 struct _umtx_time *timeout, int try)
2518 struct umtx_abs_timeout timo;
2519 struct umtx_q *uq, *uq2;
2523 int error, pri, old_inherited_pri, su, rv;
2527 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2528 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2532 if (timeout != NULL)
2533 umtx_abs_timeout_init2(&timo, timeout);
2535 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2537 old_inherited_pri = uq->uq_inherited_pri;
2538 umtxq_lock(&uq->uq_key);
2539 umtxq_busy(&uq->uq_key);
2540 umtxq_unlock(&uq->uq_key);
2542 rv = fueword32(&m->m_ceilings[0], &ceiling);
2547 ceiling = RTP_PRIO_MAX - ceiling;
2548 if (ceiling > RTP_PRIO_MAX) {
2553 mtx_lock(&umtx_lock);
2554 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2555 mtx_unlock(&umtx_lock);
2559 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2560 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2562 if (uq->uq_inherited_pri < UPRI(td))
2563 sched_lend_user_prio(td, uq->uq_inherited_pri);
2566 mtx_unlock(&umtx_lock);
2568 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2569 id | UMUTEX_CONTESTED);
2570 /* The address was invalid. */
2576 MPASS(owner == UMUTEX_CONTESTED);
2581 if (owner == UMUTEX_RB_OWNERDEAD) {
2582 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2583 &owner, id | UMUTEX_CONTESTED);
2589 MPASS(owner == UMUTEX_RB_OWNERDEAD);
2590 error = EOWNERDEAD; /* success */
2595 * rv == 1, only check for suspension if we
2596 * did not already catched a signal. If we
2597 * get an error from the check, the same
2598 * condition is checked by the umtxq_sleep()
2599 * call below, so we should obliterate the
2600 * error to not skip the last loop iteration.
2603 error = thread_check_susp(td, false);
2612 } else if (owner == UMUTEX_RB_NOTRECOV) {
2613 error = ENOTRECOVERABLE;
2620 * If we caught a signal, we have retried and now
2626 umtxq_lock(&uq->uq_key);
2628 umtxq_unbusy(&uq->uq_key);
2629 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2632 umtxq_unlock(&uq->uq_key);
2634 mtx_lock(&umtx_lock);
2635 uq->uq_inherited_pri = old_inherited_pri;
2637 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2638 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2640 if (pri > UPRI(uq2->uq_thread))
2641 pri = UPRI(uq2->uq_thread);
2644 if (pri > uq->uq_inherited_pri)
2645 pri = uq->uq_inherited_pri;
2647 sched_lend_user_prio(td, pri);
2649 mtx_unlock(&umtx_lock);
2652 if (error != 0 && error != EOWNERDEAD) {
2653 mtx_lock(&umtx_lock);
2654 uq->uq_inherited_pri = old_inherited_pri;
2656 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2657 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2659 if (pri > UPRI(uq2->uq_thread))
2660 pri = UPRI(uq2->uq_thread);
2663 if (pri > uq->uq_inherited_pri)
2664 pri = uq->uq_inherited_pri;
2666 sched_lend_user_prio(td, pri);
2668 mtx_unlock(&umtx_lock);
2672 umtxq_unbusy_unlocked(&uq->uq_key);
2673 umtx_key_release(&uq->uq_key);
2678 * Unlock a PP mutex.
2681 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2683 struct umtx_key key;
2684 struct umtx_q *uq, *uq2;
2686 uint32_t id, owner, rceiling;
2687 int error, pri, new_inherited_pri, su;
2691 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2694 * Make sure we own this mtx.
2696 error = fueword32(&m->m_owner, &owner);
2700 if ((owner & ~UMUTEX_CONTESTED) != id)
2703 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2708 new_inherited_pri = PRI_MAX;
2710 rceiling = RTP_PRIO_MAX - rceiling;
2711 if (rceiling > RTP_PRIO_MAX)
2713 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2716 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2717 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2724 * For priority protected mutex, always set unlocked state
2725 * to UMUTEX_CONTESTED, so that userland always enters kernel
2726 * to lock the mutex, it is necessary because thread priority
2727 * has to be adjusted for such mutex.
2729 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2734 umtxq_signal(&key, 1);
2741 mtx_lock(&umtx_lock);
2743 uq->uq_inherited_pri = new_inherited_pri;
2745 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2746 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2748 if (pri > UPRI(uq2->uq_thread))
2749 pri = UPRI(uq2->uq_thread);
2752 if (pri > uq->uq_inherited_pri)
2753 pri = uq->uq_inherited_pri;
2755 sched_lend_user_prio(td, pri);
2757 mtx_unlock(&umtx_lock);
2759 umtx_key_release(&key);
2764 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2765 uint32_t *old_ceiling)
2768 uint32_t flags, id, owner, save_ceiling;
2771 error = fueword32(&m->m_flags, &flags);
2774 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2776 if (ceiling > RTP_PRIO_MAX)
2780 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2781 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2785 umtxq_lock(&uq->uq_key);
2786 umtxq_busy(&uq->uq_key);
2787 umtxq_unlock(&uq->uq_key);
2789 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2795 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2796 id | UMUTEX_CONTESTED);
2803 MPASS(owner == UMUTEX_CONTESTED);
2804 rv = suword32(&m->m_ceilings[0], ceiling);
2805 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2806 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2810 if ((owner & ~UMUTEX_CONTESTED) == id) {
2811 rv = suword32(&m->m_ceilings[0], ceiling);
2812 error = rv == 0 ? 0 : EFAULT;
2816 if (owner == UMUTEX_RB_OWNERDEAD) {
2819 } else if (owner == UMUTEX_RB_NOTRECOV) {
2820 error = ENOTRECOVERABLE;
2825 * If we caught a signal, we have retried and now
2832 * We set the contested bit, sleep. Otherwise the lock changed
2833 * and we need to retry or we lost a race to the thread
2834 * unlocking the umtx.
2836 umtxq_lock(&uq->uq_key);
2838 umtxq_unbusy(&uq->uq_key);
2839 error = umtxq_sleep(uq, "umtxpp", NULL);
2841 umtxq_unlock(&uq->uq_key);
2843 umtxq_lock(&uq->uq_key);
2845 umtxq_signal(&uq->uq_key, INT_MAX);
2846 umtxq_unbusy(&uq->uq_key);
2847 umtxq_unlock(&uq->uq_key);
2848 umtx_key_release(&uq->uq_key);
2849 if (error == 0 && old_ceiling != NULL) {
2850 rv = suword32(old_ceiling, save_ceiling);
2851 error = rv == 0 ? 0 : EFAULT;
2857 * Lock a userland POSIX mutex.
2860 do_lock_umutex(struct thread *td, struct umutex *m,
2861 struct _umtx_time *timeout, int mode)
2866 error = fueword32(&m->m_flags, &flags);
2870 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2872 error = do_lock_normal(td, m, flags, timeout, mode);
2874 case UMUTEX_PRIO_INHERIT:
2875 error = do_lock_pi(td, m, flags, timeout, mode);
2877 case UMUTEX_PRIO_PROTECT:
2878 error = do_lock_pp(td, m, flags, timeout, mode);
2883 if (timeout == NULL) {
2884 if (error == EINTR && mode != _UMUTEX_WAIT)
2887 /* Timed-locking is not restarted. */
2888 if (error == ERESTART)
2895 * Unlock a userland POSIX mutex.
2898 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2903 error = fueword32(&m->m_flags, &flags);
2907 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2909 return (do_unlock_normal(td, m, flags, rb));
2910 case UMUTEX_PRIO_INHERIT:
2911 return (do_unlock_pi(td, m, flags, rb));
2912 case UMUTEX_PRIO_PROTECT:
2913 return (do_unlock_pp(td, m, flags, rb));
2920 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2921 struct timespec *timeout, u_long wflags)
2923 struct umtx_abs_timeout timo;
2925 uint32_t flags, clockid, hasw;
2929 error = fueword32(&cv->c_flags, &flags);
2932 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2936 if ((wflags & CVWAIT_CLOCKID) != 0) {
2937 error = fueword32(&cv->c_clockid, &clockid);
2939 umtx_key_release(&uq->uq_key);
2942 if (clockid < CLOCK_REALTIME ||
2943 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2944 /* hmm, only HW clock id will work. */
2945 umtx_key_release(&uq->uq_key);
2949 clockid = CLOCK_REALTIME;
2952 umtxq_lock(&uq->uq_key);
2953 umtxq_busy(&uq->uq_key);
2955 umtxq_unlock(&uq->uq_key);
2958 * Set c_has_waiters to 1 before releasing user mutex, also
2959 * don't modify cache line when unnecessary.
2961 error = fueword32(&cv->c_has_waiters, &hasw);
2962 if (error == 0 && hasw == 0)
2963 error = suword32(&cv->c_has_waiters, 1);
2965 umtxq_lock(&uq->uq_key);
2967 umtxq_unbusy(&uq->uq_key);
2972 umtxq_unbusy_unlocked(&uq->uq_key);
2974 error = do_unlock_umutex(td, m, false);
2976 if (timeout != NULL)
2977 umtx_abs_timeout_init(&timo, clockid,
2978 (wflags & CVWAIT_ABSTIME) != 0, timeout);
2980 umtxq_lock(&uq->uq_key);
2982 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2986 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2990 * This must be timeout,interrupted by signal or
2991 * surprious wakeup, clear c_has_waiter flag when
2994 umtxq_busy(&uq->uq_key);
2995 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2996 int oldlen = uq->uq_cur_queue->length;
2999 umtxq_unlock(&uq->uq_key);
3000 if (suword32(&cv->c_has_waiters, 0) != 0 &&
3003 umtxq_lock(&uq->uq_key);
3006 umtxq_unbusy(&uq->uq_key);
3007 if (error == ERESTART)
3011 umtxq_unlock(&uq->uq_key);
3012 umtx_key_release(&uq->uq_key);
3017 * Signal a userland condition variable.
3020 do_cv_signal(struct thread *td, struct ucond *cv)
3022 struct umtx_key key;
3023 int error, cnt, nwake;
3026 error = fueword32(&cv->c_flags, &flags);
3029 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
3033 cnt = umtxq_count(&key);
3034 nwake = umtxq_signal(&key, 1);
3037 error = suword32(&cv->c_has_waiters, 0);
3044 umtx_key_release(&key);
3049 do_cv_broadcast(struct thread *td, struct ucond *cv)
3051 struct umtx_key key;
3055 error = fueword32(&cv->c_flags, &flags);
3058 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
3063 umtxq_signal(&key, INT_MAX);
3066 error = suword32(&cv->c_has_waiters, 0);
3070 umtxq_unbusy_unlocked(&key);
3072 umtx_key_release(&key);
3077 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
3078 struct _umtx_time *timeout)
3080 struct umtx_abs_timeout timo;
3082 uint32_t flags, wrflags;
3083 int32_t state, oldstate;
3084 int32_t blocked_readers;
3085 int error, error1, rv;
3088 error = fueword32(&rwlock->rw_flags, &flags);
3091 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3095 if (timeout != NULL)
3096 umtx_abs_timeout_init2(&timo, timeout);
3098 wrflags = URWLOCK_WRITE_OWNER;
3099 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
3100 wrflags |= URWLOCK_WRITE_WAITERS;
3103 rv = fueword32(&rwlock->rw_state, &state);
3105 umtx_key_release(&uq->uq_key);
3109 /* try to lock it */
3110 while (!(state & wrflags)) {
3111 if (__predict_false(URWLOCK_READER_COUNT(state) ==
3112 URWLOCK_MAX_READERS)) {
3113 umtx_key_release(&uq->uq_key);
3116 rv = casueword32(&rwlock->rw_state, state,
3117 &oldstate, state + 1);
3119 umtx_key_release(&uq->uq_key);
3123 MPASS(oldstate == state);
3124 umtx_key_release(&uq->uq_key);
3127 error = thread_check_susp(td, true);
3136 /* grab monitor lock */
3137 umtxq_lock(&uq->uq_key);
3138 umtxq_busy(&uq->uq_key);
3139 umtxq_unlock(&uq->uq_key);
3142 * re-read the state, in case it changed between the try-lock above
3143 * and the check below
3145 rv = fueword32(&rwlock->rw_state, &state);
3149 /* set read contention bit */
3150 while (error == 0 && (state & wrflags) &&
3151 !(state & URWLOCK_READ_WAITERS)) {
3152 rv = casueword32(&rwlock->rw_state, state,
3153 &oldstate, state | URWLOCK_READ_WAITERS);
3159 MPASS(oldstate == state);
3163 error = thread_check_susp(td, false);
3168 umtxq_unbusy_unlocked(&uq->uq_key);
3172 /* state is changed while setting flags, restart */
3173 if (!(state & wrflags)) {
3174 umtxq_unbusy_unlocked(&uq->uq_key);
3175 error = thread_check_susp(td, true);
3183 * Contention bit is set, before sleeping, increase
3184 * read waiter count.
3186 rv = fueword32(&rwlock->rw_blocked_readers,
3189 rv = suword32(&rwlock->rw_blocked_readers,
3190 blocked_readers + 1);
3192 umtxq_unbusy_unlocked(&uq->uq_key);
3197 while (state & wrflags) {
3198 umtxq_lock(&uq->uq_key);
3200 umtxq_unbusy(&uq->uq_key);
3202 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
3205 umtxq_busy(&uq->uq_key);
3207 umtxq_unlock(&uq->uq_key);
3210 rv = fueword32(&rwlock->rw_state, &state);
3217 /* decrease read waiter count, and may clear read contention bit */
3218 rv = fueword32(&rwlock->rw_blocked_readers,
3221 rv = suword32(&rwlock->rw_blocked_readers,
3222 blocked_readers - 1);
3224 umtxq_unbusy_unlocked(&uq->uq_key);
3228 if (blocked_readers == 1) {
3229 rv = fueword32(&rwlock->rw_state, &state);
3231 umtxq_unbusy_unlocked(&uq->uq_key);
3236 rv = casueword32(&rwlock->rw_state, state,
3237 &oldstate, state & ~URWLOCK_READ_WAITERS);
3243 MPASS(oldstate == state);
3247 error1 = thread_check_susp(td, false);
3256 umtxq_unbusy_unlocked(&uq->uq_key);
3260 umtx_key_release(&uq->uq_key);
3261 if (error == ERESTART)
3267 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
3269 struct umtx_abs_timeout timo;
3272 int32_t state, oldstate;
3273 int32_t blocked_writers;
3274 int32_t blocked_readers;
3275 int error, error1, rv;
3278 error = fueword32(&rwlock->rw_flags, &flags);
3281 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3285 if (timeout != NULL)
3286 umtx_abs_timeout_init2(&timo, timeout);
3288 blocked_readers = 0;
3290 rv = fueword32(&rwlock->rw_state, &state);
3292 umtx_key_release(&uq->uq_key);
3295 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
3296 URWLOCK_READER_COUNT(state) == 0) {
3297 rv = casueword32(&rwlock->rw_state, state,
3298 &oldstate, state | URWLOCK_WRITE_OWNER);
3300 umtx_key_release(&uq->uq_key);
3304 MPASS(oldstate == state);
3305 umtx_key_release(&uq->uq_key);
3309 error = thread_check_susp(td, true);
3315 if ((state & (URWLOCK_WRITE_OWNER |
3316 URWLOCK_WRITE_WAITERS)) == 0 &&
3317 blocked_readers != 0) {
3318 umtxq_lock(&uq->uq_key);
3319 umtxq_busy(&uq->uq_key);
3320 umtxq_signal_queue(&uq->uq_key, INT_MAX,
3322 umtxq_unbusy(&uq->uq_key);
3323 umtxq_unlock(&uq->uq_key);
3329 /* grab monitor lock */
3330 umtxq_lock(&uq->uq_key);
3331 umtxq_busy(&uq->uq_key);
3332 umtxq_unlock(&uq->uq_key);
3335 * Re-read the state, in case it changed between the
3336 * try-lock above and the check below.
3338 rv = fueword32(&rwlock->rw_state, &state);
3342 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
3343 URWLOCK_READER_COUNT(state) != 0) &&
3344 (state & URWLOCK_WRITE_WAITERS) == 0) {
3345 rv = casueword32(&rwlock->rw_state, state,
3346 &oldstate, state | URWLOCK_WRITE_WAITERS);
3352 MPASS(oldstate == state);
3356 error = thread_check_susp(td, false);
3361 umtxq_unbusy_unlocked(&uq->uq_key);
3365 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
3366 URWLOCK_READER_COUNT(state) == 0) {
3367 umtxq_unbusy_unlocked(&uq->uq_key);
3368 error = thread_check_susp(td, false);
3374 rv = fueword32(&rwlock->rw_blocked_writers,
3377 rv = suword32(&rwlock->rw_blocked_writers,
3378 blocked_writers + 1);
3380 umtxq_unbusy_unlocked(&uq->uq_key);
3385 while ((state & URWLOCK_WRITE_OWNER) ||
3386 URWLOCK_READER_COUNT(state) != 0) {
3387 umtxq_lock(&uq->uq_key);
3388 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3389 umtxq_unbusy(&uq->uq_key);
3391 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
3394 umtxq_busy(&uq->uq_key);
3395 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3396 umtxq_unlock(&uq->uq_key);
3399 rv = fueword32(&rwlock->rw_state, &state);
3406 rv = fueword32(&rwlock->rw_blocked_writers,
3409 rv = suword32(&rwlock->rw_blocked_writers,
3410 blocked_writers - 1);
3412 umtxq_unbusy_unlocked(&uq->uq_key);
3416 if (blocked_writers == 1) {
3417 rv = fueword32(&rwlock->rw_state, &state);
3419 umtxq_unbusy_unlocked(&uq->uq_key);
3424 rv = casueword32(&rwlock->rw_state, state,
3425 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3431 MPASS(oldstate == state);
3435 error1 = thread_check_susp(td, false);
3437 * We are leaving the URWLOCK_WRITE_WAITERS
3438 * behind, but this should not harm the
3447 rv = fueword32(&rwlock->rw_blocked_readers,
3450 umtxq_unbusy_unlocked(&uq->uq_key);
3455 blocked_readers = 0;
3457 umtxq_unbusy_unlocked(&uq->uq_key);
3460 umtx_key_release(&uq->uq_key);
3461 if (error == ERESTART)
3467 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3471 int32_t state, oldstate;
3472 int error, rv, q, count;
3475 error = fueword32(&rwlock->rw_flags, &flags);
3478 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3482 error = fueword32(&rwlock->rw_state, &state);
3487 if (state & URWLOCK_WRITE_OWNER) {
3489 rv = casueword32(&rwlock->rw_state, state,
3490 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3497 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3501 error = thread_check_susp(td, true);
3507 } else if (URWLOCK_READER_COUNT(state) != 0) {
3509 rv = casueword32(&rwlock->rw_state, state,
3510 &oldstate, state - 1);
3517 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3521 error = thread_check_susp(td, true);
3534 if (!(flags & URWLOCK_PREFER_READER)) {
3535 if (state & URWLOCK_WRITE_WAITERS) {
3537 q = UMTX_EXCLUSIVE_QUEUE;
3538 } else if (state & URWLOCK_READ_WAITERS) {
3540 q = UMTX_SHARED_QUEUE;
3543 if (state & URWLOCK_READ_WAITERS) {
3545 q = UMTX_SHARED_QUEUE;
3546 } else if (state & URWLOCK_WRITE_WAITERS) {
3548 q = UMTX_EXCLUSIVE_QUEUE;
3553 umtxq_lock(&uq->uq_key);
3554 umtxq_busy(&uq->uq_key);
3555 umtxq_signal_queue(&uq->uq_key, count, q);
3556 umtxq_unbusy(&uq->uq_key);
3557 umtxq_unlock(&uq->uq_key);
3560 umtx_key_release(&uq->uq_key);
3564 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3566 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3568 struct umtx_abs_timeout timo;
3570 uint32_t flags, count, count1;
3574 error = fueword32(&sem->_flags, &flags);
3577 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3581 if (timeout != NULL)
3582 umtx_abs_timeout_init2(&timo, timeout);
3585 umtxq_lock(&uq->uq_key);
3586 umtxq_busy(&uq->uq_key);
3588 umtxq_unlock(&uq->uq_key);
3589 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3591 rv1 = fueword32(&sem->_count, &count);
3592 if (rv == -1 || rv1 == -1 || count != 0 || (rv == 1 && count1 == 0)) {
3594 rv = suword32(&sem->_has_waiters, 0);
3595 umtxq_lock(&uq->uq_key);
3596 umtxq_unbusy(&uq->uq_key);
3598 umtxq_unlock(&uq->uq_key);
3599 if (rv == -1 || rv1 == -1) {
3607 MPASS(rv == 1 && count1 == 0);
3608 rv = thread_check_susp(td, true);
3614 umtxq_lock(&uq->uq_key);
3615 umtxq_unbusy(&uq->uq_key);
3617 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3619 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3623 /* A relative timeout cannot be restarted. */
3624 if (error == ERESTART && timeout != NULL &&
3625 (timeout->_flags & UMTX_ABSTIME) == 0)
3628 umtxq_unlock(&uq->uq_key);
3630 umtx_key_release(&uq->uq_key);
3635 * Signal a userland semaphore.
3638 do_sem_wake(struct thread *td, struct _usem *sem)
3640 struct umtx_key key;
3644 error = fueword32(&sem->_flags, &flags);
3647 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3651 cnt = umtxq_count(&key);
3654 * Check if count is greater than 0, this means the memory is
3655 * still being referenced by user code, so we can safely
3656 * update _has_waiters flag.
3660 error = suword32(&sem->_has_waiters, 0);
3665 umtxq_signal(&key, 1);
3669 umtx_key_release(&key);
3675 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3677 struct umtx_abs_timeout timo;
3679 uint32_t count, flags;
3683 flags = fuword32(&sem->_flags);
3684 if (timeout != NULL)
3685 umtx_abs_timeout_init2(&timo, timeout);
3688 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3691 umtxq_lock(&uq->uq_key);
3692 umtxq_busy(&uq->uq_key);
3694 umtxq_unlock(&uq->uq_key);
3695 rv = fueword32(&sem->_count, &count);
3697 umtxq_lock(&uq->uq_key);
3698 umtxq_unbusy(&uq->uq_key);
3700 umtxq_unlock(&uq->uq_key);
3701 umtx_key_release(&uq->uq_key);
3705 if (USEM_COUNT(count) != 0) {
3706 umtxq_lock(&uq->uq_key);
3707 umtxq_unbusy(&uq->uq_key);
3709 umtxq_unlock(&uq->uq_key);
3710 umtx_key_release(&uq->uq_key);
3713 if (count == USEM_HAS_WAITERS)
3715 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3718 umtxq_lock(&uq->uq_key);
3719 umtxq_unbusy(&uq->uq_key);
3721 umtxq_unlock(&uq->uq_key);
3722 umtx_key_release(&uq->uq_key);
3725 rv = thread_check_susp(td, true);
3730 umtxq_lock(&uq->uq_key);
3731 umtxq_unbusy(&uq->uq_key);
3733 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3735 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3739 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3740 /* A relative timeout cannot be restarted. */
3741 if (error == ERESTART)
3743 if (error == EINTR) {
3744 kern_clock_gettime(curthread, timo.clockid,
3746 timespecsub(&timo.end, &timo.cur,
3747 &timeout->_timeout);
3751 umtxq_unlock(&uq->uq_key);
3752 umtx_key_release(&uq->uq_key);
3757 * Signal a userland semaphore.
3760 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3762 struct umtx_key key;
3764 uint32_t count, flags;
3766 rv = fueword32(&sem->_flags, &flags);
3769 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3773 cnt = umtxq_count(&key);
3776 * If this was the last sleeping thread, clear the waiters
3781 rv = fueword32(&sem->_count, &count);
3782 while (rv != -1 && count & USEM_HAS_WAITERS) {
3783 rv = casueword32(&sem->_count, count, &count,
3784 count & ~USEM_HAS_WAITERS);
3786 rv = thread_check_susp(td, true);
3799 umtxq_signal(&key, 1);
3803 umtx_key_release(&key);
3807 #ifdef COMPAT_FREEBSD10
3809 freebsd10__umtx_lock(struct thread *td, struct freebsd10__umtx_lock_args *uap)
3811 return (do_lock_umtx(td, uap->umtx, td->td_tid, 0));
3815 freebsd10__umtx_unlock(struct thread *td,
3816 struct freebsd10__umtx_unlock_args *uap)
3818 return (do_unlock_umtx(td, uap->umtx, td->td_tid));
3823 umtx_copyin_timeout(const void *uaddr, struct timespec *tsp)
3827 error = copyin(uaddr, tsp, sizeof(*tsp));
3829 if (!timespecvalid_interval(tsp))
3836 umtx_copyin_umtx_time(const void *uaddr, size_t size, struct _umtx_time *tp)
3840 if (size <= sizeof(tp->_timeout)) {
3841 tp->_clockid = CLOCK_REALTIME;
3843 error = copyin(uaddr, &tp->_timeout, sizeof(tp->_timeout));
3845 error = copyin(uaddr, tp, sizeof(*tp));
3848 if (!timespecvalid_interval(&tp->_timeout))
3854 umtx_copyin_robust_lists(const void *uaddr, size_t size,
3855 struct umtx_robust_lists_params *rb)
3858 if (size > sizeof(*rb))
3860 return (copyin(uaddr, rb, size));
3864 umtx_copyout_timeout(void *uaddr, size_t sz, struct timespec *tsp)
3868 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
3869 * and we're only called if sz >= sizeof(timespec) as supplied in the
3872 KASSERT(sz >= sizeof(*tsp),
3873 ("umtx_copyops specifies incorrect sizes"));
3875 return (copyout(tsp, uaddr, sizeof(*tsp)));
3878 #ifdef COMPAT_FREEBSD10
3880 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap,
3881 const struct umtx_copyops *ops)
3883 struct timespec *ts, timeout;
3886 /* Allow a null timespec (wait forever). */
3887 if (uap->uaddr2 == NULL)
3890 error = ops->copyin_timeout(uap->uaddr2, &timeout);
3895 #ifdef COMPAT_FREEBSD32
3897 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
3899 return (do_lock_umtx(td, uap->obj, uap->val, ts));
3903 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap,
3904 const struct umtx_copyops *ops)
3906 #ifdef COMPAT_FREEBSD32
3908 return (do_unlock_umtx32(td, uap->obj, uap->val));
3910 return (do_unlock_umtx(td, uap->obj, uap->val));
3912 #endif /* COMPAT_FREEBSD10 */
3914 #if !defined(COMPAT_FREEBSD10)
3916 __umtx_op_unimpl(struct thread *td __unused, struct _umtx_op_args *uap __unused,
3917 const struct umtx_copyops *ops __unused)
3919 return (EOPNOTSUPP);
3921 #endif /* COMPAT_FREEBSD10 */
3924 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap,
3925 const struct umtx_copyops *ops)
3927 struct _umtx_time timeout, *tm_p;
3930 if (uap->uaddr2 == NULL)
3933 error = ops->copyin_umtx_time(
3934 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3939 return (do_wait(td, uap->obj, uap->val, tm_p, ops->compat32, 0));
3943 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap,
3944 const struct umtx_copyops *ops)
3946 struct _umtx_time timeout, *tm_p;
3949 if (uap->uaddr2 == NULL)
3952 error = ops->copyin_umtx_time(
3953 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3958 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3962 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap,
3963 const struct umtx_copyops *ops)
3965 struct _umtx_time *tm_p, timeout;
3968 if (uap->uaddr2 == NULL)
3971 error = ops->copyin_umtx_time(
3972 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3977 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3981 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap,
3982 const struct umtx_copyops *ops __unused)
3985 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3988 #define BATCH_SIZE 128
3990 __umtx_op_nwake_private_native(struct thread *td, struct _umtx_op_args *uap)
3992 char *uaddrs[BATCH_SIZE], **upp;
3993 int count, error, i, pos, tocopy;
3995 upp = (char **)uap->obj;
3997 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3999 tocopy = MIN(count, BATCH_SIZE);
4000 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
4003 for (i = 0; i < tocopy; ++i) {
4004 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
4012 __umtx_op_nwake_private_compat32(struct thread *td, struct _umtx_op_args *uap)
4014 uint32_t uaddrs[BATCH_SIZE], *upp;
4015 int count, error, i, pos, tocopy;
4017 upp = (uint32_t *)uap->obj;
4019 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
4021 tocopy = MIN(count, BATCH_SIZE);
4022 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
4025 for (i = 0; i < tocopy; ++i) {
4026 kern_umtx_wake(td, (void *)(uintptr_t)uaddrs[i],
4035 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap,
4036 const struct umtx_copyops *ops)
4040 return (__umtx_op_nwake_private_compat32(td, uap));
4041 return (__umtx_op_nwake_private_native(td, uap));
4045 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap,
4046 const struct umtx_copyops *ops __unused)
4049 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
4053 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap,
4054 const struct umtx_copyops *ops)
4056 struct _umtx_time *tm_p, timeout;
4059 /* Allow a null timespec (wait forever). */
4060 if (uap->uaddr2 == NULL)
4063 error = ops->copyin_umtx_time(
4064 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4069 return (do_lock_umutex(td, uap->obj, tm_p, 0));
4073 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap,
4074 const struct umtx_copyops *ops __unused)
4077 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
4081 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap,
4082 const struct umtx_copyops *ops)
4084 struct _umtx_time *tm_p, timeout;
4087 /* Allow a null timespec (wait forever). */
4088 if (uap->uaddr2 == NULL)
4091 error = ops->copyin_umtx_time(
4092 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4097 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
4101 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap,
4102 const struct umtx_copyops *ops __unused)
4105 return (do_wake_umutex(td, uap->obj));
4109 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap,
4110 const struct umtx_copyops *ops __unused)
4113 return (do_unlock_umutex(td, uap->obj, false));
4117 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap,
4118 const struct umtx_copyops *ops __unused)
4121 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
4125 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap,
4126 const struct umtx_copyops *ops)
4128 struct timespec *ts, timeout;
4131 /* Allow a null timespec (wait forever). */
4132 if (uap->uaddr2 == NULL)
4135 error = ops->copyin_timeout(uap->uaddr2, &timeout);
4140 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
4144 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap,
4145 const struct umtx_copyops *ops __unused)
4148 return (do_cv_signal(td, uap->obj));
4152 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap,
4153 const struct umtx_copyops *ops __unused)
4156 return (do_cv_broadcast(td, uap->obj));
4160 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap,
4161 const struct umtx_copyops *ops)
4163 struct _umtx_time timeout;
4166 /* Allow a null timespec (wait forever). */
4167 if (uap->uaddr2 == NULL) {
4168 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4170 error = ops->copyin_umtx_time(uap->uaddr2,
4171 (size_t)uap->uaddr1, &timeout);
4174 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4180 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap,
4181 const struct umtx_copyops *ops)
4183 struct _umtx_time timeout;
4186 /* Allow a null timespec (wait forever). */
4187 if (uap->uaddr2 == NULL) {
4188 error = do_rw_wrlock(td, uap->obj, 0);
4190 error = ops->copyin_umtx_time(uap->uaddr2,
4191 (size_t)uap->uaddr1, &timeout);
4195 error = do_rw_wrlock(td, uap->obj, &timeout);
4201 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap,
4202 const struct umtx_copyops *ops __unused)
4205 return (do_rw_unlock(td, uap->obj));
4208 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4210 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap,
4211 const struct umtx_copyops *ops)
4213 struct _umtx_time *tm_p, timeout;
4216 /* Allow a null timespec (wait forever). */
4217 if (uap->uaddr2 == NULL)
4220 error = ops->copyin_umtx_time(
4221 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4226 return (do_sem_wait(td, uap->obj, tm_p));
4230 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap,
4231 const struct umtx_copyops *ops __unused)
4234 return (do_sem_wake(td, uap->obj));
4239 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap,
4240 const struct umtx_copyops *ops __unused)
4243 return (do_wake2_umutex(td, uap->obj, uap->val));
4247 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap,
4248 const struct umtx_copyops *ops)
4250 struct _umtx_time *tm_p, timeout;
4254 /* Allow a null timespec (wait forever). */
4255 if (uap->uaddr2 == NULL) {
4259 uasize = (size_t)uap->uaddr1;
4260 error = ops->copyin_umtx_time(uap->uaddr2, uasize, &timeout);
4265 error = do_sem2_wait(td, uap->obj, tm_p);
4266 if (error == EINTR && uap->uaddr2 != NULL &&
4267 (timeout._flags & UMTX_ABSTIME) == 0 &&
4268 uasize >= ops->umtx_time_sz + ops->timespec_sz) {
4269 error = ops->copyout_timeout(
4270 (void *)((uintptr_t)uap->uaddr2 + ops->umtx_time_sz),
4271 uasize - ops->umtx_time_sz, &timeout._timeout);
4281 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap,
4282 const struct umtx_copyops *ops __unused)
4285 return (do_sem2_wake(td, uap->obj));
4288 #define USHM_OBJ_UMTX(o) \
4289 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
4291 #define USHMF_REG_LINKED 0x0001
4292 #define USHMF_OBJ_LINKED 0x0002
4293 struct umtx_shm_reg {
4294 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
4295 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
4296 struct umtx_key ushm_key;
4297 struct ucred *ushm_cred;
4298 struct shmfd *ushm_obj;
4303 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
4304 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
4306 static uma_zone_t umtx_shm_reg_zone;
4307 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
4308 static struct mtx umtx_shm_lock;
4309 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
4310 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
4312 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
4315 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
4317 struct umtx_shm_reg_head d;
4318 struct umtx_shm_reg *reg, *reg1;
4321 mtx_lock(&umtx_shm_lock);
4322 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
4323 mtx_unlock(&umtx_shm_lock);
4324 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
4325 TAILQ_REMOVE(&d, reg, ushm_reg_link);
4326 umtx_shm_free_reg(reg);
4330 static struct task umtx_shm_reg_delfree_task =
4331 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
4333 static struct umtx_shm_reg *
4334 umtx_shm_find_reg_locked(const struct umtx_key *key)
4336 struct umtx_shm_reg *reg;
4337 struct umtx_shm_reg_head *reg_head;
4339 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
4340 mtx_assert(&umtx_shm_lock, MA_OWNED);
4341 reg_head = &umtx_shm_registry[key->hash];
4342 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
4343 KASSERT(reg->ushm_key.shared,
4344 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
4345 if (reg->ushm_key.info.shared.object ==
4346 key->info.shared.object &&
4347 reg->ushm_key.info.shared.offset ==
4348 key->info.shared.offset) {
4349 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
4350 KASSERT(reg->ushm_refcnt > 0,
4351 ("reg %p refcnt 0 onlist", reg));
4352 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
4353 ("reg %p not linked", reg));
4361 static struct umtx_shm_reg *
4362 umtx_shm_find_reg(const struct umtx_key *key)
4364 struct umtx_shm_reg *reg;
4366 mtx_lock(&umtx_shm_lock);
4367 reg = umtx_shm_find_reg_locked(key);
4368 mtx_unlock(&umtx_shm_lock);
4373 umtx_shm_free_reg(struct umtx_shm_reg *reg)
4376 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
4377 crfree(reg->ushm_cred);
4378 shm_drop(reg->ushm_obj);
4379 uma_zfree(umtx_shm_reg_zone, reg);
4383 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
4387 mtx_assert(&umtx_shm_lock, MA_OWNED);
4388 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
4390 res = reg->ushm_refcnt == 0;
4392 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
4393 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
4394 reg, ushm_reg_link);
4395 reg->ushm_flags &= ~USHMF_REG_LINKED;
4397 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
4398 LIST_REMOVE(reg, ushm_obj_link);
4399 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
4406 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
4412 object = reg->ushm_obj->shm_object;
4413 VM_OBJECT_WLOCK(object);
4414 vm_object_set_flag(object, OBJ_UMTXDEAD);
4415 VM_OBJECT_WUNLOCK(object);
4417 mtx_lock(&umtx_shm_lock);
4418 dofree = umtx_shm_unref_reg_locked(reg, force);
4419 mtx_unlock(&umtx_shm_lock);
4421 umtx_shm_free_reg(reg);
4425 umtx_shm_object_init(vm_object_t object)
4428 LIST_INIT(USHM_OBJ_UMTX(object));
4432 umtx_shm_object_terminated(vm_object_t object)
4434 struct umtx_shm_reg *reg, *reg1;
4437 if (LIST_EMPTY(USHM_OBJ_UMTX(object)))
4441 mtx_lock(&umtx_shm_lock);
4442 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
4443 if (umtx_shm_unref_reg_locked(reg, true)) {
4444 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
4449 mtx_unlock(&umtx_shm_lock);
4451 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
4455 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
4456 struct umtx_shm_reg **res)
4458 struct umtx_shm_reg *reg, *reg1;
4462 reg = umtx_shm_find_reg(key);
4467 cred = td->td_ucred;
4468 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
4470 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
4471 reg->ushm_refcnt = 1;
4472 bcopy(key, ®->ushm_key, sizeof(*key));
4473 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR, false);
4474 reg->ushm_cred = crhold(cred);
4475 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
4477 umtx_shm_free_reg(reg);
4480 mtx_lock(&umtx_shm_lock);
4481 reg1 = umtx_shm_find_reg_locked(key);
4483 mtx_unlock(&umtx_shm_lock);
4484 umtx_shm_free_reg(reg);
4489 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
4490 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
4492 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
4493 mtx_unlock(&umtx_shm_lock);
4499 umtx_shm_alive(struct thread *td, void *addr)
4502 vm_map_entry_t entry;
4509 map = &td->td_proc->p_vmspace->vm_map;
4510 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
4511 &object, &pindex, &prot, &wired);
4512 if (res != KERN_SUCCESS)
4517 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
4518 vm_map_lookup_done(map, entry);
4527 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
4528 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
4529 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
4530 for (i = 0; i < nitems(umtx_shm_registry); i++)
4531 TAILQ_INIT(&umtx_shm_registry[i]);
4535 umtx_shm(struct thread *td, void *addr, u_int flags)
4537 struct umtx_key key;
4538 struct umtx_shm_reg *reg;
4542 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
4543 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
4545 if ((flags & UMTX_SHM_ALIVE) != 0)
4546 return (umtx_shm_alive(td, addr));
4547 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
4550 KASSERT(key.shared == 1, ("non-shared key"));
4551 if ((flags & UMTX_SHM_CREAT) != 0) {
4552 error = umtx_shm_create_reg(td, &key, ®);
4554 reg = umtx_shm_find_reg(&key);
4558 umtx_key_release(&key);
4561 KASSERT(reg != NULL, ("no reg"));
4562 if ((flags & UMTX_SHM_DESTROY) != 0) {
4563 umtx_shm_unref_reg(reg, true);
4567 error = mac_posixshm_check_open(td->td_ucred,
4568 reg->ushm_obj, FFLAGS(O_RDWR));
4571 error = shm_access(reg->ushm_obj, td->td_ucred,
4575 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
4577 shm_hold(reg->ushm_obj);
4578 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
4580 td->td_retval[0] = fd;
4584 umtx_shm_unref_reg(reg, false);
4589 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap,
4590 const struct umtx_copyops *ops __unused)
4593 return (umtx_shm(td, uap->uaddr1, uap->val));
4597 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap,
4598 const struct umtx_copyops *ops)
4600 struct umtx_robust_lists_params rb;
4603 if (ops->compat32) {
4604 if ((td->td_pflags2 & TDP2_COMPAT32RB) == 0 &&
4605 (td->td_rb_list != 0 || td->td_rbp_list != 0 ||
4606 td->td_rb_inact != 0))
4608 } else if ((td->td_pflags2 & TDP2_COMPAT32RB) != 0) {
4612 bzero(&rb, sizeof(rb));
4613 error = ops->copyin_robust_lists(uap->uaddr1, uap->val, &rb);
4618 td->td_pflags2 |= TDP2_COMPAT32RB;
4620 td->td_rb_list = rb.robust_list_offset;
4621 td->td_rbp_list = rb.robust_priv_list_offset;
4622 td->td_rb_inact = rb.robust_inact_offset;
4627 __umtx_op_get_min_timeout(struct thread *td, struct _umtx_op_args *uap,
4628 const struct umtx_copyops *ops)
4633 val = sbttons(td->td_proc->p_umtx_min_timeout);
4634 if (ops->compat32) {
4636 error = copyout(&val1, uap->uaddr1, sizeof(val1));
4638 error = copyout(&val, uap->uaddr1, sizeof(val));
4644 __umtx_op_set_min_timeout(struct thread *td, struct _umtx_op_args *uap,
4645 const struct umtx_copyops *ops)
4649 td->td_proc->p_umtx_min_timeout = nstosbt(uap->val);
4653 #if defined(__i386__) || defined(__amd64__)
4655 * Provide the standard 32-bit definitions for x86, since native/compat32 use a
4656 * 32-bit time_t there. Other architectures just need the i386 definitions
4657 * along with their standard compat32.
4659 struct timespecx32 {
4664 struct umtx_timex32 {
4665 struct timespecx32 _timeout;
4671 #define timespeci386 timespec32
4672 #define umtx_timei386 umtx_time32
4674 #else /* !__i386__ && !__amd64__ */
4675 /* 32-bit architectures can emulate i386, so define these almost everywhere. */
4676 struct timespeci386 {
4681 struct umtx_timei386 {
4682 struct timespeci386 _timeout;
4687 #if defined(__LP64__)
4688 #define timespecx32 timespec32
4689 #define umtx_timex32 umtx_time32
4694 umtx_copyin_robust_lists32(const void *uaddr, size_t size,
4695 struct umtx_robust_lists_params *rbp)
4697 struct umtx_robust_lists_params_compat32 rb32;
4700 if (size > sizeof(rb32))
4702 bzero(&rb32, sizeof(rb32));
4703 error = copyin(uaddr, &rb32, size);
4706 CP(rb32, *rbp, robust_list_offset);
4707 CP(rb32, *rbp, robust_priv_list_offset);
4708 CP(rb32, *rbp, robust_inact_offset);
4714 umtx_copyin_timeouti386(const void *uaddr, struct timespec *tsp)
4716 struct timespeci386 ts32;
4719 error = copyin(uaddr, &ts32, sizeof(ts32));
4721 if (!timespecvalid_interval(&ts32))
4724 CP(ts32, *tsp, tv_sec);
4725 CP(ts32, *tsp, tv_nsec);
4732 umtx_copyin_umtx_timei386(const void *uaddr, size_t size, struct _umtx_time *tp)
4734 struct umtx_timei386 t32;
4737 t32._clockid = CLOCK_REALTIME;
4739 if (size <= sizeof(t32._timeout))
4740 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4742 error = copyin(uaddr, &t32, sizeof(t32));
4745 if (!timespecvalid_interval(&t32._timeout))
4747 TS_CP(t32, *tp, _timeout);
4748 CP(t32, *tp, _flags);
4749 CP(t32, *tp, _clockid);
4754 umtx_copyout_timeouti386(void *uaddr, size_t sz, struct timespec *tsp)
4756 struct timespeci386 remain32 = {
4757 .tv_sec = tsp->tv_sec,
4758 .tv_nsec = tsp->tv_nsec,
4762 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4763 * and we're only called if sz >= sizeof(timespec) as supplied in the
4766 KASSERT(sz >= sizeof(remain32),
4767 ("umtx_copyops specifies incorrect sizes"));
4769 return (copyout(&remain32, uaddr, sizeof(remain32)));
4771 #endif /* !__i386__ */
4773 #if defined(__i386__) || defined(__LP64__)
4775 umtx_copyin_timeoutx32(const void *uaddr, struct timespec *tsp)
4777 struct timespecx32 ts32;
4780 error = copyin(uaddr, &ts32, sizeof(ts32));
4782 if (!timespecvalid_interval(&ts32))
4785 CP(ts32, *tsp, tv_sec);
4786 CP(ts32, *tsp, tv_nsec);
4793 umtx_copyin_umtx_timex32(const void *uaddr, size_t size, struct _umtx_time *tp)
4795 struct umtx_timex32 t32;
4798 t32._clockid = CLOCK_REALTIME;
4800 if (size <= sizeof(t32._timeout))
4801 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4803 error = copyin(uaddr, &t32, sizeof(t32));
4806 if (!timespecvalid_interval(&t32._timeout))
4808 TS_CP(t32, *tp, _timeout);
4809 CP(t32, *tp, _flags);
4810 CP(t32, *tp, _clockid);
4815 umtx_copyout_timeoutx32(void *uaddr, size_t sz, struct timespec *tsp)
4817 struct timespecx32 remain32 = {
4818 .tv_sec = tsp->tv_sec,
4819 .tv_nsec = tsp->tv_nsec,
4823 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4824 * and we're only called if sz >= sizeof(timespec) as supplied in the
4827 KASSERT(sz >= sizeof(remain32),
4828 ("umtx_copyops specifies incorrect sizes"));
4830 return (copyout(&remain32, uaddr, sizeof(remain32)));
4832 #endif /* __i386__ || __LP64__ */
4834 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap,
4835 const struct umtx_copyops *umtx_ops);
4837 static const _umtx_op_func op_table[] = {
4838 #ifdef COMPAT_FREEBSD10
4839 [UMTX_OP_LOCK] = __umtx_op_lock_umtx,
4840 [UMTX_OP_UNLOCK] = __umtx_op_unlock_umtx,
4842 [UMTX_OP_LOCK] = __umtx_op_unimpl,
4843 [UMTX_OP_UNLOCK] = __umtx_op_unimpl,
4845 [UMTX_OP_WAIT] = __umtx_op_wait,
4846 [UMTX_OP_WAKE] = __umtx_op_wake,
4847 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4848 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
4849 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4850 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4851 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
4852 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4853 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4854 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
4855 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
4856 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
4857 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4858 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4859 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4860 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4861 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4862 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4863 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4864 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4866 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4867 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4869 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4870 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4871 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4872 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4873 [UMTX_OP_SHM] = __umtx_op_shm,
4874 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4875 [UMTX_OP_GET_MIN_TIMEOUT] = __umtx_op_get_min_timeout,
4876 [UMTX_OP_SET_MIN_TIMEOUT] = __umtx_op_set_min_timeout,
4879 static const struct umtx_copyops umtx_native_ops = {
4880 .copyin_timeout = umtx_copyin_timeout,
4881 .copyin_umtx_time = umtx_copyin_umtx_time,
4882 .copyin_robust_lists = umtx_copyin_robust_lists,
4883 .copyout_timeout = umtx_copyout_timeout,
4884 .timespec_sz = sizeof(struct timespec),
4885 .umtx_time_sz = sizeof(struct _umtx_time),
4889 static const struct umtx_copyops umtx_native_opsi386 = {
4890 .copyin_timeout = umtx_copyin_timeouti386,
4891 .copyin_umtx_time = umtx_copyin_umtx_timei386,
4892 .copyin_robust_lists = umtx_copyin_robust_lists32,
4893 .copyout_timeout = umtx_copyout_timeouti386,
4894 .timespec_sz = sizeof(struct timespeci386),
4895 .umtx_time_sz = sizeof(struct umtx_timei386),
4900 #if defined(__i386__) || defined(__LP64__)
4901 /* i386 can emulate other 32-bit archs, too! */
4902 static const struct umtx_copyops umtx_native_opsx32 = {
4903 .copyin_timeout = umtx_copyin_timeoutx32,
4904 .copyin_umtx_time = umtx_copyin_umtx_timex32,
4905 .copyin_robust_lists = umtx_copyin_robust_lists32,
4906 .copyout_timeout = umtx_copyout_timeoutx32,
4907 .timespec_sz = sizeof(struct timespecx32),
4908 .umtx_time_sz = sizeof(struct umtx_timex32),
4912 #ifdef COMPAT_FREEBSD32
4914 #define umtx_native_ops32 umtx_native_opsi386
4916 #define umtx_native_ops32 umtx_native_opsx32
4918 #endif /* COMPAT_FREEBSD32 */
4919 #endif /* __i386__ || __LP64__ */
4921 #define UMTX_OP__FLAGS (UMTX_OP__32BIT | UMTX_OP__I386)
4924 kern__umtx_op(struct thread *td, void *obj, int op, unsigned long val,
4925 void *uaddr1, void *uaddr2, const struct umtx_copyops *ops)
4927 struct _umtx_op_args uap = {
4929 .op = op & ~UMTX_OP__FLAGS,
4935 if ((uap.op >= nitems(op_table)))
4937 return ((*op_table[uap.op])(td, &uap, ops));
4941 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4943 static const struct umtx_copyops *umtx_ops;
4945 umtx_ops = &umtx_native_ops;
4947 if ((uap->op & (UMTX_OP__32BIT | UMTX_OP__I386)) != 0) {
4948 if ((uap->op & UMTX_OP__I386) != 0)
4949 umtx_ops = &umtx_native_opsi386;
4951 umtx_ops = &umtx_native_opsx32;
4953 #elif !defined(__i386__)
4954 /* We consider UMTX_OP__32BIT a nop on !i386 ILP32. */
4955 if ((uap->op & UMTX_OP__I386) != 0)
4956 umtx_ops = &umtx_native_opsi386;
4958 /* Likewise, UMTX_OP__I386 is a nop on i386. */
4959 if ((uap->op & UMTX_OP__32BIT) != 0)
4960 umtx_ops = &umtx_native_opsx32;
4962 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr1,
4963 uap->uaddr2, umtx_ops));
4966 #ifdef COMPAT_FREEBSD32
4967 #ifdef COMPAT_FREEBSD10
4969 freebsd10_freebsd32__umtx_lock(struct thread *td,
4970 struct freebsd10_freebsd32__umtx_lock_args *uap)
4972 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
4976 freebsd10_freebsd32__umtx_unlock(struct thread *td,
4977 struct freebsd10_freebsd32__umtx_unlock_args *uap)
4979 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
4981 #endif /* COMPAT_FREEBSD10 */
4984 freebsd32__umtx_op(struct thread *td, struct freebsd32__umtx_op_args *uap)
4987 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr1,
4988 uap->uaddr2, &umtx_native_ops32));
4990 #endif /* COMPAT_FREEBSD32 */
4993 umtx_thread_init(struct thread *td)
4996 td->td_umtxq = umtxq_alloc();
4997 td->td_umtxq->uq_thread = td;
5001 umtx_thread_fini(struct thread *td)
5004 umtxq_free(td->td_umtxq);
5008 * It will be called when new thread is created, e.g fork().
5011 umtx_thread_alloc(struct thread *td)
5016 uq->uq_inherited_pri = PRI_MAX;
5018 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
5019 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
5020 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
5021 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
5027 * Clear robust lists for all process' threads, not delaying the
5028 * cleanup to thread exit, since the relevant address space is
5029 * destroyed right now.
5032 umtx_exec(struct proc *p)
5036 KASSERT(p == curproc, ("need curproc"));
5037 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
5038 (p->p_flag & P_STOPPED_SINGLE) != 0,
5039 ("curproc must be single-threaded"));
5041 * There is no need to lock the list as only this thread can be
5044 FOREACH_THREAD_IN_PROC(p, td) {
5045 KASSERT(td == curthread ||
5046 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
5047 ("running thread %p %p", p, td));
5048 umtx_thread_cleanup(td);
5049 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
5052 p->p_umtx_min_timeout = 0;
5059 umtx_thread_exit(struct thread *td)
5062 umtx_thread_cleanup(td);
5066 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res, bool compat32)
5073 error = fueword32((void *)ptr, &res32);
5077 error = fueword((void *)ptr, &res1);
5087 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list,
5090 struct umutex32 m32;
5093 memcpy(&m32, m, sizeof(m32));
5094 *rb_list = m32.m_rb_lnk;
5096 *rb_list = m->m_rb_lnk;
5101 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact,
5107 KASSERT(td->td_proc == curproc, ("need current vmspace"));
5108 error = copyin((void *)rbp, &m, sizeof(m));
5111 if (rb_list != NULL)
5112 umtx_read_rb_list(td, &m, rb_list, compat32);
5113 if ((m.m_flags & UMUTEX_ROBUST) == 0)
5115 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
5116 /* inact is cleared after unlock, allow the inconsistency */
5117 return (inact ? 0 : EINVAL);
5118 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
5122 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
5123 const char *name, bool compat32)
5131 error = umtx_read_uptr(td, rb_list, &rbp, compat32);
5132 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
5133 if (rbp == *rb_inact) {
5138 error = umtx_handle_rb(td, rbp, &rbp, inact, compat32);
5140 if (i == umtx_max_rb && umtx_verbose_rb) {
5141 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
5142 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
5144 if (error != 0 && umtx_verbose_rb) {
5145 uprintf("comm %s pid %d: handling %srb error %d\n",
5146 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
5151 * Clean up umtx data.
5154 umtx_thread_cleanup(struct thread *td)
5162 * Disown pi mutexes.
5166 if (uq->uq_inherited_pri != PRI_MAX ||
5167 !TAILQ_EMPTY(&uq->uq_pi_contested)) {
5168 mtx_lock(&umtx_lock);
5169 uq->uq_inherited_pri = PRI_MAX;
5170 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
5171 pi->pi_owner = NULL;
5172 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
5174 mtx_unlock(&umtx_lock);
5176 sched_lend_user_prio_cond(td, PRI_MAX);
5179 compat32 = (td->td_pflags2 & TDP2_COMPAT32RB) != 0;
5180 td->td_pflags2 &= ~TDP2_COMPAT32RB;
5182 if (td->td_rb_inact == 0 && td->td_rb_list == 0 && td->td_rbp_list == 0)
5186 * Handle terminated robust mutexes. Must be done after
5187 * robust pi disown, otherwise unlock could see unowned
5190 rb_inact = td->td_rb_inact;
5192 (void)umtx_read_uptr(td, rb_inact, &rb_inact, compat32);
5193 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "", compat32);
5194 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ", compat32);
5196 (void)umtx_handle_rb(td, rb_inact, NULL, true, compat32);