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)
95 * Don't propagate time-sharing priority, there is a security reason,
96 * a user can simply introduce PI-mutex, let thread A lock the mutex,
97 * and let another thread B block on the mutex, because B is
98 * sleeping, its priority will be boosted, this causes A's priority to
99 * be boosted via priority propagating too and will never be lowered even
100 * if it is using 100%CPU, this is unfair to other processes.
103 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
104 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
105 PRI_MAX_TIMESHARE : (td)->td_user_pri)
107 #define GOLDEN_RATIO_PRIME 2654404609U
109 #define UMTX_CHAINS 512
111 #define UMTX_SHIFTS (__WORD_BIT - 9)
113 #define GET_SHARE(flags) \
114 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
116 #define BUSY_SPINS 200
118 struct umtx_copyops {
119 int (*copyin_timeout)(const void *uaddr, struct timespec *tsp);
120 int (*copyin_umtx_time)(const void *uaddr, size_t size,
121 struct _umtx_time *tp);
122 int (*copyin_robust_lists)(const void *uaddr, size_t size,
123 struct umtx_robust_lists_params *rbp);
124 int (*copyout_timeout)(void *uaddr, size_t size,
125 struct timespec *tsp);
126 const size_t timespec_sz;
127 const size_t umtx_time_sz;
131 _Static_assert(sizeof(struct umutex) == sizeof(struct umutex32), "umutex32");
132 _Static_assert(__offsetof(struct umutex, m_spare[0]) ==
133 __offsetof(struct umutex32, m_spare[0]), "m_spare32");
135 int umtx_shm_vnobj_persistent = 0;
136 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_vnode_persistent, CTLFLAG_RWTUN,
137 &umtx_shm_vnobj_persistent, 0,
138 "False forces destruction of umtx attached to file, on last close");
139 static int umtx_max_rb = 1000;
140 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_max_robust, CTLFLAG_RWTUN,
142 "Maximum number of robust mutexes allowed for each thread");
144 static uma_zone_t umtx_pi_zone;
145 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
146 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
147 static int umtx_pi_allocated;
149 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
151 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
152 &umtx_pi_allocated, 0, "Allocated umtx_pi");
153 static int umtx_verbose_rb = 1;
154 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
158 #ifdef UMTX_PROFILING
159 static long max_length;
160 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
161 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
165 static inline void umtx_abs_timeout_init2(struct umtx_abs_timeout *timo,
166 const struct _umtx_time *umtxtime);
167 static int umtx_abs_timeout_gethz(struct umtx_abs_timeout *timo);
168 static inline void umtx_abs_timeout_update(struct umtx_abs_timeout *timo);
170 static void umtx_shm_init(void);
171 static void umtxq_sysinit(void *);
172 static void umtxq_hash(struct umtx_key *key);
173 static struct umtx_pi *umtx_pi_alloc(int);
174 static void umtx_pi_free(struct umtx_pi *pi);
175 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
177 static void umtx_thread_cleanup(struct thread *td);
178 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
180 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
182 static struct mtx umtx_lock;
184 #ifdef UMTX_PROFILING
186 umtx_init_profiling(void)
188 struct sysctl_oid *chain_oid;
192 for (i = 0; i < UMTX_CHAINS; ++i) {
193 snprintf(chain_name, sizeof(chain_name), "%d", i);
194 chain_oid = SYSCTL_ADD_NODE(NULL,
195 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
196 chain_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
198 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
199 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
200 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
201 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
206 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
210 struct umtxq_chain *uc;
211 u_int fract, i, j, tot, whole;
212 u_int sf0, sf1, sf2, sf3, sf4;
213 u_int si0, si1, si2, si3, si4;
214 u_int sw0, sw1, sw2, sw3, sw4;
216 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
217 for (i = 0; i < 2; i++) {
219 for (j = 0; j < UMTX_CHAINS; ++j) {
220 uc = &umtxq_chains[i][j];
221 mtx_lock(&uc->uc_lock);
222 tot += uc->max_length;
223 mtx_unlock(&uc->uc_lock);
226 sbuf_printf(&sb, "%u) Empty ", i);
228 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
229 si0 = si1 = si2 = si3 = si4 = 0;
230 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
231 for (j = 0; j < UMTX_CHAINS; j++) {
232 uc = &umtxq_chains[i][j];
233 mtx_lock(&uc->uc_lock);
234 whole = uc->max_length * 100;
235 mtx_unlock(&uc->uc_lock);
236 fract = (whole % tot) * 100;
237 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
241 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
246 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
251 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
256 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
263 sbuf_printf(&sb, "queue %u:\n", i);
264 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
266 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
268 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
270 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
272 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
278 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
284 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
286 struct umtxq_chain *uc;
291 error = sysctl_handle_int(oidp, &clear, 0, req);
292 if (error != 0 || req->newptr == NULL)
296 for (i = 0; i < 2; ++i) {
297 for (j = 0; j < UMTX_CHAINS; ++j) {
298 uc = &umtxq_chains[i][j];
299 mtx_lock(&uc->uc_lock);
302 mtx_unlock(&uc->uc_lock);
309 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
310 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
311 sysctl_debug_umtx_chains_clear, "I",
312 "Clear umtx chains statistics");
313 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
314 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
315 sysctl_debug_umtx_chains_peaks, "A",
316 "Highest peaks in chains max length");
320 umtxq_sysinit(void *arg __unused)
324 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
325 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
326 for (i = 0; i < 2; ++i) {
327 for (j = 0; j < UMTX_CHAINS; ++j) {
328 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
329 MTX_DEF | MTX_DUPOK);
330 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
331 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
332 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
333 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
334 umtxq_chains[i][j].uc_busy = 0;
335 umtxq_chains[i][j].uc_waiters = 0;
336 #ifdef UMTX_PROFILING
337 umtxq_chains[i][j].length = 0;
338 umtxq_chains[i][j].max_length = 0;
342 #ifdef UMTX_PROFILING
343 umtx_init_profiling();
345 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
354 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
355 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
357 TAILQ_INIT(&uq->uq_spare_queue->head);
358 TAILQ_INIT(&uq->uq_pi_contested);
359 uq->uq_inherited_pri = PRI_MAX;
364 umtxq_free(struct umtx_q *uq)
367 MPASS(uq->uq_spare_queue != NULL);
368 free(uq->uq_spare_queue, M_UMTX);
373 umtxq_hash(struct umtx_key *key)
377 n = (uintptr_t)key->info.both.a + key->info.both.b;
378 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
382 umtxq_getchain(struct umtx_key *key)
385 if (key->type <= TYPE_SEM)
386 return (&umtxq_chains[1][key->hash]);
387 return (&umtxq_chains[0][key->hash]);
391 * Set chain to busy state when following operation
392 * may be blocked (kernel mutex can not be used).
395 umtxq_busy(struct umtx_key *key)
397 struct umtxq_chain *uc;
399 uc = umtxq_getchain(key);
400 mtx_assert(&uc->uc_lock, MA_OWNED);
404 int count = BUSY_SPINS;
407 while (uc->uc_busy && --count > 0)
413 while (uc->uc_busy) {
415 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
426 umtxq_unbusy(struct umtx_key *key)
428 struct umtxq_chain *uc;
430 uc = umtxq_getchain(key);
431 mtx_assert(&uc->uc_lock, MA_OWNED);
432 KASSERT(uc->uc_busy != 0, ("not busy"));
439 umtxq_unbusy_unlocked(struct umtx_key *key)
447 static struct umtxq_queue *
448 umtxq_queue_lookup(struct umtx_key *key, int q)
450 struct umtxq_queue *uh;
451 struct umtxq_chain *uc;
453 uc = umtxq_getchain(key);
454 UMTXQ_LOCKED_ASSERT(uc);
455 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
456 if (umtx_key_match(&uh->key, key))
464 umtxq_insert_queue(struct umtx_q *uq, int q)
466 struct umtxq_queue *uh;
467 struct umtxq_chain *uc;
469 uc = umtxq_getchain(&uq->uq_key);
470 UMTXQ_LOCKED_ASSERT(uc);
471 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
472 uh = umtxq_queue_lookup(&uq->uq_key, q);
474 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
476 uh = uq->uq_spare_queue;
477 uh->key = uq->uq_key;
478 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
479 #ifdef UMTX_PROFILING
481 if (uc->length > uc->max_length) {
482 uc->max_length = uc->length;
483 if (uc->max_length > max_length)
484 max_length = uc->max_length;
488 uq->uq_spare_queue = NULL;
490 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
492 uq->uq_flags |= UQF_UMTXQ;
493 uq->uq_cur_queue = uh;
498 umtxq_remove_queue(struct umtx_q *uq, int q)
500 struct umtxq_chain *uc;
501 struct umtxq_queue *uh;
503 uc = umtxq_getchain(&uq->uq_key);
504 UMTXQ_LOCKED_ASSERT(uc);
505 if (uq->uq_flags & UQF_UMTXQ) {
506 uh = uq->uq_cur_queue;
507 TAILQ_REMOVE(&uh->head, uq, uq_link);
509 uq->uq_flags &= ~UQF_UMTXQ;
510 if (TAILQ_EMPTY(&uh->head)) {
511 KASSERT(uh->length == 0,
512 ("inconsistent umtxq_queue length"));
513 #ifdef UMTX_PROFILING
516 LIST_REMOVE(uh, link);
518 uh = LIST_FIRST(&uc->uc_spare_queue);
519 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
520 LIST_REMOVE(uh, link);
522 uq->uq_spare_queue = uh;
523 uq->uq_cur_queue = NULL;
528 * Check if there are multiple waiters
531 umtxq_count(struct umtx_key *key)
533 struct umtxq_queue *uh;
535 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
536 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
543 * Check if there are multiple PI waiters and returns first
547 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
549 struct umtxq_queue *uh;
552 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
553 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
555 *first = TAILQ_FIRST(&uh->head);
562 * Wake up threads waiting on an userland object.
566 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
568 struct umtxq_queue *uh;
573 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
574 uh = umtxq_queue_lookup(key, q);
576 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
577 umtxq_remove_queue(uq, q);
587 * Wake up specified thread.
590 umtxq_signal_thread(struct umtx_q *uq)
593 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
599 tstohz(const struct timespec *tsp)
603 TIMESPEC_TO_TIMEVAL(&tv, tsp);
608 umtx_abs_timeout_init(struct umtx_abs_timeout *timo, int clockid,
609 int absolute, const struct timespec *timeout)
612 timo->clockid = clockid;
614 timo->is_abs_real = false;
615 umtx_abs_timeout_update(timo);
616 timespecadd(&timo->cur, timeout, &timo->end);
618 timo->end = *timeout;
619 timo->is_abs_real = clockid == CLOCK_REALTIME ||
620 clockid == CLOCK_REALTIME_FAST ||
621 clockid == CLOCK_REALTIME_PRECISE;
623 * If is_abs_real, umtxq_sleep will read the clock
624 * after setting td_rtcgen; otherwise, read it here.
626 if (!timo->is_abs_real) {
627 umtx_abs_timeout_update(timo);
633 umtx_abs_timeout_init2(struct umtx_abs_timeout *timo,
634 const struct _umtx_time *umtxtime)
637 umtx_abs_timeout_init(timo, umtxtime->_clockid,
638 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
642 umtx_abs_timeout_update(struct umtx_abs_timeout *timo)
645 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
649 umtx_abs_timeout_gethz(struct umtx_abs_timeout *timo)
653 if (timespeccmp(&timo->end, &timo->cur, <=))
655 timespecsub(&timo->end, &timo->cur, &tts);
656 return (tstohz(&tts));
660 umtx_unlock_val(uint32_t flags, bool rb)
664 return (UMUTEX_RB_OWNERDEAD);
665 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
666 return (UMUTEX_RB_NOTRECOV);
668 return (UMUTEX_UNOWNED);
673 * Put thread into sleep state, before sleeping, check if
674 * thread was removed from umtx queue.
677 umtxq_sleep(struct umtx_q *uq, const char *wmesg,
678 struct umtx_abs_timeout *abstime)
680 struct umtxq_chain *uc;
683 if (abstime != NULL && abstime->is_abs_real) {
684 curthread->td_rtcgen = atomic_load_acq_int(&rtc_generation);
685 umtx_abs_timeout_update(abstime);
688 uc = umtxq_getchain(&uq->uq_key);
689 UMTXQ_LOCKED_ASSERT(uc);
691 if (!(uq->uq_flags & UQF_UMTXQ)) {
695 if (abstime != NULL) {
696 timo = umtx_abs_timeout_gethz(abstime);
703 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
704 if (error == EINTR || error == ERESTART) {
705 umtxq_lock(&uq->uq_key);
708 if (abstime != NULL) {
709 if (abstime->is_abs_real)
710 curthread->td_rtcgen =
711 atomic_load_acq_int(&rtc_generation);
712 umtx_abs_timeout_update(abstime);
714 umtxq_lock(&uq->uq_key);
717 curthread->td_rtcgen = 0;
722 * Convert userspace address into unique logical address.
725 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
727 struct thread *td = curthread;
729 vm_map_entry_t entry;
735 if (share == THREAD_SHARE) {
737 key->info.private.vs = td->td_proc->p_vmspace;
738 key->info.private.addr = (uintptr_t)addr;
740 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
741 map = &td->td_proc->p_vmspace->vm_map;
742 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
743 &entry, &key->info.shared.object, &pindex, &prot,
744 &wired) != KERN_SUCCESS) {
748 if ((share == PROCESS_SHARE) ||
749 (share == AUTO_SHARE &&
750 VM_INHERIT_SHARE == entry->inheritance)) {
752 key->info.shared.offset = (vm_offset_t)addr -
753 entry->start + entry->offset;
754 vm_object_reference(key->info.shared.object);
757 key->info.private.vs = td->td_proc->p_vmspace;
758 key->info.private.addr = (uintptr_t)addr;
760 vm_map_lookup_done(map, entry);
771 umtx_key_release(struct umtx_key *key)
774 vm_object_deallocate(key->info.shared.object);
777 #ifdef COMPAT_FREEBSD10
779 * Lock a umtx object.
782 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
783 const struct timespec *timeout)
785 struct umtx_abs_timeout timo;
793 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
796 * Care must be exercised when dealing with umtx structure. It
797 * can fault on any access.
801 * Try the uncontested case. This should be done in userland.
803 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
805 /* The acquire succeeded. */
806 if (owner == UMTX_UNOWNED)
809 /* The address was invalid. */
813 /* If no one owns it but it is contested try to acquire it. */
814 if (owner == UMTX_CONTESTED) {
815 owner = casuword(&umtx->u_owner,
816 UMTX_CONTESTED, id | UMTX_CONTESTED);
818 if (owner == UMTX_CONTESTED)
821 /* The address was invalid. */
825 error = thread_check_susp(td, false);
829 /* If this failed the lock has changed, restart. */
834 * If we caught a signal, we have retried and now
840 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
841 AUTO_SHARE, &uq->uq_key)) != 0)
844 umtxq_lock(&uq->uq_key);
845 umtxq_busy(&uq->uq_key);
847 umtxq_unbusy(&uq->uq_key);
848 umtxq_unlock(&uq->uq_key);
851 * Set the contested bit so that a release in user space
852 * knows to use the system call for unlock. If this fails
853 * either some one else has acquired the lock or it has been
856 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
858 /* The address was invalid. */
860 umtxq_lock(&uq->uq_key);
862 umtxq_unlock(&uq->uq_key);
863 umtx_key_release(&uq->uq_key);
868 * We set the contested bit, sleep. Otherwise the lock changed
869 * and we need to retry or we lost a race to the thread
870 * unlocking the umtx.
872 umtxq_lock(&uq->uq_key);
874 error = umtxq_sleep(uq, "umtx", timeout == NULL ? NULL :
877 umtxq_unlock(&uq->uq_key);
878 umtx_key_release(&uq->uq_key);
881 error = thread_check_susp(td, false);
884 if (timeout == NULL) {
885 /* Mutex locking is restarted if it is interrupted. */
889 /* Timed-locking is not restarted. */
890 if (error == ERESTART)
897 * Unlock a umtx object.
900 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
909 * Make sure we own this mtx.
911 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
915 if ((owner & ~UMTX_CONTESTED) != id)
918 /* This should be done in userland */
919 if ((owner & UMTX_CONTESTED) == 0) {
920 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
928 /* We should only ever be in here for contested locks */
929 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
935 count = umtxq_count(&key);
939 * When unlocking the umtx, it must be marked as unowned if
940 * there is zero or one thread only waiting for it.
941 * Otherwise, it must be marked as contested.
943 old = casuword(&umtx->u_owner, owner,
944 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
946 umtxq_signal(&key,1);
949 umtx_key_release(&key);
957 #ifdef COMPAT_FREEBSD32
960 * Lock a umtx object.
963 do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id,
964 const struct timespec *timeout)
966 struct umtx_abs_timeout timo;
975 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
978 * Care must be exercised when dealing with umtx structure. It
979 * can fault on any access.
983 * Try the uncontested case. This should be done in userland.
985 owner = casuword32(m, UMUTEX_UNOWNED, id);
987 /* The acquire succeeded. */
988 if (owner == UMUTEX_UNOWNED)
991 /* The address was invalid. */
995 /* If no one owns it but it is contested try to acquire it. */
996 if (owner == UMUTEX_CONTESTED) {
997 owner = casuword32(m,
998 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
999 if (owner == UMUTEX_CONTESTED)
1002 /* The address was invalid. */
1006 error = thread_check_susp(td, false);
1010 /* If this failed the lock has changed, restart. */
1015 * If we caught a signal, we have retried and now
1021 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
1022 AUTO_SHARE, &uq->uq_key)) != 0)
1025 umtxq_lock(&uq->uq_key);
1026 umtxq_busy(&uq->uq_key);
1028 umtxq_unbusy(&uq->uq_key);
1029 umtxq_unlock(&uq->uq_key);
1032 * Set the contested bit so that a release in user space
1033 * knows to use the system call for unlock. If this fails
1034 * either some one else has acquired the lock or it has been
1037 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
1039 /* The address was invalid. */
1041 umtxq_lock(&uq->uq_key);
1043 umtxq_unlock(&uq->uq_key);
1044 umtx_key_release(&uq->uq_key);
1049 * We set the contested bit, sleep. Otherwise the lock changed
1050 * and we need to retry or we lost a race to the thread
1051 * unlocking the umtx.
1053 umtxq_lock(&uq->uq_key);
1055 error = umtxq_sleep(uq, "umtx", timeout == NULL ?
1058 umtxq_unlock(&uq->uq_key);
1059 umtx_key_release(&uq->uq_key);
1062 error = thread_check_susp(td, false);
1065 if (timeout == NULL) {
1066 /* Mutex locking is restarted if it is interrupted. */
1070 /* Timed-locking is not restarted. */
1071 if (error == ERESTART)
1078 * Unlock a umtx object.
1081 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
1083 struct umtx_key key;
1090 * Make sure we own this mtx.
1092 owner = fuword32(m);
1096 if ((owner & ~UMUTEX_CONTESTED) != id)
1099 /* This should be done in userland */
1100 if ((owner & UMUTEX_CONTESTED) == 0) {
1101 old = casuword32(m, owner, UMUTEX_UNOWNED);
1109 /* We should only ever be in here for contested locks */
1110 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1116 count = umtxq_count(&key);
1120 * When unlocking the umtx, it must be marked as unowned if
1121 * there is zero or one thread only waiting for it.
1122 * Otherwise, it must be marked as contested.
1124 old = casuword32(m, owner,
1125 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1127 umtxq_signal(&key,1);
1130 umtx_key_release(&key);
1137 #endif /* COMPAT_FREEBSD32 */
1138 #endif /* COMPAT_FREEBSD10 */
1141 * Fetch and compare value, sleep on the address if value is not changed.
1144 do_wait(struct thread *td, void *addr, u_long id,
1145 struct _umtx_time *timeout, int compat32, int is_private)
1147 struct umtx_abs_timeout timo;
1154 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
1155 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
1158 if (timeout != NULL)
1159 umtx_abs_timeout_init2(&timo, timeout);
1161 umtxq_lock(&uq->uq_key);
1163 umtxq_unlock(&uq->uq_key);
1164 if (compat32 == 0) {
1165 error = fueword(addr, &tmp);
1169 error = fueword32(addr, &tmp32);
1175 umtxq_lock(&uq->uq_key);
1178 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
1180 if ((uq->uq_flags & UQF_UMTXQ) == 0)
1184 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
1187 umtxq_unlock(&uq->uq_key);
1188 umtx_key_release(&uq->uq_key);
1189 if (error == ERESTART)
1195 * Wake up threads sleeping on the specified address.
1198 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1200 struct umtx_key key;
1203 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1204 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1207 umtxq_signal(&key, n_wake);
1209 umtx_key_release(&key);
1214 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1217 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1218 struct _umtx_time *timeout, int mode)
1220 struct umtx_abs_timeout timo;
1222 uint32_t owner, old, id;
1228 if (timeout != NULL)
1229 umtx_abs_timeout_init2(&timo, timeout);
1232 * Care must be exercised when dealing with umtx structure. It
1233 * can fault on any access.
1236 rv = fueword32(&m->m_owner, &owner);
1239 if (mode == _UMUTEX_WAIT) {
1240 if (owner == UMUTEX_UNOWNED ||
1241 owner == UMUTEX_CONTESTED ||
1242 owner == UMUTEX_RB_OWNERDEAD ||
1243 owner == UMUTEX_RB_NOTRECOV)
1247 * Robust mutex terminated. Kernel duty is to
1248 * return EOWNERDEAD to the userspace. The
1249 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1250 * by the common userspace code.
1252 if (owner == UMUTEX_RB_OWNERDEAD) {
1253 rv = casueword32(&m->m_owner,
1254 UMUTEX_RB_OWNERDEAD, &owner,
1255 id | UMUTEX_CONTESTED);
1259 MPASS(owner == UMUTEX_RB_OWNERDEAD);
1260 return (EOWNERDEAD); /* success */
1263 rv = thread_check_susp(td, false);
1268 if (owner == UMUTEX_RB_NOTRECOV)
1269 return (ENOTRECOVERABLE);
1272 * Try the uncontested case. This should be
1275 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1277 /* The address was invalid. */
1281 /* The acquire succeeded. */
1283 MPASS(owner == UMUTEX_UNOWNED);
1288 * If no one owns it but it is contested try
1292 if (owner == UMUTEX_CONTESTED) {
1293 rv = casueword32(&m->m_owner,
1294 UMUTEX_CONTESTED, &owner,
1295 id | UMUTEX_CONTESTED);
1296 /* The address was invalid. */
1300 MPASS(owner == UMUTEX_CONTESTED);
1304 rv = thread_check_susp(td, false);
1310 * If this failed the lock has
1316 /* rv == 1 but not contested, likely store failure */
1317 rv = thread_check_susp(td, false);
1322 if (mode == _UMUTEX_TRY)
1326 * If we caught a signal, we have retried and now
1332 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1333 GET_SHARE(flags), &uq->uq_key)) != 0)
1336 umtxq_lock(&uq->uq_key);
1337 umtxq_busy(&uq->uq_key);
1339 umtxq_unlock(&uq->uq_key);
1342 * Set the contested bit so that a release in user space
1343 * knows to use the system call for unlock. If this fails
1344 * either some one else has acquired the lock or it has been
1347 rv = casueword32(&m->m_owner, owner, &old,
1348 owner | UMUTEX_CONTESTED);
1350 /* The address was invalid or casueword failed to store. */
1351 if (rv == -1 || rv == 1) {
1352 umtxq_lock(&uq->uq_key);
1354 umtxq_unbusy(&uq->uq_key);
1355 umtxq_unlock(&uq->uq_key);
1356 umtx_key_release(&uq->uq_key);
1360 rv = thread_check_susp(td, false);
1368 * We set the contested bit, sleep. Otherwise the lock changed
1369 * and we need to retry or we lost a race to the thread
1370 * unlocking the umtx.
1372 umtxq_lock(&uq->uq_key);
1373 umtxq_unbusy(&uq->uq_key);
1374 MPASS(old == owner);
1375 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1378 umtxq_unlock(&uq->uq_key);
1379 umtx_key_release(&uq->uq_key);
1382 error = thread_check_susp(td, false);
1389 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1392 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1394 struct umtx_key key;
1395 uint32_t owner, old, id, newlock;
1402 * Make sure we own this mtx.
1404 error = fueword32(&m->m_owner, &owner);
1408 if ((owner & ~UMUTEX_CONTESTED) != id)
1411 newlock = umtx_unlock_val(flags, rb);
1412 if ((owner & UMUTEX_CONTESTED) == 0) {
1413 error = casueword32(&m->m_owner, owner, &old, newlock);
1417 error = thread_check_susp(td, false);
1422 MPASS(old == owner);
1426 /* We should only ever be in here for contested locks */
1427 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1433 count = umtxq_count(&key);
1437 * When unlocking the umtx, it must be marked as unowned if
1438 * there is zero or one thread only waiting for it.
1439 * Otherwise, it must be marked as contested.
1442 newlock |= UMUTEX_CONTESTED;
1443 error = casueword32(&m->m_owner, owner, &old, newlock);
1445 umtxq_signal(&key, 1);
1448 umtx_key_release(&key);
1454 error = thread_check_susp(td, false);
1463 * Check if the mutex is available and wake up a waiter,
1464 * only for simple mutex.
1467 do_wake_umutex(struct thread *td, struct umutex *m)
1469 struct umtx_key key;
1476 error = fueword32(&m->m_owner, &owner);
1480 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1481 owner != UMUTEX_RB_NOTRECOV)
1484 error = fueword32(&m->m_flags, &flags);
1488 /* We should only ever be in here for contested locks */
1489 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1495 count = umtxq_count(&key);
1498 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1499 owner != UMUTEX_RB_NOTRECOV) {
1500 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1504 } else if (error == 1) {
1508 umtx_key_release(&key);
1509 error = thread_check_susp(td, false);
1517 if (error == 0 && count != 0) {
1518 MPASS((owner & ~UMUTEX_CONTESTED) == 0 ||
1519 owner == UMUTEX_RB_OWNERDEAD ||
1520 owner == UMUTEX_RB_NOTRECOV);
1521 umtxq_signal(&key, 1);
1525 umtx_key_release(&key);
1530 * Check if the mutex has waiters and tries to fix contention bit.
1533 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1535 struct umtx_key key;
1536 uint32_t owner, old;
1541 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1545 type = TYPE_NORMAL_UMUTEX;
1547 case UMUTEX_PRIO_INHERIT:
1548 type = TYPE_PI_UMUTEX;
1550 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1551 type = TYPE_PI_ROBUST_UMUTEX;
1553 case UMUTEX_PRIO_PROTECT:
1554 type = TYPE_PP_UMUTEX;
1556 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1557 type = TYPE_PP_ROBUST_UMUTEX;
1562 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1568 count = umtxq_count(&key);
1571 error = fueword32(&m->m_owner, &owner);
1576 * Only repair contention bit if there is a waiter, this means
1577 * the mutex is still being referenced by userland code,
1578 * otherwise don't update any memory.
1580 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0 &&
1581 (count > 1 || (count == 1 && (owner & ~UMUTEX_CONTESTED) != 0))) {
1582 error = casueword32(&m->m_owner, owner, &old,
1583 owner | UMUTEX_CONTESTED);
1589 MPASS(old == owner);
1593 error = thread_check_susp(td, false);
1597 if (error == EFAULT) {
1598 umtxq_signal(&key, INT_MAX);
1599 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1600 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1601 umtxq_signal(&key, 1);
1604 umtx_key_release(&key);
1608 static inline struct umtx_pi *
1609 umtx_pi_alloc(int flags)
1613 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1614 TAILQ_INIT(&pi->pi_blocked);
1615 atomic_add_int(&umtx_pi_allocated, 1);
1620 umtx_pi_free(struct umtx_pi *pi)
1622 uma_zfree(umtx_pi_zone, pi);
1623 atomic_add_int(&umtx_pi_allocated, -1);
1627 * Adjust the thread's position on a pi_state after its priority has been
1631 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1633 struct umtx_q *uq, *uq1, *uq2;
1636 mtx_assert(&umtx_lock, MA_OWNED);
1643 * Check if the thread needs to be moved on the blocked chain.
1644 * It needs to be moved if either its priority is lower than
1645 * the previous thread or higher than the next thread.
1647 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1648 uq2 = TAILQ_NEXT(uq, uq_lockq);
1649 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1650 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1652 * Remove thread from blocked chain and determine where
1653 * it should be moved to.
1655 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1656 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1657 td1 = uq1->uq_thread;
1658 MPASS(td1->td_proc->p_magic == P_MAGIC);
1659 if (UPRI(td1) > UPRI(td))
1664 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1666 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1671 static struct umtx_pi *
1672 umtx_pi_next(struct umtx_pi *pi)
1674 struct umtx_q *uq_owner;
1676 if (pi->pi_owner == NULL)
1678 uq_owner = pi->pi_owner->td_umtxq;
1679 if (uq_owner == NULL)
1681 return (uq_owner->uq_pi_blocked);
1685 * Floyd's Cycle-Finding Algorithm.
1688 umtx_pi_check_loop(struct umtx_pi *pi)
1690 struct umtx_pi *pi1; /* fast iterator */
1692 mtx_assert(&umtx_lock, MA_OWNED);
1697 pi = umtx_pi_next(pi);
1700 pi1 = umtx_pi_next(pi1);
1703 pi1 = umtx_pi_next(pi1);
1713 * Propagate priority when a thread is blocked on POSIX
1717 umtx_propagate_priority(struct thread *td)
1723 mtx_assert(&umtx_lock, MA_OWNED);
1726 pi = uq->uq_pi_blocked;
1729 if (umtx_pi_check_loop(pi))
1734 if (td == NULL || td == curthread)
1737 MPASS(td->td_proc != NULL);
1738 MPASS(td->td_proc->p_magic == P_MAGIC);
1741 if (td->td_lend_user_pri > pri)
1742 sched_lend_user_prio(td, pri);
1750 * Pick up the lock that td is blocked on.
1753 pi = uq->uq_pi_blocked;
1756 /* Resort td on the list if needed. */
1757 umtx_pi_adjust_thread(pi, td);
1762 * Unpropagate priority for a PI mutex when a thread blocked on
1763 * it is interrupted by signal or resumed by others.
1766 umtx_repropagate_priority(struct umtx_pi *pi)
1768 struct umtx_q *uq, *uq_owner;
1769 struct umtx_pi *pi2;
1772 mtx_assert(&umtx_lock, MA_OWNED);
1774 if (umtx_pi_check_loop(pi))
1776 while (pi != NULL && pi->pi_owner != NULL) {
1778 uq_owner = pi->pi_owner->td_umtxq;
1780 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1781 uq = TAILQ_FIRST(&pi2->pi_blocked);
1783 if (pri > UPRI(uq->uq_thread))
1784 pri = UPRI(uq->uq_thread);
1788 if (pri > uq_owner->uq_inherited_pri)
1789 pri = uq_owner->uq_inherited_pri;
1790 thread_lock(pi->pi_owner);
1791 sched_lend_user_prio(pi->pi_owner, pri);
1792 thread_unlock(pi->pi_owner);
1793 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1794 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1799 * Insert a PI mutex into owned list.
1802 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1804 struct umtx_q *uq_owner;
1806 uq_owner = owner->td_umtxq;
1807 mtx_assert(&umtx_lock, MA_OWNED);
1808 MPASS(pi->pi_owner == NULL);
1809 pi->pi_owner = owner;
1810 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1814 * Disown a PI mutex, and remove it from the owned list.
1817 umtx_pi_disown(struct umtx_pi *pi)
1820 mtx_assert(&umtx_lock, MA_OWNED);
1821 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1822 pi->pi_owner = NULL;
1826 * Claim ownership of a PI mutex.
1829 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1834 mtx_lock(&umtx_lock);
1835 if (pi->pi_owner == owner) {
1836 mtx_unlock(&umtx_lock);
1840 if (pi->pi_owner != NULL) {
1842 * userland may have already messed the mutex, sigh.
1844 mtx_unlock(&umtx_lock);
1847 umtx_pi_setowner(pi, owner);
1848 uq = TAILQ_FIRST(&pi->pi_blocked);
1850 pri = UPRI(uq->uq_thread);
1852 if (pri < UPRI(owner))
1853 sched_lend_user_prio(owner, pri);
1854 thread_unlock(owner);
1856 mtx_unlock(&umtx_lock);
1861 * Adjust a thread's order position in its blocked PI mutex,
1862 * this may result new priority propagating process.
1865 umtx_pi_adjust(struct thread *td, u_char oldpri)
1871 mtx_lock(&umtx_lock);
1873 * Pick up the lock that td is blocked on.
1875 pi = uq->uq_pi_blocked;
1877 umtx_pi_adjust_thread(pi, td);
1878 umtx_repropagate_priority(pi);
1880 mtx_unlock(&umtx_lock);
1884 * Sleep on a PI mutex.
1887 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
1888 const char *wmesg, struct umtx_abs_timeout *timo, bool shared)
1890 struct thread *td, *td1;
1894 struct umtxq_chain *uc;
1896 uc = umtxq_getchain(&pi->pi_key);
1900 KASSERT(td == curthread, ("inconsistent uq_thread"));
1901 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
1902 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1904 mtx_lock(&umtx_lock);
1905 if (pi->pi_owner == NULL) {
1906 mtx_unlock(&umtx_lock);
1907 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
1908 mtx_lock(&umtx_lock);
1910 if (pi->pi_owner == NULL)
1911 umtx_pi_setowner(pi, td1);
1912 PROC_UNLOCK(td1->td_proc);
1916 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1917 pri = UPRI(uq1->uq_thread);
1923 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1925 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1927 uq->uq_pi_blocked = pi;
1929 td->td_flags |= TDF_UPIBLOCKED;
1931 umtx_propagate_priority(td);
1932 mtx_unlock(&umtx_lock);
1933 umtxq_unbusy(&uq->uq_key);
1935 error = umtxq_sleep(uq, wmesg, timo);
1938 mtx_lock(&umtx_lock);
1939 uq->uq_pi_blocked = NULL;
1941 td->td_flags &= ~TDF_UPIBLOCKED;
1943 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1944 umtx_repropagate_priority(pi);
1945 mtx_unlock(&umtx_lock);
1946 umtxq_unlock(&uq->uq_key);
1952 * Add reference count for a PI mutex.
1955 umtx_pi_ref(struct umtx_pi *pi)
1958 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
1963 * Decrease reference count for a PI mutex, if the counter
1964 * is decreased to zero, its memory space is freed.
1967 umtx_pi_unref(struct umtx_pi *pi)
1969 struct umtxq_chain *uc;
1971 uc = umtxq_getchain(&pi->pi_key);
1972 UMTXQ_LOCKED_ASSERT(uc);
1973 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1974 if (--pi->pi_refcount == 0) {
1975 mtx_lock(&umtx_lock);
1976 if (pi->pi_owner != NULL)
1978 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1979 ("blocked queue not empty"));
1980 mtx_unlock(&umtx_lock);
1981 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1987 * Find a PI mutex in hash table.
1989 static struct umtx_pi *
1990 umtx_pi_lookup(struct umtx_key *key)
1992 struct umtxq_chain *uc;
1995 uc = umtxq_getchain(key);
1996 UMTXQ_LOCKED_ASSERT(uc);
1998 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1999 if (umtx_key_match(&pi->pi_key, key)) {
2007 * Insert a PI mutex into hash table.
2010 umtx_pi_insert(struct umtx_pi *pi)
2012 struct umtxq_chain *uc;
2014 uc = umtxq_getchain(&pi->pi_key);
2015 UMTXQ_LOCKED_ASSERT(uc);
2016 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
2023 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
2024 struct _umtx_time *timeout, int try)
2026 struct umtx_abs_timeout timo;
2028 struct umtx_pi *pi, *new_pi;
2029 uint32_t id, old_owner, owner, old;
2035 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2036 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2040 if (timeout != NULL)
2041 umtx_abs_timeout_init2(&timo, timeout);
2043 umtxq_lock(&uq->uq_key);
2044 pi = umtx_pi_lookup(&uq->uq_key);
2046 new_pi = umtx_pi_alloc(M_NOWAIT);
2047 if (new_pi == NULL) {
2048 umtxq_unlock(&uq->uq_key);
2049 new_pi = umtx_pi_alloc(M_WAITOK);
2050 umtxq_lock(&uq->uq_key);
2051 pi = umtx_pi_lookup(&uq->uq_key);
2053 umtx_pi_free(new_pi);
2057 if (new_pi != NULL) {
2058 new_pi->pi_key = uq->uq_key;
2059 umtx_pi_insert(new_pi);
2064 umtxq_unlock(&uq->uq_key);
2067 * Care must be exercised when dealing with umtx structure. It
2068 * can fault on any access.
2072 * Try the uncontested case. This should be done in userland.
2074 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
2075 /* The address was invalid. */
2080 /* The acquire succeeded. */
2082 MPASS(owner == UMUTEX_UNOWNED);
2087 if (owner == UMUTEX_RB_NOTRECOV) {
2088 error = ENOTRECOVERABLE;
2093 * Avoid overwriting a possible error from sleep due
2094 * to the pending signal with suspension check result.
2097 error = thread_check_susp(td, true);
2102 /* If no one owns it but it is contested try to acquire it. */
2103 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
2105 rv = casueword32(&m->m_owner, owner, &owner,
2106 id | UMUTEX_CONTESTED);
2107 /* The address was invalid. */
2114 error = thread_check_susp(td, true);
2120 * If this failed the lock could
2127 MPASS(owner == old_owner);
2128 umtxq_lock(&uq->uq_key);
2129 umtxq_busy(&uq->uq_key);
2130 error = umtx_pi_claim(pi, td);
2131 umtxq_unbusy(&uq->uq_key);
2132 umtxq_unlock(&uq->uq_key);
2135 * Since we're going to return an
2136 * error, restore the m_owner to its
2137 * previous, unowned state to avoid
2138 * compounding the problem.
2140 (void)casuword32(&m->m_owner,
2141 id | UMUTEX_CONTESTED, old_owner);
2143 if (error == 0 && old_owner == UMUTEX_RB_OWNERDEAD)
2148 if ((owner & ~UMUTEX_CONTESTED) == id) {
2159 * If we caught a signal, we have retried and now
2165 umtxq_lock(&uq->uq_key);
2166 umtxq_busy(&uq->uq_key);
2167 umtxq_unlock(&uq->uq_key);
2170 * Set the contested bit so that a release in user space
2171 * knows to use the system call for unlock. If this fails
2172 * either some one else has acquired the lock or it has been
2175 rv = casueword32(&m->m_owner, owner, &old, owner |
2178 /* The address was invalid. */
2180 umtxq_unbusy_unlocked(&uq->uq_key);
2185 umtxq_unbusy_unlocked(&uq->uq_key);
2186 error = thread_check_susp(td, true);
2191 * The lock changed and we need to retry or we
2192 * lost a race to the thread unlocking the
2193 * umtx. Note that the UMUTEX_RB_OWNERDEAD
2194 * value for owner is impossible there.
2199 umtxq_lock(&uq->uq_key);
2201 /* We set the contested bit, sleep. */
2202 MPASS(old == owner);
2203 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
2204 "umtxpi", timeout == NULL ? NULL : &timo,
2205 (flags & USYNC_PROCESS_SHARED) != 0);
2209 error = thread_check_susp(td, false);
2214 umtxq_lock(&uq->uq_key);
2216 umtxq_unlock(&uq->uq_key);
2218 umtx_key_release(&uq->uq_key);
2223 * Unlock a PI mutex.
2226 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2228 struct umtx_key key;
2229 struct umtx_q *uq_first, *uq_first2, *uq_me;
2230 struct umtx_pi *pi, *pi2;
2231 uint32_t id, new_owner, old, owner;
2232 int count, error, pri;
2238 * Make sure we own this mtx.
2240 error = fueword32(&m->m_owner, &owner);
2244 if ((owner & ~UMUTEX_CONTESTED) != id)
2247 new_owner = umtx_unlock_val(flags, rb);
2249 /* This should be done in userland */
2250 if ((owner & UMUTEX_CONTESTED) == 0) {
2251 error = casueword32(&m->m_owner, owner, &old, new_owner);
2255 error = thread_check_susp(td, true);
2265 /* We should only ever be in here for contested locks */
2266 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2267 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2273 count = umtxq_count_pi(&key, &uq_first);
2274 if (uq_first != NULL) {
2275 mtx_lock(&umtx_lock);
2276 pi = uq_first->uq_pi_blocked;
2277 KASSERT(pi != NULL, ("pi == NULL?"));
2278 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2279 mtx_unlock(&umtx_lock);
2282 umtx_key_release(&key);
2283 /* userland messed the mutex */
2286 uq_me = td->td_umtxq;
2287 if (pi->pi_owner == td)
2289 /* get highest priority thread which is still sleeping. */
2290 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2291 while (uq_first != NULL &&
2292 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2293 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2296 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2297 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2298 if (uq_first2 != NULL) {
2299 if (pri > UPRI(uq_first2->uq_thread))
2300 pri = UPRI(uq_first2->uq_thread);
2304 sched_lend_user_prio(td, pri);
2306 mtx_unlock(&umtx_lock);
2308 umtxq_signal_thread(uq_first);
2310 pi = umtx_pi_lookup(&key);
2312 * A umtx_pi can exist if a signal or timeout removed the
2313 * last waiter from the umtxq, but there is still
2314 * a thread in do_lock_pi() holding the umtx_pi.
2318 * The umtx_pi can be unowned, such as when a thread
2319 * has just entered do_lock_pi(), allocated the
2320 * umtx_pi, and unlocked the umtxq.
2321 * If the current thread owns it, it must disown it.
2323 mtx_lock(&umtx_lock);
2324 if (pi->pi_owner == td)
2326 mtx_unlock(&umtx_lock);
2332 * When unlocking the umtx, it must be marked as unowned if
2333 * there is zero or one thread only waiting for it.
2334 * Otherwise, it must be marked as contested.
2338 new_owner |= UMUTEX_CONTESTED;
2340 error = casueword32(&m->m_owner, owner, &old, new_owner);
2342 error = thread_check_susp(td, false);
2346 umtxq_unbusy_unlocked(&key);
2347 umtx_key_release(&key);
2350 if (error == 0 && old != owner)
2359 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2360 struct _umtx_time *timeout, int try)
2362 struct umtx_abs_timeout timo;
2363 struct umtx_q *uq, *uq2;
2367 int error, pri, old_inherited_pri, su, rv;
2371 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2372 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2376 if (timeout != NULL)
2377 umtx_abs_timeout_init2(&timo, timeout);
2379 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2381 old_inherited_pri = uq->uq_inherited_pri;
2382 umtxq_lock(&uq->uq_key);
2383 umtxq_busy(&uq->uq_key);
2384 umtxq_unlock(&uq->uq_key);
2386 rv = fueword32(&m->m_ceilings[0], &ceiling);
2391 ceiling = RTP_PRIO_MAX - ceiling;
2392 if (ceiling > RTP_PRIO_MAX) {
2397 mtx_lock(&umtx_lock);
2398 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2399 mtx_unlock(&umtx_lock);
2403 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2404 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2406 if (uq->uq_inherited_pri < UPRI(td))
2407 sched_lend_user_prio(td, uq->uq_inherited_pri);
2410 mtx_unlock(&umtx_lock);
2412 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2413 id | UMUTEX_CONTESTED);
2414 /* The address was invalid. */
2420 MPASS(owner == UMUTEX_CONTESTED);
2425 if (owner == UMUTEX_RB_OWNERDEAD) {
2426 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2427 &owner, id | UMUTEX_CONTESTED);
2433 MPASS(owner == UMUTEX_RB_OWNERDEAD);
2434 error = EOWNERDEAD; /* success */
2439 * rv == 1, only check for suspension if we
2440 * did not already catched a signal. If we
2441 * get an error from the check, the same
2442 * condition is checked by the umtxq_sleep()
2443 * call below, so we should obliterate the
2444 * error to not skip the last loop iteration.
2447 error = thread_check_susp(td, false);
2456 } else if (owner == UMUTEX_RB_NOTRECOV) {
2457 error = ENOTRECOVERABLE;
2464 * If we caught a signal, we have retried and now
2470 umtxq_lock(&uq->uq_key);
2472 umtxq_unbusy(&uq->uq_key);
2473 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2476 umtxq_unlock(&uq->uq_key);
2478 mtx_lock(&umtx_lock);
2479 uq->uq_inherited_pri = old_inherited_pri;
2481 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2482 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2484 if (pri > UPRI(uq2->uq_thread))
2485 pri = UPRI(uq2->uq_thread);
2488 if (pri > uq->uq_inherited_pri)
2489 pri = uq->uq_inherited_pri;
2491 sched_lend_user_prio(td, pri);
2493 mtx_unlock(&umtx_lock);
2496 if (error != 0 && error != EOWNERDEAD) {
2497 mtx_lock(&umtx_lock);
2498 uq->uq_inherited_pri = old_inherited_pri;
2500 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2501 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2503 if (pri > UPRI(uq2->uq_thread))
2504 pri = UPRI(uq2->uq_thread);
2507 if (pri > uq->uq_inherited_pri)
2508 pri = uq->uq_inherited_pri;
2510 sched_lend_user_prio(td, pri);
2512 mtx_unlock(&umtx_lock);
2516 umtxq_unbusy_unlocked(&uq->uq_key);
2517 umtx_key_release(&uq->uq_key);
2522 * Unlock a PP mutex.
2525 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2527 struct umtx_key key;
2528 struct umtx_q *uq, *uq2;
2530 uint32_t id, owner, rceiling;
2531 int error, pri, new_inherited_pri, su;
2535 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2538 * Make sure we own this mtx.
2540 error = fueword32(&m->m_owner, &owner);
2544 if ((owner & ~UMUTEX_CONTESTED) != id)
2547 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2552 new_inherited_pri = PRI_MAX;
2554 rceiling = RTP_PRIO_MAX - rceiling;
2555 if (rceiling > RTP_PRIO_MAX)
2557 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2560 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2561 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2568 * For priority protected mutex, always set unlocked state
2569 * to UMUTEX_CONTESTED, so that userland always enters kernel
2570 * to lock the mutex, it is necessary because thread priority
2571 * has to be adjusted for such mutex.
2573 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2578 umtxq_signal(&key, 1);
2585 mtx_lock(&umtx_lock);
2587 uq->uq_inherited_pri = new_inherited_pri;
2589 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2590 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2592 if (pri > UPRI(uq2->uq_thread))
2593 pri = UPRI(uq2->uq_thread);
2596 if (pri > uq->uq_inherited_pri)
2597 pri = uq->uq_inherited_pri;
2599 sched_lend_user_prio(td, pri);
2601 mtx_unlock(&umtx_lock);
2603 umtx_key_release(&key);
2608 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2609 uint32_t *old_ceiling)
2612 uint32_t flags, id, owner, save_ceiling;
2615 error = fueword32(&m->m_flags, &flags);
2618 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2620 if (ceiling > RTP_PRIO_MAX)
2624 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2625 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2629 umtxq_lock(&uq->uq_key);
2630 umtxq_busy(&uq->uq_key);
2631 umtxq_unlock(&uq->uq_key);
2633 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2639 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2640 id | UMUTEX_CONTESTED);
2647 MPASS(owner == UMUTEX_CONTESTED);
2648 rv = suword32(&m->m_ceilings[0], ceiling);
2649 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2650 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2654 if ((owner & ~UMUTEX_CONTESTED) == id) {
2655 rv = suword32(&m->m_ceilings[0], ceiling);
2656 error = rv == 0 ? 0 : EFAULT;
2660 if (owner == UMUTEX_RB_OWNERDEAD) {
2663 } else if (owner == UMUTEX_RB_NOTRECOV) {
2664 error = ENOTRECOVERABLE;
2669 * If we caught a signal, we have retried and now
2676 * We set the contested bit, sleep. Otherwise the lock changed
2677 * and we need to retry or we lost a race to the thread
2678 * unlocking the umtx.
2680 umtxq_lock(&uq->uq_key);
2682 umtxq_unbusy(&uq->uq_key);
2683 error = umtxq_sleep(uq, "umtxpp", NULL);
2685 umtxq_unlock(&uq->uq_key);
2687 umtxq_lock(&uq->uq_key);
2689 umtxq_signal(&uq->uq_key, INT_MAX);
2690 umtxq_unbusy(&uq->uq_key);
2691 umtxq_unlock(&uq->uq_key);
2692 umtx_key_release(&uq->uq_key);
2693 if (error == 0 && old_ceiling != NULL) {
2694 rv = suword32(old_ceiling, save_ceiling);
2695 error = rv == 0 ? 0 : EFAULT;
2701 * Lock a userland POSIX mutex.
2704 do_lock_umutex(struct thread *td, struct umutex *m,
2705 struct _umtx_time *timeout, int mode)
2710 error = fueword32(&m->m_flags, &flags);
2714 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2716 error = do_lock_normal(td, m, flags, timeout, mode);
2718 case UMUTEX_PRIO_INHERIT:
2719 error = do_lock_pi(td, m, flags, timeout, mode);
2721 case UMUTEX_PRIO_PROTECT:
2722 error = do_lock_pp(td, m, flags, timeout, mode);
2727 if (timeout == NULL) {
2728 if (error == EINTR && mode != _UMUTEX_WAIT)
2731 /* Timed-locking is not restarted. */
2732 if (error == ERESTART)
2739 * Unlock a userland POSIX mutex.
2742 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2747 error = fueword32(&m->m_flags, &flags);
2751 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2753 return (do_unlock_normal(td, m, flags, rb));
2754 case UMUTEX_PRIO_INHERIT:
2755 return (do_unlock_pi(td, m, flags, rb));
2756 case UMUTEX_PRIO_PROTECT:
2757 return (do_unlock_pp(td, m, flags, rb));
2764 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2765 struct timespec *timeout, u_long wflags)
2767 struct umtx_abs_timeout timo;
2769 uint32_t flags, clockid, hasw;
2773 error = fueword32(&cv->c_flags, &flags);
2776 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2780 if ((wflags & CVWAIT_CLOCKID) != 0) {
2781 error = fueword32(&cv->c_clockid, &clockid);
2783 umtx_key_release(&uq->uq_key);
2786 if (clockid < CLOCK_REALTIME ||
2787 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2788 /* hmm, only HW clock id will work. */
2789 umtx_key_release(&uq->uq_key);
2793 clockid = CLOCK_REALTIME;
2796 umtxq_lock(&uq->uq_key);
2797 umtxq_busy(&uq->uq_key);
2799 umtxq_unlock(&uq->uq_key);
2802 * Set c_has_waiters to 1 before releasing user mutex, also
2803 * don't modify cache line when unnecessary.
2805 error = fueword32(&cv->c_has_waiters, &hasw);
2806 if (error == 0 && hasw == 0)
2807 suword32(&cv->c_has_waiters, 1);
2809 umtxq_unbusy_unlocked(&uq->uq_key);
2811 error = do_unlock_umutex(td, m, false);
2813 if (timeout != NULL)
2814 umtx_abs_timeout_init(&timo, clockid,
2815 (wflags & CVWAIT_ABSTIME) != 0, timeout);
2817 umtxq_lock(&uq->uq_key);
2819 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2823 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2827 * This must be timeout,interrupted by signal or
2828 * surprious wakeup, clear c_has_waiter flag when
2831 umtxq_busy(&uq->uq_key);
2832 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2833 int oldlen = uq->uq_cur_queue->length;
2836 umtxq_unlock(&uq->uq_key);
2837 suword32(&cv->c_has_waiters, 0);
2838 umtxq_lock(&uq->uq_key);
2841 umtxq_unbusy(&uq->uq_key);
2842 if (error == ERESTART)
2846 umtxq_unlock(&uq->uq_key);
2847 umtx_key_release(&uq->uq_key);
2852 * Signal a userland condition variable.
2855 do_cv_signal(struct thread *td, struct ucond *cv)
2857 struct umtx_key key;
2858 int error, cnt, nwake;
2861 error = fueword32(&cv->c_flags, &flags);
2864 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2868 cnt = umtxq_count(&key);
2869 nwake = umtxq_signal(&key, 1);
2872 error = suword32(&cv->c_has_waiters, 0);
2879 umtx_key_release(&key);
2884 do_cv_broadcast(struct thread *td, struct ucond *cv)
2886 struct umtx_key key;
2890 error = fueword32(&cv->c_flags, &flags);
2893 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2898 umtxq_signal(&key, INT_MAX);
2901 error = suword32(&cv->c_has_waiters, 0);
2905 umtxq_unbusy_unlocked(&key);
2907 umtx_key_release(&key);
2912 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
2913 struct _umtx_time *timeout)
2915 struct umtx_abs_timeout timo;
2917 uint32_t flags, wrflags;
2918 int32_t state, oldstate;
2919 int32_t blocked_readers;
2920 int error, error1, rv;
2923 error = fueword32(&rwlock->rw_flags, &flags);
2926 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2930 if (timeout != NULL)
2931 umtx_abs_timeout_init2(&timo, timeout);
2933 wrflags = URWLOCK_WRITE_OWNER;
2934 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2935 wrflags |= URWLOCK_WRITE_WAITERS;
2938 rv = fueword32(&rwlock->rw_state, &state);
2940 umtx_key_release(&uq->uq_key);
2944 /* try to lock it */
2945 while (!(state & wrflags)) {
2946 if (__predict_false(URWLOCK_READER_COUNT(state) ==
2947 URWLOCK_MAX_READERS)) {
2948 umtx_key_release(&uq->uq_key);
2951 rv = casueword32(&rwlock->rw_state, state,
2952 &oldstate, state + 1);
2954 umtx_key_release(&uq->uq_key);
2958 MPASS(oldstate == state);
2959 umtx_key_release(&uq->uq_key);
2962 error = thread_check_susp(td, true);
2971 /* grab monitor lock */
2972 umtxq_lock(&uq->uq_key);
2973 umtxq_busy(&uq->uq_key);
2974 umtxq_unlock(&uq->uq_key);
2977 * re-read the state, in case it changed between the try-lock above
2978 * and the check below
2980 rv = fueword32(&rwlock->rw_state, &state);
2984 /* set read contention bit */
2985 while (error == 0 && (state & wrflags) &&
2986 !(state & URWLOCK_READ_WAITERS)) {
2987 rv = casueword32(&rwlock->rw_state, state,
2988 &oldstate, state | URWLOCK_READ_WAITERS);
2994 MPASS(oldstate == state);
2998 error = thread_check_susp(td, false);
3003 umtxq_unbusy_unlocked(&uq->uq_key);
3007 /* state is changed while setting flags, restart */
3008 if (!(state & wrflags)) {
3009 umtxq_unbusy_unlocked(&uq->uq_key);
3010 error = thread_check_susp(td, true);
3018 * Contention bit is set, before sleeping, increase
3019 * read waiter count.
3021 rv = fueword32(&rwlock->rw_blocked_readers,
3024 umtxq_unbusy_unlocked(&uq->uq_key);
3028 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
3030 while (state & wrflags) {
3031 umtxq_lock(&uq->uq_key);
3033 umtxq_unbusy(&uq->uq_key);
3035 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
3038 umtxq_busy(&uq->uq_key);
3040 umtxq_unlock(&uq->uq_key);
3043 rv = fueword32(&rwlock->rw_state, &state);
3050 /* decrease read waiter count, and may clear read contention bit */
3051 rv = fueword32(&rwlock->rw_blocked_readers,
3054 umtxq_unbusy_unlocked(&uq->uq_key);
3058 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
3059 if (blocked_readers == 1) {
3060 rv = fueword32(&rwlock->rw_state, &state);
3062 umtxq_unbusy_unlocked(&uq->uq_key);
3067 rv = casueword32(&rwlock->rw_state, state,
3068 &oldstate, state & ~URWLOCK_READ_WAITERS);
3074 MPASS(oldstate == state);
3078 error1 = thread_check_susp(td, false);
3087 umtxq_unbusy_unlocked(&uq->uq_key);
3091 umtx_key_release(&uq->uq_key);
3092 if (error == ERESTART)
3098 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
3100 struct umtx_abs_timeout timo;
3103 int32_t state, oldstate;
3104 int32_t blocked_writers;
3105 int32_t blocked_readers;
3106 int error, error1, rv;
3109 error = fueword32(&rwlock->rw_flags, &flags);
3112 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3116 if (timeout != NULL)
3117 umtx_abs_timeout_init2(&timo, timeout);
3119 blocked_readers = 0;
3121 rv = fueword32(&rwlock->rw_state, &state);
3123 umtx_key_release(&uq->uq_key);
3126 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
3127 URWLOCK_READER_COUNT(state) == 0) {
3128 rv = casueword32(&rwlock->rw_state, state,
3129 &oldstate, state | URWLOCK_WRITE_OWNER);
3131 umtx_key_release(&uq->uq_key);
3135 MPASS(oldstate == state);
3136 umtx_key_release(&uq->uq_key);
3140 error = thread_check_susp(td, true);
3146 if ((state & (URWLOCK_WRITE_OWNER |
3147 URWLOCK_WRITE_WAITERS)) == 0 &&
3148 blocked_readers != 0) {
3149 umtxq_lock(&uq->uq_key);
3150 umtxq_busy(&uq->uq_key);
3151 umtxq_signal_queue(&uq->uq_key, INT_MAX,
3153 umtxq_unbusy(&uq->uq_key);
3154 umtxq_unlock(&uq->uq_key);
3160 /* grab monitor lock */
3161 umtxq_lock(&uq->uq_key);
3162 umtxq_busy(&uq->uq_key);
3163 umtxq_unlock(&uq->uq_key);
3166 * Re-read the state, in case it changed between the
3167 * try-lock above and the check below.
3169 rv = fueword32(&rwlock->rw_state, &state);
3173 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
3174 URWLOCK_READER_COUNT(state) != 0) &&
3175 (state & URWLOCK_WRITE_WAITERS) == 0) {
3176 rv = casueword32(&rwlock->rw_state, state,
3177 &oldstate, state | URWLOCK_WRITE_WAITERS);
3183 MPASS(oldstate == state);
3187 error = thread_check_susp(td, false);
3192 umtxq_unbusy_unlocked(&uq->uq_key);
3196 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
3197 URWLOCK_READER_COUNT(state) == 0) {
3198 umtxq_unbusy_unlocked(&uq->uq_key);
3199 error = thread_check_susp(td, false);
3205 rv = fueword32(&rwlock->rw_blocked_writers,
3208 umtxq_unbusy_unlocked(&uq->uq_key);
3212 suword32(&rwlock->rw_blocked_writers, blocked_writers + 1);
3214 while ((state & URWLOCK_WRITE_OWNER) ||
3215 URWLOCK_READER_COUNT(state) != 0) {
3216 umtxq_lock(&uq->uq_key);
3217 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3218 umtxq_unbusy(&uq->uq_key);
3220 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
3223 umtxq_busy(&uq->uq_key);
3224 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3225 umtxq_unlock(&uq->uq_key);
3228 rv = fueword32(&rwlock->rw_state, &state);
3235 rv = fueword32(&rwlock->rw_blocked_writers,
3238 umtxq_unbusy_unlocked(&uq->uq_key);
3242 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
3243 if (blocked_writers == 1) {
3244 rv = fueword32(&rwlock->rw_state, &state);
3246 umtxq_unbusy_unlocked(&uq->uq_key);
3251 rv = casueword32(&rwlock->rw_state, state,
3252 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3258 MPASS(oldstate == state);
3262 error1 = thread_check_susp(td, false);
3264 * We are leaving the URWLOCK_WRITE_WAITERS
3265 * behind, but this should not harm the
3274 rv = fueword32(&rwlock->rw_blocked_readers,
3277 umtxq_unbusy_unlocked(&uq->uq_key);
3282 blocked_readers = 0;
3284 umtxq_unbusy_unlocked(&uq->uq_key);
3287 umtx_key_release(&uq->uq_key);
3288 if (error == ERESTART)
3294 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3298 int32_t state, oldstate;
3299 int error, rv, q, count;
3302 error = fueword32(&rwlock->rw_flags, &flags);
3305 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3309 error = fueword32(&rwlock->rw_state, &state);
3314 if (state & URWLOCK_WRITE_OWNER) {
3316 rv = casueword32(&rwlock->rw_state, state,
3317 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3324 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3328 error = thread_check_susp(td, true);
3334 } else if (URWLOCK_READER_COUNT(state) != 0) {
3336 rv = casueword32(&rwlock->rw_state, state,
3337 &oldstate, state - 1);
3344 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3348 error = thread_check_susp(td, true);
3361 if (!(flags & URWLOCK_PREFER_READER)) {
3362 if (state & URWLOCK_WRITE_WAITERS) {
3364 q = UMTX_EXCLUSIVE_QUEUE;
3365 } else if (state & URWLOCK_READ_WAITERS) {
3367 q = UMTX_SHARED_QUEUE;
3370 if (state & URWLOCK_READ_WAITERS) {
3372 q = UMTX_SHARED_QUEUE;
3373 } else if (state & URWLOCK_WRITE_WAITERS) {
3375 q = UMTX_EXCLUSIVE_QUEUE;
3380 umtxq_lock(&uq->uq_key);
3381 umtxq_busy(&uq->uq_key);
3382 umtxq_signal_queue(&uq->uq_key, count, q);
3383 umtxq_unbusy(&uq->uq_key);
3384 umtxq_unlock(&uq->uq_key);
3387 umtx_key_release(&uq->uq_key);
3391 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3393 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3395 struct umtx_abs_timeout timo;
3397 uint32_t flags, count, count1;
3401 error = fueword32(&sem->_flags, &flags);
3404 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3408 if (timeout != NULL)
3409 umtx_abs_timeout_init2(&timo, timeout);
3412 umtxq_lock(&uq->uq_key);
3413 umtxq_busy(&uq->uq_key);
3415 umtxq_unlock(&uq->uq_key);
3416 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3418 rv1 = fueword32(&sem->_count, &count);
3419 if (rv == -1 || (rv == 0 && (rv1 == -1 || count != 0)) ||
3420 (rv == 1 && count1 == 0)) {
3421 umtxq_lock(&uq->uq_key);
3422 umtxq_unbusy(&uq->uq_key);
3424 umtxq_unlock(&uq->uq_key);
3426 rv = thread_check_susp(td, true);
3434 error = rv == -1 ? EFAULT : 0;
3437 umtxq_lock(&uq->uq_key);
3438 umtxq_unbusy(&uq->uq_key);
3440 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3442 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3446 /* A relative timeout cannot be restarted. */
3447 if (error == ERESTART && timeout != NULL &&
3448 (timeout->_flags & UMTX_ABSTIME) == 0)
3451 umtxq_unlock(&uq->uq_key);
3453 umtx_key_release(&uq->uq_key);
3458 * Signal a userland semaphore.
3461 do_sem_wake(struct thread *td, struct _usem *sem)
3463 struct umtx_key key;
3467 error = fueword32(&sem->_flags, &flags);
3470 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3474 cnt = umtxq_count(&key);
3477 * Check if count is greater than 0, this means the memory is
3478 * still being referenced by user code, so we can safely
3479 * update _has_waiters flag.
3483 error = suword32(&sem->_has_waiters, 0);
3488 umtxq_signal(&key, 1);
3492 umtx_key_release(&key);
3498 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3500 struct umtx_abs_timeout timo;
3502 uint32_t count, flags;
3506 flags = fuword32(&sem->_flags);
3507 if (timeout != NULL)
3508 umtx_abs_timeout_init2(&timo, timeout);
3511 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3514 umtxq_lock(&uq->uq_key);
3515 umtxq_busy(&uq->uq_key);
3517 umtxq_unlock(&uq->uq_key);
3518 rv = fueword32(&sem->_count, &count);
3520 umtxq_lock(&uq->uq_key);
3521 umtxq_unbusy(&uq->uq_key);
3523 umtxq_unlock(&uq->uq_key);
3524 umtx_key_release(&uq->uq_key);
3528 if (USEM_COUNT(count) != 0) {
3529 umtxq_lock(&uq->uq_key);
3530 umtxq_unbusy(&uq->uq_key);
3532 umtxq_unlock(&uq->uq_key);
3533 umtx_key_release(&uq->uq_key);
3536 if (count == USEM_HAS_WAITERS)
3538 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3541 umtxq_lock(&uq->uq_key);
3542 umtxq_unbusy(&uq->uq_key);
3544 umtxq_unlock(&uq->uq_key);
3545 umtx_key_release(&uq->uq_key);
3548 rv = thread_check_susp(td, true);
3553 umtxq_lock(&uq->uq_key);
3554 umtxq_unbusy(&uq->uq_key);
3556 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3558 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3562 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3563 /* A relative timeout cannot be restarted. */
3564 if (error == ERESTART)
3566 if (error == EINTR) {
3567 umtx_abs_timeout_update(&timo);
3568 timespecsub(&timo.end, &timo.cur,
3569 &timeout->_timeout);
3573 umtxq_unlock(&uq->uq_key);
3574 umtx_key_release(&uq->uq_key);
3579 * Signal a userland semaphore.
3582 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3584 struct umtx_key key;
3586 uint32_t count, flags;
3588 rv = fueword32(&sem->_flags, &flags);
3591 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3595 cnt = umtxq_count(&key);
3598 * If this was the last sleeping thread, clear the waiters
3603 rv = fueword32(&sem->_count, &count);
3604 while (rv != -1 && count & USEM_HAS_WAITERS) {
3605 rv = casueword32(&sem->_count, count, &count,
3606 count & ~USEM_HAS_WAITERS);
3608 rv = thread_check_susp(td, true);
3621 umtxq_signal(&key, 1);
3625 umtx_key_release(&key);
3629 #ifdef COMPAT_FREEBSD10
3631 freebsd10__umtx_lock(struct thread *td, struct freebsd10__umtx_lock_args *uap)
3633 return (do_lock_umtx(td, uap->umtx, td->td_tid, 0));
3637 freebsd10__umtx_unlock(struct thread *td,
3638 struct freebsd10__umtx_unlock_args *uap)
3640 return (do_unlock_umtx(td, uap->umtx, td->td_tid));
3645 umtx_copyin_timeout(const void *uaddr, struct timespec *tsp)
3649 error = copyin(uaddr, tsp, sizeof(*tsp));
3651 if (tsp->tv_sec < 0 ||
3652 tsp->tv_nsec >= 1000000000 ||
3660 umtx_copyin_umtx_time(const void *uaddr, size_t size, struct _umtx_time *tp)
3664 if (size <= sizeof(tp->_timeout)) {
3665 tp->_clockid = CLOCK_REALTIME;
3667 error = copyin(uaddr, &tp->_timeout, sizeof(tp->_timeout));
3669 error = copyin(uaddr, tp, sizeof(*tp));
3672 if (tp->_timeout.tv_sec < 0 ||
3673 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3679 umtx_copyin_robust_lists(const void *uaddr, size_t size,
3680 struct umtx_robust_lists_params *rb)
3683 if (size > sizeof(*rb))
3685 return (copyin(uaddr, rb, size));
3689 umtx_copyout_timeout(void *uaddr, size_t sz, struct timespec *tsp)
3693 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
3694 * and we're only called if sz >= sizeof(timespec) as supplied in the
3697 KASSERT(sz >= sizeof(*tsp),
3698 ("umtx_copyops specifies incorrect sizes"));
3700 return (copyout(tsp, uaddr, sizeof(*tsp)));
3703 #ifdef COMPAT_FREEBSD10
3705 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap,
3706 const struct umtx_copyops *ops)
3708 struct timespec *ts, timeout;
3711 /* Allow a null timespec (wait forever). */
3712 if (uap->uaddr2 == NULL)
3715 error = ops->copyin_timeout(uap->uaddr2, &timeout);
3720 #ifdef COMPAT_FREEBSD32
3722 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
3724 return (do_lock_umtx(td, uap->obj, uap->val, ts));
3728 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap,
3729 const struct umtx_copyops *ops)
3731 #ifdef COMPAT_FREEBSD32
3733 return (do_unlock_umtx32(td, uap->obj, uap->val));
3735 return (do_unlock_umtx(td, uap->obj, uap->val));
3737 #endif /* COMPAT_FREEBSD10 */
3739 #if !defined(COMPAT_FREEBSD10)
3741 __umtx_op_unimpl(struct thread *td __unused, struct _umtx_op_args *uap __unused,
3742 const struct umtx_copyops *ops __unused)
3744 return (EOPNOTSUPP);
3746 #endif /* COMPAT_FREEBSD10 */
3749 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap,
3750 const struct umtx_copyops *ops)
3752 struct _umtx_time timeout, *tm_p;
3755 if (uap->uaddr2 == NULL)
3758 error = ops->copyin_umtx_time(
3759 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3764 return (do_wait(td, uap->obj, uap->val, tm_p, ops->compat32, 0));
3768 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap,
3769 const struct umtx_copyops *ops)
3771 struct _umtx_time timeout, *tm_p;
3774 if (uap->uaddr2 == NULL)
3777 error = ops->copyin_umtx_time(
3778 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3783 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3787 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap,
3788 const struct umtx_copyops *ops)
3790 struct _umtx_time *tm_p, timeout;
3793 if (uap->uaddr2 == NULL)
3796 error = ops->copyin_umtx_time(
3797 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3802 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3806 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap,
3807 const struct umtx_copyops *ops __unused)
3810 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3813 #define BATCH_SIZE 128
3815 __umtx_op_nwake_private_native(struct thread *td, struct _umtx_op_args *uap)
3817 char *uaddrs[BATCH_SIZE], **upp;
3818 int count, error, i, pos, tocopy;
3820 upp = (char **)uap->obj;
3822 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3824 tocopy = MIN(count, BATCH_SIZE);
3825 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3828 for (i = 0; i < tocopy; ++i) {
3829 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3837 __umtx_op_nwake_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3839 uint32_t uaddrs[BATCH_SIZE], *upp;
3840 int count, error, i, pos, tocopy;
3842 upp = (uint32_t *)uap->obj;
3844 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3846 tocopy = MIN(count, BATCH_SIZE);
3847 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
3850 for (i = 0; i < tocopy; ++i) {
3851 kern_umtx_wake(td, (void *)(uintptr_t)uaddrs[i],
3860 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap,
3861 const struct umtx_copyops *ops)
3865 return (__umtx_op_nwake_private_compat32(td, uap));
3866 return (__umtx_op_nwake_private_native(td, uap));
3870 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap,
3871 const struct umtx_copyops *ops __unused)
3874 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3878 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap,
3879 const struct umtx_copyops *ops)
3881 struct _umtx_time *tm_p, timeout;
3884 /* Allow a null timespec (wait forever). */
3885 if (uap->uaddr2 == NULL)
3888 error = ops->copyin_umtx_time(
3889 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3894 return (do_lock_umutex(td, uap->obj, tm_p, 0));
3898 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap,
3899 const struct umtx_copyops *ops __unused)
3902 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
3906 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap,
3907 const struct umtx_copyops *ops)
3909 struct _umtx_time *tm_p, timeout;
3912 /* Allow a null timespec (wait forever). */
3913 if (uap->uaddr2 == NULL)
3916 error = ops->copyin_umtx_time(
3917 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3922 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
3926 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap,
3927 const struct umtx_copyops *ops __unused)
3930 return (do_wake_umutex(td, uap->obj));
3934 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap,
3935 const struct umtx_copyops *ops __unused)
3938 return (do_unlock_umutex(td, uap->obj, false));
3942 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap,
3943 const struct umtx_copyops *ops __unused)
3946 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
3950 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap,
3951 const struct umtx_copyops *ops)
3953 struct timespec *ts, timeout;
3956 /* Allow a null timespec (wait forever). */
3957 if (uap->uaddr2 == NULL)
3960 error = ops->copyin_timeout(uap->uaddr2, &timeout);
3965 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3969 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap,
3970 const struct umtx_copyops *ops __unused)
3973 return (do_cv_signal(td, uap->obj));
3977 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap,
3978 const struct umtx_copyops *ops __unused)
3981 return (do_cv_broadcast(td, uap->obj));
3985 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap,
3986 const struct umtx_copyops *ops)
3988 struct _umtx_time timeout;
3991 /* Allow a null timespec (wait forever). */
3992 if (uap->uaddr2 == NULL) {
3993 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3995 error = ops->copyin_umtx_time(uap->uaddr2,
3996 (size_t)uap->uaddr1, &timeout);
3999 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4005 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap,
4006 const struct umtx_copyops *ops)
4008 struct _umtx_time timeout;
4011 /* Allow a null timespec (wait forever). */
4012 if (uap->uaddr2 == NULL) {
4013 error = do_rw_wrlock(td, uap->obj, 0);
4015 error = ops->copyin_umtx_time(uap->uaddr2,
4016 (size_t)uap->uaddr1, &timeout);
4020 error = do_rw_wrlock(td, uap->obj, &timeout);
4026 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap,
4027 const struct umtx_copyops *ops __unused)
4030 return (do_rw_unlock(td, uap->obj));
4033 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4035 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap,
4036 const struct umtx_copyops *ops)
4038 struct _umtx_time *tm_p, timeout;
4041 /* Allow a null timespec (wait forever). */
4042 if (uap->uaddr2 == NULL)
4045 error = ops->copyin_umtx_time(
4046 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4051 return (do_sem_wait(td, uap->obj, tm_p));
4055 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap,
4056 const struct umtx_copyops *ops __unused)
4059 return (do_sem_wake(td, uap->obj));
4064 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap,
4065 const struct umtx_copyops *ops __unused)
4068 return (do_wake2_umutex(td, uap->obj, uap->val));
4072 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap,
4073 const struct umtx_copyops *ops)
4075 struct _umtx_time *tm_p, timeout;
4079 /* Allow a null timespec (wait forever). */
4080 if (uap->uaddr2 == NULL) {
4084 uasize = (size_t)uap->uaddr1;
4085 error = ops->copyin_umtx_time(uap->uaddr2, uasize, &timeout);
4090 error = do_sem2_wait(td, uap->obj, tm_p);
4091 if (error == EINTR && uap->uaddr2 != NULL &&
4092 (timeout._flags & UMTX_ABSTIME) == 0 &&
4093 uasize >= ops->umtx_time_sz + ops->timespec_sz) {
4094 error = ops->copyout_timeout(
4095 (void *)((uintptr_t)uap->uaddr2 + ops->umtx_time_sz),
4096 uasize - ops->umtx_time_sz, &timeout._timeout);
4106 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap,
4107 const struct umtx_copyops *ops __unused)
4110 return (do_sem2_wake(td, uap->obj));
4113 #define USHM_OBJ_UMTX(o) \
4114 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
4116 #define USHMF_REG_LINKED 0x0001
4117 #define USHMF_OBJ_LINKED 0x0002
4118 struct umtx_shm_reg {
4119 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
4120 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
4121 struct umtx_key ushm_key;
4122 struct ucred *ushm_cred;
4123 struct shmfd *ushm_obj;
4128 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
4129 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
4131 static uma_zone_t umtx_shm_reg_zone;
4132 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
4133 static struct mtx umtx_shm_lock;
4134 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
4135 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
4137 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
4140 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
4142 struct umtx_shm_reg_head d;
4143 struct umtx_shm_reg *reg, *reg1;
4146 mtx_lock(&umtx_shm_lock);
4147 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
4148 mtx_unlock(&umtx_shm_lock);
4149 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
4150 TAILQ_REMOVE(&d, reg, ushm_reg_link);
4151 umtx_shm_free_reg(reg);
4155 static struct task umtx_shm_reg_delfree_task =
4156 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
4158 static struct umtx_shm_reg *
4159 umtx_shm_find_reg_locked(const struct umtx_key *key)
4161 struct umtx_shm_reg *reg;
4162 struct umtx_shm_reg_head *reg_head;
4164 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
4165 mtx_assert(&umtx_shm_lock, MA_OWNED);
4166 reg_head = &umtx_shm_registry[key->hash];
4167 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
4168 KASSERT(reg->ushm_key.shared,
4169 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
4170 if (reg->ushm_key.info.shared.object ==
4171 key->info.shared.object &&
4172 reg->ushm_key.info.shared.offset ==
4173 key->info.shared.offset) {
4174 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
4175 KASSERT(reg->ushm_refcnt > 0,
4176 ("reg %p refcnt 0 onlist", reg));
4177 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
4178 ("reg %p not linked", reg));
4186 static struct umtx_shm_reg *
4187 umtx_shm_find_reg(const struct umtx_key *key)
4189 struct umtx_shm_reg *reg;
4191 mtx_lock(&umtx_shm_lock);
4192 reg = umtx_shm_find_reg_locked(key);
4193 mtx_unlock(&umtx_shm_lock);
4198 umtx_shm_free_reg(struct umtx_shm_reg *reg)
4201 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
4202 crfree(reg->ushm_cred);
4203 shm_drop(reg->ushm_obj);
4204 uma_zfree(umtx_shm_reg_zone, reg);
4208 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
4212 mtx_assert(&umtx_shm_lock, MA_OWNED);
4213 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
4215 res = reg->ushm_refcnt == 0;
4217 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
4218 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
4219 reg, ushm_reg_link);
4220 reg->ushm_flags &= ~USHMF_REG_LINKED;
4222 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
4223 LIST_REMOVE(reg, ushm_obj_link);
4224 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
4231 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
4237 object = reg->ushm_obj->shm_object;
4238 VM_OBJECT_WLOCK(object);
4239 object->flags |= OBJ_UMTXDEAD;
4240 VM_OBJECT_WUNLOCK(object);
4242 mtx_lock(&umtx_shm_lock);
4243 dofree = umtx_shm_unref_reg_locked(reg, force);
4244 mtx_unlock(&umtx_shm_lock);
4246 umtx_shm_free_reg(reg);
4250 umtx_shm_object_init(vm_object_t object)
4253 LIST_INIT(USHM_OBJ_UMTX(object));
4257 umtx_shm_object_terminated(vm_object_t object)
4259 struct umtx_shm_reg *reg, *reg1;
4262 if (LIST_EMPTY(USHM_OBJ_UMTX(object)))
4266 mtx_lock(&umtx_shm_lock);
4267 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
4268 if (umtx_shm_unref_reg_locked(reg, true)) {
4269 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
4274 mtx_unlock(&umtx_shm_lock);
4276 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
4280 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
4281 struct umtx_shm_reg **res)
4283 struct umtx_shm_reg *reg, *reg1;
4287 reg = umtx_shm_find_reg(key);
4292 cred = td->td_ucred;
4293 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
4295 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
4296 reg->ushm_refcnt = 1;
4297 bcopy(key, ®->ushm_key, sizeof(*key));
4298 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR, false);
4299 reg->ushm_cred = crhold(cred);
4300 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
4302 umtx_shm_free_reg(reg);
4305 mtx_lock(&umtx_shm_lock);
4306 reg1 = umtx_shm_find_reg_locked(key);
4308 mtx_unlock(&umtx_shm_lock);
4309 umtx_shm_free_reg(reg);
4314 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
4315 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
4317 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
4318 mtx_unlock(&umtx_shm_lock);
4324 umtx_shm_alive(struct thread *td, void *addr)
4327 vm_map_entry_t entry;
4334 map = &td->td_proc->p_vmspace->vm_map;
4335 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
4336 &object, &pindex, &prot, &wired);
4337 if (res != KERN_SUCCESS)
4342 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
4343 vm_map_lookup_done(map, entry);
4352 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
4353 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
4354 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
4355 for (i = 0; i < nitems(umtx_shm_registry); i++)
4356 TAILQ_INIT(&umtx_shm_registry[i]);
4360 umtx_shm(struct thread *td, void *addr, u_int flags)
4362 struct umtx_key key;
4363 struct umtx_shm_reg *reg;
4367 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
4368 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
4370 if ((flags & UMTX_SHM_ALIVE) != 0)
4371 return (umtx_shm_alive(td, addr));
4372 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
4375 KASSERT(key.shared == 1, ("non-shared key"));
4376 if ((flags & UMTX_SHM_CREAT) != 0) {
4377 error = umtx_shm_create_reg(td, &key, ®);
4379 reg = umtx_shm_find_reg(&key);
4383 umtx_key_release(&key);
4386 KASSERT(reg != NULL, ("no reg"));
4387 if ((flags & UMTX_SHM_DESTROY) != 0) {
4388 umtx_shm_unref_reg(reg, true);
4392 error = mac_posixshm_check_open(td->td_ucred,
4393 reg->ushm_obj, FFLAGS(O_RDWR));
4396 error = shm_access(reg->ushm_obj, td->td_ucred,
4400 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
4402 shm_hold(reg->ushm_obj);
4403 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
4405 td->td_retval[0] = fd;
4409 umtx_shm_unref_reg(reg, false);
4414 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap,
4415 const struct umtx_copyops *ops __unused)
4418 return (umtx_shm(td, uap->uaddr1, uap->val));
4422 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap,
4423 const struct umtx_copyops *ops)
4425 struct umtx_robust_lists_params rb;
4428 if (ops->compat32) {
4429 if ((td->td_pflags2 & TDP2_COMPAT32RB) == 0 &&
4430 (td->td_rb_list != 0 || td->td_rbp_list != 0 ||
4431 td->td_rb_inact != 0))
4433 } else if ((td->td_pflags2 & TDP2_COMPAT32RB) != 0) {
4437 bzero(&rb, sizeof(rb));
4438 error = ops->copyin_robust_lists(uap->uaddr1, uap->val, &rb);
4443 td->td_pflags2 |= TDP2_COMPAT32RB;
4445 td->td_rb_list = rb.robust_list_offset;
4446 td->td_rbp_list = rb.robust_priv_list_offset;
4447 td->td_rb_inact = rb.robust_inact_offset;
4451 #if defined(__i386__) || defined(__amd64__)
4453 * Provide the standard 32-bit definitions for x86, since native/compat32 use a
4454 * 32-bit time_t there. Other architectures just need the i386 definitions
4455 * along with their standard compat32.
4457 struct timespecx32 {
4462 struct umtx_timex32 {
4463 struct timespecx32 _timeout;
4469 #define timespeci386 timespec32
4470 #define umtx_timei386 umtx_time32
4472 #else /* !__i386__ && !__amd64__ */
4473 /* 32-bit architectures can emulate i386, so define these almost everywhere. */
4474 struct timespeci386 {
4479 struct umtx_timei386 {
4480 struct timespeci386 _timeout;
4485 #if defined(__LP64__)
4486 #define timespecx32 timespec32
4487 #define umtx_timex32 umtx_time32
4492 umtx_copyin_robust_lists32(const void *uaddr, size_t size,
4493 struct umtx_robust_lists_params *rbp)
4495 struct umtx_robust_lists_params_compat32 rb32;
4498 if (size > sizeof(rb32))
4500 bzero(&rb32, sizeof(rb32));
4501 error = copyin(uaddr, &rb32, size);
4504 CP(rb32, *rbp, robust_list_offset);
4505 CP(rb32, *rbp, robust_priv_list_offset);
4506 CP(rb32, *rbp, robust_inact_offset);
4512 umtx_copyin_timeouti386(const void *uaddr, struct timespec *tsp)
4514 struct timespeci386 ts32;
4517 error = copyin(uaddr, &ts32, sizeof(ts32));
4519 if (ts32.tv_sec < 0 ||
4520 ts32.tv_nsec >= 1000000000 ||
4524 CP(ts32, *tsp, tv_sec);
4525 CP(ts32, *tsp, tv_nsec);
4532 umtx_copyin_umtx_timei386(const void *uaddr, size_t size, struct _umtx_time *tp)
4534 struct umtx_timei386 t32;
4537 t32._clockid = CLOCK_REALTIME;
4539 if (size <= sizeof(t32._timeout))
4540 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4542 error = copyin(uaddr, &t32, sizeof(t32));
4545 if (t32._timeout.tv_sec < 0 ||
4546 t32._timeout.tv_nsec >= 1000000000 || t32._timeout.tv_nsec < 0)
4548 TS_CP(t32, *tp, _timeout);
4549 CP(t32, *tp, _flags);
4550 CP(t32, *tp, _clockid);
4555 umtx_copyout_timeouti386(void *uaddr, size_t sz, struct timespec *tsp)
4557 struct timespeci386 remain32 = {
4558 .tv_sec = tsp->tv_sec,
4559 .tv_nsec = tsp->tv_nsec,
4563 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4564 * and we're only called if sz >= sizeof(timespec) as supplied in the
4567 KASSERT(sz >= sizeof(remain32),
4568 ("umtx_copyops specifies incorrect sizes"));
4570 return (copyout(&remain32, uaddr, sizeof(remain32)));
4572 #endif /* !__i386__ */
4574 #if defined(__i386__) || defined(__LP64__)
4576 umtx_copyin_timeoutx32(const void *uaddr, struct timespec *tsp)
4578 struct timespecx32 ts32;
4581 error = copyin(uaddr, &ts32, sizeof(ts32));
4583 if (ts32.tv_sec < 0 ||
4584 ts32.tv_nsec >= 1000000000 ||
4588 CP(ts32, *tsp, tv_sec);
4589 CP(ts32, *tsp, tv_nsec);
4596 umtx_copyin_umtx_timex32(const void *uaddr, size_t size, struct _umtx_time *tp)
4598 struct umtx_timex32 t32;
4601 t32._clockid = CLOCK_REALTIME;
4603 if (size <= sizeof(t32._timeout))
4604 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4606 error = copyin(uaddr, &t32, sizeof(t32));
4609 if (t32._timeout.tv_sec < 0 ||
4610 t32._timeout.tv_nsec >= 1000000000 || t32._timeout.tv_nsec < 0)
4612 TS_CP(t32, *tp, _timeout);
4613 CP(t32, *tp, _flags);
4614 CP(t32, *tp, _clockid);
4619 umtx_copyout_timeoutx32(void *uaddr, size_t sz, struct timespec *tsp)
4621 struct timespecx32 remain32 = {
4622 .tv_sec = tsp->tv_sec,
4623 .tv_nsec = tsp->tv_nsec,
4627 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4628 * and we're only called if sz >= sizeof(timespec) as supplied in the
4631 KASSERT(sz >= sizeof(remain32),
4632 ("umtx_copyops specifies incorrect sizes"));
4634 return (copyout(&remain32, uaddr, sizeof(remain32)));
4636 #endif /* __i386__ || __LP64__ */
4638 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap,
4639 const struct umtx_copyops *umtx_ops);
4641 static const _umtx_op_func op_table[] = {
4642 #ifdef COMPAT_FREEBSD10
4643 [UMTX_OP_LOCK] = __umtx_op_lock_umtx,
4644 [UMTX_OP_UNLOCK] = __umtx_op_unlock_umtx,
4646 [UMTX_OP_LOCK] = __umtx_op_unimpl,
4647 [UMTX_OP_UNLOCK] = __umtx_op_unimpl,
4649 [UMTX_OP_WAIT] = __umtx_op_wait,
4650 [UMTX_OP_WAKE] = __umtx_op_wake,
4651 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4652 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
4653 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4654 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4655 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
4656 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4657 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4658 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
4659 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
4660 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
4661 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4662 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4663 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4664 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4665 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4666 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4667 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4668 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4670 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4671 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4673 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4674 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4675 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4676 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4677 [UMTX_OP_SHM] = __umtx_op_shm,
4678 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4681 static const struct umtx_copyops umtx_native_ops = {
4682 .copyin_timeout = umtx_copyin_timeout,
4683 .copyin_umtx_time = umtx_copyin_umtx_time,
4684 .copyin_robust_lists = umtx_copyin_robust_lists,
4685 .copyout_timeout = umtx_copyout_timeout,
4686 .timespec_sz = sizeof(struct timespec),
4687 .umtx_time_sz = sizeof(struct _umtx_time),
4691 static const struct umtx_copyops umtx_native_opsi386 = {
4692 .copyin_timeout = umtx_copyin_timeouti386,
4693 .copyin_umtx_time = umtx_copyin_umtx_timei386,
4694 .copyin_robust_lists = umtx_copyin_robust_lists32,
4695 .copyout_timeout = umtx_copyout_timeouti386,
4696 .timespec_sz = sizeof(struct timespeci386),
4697 .umtx_time_sz = sizeof(struct umtx_timei386),
4702 #if defined(__i386__) || defined(__LP64__)
4703 /* i386 can emulate other 32-bit archs, too! */
4704 static const struct umtx_copyops umtx_native_opsx32 = {
4705 .copyin_timeout = umtx_copyin_timeoutx32,
4706 .copyin_umtx_time = umtx_copyin_umtx_timex32,
4707 .copyin_robust_lists = umtx_copyin_robust_lists32,
4708 .copyout_timeout = umtx_copyout_timeoutx32,
4709 .timespec_sz = sizeof(struct timespecx32),
4710 .umtx_time_sz = sizeof(struct umtx_timex32),
4714 #ifdef COMPAT_FREEBSD32
4716 #define umtx_native_ops32 umtx_native_opsi386
4718 #define umtx_native_ops32 umtx_native_opsx32
4720 #endif /* COMPAT_FREEBSD32 */
4721 #endif /* __i386__ || __LP64__ */
4723 #define UMTX_OP__FLAGS (UMTX_OP__32BIT | UMTX_OP__I386)
4726 kern__umtx_op(struct thread *td, void *obj, int op, unsigned long val,
4727 void *uaddr1, void *uaddr2, const struct umtx_copyops *ops)
4729 struct _umtx_op_args uap = {
4731 .op = op & ~UMTX_OP__FLAGS,
4737 if ((uap.op >= nitems(op_table)))
4739 return ((*op_table[uap.op])(td, &uap, ops));
4743 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4745 static const struct umtx_copyops *umtx_ops;
4747 umtx_ops = &umtx_native_ops;
4749 if ((uap->op & (UMTX_OP__32BIT | UMTX_OP__I386)) != 0) {
4750 if ((uap->op & UMTX_OP__I386) != 0)
4751 umtx_ops = &umtx_native_opsi386;
4753 umtx_ops = &umtx_native_opsx32;
4755 #elif !defined(__i386__)
4756 /* We consider UMTX_OP__32BIT a nop on !i386 ILP32. */
4757 if ((uap->op & UMTX_OP__I386) != 0)
4758 umtx_ops = &umtx_native_opsi386;
4760 /* Likewise, UMTX_OP__I386 is a nop on i386. */
4761 if ((uap->op & UMTX_OP__32BIT) != 0)
4762 umtx_ops = &umtx_native_opsx32;
4764 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr1,
4765 uap->uaddr2, umtx_ops));
4768 #ifdef COMPAT_FREEBSD32
4769 #ifdef COMPAT_FREEBSD10
4771 freebsd10_freebsd32_umtx_lock(struct thread *td,
4772 struct freebsd10_freebsd32_umtx_lock_args *uap)
4774 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
4778 freebsd10_freebsd32_umtx_unlock(struct thread *td,
4779 struct freebsd10_freebsd32_umtx_unlock_args *uap)
4781 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
4783 #endif /* COMPAT_FREEBSD10 */
4786 freebsd32__umtx_op(struct thread *td, struct freebsd32__umtx_op_args *uap)
4789 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr,
4790 uap->uaddr2, &umtx_native_ops32));
4792 #endif /* COMPAT_FREEBSD32 */
4795 umtx_thread_init(struct thread *td)
4798 td->td_umtxq = umtxq_alloc();
4799 td->td_umtxq->uq_thread = td;
4803 umtx_thread_fini(struct thread *td)
4806 umtxq_free(td->td_umtxq);
4810 * It will be called when new thread is created, e.g fork().
4813 umtx_thread_alloc(struct thread *td)
4818 uq->uq_inherited_pri = PRI_MAX;
4820 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4821 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4822 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4823 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4829 * Clear robust lists for all process' threads, not delaying the
4830 * cleanup to thread exit, since the relevant address space is
4831 * destroyed right now.
4834 umtx_exec(struct proc *p)
4838 KASSERT(p == curproc, ("need curproc"));
4839 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4840 (p->p_flag & P_STOPPED_SINGLE) != 0,
4841 ("curproc must be single-threaded"));
4843 * There is no need to lock the list as only this thread can be
4846 FOREACH_THREAD_IN_PROC(p, td) {
4847 KASSERT(td == curthread ||
4848 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4849 ("running thread %p %p", p, td));
4850 umtx_thread_cleanup(td);
4851 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4859 umtx_thread_exit(struct thread *td)
4862 umtx_thread_cleanup(td);
4866 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res, bool compat32)
4873 error = fueword32((void *)ptr, &res32);
4877 error = fueword((void *)ptr, &res1);
4887 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list,
4890 struct umutex32 m32;
4893 memcpy(&m32, m, sizeof(m32));
4894 *rb_list = m32.m_rb_lnk;
4896 *rb_list = m->m_rb_lnk;
4901 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact,
4907 KASSERT(td->td_proc == curproc, ("need current vmspace"));
4908 error = copyin((void *)rbp, &m, sizeof(m));
4911 if (rb_list != NULL)
4912 umtx_read_rb_list(td, &m, rb_list, compat32);
4913 if ((m.m_flags & UMUTEX_ROBUST) == 0)
4915 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
4916 /* inact is cleared after unlock, allow the inconsistency */
4917 return (inact ? 0 : EINVAL);
4918 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
4922 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
4923 const char *name, bool compat32)
4931 error = umtx_read_uptr(td, rb_list, &rbp, compat32);
4932 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
4933 if (rbp == *rb_inact) {
4938 error = umtx_handle_rb(td, rbp, &rbp, inact, compat32);
4940 if (i == umtx_max_rb && umtx_verbose_rb) {
4941 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
4942 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
4944 if (error != 0 && umtx_verbose_rb) {
4945 uprintf("comm %s pid %d: handling %srb error %d\n",
4946 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
4951 * Clean up umtx data.
4954 umtx_thread_cleanup(struct thread *td)
4962 * Disown pi mutexes.
4966 if (uq->uq_inherited_pri != PRI_MAX ||
4967 !TAILQ_EMPTY(&uq->uq_pi_contested)) {
4968 mtx_lock(&umtx_lock);
4969 uq->uq_inherited_pri = PRI_MAX;
4970 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
4971 pi->pi_owner = NULL;
4972 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
4974 mtx_unlock(&umtx_lock);
4976 sched_lend_user_prio_cond(td, PRI_MAX);
4979 compat32 = (td->td_pflags2 & TDP2_COMPAT32RB) != 0;
4980 td->td_pflags2 &= ~TDP2_COMPAT32RB;
4982 if (td->td_rb_inact == 0 && td->td_rb_list == 0 && td->td_rbp_list == 0)
4986 * Handle terminated robust mutexes. Must be done after
4987 * robust pi disown, otherwise unlock could see unowned
4990 rb_inact = td->td_rb_inact;
4992 (void)umtx_read_uptr(td, rb_inact, &rb_inact, compat32);
4993 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "", compat32);
4994 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ", compat32);
4996 (void)umtx_handle_rb(td, rb_inact, NULL, true, compat32);