2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2015, 2016 The FreeBSD Foundation
5 * Copyright (c) 2004, David Xu <davidxu@freebsd.org>
6 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
9 * Portions of this software were developed by Konstantin Belousov
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice unmodified, this list of conditions, and the following
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 #include "opt_umtx_profiling.h"
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/fcntl.h>
41 #include <sys/filedesc.h>
42 #include <sys/limits.h>
44 #include <sys/malloc.h>
46 #include <sys/mutex.h>
49 #include <sys/resource.h>
50 #include <sys/resourcevar.h>
51 #include <sys/rwlock.h>
53 #include <sys/sched.h>
55 #include <sys/sysctl.h>
56 #include <sys/systm.h>
57 #include <sys/sysproto.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/taskqueue.h>
61 #include <sys/eventhandler.h>
63 #include <sys/umtxvar.h>
65 #include <security/mac/mac_framework.h>
68 #include <vm/vm_param.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
73 #include <machine/atomic.h>
74 #include <machine/cpu.h>
76 #include <compat/freebsd32/freebsd32.h>
77 #ifdef COMPAT_FREEBSD32
78 #include <compat/freebsd32/freebsd32_proto.h>
82 #define _UMUTEX_WAIT 2
85 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
86 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
89 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
91 #define UMTXQ_ASSERT_LOCKED_BUSY(key) do { \
92 struct umtxq_chain *uc; \
94 uc = umtxq_getchain(key); \
95 mtx_assert(&uc->uc_lock, MA_OWNED); \
96 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy")); \
99 #define UMTXQ_ASSERT_LOCKED_BUSY(key) do {} while (0)
103 * Don't propagate time-sharing priority, there is a security reason,
104 * a user can simply introduce PI-mutex, let thread A lock the mutex,
105 * and let another thread B block on the mutex, because B is
106 * sleeping, its priority will be boosted, this causes A's priority to
107 * be boosted via priority propagating too and will never be lowered even
108 * if it is using 100%CPU, this is unfair to other processes.
111 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
112 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
113 PRI_MAX_TIMESHARE : (td)->td_user_pri)
115 #define GOLDEN_RATIO_PRIME 2654404609U
117 #define UMTX_CHAINS 512
119 #define UMTX_SHIFTS (__WORD_BIT - 9)
121 #define GET_SHARE(flags) \
122 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
124 #define BUSY_SPINS 200
126 struct umtx_copyops {
127 int (*copyin_timeout)(const void *uaddr, struct timespec *tsp);
128 int (*copyin_umtx_time)(const void *uaddr, size_t size,
129 struct _umtx_time *tp);
130 int (*copyin_robust_lists)(const void *uaddr, size_t size,
131 struct umtx_robust_lists_params *rbp);
132 int (*copyout_timeout)(void *uaddr, size_t size,
133 struct timespec *tsp);
134 const size_t timespec_sz;
135 const size_t umtx_time_sz;
139 _Static_assert(sizeof(struct umutex) == sizeof(struct umutex32), "umutex32");
140 _Static_assert(__offsetof(struct umutex, m_spare[0]) ==
141 __offsetof(struct umutex32, m_spare[0]), "m_spare32");
143 int umtx_shm_vnobj_persistent = 0;
144 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_vnode_persistent, CTLFLAG_RWTUN,
145 &umtx_shm_vnobj_persistent, 0,
146 "False forces destruction of umtx attached to file, on last close");
147 static int umtx_max_rb = 1000;
148 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_max_robust, CTLFLAG_RWTUN,
150 "Maximum number of robust mutexes allowed for each thread");
152 static uma_zone_t umtx_pi_zone;
153 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
154 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
155 static int umtx_pi_allocated;
157 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
159 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
160 &umtx_pi_allocated, 0, "Allocated umtx_pi");
161 static int umtx_verbose_rb = 1;
162 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
166 #ifdef UMTX_PROFILING
167 static long max_length;
168 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
169 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
173 static inline void umtx_abs_timeout_init2(struct umtx_abs_timeout *timo,
174 const struct _umtx_time *umtxtime);
176 static void umtx_shm_init(void);
177 static void umtxq_sysinit(void *);
178 static void umtxq_hash(struct umtx_key *key);
179 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
181 static void umtx_thread_cleanup(struct thread *td);
182 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
184 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
186 static struct mtx umtx_lock;
188 #ifdef UMTX_PROFILING
190 umtx_init_profiling(void)
192 struct sysctl_oid *chain_oid;
196 for (i = 0; i < UMTX_CHAINS; ++i) {
197 snprintf(chain_name, sizeof(chain_name), "%d", i);
198 chain_oid = SYSCTL_ADD_NODE(NULL,
199 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
200 chain_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
202 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
203 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
204 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
205 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
210 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
214 struct umtxq_chain *uc;
215 u_int fract, i, j, tot, whole;
216 u_int sf0, sf1, sf2, sf3, sf4;
217 u_int si0, si1, si2, si3, si4;
218 u_int sw0, sw1, sw2, sw3, sw4;
220 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
221 for (i = 0; i < 2; i++) {
223 for (j = 0; j < UMTX_CHAINS; ++j) {
224 uc = &umtxq_chains[i][j];
225 mtx_lock(&uc->uc_lock);
226 tot += uc->max_length;
227 mtx_unlock(&uc->uc_lock);
230 sbuf_printf(&sb, "%u) Empty ", i);
232 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
233 si0 = si1 = si2 = si3 = si4 = 0;
234 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
235 for (j = 0; j < UMTX_CHAINS; j++) {
236 uc = &umtxq_chains[i][j];
237 mtx_lock(&uc->uc_lock);
238 whole = uc->max_length * 100;
239 mtx_unlock(&uc->uc_lock);
240 fract = (whole % tot) * 100;
241 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
245 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
250 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
255 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
260 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
267 sbuf_printf(&sb, "queue %u:\n", i);
268 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
270 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
272 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
274 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
276 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
282 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
288 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
290 struct umtxq_chain *uc;
295 error = sysctl_handle_int(oidp, &clear, 0, req);
296 if (error != 0 || req->newptr == NULL)
300 for (i = 0; i < 2; ++i) {
301 for (j = 0; j < UMTX_CHAINS; ++j) {
302 uc = &umtxq_chains[i][j];
303 mtx_lock(&uc->uc_lock);
306 mtx_unlock(&uc->uc_lock);
313 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
314 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
315 sysctl_debug_umtx_chains_clear, "I",
316 "Clear umtx chains statistics");
317 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
318 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
319 sysctl_debug_umtx_chains_peaks, "A",
320 "Highest peaks in chains max length");
324 umtxq_sysinit(void *arg __unused)
328 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
329 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
330 for (i = 0; i < 2; ++i) {
331 for (j = 0; j < UMTX_CHAINS; ++j) {
332 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
333 MTX_DEF | MTX_DUPOK);
334 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
335 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
336 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
337 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
338 umtxq_chains[i][j].uc_busy = 0;
339 umtxq_chains[i][j].uc_waiters = 0;
340 #ifdef UMTX_PROFILING
341 umtxq_chains[i][j].length = 0;
342 umtxq_chains[i][j].max_length = 0;
346 #ifdef UMTX_PROFILING
347 umtx_init_profiling();
349 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
358 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
359 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
361 TAILQ_INIT(&uq->uq_spare_queue->head);
362 TAILQ_INIT(&uq->uq_pi_contested);
363 uq->uq_inherited_pri = PRI_MAX;
368 umtxq_free(struct umtx_q *uq)
371 MPASS(uq->uq_spare_queue != NULL);
372 free(uq->uq_spare_queue, M_UMTX);
377 umtxq_hash(struct umtx_key *key)
381 n = (uintptr_t)key->info.both.a + key->info.both.b;
382 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
386 umtxq_getchain(struct umtx_key *key)
389 if (key->type <= TYPE_SEM)
390 return (&umtxq_chains[1][key->hash]);
391 return (&umtxq_chains[0][key->hash]);
395 * Set chain to busy state when following operation
396 * may be blocked (kernel mutex can not be used).
399 umtxq_busy(struct umtx_key *key)
401 struct umtxq_chain *uc;
403 uc = umtxq_getchain(key);
404 mtx_assert(&uc->uc_lock, MA_OWNED);
408 int count = BUSY_SPINS;
411 while (uc->uc_busy && --count > 0)
417 while (uc->uc_busy) {
419 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
430 umtxq_unbusy(struct umtx_key *key)
432 struct umtxq_chain *uc;
434 uc = umtxq_getchain(key);
435 mtx_assert(&uc->uc_lock, MA_OWNED);
436 KASSERT(uc->uc_busy != 0, ("not busy"));
443 umtxq_unbusy_unlocked(struct umtx_key *key)
451 static struct umtxq_queue *
452 umtxq_queue_lookup(struct umtx_key *key, int q)
454 struct umtxq_queue *uh;
455 struct umtxq_chain *uc;
457 uc = umtxq_getchain(key);
458 UMTXQ_LOCKED_ASSERT(uc);
459 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
460 if (umtx_key_match(&uh->key, key))
468 umtxq_insert_queue(struct umtx_q *uq, int q)
470 struct umtxq_queue *uh;
471 struct umtxq_chain *uc;
473 uc = umtxq_getchain(&uq->uq_key);
474 UMTXQ_LOCKED_ASSERT(uc);
475 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
476 uh = umtxq_queue_lookup(&uq->uq_key, q);
478 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
480 uh = uq->uq_spare_queue;
481 uh->key = uq->uq_key;
482 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
483 #ifdef UMTX_PROFILING
485 if (uc->length > uc->max_length) {
486 uc->max_length = uc->length;
487 if (uc->max_length > max_length)
488 max_length = uc->max_length;
492 uq->uq_spare_queue = NULL;
494 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
496 uq->uq_flags |= UQF_UMTXQ;
497 uq->uq_cur_queue = uh;
502 umtxq_remove_queue(struct umtx_q *uq, int q)
504 struct umtxq_chain *uc;
505 struct umtxq_queue *uh;
507 uc = umtxq_getchain(&uq->uq_key);
508 UMTXQ_LOCKED_ASSERT(uc);
509 if (uq->uq_flags & UQF_UMTXQ) {
510 uh = uq->uq_cur_queue;
511 TAILQ_REMOVE(&uh->head, uq, uq_link);
513 uq->uq_flags &= ~UQF_UMTXQ;
514 if (TAILQ_EMPTY(&uh->head)) {
515 KASSERT(uh->length == 0,
516 ("inconsistent umtxq_queue length"));
517 #ifdef UMTX_PROFILING
520 LIST_REMOVE(uh, link);
522 uh = LIST_FIRST(&uc->uc_spare_queue);
523 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
524 LIST_REMOVE(uh, link);
526 uq->uq_spare_queue = uh;
527 uq->uq_cur_queue = NULL;
532 * Check if there are multiple waiters
535 umtxq_count(struct umtx_key *key)
537 struct umtxq_queue *uh;
539 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
540 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
547 * Check if there are multiple PI waiters and returns first
551 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
553 struct umtxq_queue *uh;
556 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
557 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
559 *first = TAILQ_FIRST(&uh->head);
566 * Wake up threads waiting on an userland object by a bit mask.
569 umtxq_signal_mask(struct umtx_key *key, int n_wake, u_int bitset)
571 struct umtxq_queue *uh;
572 struct umtx_q *uq, *uq_temp;
576 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
577 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
580 TAILQ_FOREACH_SAFE(uq, &uh->head, uq_link, uq_temp) {
581 if ((uq->uq_bitset & bitset) == 0)
583 umtxq_remove_queue(uq, UMTX_SHARED_QUEUE);
592 * Wake up threads waiting on an userland object.
596 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
598 struct umtxq_queue *uh;
603 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
604 uh = umtxq_queue_lookup(key, q);
606 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
607 umtxq_remove_queue(uq, q);
617 * Wake up specified thread.
620 umtxq_signal_thread(struct umtx_q *uq)
623 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
629 * Wake up a maximum of n_wake threads that are waiting on an userland
630 * object identified by key. The remaining threads are removed from queue
631 * identified by key and added to the queue identified by key2 (requeued).
632 * The n_requeue specifies an upper limit on the number of threads that
633 * are requeued to the second queue.
636 umtxq_requeue(struct umtx_key *key, int n_wake, struct umtx_key *key2,
639 struct umtxq_queue *uh, *uh2;
640 struct umtx_q *uq, *uq_temp;
644 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
645 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key2));
646 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
647 uh2 = umtxq_queue_lookup(key2, UMTX_SHARED_QUEUE);
650 TAILQ_FOREACH_SAFE(uq, &uh->head, uq_link, uq_temp) {
651 if (++ret <= n_wake) {
658 if (ret - n_wake == n_requeue)
666 tstohz(const struct timespec *tsp)
670 TIMESPEC_TO_TIMEVAL(&tv, tsp);
675 umtx_abs_timeout_init(struct umtx_abs_timeout *timo, int clockid,
676 int absolute, const struct timespec *timeout)
679 timo->clockid = clockid;
681 timo->is_abs_real = false;
682 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
683 timespecadd(&timo->cur, timeout, &timo->end);
685 timo->end = *timeout;
686 timo->is_abs_real = clockid == CLOCK_REALTIME ||
687 clockid == CLOCK_REALTIME_FAST ||
688 clockid == CLOCK_REALTIME_PRECISE ||
689 clockid == CLOCK_SECOND;
694 umtx_abs_timeout_init2(struct umtx_abs_timeout *timo,
695 const struct _umtx_time *umtxtime)
698 umtx_abs_timeout_init(timo, umtxtime->_clockid,
699 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
703 umtx_abs_timeout_enforce_min(sbintime_t *sbt)
705 sbintime_t when, mint;
707 mint = curproc->p_umtx_min_timeout;
708 if (__predict_false(mint != 0)) {
709 when = sbinuptime() + mint;
716 umtx_abs_timeout_getsbt(struct umtx_abs_timeout *timo, sbintime_t *sbt,
719 struct bintime bt, bbt;
723 switch (timo->clockid) {
725 /* Clocks that can be converted into absolute time. */
727 case CLOCK_REALTIME_PRECISE:
728 case CLOCK_REALTIME_FAST:
729 case CLOCK_MONOTONIC:
730 case CLOCK_MONOTONIC_PRECISE:
731 case CLOCK_MONOTONIC_FAST:
733 case CLOCK_UPTIME_PRECISE:
734 case CLOCK_UPTIME_FAST:
736 timespec2bintime(&timo->end, &bt);
737 switch (timo->clockid) {
739 case CLOCK_REALTIME_PRECISE:
740 case CLOCK_REALTIME_FAST:
742 getboottimebin(&bbt);
743 bintime_sub(&bt, &bbt);
748 if (bt.sec >= (SBT_MAX >> 32)) {
754 umtx_abs_timeout_enforce_min(sbt);
757 * Check if the absolute time should be aligned to
758 * avoid firing multiple timer events in non-periodic
761 switch (timo->clockid) {
762 case CLOCK_REALTIME_FAST:
763 case CLOCK_MONOTONIC_FAST:
764 case CLOCK_UPTIME_FAST:
765 rem = *sbt % tc_tick_sbt;
766 if (__predict_true(rem != 0))
767 *sbt += tc_tick_sbt - rem;
771 if (__predict_true(rem != 0))
772 *sbt += SBT_1S - rem;
778 /* Clocks that has to be periodically polled. */
781 case CLOCK_THREAD_CPUTIME_ID:
782 case CLOCK_PROCESS_CPUTIME_ID:
784 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
785 if (timespeccmp(&timo->end, &timo->cur, <=))
787 timespecsub(&timo->end, &timo->cur, &tts);
788 *sbt = tick_sbt * tstohz(&tts);
789 *flags = C_HARDCLOCK;
795 umtx_unlock_val(uint32_t flags, bool rb)
799 return (UMUTEX_RB_OWNERDEAD);
800 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
801 return (UMUTEX_RB_NOTRECOV);
803 return (UMUTEX_UNOWNED);
808 * Put thread into sleep state, before sleeping, check if
809 * thread was removed from umtx queue.
812 umtxq_sleep(struct umtx_q *uq, const char *wmesg,
813 struct umtx_abs_timeout *timo)
815 struct umtxq_chain *uc;
817 int error, flags = 0;
819 uc = umtxq_getchain(&uq->uq_key);
820 UMTXQ_LOCKED_ASSERT(uc);
822 if (!(uq->uq_flags & UQF_UMTXQ)) {
827 if (timo->is_abs_real)
828 curthread->td_rtcgen =
829 atomic_load_acq_int(&rtc_generation);
830 error = umtx_abs_timeout_getsbt(timo, &sbt, &flags);
834 error = msleep_sbt(uq, &uc->uc_lock, PCATCH | PDROP, wmesg,
836 uc = umtxq_getchain(&uq->uq_key);
837 mtx_lock(&uc->uc_lock);
838 if (error == EINTR || error == ERESTART)
840 if (error == EWOULDBLOCK && (flags & C_ABSOLUTE) != 0) {
846 curthread->td_rtcgen = 0;
851 * Convert userspace address into unique logical address.
854 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
856 struct thread *td = curthread;
858 vm_map_entry_t entry;
864 if (share == THREAD_SHARE) {
866 key->info.private.vs = td->td_proc->p_vmspace;
867 key->info.private.addr = (uintptr_t)addr;
869 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
870 map = &td->td_proc->p_vmspace->vm_map;
871 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
872 &entry, &key->info.shared.object, &pindex, &prot,
873 &wired) != KERN_SUCCESS) {
877 if ((share == PROCESS_SHARE) ||
878 (share == AUTO_SHARE &&
879 VM_INHERIT_SHARE == entry->inheritance)) {
881 key->info.shared.offset = (vm_offset_t)addr -
882 entry->start + entry->offset;
883 vm_object_reference(key->info.shared.object);
886 key->info.private.vs = td->td_proc->p_vmspace;
887 key->info.private.addr = (uintptr_t)addr;
889 vm_map_lookup_done(map, entry);
900 umtx_key_release(struct umtx_key *key)
903 vm_object_deallocate(key->info.shared.object);
906 #ifdef COMPAT_FREEBSD10
908 * Lock a umtx object.
911 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
912 const struct timespec *timeout)
914 struct umtx_abs_timeout timo;
922 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
925 * Care must be exercised when dealing with umtx structure. It
926 * can fault on any access.
930 * Try the uncontested case. This should be done in userland.
932 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
934 /* The acquire succeeded. */
935 if (owner == UMTX_UNOWNED)
938 /* The address was invalid. */
942 /* If no one owns it but it is contested try to acquire it. */
943 if (owner == UMTX_CONTESTED) {
944 owner = casuword(&umtx->u_owner,
945 UMTX_CONTESTED, id | UMTX_CONTESTED);
947 if (owner == UMTX_CONTESTED)
950 /* The address was invalid. */
954 error = thread_check_susp(td, false);
958 /* If this failed the lock has changed, restart. */
963 * If we caught a signal, we have retried and now
969 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
970 AUTO_SHARE, &uq->uq_key)) != 0)
973 umtxq_lock(&uq->uq_key);
974 umtxq_busy(&uq->uq_key);
976 umtxq_unbusy(&uq->uq_key);
977 umtxq_unlock(&uq->uq_key);
980 * Set the contested bit so that a release in user space
981 * knows to use the system call for unlock. If this fails
982 * either some one else has acquired the lock or it has been
985 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
987 /* The address was invalid. */
989 umtxq_lock(&uq->uq_key);
991 umtxq_unlock(&uq->uq_key);
992 umtx_key_release(&uq->uq_key);
997 * We set the contested bit, sleep. Otherwise the lock changed
998 * and we need to retry or we lost a race to the thread
999 * unlocking the umtx.
1001 umtxq_lock(&uq->uq_key);
1003 error = umtxq_sleep(uq, "umtx", timeout == NULL ? NULL :
1006 umtxq_unlock(&uq->uq_key);
1007 umtx_key_release(&uq->uq_key);
1010 error = thread_check_susp(td, false);
1013 if (timeout == NULL) {
1014 /* Mutex locking is restarted if it is interrupted. */
1018 /* Timed-locking is not restarted. */
1019 if (error == ERESTART)
1026 * Unlock a umtx object.
1029 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
1031 struct umtx_key key;
1038 * Make sure we own this mtx.
1040 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
1044 if ((owner & ~UMTX_CONTESTED) != id)
1047 /* This should be done in userland */
1048 if ((owner & UMTX_CONTESTED) == 0) {
1049 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
1057 /* We should only ever be in here for contested locks */
1058 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1064 count = umtxq_count(&key);
1068 * When unlocking the umtx, it must be marked as unowned if
1069 * there is zero or one thread only waiting for it.
1070 * Otherwise, it must be marked as contested.
1072 old = casuword(&umtx->u_owner, owner,
1073 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
1075 umtxq_signal(&key,1);
1078 umtx_key_release(&key);
1086 #ifdef COMPAT_FREEBSD32
1089 * Lock a umtx object.
1092 do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id,
1093 const struct timespec *timeout)
1095 struct umtx_abs_timeout timo;
1103 if (timeout != NULL)
1104 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
1107 * Care must be exercised when dealing with umtx structure. It
1108 * can fault on any access.
1112 * Try the uncontested case. This should be done in userland.
1114 owner = casuword32(m, UMUTEX_UNOWNED, id);
1116 /* The acquire succeeded. */
1117 if (owner == UMUTEX_UNOWNED)
1120 /* The address was invalid. */
1124 /* If no one owns it but it is contested try to acquire it. */
1125 if (owner == UMUTEX_CONTESTED) {
1126 owner = casuword32(m,
1127 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1128 if (owner == UMUTEX_CONTESTED)
1131 /* The address was invalid. */
1135 error = thread_check_susp(td, false);
1139 /* If this failed the lock has changed, restart. */
1144 * If we caught a signal, we have retried and now
1150 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
1151 AUTO_SHARE, &uq->uq_key)) != 0)
1154 umtxq_lock(&uq->uq_key);
1155 umtxq_busy(&uq->uq_key);
1157 umtxq_unbusy(&uq->uq_key);
1158 umtxq_unlock(&uq->uq_key);
1161 * Set the contested bit so that a release in user space
1162 * knows to use the system call for unlock. If this fails
1163 * either some one else has acquired the lock or it has been
1166 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
1168 /* The address was invalid. */
1170 umtxq_lock(&uq->uq_key);
1172 umtxq_unlock(&uq->uq_key);
1173 umtx_key_release(&uq->uq_key);
1178 * We set the contested bit, sleep. Otherwise the lock changed
1179 * and we need to retry or we lost a race to the thread
1180 * unlocking the umtx.
1182 umtxq_lock(&uq->uq_key);
1184 error = umtxq_sleep(uq, "umtx", timeout == NULL ?
1187 umtxq_unlock(&uq->uq_key);
1188 umtx_key_release(&uq->uq_key);
1191 error = thread_check_susp(td, false);
1194 if (timeout == NULL) {
1195 /* Mutex locking is restarted if it is interrupted. */
1199 /* Timed-locking is not restarted. */
1200 if (error == ERESTART)
1207 * Unlock a umtx object.
1210 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
1212 struct umtx_key key;
1219 * Make sure we own this mtx.
1221 owner = fuword32(m);
1225 if ((owner & ~UMUTEX_CONTESTED) != id)
1228 /* This should be done in userland */
1229 if ((owner & UMUTEX_CONTESTED) == 0) {
1230 old = casuword32(m, owner, UMUTEX_UNOWNED);
1238 /* We should only ever be in here for contested locks */
1239 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1245 count = umtxq_count(&key);
1249 * When unlocking the umtx, it must be marked as unowned if
1250 * there is zero or one thread only waiting for it.
1251 * Otherwise, it must be marked as contested.
1253 old = casuword32(m, owner,
1254 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1256 umtxq_signal(&key,1);
1259 umtx_key_release(&key);
1266 #endif /* COMPAT_FREEBSD32 */
1267 #endif /* COMPAT_FREEBSD10 */
1270 * Fetch and compare value, sleep on the address if value is not changed.
1273 do_wait(struct thread *td, void *addr, u_long id,
1274 struct _umtx_time *timeout, int compat32, int is_private)
1276 struct umtx_abs_timeout timo;
1283 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
1284 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
1287 if (timeout != NULL)
1288 umtx_abs_timeout_init2(&timo, timeout);
1290 umtxq_lock(&uq->uq_key);
1292 umtxq_unlock(&uq->uq_key);
1293 if (compat32 == 0) {
1294 error = fueword(addr, &tmp);
1298 error = fueword32(addr, &tmp32);
1304 umtxq_lock(&uq->uq_key);
1307 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
1309 if ((uq->uq_flags & UQF_UMTXQ) == 0)
1313 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
1316 umtxq_unlock(&uq->uq_key);
1317 umtx_key_release(&uq->uq_key);
1318 if (error == ERESTART)
1324 * Wake up threads sleeping on the specified address.
1327 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1329 struct umtx_key key;
1332 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1333 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1336 umtxq_signal(&key, n_wake);
1338 umtx_key_release(&key);
1343 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1346 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1347 struct _umtx_time *timeout, int mode)
1349 struct umtx_abs_timeout timo;
1351 uint32_t owner, old, id;
1357 if (timeout != NULL)
1358 umtx_abs_timeout_init2(&timo, timeout);
1361 * Care must be exercised when dealing with umtx structure. It
1362 * can fault on any access.
1365 rv = fueword32(&m->m_owner, &owner);
1368 if (mode == _UMUTEX_WAIT) {
1369 if (owner == UMUTEX_UNOWNED ||
1370 owner == UMUTEX_CONTESTED ||
1371 owner == UMUTEX_RB_OWNERDEAD ||
1372 owner == UMUTEX_RB_NOTRECOV)
1376 * Robust mutex terminated. Kernel duty is to
1377 * return EOWNERDEAD to the userspace. The
1378 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1379 * by the common userspace code.
1381 if (owner == UMUTEX_RB_OWNERDEAD) {
1382 rv = casueword32(&m->m_owner,
1383 UMUTEX_RB_OWNERDEAD, &owner,
1384 id | UMUTEX_CONTESTED);
1388 MPASS(owner == UMUTEX_RB_OWNERDEAD);
1389 return (EOWNERDEAD); /* success */
1392 rv = thread_check_susp(td, false);
1397 if (owner == UMUTEX_RB_NOTRECOV)
1398 return (ENOTRECOVERABLE);
1401 * Try the uncontested case. This should be
1404 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1406 /* The address was invalid. */
1410 /* The acquire succeeded. */
1412 MPASS(owner == UMUTEX_UNOWNED);
1417 * If no one owns it but it is contested try
1421 if (owner == UMUTEX_CONTESTED) {
1422 rv = casueword32(&m->m_owner,
1423 UMUTEX_CONTESTED, &owner,
1424 id | UMUTEX_CONTESTED);
1425 /* The address was invalid. */
1429 MPASS(owner == UMUTEX_CONTESTED);
1433 rv = thread_check_susp(td, false);
1439 * If this failed the lock has
1445 /* rv == 1 but not contested, likely store failure */
1446 rv = thread_check_susp(td, false);
1451 if (mode == _UMUTEX_TRY)
1455 * If we caught a signal, we have retried and now
1461 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1462 GET_SHARE(flags), &uq->uq_key)) != 0)
1465 umtxq_lock(&uq->uq_key);
1466 umtxq_busy(&uq->uq_key);
1468 umtxq_unlock(&uq->uq_key);
1471 * Set the contested bit so that a release in user space
1472 * knows to use the system call for unlock. If this fails
1473 * either some one else has acquired the lock or it has been
1476 rv = casueword32(&m->m_owner, owner, &old,
1477 owner | UMUTEX_CONTESTED);
1479 /* The address was invalid or casueword failed to store. */
1480 if (rv == -1 || rv == 1) {
1481 umtxq_lock(&uq->uq_key);
1483 umtxq_unbusy(&uq->uq_key);
1484 umtxq_unlock(&uq->uq_key);
1485 umtx_key_release(&uq->uq_key);
1489 rv = thread_check_susp(td, false);
1497 * We set the contested bit, sleep. Otherwise the lock changed
1498 * and we need to retry or we lost a race to the thread
1499 * unlocking the umtx.
1501 umtxq_lock(&uq->uq_key);
1502 umtxq_unbusy(&uq->uq_key);
1503 MPASS(old == owner);
1504 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1507 umtxq_unlock(&uq->uq_key);
1508 umtx_key_release(&uq->uq_key);
1511 error = thread_check_susp(td, false);
1518 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1521 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1523 struct umtx_key key;
1524 uint32_t owner, old, id, newlock;
1531 * Make sure we own this mtx.
1533 error = fueword32(&m->m_owner, &owner);
1537 if ((owner & ~UMUTEX_CONTESTED) != id)
1540 newlock = umtx_unlock_val(flags, rb);
1541 if ((owner & UMUTEX_CONTESTED) == 0) {
1542 error = casueword32(&m->m_owner, owner, &old, newlock);
1546 error = thread_check_susp(td, false);
1551 MPASS(old == owner);
1555 /* We should only ever be in here for contested locks */
1556 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1562 count = umtxq_count(&key);
1566 * When unlocking the umtx, it must be marked as unowned if
1567 * there is zero or one thread only waiting for it.
1568 * Otherwise, it must be marked as contested.
1571 newlock |= UMUTEX_CONTESTED;
1572 error = casueword32(&m->m_owner, owner, &old, newlock);
1574 umtxq_signal(&key, 1);
1577 umtx_key_release(&key);
1583 error = thread_check_susp(td, false);
1592 * Check if the mutex is available and wake up a waiter,
1593 * only for simple mutex.
1596 do_wake_umutex(struct thread *td, struct umutex *m)
1598 struct umtx_key key;
1605 error = fueword32(&m->m_owner, &owner);
1609 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1610 owner != UMUTEX_RB_NOTRECOV)
1613 error = fueword32(&m->m_flags, &flags);
1617 /* We should only ever be in here for contested locks */
1618 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1624 count = umtxq_count(&key);
1627 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1628 owner != UMUTEX_RB_NOTRECOV) {
1629 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1633 } else if (error == 1) {
1637 umtx_key_release(&key);
1638 error = thread_check_susp(td, false);
1646 if (error == 0 && count != 0) {
1647 MPASS((owner & ~UMUTEX_CONTESTED) == 0 ||
1648 owner == UMUTEX_RB_OWNERDEAD ||
1649 owner == UMUTEX_RB_NOTRECOV);
1650 umtxq_signal(&key, 1);
1654 umtx_key_release(&key);
1659 * Check if the mutex has waiters and tries to fix contention bit.
1662 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1664 struct umtx_key key;
1665 uint32_t owner, old;
1670 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1674 type = TYPE_NORMAL_UMUTEX;
1676 case UMUTEX_PRIO_INHERIT:
1677 type = TYPE_PI_UMUTEX;
1679 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1680 type = TYPE_PI_ROBUST_UMUTEX;
1682 case UMUTEX_PRIO_PROTECT:
1683 type = TYPE_PP_UMUTEX;
1685 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1686 type = TYPE_PP_ROBUST_UMUTEX;
1691 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1697 count = umtxq_count(&key);
1700 error = fueword32(&m->m_owner, &owner);
1705 * Only repair contention bit if there is a waiter, this means
1706 * the mutex is still being referenced by userland code,
1707 * otherwise don't update any memory.
1709 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0 &&
1710 (count > 1 || (count == 1 && (owner & ~UMUTEX_CONTESTED) != 0))) {
1711 error = casueword32(&m->m_owner, owner, &old,
1712 owner | UMUTEX_CONTESTED);
1718 MPASS(old == owner);
1722 error = thread_check_susp(td, false);
1726 if (error == EFAULT) {
1727 umtxq_signal(&key, INT_MAX);
1728 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1729 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1730 umtxq_signal(&key, 1);
1733 umtx_key_release(&key);
1738 umtx_pi_alloc(int flags)
1742 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1743 TAILQ_INIT(&pi->pi_blocked);
1744 atomic_add_int(&umtx_pi_allocated, 1);
1749 umtx_pi_free(struct umtx_pi *pi)
1751 uma_zfree(umtx_pi_zone, pi);
1752 atomic_add_int(&umtx_pi_allocated, -1);
1756 * Adjust the thread's position on a pi_state after its priority has been
1760 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1762 struct umtx_q *uq, *uq1, *uq2;
1765 mtx_assert(&umtx_lock, MA_OWNED);
1772 * Check if the thread needs to be moved on the blocked chain.
1773 * It needs to be moved if either its priority is lower than
1774 * the previous thread or higher than the next thread.
1776 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1777 uq2 = TAILQ_NEXT(uq, uq_lockq);
1778 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1779 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1781 * Remove thread from blocked chain and determine where
1782 * it should be moved to.
1784 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1785 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1786 td1 = uq1->uq_thread;
1787 MPASS(td1->td_proc->p_magic == P_MAGIC);
1788 if (UPRI(td1) > UPRI(td))
1793 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1795 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1800 static struct umtx_pi *
1801 umtx_pi_next(struct umtx_pi *pi)
1803 struct umtx_q *uq_owner;
1805 if (pi->pi_owner == NULL)
1807 uq_owner = pi->pi_owner->td_umtxq;
1808 if (uq_owner == NULL)
1810 return (uq_owner->uq_pi_blocked);
1814 * Floyd's Cycle-Finding Algorithm.
1817 umtx_pi_check_loop(struct umtx_pi *pi)
1819 struct umtx_pi *pi1; /* fast iterator */
1821 mtx_assert(&umtx_lock, MA_OWNED);
1826 pi = umtx_pi_next(pi);
1829 pi1 = umtx_pi_next(pi1);
1832 pi1 = umtx_pi_next(pi1);
1842 * Propagate priority when a thread is blocked on POSIX
1846 umtx_propagate_priority(struct thread *td)
1852 mtx_assert(&umtx_lock, MA_OWNED);
1855 pi = uq->uq_pi_blocked;
1858 if (umtx_pi_check_loop(pi))
1863 if (td == NULL || td == curthread)
1866 MPASS(td->td_proc != NULL);
1867 MPASS(td->td_proc->p_magic == P_MAGIC);
1870 if (td->td_lend_user_pri > pri)
1871 sched_lend_user_prio(td, pri);
1879 * Pick up the lock that td is blocked on.
1882 pi = uq->uq_pi_blocked;
1885 /* Resort td on the list if needed. */
1886 umtx_pi_adjust_thread(pi, td);
1891 * Unpropagate priority for a PI mutex when a thread blocked on
1892 * it is interrupted by signal or resumed by others.
1895 umtx_repropagate_priority(struct umtx_pi *pi)
1897 struct umtx_q *uq, *uq_owner;
1898 struct umtx_pi *pi2;
1901 mtx_assert(&umtx_lock, MA_OWNED);
1903 if (umtx_pi_check_loop(pi))
1905 while (pi != NULL && pi->pi_owner != NULL) {
1907 uq_owner = pi->pi_owner->td_umtxq;
1909 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1910 uq = TAILQ_FIRST(&pi2->pi_blocked);
1912 if (pri > UPRI(uq->uq_thread))
1913 pri = UPRI(uq->uq_thread);
1917 if (pri > uq_owner->uq_inherited_pri)
1918 pri = uq_owner->uq_inherited_pri;
1919 thread_lock(pi->pi_owner);
1920 sched_lend_user_prio(pi->pi_owner, pri);
1921 thread_unlock(pi->pi_owner);
1922 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1923 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1928 * Insert a PI mutex into owned list.
1931 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1933 struct umtx_q *uq_owner;
1935 uq_owner = owner->td_umtxq;
1936 mtx_assert(&umtx_lock, MA_OWNED);
1937 MPASS(pi->pi_owner == NULL);
1938 pi->pi_owner = owner;
1939 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1943 * Disown a PI mutex, and remove it from the owned list.
1946 umtx_pi_disown(struct umtx_pi *pi)
1949 mtx_assert(&umtx_lock, MA_OWNED);
1950 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1951 pi->pi_owner = NULL;
1955 * Claim ownership of a PI mutex.
1958 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1963 mtx_lock(&umtx_lock);
1964 if (pi->pi_owner == owner) {
1965 mtx_unlock(&umtx_lock);
1969 if (pi->pi_owner != NULL) {
1971 * userland may have already messed the mutex, sigh.
1973 mtx_unlock(&umtx_lock);
1976 umtx_pi_setowner(pi, owner);
1977 uq = TAILQ_FIRST(&pi->pi_blocked);
1979 pri = UPRI(uq->uq_thread);
1981 if (pri < UPRI(owner))
1982 sched_lend_user_prio(owner, pri);
1983 thread_unlock(owner);
1985 mtx_unlock(&umtx_lock);
1990 * Adjust a thread's order position in its blocked PI mutex,
1991 * this may result new priority propagating process.
1994 umtx_pi_adjust(struct thread *td, u_char oldpri)
2000 mtx_lock(&umtx_lock);
2002 * Pick up the lock that td is blocked on.
2004 pi = uq->uq_pi_blocked;
2006 umtx_pi_adjust_thread(pi, td);
2007 umtx_repropagate_priority(pi);
2009 mtx_unlock(&umtx_lock);
2013 * Sleep on a PI mutex.
2016 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
2017 const char *wmesg, struct umtx_abs_timeout *timo, bool shared)
2019 struct thread *td, *td1;
2023 struct umtxq_chain *uc;
2025 uc = umtxq_getchain(&pi->pi_key);
2029 KASSERT(td == curthread, ("inconsistent uq_thread"));
2030 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
2031 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
2033 mtx_lock(&umtx_lock);
2034 if (pi->pi_owner == NULL) {
2035 mtx_unlock(&umtx_lock);
2036 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
2037 mtx_lock(&umtx_lock);
2039 if (pi->pi_owner == NULL)
2040 umtx_pi_setowner(pi, td1);
2041 PROC_UNLOCK(td1->td_proc);
2045 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
2046 pri = UPRI(uq1->uq_thread);
2052 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
2054 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
2056 uq->uq_pi_blocked = pi;
2058 td->td_flags |= TDF_UPIBLOCKED;
2060 umtx_propagate_priority(td);
2061 mtx_unlock(&umtx_lock);
2062 umtxq_unbusy(&uq->uq_key);
2064 error = umtxq_sleep(uq, wmesg, timo);
2067 mtx_lock(&umtx_lock);
2068 uq->uq_pi_blocked = NULL;
2070 td->td_flags &= ~TDF_UPIBLOCKED;
2072 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
2073 umtx_repropagate_priority(pi);
2074 mtx_unlock(&umtx_lock);
2075 umtxq_unlock(&uq->uq_key);
2081 * Add reference count for a PI mutex.
2084 umtx_pi_ref(struct umtx_pi *pi)
2087 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
2092 * Decrease reference count for a PI mutex, if the counter
2093 * is decreased to zero, its memory space is freed.
2096 umtx_pi_unref(struct umtx_pi *pi)
2098 struct umtxq_chain *uc;
2100 uc = umtxq_getchain(&pi->pi_key);
2101 UMTXQ_LOCKED_ASSERT(uc);
2102 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
2103 if (--pi->pi_refcount == 0) {
2104 mtx_lock(&umtx_lock);
2105 if (pi->pi_owner != NULL)
2107 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
2108 ("blocked queue not empty"));
2109 mtx_unlock(&umtx_lock);
2110 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
2116 * Find a PI mutex in hash table.
2119 umtx_pi_lookup(struct umtx_key *key)
2121 struct umtxq_chain *uc;
2124 uc = umtxq_getchain(key);
2125 UMTXQ_LOCKED_ASSERT(uc);
2127 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
2128 if (umtx_key_match(&pi->pi_key, key)) {
2136 * Insert a PI mutex into hash table.
2139 umtx_pi_insert(struct umtx_pi *pi)
2141 struct umtxq_chain *uc;
2143 uc = umtxq_getchain(&pi->pi_key);
2144 UMTXQ_LOCKED_ASSERT(uc);
2145 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
2149 * Drop a PI mutex and wakeup a top waiter.
2152 umtx_pi_drop(struct thread *td, struct umtx_key *key, bool rb, int *count)
2154 struct umtx_q *uq_first, *uq_first2, *uq_me;
2155 struct umtx_pi *pi, *pi2;
2158 UMTXQ_ASSERT_LOCKED_BUSY(key);
2159 *count = umtxq_count_pi(key, &uq_first);
2160 if (uq_first != NULL) {
2161 mtx_lock(&umtx_lock);
2162 pi = uq_first->uq_pi_blocked;
2163 KASSERT(pi != NULL, ("pi == NULL?"));
2164 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2165 mtx_unlock(&umtx_lock);
2166 /* userland messed the mutex */
2169 uq_me = td->td_umtxq;
2170 if (pi->pi_owner == td)
2172 /* get highest priority thread which is still sleeping. */
2173 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2174 while (uq_first != NULL &&
2175 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2176 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2179 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2180 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2181 if (uq_first2 != NULL) {
2182 if (pri > UPRI(uq_first2->uq_thread))
2183 pri = UPRI(uq_first2->uq_thread);
2187 sched_lend_user_prio(td, pri);
2189 mtx_unlock(&umtx_lock);
2191 umtxq_signal_thread(uq_first);
2193 pi = umtx_pi_lookup(key);
2195 * A umtx_pi can exist if a signal or timeout removed the
2196 * last waiter from the umtxq, but there is still
2197 * a thread in do_lock_pi() holding the umtx_pi.
2201 * The umtx_pi can be unowned, such as when a thread
2202 * has just entered do_lock_pi(), allocated the
2203 * umtx_pi, and unlocked the umtxq.
2204 * If the current thread owns it, it must disown it.
2206 mtx_lock(&umtx_lock);
2207 if (pi->pi_owner == td)
2209 mtx_unlock(&umtx_lock);
2219 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
2220 struct _umtx_time *timeout, int try)
2222 struct umtx_abs_timeout timo;
2224 struct umtx_pi *pi, *new_pi;
2225 uint32_t id, old_owner, owner, old;
2231 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2232 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2236 if (timeout != NULL)
2237 umtx_abs_timeout_init2(&timo, timeout);
2239 umtxq_lock(&uq->uq_key);
2240 pi = umtx_pi_lookup(&uq->uq_key);
2242 new_pi = umtx_pi_alloc(M_NOWAIT);
2243 if (new_pi == NULL) {
2244 umtxq_unlock(&uq->uq_key);
2245 new_pi = umtx_pi_alloc(M_WAITOK);
2246 umtxq_lock(&uq->uq_key);
2247 pi = umtx_pi_lookup(&uq->uq_key);
2249 umtx_pi_free(new_pi);
2253 if (new_pi != NULL) {
2254 new_pi->pi_key = uq->uq_key;
2255 umtx_pi_insert(new_pi);
2260 umtxq_unlock(&uq->uq_key);
2263 * Care must be exercised when dealing with umtx structure. It
2264 * can fault on any access.
2268 * Try the uncontested case. This should be done in userland.
2270 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
2271 /* The address was invalid. */
2276 /* The acquire succeeded. */
2278 MPASS(owner == UMUTEX_UNOWNED);
2283 if (owner == UMUTEX_RB_NOTRECOV) {
2284 error = ENOTRECOVERABLE;
2289 * Nobody owns it, but the acquire failed. This can happen
2290 * with ll/sc atomics.
2292 if (owner == UMUTEX_UNOWNED) {
2293 error = thread_check_susp(td, true);
2300 * Avoid overwriting a possible error from sleep due
2301 * to the pending signal with suspension check result.
2304 error = thread_check_susp(td, true);
2309 /* If no one owns it but it is contested try to acquire it. */
2310 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
2312 rv = casueword32(&m->m_owner, owner, &owner,
2313 id | UMUTEX_CONTESTED);
2314 /* The address was invalid. */
2321 error = thread_check_susp(td, true);
2327 * If this failed the lock could
2334 MPASS(owner == old_owner);
2335 umtxq_lock(&uq->uq_key);
2336 umtxq_busy(&uq->uq_key);
2337 error = umtx_pi_claim(pi, td);
2338 umtxq_unbusy(&uq->uq_key);
2339 umtxq_unlock(&uq->uq_key);
2342 * Since we're going to return an
2343 * error, restore the m_owner to its
2344 * previous, unowned state to avoid
2345 * compounding the problem.
2347 (void)casuword32(&m->m_owner,
2348 id | UMUTEX_CONTESTED, old_owner);
2350 if (error == 0 && old_owner == UMUTEX_RB_OWNERDEAD)
2355 if ((owner & ~UMUTEX_CONTESTED) == id) {
2366 * If we caught a signal, we have retried and now
2372 umtxq_lock(&uq->uq_key);
2373 umtxq_busy(&uq->uq_key);
2374 umtxq_unlock(&uq->uq_key);
2377 * Set the contested bit so that a release in user space
2378 * knows to use the system call for unlock. If this fails
2379 * either some one else has acquired the lock or it has been
2382 rv = casueword32(&m->m_owner, owner, &old, owner |
2385 /* The address was invalid. */
2387 umtxq_unbusy_unlocked(&uq->uq_key);
2392 umtxq_unbusy_unlocked(&uq->uq_key);
2393 error = thread_check_susp(td, true);
2398 * The lock changed and we need to retry or we
2399 * lost a race to the thread unlocking the
2400 * umtx. Note that the UMUTEX_RB_OWNERDEAD
2401 * value for owner is impossible there.
2406 umtxq_lock(&uq->uq_key);
2408 /* We set the contested bit, sleep. */
2409 MPASS(old == owner);
2410 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
2411 "umtxpi", timeout == NULL ? NULL : &timo,
2412 (flags & USYNC_PROCESS_SHARED) != 0);
2416 error = thread_check_susp(td, false);
2421 umtxq_lock(&uq->uq_key);
2423 umtxq_unlock(&uq->uq_key);
2425 umtx_key_release(&uq->uq_key);
2430 * Unlock a PI mutex.
2433 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2435 struct umtx_key key;
2436 uint32_t id, new_owner, old, owner;
2443 * Make sure we own this mtx.
2445 error = fueword32(&m->m_owner, &owner);
2449 if ((owner & ~UMUTEX_CONTESTED) != id)
2452 new_owner = umtx_unlock_val(flags, rb);
2454 /* This should be done in userland */
2455 if ((owner & UMUTEX_CONTESTED) == 0) {
2456 error = casueword32(&m->m_owner, owner, &old, new_owner);
2460 error = thread_check_susp(td, true);
2470 /* We should only ever be in here for contested locks */
2471 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2472 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2478 error = umtx_pi_drop(td, &key, rb, &count);
2482 umtx_key_release(&key);
2483 /* userland messed the mutex */
2489 * When unlocking the umtx, it must be marked as unowned if
2490 * there is zero or one thread only waiting for it.
2491 * Otherwise, it must be marked as contested.
2495 new_owner |= UMUTEX_CONTESTED;
2497 error = casueword32(&m->m_owner, owner, &old, new_owner);
2499 error = thread_check_susp(td, false);
2503 umtxq_unbusy_unlocked(&key);
2504 umtx_key_release(&key);
2507 if (error == 0 && old != owner)
2516 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2517 struct _umtx_time *timeout, int try)
2519 struct umtx_abs_timeout timo;
2520 struct umtx_q *uq, *uq2;
2524 int error, pri, old_inherited_pri, su, rv;
2528 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2529 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2533 if (timeout != NULL)
2534 umtx_abs_timeout_init2(&timo, timeout);
2536 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2538 old_inherited_pri = uq->uq_inherited_pri;
2539 umtxq_lock(&uq->uq_key);
2540 umtxq_busy(&uq->uq_key);
2541 umtxq_unlock(&uq->uq_key);
2543 rv = fueword32(&m->m_ceilings[0], &ceiling);
2548 ceiling = RTP_PRIO_MAX - ceiling;
2549 if (ceiling > RTP_PRIO_MAX) {
2554 mtx_lock(&umtx_lock);
2555 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2556 mtx_unlock(&umtx_lock);
2560 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2561 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2563 if (uq->uq_inherited_pri < UPRI(td))
2564 sched_lend_user_prio(td, uq->uq_inherited_pri);
2567 mtx_unlock(&umtx_lock);
2569 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2570 id | UMUTEX_CONTESTED);
2571 /* The address was invalid. */
2577 MPASS(owner == UMUTEX_CONTESTED);
2582 if (owner == UMUTEX_RB_OWNERDEAD) {
2583 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2584 &owner, id | UMUTEX_CONTESTED);
2590 MPASS(owner == UMUTEX_RB_OWNERDEAD);
2591 error = EOWNERDEAD; /* success */
2596 * rv == 1, only check for suspension if we
2597 * did not already catched a signal. If we
2598 * get an error from the check, the same
2599 * condition is checked by the umtxq_sleep()
2600 * call below, so we should obliterate the
2601 * error to not skip the last loop iteration.
2604 error = thread_check_susp(td, false);
2613 } else if (owner == UMUTEX_RB_NOTRECOV) {
2614 error = ENOTRECOVERABLE;
2621 * If we caught a signal, we have retried and now
2627 umtxq_lock(&uq->uq_key);
2629 umtxq_unbusy(&uq->uq_key);
2630 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2633 umtxq_unlock(&uq->uq_key);
2635 mtx_lock(&umtx_lock);
2636 uq->uq_inherited_pri = old_inherited_pri;
2638 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2639 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2641 if (pri > UPRI(uq2->uq_thread))
2642 pri = UPRI(uq2->uq_thread);
2645 if (pri > uq->uq_inherited_pri)
2646 pri = uq->uq_inherited_pri;
2648 sched_lend_user_prio(td, pri);
2650 mtx_unlock(&umtx_lock);
2653 if (error != 0 && error != EOWNERDEAD) {
2654 mtx_lock(&umtx_lock);
2655 uq->uq_inherited_pri = old_inherited_pri;
2657 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2658 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2660 if (pri > UPRI(uq2->uq_thread))
2661 pri = UPRI(uq2->uq_thread);
2664 if (pri > uq->uq_inherited_pri)
2665 pri = uq->uq_inherited_pri;
2667 sched_lend_user_prio(td, pri);
2669 mtx_unlock(&umtx_lock);
2673 umtxq_unbusy_unlocked(&uq->uq_key);
2674 umtx_key_release(&uq->uq_key);
2679 * Unlock a PP mutex.
2682 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2684 struct umtx_key key;
2685 struct umtx_q *uq, *uq2;
2687 uint32_t id, owner, rceiling;
2688 int error, pri, new_inherited_pri, su;
2692 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2695 * Make sure we own this mtx.
2697 error = fueword32(&m->m_owner, &owner);
2701 if ((owner & ~UMUTEX_CONTESTED) != id)
2704 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2709 new_inherited_pri = PRI_MAX;
2711 rceiling = RTP_PRIO_MAX - rceiling;
2712 if (rceiling > RTP_PRIO_MAX)
2714 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2717 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2718 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2725 * For priority protected mutex, always set unlocked state
2726 * to UMUTEX_CONTESTED, so that userland always enters kernel
2727 * to lock the mutex, it is necessary because thread priority
2728 * has to be adjusted for such mutex.
2730 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2735 umtxq_signal(&key, 1);
2742 mtx_lock(&umtx_lock);
2744 uq->uq_inherited_pri = new_inherited_pri;
2746 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2747 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2749 if (pri > UPRI(uq2->uq_thread))
2750 pri = UPRI(uq2->uq_thread);
2753 if (pri > uq->uq_inherited_pri)
2754 pri = uq->uq_inherited_pri;
2756 sched_lend_user_prio(td, pri);
2758 mtx_unlock(&umtx_lock);
2760 umtx_key_release(&key);
2765 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2766 uint32_t *old_ceiling)
2769 uint32_t flags, id, owner, save_ceiling;
2772 error = fueword32(&m->m_flags, &flags);
2775 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2777 if (ceiling > RTP_PRIO_MAX)
2781 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2782 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2786 umtxq_lock(&uq->uq_key);
2787 umtxq_busy(&uq->uq_key);
2788 umtxq_unlock(&uq->uq_key);
2790 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2796 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2797 id | UMUTEX_CONTESTED);
2804 MPASS(owner == UMUTEX_CONTESTED);
2805 rv = suword32(&m->m_ceilings[0], ceiling);
2806 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2807 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2811 if ((owner & ~UMUTEX_CONTESTED) == id) {
2812 rv = suword32(&m->m_ceilings[0], ceiling);
2813 error = rv == 0 ? 0 : EFAULT;
2817 if (owner == UMUTEX_RB_OWNERDEAD) {
2820 } else if (owner == UMUTEX_RB_NOTRECOV) {
2821 error = ENOTRECOVERABLE;
2826 * If we caught a signal, we have retried and now
2833 * We set the contested bit, sleep. Otherwise the lock changed
2834 * and we need to retry or we lost a race to the thread
2835 * unlocking the umtx.
2837 umtxq_lock(&uq->uq_key);
2839 umtxq_unbusy(&uq->uq_key);
2840 error = umtxq_sleep(uq, "umtxpp", NULL);
2842 umtxq_unlock(&uq->uq_key);
2844 umtxq_lock(&uq->uq_key);
2846 umtxq_signal(&uq->uq_key, INT_MAX);
2847 umtxq_unbusy(&uq->uq_key);
2848 umtxq_unlock(&uq->uq_key);
2849 umtx_key_release(&uq->uq_key);
2850 if (error == 0 && old_ceiling != NULL) {
2851 rv = suword32(old_ceiling, save_ceiling);
2852 error = rv == 0 ? 0 : EFAULT;
2858 * Lock a userland POSIX mutex.
2861 do_lock_umutex(struct thread *td, struct umutex *m,
2862 struct _umtx_time *timeout, int mode)
2867 error = fueword32(&m->m_flags, &flags);
2871 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2873 error = do_lock_normal(td, m, flags, timeout, mode);
2875 case UMUTEX_PRIO_INHERIT:
2876 error = do_lock_pi(td, m, flags, timeout, mode);
2878 case UMUTEX_PRIO_PROTECT:
2879 error = do_lock_pp(td, m, flags, timeout, mode);
2884 if (timeout == NULL) {
2885 if (error == EINTR && mode != _UMUTEX_WAIT)
2888 /* Timed-locking is not restarted. */
2889 if (error == ERESTART)
2896 * Unlock a userland POSIX mutex.
2899 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2904 error = fueword32(&m->m_flags, &flags);
2908 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2910 return (do_unlock_normal(td, m, flags, rb));
2911 case UMUTEX_PRIO_INHERIT:
2912 return (do_unlock_pi(td, m, flags, rb));
2913 case UMUTEX_PRIO_PROTECT:
2914 return (do_unlock_pp(td, m, flags, rb));
2921 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2922 struct timespec *timeout, u_long wflags)
2924 struct umtx_abs_timeout timo;
2926 uint32_t flags, clockid, hasw;
2930 error = fueword32(&cv->c_flags, &flags);
2933 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2937 if ((wflags & CVWAIT_CLOCKID) != 0) {
2938 error = fueword32(&cv->c_clockid, &clockid);
2940 umtx_key_release(&uq->uq_key);
2943 if (clockid < CLOCK_REALTIME ||
2944 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2945 /* hmm, only HW clock id will work. */
2946 umtx_key_release(&uq->uq_key);
2950 clockid = CLOCK_REALTIME;
2953 umtxq_lock(&uq->uq_key);
2954 umtxq_busy(&uq->uq_key);
2956 umtxq_unlock(&uq->uq_key);
2959 * Set c_has_waiters to 1 before releasing user mutex, also
2960 * don't modify cache line when unnecessary.
2962 error = fueword32(&cv->c_has_waiters, &hasw);
2963 if (error == 0 && hasw == 0)
2964 error = suword32(&cv->c_has_waiters, 1);
2966 umtxq_lock(&uq->uq_key);
2968 umtxq_unbusy(&uq->uq_key);
2973 umtxq_unbusy_unlocked(&uq->uq_key);
2975 error = do_unlock_umutex(td, m, false);
2977 if (timeout != NULL)
2978 umtx_abs_timeout_init(&timo, clockid,
2979 (wflags & CVWAIT_ABSTIME) != 0, timeout);
2981 umtxq_lock(&uq->uq_key);
2983 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2987 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2991 * This must be timeout,interrupted by signal or
2992 * surprious wakeup, clear c_has_waiter flag when
2995 umtxq_busy(&uq->uq_key);
2996 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2997 int oldlen = uq->uq_cur_queue->length;
3000 umtxq_unlock(&uq->uq_key);
3001 if (suword32(&cv->c_has_waiters, 0) != 0 &&
3004 umtxq_lock(&uq->uq_key);
3007 umtxq_unbusy(&uq->uq_key);
3008 if (error == ERESTART)
3012 umtxq_unlock(&uq->uq_key);
3013 umtx_key_release(&uq->uq_key);
3018 * Signal a userland condition variable.
3021 do_cv_signal(struct thread *td, struct ucond *cv)
3023 struct umtx_key key;
3024 int error, cnt, nwake;
3027 error = fueword32(&cv->c_flags, &flags);
3030 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
3034 cnt = umtxq_count(&key);
3035 nwake = umtxq_signal(&key, 1);
3038 error = suword32(&cv->c_has_waiters, 0);
3045 umtx_key_release(&key);
3050 do_cv_broadcast(struct thread *td, struct ucond *cv)
3052 struct umtx_key key;
3056 error = fueword32(&cv->c_flags, &flags);
3059 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
3064 umtxq_signal(&key, INT_MAX);
3067 error = suword32(&cv->c_has_waiters, 0);
3071 umtxq_unbusy_unlocked(&key);
3073 umtx_key_release(&key);
3078 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
3079 struct _umtx_time *timeout)
3081 struct umtx_abs_timeout timo;
3083 uint32_t flags, wrflags;
3084 int32_t state, oldstate;
3085 int32_t blocked_readers;
3086 int error, error1, rv;
3089 error = fueword32(&rwlock->rw_flags, &flags);
3092 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3096 if (timeout != NULL)
3097 umtx_abs_timeout_init2(&timo, timeout);
3099 wrflags = URWLOCK_WRITE_OWNER;
3100 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
3101 wrflags |= URWLOCK_WRITE_WAITERS;
3104 rv = fueword32(&rwlock->rw_state, &state);
3106 umtx_key_release(&uq->uq_key);
3110 /* try to lock it */
3111 while (!(state & wrflags)) {
3112 if (__predict_false(URWLOCK_READER_COUNT(state) ==
3113 URWLOCK_MAX_READERS)) {
3114 umtx_key_release(&uq->uq_key);
3117 rv = casueword32(&rwlock->rw_state, state,
3118 &oldstate, state + 1);
3120 umtx_key_release(&uq->uq_key);
3124 MPASS(oldstate == state);
3125 umtx_key_release(&uq->uq_key);
3128 error = thread_check_susp(td, true);
3137 /* grab monitor lock */
3138 umtxq_lock(&uq->uq_key);
3139 umtxq_busy(&uq->uq_key);
3140 umtxq_unlock(&uq->uq_key);
3143 * re-read the state, in case it changed between the try-lock above
3144 * and the check below
3146 rv = fueword32(&rwlock->rw_state, &state);
3150 /* set read contention bit */
3151 while (error == 0 && (state & wrflags) &&
3152 !(state & URWLOCK_READ_WAITERS)) {
3153 rv = casueword32(&rwlock->rw_state, state,
3154 &oldstate, state | URWLOCK_READ_WAITERS);
3160 MPASS(oldstate == state);
3164 error = thread_check_susp(td, false);
3169 umtxq_unbusy_unlocked(&uq->uq_key);
3173 /* state is changed while setting flags, restart */
3174 if (!(state & wrflags)) {
3175 umtxq_unbusy_unlocked(&uq->uq_key);
3176 error = thread_check_susp(td, true);
3184 * Contention bit is set, before sleeping, increase
3185 * read waiter count.
3187 rv = fueword32(&rwlock->rw_blocked_readers,
3190 rv = suword32(&rwlock->rw_blocked_readers,
3191 blocked_readers + 1);
3193 umtxq_unbusy_unlocked(&uq->uq_key);
3198 while (state & wrflags) {
3199 umtxq_lock(&uq->uq_key);
3201 umtxq_unbusy(&uq->uq_key);
3203 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
3206 umtxq_busy(&uq->uq_key);
3208 umtxq_unlock(&uq->uq_key);
3211 rv = fueword32(&rwlock->rw_state, &state);
3218 /* decrease read waiter count, and may clear read contention bit */
3219 rv = fueword32(&rwlock->rw_blocked_readers,
3222 rv = suword32(&rwlock->rw_blocked_readers,
3223 blocked_readers - 1);
3225 umtxq_unbusy_unlocked(&uq->uq_key);
3229 if (blocked_readers == 1) {
3230 rv = fueword32(&rwlock->rw_state, &state);
3232 umtxq_unbusy_unlocked(&uq->uq_key);
3237 rv = casueword32(&rwlock->rw_state, state,
3238 &oldstate, state & ~URWLOCK_READ_WAITERS);
3244 MPASS(oldstate == state);
3248 error1 = thread_check_susp(td, false);
3257 umtxq_unbusy_unlocked(&uq->uq_key);
3261 umtx_key_release(&uq->uq_key);
3262 if (error == ERESTART)
3268 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
3270 struct umtx_abs_timeout timo;
3273 int32_t state, oldstate;
3274 int32_t blocked_writers;
3275 int32_t blocked_readers;
3276 int error, error1, rv;
3279 error = fueword32(&rwlock->rw_flags, &flags);
3282 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3286 if (timeout != NULL)
3287 umtx_abs_timeout_init2(&timo, timeout);
3289 blocked_readers = 0;
3291 rv = fueword32(&rwlock->rw_state, &state);
3293 umtx_key_release(&uq->uq_key);
3296 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
3297 URWLOCK_READER_COUNT(state) == 0) {
3298 rv = casueword32(&rwlock->rw_state, state,
3299 &oldstate, state | URWLOCK_WRITE_OWNER);
3301 umtx_key_release(&uq->uq_key);
3305 MPASS(oldstate == state);
3306 umtx_key_release(&uq->uq_key);
3310 error = thread_check_susp(td, true);
3316 if ((state & (URWLOCK_WRITE_OWNER |
3317 URWLOCK_WRITE_WAITERS)) == 0 &&
3318 blocked_readers != 0) {
3319 umtxq_lock(&uq->uq_key);
3320 umtxq_busy(&uq->uq_key);
3321 umtxq_signal_queue(&uq->uq_key, INT_MAX,
3323 umtxq_unbusy(&uq->uq_key);
3324 umtxq_unlock(&uq->uq_key);
3330 /* grab monitor lock */
3331 umtxq_lock(&uq->uq_key);
3332 umtxq_busy(&uq->uq_key);
3333 umtxq_unlock(&uq->uq_key);
3336 * Re-read the state, in case it changed between the
3337 * try-lock above and the check below.
3339 rv = fueword32(&rwlock->rw_state, &state);
3343 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
3344 URWLOCK_READER_COUNT(state) != 0) &&
3345 (state & URWLOCK_WRITE_WAITERS) == 0) {
3346 rv = casueword32(&rwlock->rw_state, state,
3347 &oldstate, state | URWLOCK_WRITE_WAITERS);
3353 MPASS(oldstate == state);
3357 error = thread_check_susp(td, false);
3362 umtxq_unbusy_unlocked(&uq->uq_key);
3366 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
3367 URWLOCK_READER_COUNT(state) == 0) {
3368 umtxq_unbusy_unlocked(&uq->uq_key);
3369 error = thread_check_susp(td, false);
3375 rv = fueword32(&rwlock->rw_blocked_writers,
3378 rv = suword32(&rwlock->rw_blocked_writers,
3379 blocked_writers + 1);
3381 umtxq_unbusy_unlocked(&uq->uq_key);
3386 while ((state & URWLOCK_WRITE_OWNER) ||
3387 URWLOCK_READER_COUNT(state) != 0) {
3388 umtxq_lock(&uq->uq_key);
3389 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3390 umtxq_unbusy(&uq->uq_key);
3392 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
3395 umtxq_busy(&uq->uq_key);
3396 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3397 umtxq_unlock(&uq->uq_key);
3400 rv = fueword32(&rwlock->rw_state, &state);
3407 rv = fueword32(&rwlock->rw_blocked_writers,
3410 rv = suword32(&rwlock->rw_blocked_writers,
3411 blocked_writers - 1);
3413 umtxq_unbusy_unlocked(&uq->uq_key);
3417 if (blocked_writers == 1) {
3418 rv = fueword32(&rwlock->rw_state, &state);
3420 umtxq_unbusy_unlocked(&uq->uq_key);
3425 rv = casueword32(&rwlock->rw_state, state,
3426 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3432 MPASS(oldstate == state);
3436 error1 = thread_check_susp(td, false);
3438 * We are leaving the URWLOCK_WRITE_WAITERS
3439 * behind, but this should not harm the
3448 rv = fueword32(&rwlock->rw_blocked_readers,
3451 umtxq_unbusy_unlocked(&uq->uq_key);
3456 blocked_readers = 0;
3458 umtxq_unbusy_unlocked(&uq->uq_key);
3461 umtx_key_release(&uq->uq_key);
3462 if (error == ERESTART)
3468 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3472 int32_t state, oldstate;
3473 int error, rv, q, count;
3476 error = fueword32(&rwlock->rw_flags, &flags);
3479 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3483 error = fueword32(&rwlock->rw_state, &state);
3488 if (state & URWLOCK_WRITE_OWNER) {
3490 rv = casueword32(&rwlock->rw_state, state,
3491 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3498 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3502 error = thread_check_susp(td, true);
3508 } else if (URWLOCK_READER_COUNT(state) != 0) {
3510 rv = casueword32(&rwlock->rw_state, state,
3511 &oldstate, state - 1);
3518 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3522 error = thread_check_susp(td, true);
3535 if (!(flags & URWLOCK_PREFER_READER)) {
3536 if (state & URWLOCK_WRITE_WAITERS) {
3538 q = UMTX_EXCLUSIVE_QUEUE;
3539 } else if (state & URWLOCK_READ_WAITERS) {
3541 q = UMTX_SHARED_QUEUE;
3544 if (state & URWLOCK_READ_WAITERS) {
3546 q = UMTX_SHARED_QUEUE;
3547 } else if (state & URWLOCK_WRITE_WAITERS) {
3549 q = UMTX_EXCLUSIVE_QUEUE;
3554 umtxq_lock(&uq->uq_key);
3555 umtxq_busy(&uq->uq_key);
3556 umtxq_signal_queue(&uq->uq_key, count, q);
3557 umtxq_unbusy(&uq->uq_key);
3558 umtxq_unlock(&uq->uq_key);
3561 umtx_key_release(&uq->uq_key);
3565 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3567 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3569 struct umtx_abs_timeout timo;
3571 uint32_t flags, count, count1;
3575 error = fueword32(&sem->_flags, &flags);
3578 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3582 if (timeout != NULL)
3583 umtx_abs_timeout_init2(&timo, timeout);
3586 umtxq_lock(&uq->uq_key);
3587 umtxq_busy(&uq->uq_key);
3589 umtxq_unlock(&uq->uq_key);
3590 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3592 rv1 = fueword32(&sem->_count, &count);
3593 if (rv == -1 || rv1 == -1 || count != 0 || (rv == 1 && count1 == 0)) {
3595 rv = suword32(&sem->_has_waiters, 0);
3596 umtxq_lock(&uq->uq_key);
3597 umtxq_unbusy(&uq->uq_key);
3599 umtxq_unlock(&uq->uq_key);
3600 if (rv == -1 || rv1 == -1) {
3608 MPASS(rv == 1 && count1 == 0);
3609 rv = thread_check_susp(td, true);
3615 umtxq_lock(&uq->uq_key);
3616 umtxq_unbusy(&uq->uq_key);
3618 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3620 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3624 /* A relative timeout cannot be restarted. */
3625 if (error == ERESTART && timeout != NULL &&
3626 (timeout->_flags & UMTX_ABSTIME) == 0)
3629 umtxq_unlock(&uq->uq_key);
3631 umtx_key_release(&uq->uq_key);
3636 * Signal a userland semaphore.
3639 do_sem_wake(struct thread *td, struct _usem *sem)
3641 struct umtx_key key;
3645 error = fueword32(&sem->_flags, &flags);
3648 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3652 cnt = umtxq_count(&key);
3655 * Check if count is greater than 0, this means the memory is
3656 * still being referenced by user code, so we can safely
3657 * update _has_waiters flag.
3661 error = suword32(&sem->_has_waiters, 0);
3666 umtxq_signal(&key, 1);
3670 umtx_key_release(&key);
3676 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3678 struct umtx_abs_timeout timo;
3680 uint32_t count, flags;
3684 flags = fuword32(&sem->_flags);
3685 if (timeout != NULL)
3686 umtx_abs_timeout_init2(&timo, timeout);
3689 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3692 umtxq_lock(&uq->uq_key);
3693 umtxq_busy(&uq->uq_key);
3695 umtxq_unlock(&uq->uq_key);
3696 rv = fueword32(&sem->_count, &count);
3698 umtxq_lock(&uq->uq_key);
3699 umtxq_unbusy(&uq->uq_key);
3701 umtxq_unlock(&uq->uq_key);
3702 umtx_key_release(&uq->uq_key);
3706 if (USEM_COUNT(count) != 0) {
3707 umtxq_lock(&uq->uq_key);
3708 umtxq_unbusy(&uq->uq_key);
3710 umtxq_unlock(&uq->uq_key);
3711 umtx_key_release(&uq->uq_key);
3714 if (count == USEM_HAS_WAITERS)
3716 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3719 umtxq_lock(&uq->uq_key);
3720 umtxq_unbusy(&uq->uq_key);
3722 umtxq_unlock(&uq->uq_key);
3723 umtx_key_release(&uq->uq_key);
3726 rv = thread_check_susp(td, true);
3731 umtxq_lock(&uq->uq_key);
3732 umtxq_unbusy(&uq->uq_key);
3734 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3736 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3740 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3741 /* A relative timeout cannot be restarted. */
3742 if (error == ERESTART)
3744 if (error == EINTR) {
3745 kern_clock_gettime(curthread, timo.clockid,
3747 timespecsub(&timo.end, &timo.cur,
3748 &timeout->_timeout);
3752 umtxq_unlock(&uq->uq_key);
3753 umtx_key_release(&uq->uq_key);
3758 * Signal a userland semaphore.
3761 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3763 struct umtx_key key;
3765 uint32_t count, flags;
3767 rv = fueword32(&sem->_flags, &flags);
3770 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3774 cnt = umtxq_count(&key);
3777 * If this was the last sleeping thread, clear the waiters
3782 rv = fueword32(&sem->_count, &count);
3783 while (rv != -1 && count & USEM_HAS_WAITERS) {
3784 rv = casueword32(&sem->_count, count, &count,
3785 count & ~USEM_HAS_WAITERS);
3787 rv = thread_check_susp(td, true);
3800 umtxq_signal(&key, 1);
3804 umtx_key_release(&key);
3808 #ifdef COMPAT_FREEBSD10
3810 freebsd10__umtx_lock(struct thread *td, struct freebsd10__umtx_lock_args *uap)
3812 return (do_lock_umtx(td, uap->umtx, td->td_tid, 0));
3816 freebsd10__umtx_unlock(struct thread *td,
3817 struct freebsd10__umtx_unlock_args *uap)
3819 return (do_unlock_umtx(td, uap->umtx, td->td_tid));
3824 umtx_copyin_timeout(const void *uaddr, struct timespec *tsp)
3828 error = copyin(uaddr, tsp, sizeof(*tsp));
3830 if (!timespecvalid_interval(tsp))
3837 umtx_copyin_umtx_time(const void *uaddr, size_t size, struct _umtx_time *tp)
3841 if (size <= sizeof(tp->_timeout)) {
3842 tp->_clockid = CLOCK_REALTIME;
3844 error = copyin(uaddr, &tp->_timeout, sizeof(tp->_timeout));
3846 error = copyin(uaddr, tp, sizeof(*tp));
3849 if (!timespecvalid_interval(&tp->_timeout))
3855 umtx_copyin_robust_lists(const void *uaddr, size_t size,
3856 struct umtx_robust_lists_params *rb)
3859 if (size > sizeof(*rb))
3861 return (copyin(uaddr, rb, size));
3865 umtx_copyout_timeout(void *uaddr, size_t sz, struct timespec *tsp)
3869 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
3870 * and we're only called if sz >= sizeof(timespec) as supplied in the
3873 KASSERT(sz >= sizeof(*tsp),
3874 ("umtx_copyops specifies incorrect sizes"));
3876 return (copyout(tsp, uaddr, sizeof(*tsp)));
3879 #ifdef COMPAT_FREEBSD10
3881 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap,
3882 const struct umtx_copyops *ops)
3884 struct timespec *ts, timeout;
3887 /* Allow a null timespec (wait forever). */
3888 if (uap->uaddr2 == NULL)
3891 error = ops->copyin_timeout(uap->uaddr2, &timeout);
3896 #ifdef COMPAT_FREEBSD32
3898 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
3900 return (do_lock_umtx(td, uap->obj, uap->val, ts));
3904 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap,
3905 const struct umtx_copyops *ops)
3907 #ifdef COMPAT_FREEBSD32
3909 return (do_unlock_umtx32(td, uap->obj, uap->val));
3911 return (do_unlock_umtx(td, uap->obj, uap->val));
3913 #endif /* COMPAT_FREEBSD10 */
3915 #if !defined(COMPAT_FREEBSD10)
3917 __umtx_op_unimpl(struct thread *td __unused, struct _umtx_op_args *uap __unused,
3918 const struct umtx_copyops *ops __unused)
3920 return (EOPNOTSUPP);
3922 #endif /* COMPAT_FREEBSD10 */
3925 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap,
3926 const struct umtx_copyops *ops)
3928 struct _umtx_time timeout, *tm_p;
3931 if (uap->uaddr2 == NULL)
3934 error = ops->copyin_umtx_time(
3935 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3940 return (do_wait(td, uap->obj, uap->val, tm_p, ops->compat32, 0));
3944 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap,
3945 const struct umtx_copyops *ops)
3947 struct _umtx_time timeout, *tm_p;
3950 if (uap->uaddr2 == NULL)
3953 error = ops->copyin_umtx_time(
3954 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3959 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3963 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap,
3964 const struct umtx_copyops *ops)
3966 struct _umtx_time *tm_p, timeout;
3969 if (uap->uaddr2 == NULL)
3972 error = ops->copyin_umtx_time(
3973 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3978 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3982 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap,
3983 const struct umtx_copyops *ops __unused)
3986 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3989 #define BATCH_SIZE 128
3991 __umtx_op_nwake_private_native(struct thread *td, struct _umtx_op_args *uap)
3993 char *uaddrs[BATCH_SIZE], **upp;
3994 int count, error, i, pos, tocopy;
3996 upp = (char **)uap->obj;
3998 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
4000 tocopy = MIN(count, BATCH_SIZE);
4001 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
4004 for (i = 0; i < tocopy; ++i) {
4005 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
4013 __umtx_op_nwake_private_compat32(struct thread *td, struct _umtx_op_args *uap)
4015 uint32_t uaddrs[BATCH_SIZE], *upp;
4016 int count, error, i, pos, tocopy;
4018 upp = (uint32_t *)uap->obj;
4020 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
4022 tocopy = MIN(count, BATCH_SIZE);
4023 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
4026 for (i = 0; i < tocopy; ++i) {
4027 kern_umtx_wake(td, (void *)(uintptr_t)uaddrs[i],
4036 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap,
4037 const struct umtx_copyops *ops)
4041 return (__umtx_op_nwake_private_compat32(td, uap));
4042 return (__umtx_op_nwake_private_native(td, uap));
4046 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap,
4047 const struct umtx_copyops *ops __unused)
4050 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
4054 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap,
4055 const struct umtx_copyops *ops)
4057 struct _umtx_time *tm_p, timeout;
4060 /* Allow a null timespec (wait forever). */
4061 if (uap->uaddr2 == NULL)
4064 error = ops->copyin_umtx_time(
4065 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4070 return (do_lock_umutex(td, uap->obj, tm_p, 0));
4074 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap,
4075 const struct umtx_copyops *ops __unused)
4078 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
4082 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap,
4083 const struct umtx_copyops *ops)
4085 struct _umtx_time *tm_p, timeout;
4088 /* Allow a null timespec (wait forever). */
4089 if (uap->uaddr2 == NULL)
4092 error = ops->copyin_umtx_time(
4093 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4098 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
4102 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap,
4103 const struct umtx_copyops *ops __unused)
4106 return (do_wake_umutex(td, uap->obj));
4110 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap,
4111 const struct umtx_copyops *ops __unused)
4114 return (do_unlock_umutex(td, uap->obj, false));
4118 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap,
4119 const struct umtx_copyops *ops __unused)
4122 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
4126 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap,
4127 const struct umtx_copyops *ops)
4129 struct timespec *ts, timeout;
4132 /* Allow a null timespec (wait forever). */
4133 if (uap->uaddr2 == NULL)
4136 error = ops->copyin_timeout(uap->uaddr2, &timeout);
4141 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
4145 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap,
4146 const struct umtx_copyops *ops __unused)
4149 return (do_cv_signal(td, uap->obj));
4153 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap,
4154 const struct umtx_copyops *ops __unused)
4157 return (do_cv_broadcast(td, uap->obj));
4161 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap,
4162 const struct umtx_copyops *ops)
4164 struct _umtx_time timeout;
4167 /* Allow a null timespec (wait forever). */
4168 if (uap->uaddr2 == NULL) {
4169 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4171 error = ops->copyin_umtx_time(uap->uaddr2,
4172 (size_t)uap->uaddr1, &timeout);
4175 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4181 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap,
4182 const struct umtx_copyops *ops)
4184 struct _umtx_time timeout;
4187 /* Allow a null timespec (wait forever). */
4188 if (uap->uaddr2 == NULL) {
4189 error = do_rw_wrlock(td, uap->obj, 0);
4191 error = ops->copyin_umtx_time(uap->uaddr2,
4192 (size_t)uap->uaddr1, &timeout);
4196 error = do_rw_wrlock(td, uap->obj, &timeout);
4202 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap,
4203 const struct umtx_copyops *ops __unused)
4206 return (do_rw_unlock(td, uap->obj));
4209 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4211 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap,
4212 const struct umtx_copyops *ops)
4214 struct _umtx_time *tm_p, timeout;
4217 /* Allow a null timespec (wait forever). */
4218 if (uap->uaddr2 == NULL)
4221 error = ops->copyin_umtx_time(
4222 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4227 return (do_sem_wait(td, uap->obj, tm_p));
4231 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap,
4232 const struct umtx_copyops *ops __unused)
4235 return (do_sem_wake(td, uap->obj));
4240 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap,
4241 const struct umtx_copyops *ops __unused)
4244 return (do_wake2_umutex(td, uap->obj, uap->val));
4248 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap,
4249 const struct umtx_copyops *ops)
4251 struct _umtx_time *tm_p, timeout;
4255 /* Allow a null timespec (wait forever). */
4256 if (uap->uaddr2 == NULL) {
4260 uasize = (size_t)uap->uaddr1;
4261 error = ops->copyin_umtx_time(uap->uaddr2, uasize, &timeout);
4266 error = do_sem2_wait(td, uap->obj, tm_p);
4267 if (error == EINTR && uap->uaddr2 != NULL &&
4268 (timeout._flags & UMTX_ABSTIME) == 0 &&
4269 uasize >= ops->umtx_time_sz + ops->timespec_sz) {
4270 error = ops->copyout_timeout(
4271 (void *)((uintptr_t)uap->uaddr2 + ops->umtx_time_sz),
4272 uasize - ops->umtx_time_sz, &timeout._timeout);
4282 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap,
4283 const struct umtx_copyops *ops __unused)
4286 return (do_sem2_wake(td, uap->obj));
4289 #define USHM_OBJ_UMTX(o) \
4290 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
4292 #define USHMF_REG_LINKED 0x0001
4293 #define USHMF_OBJ_LINKED 0x0002
4294 struct umtx_shm_reg {
4295 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
4296 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
4297 struct umtx_key ushm_key;
4298 struct ucred *ushm_cred;
4299 struct shmfd *ushm_obj;
4304 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
4305 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
4307 static uma_zone_t umtx_shm_reg_zone;
4308 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
4309 static struct mtx umtx_shm_lock;
4310 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
4311 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
4313 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
4316 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
4318 struct umtx_shm_reg_head d;
4319 struct umtx_shm_reg *reg, *reg1;
4322 mtx_lock(&umtx_shm_lock);
4323 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
4324 mtx_unlock(&umtx_shm_lock);
4325 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
4326 TAILQ_REMOVE(&d, reg, ushm_reg_link);
4327 umtx_shm_free_reg(reg);
4331 static struct task umtx_shm_reg_delfree_task =
4332 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
4334 static struct umtx_shm_reg *
4335 umtx_shm_find_reg_locked(const struct umtx_key *key)
4337 struct umtx_shm_reg *reg;
4338 struct umtx_shm_reg_head *reg_head;
4340 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
4341 mtx_assert(&umtx_shm_lock, MA_OWNED);
4342 reg_head = &umtx_shm_registry[key->hash];
4343 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
4344 KASSERT(reg->ushm_key.shared,
4345 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
4346 if (reg->ushm_key.info.shared.object ==
4347 key->info.shared.object &&
4348 reg->ushm_key.info.shared.offset ==
4349 key->info.shared.offset) {
4350 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
4351 KASSERT(reg->ushm_refcnt > 0,
4352 ("reg %p refcnt 0 onlist", reg));
4353 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
4354 ("reg %p not linked", reg));
4362 static struct umtx_shm_reg *
4363 umtx_shm_find_reg(const struct umtx_key *key)
4365 struct umtx_shm_reg *reg;
4367 mtx_lock(&umtx_shm_lock);
4368 reg = umtx_shm_find_reg_locked(key);
4369 mtx_unlock(&umtx_shm_lock);
4374 umtx_shm_free_reg(struct umtx_shm_reg *reg)
4377 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
4378 crfree(reg->ushm_cred);
4379 shm_drop(reg->ushm_obj);
4380 uma_zfree(umtx_shm_reg_zone, reg);
4384 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
4388 mtx_assert(&umtx_shm_lock, MA_OWNED);
4389 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
4391 res = reg->ushm_refcnt == 0;
4393 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
4394 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
4395 reg, ushm_reg_link);
4396 reg->ushm_flags &= ~USHMF_REG_LINKED;
4398 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
4399 LIST_REMOVE(reg, ushm_obj_link);
4400 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
4407 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
4413 object = reg->ushm_obj->shm_object;
4414 VM_OBJECT_WLOCK(object);
4415 vm_object_set_flag(object, OBJ_UMTXDEAD);
4416 VM_OBJECT_WUNLOCK(object);
4418 mtx_lock(&umtx_shm_lock);
4419 dofree = umtx_shm_unref_reg_locked(reg, force);
4420 mtx_unlock(&umtx_shm_lock);
4422 umtx_shm_free_reg(reg);
4426 umtx_shm_object_init(vm_object_t object)
4429 LIST_INIT(USHM_OBJ_UMTX(object));
4433 umtx_shm_object_terminated(vm_object_t object)
4435 struct umtx_shm_reg *reg, *reg1;
4438 if (LIST_EMPTY(USHM_OBJ_UMTX(object)))
4442 mtx_lock(&umtx_shm_lock);
4443 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
4444 if (umtx_shm_unref_reg_locked(reg, true)) {
4445 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
4450 mtx_unlock(&umtx_shm_lock);
4452 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
4456 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
4457 struct umtx_shm_reg **res)
4459 struct umtx_shm_reg *reg, *reg1;
4463 reg = umtx_shm_find_reg(key);
4468 cred = td->td_ucred;
4469 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
4471 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
4472 reg->ushm_refcnt = 1;
4473 bcopy(key, ®->ushm_key, sizeof(*key));
4474 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR, false);
4475 reg->ushm_cred = crhold(cred);
4476 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
4478 umtx_shm_free_reg(reg);
4481 mtx_lock(&umtx_shm_lock);
4482 reg1 = umtx_shm_find_reg_locked(key);
4484 mtx_unlock(&umtx_shm_lock);
4485 umtx_shm_free_reg(reg);
4490 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
4491 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
4493 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
4494 mtx_unlock(&umtx_shm_lock);
4500 umtx_shm_alive(struct thread *td, void *addr)
4503 vm_map_entry_t entry;
4510 map = &td->td_proc->p_vmspace->vm_map;
4511 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
4512 &object, &pindex, &prot, &wired);
4513 if (res != KERN_SUCCESS)
4518 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
4519 vm_map_lookup_done(map, entry);
4528 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
4529 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
4530 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
4531 for (i = 0; i < nitems(umtx_shm_registry); i++)
4532 TAILQ_INIT(&umtx_shm_registry[i]);
4536 umtx_shm(struct thread *td, void *addr, u_int flags)
4538 struct umtx_key key;
4539 struct umtx_shm_reg *reg;
4543 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
4544 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
4546 if ((flags & UMTX_SHM_ALIVE) != 0)
4547 return (umtx_shm_alive(td, addr));
4548 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
4551 KASSERT(key.shared == 1, ("non-shared key"));
4552 if ((flags & UMTX_SHM_CREAT) != 0) {
4553 error = umtx_shm_create_reg(td, &key, ®);
4555 reg = umtx_shm_find_reg(&key);
4559 umtx_key_release(&key);
4562 KASSERT(reg != NULL, ("no reg"));
4563 if ((flags & UMTX_SHM_DESTROY) != 0) {
4564 umtx_shm_unref_reg(reg, true);
4568 error = mac_posixshm_check_open(td->td_ucred,
4569 reg->ushm_obj, FFLAGS(O_RDWR));
4572 error = shm_access(reg->ushm_obj, td->td_ucred,
4576 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
4578 shm_hold(reg->ushm_obj);
4579 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
4581 td->td_retval[0] = fd;
4585 umtx_shm_unref_reg(reg, false);
4590 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap,
4591 const struct umtx_copyops *ops __unused)
4594 return (umtx_shm(td, uap->uaddr1, uap->val));
4598 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap,
4599 const struct umtx_copyops *ops)
4601 struct umtx_robust_lists_params rb;
4604 if (ops->compat32) {
4605 if ((td->td_pflags2 & TDP2_COMPAT32RB) == 0 &&
4606 (td->td_rb_list != 0 || td->td_rbp_list != 0 ||
4607 td->td_rb_inact != 0))
4609 } else if ((td->td_pflags2 & TDP2_COMPAT32RB) != 0) {
4613 bzero(&rb, sizeof(rb));
4614 error = ops->copyin_robust_lists(uap->uaddr1, uap->val, &rb);
4619 td->td_pflags2 |= TDP2_COMPAT32RB;
4621 td->td_rb_list = rb.robust_list_offset;
4622 td->td_rbp_list = rb.robust_priv_list_offset;
4623 td->td_rb_inact = rb.robust_inact_offset;
4628 __umtx_op_get_min_timeout(struct thread *td, struct _umtx_op_args *uap,
4629 const struct umtx_copyops *ops)
4634 val = sbttons(td->td_proc->p_umtx_min_timeout);
4635 if (ops->compat32) {
4637 error = copyout(&val1, uap->uaddr1, sizeof(val1));
4639 error = copyout(&val, uap->uaddr1, sizeof(val));
4645 __umtx_op_set_min_timeout(struct thread *td, struct _umtx_op_args *uap,
4646 const struct umtx_copyops *ops)
4650 td->td_proc->p_umtx_min_timeout = nstosbt(uap->val);
4654 #if defined(__i386__) || defined(__amd64__)
4656 * Provide the standard 32-bit definitions for x86, since native/compat32 use a
4657 * 32-bit time_t there. Other architectures just need the i386 definitions
4658 * along with their standard compat32.
4660 struct timespecx32 {
4665 struct umtx_timex32 {
4666 struct timespecx32 _timeout;
4672 #define timespeci386 timespec32
4673 #define umtx_timei386 umtx_time32
4675 #else /* !__i386__ && !__amd64__ */
4676 /* 32-bit architectures can emulate i386, so define these almost everywhere. */
4677 struct timespeci386 {
4682 struct umtx_timei386 {
4683 struct timespeci386 _timeout;
4688 #if defined(__LP64__)
4689 #define timespecx32 timespec32
4690 #define umtx_timex32 umtx_time32
4695 umtx_copyin_robust_lists32(const void *uaddr, size_t size,
4696 struct umtx_robust_lists_params *rbp)
4698 struct umtx_robust_lists_params_compat32 rb32;
4701 if (size > sizeof(rb32))
4703 bzero(&rb32, sizeof(rb32));
4704 error = copyin(uaddr, &rb32, size);
4707 CP(rb32, *rbp, robust_list_offset);
4708 CP(rb32, *rbp, robust_priv_list_offset);
4709 CP(rb32, *rbp, robust_inact_offset);
4715 umtx_copyin_timeouti386(const void *uaddr, struct timespec *tsp)
4717 struct timespeci386 ts32;
4720 error = copyin(uaddr, &ts32, sizeof(ts32));
4722 if (!timespecvalid_interval(&ts32))
4725 CP(ts32, *tsp, tv_sec);
4726 CP(ts32, *tsp, tv_nsec);
4733 umtx_copyin_umtx_timei386(const void *uaddr, size_t size, struct _umtx_time *tp)
4735 struct umtx_timei386 t32;
4738 t32._clockid = CLOCK_REALTIME;
4740 if (size <= sizeof(t32._timeout))
4741 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4743 error = copyin(uaddr, &t32, sizeof(t32));
4746 if (!timespecvalid_interval(&t32._timeout))
4748 TS_CP(t32, *tp, _timeout);
4749 CP(t32, *tp, _flags);
4750 CP(t32, *tp, _clockid);
4755 umtx_copyout_timeouti386(void *uaddr, size_t sz, struct timespec *tsp)
4757 struct timespeci386 remain32 = {
4758 .tv_sec = tsp->tv_sec,
4759 .tv_nsec = tsp->tv_nsec,
4763 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4764 * and we're only called if sz >= sizeof(timespec) as supplied in the
4767 KASSERT(sz >= sizeof(remain32),
4768 ("umtx_copyops specifies incorrect sizes"));
4770 return (copyout(&remain32, uaddr, sizeof(remain32)));
4772 #endif /* !__i386__ */
4774 #if defined(__i386__) || defined(__LP64__)
4776 umtx_copyin_timeoutx32(const void *uaddr, struct timespec *tsp)
4778 struct timespecx32 ts32;
4781 error = copyin(uaddr, &ts32, sizeof(ts32));
4783 if (!timespecvalid_interval(&ts32))
4786 CP(ts32, *tsp, tv_sec);
4787 CP(ts32, *tsp, tv_nsec);
4794 umtx_copyin_umtx_timex32(const void *uaddr, size_t size, struct _umtx_time *tp)
4796 struct umtx_timex32 t32;
4799 t32._clockid = CLOCK_REALTIME;
4801 if (size <= sizeof(t32._timeout))
4802 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4804 error = copyin(uaddr, &t32, sizeof(t32));
4807 if (!timespecvalid_interval(&t32._timeout))
4809 TS_CP(t32, *tp, _timeout);
4810 CP(t32, *tp, _flags);
4811 CP(t32, *tp, _clockid);
4816 umtx_copyout_timeoutx32(void *uaddr, size_t sz, struct timespec *tsp)
4818 struct timespecx32 remain32 = {
4819 .tv_sec = tsp->tv_sec,
4820 .tv_nsec = tsp->tv_nsec,
4824 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4825 * and we're only called if sz >= sizeof(timespec) as supplied in the
4828 KASSERT(sz >= sizeof(remain32),
4829 ("umtx_copyops specifies incorrect sizes"));
4831 return (copyout(&remain32, uaddr, sizeof(remain32)));
4833 #endif /* __i386__ || __LP64__ */
4835 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap,
4836 const struct umtx_copyops *umtx_ops);
4838 static const _umtx_op_func op_table[] = {
4839 #ifdef COMPAT_FREEBSD10
4840 [UMTX_OP_LOCK] = __umtx_op_lock_umtx,
4841 [UMTX_OP_UNLOCK] = __umtx_op_unlock_umtx,
4843 [UMTX_OP_LOCK] = __umtx_op_unimpl,
4844 [UMTX_OP_UNLOCK] = __umtx_op_unimpl,
4846 [UMTX_OP_WAIT] = __umtx_op_wait,
4847 [UMTX_OP_WAKE] = __umtx_op_wake,
4848 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4849 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
4850 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4851 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4852 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
4853 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4854 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4855 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
4856 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
4857 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
4858 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4859 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4860 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4861 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4862 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4863 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4864 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4865 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4867 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4868 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4870 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4871 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4872 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4873 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4874 [UMTX_OP_SHM] = __umtx_op_shm,
4875 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4876 [UMTX_OP_GET_MIN_TIMEOUT] = __umtx_op_get_min_timeout,
4877 [UMTX_OP_SET_MIN_TIMEOUT] = __umtx_op_set_min_timeout,
4880 static const struct umtx_copyops umtx_native_ops = {
4881 .copyin_timeout = umtx_copyin_timeout,
4882 .copyin_umtx_time = umtx_copyin_umtx_time,
4883 .copyin_robust_lists = umtx_copyin_robust_lists,
4884 .copyout_timeout = umtx_copyout_timeout,
4885 .timespec_sz = sizeof(struct timespec),
4886 .umtx_time_sz = sizeof(struct _umtx_time),
4890 static const struct umtx_copyops umtx_native_opsi386 = {
4891 .copyin_timeout = umtx_copyin_timeouti386,
4892 .copyin_umtx_time = umtx_copyin_umtx_timei386,
4893 .copyin_robust_lists = umtx_copyin_robust_lists32,
4894 .copyout_timeout = umtx_copyout_timeouti386,
4895 .timespec_sz = sizeof(struct timespeci386),
4896 .umtx_time_sz = sizeof(struct umtx_timei386),
4901 #if defined(__i386__) || defined(__LP64__)
4902 /* i386 can emulate other 32-bit archs, too! */
4903 static const struct umtx_copyops umtx_native_opsx32 = {
4904 .copyin_timeout = umtx_copyin_timeoutx32,
4905 .copyin_umtx_time = umtx_copyin_umtx_timex32,
4906 .copyin_robust_lists = umtx_copyin_robust_lists32,
4907 .copyout_timeout = umtx_copyout_timeoutx32,
4908 .timespec_sz = sizeof(struct timespecx32),
4909 .umtx_time_sz = sizeof(struct umtx_timex32),
4913 #ifdef COMPAT_FREEBSD32
4915 #define umtx_native_ops32 umtx_native_opsi386
4917 #define umtx_native_ops32 umtx_native_opsx32
4919 #endif /* COMPAT_FREEBSD32 */
4920 #endif /* __i386__ || __LP64__ */
4922 #define UMTX_OP__FLAGS (UMTX_OP__32BIT | UMTX_OP__I386)
4925 kern__umtx_op(struct thread *td, void *obj, int op, unsigned long val,
4926 void *uaddr1, void *uaddr2, const struct umtx_copyops *ops)
4928 struct _umtx_op_args uap = {
4930 .op = op & ~UMTX_OP__FLAGS,
4936 if ((uap.op >= nitems(op_table)))
4938 return ((*op_table[uap.op])(td, &uap, ops));
4942 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4944 static const struct umtx_copyops *umtx_ops;
4946 umtx_ops = &umtx_native_ops;
4948 if ((uap->op & (UMTX_OP__32BIT | UMTX_OP__I386)) != 0) {
4949 if ((uap->op & UMTX_OP__I386) != 0)
4950 umtx_ops = &umtx_native_opsi386;
4952 umtx_ops = &umtx_native_opsx32;
4954 #elif !defined(__i386__)
4955 /* We consider UMTX_OP__32BIT a nop on !i386 ILP32. */
4956 if ((uap->op & UMTX_OP__I386) != 0)
4957 umtx_ops = &umtx_native_opsi386;
4959 /* Likewise, UMTX_OP__I386 is a nop on i386. */
4960 if ((uap->op & UMTX_OP__32BIT) != 0)
4961 umtx_ops = &umtx_native_opsx32;
4963 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr1,
4964 uap->uaddr2, umtx_ops));
4967 #ifdef COMPAT_FREEBSD32
4968 #ifdef COMPAT_FREEBSD10
4970 freebsd10_freebsd32_umtx_lock(struct thread *td,
4971 struct freebsd10_freebsd32_umtx_lock_args *uap)
4973 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
4977 freebsd10_freebsd32_umtx_unlock(struct thread *td,
4978 struct freebsd10_freebsd32_umtx_unlock_args *uap)
4980 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
4982 #endif /* COMPAT_FREEBSD10 */
4985 freebsd32__umtx_op(struct thread *td, struct freebsd32__umtx_op_args *uap)
4988 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr,
4989 uap->uaddr2, &umtx_native_ops32));
4991 #endif /* COMPAT_FREEBSD32 */
4994 umtx_thread_init(struct thread *td)
4997 td->td_umtxq = umtxq_alloc();
4998 td->td_umtxq->uq_thread = td;
5002 umtx_thread_fini(struct thread *td)
5005 umtxq_free(td->td_umtxq);
5009 * It will be called when new thread is created, e.g fork().
5012 umtx_thread_alloc(struct thread *td)
5017 uq->uq_inherited_pri = PRI_MAX;
5019 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
5020 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
5021 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
5022 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
5028 * Clear robust lists for all process' threads, not delaying the
5029 * cleanup to thread exit, since the relevant address space is
5030 * destroyed right now.
5033 umtx_exec(struct proc *p)
5037 KASSERT(p == curproc, ("need curproc"));
5038 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
5039 (p->p_flag & P_STOPPED_SINGLE) != 0,
5040 ("curproc must be single-threaded"));
5042 * There is no need to lock the list as only this thread can be
5045 FOREACH_THREAD_IN_PROC(p, td) {
5046 KASSERT(td == curthread ||
5047 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
5048 ("running thread %p %p", p, td));
5049 umtx_thread_cleanup(td);
5050 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
5053 p->p_umtx_min_timeout = 0;
5060 umtx_thread_exit(struct thread *td)
5063 umtx_thread_cleanup(td);
5067 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res, bool compat32)
5074 error = fueword32((void *)ptr, &res32);
5078 error = fueword((void *)ptr, &res1);
5088 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list,
5091 struct umutex32 m32;
5094 memcpy(&m32, m, sizeof(m32));
5095 *rb_list = m32.m_rb_lnk;
5097 *rb_list = m->m_rb_lnk;
5102 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact,
5108 KASSERT(td->td_proc == curproc, ("need current vmspace"));
5109 error = copyin((void *)rbp, &m, sizeof(m));
5112 if (rb_list != NULL)
5113 umtx_read_rb_list(td, &m, rb_list, compat32);
5114 if ((m.m_flags & UMUTEX_ROBUST) == 0)
5116 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
5117 /* inact is cleared after unlock, allow the inconsistency */
5118 return (inact ? 0 : EINVAL);
5119 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
5123 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
5124 const char *name, bool compat32)
5132 error = umtx_read_uptr(td, rb_list, &rbp, compat32);
5133 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
5134 if (rbp == *rb_inact) {
5139 error = umtx_handle_rb(td, rbp, &rbp, inact, compat32);
5141 if (i == umtx_max_rb && umtx_verbose_rb) {
5142 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
5143 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
5145 if (error != 0 && umtx_verbose_rb) {
5146 uprintf("comm %s pid %d: handling %srb error %d\n",
5147 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
5152 * Clean up umtx data.
5155 umtx_thread_cleanup(struct thread *td)
5163 * Disown pi mutexes.
5167 if (uq->uq_inherited_pri != PRI_MAX ||
5168 !TAILQ_EMPTY(&uq->uq_pi_contested)) {
5169 mtx_lock(&umtx_lock);
5170 uq->uq_inherited_pri = PRI_MAX;
5171 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
5172 pi->pi_owner = NULL;
5173 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
5175 mtx_unlock(&umtx_lock);
5177 sched_lend_user_prio_cond(td, PRI_MAX);
5180 compat32 = (td->td_pflags2 & TDP2_COMPAT32RB) != 0;
5181 td->td_pflags2 &= ~TDP2_COMPAT32RB;
5183 if (td->td_rb_inact == 0 && td->td_rb_list == 0 && td->td_rbp_list == 0)
5187 * Handle terminated robust mutexes. Must be done after
5188 * robust pi disown, otherwise unlock could see unowned
5191 rb_inact = td->td_rb_inact;
5193 (void)umtx_read_uptr(td, rb_inact, &rb_inact, compat32);
5194 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "", compat32);
5195 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ", compat32);
5197 (void)umtx_handle_rb(td, rb_inact, NULL, true, compat32);