2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015, 2016 The FreeBSD Foundation
5 * Copyright (c) 2004, David Xu <davidxu@freebsd.org>
6 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
9 * Portions of this software were developed by Konstantin Belousov
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice unmodified, this list of conditions, and the following
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_umtx_profiling.h"
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
43 #include <sys/filedesc.h>
44 #include <sys/limits.h>
46 #include <sys/malloc.h>
48 #include <sys/mutex.h>
51 #include <sys/resource.h>
52 #include <sys/resourcevar.h>
53 #include <sys/rwlock.h>
55 #include <sys/sched.h>
57 #include <sys/sysctl.h>
58 #include <sys/sysent.h>
59 #include <sys/systm.h>
60 #include <sys/sysproto.h>
61 #include <sys/syscallsubr.h>
62 #include <sys/taskqueue.h>
64 #include <sys/eventhandler.h>
66 #include <sys/umtxvar.h>
68 #include <security/mac/mac_framework.h>
71 #include <vm/vm_param.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_object.h>
76 #include <machine/atomic.h>
77 #include <machine/cpu.h>
79 #include <compat/freebsd32/freebsd32.h>
80 #ifdef COMPAT_FREEBSD32
81 #include <compat/freebsd32/freebsd32_proto.h>
85 #define _UMUTEX_WAIT 2
88 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
89 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
92 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
94 #define UMTXQ_ASSERT_LOCKED_BUSY(key) do { \
95 struct umtxq_chain *uc; \
97 uc = umtxq_getchain(key); \
98 mtx_assert(&uc->uc_lock, MA_OWNED); \
99 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy")); \
102 #define UMTXQ_ASSERT_LOCKED_BUSY(key) do {} while (0)
106 * Don't propagate time-sharing priority, there is a security reason,
107 * a user can simply introduce PI-mutex, let thread A lock the mutex,
108 * and let another thread B block on the mutex, because B is
109 * sleeping, its priority will be boosted, this causes A's priority to
110 * be boosted via priority propagating too and will never be lowered even
111 * if it is using 100%CPU, this is unfair to other processes.
114 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
115 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
116 PRI_MAX_TIMESHARE : (td)->td_user_pri)
118 #define GOLDEN_RATIO_PRIME 2654404609U
120 #define UMTX_CHAINS 512
122 #define UMTX_SHIFTS (__WORD_BIT - 9)
124 #define GET_SHARE(flags) \
125 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
127 #define BUSY_SPINS 200
129 struct umtx_copyops {
130 int (*copyin_timeout)(const void *uaddr, struct timespec *tsp);
131 int (*copyin_umtx_time)(const void *uaddr, size_t size,
132 struct _umtx_time *tp);
133 int (*copyin_robust_lists)(const void *uaddr, size_t size,
134 struct umtx_robust_lists_params *rbp);
135 int (*copyout_timeout)(void *uaddr, size_t size,
136 struct timespec *tsp);
137 const size_t timespec_sz;
138 const size_t umtx_time_sz;
142 _Static_assert(sizeof(struct umutex) == sizeof(struct umutex32), "umutex32");
143 _Static_assert(__offsetof(struct umutex, m_spare[0]) ==
144 __offsetof(struct umutex32, m_spare[0]), "m_spare32");
146 int umtx_shm_vnobj_persistent = 0;
147 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_vnode_persistent, CTLFLAG_RWTUN,
148 &umtx_shm_vnobj_persistent, 0,
149 "False forces destruction of umtx attached to file, on last close");
150 static int umtx_max_rb = 1000;
151 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_max_robust, CTLFLAG_RWTUN,
153 "Maximum number of robust mutexes allowed for each thread");
155 static uma_zone_t umtx_pi_zone;
156 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
157 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
158 static int umtx_pi_allocated;
160 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
162 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
163 &umtx_pi_allocated, 0, "Allocated umtx_pi");
164 static int umtx_verbose_rb = 1;
165 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
169 #ifdef UMTX_PROFILING
170 static long max_length;
171 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
172 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
176 static inline void umtx_abs_timeout_init2(struct umtx_abs_timeout *timo,
177 const struct _umtx_time *umtxtime);
178 static int umtx_abs_timeout_gethz(struct umtx_abs_timeout *timo);
179 static inline void umtx_abs_timeout_update(struct umtx_abs_timeout *timo);
181 static void umtx_shm_init(void);
182 static void umtxq_sysinit(void *);
183 static void umtxq_hash(struct umtx_key *key);
184 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
186 static void umtx_thread_cleanup(struct thread *td);
187 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
189 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
191 static struct mtx umtx_lock;
193 #ifdef UMTX_PROFILING
195 umtx_init_profiling(void)
197 struct sysctl_oid *chain_oid;
201 for (i = 0; i < UMTX_CHAINS; ++i) {
202 snprintf(chain_name, sizeof(chain_name), "%d", i);
203 chain_oid = SYSCTL_ADD_NODE(NULL,
204 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
205 chain_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
207 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
208 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
209 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
210 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
215 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
219 struct umtxq_chain *uc;
220 u_int fract, i, j, tot, whole;
221 u_int sf0, sf1, sf2, sf3, sf4;
222 u_int si0, si1, si2, si3, si4;
223 u_int sw0, sw1, sw2, sw3, sw4;
225 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
226 for (i = 0; i < 2; i++) {
228 for (j = 0; j < UMTX_CHAINS; ++j) {
229 uc = &umtxq_chains[i][j];
230 mtx_lock(&uc->uc_lock);
231 tot += uc->max_length;
232 mtx_unlock(&uc->uc_lock);
235 sbuf_printf(&sb, "%u) Empty ", i);
237 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
238 si0 = si1 = si2 = si3 = si4 = 0;
239 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
240 for (j = 0; j < UMTX_CHAINS; j++) {
241 uc = &umtxq_chains[i][j];
242 mtx_lock(&uc->uc_lock);
243 whole = uc->max_length * 100;
244 mtx_unlock(&uc->uc_lock);
245 fract = (whole % tot) * 100;
246 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
250 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
255 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
260 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
265 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
272 sbuf_printf(&sb, "queue %u:\n", i);
273 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
275 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
277 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
279 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
281 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
287 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
293 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
295 struct umtxq_chain *uc;
300 error = sysctl_handle_int(oidp, &clear, 0, req);
301 if (error != 0 || req->newptr == NULL)
305 for (i = 0; i < 2; ++i) {
306 for (j = 0; j < UMTX_CHAINS; ++j) {
307 uc = &umtxq_chains[i][j];
308 mtx_lock(&uc->uc_lock);
311 mtx_unlock(&uc->uc_lock);
318 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
319 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
320 sysctl_debug_umtx_chains_clear, "I",
321 "Clear umtx chains statistics");
322 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
323 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
324 sysctl_debug_umtx_chains_peaks, "A",
325 "Highest peaks in chains max length");
329 umtxq_sysinit(void *arg __unused)
333 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
334 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
335 for (i = 0; i < 2; ++i) {
336 for (j = 0; j < UMTX_CHAINS; ++j) {
337 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
338 MTX_DEF | MTX_DUPOK);
339 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
340 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
341 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
342 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
343 umtxq_chains[i][j].uc_busy = 0;
344 umtxq_chains[i][j].uc_waiters = 0;
345 #ifdef UMTX_PROFILING
346 umtxq_chains[i][j].length = 0;
347 umtxq_chains[i][j].max_length = 0;
351 #ifdef UMTX_PROFILING
352 umtx_init_profiling();
354 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
363 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
364 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
366 TAILQ_INIT(&uq->uq_spare_queue->head);
367 TAILQ_INIT(&uq->uq_pi_contested);
368 uq->uq_inherited_pri = PRI_MAX;
373 umtxq_free(struct umtx_q *uq)
376 MPASS(uq->uq_spare_queue != NULL);
377 free(uq->uq_spare_queue, M_UMTX);
382 umtxq_hash(struct umtx_key *key)
386 n = (uintptr_t)key->info.both.a + key->info.both.b;
387 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
391 umtxq_getchain(struct umtx_key *key)
394 if (key->type <= TYPE_SEM)
395 return (&umtxq_chains[1][key->hash]);
396 return (&umtxq_chains[0][key->hash]);
400 * Set chain to busy state when following operation
401 * may be blocked (kernel mutex can not be used).
404 umtxq_busy(struct umtx_key *key)
406 struct umtxq_chain *uc;
408 uc = umtxq_getchain(key);
409 mtx_assert(&uc->uc_lock, MA_OWNED);
413 int count = BUSY_SPINS;
416 while (uc->uc_busy && --count > 0)
422 while (uc->uc_busy) {
424 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
435 umtxq_unbusy(struct umtx_key *key)
437 struct umtxq_chain *uc;
439 uc = umtxq_getchain(key);
440 mtx_assert(&uc->uc_lock, MA_OWNED);
441 KASSERT(uc->uc_busy != 0, ("not busy"));
448 umtxq_unbusy_unlocked(struct umtx_key *key)
456 static struct umtxq_queue *
457 umtxq_queue_lookup(struct umtx_key *key, int q)
459 struct umtxq_queue *uh;
460 struct umtxq_chain *uc;
462 uc = umtxq_getchain(key);
463 UMTXQ_LOCKED_ASSERT(uc);
464 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
465 if (umtx_key_match(&uh->key, key))
473 umtxq_insert_queue(struct umtx_q *uq, int q)
475 struct umtxq_queue *uh;
476 struct umtxq_chain *uc;
478 uc = umtxq_getchain(&uq->uq_key);
479 UMTXQ_LOCKED_ASSERT(uc);
480 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
481 uh = umtxq_queue_lookup(&uq->uq_key, q);
483 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
485 uh = uq->uq_spare_queue;
486 uh->key = uq->uq_key;
487 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
488 #ifdef UMTX_PROFILING
490 if (uc->length > uc->max_length) {
491 uc->max_length = uc->length;
492 if (uc->max_length > max_length)
493 max_length = uc->max_length;
497 uq->uq_spare_queue = NULL;
499 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
501 uq->uq_flags |= UQF_UMTXQ;
502 uq->uq_cur_queue = uh;
507 umtxq_remove_queue(struct umtx_q *uq, int q)
509 struct umtxq_chain *uc;
510 struct umtxq_queue *uh;
512 uc = umtxq_getchain(&uq->uq_key);
513 UMTXQ_LOCKED_ASSERT(uc);
514 if (uq->uq_flags & UQF_UMTXQ) {
515 uh = uq->uq_cur_queue;
516 TAILQ_REMOVE(&uh->head, uq, uq_link);
518 uq->uq_flags &= ~UQF_UMTXQ;
519 if (TAILQ_EMPTY(&uh->head)) {
520 KASSERT(uh->length == 0,
521 ("inconsistent umtxq_queue length"));
522 #ifdef UMTX_PROFILING
525 LIST_REMOVE(uh, link);
527 uh = LIST_FIRST(&uc->uc_spare_queue);
528 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
529 LIST_REMOVE(uh, link);
531 uq->uq_spare_queue = uh;
532 uq->uq_cur_queue = NULL;
537 * Check if there are multiple waiters
540 umtxq_count(struct umtx_key *key)
542 struct umtxq_queue *uh;
544 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
545 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
552 * Check if there are multiple PI waiters and returns first
556 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
558 struct umtxq_queue *uh;
561 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
562 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
564 *first = TAILQ_FIRST(&uh->head);
571 * Wake up threads waiting on an userland object by a bit mask.
574 umtxq_signal_mask(struct umtx_key *key, int n_wake, u_int bitset)
576 struct umtxq_queue *uh;
577 struct umtx_q *uq, *uq_temp;
581 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
582 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
585 TAILQ_FOREACH_SAFE(uq, &uh->head, uq_link, uq_temp) {
586 if ((uq->uq_bitset & bitset) == 0)
588 umtxq_remove_queue(uq, UMTX_SHARED_QUEUE);
597 * Wake up threads waiting on an userland object.
601 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
603 struct umtxq_queue *uh;
608 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
609 uh = umtxq_queue_lookup(key, q);
611 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
612 umtxq_remove_queue(uq, q);
622 * Wake up specified thread.
625 umtxq_signal_thread(struct umtx_q *uq)
628 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
634 * Wake up a maximum of n_wake threads that are waiting on an userland
635 * object identified by key. The remaining threads are removed from queue
636 * identified by key and added to the queue identified by key2 (requeued).
637 * The n_requeue specifies an upper limit on the number of threads that
638 * are requeued to the second queue.
641 umtxq_requeue(struct umtx_key *key, int n_wake, struct umtx_key *key2,
644 struct umtxq_queue *uh;
645 struct umtx_q *uq, *uq_temp;
649 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
650 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key2));
651 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
654 TAILQ_FOREACH_SAFE(uq, &uh->head, uq_link, uq_temp) {
655 if (++ret <= n_wake) {
662 if (ret - n_wake == n_requeue)
670 tstohz(const struct timespec *tsp)
674 TIMESPEC_TO_TIMEVAL(&tv, tsp);
679 umtx_abs_timeout_init(struct umtx_abs_timeout *timo, int clockid,
680 int absolute, const struct timespec *timeout)
683 timo->clockid = clockid;
685 timo->is_abs_real = false;
686 umtx_abs_timeout_update(timo);
687 timespecadd(&timo->cur, timeout, &timo->end);
689 timo->end = *timeout;
690 timo->is_abs_real = clockid == CLOCK_REALTIME ||
691 clockid == CLOCK_REALTIME_FAST ||
692 clockid == CLOCK_REALTIME_PRECISE;
694 * If is_abs_real, umtxq_sleep will read the clock
695 * after setting td_rtcgen; otherwise, read it here.
697 if (!timo->is_abs_real) {
698 umtx_abs_timeout_update(timo);
704 umtx_abs_timeout_init2(struct umtx_abs_timeout *timo,
705 const struct _umtx_time *umtxtime)
708 umtx_abs_timeout_init(timo, umtxtime->_clockid,
709 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
713 umtx_abs_timeout_update(struct umtx_abs_timeout *timo)
716 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
720 umtx_abs_timeout_gethz(struct umtx_abs_timeout *timo)
724 if (timespeccmp(&timo->end, &timo->cur, <=))
726 timespecsub(&timo->end, &timo->cur, &tts);
727 return (tstohz(&tts));
731 umtx_unlock_val(uint32_t flags, bool rb)
735 return (UMUTEX_RB_OWNERDEAD);
736 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
737 return (UMUTEX_RB_NOTRECOV);
739 return (UMUTEX_UNOWNED);
744 * Put thread into sleep state, before sleeping, check if
745 * thread was removed from umtx queue.
748 umtxq_sleep(struct umtx_q *uq, const char *wmesg,
749 struct umtx_abs_timeout *abstime)
751 struct umtxq_chain *uc;
754 if (abstime != NULL && abstime->is_abs_real) {
755 curthread->td_rtcgen = atomic_load_acq_int(&rtc_generation);
756 umtx_abs_timeout_update(abstime);
759 uc = umtxq_getchain(&uq->uq_key);
760 UMTXQ_LOCKED_ASSERT(uc);
762 if (!(uq->uq_flags & UQF_UMTXQ)) {
766 if (abstime != NULL) {
767 timo = umtx_abs_timeout_gethz(abstime);
774 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
775 if (error == EINTR || error == ERESTART) {
776 umtxq_lock(&uq->uq_key);
779 if (abstime != NULL) {
780 if (abstime->is_abs_real)
781 curthread->td_rtcgen =
782 atomic_load_acq_int(&rtc_generation);
783 umtx_abs_timeout_update(abstime);
785 umtxq_lock(&uq->uq_key);
788 curthread->td_rtcgen = 0;
793 * Convert userspace address into unique logical address.
796 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
798 struct thread *td = curthread;
800 vm_map_entry_t entry;
806 if (share == THREAD_SHARE) {
808 key->info.private.vs = td->td_proc->p_vmspace;
809 key->info.private.addr = (uintptr_t)addr;
811 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
812 map = &td->td_proc->p_vmspace->vm_map;
813 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
814 &entry, &key->info.shared.object, &pindex, &prot,
815 &wired) != KERN_SUCCESS) {
819 if ((share == PROCESS_SHARE) ||
820 (share == AUTO_SHARE &&
821 VM_INHERIT_SHARE == entry->inheritance)) {
823 key->info.shared.offset = (vm_offset_t)addr -
824 entry->start + entry->offset;
825 vm_object_reference(key->info.shared.object);
828 key->info.private.vs = td->td_proc->p_vmspace;
829 key->info.private.addr = (uintptr_t)addr;
831 vm_map_lookup_done(map, entry);
842 umtx_key_release(struct umtx_key *key)
845 vm_object_deallocate(key->info.shared.object);
848 #ifdef COMPAT_FREEBSD10
850 * Lock a umtx object.
853 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
854 const struct timespec *timeout)
856 struct umtx_abs_timeout timo;
864 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
867 * Care must be exercised when dealing with umtx structure. It
868 * can fault on any access.
872 * Try the uncontested case. This should be done in userland.
874 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
876 /* The acquire succeeded. */
877 if (owner == UMTX_UNOWNED)
880 /* The address was invalid. */
884 /* If no one owns it but it is contested try to acquire it. */
885 if (owner == UMTX_CONTESTED) {
886 owner = casuword(&umtx->u_owner,
887 UMTX_CONTESTED, id | UMTX_CONTESTED);
889 if (owner == UMTX_CONTESTED)
892 /* The address was invalid. */
896 error = thread_check_susp(td, false);
900 /* If this failed the lock has changed, restart. */
905 * If we caught a signal, we have retried and now
911 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
912 AUTO_SHARE, &uq->uq_key)) != 0)
915 umtxq_lock(&uq->uq_key);
916 umtxq_busy(&uq->uq_key);
918 umtxq_unbusy(&uq->uq_key);
919 umtxq_unlock(&uq->uq_key);
922 * Set the contested bit so that a release in user space
923 * knows to use the system call for unlock. If this fails
924 * either some one else has acquired the lock or it has been
927 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
929 /* The address was invalid. */
931 umtxq_lock(&uq->uq_key);
933 umtxq_unlock(&uq->uq_key);
934 umtx_key_release(&uq->uq_key);
939 * We set the contested bit, sleep. Otherwise the lock changed
940 * and we need to retry or we lost a race to the thread
941 * unlocking the umtx.
943 umtxq_lock(&uq->uq_key);
945 error = umtxq_sleep(uq, "umtx", timeout == NULL ? NULL :
948 umtxq_unlock(&uq->uq_key);
949 umtx_key_release(&uq->uq_key);
952 error = thread_check_susp(td, false);
955 if (timeout == NULL) {
956 /* Mutex locking is restarted if it is interrupted. */
960 /* Timed-locking is not restarted. */
961 if (error == ERESTART)
968 * Unlock a umtx object.
971 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
980 * Make sure we own this mtx.
982 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
986 if ((owner & ~UMTX_CONTESTED) != id)
989 /* This should be done in userland */
990 if ((owner & UMTX_CONTESTED) == 0) {
991 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
999 /* We should only ever be in here for contested locks */
1000 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1006 count = umtxq_count(&key);
1010 * When unlocking the umtx, it must be marked as unowned if
1011 * there is zero or one thread only waiting for it.
1012 * Otherwise, it must be marked as contested.
1014 old = casuword(&umtx->u_owner, owner,
1015 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
1017 umtxq_signal(&key,1);
1020 umtx_key_release(&key);
1028 #ifdef COMPAT_FREEBSD32
1031 * Lock a umtx object.
1034 do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id,
1035 const struct timespec *timeout)
1037 struct umtx_abs_timeout timo;
1045 if (timeout != NULL)
1046 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
1049 * Care must be exercised when dealing with umtx structure. It
1050 * can fault on any access.
1054 * Try the uncontested case. This should be done in userland.
1056 owner = casuword32(m, UMUTEX_UNOWNED, id);
1058 /* The acquire succeeded. */
1059 if (owner == UMUTEX_UNOWNED)
1062 /* The address was invalid. */
1066 /* If no one owns it but it is contested try to acquire it. */
1067 if (owner == UMUTEX_CONTESTED) {
1068 owner = casuword32(m,
1069 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1070 if (owner == UMUTEX_CONTESTED)
1073 /* The address was invalid. */
1077 error = thread_check_susp(td, false);
1081 /* If this failed the lock has changed, restart. */
1086 * If we caught a signal, we have retried and now
1092 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
1093 AUTO_SHARE, &uq->uq_key)) != 0)
1096 umtxq_lock(&uq->uq_key);
1097 umtxq_busy(&uq->uq_key);
1099 umtxq_unbusy(&uq->uq_key);
1100 umtxq_unlock(&uq->uq_key);
1103 * Set the contested bit so that a release in user space
1104 * knows to use the system call for unlock. If this fails
1105 * either some one else has acquired the lock or it has been
1108 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
1110 /* The address was invalid. */
1112 umtxq_lock(&uq->uq_key);
1114 umtxq_unlock(&uq->uq_key);
1115 umtx_key_release(&uq->uq_key);
1120 * We set the contested bit, sleep. Otherwise the lock changed
1121 * and we need to retry or we lost a race to the thread
1122 * unlocking the umtx.
1124 umtxq_lock(&uq->uq_key);
1126 error = umtxq_sleep(uq, "umtx", timeout == NULL ?
1129 umtxq_unlock(&uq->uq_key);
1130 umtx_key_release(&uq->uq_key);
1133 error = thread_check_susp(td, false);
1136 if (timeout == NULL) {
1137 /* Mutex locking is restarted if it is interrupted. */
1141 /* Timed-locking is not restarted. */
1142 if (error == ERESTART)
1149 * Unlock a umtx object.
1152 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
1154 struct umtx_key key;
1161 * Make sure we own this mtx.
1163 owner = fuword32(m);
1167 if ((owner & ~UMUTEX_CONTESTED) != id)
1170 /* This should be done in userland */
1171 if ((owner & UMUTEX_CONTESTED) == 0) {
1172 old = casuword32(m, owner, UMUTEX_UNOWNED);
1180 /* We should only ever be in here for contested locks */
1181 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1187 count = umtxq_count(&key);
1191 * When unlocking the umtx, it must be marked as unowned if
1192 * there is zero or one thread only waiting for it.
1193 * Otherwise, it must be marked as contested.
1195 old = casuword32(m, owner,
1196 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1198 umtxq_signal(&key,1);
1201 umtx_key_release(&key);
1208 #endif /* COMPAT_FREEBSD32 */
1209 #endif /* COMPAT_FREEBSD10 */
1212 * Fetch and compare value, sleep on the address if value is not changed.
1215 do_wait(struct thread *td, void *addr, u_long id,
1216 struct _umtx_time *timeout, int compat32, int is_private)
1218 struct umtx_abs_timeout timo;
1225 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
1226 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
1229 if (timeout != NULL)
1230 umtx_abs_timeout_init2(&timo, timeout);
1232 umtxq_lock(&uq->uq_key);
1234 umtxq_unlock(&uq->uq_key);
1235 if (compat32 == 0) {
1236 error = fueword(addr, &tmp);
1240 error = fueword32(addr, &tmp32);
1246 umtxq_lock(&uq->uq_key);
1249 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
1251 if ((uq->uq_flags & UQF_UMTXQ) == 0)
1255 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
1258 umtxq_unlock(&uq->uq_key);
1259 umtx_key_release(&uq->uq_key);
1260 if (error == ERESTART)
1266 * Wake up threads sleeping on the specified address.
1269 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1271 struct umtx_key key;
1274 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1275 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1278 umtxq_signal(&key, n_wake);
1280 umtx_key_release(&key);
1285 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1288 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1289 struct _umtx_time *timeout, int mode)
1291 struct umtx_abs_timeout timo;
1293 uint32_t owner, old, id;
1299 if (timeout != NULL)
1300 umtx_abs_timeout_init2(&timo, timeout);
1303 * Care must be exercised when dealing with umtx structure. It
1304 * can fault on any access.
1307 rv = fueword32(&m->m_owner, &owner);
1310 if (mode == _UMUTEX_WAIT) {
1311 if (owner == UMUTEX_UNOWNED ||
1312 owner == UMUTEX_CONTESTED ||
1313 owner == UMUTEX_RB_OWNERDEAD ||
1314 owner == UMUTEX_RB_NOTRECOV)
1318 * Robust mutex terminated. Kernel duty is to
1319 * return EOWNERDEAD to the userspace. The
1320 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1321 * by the common userspace code.
1323 if (owner == UMUTEX_RB_OWNERDEAD) {
1324 rv = casueword32(&m->m_owner,
1325 UMUTEX_RB_OWNERDEAD, &owner,
1326 id | UMUTEX_CONTESTED);
1330 MPASS(owner == UMUTEX_RB_OWNERDEAD);
1331 return (EOWNERDEAD); /* success */
1334 rv = thread_check_susp(td, false);
1339 if (owner == UMUTEX_RB_NOTRECOV)
1340 return (ENOTRECOVERABLE);
1343 * Try the uncontested case. This should be
1346 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1348 /* The address was invalid. */
1352 /* The acquire succeeded. */
1354 MPASS(owner == UMUTEX_UNOWNED);
1359 * If no one owns it but it is contested try
1363 if (owner == UMUTEX_CONTESTED) {
1364 rv = casueword32(&m->m_owner,
1365 UMUTEX_CONTESTED, &owner,
1366 id | UMUTEX_CONTESTED);
1367 /* The address was invalid. */
1371 MPASS(owner == UMUTEX_CONTESTED);
1375 rv = thread_check_susp(td, false);
1381 * If this failed the lock has
1387 /* rv == 1 but not contested, likely store failure */
1388 rv = thread_check_susp(td, false);
1393 if (mode == _UMUTEX_TRY)
1397 * If we caught a signal, we have retried and now
1403 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1404 GET_SHARE(flags), &uq->uq_key)) != 0)
1407 umtxq_lock(&uq->uq_key);
1408 umtxq_busy(&uq->uq_key);
1410 umtxq_unlock(&uq->uq_key);
1413 * Set the contested bit so that a release in user space
1414 * knows to use the system call for unlock. If this fails
1415 * either some one else has acquired the lock or it has been
1418 rv = casueword32(&m->m_owner, owner, &old,
1419 owner | UMUTEX_CONTESTED);
1421 /* The address was invalid or casueword failed to store. */
1422 if (rv == -1 || rv == 1) {
1423 umtxq_lock(&uq->uq_key);
1425 umtxq_unbusy(&uq->uq_key);
1426 umtxq_unlock(&uq->uq_key);
1427 umtx_key_release(&uq->uq_key);
1431 rv = thread_check_susp(td, false);
1439 * We set the contested bit, sleep. Otherwise the lock changed
1440 * and we need to retry or we lost a race to the thread
1441 * unlocking the umtx.
1443 umtxq_lock(&uq->uq_key);
1444 umtxq_unbusy(&uq->uq_key);
1445 MPASS(old == owner);
1446 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1449 umtxq_unlock(&uq->uq_key);
1450 umtx_key_release(&uq->uq_key);
1453 error = thread_check_susp(td, false);
1460 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1463 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1465 struct umtx_key key;
1466 uint32_t owner, old, id, newlock;
1473 * Make sure we own this mtx.
1475 error = fueword32(&m->m_owner, &owner);
1479 if ((owner & ~UMUTEX_CONTESTED) != id)
1482 newlock = umtx_unlock_val(flags, rb);
1483 if ((owner & UMUTEX_CONTESTED) == 0) {
1484 error = casueword32(&m->m_owner, owner, &old, newlock);
1488 error = thread_check_susp(td, false);
1493 MPASS(old == owner);
1497 /* We should only ever be in here for contested locks */
1498 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1504 count = umtxq_count(&key);
1508 * When unlocking the umtx, it must be marked as unowned if
1509 * there is zero or one thread only waiting for it.
1510 * Otherwise, it must be marked as contested.
1513 newlock |= UMUTEX_CONTESTED;
1514 error = casueword32(&m->m_owner, owner, &old, newlock);
1516 umtxq_signal(&key, 1);
1519 umtx_key_release(&key);
1525 error = thread_check_susp(td, false);
1534 * Check if the mutex is available and wake up a waiter,
1535 * only for simple mutex.
1538 do_wake_umutex(struct thread *td, struct umutex *m)
1540 struct umtx_key key;
1547 error = fueword32(&m->m_owner, &owner);
1551 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1552 owner != UMUTEX_RB_NOTRECOV)
1555 error = fueword32(&m->m_flags, &flags);
1559 /* We should only ever be in here for contested locks */
1560 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1566 count = umtxq_count(&key);
1569 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1570 owner != UMUTEX_RB_NOTRECOV) {
1571 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1575 } else if (error == 1) {
1579 umtx_key_release(&key);
1580 error = thread_check_susp(td, false);
1588 if (error == 0 && count != 0) {
1589 MPASS((owner & ~UMUTEX_CONTESTED) == 0 ||
1590 owner == UMUTEX_RB_OWNERDEAD ||
1591 owner == UMUTEX_RB_NOTRECOV);
1592 umtxq_signal(&key, 1);
1596 umtx_key_release(&key);
1601 * Check if the mutex has waiters and tries to fix contention bit.
1604 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1606 struct umtx_key key;
1607 uint32_t owner, old;
1612 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1616 type = TYPE_NORMAL_UMUTEX;
1618 case UMUTEX_PRIO_INHERIT:
1619 type = TYPE_PI_UMUTEX;
1621 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1622 type = TYPE_PI_ROBUST_UMUTEX;
1624 case UMUTEX_PRIO_PROTECT:
1625 type = TYPE_PP_UMUTEX;
1627 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1628 type = TYPE_PP_ROBUST_UMUTEX;
1633 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1639 count = umtxq_count(&key);
1642 error = fueword32(&m->m_owner, &owner);
1647 * Only repair contention bit if there is a waiter, this means
1648 * the mutex is still being referenced by userland code,
1649 * otherwise don't update any memory.
1651 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0 &&
1652 (count > 1 || (count == 1 && (owner & ~UMUTEX_CONTESTED) != 0))) {
1653 error = casueword32(&m->m_owner, owner, &old,
1654 owner | UMUTEX_CONTESTED);
1660 MPASS(old == owner);
1664 error = thread_check_susp(td, false);
1668 if (error == EFAULT) {
1669 umtxq_signal(&key, INT_MAX);
1670 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1671 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1672 umtxq_signal(&key, 1);
1675 umtx_key_release(&key);
1680 umtx_pi_alloc(int flags)
1684 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1685 TAILQ_INIT(&pi->pi_blocked);
1686 atomic_add_int(&umtx_pi_allocated, 1);
1691 umtx_pi_free(struct umtx_pi *pi)
1693 uma_zfree(umtx_pi_zone, pi);
1694 atomic_add_int(&umtx_pi_allocated, -1);
1698 * Adjust the thread's position on a pi_state after its priority has been
1702 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1704 struct umtx_q *uq, *uq1, *uq2;
1707 mtx_assert(&umtx_lock, MA_OWNED);
1714 * Check if the thread needs to be moved on the blocked chain.
1715 * It needs to be moved if either its priority is lower than
1716 * the previous thread or higher than the next thread.
1718 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1719 uq2 = TAILQ_NEXT(uq, uq_lockq);
1720 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1721 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1723 * Remove thread from blocked chain and determine where
1724 * it should be moved to.
1726 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1727 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1728 td1 = uq1->uq_thread;
1729 MPASS(td1->td_proc->p_magic == P_MAGIC);
1730 if (UPRI(td1) > UPRI(td))
1735 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1737 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1742 static struct umtx_pi *
1743 umtx_pi_next(struct umtx_pi *pi)
1745 struct umtx_q *uq_owner;
1747 if (pi->pi_owner == NULL)
1749 uq_owner = pi->pi_owner->td_umtxq;
1750 if (uq_owner == NULL)
1752 return (uq_owner->uq_pi_blocked);
1756 * Floyd's Cycle-Finding Algorithm.
1759 umtx_pi_check_loop(struct umtx_pi *pi)
1761 struct umtx_pi *pi1; /* fast iterator */
1763 mtx_assert(&umtx_lock, MA_OWNED);
1768 pi = umtx_pi_next(pi);
1771 pi1 = umtx_pi_next(pi1);
1774 pi1 = umtx_pi_next(pi1);
1784 * Propagate priority when a thread is blocked on POSIX
1788 umtx_propagate_priority(struct thread *td)
1794 mtx_assert(&umtx_lock, MA_OWNED);
1797 pi = uq->uq_pi_blocked;
1800 if (umtx_pi_check_loop(pi))
1805 if (td == NULL || td == curthread)
1808 MPASS(td->td_proc != NULL);
1809 MPASS(td->td_proc->p_magic == P_MAGIC);
1812 if (td->td_lend_user_pri > pri)
1813 sched_lend_user_prio(td, pri);
1821 * Pick up the lock that td is blocked on.
1824 pi = uq->uq_pi_blocked;
1827 /* Resort td on the list if needed. */
1828 umtx_pi_adjust_thread(pi, td);
1833 * Unpropagate priority for a PI mutex when a thread blocked on
1834 * it is interrupted by signal or resumed by others.
1837 umtx_repropagate_priority(struct umtx_pi *pi)
1839 struct umtx_q *uq, *uq_owner;
1840 struct umtx_pi *pi2;
1843 mtx_assert(&umtx_lock, MA_OWNED);
1845 if (umtx_pi_check_loop(pi))
1847 while (pi != NULL && pi->pi_owner != NULL) {
1849 uq_owner = pi->pi_owner->td_umtxq;
1851 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1852 uq = TAILQ_FIRST(&pi2->pi_blocked);
1854 if (pri > UPRI(uq->uq_thread))
1855 pri = UPRI(uq->uq_thread);
1859 if (pri > uq_owner->uq_inherited_pri)
1860 pri = uq_owner->uq_inherited_pri;
1861 thread_lock(pi->pi_owner);
1862 sched_lend_user_prio(pi->pi_owner, pri);
1863 thread_unlock(pi->pi_owner);
1864 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1865 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1870 * Insert a PI mutex into owned list.
1873 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1875 struct umtx_q *uq_owner;
1877 uq_owner = owner->td_umtxq;
1878 mtx_assert(&umtx_lock, MA_OWNED);
1879 MPASS(pi->pi_owner == NULL);
1880 pi->pi_owner = owner;
1881 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1885 * Disown a PI mutex, and remove it from the owned list.
1888 umtx_pi_disown(struct umtx_pi *pi)
1891 mtx_assert(&umtx_lock, MA_OWNED);
1892 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1893 pi->pi_owner = NULL;
1897 * Claim ownership of a PI mutex.
1900 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1905 mtx_lock(&umtx_lock);
1906 if (pi->pi_owner == owner) {
1907 mtx_unlock(&umtx_lock);
1911 if (pi->pi_owner != NULL) {
1913 * userland may have already messed the mutex, sigh.
1915 mtx_unlock(&umtx_lock);
1918 umtx_pi_setowner(pi, owner);
1919 uq = TAILQ_FIRST(&pi->pi_blocked);
1921 pri = UPRI(uq->uq_thread);
1923 if (pri < UPRI(owner))
1924 sched_lend_user_prio(owner, pri);
1925 thread_unlock(owner);
1927 mtx_unlock(&umtx_lock);
1932 * Adjust a thread's order position in its blocked PI mutex,
1933 * this may result new priority propagating process.
1936 umtx_pi_adjust(struct thread *td, u_char oldpri)
1942 mtx_lock(&umtx_lock);
1944 * Pick up the lock that td is blocked on.
1946 pi = uq->uq_pi_blocked;
1948 umtx_pi_adjust_thread(pi, td);
1949 umtx_repropagate_priority(pi);
1951 mtx_unlock(&umtx_lock);
1955 * Sleep on a PI mutex.
1958 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
1959 const char *wmesg, struct umtx_abs_timeout *timo, bool shared)
1961 struct thread *td, *td1;
1965 struct umtxq_chain *uc;
1967 uc = umtxq_getchain(&pi->pi_key);
1971 KASSERT(td == curthread, ("inconsistent uq_thread"));
1972 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
1973 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1975 mtx_lock(&umtx_lock);
1976 if (pi->pi_owner == NULL) {
1977 mtx_unlock(&umtx_lock);
1978 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
1979 mtx_lock(&umtx_lock);
1981 if (pi->pi_owner == NULL)
1982 umtx_pi_setowner(pi, td1);
1983 PROC_UNLOCK(td1->td_proc);
1987 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1988 pri = UPRI(uq1->uq_thread);
1994 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1996 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1998 uq->uq_pi_blocked = pi;
2000 td->td_flags |= TDF_UPIBLOCKED;
2002 umtx_propagate_priority(td);
2003 mtx_unlock(&umtx_lock);
2004 umtxq_unbusy(&uq->uq_key);
2006 error = umtxq_sleep(uq, wmesg, timo);
2009 mtx_lock(&umtx_lock);
2010 uq->uq_pi_blocked = NULL;
2012 td->td_flags &= ~TDF_UPIBLOCKED;
2014 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
2015 umtx_repropagate_priority(pi);
2016 mtx_unlock(&umtx_lock);
2017 umtxq_unlock(&uq->uq_key);
2023 * Add reference count for a PI mutex.
2026 umtx_pi_ref(struct umtx_pi *pi)
2029 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
2034 * Decrease reference count for a PI mutex, if the counter
2035 * is decreased to zero, its memory space is freed.
2038 umtx_pi_unref(struct umtx_pi *pi)
2040 struct umtxq_chain *uc;
2042 uc = umtxq_getchain(&pi->pi_key);
2043 UMTXQ_LOCKED_ASSERT(uc);
2044 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
2045 if (--pi->pi_refcount == 0) {
2046 mtx_lock(&umtx_lock);
2047 if (pi->pi_owner != NULL)
2049 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
2050 ("blocked queue not empty"));
2051 mtx_unlock(&umtx_lock);
2052 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
2058 * Find a PI mutex in hash table.
2061 umtx_pi_lookup(struct umtx_key *key)
2063 struct umtxq_chain *uc;
2066 uc = umtxq_getchain(key);
2067 UMTXQ_LOCKED_ASSERT(uc);
2069 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
2070 if (umtx_key_match(&pi->pi_key, key)) {
2078 * Insert a PI mutex into hash table.
2081 umtx_pi_insert(struct umtx_pi *pi)
2083 struct umtxq_chain *uc;
2085 uc = umtxq_getchain(&pi->pi_key);
2086 UMTXQ_LOCKED_ASSERT(uc);
2087 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
2091 * Drop a PI mutex and wakeup a top waiter.
2094 umtx_pi_drop(struct thread *td, struct umtx_key *key, bool rb, int *count)
2096 struct umtx_q *uq_first, *uq_first2, *uq_me;
2097 struct umtx_pi *pi, *pi2;
2100 UMTXQ_ASSERT_LOCKED_BUSY(key);
2101 *count = umtxq_count_pi(key, &uq_first);
2102 if (uq_first != NULL) {
2103 mtx_lock(&umtx_lock);
2104 pi = uq_first->uq_pi_blocked;
2105 KASSERT(pi != NULL, ("pi == NULL?"));
2106 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2107 mtx_unlock(&umtx_lock);
2108 /* userland messed the mutex */
2111 uq_me = td->td_umtxq;
2112 if (pi->pi_owner == td)
2114 /* get highest priority thread which is still sleeping. */
2115 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2116 while (uq_first != NULL &&
2117 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2118 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2121 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2122 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2123 if (uq_first2 != NULL) {
2124 if (pri > UPRI(uq_first2->uq_thread))
2125 pri = UPRI(uq_first2->uq_thread);
2129 sched_lend_user_prio(td, pri);
2131 mtx_unlock(&umtx_lock);
2133 umtxq_signal_thread(uq_first);
2135 pi = umtx_pi_lookup(key);
2137 * A umtx_pi can exist if a signal or timeout removed the
2138 * last waiter from the umtxq, but there is still
2139 * a thread in do_lock_pi() holding the umtx_pi.
2143 * The umtx_pi can be unowned, such as when a thread
2144 * has just entered do_lock_pi(), allocated the
2145 * umtx_pi, and unlocked the umtxq.
2146 * If the current thread owns it, it must disown it.
2148 mtx_lock(&umtx_lock);
2149 if (pi->pi_owner == td)
2151 mtx_unlock(&umtx_lock);
2161 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
2162 struct _umtx_time *timeout, int try)
2164 struct umtx_abs_timeout timo;
2166 struct umtx_pi *pi, *new_pi;
2167 uint32_t id, old_owner, owner, old;
2173 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2174 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2178 if (timeout != NULL)
2179 umtx_abs_timeout_init2(&timo, timeout);
2181 umtxq_lock(&uq->uq_key);
2182 pi = umtx_pi_lookup(&uq->uq_key);
2184 new_pi = umtx_pi_alloc(M_NOWAIT);
2185 if (new_pi == NULL) {
2186 umtxq_unlock(&uq->uq_key);
2187 new_pi = umtx_pi_alloc(M_WAITOK);
2188 umtxq_lock(&uq->uq_key);
2189 pi = umtx_pi_lookup(&uq->uq_key);
2191 umtx_pi_free(new_pi);
2195 if (new_pi != NULL) {
2196 new_pi->pi_key = uq->uq_key;
2197 umtx_pi_insert(new_pi);
2202 umtxq_unlock(&uq->uq_key);
2205 * Care must be exercised when dealing with umtx structure. It
2206 * can fault on any access.
2210 * Try the uncontested case. This should be done in userland.
2212 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
2213 /* The address was invalid. */
2218 /* The acquire succeeded. */
2220 MPASS(owner == UMUTEX_UNOWNED);
2225 if (owner == UMUTEX_RB_NOTRECOV) {
2226 error = ENOTRECOVERABLE;
2231 * Avoid overwriting a possible error from sleep due
2232 * to the pending signal with suspension check result.
2235 error = thread_check_susp(td, true);
2240 /* If no one owns it but it is contested try to acquire it. */
2241 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
2243 rv = casueword32(&m->m_owner, owner, &owner,
2244 id | UMUTEX_CONTESTED);
2245 /* The address was invalid. */
2252 error = thread_check_susp(td, true);
2258 * If this failed the lock could
2265 MPASS(owner == old_owner);
2266 umtxq_lock(&uq->uq_key);
2267 umtxq_busy(&uq->uq_key);
2268 error = umtx_pi_claim(pi, td);
2269 umtxq_unbusy(&uq->uq_key);
2270 umtxq_unlock(&uq->uq_key);
2273 * Since we're going to return an
2274 * error, restore the m_owner to its
2275 * previous, unowned state to avoid
2276 * compounding the problem.
2278 (void)casuword32(&m->m_owner,
2279 id | UMUTEX_CONTESTED, old_owner);
2281 if (error == 0 && old_owner == UMUTEX_RB_OWNERDEAD)
2286 if ((owner & ~UMUTEX_CONTESTED) == id) {
2297 * If we caught a signal, we have retried and now
2303 umtxq_lock(&uq->uq_key);
2304 umtxq_busy(&uq->uq_key);
2305 umtxq_unlock(&uq->uq_key);
2308 * Set the contested bit so that a release in user space
2309 * knows to use the system call for unlock. If this fails
2310 * either some one else has acquired the lock or it has been
2313 rv = casueword32(&m->m_owner, owner, &old, owner |
2316 /* The address was invalid. */
2318 umtxq_unbusy_unlocked(&uq->uq_key);
2323 umtxq_unbusy_unlocked(&uq->uq_key);
2324 error = thread_check_susp(td, true);
2329 * The lock changed and we need to retry or we
2330 * lost a race to the thread unlocking the
2331 * umtx. Note that the UMUTEX_RB_OWNERDEAD
2332 * value for owner is impossible there.
2337 umtxq_lock(&uq->uq_key);
2339 /* We set the contested bit, sleep. */
2340 MPASS(old == owner);
2341 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
2342 "umtxpi", timeout == NULL ? NULL : &timo,
2343 (flags & USYNC_PROCESS_SHARED) != 0);
2347 error = thread_check_susp(td, false);
2352 umtxq_lock(&uq->uq_key);
2354 umtxq_unlock(&uq->uq_key);
2356 umtx_key_release(&uq->uq_key);
2361 * Unlock a PI mutex.
2364 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2366 struct umtx_key key;
2367 uint32_t id, new_owner, old, owner;
2374 * Make sure we own this mtx.
2376 error = fueword32(&m->m_owner, &owner);
2380 if ((owner & ~UMUTEX_CONTESTED) != id)
2383 new_owner = umtx_unlock_val(flags, rb);
2385 /* This should be done in userland */
2386 if ((owner & UMUTEX_CONTESTED) == 0) {
2387 error = casueword32(&m->m_owner, owner, &old, new_owner);
2391 error = thread_check_susp(td, true);
2401 /* We should only ever be in here for contested locks */
2402 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2403 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2409 error = umtx_pi_drop(td, &key, rb, &count);
2413 umtx_key_release(&key);
2414 /* userland messed the mutex */
2420 * When unlocking the umtx, it must be marked as unowned if
2421 * there is zero or one thread only waiting for it.
2422 * Otherwise, it must be marked as contested.
2426 new_owner |= UMUTEX_CONTESTED;
2428 error = casueword32(&m->m_owner, owner, &old, new_owner);
2430 error = thread_check_susp(td, false);
2434 umtxq_unbusy_unlocked(&key);
2435 umtx_key_release(&key);
2438 if (error == 0 && old != owner)
2447 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2448 struct _umtx_time *timeout, int try)
2450 struct umtx_abs_timeout timo;
2451 struct umtx_q *uq, *uq2;
2455 int error, pri, old_inherited_pri, su, rv;
2459 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2460 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2464 if (timeout != NULL)
2465 umtx_abs_timeout_init2(&timo, timeout);
2467 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2469 old_inherited_pri = uq->uq_inherited_pri;
2470 umtxq_lock(&uq->uq_key);
2471 umtxq_busy(&uq->uq_key);
2472 umtxq_unlock(&uq->uq_key);
2474 rv = fueword32(&m->m_ceilings[0], &ceiling);
2479 ceiling = RTP_PRIO_MAX - ceiling;
2480 if (ceiling > RTP_PRIO_MAX) {
2485 mtx_lock(&umtx_lock);
2486 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2487 mtx_unlock(&umtx_lock);
2491 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2492 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2494 if (uq->uq_inherited_pri < UPRI(td))
2495 sched_lend_user_prio(td, uq->uq_inherited_pri);
2498 mtx_unlock(&umtx_lock);
2500 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2501 id | UMUTEX_CONTESTED);
2502 /* The address was invalid. */
2508 MPASS(owner == UMUTEX_CONTESTED);
2513 if (owner == UMUTEX_RB_OWNERDEAD) {
2514 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2515 &owner, id | UMUTEX_CONTESTED);
2521 MPASS(owner == UMUTEX_RB_OWNERDEAD);
2522 error = EOWNERDEAD; /* success */
2527 * rv == 1, only check for suspension if we
2528 * did not already catched a signal. If we
2529 * get an error from the check, the same
2530 * condition is checked by the umtxq_sleep()
2531 * call below, so we should obliterate the
2532 * error to not skip the last loop iteration.
2535 error = thread_check_susp(td, false);
2544 } else if (owner == UMUTEX_RB_NOTRECOV) {
2545 error = ENOTRECOVERABLE;
2552 * If we caught a signal, we have retried and now
2558 umtxq_lock(&uq->uq_key);
2560 umtxq_unbusy(&uq->uq_key);
2561 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2564 umtxq_unlock(&uq->uq_key);
2566 mtx_lock(&umtx_lock);
2567 uq->uq_inherited_pri = old_inherited_pri;
2569 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2570 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2572 if (pri > UPRI(uq2->uq_thread))
2573 pri = UPRI(uq2->uq_thread);
2576 if (pri > uq->uq_inherited_pri)
2577 pri = uq->uq_inherited_pri;
2579 sched_lend_user_prio(td, pri);
2581 mtx_unlock(&umtx_lock);
2584 if (error != 0 && error != EOWNERDEAD) {
2585 mtx_lock(&umtx_lock);
2586 uq->uq_inherited_pri = old_inherited_pri;
2588 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2589 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2591 if (pri > UPRI(uq2->uq_thread))
2592 pri = UPRI(uq2->uq_thread);
2595 if (pri > uq->uq_inherited_pri)
2596 pri = uq->uq_inherited_pri;
2598 sched_lend_user_prio(td, pri);
2600 mtx_unlock(&umtx_lock);
2604 umtxq_unbusy_unlocked(&uq->uq_key);
2605 umtx_key_release(&uq->uq_key);
2610 * Unlock a PP mutex.
2613 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2615 struct umtx_key key;
2616 struct umtx_q *uq, *uq2;
2618 uint32_t id, owner, rceiling;
2619 int error, pri, new_inherited_pri, su;
2623 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2626 * Make sure we own this mtx.
2628 error = fueword32(&m->m_owner, &owner);
2632 if ((owner & ~UMUTEX_CONTESTED) != id)
2635 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2640 new_inherited_pri = PRI_MAX;
2642 rceiling = RTP_PRIO_MAX - rceiling;
2643 if (rceiling > RTP_PRIO_MAX)
2645 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2648 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2649 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2656 * For priority protected mutex, always set unlocked state
2657 * to UMUTEX_CONTESTED, so that userland always enters kernel
2658 * to lock the mutex, it is necessary because thread priority
2659 * has to be adjusted for such mutex.
2661 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2666 umtxq_signal(&key, 1);
2673 mtx_lock(&umtx_lock);
2675 uq->uq_inherited_pri = new_inherited_pri;
2677 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2678 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2680 if (pri > UPRI(uq2->uq_thread))
2681 pri = UPRI(uq2->uq_thread);
2684 if (pri > uq->uq_inherited_pri)
2685 pri = uq->uq_inherited_pri;
2687 sched_lend_user_prio(td, pri);
2689 mtx_unlock(&umtx_lock);
2691 umtx_key_release(&key);
2696 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2697 uint32_t *old_ceiling)
2700 uint32_t flags, id, owner, save_ceiling;
2703 error = fueword32(&m->m_flags, &flags);
2706 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2708 if (ceiling > RTP_PRIO_MAX)
2712 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2713 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2717 umtxq_lock(&uq->uq_key);
2718 umtxq_busy(&uq->uq_key);
2719 umtxq_unlock(&uq->uq_key);
2721 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2727 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2728 id | UMUTEX_CONTESTED);
2735 MPASS(owner == UMUTEX_CONTESTED);
2736 rv = suword32(&m->m_ceilings[0], ceiling);
2737 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2738 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2742 if ((owner & ~UMUTEX_CONTESTED) == id) {
2743 rv = suword32(&m->m_ceilings[0], ceiling);
2744 error = rv == 0 ? 0 : EFAULT;
2748 if (owner == UMUTEX_RB_OWNERDEAD) {
2751 } else if (owner == UMUTEX_RB_NOTRECOV) {
2752 error = ENOTRECOVERABLE;
2757 * If we caught a signal, we have retried and now
2764 * We set the contested bit, sleep. Otherwise the lock changed
2765 * and we need to retry or we lost a race to the thread
2766 * unlocking the umtx.
2768 umtxq_lock(&uq->uq_key);
2770 umtxq_unbusy(&uq->uq_key);
2771 error = umtxq_sleep(uq, "umtxpp", NULL);
2773 umtxq_unlock(&uq->uq_key);
2775 umtxq_lock(&uq->uq_key);
2777 umtxq_signal(&uq->uq_key, INT_MAX);
2778 umtxq_unbusy(&uq->uq_key);
2779 umtxq_unlock(&uq->uq_key);
2780 umtx_key_release(&uq->uq_key);
2781 if (error == 0 && old_ceiling != NULL) {
2782 rv = suword32(old_ceiling, save_ceiling);
2783 error = rv == 0 ? 0 : EFAULT;
2789 * Lock a userland POSIX mutex.
2792 do_lock_umutex(struct thread *td, struct umutex *m,
2793 struct _umtx_time *timeout, int mode)
2798 error = fueword32(&m->m_flags, &flags);
2802 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2804 error = do_lock_normal(td, m, flags, timeout, mode);
2806 case UMUTEX_PRIO_INHERIT:
2807 error = do_lock_pi(td, m, flags, timeout, mode);
2809 case UMUTEX_PRIO_PROTECT:
2810 error = do_lock_pp(td, m, flags, timeout, mode);
2815 if (timeout == NULL) {
2816 if (error == EINTR && mode != _UMUTEX_WAIT)
2819 /* Timed-locking is not restarted. */
2820 if (error == ERESTART)
2827 * Unlock a userland POSIX mutex.
2830 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2835 error = fueword32(&m->m_flags, &flags);
2839 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2841 return (do_unlock_normal(td, m, flags, rb));
2842 case UMUTEX_PRIO_INHERIT:
2843 return (do_unlock_pi(td, m, flags, rb));
2844 case UMUTEX_PRIO_PROTECT:
2845 return (do_unlock_pp(td, m, flags, rb));
2852 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2853 struct timespec *timeout, u_long wflags)
2855 struct umtx_abs_timeout timo;
2857 uint32_t flags, clockid, hasw;
2861 error = fueword32(&cv->c_flags, &flags);
2864 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2868 if ((wflags & CVWAIT_CLOCKID) != 0) {
2869 error = fueword32(&cv->c_clockid, &clockid);
2871 umtx_key_release(&uq->uq_key);
2874 if (clockid < CLOCK_REALTIME ||
2875 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2876 /* hmm, only HW clock id will work. */
2877 umtx_key_release(&uq->uq_key);
2881 clockid = CLOCK_REALTIME;
2884 umtxq_lock(&uq->uq_key);
2885 umtxq_busy(&uq->uq_key);
2887 umtxq_unlock(&uq->uq_key);
2890 * Set c_has_waiters to 1 before releasing user mutex, also
2891 * don't modify cache line when unnecessary.
2893 error = fueword32(&cv->c_has_waiters, &hasw);
2894 if (error == 0 && hasw == 0)
2895 suword32(&cv->c_has_waiters, 1);
2897 umtxq_unbusy_unlocked(&uq->uq_key);
2899 error = do_unlock_umutex(td, m, false);
2901 if (timeout != NULL)
2902 umtx_abs_timeout_init(&timo, clockid,
2903 (wflags & CVWAIT_ABSTIME) != 0, timeout);
2905 umtxq_lock(&uq->uq_key);
2907 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2911 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2915 * This must be timeout,interrupted by signal or
2916 * surprious wakeup, clear c_has_waiter flag when
2919 umtxq_busy(&uq->uq_key);
2920 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2921 int oldlen = uq->uq_cur_queue->length;
2924 umtxq_unlock(&uq->uq_key);
2925 suword32(&cv->c_has_waiters, 0);
2926 umtxq_lock(&uq->uq_key);
2929 umtxq_unbusy(&uq->uq_key);
2930 if (error == ERESTART)
2934 umtxq_unlock(&uq->uq_key);
2935 umtx_key_release(&uq->uq_key);
2940 * Signal a userland condition variable.
2943 do_cv_signal(struct thread *td, struct ucond *cv)
2945 struct umtx_key key;
2946 int error, cnt, nwake;
2949 error = fueword32(&cv->c_flags, &flags);
2952 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2956 cnt = umtxq_count(&key);
2957 nwake = umtxq_signal(&key, 1);
2960 error = suword32(&cv->c_has_waiters, 0);
2967 umtx_key_release(&key);
2972 do_cv_broadcast(struct thread *td, struct ucond *cv)
2974 struct umtx_key key;
2978 error = fueword32(&cv->c_flags, &flags);
2981 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2986 umtxq_signal(&key, INT_MAX);
2989 error = suword32(&cv->c_has_waiters, 0);
2993 umtxq_unbusy_unlocked(&key);
2995 umtx_key_release(&key);
3000 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
3001 struct _umtx_time *timeout)
3003 struct umtx_abs_timeout timo;
3005 uint32_t flags, wrflags;
3006 int32_t state, oldstate;
3007 int32_t blocked_readers;
3008 int error, error1, rv;
3011 error = fueword32(&rwlock->rw_flags, &flags);
3014 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3018 if (timeout != NULL)
3019 umtx_abs_timeout_init2(&timo, timeout);
3021 wrflags = URWLOCK_WRITE_OWNER;
3022 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
3023 wrflags |= URWLOCK_WRITE_WAITERS;
3026 rv = fueword32(&rwlock->rw_state, &state);
3028 umtx_key_release(&uq->uq_key);
3032 /* try to lock it */
3033 while (!(state & wrflags)) {
3034 if (__predict_false(URWLOCK_READER_COUNT(state) ==
3035 URWLOCK_MAX_READERS)) {
3036 umtx_key_release(&uq->uq_key);
3039 rv = casueword32(&rwlock->rw_state, state,
3040 &oldstate, state + 1);
3042 umtx_key_release(&uq->uq_key);
3046 MPASS(oldstate == state);
3047 umtx_key_release(&uq->uq_key);
3050 error = thread_check_susp(td, true);
3059 /* grab monitor lock */
3060 umtxq_lock(&uq->uq_key);
3061 umtxq_busy(&uq->uq_key);
3062 umtxq_unlock(&uq->uq_key);
3065 * re-read the state, in case it changed between the try-lock above
3066 * and the check below
3068 rv = fueword32(&rwlock->rw_state, &state);
3072 /* set read contention bit */
3073 while (error == 0 && (state & wrflags) &&
3074 !(state & URWLOCK_READ_WAITERS)) {
3075 rv = casueword32(&rwlock->rw_state, state,
3076 &oldstate, state | URWLOCK_READ_WAITERS);
3082 MPASS(oldstate == state);
3086 error = thread_check_susp(td, false);
3091 umtxq_unbusy_unlocked(&uq->uq_key);
3095 /* state is changed while setting flags, restart */
3096 if (!(state & wrflags)) {
3097 umtxq_unbusy_unlocked(&uq->uq_key);
3098 error = thread_check_susp(td, true);
3106 * Contention bit is set, before sleeping, increase
3107 * read waiter count.
3109 rv = fueword32(&rwlock->rw_blocked_readers,
3112 umtxq_unbusy_unlocked(&uq->uq_key);
3116 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
3118 while (state & wrflags) {
3119 umtxq_lock(&uq->uq_key);
3121 umtxq_unbusy(&uq->uq_key);
3123 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
3126 umtxq_busy(&uq->uq_key);
3128 umtxq_unlock(&uq->uq_key);
3131 rv = fueword32(&rwlock->rw_state, &state);
3138 /* decrease read waiter count, and may clear read contention bit */
3139 rv = fueword32(&rwlock->rw_blocked_readers,
3142 umtxq_unbusy_unlocked(&uq->uq_key);
3146 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
3147 if (blocked_readers == 1) {
3148 rv = fueword32(&rwlock->rw_state, &state);
3150 umtxq_unbusy_unlocked(&uq->uq_key);
3155 rv = casueword32(&rwlock->rw_state, state,
3156 &oldstate, state & ~URWLOCK_READ_WAITERS);
3162 MPASS(oldstate == state);
3166 error1 = thread_check_susp(td, false);
3175 umtxq_unbusy_unlocked(&uq->uq_key);
3179 umtx_key_release(&uq->uq_key);
3180 if (error == ERESTART)
3186 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
3188 struct umtx_abs_timeout timo;
3191 int32_t state, oldstate;
3192 int32_t blocked_writers;
3193 int32_t blocked_readers;
3194 int error, error1, rv;
3197 error = fueword32(&rwlock->rw_flags, &flags);
3200 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3204 if (timeout != NULL)
3205 umtx_abs_timeout_init2(&timo, timeout);
3207 blocked_readers = 0;
3209 rv = fueword32(&rwlock->rw_state, &state);
3211 umtx_key_release(&uq->uq_key);
3214 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
3215 URWLOCK_READER_COUNT(state) == 0) {
3216 rv = casueword32(&rwlock->rw_state, state,
3217 &oldstate, state | URWLOCK_WRITE_OWNER);
3219 umtx_key_release(&uq->uq_key);
3223 MPASS(oldstate == state);
3224 umtx_key_release(&uq->uq_key);
3228 error = thread_check_susp(td, true);
3234 if ((state & (URWLOCK_WRITE_OWNER |
3235 URWLOCK_WRITE_WAITERS)) == 0 &&
3236 blocked_readers != 0) {
3237 umtxq_lock(&uq->uq_key);
3238 umtxq_busy(&uq->uq_key);
3239 umtxq_signal_queue(&uq->uq_key, INT_MAX,
3241 umtxq_unbusy(&uq->uq_key);
3242 umtxq_unlock(&uq->uq_key);
3248 /* grab monitor lock */
3249 umtxq_lock(&uq->uq_key);
3250 umtxq_busy(&uq->uq_key);
3251 umtxq_unlock(&uq->uq_key);
3254 * Re-read the state, in case it changed between the
3255 * try-lock above and the check below.
3257 rv = fueword32(&rwlock->rw_state, &state);
3261 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
3262 URWLOCK_READER_COUNT(state) != 0) &&
3263 (state & URWLOCK_WRITE_WAITERS) == 0) {
3264 rv = casueword32(&rwlock->rw_state, state,
3265 &oldstate, state | URWLOCK_WRITE_WAITERS);
3271 MPASS(oldstate == state);
3275 error = thread_check_susp(td, false);
3280 umtxq_unbusy_unlocked(&uq->uq_key);
3284 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
3285 URWLOCK_READER_COUNT(state) == 0) {
3286 umtxq_unbusy_unlocked(&uq->uq_key);
3287 error = thread_check_susp(td, false);
3293 rv = fueword32(&rwlock->rw_blocked_writers,
3296 umtxq_unbusy_unlocked(&uq->uq_key);
3300 suword32(&rwlock->rw_blocked_writers, blocked_writers + 1);
3302 while ((state & URWLOCK_WRITE_OWNER) ||
3303 URWLOCK_READER_COUNT(state) != 0) {
3304 umtxq_lock(&uq->uq_key);
3305 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3306 umtxq_unbusy(&uq->uq_key);
3308 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
3311 umtxq_busy(&uq->uq_key);
3312 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3313 umtxq_unlock(&uq->uq_key);
3316 rv = fueword32(&rwlock->rw_state, &state);
3323 rv = fueword32(&rwlock->rw_blocked_writers,
3326 umtxq_unbusy_unlocked(&uq->uq_key);
3330 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
3331 if (blocked_writers == 1) {
3332 rv = fueword32(&rwlock->rw_state, &state);
3334 umtxq_unbusy_unlocked(&uq->uq_key);
3339 rv = casueword32(&rwlock->rw_state, state,
3340 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3346 MPASS(oldstate == state);
3350 error1 = thread_check_susp(td, false);
3352 * We are leaving the URWLOCK_WRITE_WAITERS
3353 * behind, but this should not harm the
3362 rv = fueword32(&rwlock->rw_blocked_readers,
3365 umtxq_unbusy_unlocked(&uq->uq_key);
3370 blocked_readers = 0;
3372 umtxq_unbusy_unlocked(&uq->uq_key);
3375 umtx_key_release(&uq->uq_key);
3376 if (error == ERESTART)
3382 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3386 int32_t state, oldstate;
3387 int error, rv, q, count;
3390 error = fueword32(&rwlock->rw_flags, &flags);
3393 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3397 error = fueword32(&rwlock->rw_state, &state);
3402 if (state & URWLOCK_WRITE_OWNER) {
3404 rv = casueword32(&rwlock->rw_state, state,
3405 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3412 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3416 error = thread_check_susp(td, true);
3422 } else if (URWLOCK_READER_COUNT(state) != 0) {
3424 rv = casueword32(&rwlock->rw_state, state,
3425 &oldstate, state - 1);
3432 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3436 error = thread_check_susp(td, true);
3449 if (!(flags & URWLOCK_PREFER_READER)) {
3450 if (state & URWLOCK_WRITE_WAITERS) {
3452 q = UMTX_EXCLUSIVE_QUEUE;
3453 } else if (state & URWLOCK_READ_WAITERS) {
3455 q = UMTX_SHARED_QUEUE;
3458 if (state & URWLOCK_READ_WAITERS) {
3460 q = UMTX_SHARED_QUEUE;
3461 } else if (state & URWLOCK_WRITE_WAITERS) {
3463 q = UMTX_EXCLUSIVE_QUEUE;
3468 umtxq_lock(&uq->uq_key);
3469 umtxq_busy(&uq->uq_key);
3470 umtxq_signal_queue(&uq->uq_key, count, q);
3471 umtxq_unbusy(&uq->uq_key);
3472 umtxq_unlock(&uq->uq_key);
3475 umtx_key_release(&uq->uq_key);
3479 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3481 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3483 struct umtx_abs_timeout timo;
3485 uint32_t flags, count, count1;
3489 error = fueword32(&sem->_flags, &flags);
3492 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3496 if (timeout != NULL)
3497 umtx_abs_timeout_init2(&timo, timeout);
3500 umtxq_lock(&uq->uq_key);
3501 umtxq_busy(&uq->uq_key);
3503 umtxq_unlock(&uq->uq_key);
3504 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3506 rv1 = fueword32(&sem->_count, &count);
3507 if (rv == -1 || (rv == 0 && (rv1 == -1 || count != 0)) ||
3508 (rv == 1 && count1 == 0)) {
3509 umtxq_lock(&uq->uq_key);
3510 umtxq_unbusy(&uq->uq_key);
3512 umtxq_unlock(&uq->uq_key);
3514 rv = thread_check_susp(td, true);
3522 error = rv == -1 ? EFAULT : 0;
3525 umtxq_lock(&uq->uq_key);
3526 umtxq_unbusy(&uq->uq_key);
3528 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3530 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3534 /* A relative timeout cannot be restarted. */
3535 if (error == ERESTART && timeout != NULL &&
3536 (timeout->_flags & UMTX_ABSTIME) == 0)
3539 umtxq_unlock(&uq->uq_key);
3541 umtx_key_release(&uq->uq_key);
3546 * Signal a userland semaphore.
3549 do_sem_wake(struct thread *td, struct _usem *sem)
3551 struct umtx_key key;
3555 error = fueword32(&sem->_flags, &flags);
3558 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3562 cnt = umtxq_count(&key);
3565 * Check if count is greater than 0, this means the memory is
3566 * still being referenced by user code, so we can safely
3567 * update _has_waiters flag.
3571 error = suword32(&sem->_has_waiters, 0);
3576 umtxq_signal(&key, 1);
3580 umtx_key_release(&key);
3586 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3588 struct umtx_abs_timeout timo;
3590 uint32_t count, flags;
3594 flags = fuword32(&sem->_flags);
3595 if (timeout != NULL)
3596 umtx_abs_timeout_init2(&timo, timeout);
3599 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3602 umtxq_lock(&uq->uq_key);
3603 umtxq_busy(&uq->uq_key);
3605 umtxq_unlock(&uq->uq_key);
3606 rv = fueword32(&sem->_count, &count);
3608 umtxq_lock(&uq->uq_key);
3609 umtxq_unbusy(&uq->uq_key);
3611 umtxq_unlock(&uq->uq_key);
3612 umtx_key_release(&uq->uq_key);
3616 if (USEM_COUNT(count) != 0) {
3617 umtxq_lock(&uq->uq_key);
3618 umtxq_unbusy(&uq->uq_key);
3620 umtxq_unlock(&uq->uq_key);
3621 umtx_key_release(&uq->uq_key);
3624 if (count == USEM_HAS_WAITERS)
3626 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3629 umtxq_lock(&uq->uq_key);
3630 umtxq_unbusy(&uq->uq_key);
3632 umtxq_unlock(&uq->uq_key);
3633 umtx_key_release(&uq->uq_key);
3636 rv = thread_check_susp(td, true);
3641 umtxq_lock(&uq->uq_key);
3642 umtxq_unbusy(&uq->uq_key);
3644 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3646 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3650 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3651 /* A relative timeout cannot be restarted. */
3652 if (error == ERESTART)
3654 if (error == EINTR) {
3655 umtx_abs_timeout_update(&timo);
3656 timespecsub(&timo.end, &timo.cur,
3657 &timeout->_timeout);
3661 umtxq_unlock(&uq->uq_key);
3662 umtx_key_release(&uq->uq_key);
3667 * Signal a userland semaphore.
3670 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3672 struct umtx_key key;
3674 uint32_t count, flags;
3676 rv = fueword32(&sem->_flags, &flags);
3679 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3683 cnt = umtxq_count(&key);
3686 * If this was the last sleeping thread, clear the waiters
3691 rv = fueword32(&sem->_count, &count);
3692 while (rv != -1 && count & USEM_HAS_WAITERS) {
3693 rv = casueword32(&sem->_count, count, &count,
3694 count & ~USEM_HAS_WAITERS);
3696 rv = thread_check_susp(td, true);
3709 umtxq_signal(&key, 1);
3713 umtx_key_release(&key);
3717 #ifdef COMPAT_FREEBSD10
3719 freebsd10__umtx_lock(struct thread *td, struct freebsd10__umtx_lock_args *uap)
3721 return (do_lock_umtx(td, uap->umtx, td->td_tid, 0));
3725 freebsd10__umtx_unlock(struct thread *td,
3726 struct freebsd10__umtx_unlock_args *uap)
3728 return (do_unlock_umtx(td, uap->umtx, td->td_tid));
3733 umtx_copyin_timeout(const void *uaddr, struct timespec *tsp)
3737 error = copyin(uaddr, tsp, sizeof(*tsp));
3739 if (tsp->tv_sec < 0 ||
3740 tsp->tv_nsec >= 1000000000 ||
3748 umtx_copyin_umtx_time(const void *uaddr, size_t size, struct _umtx_time *tp)
3752 if (size <= sizeof(tp->_timeout)) {
3753 tp->_clockid = CLOCK_REALTIME;
3755 error = copyin(uaddr, &tp->_timeout, sizeof(tp->_timeout));
3757 error = copyin(uaddr, tp, sizeof(*tp));
3760 if (tp->_timeout.tv_sec < 0 ||
3761 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3767 umtx_copyin_robust_lists(const void *uaddr, size_t size,
3768 struct umtx_robust_lists_params *rb)
3771 if (size > sizeof(*rb))
3773 return (copyin(uaddr, rb, size));
3777 umtx_copyout_timeout(void *uaddr, size_t sz, struct timespec *tsp)
3781 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
3782 * and we're only called if sz >= sizeof(timespec) as supplied in the
3785 KASSERT(sz >= sizeof(*tsp),
3786 ("umtx_copyops specifies incorrect sizes"));
3788 return (copyout(tsp, uaddr, sizeof(*tsp)));
3791 #ifdef COMPAT_FREEBSD10
3793 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap,
3794 const struct umtx_copyops *ops)
3796 struct timespec *ts, timeout;
3799 /* Allow a null timespec (wait forever). */
3800 if (uap->uaddr2 == NULL)
3803 error = ops->copyin_timeout(uap->uaddr2, &timeout);
3808 #ifdef COMPAT_FREEBSD32
3810 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
3812 return (do_lock_umtx(td, uap->obj, uap->val, ts));
3816 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap,
3817 const struct umtx_copyops *ops)
3819 #ifdef COMPAT_FREEBSD32
3821 return (do_unlock_umtx32(td, uap->obj, uap->val));
3823 return (do_unlock_umtx(td, uap->obj, uap->val));
3825 #endif /* COMPAT_FREEBSD10 */
3827 #if !defined(COMPAT_FREEBSD10)
3829 __umtx_op_unimpl(struct thread *td __unused, struct _umtx_op_args *uap __unused,
3830 const struct umtx_copyops *ops __unused)
3832 return (EOPNOTSUPP);
3834 #endif /* COMPAT_FREEBSD10 */
3837 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap,
3838 const struct umtx_copyops *ops)
3840 struct _umtx_time timeout, *tm_p;
3843 if (uap->uaddr2 == NULL)
3846 error = ops->copyin_umtx_time(
3847 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3852 return (do_wait(td, uap->obj, uap->val, tm_p, ops->compat32, 0));
3856 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap,
3857 const struct umtx_copyops *ops)
3859 struct _umtx_time timeout, *tm_p;
3862 if (uap->uaddr2 == NULL)
3865 error = ops->copyin_umtx_time(
3866 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3871 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3875 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap,
3876 const struct umtx_copyops *ops)
3878 struct _umtx_time *tm_p, timeout;
3881 if (uap->uaddr2 == NULL)
3884 error = ops->copyin_umtx_time(
3885 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3890 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3894 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap,
3895 const struct umtx_copyops *ops __unused)
3898 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3901 #define BATCH_SIZE 128
3903 __umtx_op_nwake_private_native(struct thread *td, struct _umtx_op_args *uap)
3905 char *uaddrs[BATCH_SIZE], **upp;
3906 int count, error, i, pos, tocopy;
3908 upp = (char **)uap->obj;
3910 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3912 tocopy = MIN(count, BATCH_SIZE);
3913 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3916 for (i = 0; i < tocopy; ++i) {
3917 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3925 __umtx_op_nwake_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3927 uint32_t uaddrs[BATCH_SIZE], *upp;
3928 int count, error, i, pos, tocopy;
3930 upp = (uint32_t *)uap->obj;
3932 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3934 tocopy = MIN(count, BATCH_SIZE);
3935 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
3938 for (i = 0; i < tocopy; ++i) {
3939 kern_umtx_wake(td, (void *)(uintptr_t)uaddrs[i],
3948 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap,
3949 const struct umtx_copyops *ops)
3953 return (__umtx_op_nwake_private_compat32(td, uap));
3954 return (__umtx_op_nwake_private_native(td, uap));
3958 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap,
3959 const struct umtx_copyops *ops __unused)
3962 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3966 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap,
3967 const struct umtx_copyops *ops)
3969 struct _umtx_time *tm_p, timeout;
3972 /* Allow a null timespec (wait forever). */
3973 if (uap->uaddr2 == NULL)
3976 error = ops->copyin_umtx_time(
3977 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3982 return (do_lock_umutex(td, uap->obj, tm_p, 0));
3986 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap,
3987 const struct umtx_copyops *ops __unused)
3990 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
3994 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap,
3995 const struct umtx_copyops *ops)
3997 struct _umtx_time *tm_p, timeout;
4000 /* Allow a null timespec (wait forever). */
4001 if (uap->uaddr2 == NULL)
4004 error = ops->copyin_umtx_time(
4005 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4010 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
4014 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap,
4015 const struct umtx_copyops *ops __unused)
4018 return (do_wake_umutex(td, uap->obj));
4022 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap,
4023 const struct umtx_copyops *ops __unused)
4026 return (do_unlock_umutex(td, uap->obj, false));
4030 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap,
4031 const struct umtx_copyops *ops __unused)
4034 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
4038 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap,
4039 const struct umtx_copyops *ops)
4041 struct timespec *ts, timeout;
4044 /* Allow a null timespec (wait forever). */
4045 if (uap->uaddr2 == NULL)
4048 error = ops->copyin_timeout(uap->uaddr2, &timeout);
4053 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
4057 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap,
4058 const struct umtx_copyops *ops __unused)
4061 return (do_cv_signal(td, uap->obj));
4065 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap,
4066 const struct umtx_copyops *ops __unused)
4069 return (do_cv_broadcast(td, uap->obj));
4073 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap,
4074 const struct umtx_copyops *ops)
4076 struct _umtx_time timeout;
4079 /* Allow a null timespec (wait forever). */
4080 if (uap->uaddr2 == NULL) {
4081 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4083 error = ops->copyin_umtx_time(uap->uaddr2,
4084 (size_t)uap->uaddr1, &timeout);
4087 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4093 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap,
4094 const struct umtx_copyops *ops)
4096 struct _umtx_time timeout;
4099 /* Allow a null timespec (wait forever). */
4100 if (uap->uaddr2 == NULL) {
4101 error = do_rw_wrlock(td, uap->obj, 0);
4103 error = ops->copyin_umtx_time(uap->uaddr2,
4104 (size_t)uap->uaddr1, &timeout);
4108 error = do_rw_wrlock(td, uap->obj, &timeout);
4114 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap,
4115 const struct umtx_copyops *ops __unused)
4118 return (do_rw_unlock(td, uap->obj));
4121 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4123 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap,
4124 const struct umtx_copyops *ops)
4126 struct _umtx_time *tm_p, timeout;
4129 /* Allow a null timespec (wait forever). */
4130 if (uap->uaddr2 == NULL)
4133 error = ops->copyin_umtx_time(
4134 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4139 return (do_sem_wait(td, uap->obj, tm_p));
4143 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap,
4144 const struct umtx_copyops *ops __unused)
4147 return (do_sem_wake(td, uap->obj));
4152 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap,
4153 const struct umtx_copyops *ops __unused)
4156 return (do_wake2_umutex(td, uap->obj, uap->val));
4160 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap,
4161 const struct umtx_copyops *ops)
4163 struct _umtx_time *tm_p, timeout;
4167 /* Allow a null timespec (wait forever). */
4168 if (uap->uaddr2 == NULL) {
4172 uasize = (size_t)uap->uaddr1;
4173 error = ops->copyin_umtx_time(uap->uaddr2, uasize, &timeout);
4178 error = do_sem2_wait(td, uap->obj, tm_p);
4179 if (error == EINTR && uap->uaddr2 != NULL &&
4180 (timeout._flags & UMTX_ABSTIME) == 0 &&
4181 uasize >= ops->umtx_time_sz + ops->timespec_sz) {
4182 error = ops->copyout_timeout(
4183 (void *)((uintptr_t)uap->uaddr2 + ops->umtx_time_sz),
4184 uasize - ops->umtx_time_sz, &timeout._timeout);
4194 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap,
4195 const struct umtx_copyops *ops __unused)
4198 return (do_sem2_wake(td, uap->obj));
4201 #define USHM_OBJ_UMTX(o) \
4202 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
4204 #define USHMF_REG_LINKED 0x0001
4205 #define USHMF_OBJ_LINKED 0x0002
4206 struct umtx_shm_reg {
4207 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
4208 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
4209 struct umtx_key ushm_key;
4210 struct ucred *ushm_cred;
4211 struct shmfd *ushm_obj;
4216 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
4217 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
4219 static uma_zone_t umtx_shm_reg_zone;
4220 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
4221 static struct mtx umtx_shm_lock;
4222 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
4223 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
4225 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
4228 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
4230 struct umtx_shm_reg_head d;
4231 struct umtx_shm_reg *reg, *reg1;
4234 mtx_lock(&umtx_shm_lock);
4235 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
4236 mtx_unlock(&umtx_shm_lock);
4237 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
4238 TAILQ_REMOVE(&d, reg, ushm_reg_link);
4239 umtx_shm_free_reg(reg);
4243 static struct task umtx_shm_reg_delfree_task =
4244 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
4246 static struct umtx_shm_reg *
4247 umtx_shm_find_reg_locked(const struct umtx_key *key)
4249 struct umtx_shm_reg *reg;
4250 struct umtx_shm_reg_head *reg_head;
4252 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
4253 mtx_assert(&umtx_shm_lock, MA_OWNED);
4254 reg_head = &umtx_shm_registry[key->hash];
4255 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
4256 KASSERT(reg->ushm_key.shared,
4257 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
4258 if (reg->ushm_key.info.shared.object ==
4259 key->info.shared.object &&
4260 reg->ushm_key.info.shared.offset ==
4261 key->info.shared.offset) {
4262 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
4263 KASSERT(reg->ushm_refcnt > 0,
4264 ("reg %p refcnt 0 onlist", reg));
4265 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
4266 ("reg %p not linked", reg));
4274 static struct umtx_shm_reg *
4275 umtx_shm_find_reg(const struct umtx_key *key)
4277 struct umtx_shm_reg *reg;
4279 mtx_lock(&umtx_shm_lock);
4280 reg = umtx_shm_find_reg_locked(key);
4281 mtx_unlock(&umtx_shm_lock);
4286 umtx_shm_free_reg(struct umtx_shm_reg *reg)
4289 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
4290 crfree(reg->ushm_cred);
4291 shm_drop(reg->ushm_obj);
4292 uma_zfree(umtx_shm_reg_zone, reg);
4296 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
4300 mtx_assert(&umtx_shm_lock, MA_OWNED);
4301 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
4303 res = reg->ushm_refcnt == 0;
4305 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
4306 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
4307 reg, ushm_reg_link);
4308 reg->ushm_flags &= ~USHMF_REG_LINKED;
4310 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
4311 LIST_REMOVE(reg, ushm_obj_link);
4312 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
4319 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
4325 object = reg->ushm_obj->shm_object;
4326 VM_OBJECT_WLOCK(object);
4327 object->flags |= OBJ_UMTXDEAD;
4328 VM_OBJECT_WUNLOCK(object);
4330 mtx_lock(&umtx_shm_lock);
4331 dofree = umtx_shm_unref_reg_locked(reg, force);
4332 mtx_unlock(&umtx_shm_lock);
4334 umtx_shm_free_reg(reg);
4338 umtx_shm_object_init(vm_object_t object)
4341 LIST_INIT(USHM_OBJ_UMTX(object));
4345 umtx_shm_object_terminated(vm_object_t object)
4347 struct umtx_shm_reg *reg, *reg1;
4350 if (LIST_EMPTY(USHM_OBJ_UMTX(object)))
4354 mtx_lock(&umtx_shm_lock);
4355 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
4356 if (umtx_shm_unref_reg_locked(reg, true)) {
4357 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
4362 mtx_unlock(&umtx_shm_lock);
4364 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
4368 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
4369 struct umtx_shm_reg **res)
4371 struct umtx_shm_reg *reg, *reg1;
4375 reg = umtx_shm_find_reg(key);
4380 cred = td->td_ucred;
4381 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
4383 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
4384 reg->ushm_refcnt = 1;
4385 bcopy(key, ®->ushm_key, sizeof(*key));
4386 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR, false);
4387 reg->ushm_cred = crhold(cred);
4388 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
4390 umtx_shm_free_reg(reg);
4393 mtx_lock(&umtx_shm_lock);
4394 reg1 = umtx_shm_find_reg_locked(key);
4396 mtx_unlock(&umtx_shm_lock);
4397 umtx_shm_free_reg(reg);
4402 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
4403 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
4405 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
4406 mtx_unlock(&umtx_shm_lock);
4412 umtx_shm_alive(struct thread *td, void *addr)
4415 vm_map_entry_t entry;
4422 map = &td->td_proc->p_vmspace->vm_map;
4423 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
4424 &object, &pindex, &prot, &wired);
4425 if (res != KERN_SUCCESS)
4430 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
4431 vm_map_lookup_done(map, entry);
4440 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
4441 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
4442 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
4443 for (i = 0; i < nitems(umtx_shm_registry); i++)
4444 TAILQ_INIT(&umtx_shm_registry[i]);
4448 umtx_shm(struct thread *td, void *addr, u_int flags)
4450 struct umtx_key key;
4451 struct umtx_shm_reg *reg;
4455 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
4456 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
4458 if ((flags & UMTX_SHM_ALIVE) != 0)
4459 return (umtx_shm_alive(td, addr));
4460 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
4463 KASSERT(key.shared == 1, ("non-shared key"));
4464 if ((flags & UMTX_SHM_CREAT) != 0) {
4465 error = umtx_shm_create_reg(td, &key, ®);
4467 reg = umtx_shm_find_reg(&key);
4471 umtx_key_release(&key);
4474 KASSERT(reg != NULL, ("no reg"));
4475 if ((flags & UMTX_SHM_DESTROY) != 0) {
4476 umtx_shm_unref_reg(reg, true);
4480 error = mac_posixshm_check_open(td->td_ucred,
4481 reg->ushm_obj, FFLAGS(O_RDWR));
4484 error = shm_access(reg->ushm_obj, td->td_ucred,
4488 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
4490 shm_hold(reg->ushm_obj);
4491 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
4493 td->td_retval[0] = fd;
4497 umtx_shm_unref_reg(reg, false);
4502 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap,
4503 const struct umtx_copyops *ops __unused)
4506 return (umtx_shm(td, uap->uaddr1, uap->val));
4510 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap,
4511 const struct umtx_copyops *ops)
4513 struct umtx_robust_lists_params rb;
4516 if (ops->compat32) {
4517 if ((td->td_pflags2 & TDP2_COMPAT32RB) == 0 &&
4518 (td->td_rb_list != 0 || td->td_rbp_list != 0 ||
4519 td->td_rb_inact != 0))
4521 } else if ((td->td_pflags2 & TDP2_COMPAT32RB) != 0) {
4525 bzero(&rb, sizeof(rb));
4526 error = ops->copyin_robust_lists(uap->uaddr1, uap->val, &rb);
4531 td->td_pflags2 |= TDP2_COMPAT32RB;
4533 td->td_rb_list = rb.robust_list_offset;
4534 td->td_rbp_list = rb.robust_priv_list_offset;
4535 td->td_rb_inact = rb.robust_inact_offset;
4539 #if defined(__i386__) || defined(__amd64__)
4541 * Provide the standard 32-bit definitions for x86, since native/compat32 use a
4542 * 32-bit time_t there. Other architectures just need the i386 definitions
4543 * along with their standard compat32.
4545 struct timespecx32 {
4550 struct umtx_timex32 {
4551 struct timespecx32 _timeout;
4557 #define timespeci386 timespec32
4558 #define umtx_timei386 umtx_time32
4560 #else /* !__i386__ && !__amd64__ */
4561 /* 32-bit architectures can emulate i386, so define these almost everywhere. */
4562 struct timespeci386 {
4567 struct umtx_timei386 {
4568 struct timespeci386 _timeout;
4573 #if defined(__LP64__)
4574 #define timespecx32 timespec32
4575 #define umtx_timex32 umtx_time32
4580 umtx_copyin_robust_lists32(const void *uaddr, size_t size,
4581 struct umtx_robust_lists_params *rbp)
4583 struct umtx_robust_lists_params_compat32 rb32;
4586 if (size > sizeof(rb32))
4588 bzero(&rb32, sizeof(rb32));
4589 error = copyin(uaddr, &rb32, size);
4592 CP(rb32, *rbp, robust_list_offset);
4593 CP(rb32, *rbp, robust_priv_list_offset);
4594 CP(rb32, *rbp, robust_inact_offset);
4600 umtx_copyin_timeouti386(const void *uaddr, struct timespec *tsp)
4602 struct timespeci386 ts32;
4605 error = copyin(uaddr, &ts32, sizeof(ts32));
4607 if (ts32.tv_sec < 0 ||
4608 ts32.tv_nsec >= 1000000000 ||
4612 CP(ts32, *tsp, tv_sec);
4613 CP(ts32, *tsp, tv_nsec);
4620 umtx_copyin_umtx_timei386(const void *uaddr, size_t size, struct _umtx_time *tp)
4622 struct umtx_timei386 t32;
4625 t32._clockid = CLOCK_REALTIME;
4627 if (size <= sizeof(t32._timeout))
4628 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4630 error = copyin(uaddr, &t32, sizeof(t32));
4633 if (t32._timeout.tv_sec < 0 ||
4634 t32._timeout.tv_nsec >= 1000000000 || t32._timeout.tv_nsec < 0)
4636 TS_CP(t32, *tp, _timeout);
4637 CP(t32, *tp, _flags);
4638 CP(t32, *tp, _clockid);
4643 umtx_copyout_timeouti386(void *uaddr, size_t sz, struct timespec *tsp)
4645 struct timespeci386 remain32 = {
4646 .tv_sec = tsp->tv_sec,
4647 .tv_nsec = tsp->tv_nsec,
4651 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4652 * and we're only called if sz >= sizeof(timespec) as supplied in the
4655 KASSERT(sz >= sizeof(remain32),
4656 ("umtx_copyops specifies incorrect sizes"));
4658 return (copyout(&remain32, uaddr, sizeof(remain32)));
4660 #endif /* !__i386__ */
4662 #if defined(__i386__) || defined(__LP64__)
4664 umtx_copyin_timeoutx32(const void *uaddr, struct timespec *tsp)
4666 struct timespecx32 ts32;
4669 error = copyin(uaddr, &ts32, sizeof(ts32));
4671 if (ts32.tv_sec < 0 ||
4672 ts32.tv_nsec >= 1000000000 ||
4676 CP(ts32, *tsp, tv_sec);
4677 CP(ts32, *tsp, tv_nsec);
4684 umtx_copyin_umtx_timex32(const void *uaddr, size_t size, struct _umtx_time *tp)
4686 struct umtx_timex32 t32;
4689 t32._clockid = CLOCK_REALTIME;
4691 if (size <= sizeof(t32._timeout))
4692 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4694 error = copyin(uaddr, &t32, sizeof(t32));
4697 if (t32._timeout.tv_sec < 0 ||
4698 t32._timeout.tv_nsec >= 1000000000 || t32._timeout.tv_nsec < 0)
4700 TS_CP(t32, *tp, _timeout);
4701 CP(t32, *tp, _flags);
4702 CP(t32, *tp, _clockid);
4707 umtx_copyout_timeoutx32(void *uaddr, size_t sz, struct timespec *tsp)
4709 struct timespecx32 remain32 = {
4710 .tv_sec = tsp->tv_sec,
4711 .tv_nsec = tsp->tv_nsec,
4715 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4716 * and we're only called if sz >= sizeof(timespec) as supplied in the
4719 KASSERT(sz >= sizeof(remain32),
4720 ("umtx_copyops specifies incorrect sizes"));
4722 return (copyout(&remain32, uaddr, sizeof(remain32)));
4724 #endif /* __i386__ || __LP64__ */
4726 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap,
4727 const struct umtx_copyops *umtx_ops);
4729 static const _umtx_op_func op_table[] = {
4730 #ifdef COMPAT_FREEBSD10
4731 [UMTX_OP_LOCK] = __umtx_op_lock_umtx,
4732 [UMTX_OP_UNLOCK] = __umtx_op_unlock_umtx,
4734 [UMTX_OP_LOCK] = __umtx_op_unimpl,
4735 [UMTX_OP_UNLOCK] = __umtx_op_unimpl,
4737 [UMTX_OP_WAIT] = __umtx_op_wait,
4738 [UMTX_OP_WAKE] = __umtx_op_wake,
4739 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4740 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
4741 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4742 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4743 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
4744 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4745 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4746 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
4747 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
4748 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
4749 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4750 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4751 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4752 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4753 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4754 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4755 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4756 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4758 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4759 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4761 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4762 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4763 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4764 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4765 [UMTX_OP_SHM] = __umtx_op_shm,
4766 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4769 static const struct umtx_copyops umtx_native_ops = {
4770 .copyin_timeout = umtx_copyin_timeout,
4771 .copyin_umtx_time = umtx_copyin_umtx_time,
4772 .copyin_robust_lists = umtx_copyin_robust_lists,
4773 .copyout_timeout = umtx_copyout_timeout,
4774 .timespec_sz = sizeof(struct timespec),
4775 .umtx_time_sz = sizeof(struct _umtx_time),
4779 static const struct umtx_copyops umtx_native_opsi386 = {
4780 .copyin_timeout = umtx_copyin_timeouti386,
4781 .copyin_umtx_time = umtx_copyin_umtx_timei386,
4782 .copyin_robust_lists = umtx_copyin_robust_lists32,
4783 .copyout_timeout = umtx_copyout_timeouti386,
4784 .timespec_sz = sizeof(struct timespeci386),
4785 .umtx_time_sz = sizeof(struct umtx_timei386),
4790 #if defined(__i386__) || defined(__LP64__)
4791 /* i386 can emulate other 32-bit archs, too! */
4792 static const struct umtx_copyops umtx_native_opsx32 = {
4793 .copyin_timeout = umtx_copyin_timeoutx32,
4794 .copyin_umtx_time = umtx_copyin_umtx_timex32,
4795 .copyin_robust_lists = umtx_copyin_robust_lists32,
4796 .copyout_timeout = umtx_copyout_timeoutx32,
4797 .timespec_sz = sizeof(struct timespecx32),
4798 .umtx_time_sz = sizeof(struct umtx_timex32),
4802 #ifdef COMPAT_FREEBSD32
4804 #define umtx_native_ops32 umtx_native_opsi386
4806 #define umtx_native_ops32 umtx_native_opsx32
4808 #endif /* COMPAT_FREEBSD32 */
4809 #endif /* __i386__ || __LP64__ */
4811 #define UMTX_OP__FLAGS (UMTX_OP__32BIT | UMTX_OP__I386)
4814 kern__umtx_op(struct thread *td, void *obj, int op, unsigned long val,
4815 void *uaddr1, void *uaddr2, const struct umtx_copyops *ops)
4817 struct _umtx_op_args uap = {
4819 .op = op & ~UMTX_OP__FLAGS,
4825 if ((uap.op >= nitems(op_table)))
4827 return ((*op_table[uap.op])(td, &uap, ops));
4831 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4833 static const struct umtx_copyops *umtx_ops;
4835 umtx_ops = &umtx_native_ops;
4837 if ((uap->op & (UMTX_OP__32BIT | UMTX_OP__I386)) != 0) {
4838 if ((uap->op & UMTX_OP__I386) != 0)
4839 umtx_ops = &umtx_native_opsi386;
4841 umtx_ops = &umtx_native_opsx32;
4843 #elif !defined(__i386__)
4844 /* We consider UMTX_OP__32BIT a nop on !i386 ILP32. */
4845 if ((uap->op & UMTX_OP__I386) != 0)
4846 umtx_ops = &umtx_native_opsi386;
4848 /* Likewise, UMTX_OP__I386 is a nop on i386. */
4849 if ((uap->op & UMTX_OP__32BIT) != 0)
4850 umtx_ops = &umtx_native_opsx32;
4852 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr1,
4853 uap->uaddr2, umtx_ops));
4856 #ifdef COMPAT_FREEBSD32
4857 #ifdef COMPAT_FREEBSD10
4859 freebsd10_freebsd32__umtx_lock(struct thread *td,
4860 struct freebsd10_freebsd32__umtx_lock_args *uap)
4862 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
4866 freebsd10_freebsd32__umtx_unlock(struct thread *td,
4867 struct freebsd10_freebsd32__umtx_unlock_args *uap)
4869 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
4871 #endif /* COMPAT_FREEBSD10 */
4874 freebsd32__umtx_op(struct thread *td, struct freebsd32__umtx_op_args *uap)
4877 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr1,
4878 uap->uaddr2, &umtx_native_ops32));
4880 #endif /* COMPAT_FREEBSD32 */
4883 umtx_thread_init(struct thread *td)
4886 td->td_umtxq = umtxq_alloc();
4887 td->td_umtxq->uq_thread = td;
4891 umtx_thread_fini(struct thread *td)
4894 umtxq_free(td->td_umtxq);
4898 * It will be called when new thread is created, e.g fork().
4901 umtx_thread_alloc(struct thread *td)
4906 uq->uq_inherited_pri = PRI_MAX;
4908 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4909 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4910 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4911 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4917 * Clear robust lists for all process' threads, not delaying the
4918 * cleanup to thread exit, since the relevant address space is
4919 * destroyed right now.
4922 umtx_exec(struct proc *p)
4926 KASSERT(p == curproc, ("need curproc"));
4927 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4928 (p->p_flag & P_STOPPED_SINGLE) != 0,
4929 ("curproc must be single-threaded"));
4931 * There is no need to lock the list as only this thread can be
4934 FOREACH_THREAD_IN_PROC(p, td) {
4935 KASSERT(td == curthread ||
4936 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4937 ("running thread %p %p", p, td));
4938 umtx_thread_cleanup(td);
4939 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4947 umtx_thread_exit(struct thread *td)
4950 umtx_thread_cleanup(td);
4954 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res, bool compat32)
4961 error = fueword32((void *)ptr, &res32);
4965 error = fueword((void *)ptr, &res1);
4975 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list,
4978 struct umutex32 m32;
4981 memcpy(&m32, m, sizeof(m32));
4982 *rb_list = m32.m_rb_lnk;
4984 *rb_list = m->m_rb_lnk;
4989 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact,
4995 KASSERT(td->td_proc == curproc, ("need current vmspace"));
4996 error = copyin((void *)rbp, &m, sizeof(m));
4999 if (rb_list != NULL)
5000 umtx_read_rb_list(td, &m, rb_list, compat32);
5001 if ((m.m_flags & UMUTEX_ROBUST) == 0)
5003 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
5004 /* inact is cleared after unlock, allow the inconsistency */
5005 return (inact ? 0 : EINVAL);
5006 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
5010 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
5011 const char *name, bool compat32)
5019 error = umtx_read_uptr(td, rb_list, &rbp, compat32);
5020 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
5021 if (rbp == *rb_inact) {
5026 error = umtx_handle_rb(td, rbp, &rbp, inact, compat32);
5028 if (i == umtx_max_rb && umtx_verbose_rb) {
5029 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
5030 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
5032 if (error != 0 && umtx_verbose_rb) {
5033 uprintf("comm %s pid %d: handling %srb error %d\n",
5034 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
5039 * Clean up umtx data.
5042 umtx_thread_cleanup(struct thread *td)
5050 * Disown pi mutexes.
5054 if (uq->uq_inherited_pri != PRI_MAX ||
5055 !TAILQ_EMPTY(&uq->uq_pi_contested)) {
5056 mtx_lock(&umtx_lock);
5057 uq->uq_inherited_pri = PRI_MAX;
5058 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
5059 pi->pi_owner = NULL;
5060 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
5062 mtx_unlock(&umtx_lock);
5064 sched_lend_user_prio_cond(td, PRI_MAX);
5067 compat32 = (td->td_pflags2 & TDP2_COMPAT32RB) != 0;
5068 td->td_pflags2 &= ~TDP2_COMPAT32RB;
5070 if (td->td_rb_inact == 0 && td->td_rb_list == 0 && td->td_rbp_list == 0)
5074 * Handle terminated robust mutexes. Must be done after
5075 * robust pi disown, otherwise unlock could see unowned
5078 rb_inact = td->td_rb_inact;
5080 (void)umtx_read_uptr(td, rb_inact, &rb_inact, compat32);
5081 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "", compat32);
5082 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ", compat32);
5084 (void)umtx_handle_rb(td, rb_inact, NULL, true, compat32);