2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015, 2016 The FreeBSD Foundation
5 * Copyright (c) 2004, David Xu <davidxu@freebsd.org>
6 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
9 * Portions of this software were developed by Konstantin Belousov
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice unmodified, this list of conditions, and the following
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_umtx_profiling.h"
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
43 #include <sys/filedesc.h>
44 #include <sys/limits.h>
46 #include <sys/malloc.h>
48 #include <sys/mutex.h>
51 #include <sys/resource.h>
52 #include <sys/resourcevar.h>
53 #include <sys/rwlock.h>
55 #include <sys/sched.h>
57 #include <sys/sysctl.h>
58 #include <sys/systm.h>
59 #include <sys/sysproto.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/taskqueue.h>
63 #include <sys/eventhandler.h>
65 #include <sys/umtxvar.h>
67 #include <security/mac/mac_framework.h>
70 #include <vm/vm_param.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_object.h>
75 #include <machine/atomic.h>
76 #include <machine/cpu.h>
78 #include <compat/freebsd32/freebsd32.h>
79 #ifdef COMPAT_FREEBSD32
80 #include <compat/freebsd32/freebsd32_proto.h>
84 #define _UMUTEX_WAIT 2
87 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
88 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
91 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
93 #define UMTXQ_ASSERT_LOCKED_BUSY(key) do { \
94 struct umtxq_chain *uc; \
96 uc = umtxq_getchain(key); \
97 mtx_assert(&uc->uc_lock, MA_OWNED); \
98 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy")); \
101 #define UMTXQ_ASSERT_LOCKED_BUSY(key) do {} while (0)
105 * Don't propagate time-sharing priority, there is a security reason,
106 * a user can simply introduce PI-mutex, let thread A lock the mutex,
107 * and let another thread B block on the mutex, because B is
108 * sleeping, its priority will be boosted, this causes A's priority to
109 * be boosted via priority propagating too and will never be lowered even
110 * if it is using 100%CPU, this is unfair to other processes.
113 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
114 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
115 PRI_MAX_TIMESHARE : (td)->td_user_pri)
117 #define GOLDEN_RATIO_PRIME 2654404609U
119 #define UMTX_CHAINS 512
121 #define UMTX_SHIFTS (__WORD_BIT - 9)
123 #define GET_SHARE(flags) \
124 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
126 #define BUSY_SPINS 200
128 struct umtx_copyops {
129 int (*copyin_timeout)(const void *uaddr, struct timespec *tsp);
130 int (*copyin_umtx_time)(const void *uaddr, size_t size,
131 struct _umtx_time *tp);
132 int (*copyin_robust_lists)(const void *uaddr, size_t size,
133 struct umtx_robust_lists_params *rbp);
134 int (*copyout_timeout)(void *uaddr, size_t size,
135 struct timespec *tsp);
136 const size_t timespec_sz;
137 const size_t umtx_time_sz;
141 _Static_assert(sizeof(struct umutex) == sizeof(struct umutex32), "umutex32");
142 _Static_assert(__offsetof(struct umutex, m_spare[0]) ==
143 __offsetof(struct umutex32, m_spare[0]), "m_spare32");
145 int umtx_shm_vnobj_persistent = 0;
146 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_vnode_persistent, CTLFLAG_RWTUN,
147 &umtx_shm_vnobj_persistent, 0,
148 "False forces destruction of umtx attached to file, on last close");
149 static int umtx_max_rb = 1000;
150 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_max_robust, CTLFLAG_RWTUN,
152 "Maximum number of robust mutexes allowed for each thread");
154 static uma_zone_t umtx_pi_zone;
155 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
156 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
157 static int umtx_pi_allocated;
159 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
161 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
162 &umtx_pi_allocated, 0, "Allocated umtx_pi");
163 static int umtx_verbose_rb = 1;
164 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
168 #ifdef UMTX_PROFILING
169 static long max_length;
170 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
171 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
175 static inline void umtx_abs_timeout_init2(struct umtx_abs_timeout *timo,
176 const struct _umtx_time *umtxtime);
178 static void umtx_shm_init(void);
179 static void umtxq_sysinit(void *);
180 static void umtxq_hash(struct umtx_key *key);
181 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
183 static void umtx_thread_cleanup(struct thread *td);
184 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
186 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
188 static struct mtx umtx_lock;
190 #ifdef UMTX_PROFILING
192 umtx_init_profiling(void)
194 struct sysctl_oid *chain_oid;
198 for (i = 0; i < UMTX_CHAINS; ++i) {
199 snprintf(chain_name, sizeof(chain_name), "%d", i);
200 chain_oid = SYSCTL_ADD_NODE(NULL,
201 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
202 chain_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
204 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
205 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
206 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
207 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
212 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
216 struct umtxq_chain *uc;
217 u_int fract, i, j, tot, whole;
218 u_int sf0, sf1, sf2, sf3, sf4;
219 u_int si0, si1, si2, si3, si4;
220 u_int sw0, sw1, sw2, sw3, sw4;
222 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
223 for (i = 0; i < 2; i++) {
225 for (j = 0; j < UMTX_CHAINS; ++j) {
226 uc = &umtxq_chains[i][j];
227 mtx_lock(&uc->uc_lock);
228 tot += uc->max_length;
229 mtx_unlock(&uc->uc_lock);
232 sbuf_printf(&sb, "%u) Empty ", i);
234 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
235 si0 = si1 = si2 = si3 = si4 = 0;
236 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
237 for (j = 0; j < UMTX_CHAINS; j++) {
238 uc = &umtxq_chains[i][j];
239 mtx_lock(&uc->uc_lock);
240 whole = uc->max_length * 100;
241 mtx_unlock(&uc->uc_lock);
242 fract = (whole % tot) * 100;
243 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
247 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
252 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
257 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
262 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
269 sbuf_printf(&sb, "queue %u:\n", i);
270 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
272 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
274 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
276 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
278 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
284 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
290 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
292 struct umtxq_chain *uc;
297 error = sysctl_handle_int(oidp, &clear, 0, req);
298 if (error != 0 || req->newptr == NULL)
302 for (i = 0; i < 2; ++i) {
303 for (j = 0; j < UMTX_CHAINS; ++j) {
304 uc = &umtxq_chains[i][j];
305 mtx_lock(&uc->uc_lock);
308 mtx_unlock(&uc->uc_lock);
315 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
316 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
317 sysctl_debug_umtx_chains_clear, "I",
318 "Clear umtx chains statistics");
319 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
320 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
321 sysctl_debug_umtx_chains_peaks, "A",
322 "Highest peaks in chains max length");
326 umtxq_sysinit(void *arg __unused)
330 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
331 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
332 for (i = 0; i < 2; ++i) {
333 for (j = 0; j < UMTX_CHAINS; ++j) {
334 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
335 MTX_DEF | MTX_DUPOK);
336 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
337 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
338 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
339 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
340 umtxq_chains[i][j].uc_busy = 0;
341 umtxq_chains[i][j].uc_waiters = 0;
342 #ifdef UMTX_PROFILING
343 umtxq_chains[i][j].length = 0;
344 umtxq_chains[i][j].max_length = 0;
348 #ifdef UMTX_PROFILING
349 umtx_init_profiling();
351 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
360 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
361 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
363 TAILQ_INIT(&uq->uq_spare_queue->head);
364 TAILQ_INIT(&uq->uq_pi_contested);
365 uq->uq_inherited_pri = PRI_MAX;
370 umtxq_free(struct umtx_q *uq)
373 MPASS(uq->uq_spare_queue != NULL);
374 free(uq->uq_spare_queue, M_UMTX);
379 umtxq_hash(struct umtx_key *key)
383 n = (uintptr_t)key->info.both.a + key->info.both.b;
384 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
388 umtxq_getchain(struct umtx_key *key)
391 if (key->type <= TYPE_SEM)
392 return (&umtxq_chains[1][key->hash]);
393 return (&umtxq_chains[0][key->hash]);
397 * Set chain to busy state when following operation
398 * may be blocked (kernel mutex can not be used).
401 umtxq_busy(struct umtx_key *key)
403 struct umtxq_chain *uc;
405 uc = umtxq_getchain(key);
406 mtx_assert(&uc->uc_lock, MA_OWNED);
410 int count = BUSY_SPINS;
413 while (uc->uc_busy && --count > 0)
419 while (uc->uc_busy) {
421 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
432 umtxq_unbusy(struct umtx_key *key)
434 struct umtxq_chain *uc;
436 uc = umtxq_getchain(key);
437 mtx_assert(&uc->uc_lock, MA_OWNED);
438 KASSERT(uc->uc_busy != 0, ("not busy"));
445 umtxq_unbusy_unlocked(struct umtx_key *key)
453 static struct umtxq_queue *
454 umtxq_queue_lookup(struct umtx_key *key, int q)
456 struct umtxq_queue *uh;
457 struct umtxq_chain *uc;
459 uc = umtxq_getchain(key);
460 UMTXQ_LOCKED_ASSERT(uc);
461 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
462 if (umtx_key_match(&uh->key, key))
470 umtxq_insert_queue(struct umtx_q *uq, int q)
472 struct umtxq_queue *uh;
473 struct umtxq_chain *uc;
475 uc = umtxq_getchain(&uq->uq_key);
476 UMTXQ_LOCKED_ASSERT(uc);
477 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
478 uh = umtxq_queue_lookup(&uq->uq_key, q);
480 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
482 uh = uq->uq_spare_queue;
483 uh->key = uq->uq_key;
484 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
485 #ifdef UMTX_PROFILING
487 if (uc->length > uc->max_length) {
488 uc->max_length = uc->length;
489 if (uc->max_length > max_length)
490 max_length = uc->max_length;
494 uq->uq_spare_queue = NULL;
496 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
498 uq->uq_flags |= UQF_UMTXQ;
499 uq->uq_cur_queue = uh;
504 umtxq_remove_queue(struct umtx_q *uq, int q)
506 struct umtxq_chain *uc;
507 struct umtxq_queue *uh;
509 uc = umtxq_getchain(&uq->uq_key);
510 UMTXQ_LOCKED_ASSERT(uc);
511 if (uq->uq_flags & UQF_UMTXQ) {
512 uh = uq->uq_cur_queue;
513 TAILQ_REMOVE(&uh->head, uq, uq_link);
515 uq->uq_flags &= ~UQF_UMTXQ;
516 if (TAILQ_EMPTY(&uh->head)) {
517 KASSERT(uh->length == 0,
518 ("inconsistent umtxq_queue length"));
519 #ifdef UMTX_PROFILING
522 LIST_REMOVE(uh, link);
524 uh = LIST_FIRST(&uc->uc_spare_queue);
525 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
526 LIST_REMOVE(uh, link);
528 uq->uq_spare_queue = uh;
529 uq->uq_cur_queue = NULL;
534 * Check if there are multiple waiters
537 umtxq_count(struct umtx_key *key)
539 struct umtxq_queue *uh;
541 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
542 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
549 * Check if there are multiple PI waiters and returns first
553 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
555 struct umtxq_queue *uh;
558 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
559 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
561 *first = TAILQ_FIRST(&uh->head);
568 * Wake up threads waiting on an userland object by a bit mask.
571 umtxq_signal_mask(struct umtx_key *key, int n_wake, u_int bitset)
573 struct umtxq_queue *uh;
574 struct umtx_q *uq, *uq_temp;
578 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
579 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
582 TAILQ_FOREACH_SAFE(uq, &uh->head, uq_link, uq_temp) {
583 if ((uq->uq_bitset & bitset) == 0)
585 umtxq_remove_queue(uq, UMTX_SHARED_QUEUE);
594 * Wake up threads waiting on an userland object.
598 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
600 struct umtxq_queue *uh;
605 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
606 uh = umtxq_queue_lookup(key, q);
608 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
609 umtxq_remove_queue(uq, q);
619 * Wake up specified thread.
622 umtxq_signal_thread(struct umtx_q *uq)
625 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
631 * Wake up a maximum of n_wake threads that are waiting on an userland
632 * object identified by key. The remaining threads are removed from queue
633 * identified by key and added to the queue identified by key2 (requeued).
634 * The n_requeue specifies an upper limit on the number of threads that
635 * are requeued to the second queue.
638 umtxq_requeue(struct umtx_key *key, int n_wake, struct umtx_key *key2,
641 struct umtxq_queue *uh;
642 struct umtx_q *uq, *uq_temp;
646 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
647 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key2));
648 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
651 TAILQ_FOREACH_SAFE(uq, &uh->head, uq_link, uq_temp) {
652 if (++ret <= n_wake) {
659 if (ret - n_wake == n_requeue)
667 tstohz(const struct timespec *tsp)
671 TIMESPEC_TO_TIMEVAL(&tv, tsp);
676 umtx_abs_timeout_init(struct umtx_abs_timeout *timo, int clockid,
677 int absolute, const struct timespec *timeout)
680 timo->clockid = clockid;
682 timo->is_abs_real = false;
683 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
684 timespecadd(&timo->cur, timeout, &timo->end);
686 timo->end = *timeout;
687 timo->is_abs_real = clockid == CLOCK_REALTIME ||
688 clockid == CLOCK_REALTIME_FAST ||
689 clockid == CLOCK_REALTIME_PRECISE ||
690 clockid == CLOCK_SECOND;
695 umtx_abs_timeout_init2(struct umtx_abs_timeout *timo,
696 const struct _umtx_time *umtxtime)
699 umtx_abs_timeout_init(timo, umtxtime->_clockid,
700 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
704 umtx_abs_timeout_getsbt(struct umtx_abs_timeout *timo, sbintime_t *sbt,
707 struct bintime bt, bbt;
710 switch (timo->clockid) {
712 /* Clocks that can be converted into absolute time. */
714 case CLOCK_REALTIME_PRECISE:
715 case CLOCK_REALTIME_FAST:
716 case CLOCK_MONOTONIC:
717 case CLOCK_MONOTONIC_PRECISE:
718 case CLOCK_MONOTONIC_FAST:
720 case CLOCK_UPTIME_PRECISE:
721 case CLOCK_UPTIME_FAST:
723 timespec2bintime(&timo->end, &bt);
724 switch (timo->clockid) {
726 case CLOCK_REALTIME_PRECISE:
727 case CLOCK_REALTIME_FAST:
729 getboottimebin(&bbt);
730 bintime_sub(&bt, &bbt);
735 if (bt.sec >= (SBT_MAX >> 32)) {
741 switch (timo->clockid) {
742 case CLOCK_REALTIME_FAST:
743 case CLOCK_MONOTONIC_FAST:
744 case CLOCK_UPTIME_FAST:
754 /* Clocks that has to be periodically polled. */
757 case CLOCK_THREAD_CPUTIME_ID:
758 case CLOCK_PROCESS_CPUTIME_ID:
760 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
761 if (timespeccmp(&timo->end, &timo->cur, <=))
763 timespecsub(&timo->end, &timo->cur, &tts);
764 *sbt = tick_sbt * tstohz(&tts);
765 *flags = C_HARDCLOCK;
771 umtx_unlock_val(uint32_t flags, bool rb)
775 return (UMUTEX_RB_OWNERDEAD);
776 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
777 return (UMUTEX_RB_NOTRECOV);
779 return (UMUTEX_UNOWNED);
784 * Put thread into sleep state, before sleeping, check if
785 * thread was removed from umtx queue.
788 umtxq_sleep(struct umtx_q *uq, const char *wmesg,
789 struct umtx_abs_timeout *timo)
791 struct umtxq_chain *uc;
793 int error, flags = 0;
795 uc = umtxq_getchain(&uq->uq_key);
796 UMTXQ_LOCKED_ASSERT(uc);
798 if (!(uq->uq_flags & UQF_UMTXQ)) {
803 if (timo->is_abs_real)
804 curthread->td_rtcgen =
805 atomic_load_acq_int(&rtc_generation);
806 error = umtx_abs_timeout_getsbt(timo, &sbt, &flags);
810 error = msleep_sbt(uq, &uc->uc_lock, PCATCH | PDROP, wmesg,
812 uc = umtxq_getchain(&uq->uq_key);
813 mtx_lock(&uc->uc_lock);
814 if (error == EINTR || error == ERESTART)
816 if (error == EWOULDBLOCK && (flags & C_ABSOLUTE) != 0) {
822 curthread->td_rtcgen = 0;
827 * Convert userspace address into unique logical address.
830 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
832 struct thread *td = curthread;
834 vm_map_entry_t entry;
840 if (share == THREAD_SHARE) {
842 key->info.private.vs = td->td_proc->p_vmspace;
843 key->info.private.addr = (uintptr_t)addr;
845 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
846 map = &td->td_proc->p_vmspace->vm_map;
847 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
848 &entry, &key->info.shared.object, &pindex, &prot,
849 &wired) != KERN_SUCCESS) {
853 if ((share == PROCESS_SHARE) ||
854 (share == AUTO_SHARE &&
855 VM_INHERIT_SHARE == entry->inheritance)) {
857 key->info.shared.offset = (vm_offset_t)addr -
858 entry->start + entry->offset;
859 vm_object_reference(key->info.shared.object);
862 key->info.private.vs = td->td_proc->p_vmspace;
863 key->info.private.addr = (uintptr_t)addr;
865 vm_map_lookup_done(map, entry);
876 umtx_key_release(struct umtx_key *key)
879 vm_object_deallocate(key->info.shared.object);
882 #ifdef COMPAT_FREEBSD10
884 * Lock a umtx object.
887 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
888 const struct timespec *timeout)
890 struct umtx_abs_timeout timo;
898 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
901 * Care must be exercised when dealing with umtx structure. It
902 * can fault on any access.
906 * Try the uncontested case. This should be done in userland.
908 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
910 /* The acquire succeeded. */
911 if (owner == UMTX_UNOWNED)
914 /* The address was invalid. */
918 /* If no one owns it but it is contested try to acquire it. */
919 if (owner == UMTX_CONTESTED) {
920 owner = casuword(&umtx->u_owner,
921 UMTX_CONTESTED, id | UMTX_CONTESTED);
923 if (owner == UMTX_CONTESTED)
926 /* The address was invalid. */
930 error = thread_check_susp(td, false);
934 /* If this failed the lock has changed, restart. */
939 * If we caught a signal, we have retried and now
945 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
946 AUTO_SHARE, &uq->uq_key)) != 0)
949 umtxq_lock(&uq->uq_key);
950 umtxq_busy(&uq->uq_key);
952 umtxq_unbusy(&uq->uq_key);
953 umtxq_unlock(&uq->uq_key);
956 * Set the contested bit so that a release in user space
957 * knows to use the system call for unlock. If this fails
958 * either some one else has acquired the lock or it has been
961 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
963 /* The address was invalid. */
965 umtxq_lock(&uq->uq_key);
967 umtxq_unlock(&uq->uq_key);
968 umtx_key_release(&uq->uq_key);
973 * We set the contested bit, sleep. Otherwise the lock changed
974 * and we need to retry or we lost a race to the thread
975 * unlocking the umtx.
977 umtxq_lock(&uq->uq_key);
979 error = umtxq_sleep(uq, "umtx", timeout == NULL ? NULL :
982 umtxq_unlock(&uq->uq_key);
983 umtx_key_release(&uq->uq_key);
986 error = thread_check_susp(td, false);
989 if (timeout == NULL) {
990 /* Mutex locking is restarted if it is interrupted. */
994 /* Timed-locking is not restarted. */
995 if (error == ERESTART)
1002 * Unlock a umtx object.
1005 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
1007 struct umtx_key key;
1014 * Make sure we own this mtx.
1016 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
1020 if ((owner & ~UMTX_CONTESTED) != id)
1023 /* This should be done in userland */
1024 if ((owner & UMTX_CONTESTED) == 0) {
1025 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
1033 /* We should only ever be in here for contested locks */
1034 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1040 count = umtxq_count(&key);
1044 * When unlocking the umtx, it must be marked as unowned if
1045 * there is zero or one thread only waiting for it.
1046 * Otherwise, it must be marked as contested.
1048 old = casuword(&umtx->u_owner, owner,
1049 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
1051 umtxq_signal(&key,1);
1054 umtx_key_release(&key);
1062 #ifdef COMPAT_FREEBSD32
1065 * Lock a umtx object.
1068 do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id,
1069 const struct timespec *timeout)
1071 struct umtx_abs_timeout timo;
1079 if (timeout != NULL)
1080 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
1083 * Care must be exercised when dealing with umtx structure. It
1084 * can fault on any access.
1088 * Try the uncontested case. This should be done in userland.
1090 owner = casuword32(m, UMUTEX_UNOWNED, id);
1092 /* The acquire succeeded. */
1093 if (owner == UMUTEX_UNOWNED)
1096 /* The address was invalid. */
1100 /* If no one owns it but it is contested try to acquire it. */
1101 if (owner == UMUTEX_CONTESTED) {
1102 owner = casuword32(m,
1103 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1104 if (owner == UMUTEX_CONTESTED)
1107 /* The address was invalid. */
1111 error = thread_check_susp(td, false);
1115 /* If this failed the lock has changed, restart. */
1120 * If we caught a signal, we have retried and now
1126 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
1127 AUTO_SHARE, &uq->uq_key)) != 0)
1130 umtxq_lock(&uq->uq_key);
1131 umtxq_busy(&uq->uq_key);
1133 umtxq_unbusy(&uq->uq_key);
1134 umtxq_unlock(&uq->uq_key);
1137 * Set the contested bit so that a release in user space
1138 * knows to use the system call for unlock. If this fails
1139 * either some one else has acquired the lock or it has been
1142 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
1144 /* The address was invalid. */
1146 umtxq_lock(&uq->uq_key);
1148 umtxq_unlock(&uq->uq_key);
1149 umtx_key_release(&uq->uq_key);
1154 * We set the contested bit, sleep. Otherwise the lock changed
1155 * and we need to retry or we lost a race to the thread
1156 * unlocking the umtx.
1158 umtxq_lock(&uq->uq_key);
1160 error = umtxq_sleep(uq, "umtx", timeout == NULL ?
1163 umtxq_unlock(&uq->uq_key);
1164 umtx_key_release(&uq->uq_key);
1167 error = thread_check_susp(td, false);
1170 if (timeout == NULL) {
1171 /* Mutex locking is restarted if it is interrupted. */
1175 /* Timed-locking is not restarted. */
1176 if (error == ERESTART)
1183 * Unlock a umtx object.
1186 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
1188 struct umtx_key key;
1195 * Make sure we own this mtx.
1197 owner = fuword32(m);
1201 if ((owner & ~UMUTEX_CONTESTED) != id)
1204 /* This should be done in userland */
1205 if ((owner & UMUTEX_CONTESTED) == 0) {
1206 old = casuword32(m, owner, UMUTEX_UNOWNED);
1214 /* We should only ever be in here for contested locks */
1215 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1221 count = umtxq_count(&key);
1225 * When unlocking the umtx, it must be marked as unowned if
1226 * there is zero or one thread only waiting for it.
1227 * Otherwise, it must be marked as contested.
1229 old = casuword32(m, owner,
1230 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1232 umtxq_signal(&key,1);
1235 umtx_key_release(&key);
1242 #endif /* COMPAT_FREEBSD32 */
1243 #endif /* COMPAT_FREEBSD10 */
1246 * Fetch and compare value, sleep on the address if value is not changed.
1249 do_wait(struct thread *td, void *addr, u_long id,
1250 struct _umtx_time *timeout, int compat32, int is_private)
1252 struct umtx_abs_timeout timo;
1259 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
1260 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
1263 if (timeout != NULL)
1264 umtx_abs_timeout_init2(&timo, timeout);
1266 umtxq_lock(&uq->uq_key);
1268 umtxq_unlock(&uq->uq_key);
1269 if (compat32 == 0) {
1270 error = fueword(addr, &tmp);
1274 error = fueword32(addr, &tmp32);
1280 umtxq_lock(&uq->uq_key);
1283 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
1285 if ((uq->uq_flags & UQF_UMTXQ) == 0)
1289 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
1292 umtxq_unlock(&uq->uq_key);
1293 umtx_key_release(&uq->uq_key);
1294 if (error == ERESTART)
1300 * Wake up threads sleeping on the specified address.
1303 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1305 struct umtx_key key;
1308 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1309 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1312 umtxq_signal(&key, n_wake);
1314 umtx_key_release(&key);
1319 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1322 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1323 struct _umtx_time *timeout, int mode)
1325 struct umtx_abs_timeout timo;
1327 uint32_t owner, old, id;
1333 if (timeout != NULL)
1334 umtx_abs_timeout_init2(&timo, timeout);
1337 * Care must be exercised when dealing with umtx structure. It
1338 * can fault on any access.
1341 rv = fueword32(&m->m_owner, &owner);
1344 if (mode == _UMUTEX_WAIT) {
1345 if (owner == UMUTEX_UNOWNED ||
1346 owner == UMUTEX_CONTESTED ||
1347 owner == UMUTEX_RB_OWNERDEAD ||
1348 owner == UMUTEX_RB_NOTRECOV)
1352 * Robust mutex terminated. Kernel duty is to
1353 * return EOWNERDEAD to the userspace. The
1354 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1355 * by the common userspace code.
1357 if (owner == UMUTEX_RB_OWNERDEAD) {
1358 rv = casueword32(&m->m_owner,
1359 UMUTEX_RB_OWNERDEAD, &owner,
1360 id | UMUTEX_CONTESTED);
1364 MPASS(owner == UMUTEX_RB_OWNERDEAD);
1365 return (EOWNERDEAD); /* success */
1368 rv = thread_check_susp(td, false);
1373 if (owner == UMUTEX_RB_NOTRECOV)
1374 return (ENOTRECOVERABLE);
1377 * Try the uncontested case. This should be
1380 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1382 /* The address was invalid. */
1386 /* The acquire succeeded. */
1388 MPASS(owner == UMUTEX_UNOWNED);
1393 * If no one owns it but it is contested try
1397 if (owner == UMUTEX_CONTESTED) {
1398 rv = casueword32(&m->m_owner,
1399 UMUTEX_CONTESTED, &owner,
1400 id | UMUTEX_CONTESTED);
1401 /* The address was invalid. */
1405 MPASS(owner == UMUTEX_CONTESTED);
1409 rv = thread_check_susp(td, false);
1415 * If this failed the lock has
1421 /* rv == 1 but not contested, likely store failure */
1422 rv = thread_check_susp(td, false);
1427 if (mode == _UMUTEX_TRY)
1431 * If we caught a signal, we have retried and now
1437 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1438 GET_SHARE(flags), &uq->uq_key)) != 0)
1441 umtxq_lock(&uq->uq_key);
1442 umtxq_busy(&uq->uq_key);
1444 umtxq_unlock(&uq->uq_key);
1447 * Set the contested bit so that a release in user space
1448 * knows to use the system call for unlock. If this fails
1449 * either some one else has acquired the lock or it has been
1452 rv = casueword32(&m->m_owner, owner, &old,
1453 owner | UMUTEX_CONTESTED);
1455 /* The address was invalid or casueword failed to store. */
1456 if (rv == -1 || rv == 1) {
1457 umtxq_lock(&uq->uq_key);
1459 umtxq_unbusy(&uq->uq_key);
1460 umtxq_unlock(&uq->uq_key);
1461 umtx_key_release(&uq->uq_key);
1465 rv = thread_check_susp(td, false);
1473 * We set the contested bit, sleep. Otherwise the lock changed
1474 * and we need to retry or we lost a race to the thread
1475 * unlocking the umtx.
1477 umtxq_lock(&uq->uq_key);
1478 umtxq_unbusy(&uq->uq_key);
1479 MPASS(old == owner);
1480 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1483 umtxq_unlock(&uq->uq_key);
1484 umtx_key_release(&uq->uq_key);
1487 error = thread_check_susp(td, false);
1494 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1497 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1499 struct umtx_key key;
1500 uint32_t owner, old, id, newlock;
1507 * Make sure we own this mtx.
1509 error = fueword32(&m->m_owner, &owner);
1513 if ((owner & ~UMUTEX_CONTESTED) != id)
1516 newlock = umtx_unlock_val(flags, rb);
1517 if ((owner & UMUTEX_CONTESTED) == 0) {
1518 error = casueword32(&m->m_owner, owner, &old, newlock);
1522 error = thread_check_susp(td, false);
1527 MPASS(old == owner);
1531 /* We should only ever be in here for contested locks */
1532 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1538 count = umtxq_count(&key);
1542 * When unlocking the umtx, it must be marked as unowned if
1543 * there is zero or one thread only waiting for it.
1544 * Otherwise, it must be marked as contested.
1547 newlock |= UMUTEX_CONTESTED;
1548 error = casueword32(&m->m_owner, owner, &old, newlock);
1550 umtxq_signal(&key, 1);
1553 umtx_key_release(&key);
1559 error = thread_check_susp(td, false);
1568 * Check if the mutex is available and wake up a waiter,
1569 * only for simple mutex.
1572 do_wake_umutex(struct thread *td, struct umutex *m)
1574 struct umtx_key key;
1581 error = fueword32(&m->m_owner, &owner);
1585 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1586 owner != UMUTEX_RB_NOTRECOV)
1589 error = fueword32(&m->m_flags, &flags);
1593 /* We should only ever be in here for contested locks */
1594 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1600 count = umtxq_count(&key);
1603 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1604 owner != UMUTEX_RB_NOTRECOV) {
1605 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1609 } else if (error == 1) {
1613 umtx_key_release(&key);
1614 error = thread_check_susp(td, false);
1622 if (error == 0 && count != 0) {
1623 MPASS((owner & ~UMUTEX_CONTESTED) == 0 ||
1624 owner == UMUTEX_RB_OWNERDEAD ||
1625 owner == UMUTEX_RB_NOTRECOV);
1626 umtxq_signal(&key, 1);
1630 umtx_key_release(&key);
1635 * Check if the mutex has waiters and tries to fix contention bit.
1638 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1640 struct umtx_key key;
1641 uint32_t owner, old;
1646 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1650 type = TYPE_NORMAL_UMUTEX;
1652 case UMUTEX_PRIO_INHERIT:
1653 type = TYPE_PI_UMUTEX;
1655 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1656 type = TYPE_PI_ROBUST_UMUTEX;
1658 case UMUTEX_PRIO_PROTECT:
1659 type = TYPE_PP_UMUTEX;
1661 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1662 type = TYPE_PP_ROBUST_UMUTEX;
1667 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1673 count = umtxq_count(&key);
1676 error = fueword32(&m->m_owner, &owner);
1681 * Only repair contention bit if there is a waiter, this means
1682 * the mutex is still being referenced by userland code,
1683 * otherwise don't update any memory.
1685 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0 &&
1686 (count > 1 || (count == 1 && (owner & ~UMUTEX_CONTESTED) != 0))) {
1687 error = casueword32(&m->m_owner, owner, &old,
1688 owner | UMUTEX_CONTESTED);
1694 MPASS(old == owner);
1698 error = thread_check_susp(td, false);
1702 if (error == EFAULT) {
1703 umtxq_signal(&key, INT_MAX);
1704 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1705 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1706 umtxq_signal(&key, 1);
1709 umtx_key_release(&key);
1714 umtx_pi_alloc(int flags)
1718 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1719 TAILQ_INIT(&pi->pi_blocked);
1720 atomic_add_int(&umtx_pi_allocated, 1);
1725 umtx_pi_free(struct umtx_pi *pi)
1727 uma_zfree(umtx_pi_zone, pi);
1728 atomic_add_int(&umtx_pi_allocated, -1);
1732 * Adjust the thread's position on a pi_state after its priority has been
1736 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1738 struct umtx_q *uq, *uq1, *uq2;
1741 mtx_assert(&umtx_lock, MA_OWNED);
1748 * Check if the thread needs to be moved on the blocked chain.
1749 * It needs to be moved if either its priority is lower than
1750 * the previous thread or higher than the next thread.
1752 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1753 uq2 = TAILQ_NEXT(uq, uq_lockq);
1754 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1755 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1757 * Remove thread from blocked chain and determine where
1758 * it should be moved to.
1760 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1761 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1762 td1 = uq1->uq_thread;
1763 MPASS(td1->td_proc->p_magic == P_MAGIC);
1764 if (UPRI(td1) > UPRI(td))
1769 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1771 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1776 static struct umtx_pi *
1777 umtx_pi_next(struct umtx_pi *pi)
1779 struct umtx_q *uq_owner;
1781 if (pi->pi_owner == NULL)
1783 uq_owner = pi->pi_owner->td_umtxq;
1784 if (uq_owner == NULL)
1786 return (uq_owner->uq_pi_blocked);
1790 * Floyd's Cycle-Finding Algorithm.
1793 umtx_pi_check_loop(struct umtx_pi *pi)
1795 struct umtx_pi *pi1; /* fast iterator */
1797 mtx_assert(&umtx_lock, MA_OWNED);
1802 pi = umtx_pi_next(pi);
1805 pi1 = umtx_pi_next(pi1);
1808 pi1 = umtx_pi_next(pi1);
1818 * Propagate priority when a thread is blocked on POSIX
1822 umtx_propagate_priority(struct thread *td)
1828 mtx_assert(&umtx_lock, MA_OWNED);
1831 pi = uq->uq_pi_blocked;
1834 if (umtx_pi_check_loop(pi))
1839 if (td == NULL || td == curthread)
1842 MPASS(td->td_proc != NULL);
1843 MPASS(td->td_proc->p_magic == P_MAGIC);
1846 if (td->td_lend_user_pri > pri)
1847 sched_lend_user_prio(td, pri);
1855 * Pick up the lock that td is blocked on.
1858 pi = uq->uq_pi_blocked;
1861 /* Resort td on the list if needed. */
1862 umtx_pi_adjust_thread(pi, td);
1867 * Unpropagate priority for a PI mutex when a thread blocked on
1868 * it is interrupted by signal or resumed by others.
1871 umtx_repropagate_priority(struct umtx_pi *pi)
1873 struct umtx_q *uq, *uq_owner;
1874 struct umtx_pi *pi2;
1877 mtx_assert(&umtx_lock, MA_OWNED);
1879 if (umtx_pi_check_loop(pi))
1881 while (pi != NULL && pi->pi_owner != NULL) {
1883 uq_owner = pi->pi_owner->td_umtxq;
1885 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1886 uq = TAILQ_FIRST(&pi2->pi_blocked);
1888 if (pri > UPRI(uq->uq_thread))
1889 pri = UPRI(uq->uq_thread);
1893 if (pri > uq_owner->uq_inherited_pri)
1894 pri = uq_owner->uq_inherited_pri;
1895 thread_lock(pi->pi_owner);
1896 sched_lend_user_prio(pi->pi_owner, pri);
1897 thread_unlock(pi->pi_owner);
1898 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1899 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1904 * Insert a PI mutex into owned list.
1907 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1909 struct umtx_q *uq_owner;
1911 uq_owner = owner->td_umtxq;
1912 mtx_assert(&umtx_lock, MA_OWNED);
1913 MPASS(pi->pi_owner == NULL);
1914 pi->pi_owner = owner;
1915 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1919 * Disown a PI mutex, and remove it from the owned list.
1922 umtx_pi_disown(struct umtx_pi *pi)
1925 mtx_assert(&umtx_lock, MA_OWNED);
1926 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1927 pi->pi_owner = NULL;
1931 * Claim ownership of a PI mutex.
1934 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1939 mtx_lock(&umtx_lock);
1940 if (pi->pi_owner == owner) {
1941 mtx_unlock(&umtx_lock);
1945 if (pi->pi_owner != NULL) {
1947 * userland may have already messed the mutex, sigh.
1949 mtx_unlock(&umtx_lock);
1952 umtx_pi_setowner(pi, owner);
1953 uq = TAILQ_FIRST(&pi->pi_blocked);
1955 pri = UPRI(uq->uq_thread);
1957 if (pri < UPRI(owner))
1958 sched_lend_user_prio(owner, pri);
1959 thread_unlock(owner);
1961 mtx_unlock(&umtx_lock);
1966 * Adjust a thread's order position in its blocked PI mutex,
1967 * this may result new priority propagating process.
1970 umtx_pi_adjust(struct thread *td, u_char oldpri)
1976 mtx_lock(&umtx_lock);
1978 * Pick up the lock that td is blocked on.
1980 pi = uq->uq_pi_blocked;
1982 umtx_pi_adjust_thread(pi, td);
1983 umtx_repropagate_priority(pi);
1985 mtx_unlock(&umtx_lock);
1989 * Sleep on a PI mutex.
1992 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
1993 const char *wmesg, struct umtx_abs_timeout *timo, bool shared)
1995 struct thread *td, *td1;
1999 struct umtxq_chain *uc;
2001 uc = umtxq_getchain(&pi->pi_key);
2005 KASSERT(td == curthread, ("inconsistent uq_thread"));
2006 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
2007 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
2009 mtx_lock(&umtx_lock);
2010 if (pi->pi_owner == NULL) {
2011 mtx_unlock(&umtx_lock);
2012 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
2013 mtx_lock(&umtx_lock);
2015 if (pi->pi_owner == NULL)
2016 umtx_pi_setowner(pi, td1);
2017 PROC_UNLOCK(td1->td_proc);
2021 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
2022 pri = UPRI(uq1->uq_thread);
2028 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
2030 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
2032 uq->uq_pi_blocked = pi;
2034 td->td_flags |= TDF_UPIBLOCKED;
2036 umtx_propagate_priority(td);
2037 mtx_unlock(&umtx_lock);
2038 umtxq_unbusy(&uq->uq_key);
2040 error = umtxq_sleep(uq, wmesg, timo);
2043 mtx_lock(&umtx_lock);
2044 uq->uq_pi_blocked = NULL;
2046 td->td_flags &= ~TDF_UPIBLOCKED;
2048 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
2049 umtx_repropagate_priority(pi);
2050 mtx_unlock(&umtx_lock);
2051 umtxq_unlock(&uq->uq_key);
2057 * Add reference count for a PI mutex.
2060 umtx_pi_ref(struct umtx_pi *pi)
2063 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
2068 * Decrease reference count for a PI mutex, if the counter
2069 * is decreased to zero, its memory space is freed.
2072 umtx_pi_unref(struct umtx_pi *pi)
2074 struct umtxq_chain *uc;
2076 uc = umtxq_getchain(&pi->pi_key);
2077 UMTXQ_LOCKED_ASSERT(uc);
2078 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
2079 if (--pi->pi_refcount == 0) {
2080 mtx_lock(&umtx_lock);
2081 if (pi->pi_owner != NULL)
2083 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
2084 ("blocked queue not empty"));
2085 mtx_unlock(&umtx_lock);
2086 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
2092 * Find a PI mutex in hash table.
2095 umtx_pi_lookup(struct umtx_key *key)
2097 struct umtxq_chain *uc;
2100 uc = umtxq_getchain(key);
2101 UMTXQ_LOCKED_ASSERT(uc);
2103 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
2104 if (umtx_key_match(&pi->pi_key, key)) {
2112 * Insert a PI mutex into hash table.
2115 umtx_pi_insert(struct umtx_pi *pi)
2117 struct umtxq_chain *uc;
2119 uc = umtxq_getchain(&pi->pi_key);
2120 UMTXQ_LOCKED_ASSERT(uc);
2121 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
2125 * Drop a PI mutex and wakeup a top waiter.
2128 umtx_pi_drop(struct thread *td, struct umtx_key *key, bool rb, int *count)
2130 struct umtx_q *uq_first, *uq_first2, *uq_me;
2131 struct umtx_pi *pi, *pi2;
2134 UMTXQ_ASSERT_LOCKED_BUSY(key);
2135 *count = umtxq_count_pi(key, &uq_first);
2136 if (uq_first != NULL) {
2137 mtx_lock(&umtx_lock);
2138 pi = uq_first->uq_pi_blocked;
2139 KASSERT(pi != NULL, ("pi == NULL?"));
2140 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2141 mtx_unlock(&umtx_lock);
2142 /* userland messed the mutex */
2145 uq_me = td->td_umtxq;
2146 if (pi->pi_owner == td)
2148 /* get highest priority thread which is still sleeping. */
2149 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2150 while (uq_first != NULL &&
2151 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2152 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2155 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2156 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2157 if (uq_first2 != NULL) {
2158 if (pri > UPRI(uq_first2->uq_thread))
2159 pri = UPRI(uq_first2->uq_thread);
2163 sched_lend_user_prio(td, pri);
2165 mtx_unlock(&umtx_lock);
2167 umtxq_signal_thread(uq_first);
2169 pi = umtx_pi_lookup(key);
2171 * A umtx_pi can exist if a signal or timeout removed the
2172 * last waiter from the umtxq, but there is still
2173 * a thread in do_lock_pi() holding the umtx_pi.
2177 * The umtx_pi can be unowned, such as when a thread
2178 * has just entered do_lock_pi(), allocated the
2179 * umtx_pi, and unlocked the umtxq.
2180 * If the current thread owns it, it must disown it.
2182 mtx_lock(&umtx_lock);
2183 if (pi->pi_owner == td)
2185 mtx_unlock(&umtx_lock);
2195 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
2196 struct _umtx_time *timeout, int try)
2198 struct umtx_abs_timeout timo;
2200 struct umtx_pi *pi, *new_pi;
2201 uint32_t id, old_owner, owner, old;
2207 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2208 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2212 if (timeout != NULL)
2213 umtx_abs_timeout_init2(&timo, timeout);
2215 umtxq_lock(&uq->uq_key);
2216 pi = umtx_pi_lookup(&uq->uq_key);
2218 new_pi = umtx_pi_alloc(M_NOWAIT);
2219 if (new_pi == NULL) {
2220 umtxq_unlock(&uq->uq_key);
2221 new_pi = umtx_pi_alloc(M_WAITOK);
2222 umtxq_lock(&uq->uq_key);
2223 pi = umtx_pi_lookup(&uq->uq_key);
2225 umtx_pi_free(new_pi);
2229 if (new_pi != NULL) {
2230 new_pi->pi_key = uq->uq_key;
2231 umtx_pi_insert(new_pi);
2236 umtxq_unlock(&uq->uq_key);
2239 * Care must be exercised when dealing with umtx structure. It
2240 * can fault on any access.
2244 * Try the uncontested case. This should be done in userland.
2246 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
2247 /* The address was invalid. */
2252 /* The acquire succeeded. */
2254 MPASS(owner == UMUTEX_UNOWNED);
2259 if (owner == UMUTEX_RB_NOTRECOV) {
2260 error = ENOTRECOVERABLE;
2265 * Nobody owns it, but the acquire failed. This can happen
2266 * with ll/sc atomics.
2268 if (owner == UMUTEX_UNOWNED) {
2269 error = thread_check_susp(td, true);
2276 * Avoid overwriting a possible error from sleep due
2277 * to the pending signal with suspension check result.
2280 error = thread_check_susp(td, true);
2285 /* If no one owns it but it is contested try to acquire it. */
2286 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
2288 rv = casueword32(&m->m_owner, owner, &owner,
2289 id | UMUTEX_CONTESTED);
2290 /* The address was invalid. */
2297 error = thread_check_susp(td, true);
2303 * If this failed the lock could
2310 MPASS(owner == old_owner);
2311 umtxq_lock(&uq->uq_key);
2312 umtxq_busy(&uq->uq_key);
2313 error = umtx_pi_claim(pi, td);
2314 umtxq_unbusy(&uq->uq_key);
2315 umtxq_unlock(&uq->uq_key);
2318 * Since we're going to return an
2319 * error, restore the m_owner to its
2320 * previous, unowned state to avoid
2321 * compounding the problem.
2323 (void)casuword32(&m->m_owner,
2324 id | UMUTEX_CONTESTED, old_owner);
2326 if (error == 0 && old_owner == UMUTEX_RB_OWNERDEAD)
2331 if ((owner & ~UMUTEX_CONTESTED) == id) {
2342 * If we caught a signal, we have retried and now
2348 umtxq_lock(&uq->uq_key);
2349 umtxq_busy(&uq->uq_key);
2350 umtxq_unlock(&uq->uq_key);
2353 * Set the contested bit so that a release in user space
2354 * knows to use the system call for unlock. If this fails
2355 * either some one else has acquired the lock or it has been
2358 rv = casueword32(&m->m_owner, owner, &old, owner |
2361 /* The address was invalid. */
2363 umtxq_unbusy_unlocked(&uq->uq_key);
2368 umtxq_unbusy_unlocked(&uq->uq_key);
2369 error = thread_check_susp(td, true);
2374 * The lock changed and we need to retry or we
2375 * lost a race to the thread unlocking the
2376 * umtx. Note that the UMUTEX_RB_OWNERDEAD
2377 * value for owner is impossible there.
2382 umtxq_lock(&uq->uq_key);
2384 /* We set the contested bit, sleep. */
2385 MPASS(old == owner);
2386 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
2387 "umtxpi", timeout == NULL ? NULL : &timo,
2388 (flags & USYNC_PROCESS_SHARED) != 0);
2392 error = thread_check_susp(td, false);
2397 umtxq_lock(&uq->uq_key);
2399 umtxq_unlock(&uq->uq_key);
2401 umtx_key_release(&uq->uq_key);
2406 * Unlock a PI mutex.
2409 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2411 struct umtx_key key;
2412 uint32_t id, new_owner, old, owner;
2419 * Make sure we own this mtx.
2421 error = fueword32(&m->m_owner, &owner);
2425 if ((owner & ~UMUTEX_CONTESTED) != id)
2428 new_owner = umtx_unlock_val(flags, rb);
2430 /* This should be done in userland */
2431 if ((owner & UMUTEX_CONTESTED) == 0) {
2432 error = casueword32(&m->m_owner, owner, &old, new_owner);
2436 error = thread_check_susp(td, true);
2446 /* We should only ever be in here for contested locks */
2447 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2448 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2454 error = umtx_pi_drop(td, &key, rb, &count);
2458 umtx_key_release(&key);
2459 /* userland messed the mutex */
2465 * When unlocking the umtx, it must be marked as unowned if
2466 * there is zero or one thread only waiting for it.
2467 * Otherwise, it must be marked as contested.
2471 new_owner |= UMUTEX_CONTESTED;
2473 error = casueword32(&m->m_owner, owner, &old, new_owner);
2475 error = thread_check_susp(td, false);
2479 umtxq_unbusy_unlocked(&key);
2480 umtx_key_release(&key);
2483 if (error == 0 && old != owner)
2492 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2493 struct _umtx_time *timeout, int try)
2495 struct umtx_abs_timeout timo;
2496 struct umtx_q *uq, *uq2;
2500 int error, pri, old_inherited_pri, su, rv;
2504 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2505 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2509 if (timeout != NULL)
2510 umtx_abs_timeout_init2(&timo, timeout);
2512 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2514 old_inherited_pri = uq->uq_inherited_pri;
2515 umtxq_lock(&uq->uq_key);
2516 umtxq_busy(&uq->uq_key);
2517 umtxq_unlock(&uq->uq_key);
2519 rv = fueword32(&m->m_ceilings[0], &ceiling);
2524 ceiling = RTP_PRIO_MAX - ceiling;
2525 if (ceiling > RTP_PRIO_MAX) {
2530 mtx_lock(&umtx_lock);
2531 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2532 mtx_unlock(&umtx_lock);
2536 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2537 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2539 if (uq->uq_inherited_pri < UPRI(td))
2540 sched_lend_user_prio(td, uq->uq_inherited_pri);
2543 mtx_unlock(&umtx_lock);
2545 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2546 id | UMUTEX_CONTESTED);
2547 /* The address was invalid. */
2553 MPASS(owner == UMUTEX_CONTESTED);
2558 if (owner == UMUTEX_RB_OWNERDEAD) {
2559 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2560 &owner, id | UMUTEX_CONTESTED);
2566 MPASS(owner == UMUTEX_RB_OWNERDEAD);
2567 error = EOWNERDEAD; /* success */
2572 * rv == 1, only check for suspension if we
2573 * did not already catched a signal. If we
2574 * get an error from the check, the same
2575 * condition is checked by the umtxq_sleep()
2576 * call below, so we should obliterate the
2577 * error to not skip the last loop iteration.
2580 error = thread_check_susp(td, false);
2589 } else if (owner == UMUTEX_RB_NOTRECOV) {
2590 error = ENOTRECOVERABLE;
2597 * If we caught a signal, we have retried and now
2603 umtxq_lock(&uq->uq_key);
2605 umtxq_unbusy(&uq->uq_key);
2606 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2609 umtxq_unlock(&uq->uq_key);
2611 mtx_lock(&umtx_lock);
2612 uq->uq_inherited_pri = old_inherited_pri;
2614 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2615 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2617 if (pri > UPRI(uq2->uq_thread))
2618 pri = UPRI(uq2->uq_thread);
2621 if (pri > uq->uq_inherited_pri)
2622 pri = uq->uq_inherited_pri;
2624 sched_lend_user_prio(td, pri);
2626 mtx_unlock(&umtx_lock);
2629 if (error != 0 && error != EOWNERDEAD) {
2630 mtx_lock(&umtx_lock);
2631 uq->uq_inherited_pri = old_inherited_pri;
2633 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2634 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2636 if (pri > UPRI(uq2->uq_thread))
2637 pri = UPRI(uq2->uq_thread);
2640 if (pri > uq->uq_inherited_pri)
2641 pri = uq->uq_inherited_pri;
2643 sched_lend_user_prio(td, pri);
2645 mtx_unlock(&umtx_lock);
2649 umtxq_unbusy_unlocked(&uq->uq_key);
2650 umtx_key_release(&uq->uq_key);
2655 * Unlock a PP mutex.
2658 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2660 struct umtx_key key;
2661 struct umtx_q *uq, *uq2;
2663 uint32_t id, owner, rceiling;
2664 int error, pri, new_inherited_pri, su;
2668 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2671 * Make sure we own this mtx.
2673 error = fueword32(&m->m_owner, &owner);
2677 if ((owner & ~UMUTEX_CONTESTED) != id)
2680 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2685 new_inherited_pri = PRI_MAX;
2687 rceiling = RTP_PRIO_MAX - rceiling;
2688 if (rceiling > RTP_PRIO_MAX)
2690 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2693 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2694 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2701 * For priority protected mutex, always set unlocked state
2702 * to UMUTEX_CONTESTED, so that userland always enters kernel
2703 * to lock the mutex, it is necessary because thread priority
2704 * has to be adjusted for such mutex.
2706 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2711 umtxq_signal(&key, 1);
2718 mtx_lock(&umtx_lock);
2720 uq->uq_inherited_pri = new_inherited_pri;
2722 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2723 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2725 if (pri > UPRI(uq2->uq_thread))
2726 pri = UPRI(uq2->uq_thread);
2729 if (pri > uq->uq_inherited_pri)
2730 pri = uq->uq_inherited_pri;
2732 sched_lend_user_prio(td, pri);
2734 mtx_unlock(&umtx_lock);
2736 umtx_key_release(&key);
2741 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2742 uint32_t *old_ceiling)
2745 uint32_t flags, id, owner, save_ceiling;
2748 error = fueword32(&m->m_flags, &flags);
2751 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2753 if (ceiling > RTP_PRIO_MAX)
2757 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2758 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2762 umtxq_lock(&uq->uq_key);
2763 umtxq_busy(&uq->uq_key);
2764 umtxq_unlock(&uq->uq_key);
2766 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2772 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2773 id | UMUTEX_CONTESTED);
2780 MPASS(owner == UMUTEX_CONTESTED);
2781 rv = suword32(&m->m_ceilings[0], ceiling);
2782 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2783 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2787 if ((owner & ~UMUTEX_CONTESTED) == id) {
2788 rv = suword32(&m->m_ceilings[0], ceiling);
2789 error = rv == 0 ? 0 : EFAULT;
2793 if (owner == UMUTEX_RB_OWNERDEAD) {
2796 } else if (owner == UMUTEX_RB_NOTRECOV) {
2797 error = ENOTRECOVERABLE;
2802 * If we caught a signal, we have retried and now
2809 * We set the contested bit, sleep. Otherwise the lock changed
2810 * and we need to retry or we lost a race to the thread
2811 * unlocking the umtx.
2813 umtxq_lock(&uq->uq_key);
2815 umtxq_unbusy(&uq->uq_key);
2816 error = umtxq_sleep(uq, "umtxpp", NULL);
2818 umtxq_unlock(&uq->uq_key);
2820 umtxq_lock(&uq->uq_key);
2822 umtxq_signal(&uq->uq_key, INT_MAX);
2823 umtxq_unbusy(&uq->uq_key);
2824 umtxq_unlock(&uq->uq_key);
2825 umtx_key_release(&uq->uq_key);
2826 if (error == 0 && old_ceiling != NULL) {
2827 rv = suword32(old_ceiling, save_ceiling);
2828 error = rv == 0 ? 0 : EFAULT;
2834 * Lock a userland POSIX mutex.
2837 do_lock_umutex(struct thread *td, struct umutex *m,
2838 struct _umtx_time *timeout, int mode)
2843 error = fueword32(&m->m_flags, &flags);
2847 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2849 error = do_lock_normal(td, m, flags, timeout, mode);
2851 case UMUTEX_PRIO_INHERIT:
2852 error = do_lock_pi(td, m, flags, timeout, mode);
2854 case UMUTEX_PRIO_PROTECT:
2855 error = do_lock_pp(td, m, flags, timeout, mode);
2860 if (timeout == NULL) {
2861 if (error == EINTR && mode != _UMUTEX_WAIT)
2864 /* Timed-locking is not restarted. */
2865 if (error == ERESTART)
2872 * Unlock a userland POSIX mutex.
2875 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2880 error = fueword32(&m->m_flags, &flags);
2884 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2886 return (do_unlock_normal(td, m, flags, rb));
2887 case UMUTEX_PRIO_INHERIT:
2888 return (do_unlock_pi(td, m, flags, rb));
2889 case UMUTEX_PRIO_PROTECT:
2890 return (do_unlock_pp(td, m, flags, rb));
2897 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2898 struct timespec *timeout, u_long wflags)
2900 struct umtx_abs_timeout timo;
2902 uint32_t flags, clockid, hasw;
2906 error = fueword32(&cv->c_flags, &flags);
2909 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2913 if ((wflags & CVWAIT_CLOCKID) != 0) {
2914 error = fueword32(&cv->c_clockid, &clockid);
2916 umtx_key_release(&uq->uq_key);
2919 if (clockid < CLOCK_REALTIME ||
2920 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2921 /* hmm, only HW clock id will work. */
2922 umtx_key_release(&uq->uq_key);
2926 clockid = CLOCK_REALTIME;
2929 umtxq_lock(&uq->uq_key);
2930 umtxq_busy(&uq->uq_key);
2932 umtxq_unlock(&uq->uq_key);
2935 * Set c_has_waiters to 1 before releasing user mutex, also
2936 * don't modify cache line when unnecessary.
2938 error = fueword32(&cv->c_has_waiters, &hasw);
2939 if (error == 0 && hasw == 0)
2940 suword32(&cv->c_has_waiters, 1);
2942 umtxq_unbusy_unlocked(&uq->uq_key);
2944 error = do_unlock_umutex(td, m, false);
2946 if (timeout != NULL)
2947 umtx_abs_timeout_init(&timo, clockid,
2948 (wflags & CVWAIT_ABSTIME) != 0, timeout);
2950 umtxq_lock(&uq->uq_key);
2952 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2956 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2960 * This must be timeout,interrupted by signal or
2961 * surprious wakeup, clear c_has_waiter flag when
2964 umtxq_busy(&uq->uq_key);
2965 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2966 int oldlen = uq->uq_cur_queue->length;
2969 umtxq_unlock(&uq->uq_key);
2970 suword32(&cv->c_has_waiters, 0);
2971 umtxq_lock(&uq->uq_key);
2974 umtxq_unbusy(&uq->uq_key);
2975 if (error == ERESTART)
2979 umtxq_unlock(&uq->uq_key);
2980 umtx_key_release(&uq->uq_key);
2985 * Signal a userland condition variable.
2988 do_cv_signal(struct thread *td, struct ucond *cv)
2990 struct umtx_key key;
2991 int error, cnt, nwake;
2994 error = fueword32(&cv->c_flags, &flags);
2997 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
3001 cnt = umtxq_count(&key);
3002 nwake = umtxq_signal(&key, 1);
3005 error = suword32(&cv->c_has_waiters, 0);
3012 umtx_key_release(&key);
3017 do_cv_broadcast(struct thread *td, struct ucond *cv)
3019 struct umtx_key key;
3023 error = fueword32(&cv->c_flags, &flags);
3026 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
3031 umtxq_signal(&key, INT_MAX);
3034 error = suword32(&cv->c_has_waiters, 0);
3038 umtxq_unbusy_unlocked(&key);
3040 umtx_key_release(&key);
3045 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
3046 struct _umtx_time *timeout)
3048 struct umtx_abs_timeout timo;
3050 uint32_t flags, wrflags;
3051 int32_t state, oldstate;
3052 int32_t blocked_readers;
3053 int error, error1, rv;
3056 error = fueword32(&rwlock->rw_flags, &flags);
3059 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3063 if (timeout != NULL)
3064 umtx_abs_timeout_init2(&timo, timeout);
3066 wrflags = URWLOCK_WRITE_OWNER;
3067 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
3068 wrflags |= URWLOCK_WRITE_WAITERS;
3071 rv = fueword32(&rwlock->rw_state, &state);
3073 umtx_key_release(&uq->uq_key);
3077 /* try to lock it */
3078 while (!(state & wrflags)) {
3079 if (__predict_false(URWLOCK_READER_COUNT(state) ==
3080 URWLOCK_MAX_READERS)) {
3081 umtx_key_release(&uq->uq_key);
3084 rv = casueword32(&rwlock->rw_state, state,
3085 &oldstate, state + 1);
3087 umtx_key_release(&uq->uq_key);
3091 MPASS(oldstate == state);
3092 umtx_key_release(&uq->uq_key);
3095 error = thread_check_susp(td, true);
3104 /* grab monitor lock */
3105 umtxq_lock(&uq->uq_key);
3106 umtxq_busy(&uq->uq_key);
3107 umtxq_unlock(&uq->uq_key);
3110 * re-read the state, in case it changed between the try-lock above
3111 * and the check below
3113 rv = fueword32(&rwlock->rw_state, &state);
3117 /* set read contention bit */
3118 while (error == 0 && (state & wrflags) &&
3119 !(state & URWLOCK_READ_WAITERS)) {
3120 rv = casueword32(&rwlock->rw_state, state,
3121 &oldstate, state | URWLOCK_READ_WAITERS);
3127 MPASS(oldstate == state);
3131 error = thread_check_susp(td, false);
3136 umtxq_unbusy_unlocked(&uq->uq_key);
3140 /* state is changed while setting flags, restart */
3141 if (!(state & wrflags)) {
3142 umtxq_unbusy_unlocked(&uq->uq_key);
3143 error = thread_check_susp(td, true);
3151 * Contention bit is set, before sleeping, increase
3152 * read waiter count.
3154 rv = fueword32(&rwlock->rw_blocked_readers,
3157 umtxq_unbusy_unlocked(&uq->uq_key);
3161 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
3163 while (state & wrflags) {
3164 umtxq_lock(&uq->uq_key);
3166 umtxq_unbusy(&uq->uq_key);
3168 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
3171 umtxq_busy(&uq->uq_key);
3173 umtxq_unlock(&uq->uq_key);
3176 rv = fueword32(&rwlock->rw_state, &state);
3183 /* decrease read waiter count, and may clear read contention bit */
3184 rv = fueword32(&rwlock->rw_blocked_readers,
3187 umtxq_unbusy_unlocked(&uq->uq_key);
3191 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
3192 if (blocked_readers == 1) {
3193 rv = fueword32(&rwlock->rw_state, &state);
3195 umtxq_unbusy_unlocked(&uq->uq_key);
3200 rv = casueword32(&rwlock->rw_state, state,
3201 &oldstate, state & ~URWLOCK_READ_WAITERS);
3207 MPASS(oldstate == state);
3211 error1 = thread_check_susp(td, false);
3220 umtxq_unbusy_unlocked(&uq->uq_key);
3224 umtx_key_release(&uq->uq_key);
3225 if (error == ERESTART)
3231 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
3233 struct umtx_abs_timeout timo;
3236 int32_t state, oldstate;
3237 int32_t blocked_writers;
3238 int32_t blocked_readers;
3239 int error, error1, rv;
3242 error = fueword32(&rwlock->rw_flags, &flags);
3245 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3249 if (timeout != NULL)
3250 umtx_abs_timeout_init2(&timo, timeout);
3252 blocked_readers = 0;
3254 rv = fueword32(&rwlock->rw_state, &state);
3256 umtx_key_release(&uq->uq_key);
3259 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
3260 URWLOCK_READER_COUNT(state) == 0) {
3261 rv = casueword32(&rwlock->rw_state, state,
3262 &oldstate, state | URWLOCK_WRITE_OWNER);
3264 umtx_key_release(&uq->uq_key);
3268 MPASS(oldstate == state);
3269 umtx_key_release(&uq->uq_key);
3273 error = thread_check_susp(td, true);
3279 if ((state & (URWLOCK_WRITE_OWNER |
3280 URWLOCK_WRITE_WAITERS)) == 0 &&
3281 blocked_readers != 0) {
3282 umtxq_lock(&uq->uq_key);
3283 umtxq_busy(&uq->uq_key);
3284 umtxq_signal_queue(&uq->uq_key, INT_MAX,
3286 umtxq_unbusy(&uq->uq_key);
3287 umtxq_unlock(&uq->uq_key);
3293 /* grab monitor lock */
3294 umtxq_lock(&uq->uq_key);
3295 umtxq_busy(&uq->uq_key);
3296 umtxq_unlock(&uq->uq_key);
3299 * Re-read the state, in case it changed between the
3300 * try-lock above and the check below.
3302 rv = fueword32(&rwlock->rw_state, &state);
3306 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
3307 URWLOCK_READER_COUNT(state) != 0) &&
3308 (state & URWLOCK_WRITE_WAITERS) == 0) {
3309 rv = casueword32(&rwlock->rw_state, state,
3310 &oldstate, state | URWLOCK_WRITE_WAITERS);
3316 MPASS(oldstate == state);
3320 error = thread_check_susp(td, false);
3325 umtxq_unbusy_unlocked(&uq->uq_key);
3329 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
3330 URWLOCK_READER_COUNT(state) == 0) {
3331 umtxq_unbusy_unlocked(&uq->uq_key);
3332 error = thread_check_susp(td, false);
3338 rv = fueword32(&rwlock->rw_blocked_writers,
3341 umtxq_unbusy_unlocked(&uq->uq_key);
3345 suword32(&rwlock->rw_blocked_writers, blocked_writers + 1);
3347 while ((state & URWLOCK_WRITE_OWNER) ||
3348 URWLOCK_READER_COUNT(state) != 0) {
3349 umtxq_lock(&uq->uq_key);
3350 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3351 umtxq_unbusy(&uq->uq_key);
3353 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
3356 umtxq_busy(&uq->uq_key);
3357 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3358 umtxq_unlock(&uq->uq_key);
3361 rv = fueword32(&rwlock->rw_state, &state);
3368 rv = fueword32(&rwlock->rw_blocked_writers,
3371 umtxq_unbusy_unlocked(&uq->uq_key);
3375 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
3376 if (blocked_writers == 1) {
3377 rv = fueword32(&rwlock->rw_state, &state);
3379 umtxq_unbusy_unlocked(&uq->uq_key);
3384 rv = casueword32(&rwlock->rw_state, state,
3385 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3391 MPASS(oldstate == state);
3395 error1 = thread_check_susp(td, false);
3397 * We are leaving the URWLOCK_WRITE_WAITERS
3398 * behind, but this should not harm the
3407 rv = fueword32(&rwlock->rw_blocked_readers,
3410 umtxq_unbusy_unlocked(&uq->uq_key);
3415 blocked_readers = 0;
3417 umtxq_unbusy_unlocked(&uq->uq_key);
3420 umtx_key_release(&uq->uq_key);
3421 if (error == ERESTART)
3427 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3431 int32_t state, oldstate;
3432 int error, rv, q, count;
3435 error = fueword32(&rwlock->rw_flags, &flags);
3438 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3442 error = fueword32(&rwlock->rw_state, &state);
3447 if (state & URWLOCK_WRITE_OWNER) {
3449 rv = casueword32(&rwlock->rw_state, state,
3450 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3457 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3461 error = thread_check_susp(td, true);
3467 } else if (URWLOCK_READER_COUNT(state) != 0) {
3469 rv = casueword32(&rwlock->rw_state, state,
3470 &oldstate, state - 1);
3477 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3481 error = thread_check_susp(td, true);
3494 if (!(flags & URWLOCK_PREFER_READER)) {
3495 if (state & URWLOCK_WRITE_WAITERS) {
3497 q = UMTX_EXCLUSIVE_QUEUE;
3498 } else if (state & URWLOCK_READ_WAITERS) {
3500 q = UMTX_SHARED_QUEUE;
3503 if (state & URWLOCK_READ_WAITERS) {
3505 q = UMTX_SHARED_QUEUE;
3506 } else if (state & URWLOCK_WRITE_WAITERS) {
3508 q = UMTX_EXCLUSIVE_QUEUE;
3513 umtxq_lock(&uq->uq_key);
3514 umtxq_busy(&uq->uq_key);
3515 umtxq_signal_queue(&uq->uq_key, count, q);
3516 umtxq_unbusy(&uq->uq_key);
3517 umtxq_unlock(&uq->uq_key);
3520 umtx_key_release(&uq->uq_key);
3524 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3526 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3528 struct umtx_abs_timeout timo;
3530 uint32_t flags, count, count1;
3534 error = fueword32(&sem->_flags, &flags);
3537 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3541 if (timeout != NULL)
3542 umtx_abs_timeout_init2(&timo, timeout);
3545 umtxq_lock(&uq->uq_key);
3546 umtxq_busy(&uq->uq_key);
3548 umtxq_unlock(&uq->uq_key);
3549 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3551 rv1 = fueword32(&sem->_count, &count);
3552 if (rv == -1 || (rv == 0 && (rv1 == -1 || count != 0)) ||
3553 (rv == 1 && count1 == 0)) {
3554 umtxq_lock(&uq->uq_key);
3555 umtxq_unbusy(&uq->uq_key);
3557 umtxq_unlock(&uq->uq_key);
3559 rv = thread_check_susp(td, true);
3567 error = rv == -1 ? EFAULT : 0;
3570 umtxq_lock(&uq->uq_key);
3571 umtxq_unbusy(&uq->uq_key);
3573 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3575 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3579 /* A relative timeout cannot be restarted. */
3580 if (error == ERESTART && timeout != NULL &&
3581 (timeout->_flags & UMTX_ABSTIME) == 0)
3584 umtxq_unlock(&uq->uq_key);
3586 umtx_key_release(&uq->uq_key);
3591 * Signal a userland semaphore.
3594 do_sem_wake(struct thread *td, struct _usem *sem)
3596 struct umtx_key key;
3600 error = fueword32(&sem->_flags, &flags);
3603 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3607 cnt = umtxq_count(&key);
3610 * Check if count is greater than 0, this means the memory is
3611 * still being referenced by user code, so we can safely
3612 * update _has_waiters flag.
3616 error = suword32(&sem->_has_waiters, 0);
3621 umtxq_signal(&key, 1);
3625 umtx_key_release(&key);
3631 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3633 struct umtx_abs_timeout timo;
3635 uint32_t count, flags;
3639 flags = fuword32(&sem->_flags);
3640 if (timeout != NULL)
3641 umtx_abs_timeout_init2(&timo, timeout);
3644 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3647 umtxq_lock(&uq->uq_key);
3648 umtxq_busy(&uq->uq_key);
3650 umtxq_unlock(&uq->uq_key);
3651 rv = fueword32(&sem->_count, &count);
3653 umtxq_lock(&uq->uq_key);
3654 umtxq_unbusy(&uq->uq_key);
3656 umtxq_unlock(&uq->uq_key);
3657 umtx_key_release(&uq->uq_key);
3661 if (USEM_COUNT(count) != 0) {
3662 umtxq_lock(&uq->uq_key);
3663 umtxq_unbusy(&uq->uq_key);
3665 umtxq_unlock(&uq->uq_key);
3666 umtx_key_release(&uq->uq_key);
3669 if (count == USEM_HAS_WAITERS)
3671 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3674 umtxq_lock(&uq->uq_key);
3675 umtxq_unbusy(&uq->uq_key);
3677 umtxq_unlock(&uq->uq_key);
3678 umtx_key_release(&uq->uq_key);
3681 rv = thread_check_susp(td, true);
3686 umtxq_lock(&uq->uq_key);
3687 umtxq_unbusy(&uq->uq_key);
3689 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3691 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3695 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3696 /* A relative timeout cannot be restarted. */
3697 if (error == ERESTART)
3699 if (error == EINTR) {
3700 kern_clock_gettime(curthread, timo.clockid,
3702 timespecsub(&timo.end, &timo.cur,
3703 &timeout->_timeout);
3707 umtxq_unlock(&uq->uq_key);
3708 umtx_key_release(&uq->uq_key);
3713 * Signal a userland semaphore.
3716 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3718 struct umtx_key key;
3720 uint32_t count, flags;
3722 rv = fueword32(&sem->_flags, &flags);
3725 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3729 cnt = umtxq_count(&key);
3732 * If this was the last sleeping thread, clear the waiters
3737 rv = fueword32(&sem->_count, &count);
3738 while (rv != -1 && count & USEM_HAS_WAITERS) {
3739 rv = casueword32(&sem->_count, count, &count,
3740 count & ~USEM_HAS_WAITERS);
3742 rv = thread_check_susp(td, true);
3755 umtxq_signal(&key, 1);
3759 umtx_key_release(&key);
3763 #ifdef COMPAT_FREEBSD10
3765 freebsd10__umtx_lock(struct thread *td, struct freebsd10__umtx_lock_args *uap)
3767 return (do_lock_umtx(td, uap->umtx, td->td_tid, 0));
3771 freebsd10__umtx_unlock(struct thread *td,
3772 struct freebsd10__umtx_unlock_args *uap)
3774 return (do_unlock_umtx(td, uap->umtx, td->td_tid));
3779 umtx_copyin_timeout(const void *uaddr, struct timespec *tsp)
3783 error = copyin(uaddr, tsp, sizeof(*tsp));
3785 if (!timespecvalid_interval(tsp))
3792 umtx_copyin_umtx_time(const void *uaddr, size_t size, struct _umtx_time *tp)
3796 if (size <= sizeof(tp->_timeout)) {
3797 tp->_clockid = CLOCK_REALTIME;
3799 error = copyin(uaddr, &tp->_timeout, sizeof(tp->_timeout));
3801 error = copyin(uaddr, tp, sizeof(*tp));
3804 if (!timespecvalid_interval(&tp->_timeout))
3810 umtx_copyin_robust_lists(const void *uaddr, size_t size,
3811 struct umtx_robust_lists_params *rb)
3814 if (size > sizeof(*rb))
3816 return (copyin(uaddr, rb, size));
3820 umtx_copyout_timeout(void *uaddr, size_t sz, struct timespec *tsp)
3824 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
3825 * and we're only called if sz >= sizeof(timespec) as supplied in the
3828 KASSERT(sz >= sizeof(*tsp),
3829 ("umtx_copyops specifies incorrect sizes"));
3831 return (copyout(tsp, uaddr, sizeof(*tsp)));
3834 #ifdef COMPAT_FREEBSD10
3836 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap,
3837 const struct umtx_copyops *ops)
3839 struct timespec *ts, timeout;
3842 /* Allow a null timespec (wait forever). */
3843 if (uap->uaddr2 == NULL)
3846 error = ops->copyin_timeout(uap->uaddr2, &timeout);
3851 #ifdef COMPAT_FREEBSD32
3853 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
3855 return (do_lock_umtx(td, uap->obj, uap->val, ts));
3859 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap,
3860 const struct umtx_copyops *ops)
3862 #ifdef COMPAT_FREEBSD32
3864 return (do_unlock_umtx32(td, uap->obj, uap->val));
3866 return (do_unlock_umtx(td, uap->obj, uap->val));
3868 #endif /* COMPAT_FREEBSD10 */
3870 #if !defined(COMPAT_FREEBSD10)
3872 __umtx_op_unimpl(struct thread *td __unused, struct _umtx_op_args *uap __unused,
3873 const struct umtx_copyops *ops __unused)
3875 return (EOPNOTSUPP);
3877 #endif /* COMPAT_FREEBSD10 */
3880 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap,
3881 const struct umtx_copyops *ops)
3883 struct _umtx_time timeout, *tm_p;
3886 if (uap->uaddr2 == NULL)
3889 error = ops->copyin_umtx_time(
3890 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3895 return (do_wait(td, uap->obj, uap->val, tm_p, ops->compat32, 0));
3899 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap,
3900 const struct umtx_copyops *ops)
3902 struct _umtx_time timeout, *tm_p;
3905 if (uap->uaddr2 == NULL)
3908 error = ops->copyin_umtx_time(
3909 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3914 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3918 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap,
3919 const struct umtx_copyops *ops)
3921 struct _umtx_time *tm_p, timeout;
3924 if (uap->uaddr2 == NULL)
3927 error = ops->copyin_umtx_time(
3928 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3933 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3937 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap,
3938 const struct umtx_copyops *ops __unused)
3941 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3944 #define BATCH_SIZE 128
3946 __umtx_op_nwake_private_native(struct thread *td, struct _umtx_op_args *uap)
3948 char *uaddrs[BATCH_SIZE], **upp;
3949 int count, error, i, pos, tocopy;
3951 upp = (char **)uap->obj;
3953 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3955 tocopy = MIN(count, BATCH_SIZE);
3956 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3959 for (i = 0; i < tocopy; ++i) {
3960 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3968 __umtx_op_nwake_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3970 uint32_t uaddrs[BATCH_SIZE], *upp;
3971 int count, error, i, pos, tocopy;
3973 upp = (uint32_t *)uap->obj;
3975 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3977 tocopy = MIN(count, BATCH_SIZE);
3978 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
3981 for (i = 0; i < tocopy; ++i) {
3982 kern_umtx_wake(td, (void *)(uintptr_t)uaddrs[i],
3991 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap,
3992 const struct umtx_copyops *ops)
3996 return (__umtx_op_nwake_private_compat32(td, uap));
3997 return (__umtx_op_nwake_private_native(td, uap));
4001 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap,
4002 const struct umtx_copyops *ops __unused)
4005 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
4009 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap,
4010 const struct umtx_copyops *ops)
4012 struct _umtx_time *tm_p, timeout;
4015 /* Allow a null timespec (wait forever). */
4016 if (uap->uaddr2 == NULL)
4019 error = ops->copyin_umtx_time(
4020 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4025 return (do_lock_umutex(td, uap->obj, tm_p, 0));
4029 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap,
4030 const struct umtx_copyops *ops __unused)
4033 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
4037 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap,
4038 const struct umtx_copyops *ops)
4040 struct _umtx_time *tm_p, timeout;
4043 /* Allow a null timespec (wait forever). */
4044 if (uap->uaddr2 == NULL)
4047 error = ops->copyin_umtx_time(
4048 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4053 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
4057 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap,
4058 const struct umtx_copyops *ops __unused)
4061 return (do_wake_umutex(td, uap->obj));
4065 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap,
4066 const struct umtx_copyops *ops __unused)
4069 return (do_unlock_umutex(td, uap->obj, false));
4073 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap,
4074 const struct umtx_copyops *ops __unused)
4077 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
4081 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap,
4082 const struct umtx_copyops *ops)
4084 struct timespec *ts, timeout;
4087 /* Allow a null timespec (wait forever). */
4088 if (uap->uaddr2 == NULL)
4091 error = ops->copyin_timeout(uap->uaddr2, &timeout);
4096 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
4100 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap,
4101 const struct umtx_copyops *ops __unused)
4104 return (do_cv_signal(td, uap->obj));
4108 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap,
4109 const struct umtx_copyops *ops __unused)
4112 return (do_cv_broadcast(td, uap->obj));
4116 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap,
4117 const struct umtx_copyops *ops)
4119 struct _umtx_time timeout;
4122 /* Allow a null timespec (wait forever). */
4123 if (uap->uaddr2 == NULL) {
4124 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4126 error = ops->copyin_umtx_time(uap->uaddr2,
4127 (size_t)uap->uaddr1, &timeout);
4130 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4136 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap,
4137 const struct umtx_copyops *ops)
4139 struct _umtx_time timeout;
4142 /* Allow a null timespec (wait forever). */
4143 if (uap->uaddr2 == NULL) {
4144 error = do_rw_wrlock(td, uap->obj, 0);
4146 error = ops->copyin_umtx_time(uap->uaddr2,
4147 (size_t)uap->uaddr1, &timeout);
4151 error = do_rw_wrlock(td, uap->obj, &timeout);
4157 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap,
4158 const struct umtx_copyops *ops __unused)
4161 return (do_rw_unlock(td, uap->obj));
4164 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4166 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap,
4167 const struct umtx_copyops *ops)
4169 struct _umtx_time *tm_p, timeout;
4172 /* Allow a null timespec (wait forever). */
4173 if (uap->uaddr2 == NULL)
4176 error = ops->copyin_umtx_time(
4177 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4182 return (do_sem_wait(td, uap->obj, tm_p));
4186 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap,
4187 const struct umtx_copyops *ops __unused)
4190 return (do_sem_wake(td, uap->obj));
4195 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap,
4196 const struct umtx_copyops *ops __unused)
4199 return (do_wake2_umutex(td, uap->obj, uap->val));
4203 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap,
4204 const struct umtx_copyops *ops)
4206 struct _umtx_time *tm_p, timeout;
4210 /* Allow a null timespec (wait forever). */
4211 if (uap->uaddr2 == NULL) {
4215 uasize = (size_t)uap->uaddr1;
4216 error = ops->copyin_umtx_time(uap->uaddr2, uasize, &timeout);
4221 error = do_sem2_wait(td, uap->obj, tm_p);
4222 if (error == EINTR && uap->uaddr2 != NULL &&
4223 (timeout._flags & UMTX_ABSTIME) == 0 &&
4224 uasize >= ops->umtx_time_sz + ops->timespec_sz) {
4225 error = ops->copyout_timeout(
4226 (void *)((uintptr_t)uap->uaddr2 + ops->umtx_time_sz),
4227 uasize - ops->umtx_time_sz, &timeout._timeout);
4237 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap,
4238 const struct umtx_copyops *ops __unused)
4241 return (do_sem2_wake(td, uap->obj));
4244 #define USHM_OBJ_UMTX(o) \
4245 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
4247 #define USHMF_REG_LINKED 0x0001
4248 #define USHMF_OBJ_LINKED 0x0002
4249 struct umtx_shm_reg {
4250 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
4251 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
4252 struct umtx_key ushm_key;
4253 struct ucred *ushm_cred;
4254 struct shmfd *ushm_obj;
4259 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
4260 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
4262 static uma_zone_t umtx_shm_reg_zone;
4263 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
4264 static struct mtx umtx_shm_lock;
4265 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
4266 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
4268 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
4271 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
4273 struct umtx_shm_reg_head d;
4274 struct umtx_shm_reg *reg, *reg1;
4277 mtx_lock(&umtx_shm_lock);
4278 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
4279 mtx_unlock(&umtx_shm_lock);
4280 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
4281 TAILQ_REMOVE(&d, reg, ushm_reg_link);
4282 umtx_shm_free_reg(reg);
4286 static struct task umtx_shm_reg_delfree_task =
4287 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
4289 static struct umtx_shm_reg *
4290 umtx_shm_find_reg_locked(const struct umtx_key *key)
4292 struct umtx_shm_reg *reg;
4293 struct umtx_shm_reg_head *reg_head;
4295 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
4296 mtx_assert(&umtx_shm_lock, MA_OWNED);
4297 reg_head = &umtx_shm_registry[key->hash];
4298 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
4299 KASSERT(reg->ushm_key.shared,
4300 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
4301 if (reg->ushm_key.info.shared.object ==
4302 key->info.shared.object &&
4303 reg->ushm_key.info.shared.offset ==
4304 key->info.shared.offset) {
4305 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
4306 KASSERT(reg->ushm_refcnt > 0,
4307 ("reg %p refcnt 0 onlist", reg));
4308 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
4309 ("reg %p not linked", reg));
4317 static struct umtx_shm_reg *
4318 umtx_shm_find_reg(const struct umtx_key *key)
4320 struct umtx_shm_reg *reg;
4322 mtx_lock(&umtx_shm_lock);
4323 reg = umtx_shm_find_reg_locked(key);
4324 mtx_unlock(&umtx_shm_lock);
4329 umtx_shm_free_reg(struct umtx_shm_reg *reg)
4332 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
4333 crfree(reg->ushm_cred);
4334 shm_drop(reg->ushm_obj);
4335 uma_zfree(umtx_shm_reg_zone, reg);
4339 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
4343 mtx_assert(&umtx_shm_lock, MA_OWNED);
4344 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
4346 res = reg->ushm_refcnt == 0;
4348 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
4349 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
4350 reg, ushm_reg_link);
4351 reg->ushm_flags &= ~USHMF_REG_LINKED;
4353 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
4354 LIST_REMOVE(reg, ushm_obj_link);
4355 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
4362 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
4368 object = reg->ushm_obj->shm_object;
4369 VM_OBJECT_WLOCK(object);
4370 vm_object_set_flag(object, OBJ_UMTXDEAD);
4371 VM_OBJECT_WUNLOCK(object);
4373 mtx_lock(&umtx_shm_lock);
4374 dofree = umtx_shm_unref_reg_locked(reg, force);
4375 mtx_unlock(&umtx_shm_lock);
4377 umtx_shm_free_reg(reg);
4381 umtx_shm_object_init(vm_object_t object)
4384 LIST_INIT(USHM_OBJ_UMTX(object));
4388 umtx_shm_object_terminated(vm_object_t object)
4390 struct umtx_shm_reg *reg, *reg1;
4393 if (LIST_EMPTY(USHM_OBJ_UMTX(object)))
4397 mtx_lock(&umtx_shm_lock);
4398 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
4399 if (umtx_shm_unref_reg_locked(reg, true)) {
4400 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
4405 mtx_unlock(&umtx_shm_lock);
4407 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
4411 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
4412 struct umtx_shm_reg **res)
4414 struct umtx_shm_reg *reg, *reg1;
4418 reg = umtx_shm_find_reg(key);
4423 cred = td->td_ucred;
4424 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
4426 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
4427 reg->ushm_refcnt = 1;
4428 bcopy(key, ®->ushm_key, sizeof(*key));
4429 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR, false);
4430 reg->ushm_cred = crhold(cred);
4431 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
4433 umtx_shm_free_reg(reg);
4436 mtx_lock(&umtx_shm_lock);
4437 reg1 = umtx_shm_find_reg_locked(key);
4439 mtx_unlock(&umtx_shm_lock);
4440 umtx_shm_free_reg(reg);
4445 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
4446 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
4448 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
4449 mtx_unlock(&umtx_shm_lock);
4455 umtx_shm_alive(struct thread *td, void *addr)
4458 vm_map_entry_t entry;
4465 map = &td->td_proc->p_vmspace->vm_map;
4466 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
4467 &object, &pindex, &prot, &wired);
4468 if (res != KERN_SUCCESS)
4473 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
4474 vm_map_lookup_done(map, entry);
4483 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
4484 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
4485 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
4486 for (i = 0; i < nitems(umtx_shm_registry); i++)
4487 TAILQ_INIT(&umtx_shm_registry[i]);
4491 umtx_shm(struct thread *td, void *addr, u_int flags)
4493 struct umtx_key key;
4494 struct umtx_shm_reg *reg;
4498 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
4499 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
4501 if ((flags & UMTX_SHM_ALIVE) != 0)
4502 return (umtx_shm_alive(td, addr));
4503 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
4506 KASSERT(key.shared == 1, ("non-shared key"));
4507 if ((flags & UMTX_SHM_CREAT) != 0) {
4508 error = umtx_shm_create_reg(td, &key, ®);
4510 reg = umtx_shm_find_reg(&key);
4514 umtx_key_release(&key);
4517 KASSERT(reg != NULL, ("no reg"));
4518 if ((flags & UMTX_SHM_DESTROY) != 0) {
4519 umtx_shm_unref_reg(reg, true);
4523 error = mac_posixshm_check_open(td->td_ucred,
4524 reg->ushm_obj, FFLAGS(O_RDWR));
4527 error = shm_access(reg->ushm_obj, td->td_ucred,
4531 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
4533 shm_hold(reg->ushm_obj);
4534 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
4536 td->td_retval[0] = fd;
4540 umtx_shm_unref_reg(reg, false);
4545 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap,
4546 const struct umtx_copyops *ops __unused)
4549 return (umtx_shm(td, uap->uaddr1, uap->val));
4553 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap,
4554 const struct umtx_copyops *ops)
4556 struct umtx_robust_lists_params rb;
4559 if (ops->compat32) {
4560 if ((td->td_pflags2 & TDP2_COMPAT32RB) == 0 &&
4561 (td->td_rb_list != 0 || td->td_rbp_list != 0 ||
4562 td->td_rb_inact != 0))
4564 } else if ((td->td_pflags2 & TDP2_COMPAT32RB) != 0) {
4568 bzero(&rb, sizeof(rb));
4569 error = ops->copyin_robust_lists(uap->uaddr1, uap->val, &rb);
4574 td->td_pflags2 |= TDP2_COMPAT32RB;
4576 td->td_rb_list = rb.robust_list_offset;
4577 td->td_rbp_list = rb.robust_priv_list_offset;
4578 td->td_rb_inact = rb.robust_inact_offset;
4582 #if defined(__i386__) || defined(__amd64__)
4584 * Provide the standard 32-bit definitions for x86, since native/compat32 use a
4585 * 32-bit time_t there. Other architectures just need the i386 definitions
4586 * along with their standard compat32.
4588 struct timespecx32 {
4593 struct umtx_timex32 {
4594 struct timespecx32 _timeout;
4600 #define timespeci386 timespec32
4601 #define umtx_timei386 umtx_time32
4603 #else /* !__i386__ && !__amd64__ */
4604 /* 32-bit architectures can emulate i386, so define these almost everywhere. */
4605 struct timespeci386 {
4610 struct umtx_timei386 {
4611 struct timespeci386 _timeout;
4616 #if defined(__LP64__)
4617 #define timespecx32 timespec32
4618 #define umtx_timex32 umtx_time32
4623 umtx_copyin_robust_lists32(const void *uaddr, size_t size,
4624 struct umtx_robust_lists_params *rbp)
4626 struct umtx_robust_lists_params_compat32 rb32;
4629 if (size > sizeof(rb32))
4631 bzero(&rb32, sizeof(rb32));
4632 error = copyin(uaddr, &rb32, size);
4635 CP(rb32, *rbp, robust_list_offset);
4636 CP(rb32, *rbp, robust_priv_list_offset);
4637 CP(rb32, *rbp, robust_inact_offset);
4643 umtx_copyin_timeouti386(const void *uaddr, struct timespec *tsp)
4645 struct timespeci386 ts32;
4648 error = copyin(uaddr, &ts32, sizeof(ts32));
4650 if (!timespecvalid_interval(&ts32))
4653 CP(ts32, *tsp, tv_sec);
4654 CP(ts32, *tsp, tv_nsec);
4661 umtx_copyin_umtx_timei386(const void *uaddr, size_t size, struct _umtx_time *tp)
4663 struct umtx_timei386 t32;
4666 t32._clockid = CLOCK_REALTIME;
4668 if (size <= sizeof(t32._timeout))
4669 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4671 error = copyin(uaddr, &t32, sizeof(t32));
4674 if (!timespecvalid_interval(&t32._timeout))
4676 TS_CP(t32, *tp, _timeout);
4677 CP(t32, *tp, _flags);
4678 CP(t32, *tp, _clockid);
4683 umtx_copyout_timeouti386(void *uaddr, size_t sz, struct timespec *tsp)
4685 struct timespeci386 remain32 = {
4686 .tv_sec = tsp->tv_sec,
4687 .tv_nsec = tsp->tv_nsec,
4691 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4692 * and we're only called if sz >= sizeof(timespec) as supplied in the
4695 KASSERT(sz >= sizeof(remain32),
4696 ("umtx_copyops specifies incorrect sizes"));
4698 return (copyout(&remain32, uaddr, sizeof(remain32)));
4700 #endif /* !__i386__ */
4702 #if defined(__i386__) || defined(__LP64__)
4704 umtx_copyin_timeoutx32(const void *uaddr, struct timespec *tsp)
4706 struct timespecx32 ts32;
4709 error = copyin(uaddr, &ts32, sizeof(ts32));
4711 if (!timespecvalid_interval(&ts32))
4714 CP(ts32, *tsp, tv_sec);
4715 CP(ts32, *tsp, tv_nsec);
4722 umtx_copyin_umtx_timex32(const void *uaddr, size_t size, struct _umtx_time *tp)
4724 struct umtx_timex32 t32;
4727 t32._clockid = CLOCK_REALTIME;
4729 if (size <= sizeof(t32._timeout))
4730 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4732 error = copyin(uaddr, &t32, sizeof(t32));
4735 if (!timespecvalid_interval(&t32._timeout))
4737 TS_CP(t32, *tp, _timeout);
4738 CP(t32, *tp, _flags);
4739 CP(t32, *tp, _clockid);
4744 umtx_copyout_timeoutx32(void *uaddr, size_t sz, struct timespec *tsp)
4746 struct timespecx32 remain32 = {
4747 .tv_sec = tsp->tv_sec,
4748 .tv_nsec = tsp->tv_nsec,
4752 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4753 * and we're only called if sz >= sizeof(timespec) as supplied in the
4756 KASSERT(sz >= sizeof(remain32),
4757 ("umtx_copyops specifies incorrect sizes"));
4759 return (copyout(&remain32, uaddr, sizeof(remain32)));
4761 #endif /* __i386__ || __LP64__ */
4763 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap,
4764 const struct umtx_copyops *umtx_ops);
4766 static const _umtx_op_func op_table[] = {
4767 #ifdef COMPAT_FREEBSD10
4768 [UMTX_OP_LOCK] = __umtx_op_lock_umtx,
4769 [UMTX_OP_UNLOCK] = __umtx_op_unlock_umtx,
4771 [UMTX_OP_LOCK] = __umtx_op_unimpl,
4772 [UMTX_OP_UNLOCK] = __umtx_op_unimpl,
4774 [UMTX_OP_WAIT] = __umtx_op_wait,
4775 [UMTX_OP_WAKE] = __umtx_op_wake,
4776 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4777 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
4778 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4779 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4780 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
4781 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4782 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4783 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
4784 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
4785 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
4786 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4787 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4788 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4789 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4790 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4791 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4792 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4793 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4795 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4796 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4798 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4799 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4800 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4801 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4802 [UMTX_OP_SHM] = __umtx_op_shm,
4803 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4806 static const struct umtx_copyops umtx_native_ops = {
4807 .copyin_timeout = umtx_copyin_timeout,
4808 .copyin_umtx_time = umtx_copyin_umtx_time,
4809 .copyin_robust_lists = umtx_copyin_robust_lists,
4810 .copyout_timeout = umtx_copyout_timeout,
4811 .timespec_sz = sizeof(struct timespec),
4812 .umtx_time_sz = sizeof(struct _umtx_time),
4816 static const struct umtx_copyops umtx_native_opsi386 = {
4817 .copyin_timeout = umtx_copyin_timeouti386,
4818 .copyin_umtx_time = umtx_copyin_umtx_timei386,
4819 .copyin_robust_lists = umtx_copyin_robust_lists32,
4820 .copyout_timeout = umtx_copyout_timeouti386,
4821 .timespec_sz = sizeof(struct timespeci386),
4822 .umtx_time_sz = sizeof(struct umtx_timei386),
4827 #if defined(__i386__) || defined(__LP64__)
4828 /* i386 can emulate other 32-bit archs, too! */
4829 static const struct umtx_copyops umtx_native_opsx32 = {
4830 .copyin_timeout = umtx_copyin_timeoutx32,
4831 .copyin_umtx_time = umtx_copyin_umtx_timex32,
4832 .copyin_robust_lists = umtx_copyin_robust_lists32,
4833 .copyout_timeout = umtx_copyout_timeoutx32,
4834 .timespec_sz = sizeof(struct timespecx32),
4835 .umtx_time_sz = sizeof(struct umtx_timex32),
4839 #ifdef COMPAT_FREEBSD32
4841 #define umtx_native_ops32 umtx_native_opsi386
4843 #define umtx_native_ops32 umtx_native_opsx32
4845 #endif /* COMPAT_FREEBSD32 */
4846 #endif /* __i386__ || __LP64__ */
4848 #define UMTX_OP__FLAGS (UMTX_OP__32BIT | UMTX_OP__I386)
4851 kern__umtx_op(struct thread *td, void *obj, int op, unsigned long val,
4852 void *uaddr1, void *uaddr2, const struct umtx_copyops *ops)
4854 struct _umtx_op_args uap = {
4856 .op = op & ~UMTX_OP__FLAGS,
4862 if ((uap.op >= nitems(op_table)))
4864 return ((*op_table[uap.op])(td, &uap, ops));
4868 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4870 static const struct umtx_copyops *umtx_ops;
4872 umtx_ops = &umtx_native_ops;
4874 if ((uap->op & (UMTX_OP__32BIT | UMTX_OP__I386)) != 0) {
4875 if ((uap->op & UMTX_OP__I386) != 0)
4876 umtx_ops = &umtx_native_opsi386;
4878 umtx_ops = &umtx_native_opsx32;
4880 #elif !defined(__i386__)
4881 /* We consider UMTX_OP__32BIT a nop on !i386 ILP32. */
4882 if ((uap->op & UMTX_OP__I386) != 0)
4883 umtx_ops = &umtx_native_opsi386;
4885 /* Likewise, UMTX_OP__I386 is a nop on i386. */
4886 if ((uap->op & UMTX_OP__32BIT) != 0)
4887 umtx_ops = &umtx_native_opsx32;
4889 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr1,
4890 uap->uaddr2, umtx_ops));
4893 #ifdef COMPAT_FREEBSD32
4894 #ifdef COMPAT_FREEBSD10
4896 freebsd10_freebsd32__umtx_lock(struct thread *td,
4897 struct freebsd10_freebsd32__umtx_lock_args *uap)
4899 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
4903 freebsd10_freebsd32__umtx_unlock(struct thread *td,
4904 struct freebsd10_freebsd32__umtx_unlock_args *uap)
4906 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
4908 #endif /* COMPAT_FREEBSD10 */
4911 freebsd32__umtx_op(struct thread *td, struct freebsd32__umtx_op_args *uap)
4914 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr1,
4915 uap->uaddr2, &umtx_native_ops32));
4917 #endif /* COMPAT_FREEBSD32 */
4920 umtx_thread_init(struct thread *td)
4923 td->td_umtxq = umtxq_alloc();
4924 td->td_umtxq->uq_thread = td;
4928 umtx_thread_fini(struct thread *td)
4931 umtxq_free(td->td_umtxq);
4935 * It will be called when new thread is created, e.g fork().
4938 umtx_thread_alloc(struct thread *td)
4943 uq->uq_inherited_pri = PRI_MAX;
4945 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4946 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4947 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4948 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4954 * Clear robust lists for all process' threads, not delaying the
4955 * cleanup to thread exit, since the relevant address space is
4956 * destroyed right now.
4959 umtx_exec(struct proc *p)
4963 KASSERT(p == curproc, ("need curproc"));
4964 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4965 (p->p_flag & P_STOPPED_SINGLE) != 0,
4966 ("curproc must be single-threaded"));
4968 * There is no need to lock the list as only this thread can be
4971 FOREACH_THREAD_IN_PROC(p, td) {
4972 KASSERT(td == curthread ||
4973 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4974 ("running thread %p %p", p, td));
4975 umtx_thread_cleanup(td);
4976 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4984 umtx_thread_exit(struct thread *td)
4987 umtx_thread_cleanup(td);
4991 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res, bool compat32)
4998 error = fueword32((void *)ptr, &res32);
5002 error = fueword((void *)ptr, &res1);
5012 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list,
5015 struct umutex32 m32;
5018 memcpy(&m32, m, sizeof(m32));
5019 *rb_list = m32.m_rb_lnk;
5021 *rb_list = m->m_rb_lnk;
5026 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact,
5032 KASSERT(td->td_proc == curproc, ("need current vmspace"));
5033 error = copyin((void *)rbp, &m, sizeof(m));
5036 if (rb_list != NULL)
5037 umtx_read_rb_list(td, &m, rb_list, compat32);
5038 if ((m.m_flags & UMUTEX_ROBUST) == 0)
5040 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
5041 /* inact is cleared after unlock, allow the inconsistency */
5042 return (inact ? 0 : EINVAL);
5043 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
5047 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
5048 const char *name, bool compat32)
5056 error = umtx_read_uptr(td, rb_list, &rbp, compat32);
5057 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
5058 if (rbp == *rb_inact) {
5063 error = umtx_handle_rb(td, rbp, &rbp, inact, compat32);
5065 if (i == umtx_max_rb && umtx_verbose_rb) {
5066 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
5067 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
5069 if (error != 0 && umtx_verbose_rb) {
5070 uprintf("comm %s pid %d: handling %srb error %d\n",
5071 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
5076 * Clean up umtx data.
5079 umtx_thread_cleanup(struct thread *td)
5087 * Disown pi mutexes.
5091 if (uq->uq_inherited_pri != PRI_MAX ||
5092 !TAILQ_EMPTY(&uq->uq_pi_contested)) {
5093 mtx_lock(&umtx_lock);
5094 uq->uq_inherited_pri = PRI_MAX;
5095 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
5096 pi->pi_owner = NULL;
5097 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
5099 mtx_unlock(&umtx_lock);
5101 sched_lend_user_prio_cond(td, PRI_MAX);
5104 compat32 = (td->td_pflags2 & TDP2_COMPAT32RB) != 0;
5105 td->td_pflags2 &= ~TDP2_COMPAT32RB;
5107 if (td->td_rb_inact == 0 && td->td_rb_list == 0 && td->td_rbp_list == 0)
5111 * Handle terminated robust mutexes. Must be done after
5112 * robust pi disown, otherwise unlock could see unowned
5115 rb_inact = td->td_rb_inact;
5117 (void)umtx_read_uptr(td, rb_inact, &rb_inact, compat32);
5118 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "", compat32);
5119 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ", compat32);
5121 (void)umtx_handle_rb(td, rb_inact, NULL, true, compat32);