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 by a bit mask.
565 umtxq_signal_mask(struct umtx_key *key, int n_wake, u_int bitset)
567 struct umtxq_queue *uh;
568 struct umtx_q *uq, *uq_temp;
572 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
573 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
576 TAILQ_FOREACH_SAFE(uq, &uh->head, uq_link, uq_temp) {
577 if ((uq->uq_bitset & bitset) == 0)
579 umtxq_remove_queue(uq, UMTX_SHARED_QUEUE);
588 * Wake up threads waiting on an userland object.
592 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
594 struct umtxq_queue *uh;
599 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
600 uh = umtxq_queue_lookup(key, q);
602 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
603 umtxq_remove_queue(uq, q);
613 * Wake up specified thread.
616 umtxq_signal_thread(struct umtx_q *uq)
619 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
625 tstohz(const struct timespec *tsp)
629 TIMESPEC_TO_TIMEVAL(&tv, tsp);
634 umtx_abs_timeout_init(struct umtx_abs_timeout *timo, int clockid,
635 int absolute, const struct timespec *timeout)
638 timo->clockid = clockid;
640 timo->is_abs_real = false;
641 umtx_abs_timeout_update(timo);
642 timespecadd(&timo->cur, timeout, &timo->end);
644 timo->end = *timeout;
645 timo->is_abs_real = clockid == CLOCK_REALTIME ||
646 clockid == CLOCK_REALTIME_FAST ||
647 clockid == CLOCK_REALTIME_PRECISE;
649 * If is_abs_real, umtxq_sleep will read the clock
650 * after setting td_rtcgen; otherwise, read it here.
652 if (!timo->is_abs_real) {
653 umtx_abs_timeout_update(timo);
659 umtx_abs_timeout_init2(struct umtx_abs_timeout *timo,
660 const struct _umtx_time *umtxtime)
663 umtx_abs_timeout_init(timo, umtxtime->_clockid,
664 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
668 umtx_abs_timeout_update(struct umtx_abs_timeout *timo)
671 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
675 umtx_abs_timeout_gethz(struct umtx_abs_timeout *timo)
679 if (timespeccmp(&timo->end, &timo->cur, <=))
681 timespecsub(&timo->end, &timo->cur, &tts);
682 return (tstohz(&tts));
686 umtx_unlock_val(uint32_t flags, bool rb)
690 return (UMUTEX_RB_OWNERDEAD);
691 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
692 return (UMUTEX_RB_NOTRECOV);
694 return (UMUTEX_UNOWNED);
699 * Put thread into sleep state, before sleeping, check if
700 * thread was removed from umtx queue.
703 umtxq_sleep(struct umtx_q *uq, const char *wmesg,
704 struct umtx_abs_timeout *abstime)
706 struct umtxq_chain *uc;
709 if (abstime != NULL && abstime->is_abs_real) {
710 curthread->td_rtcgen = atomic_load_acq_int(&rtc_generation);
711 umtx_abs_timeout_update(abstime);
714 uc = umtxq_getchain(&uq->uq_key);
715 UMTXQ_LOCKED_ASSERT(uc);
717 if (!(uq->uq_flags & UQF_UMTXQ)) {
721 if (abstime != NULL) {
722 timo = umtx_abs_timeout_gethz(abstime);
729 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
730 if (error == EINTR || error == ERESTART) {
731 umtxq_lock(&uq->uq_key);
734 if (abstime != NULL) {
735 if (abstime->is_abs_real)
736 curthread->td_rtcgen =
737 atomic_load_acq_int(&rtc_generation);
738 umtx_abs_timeout_update(abstime);
740 umtxq_lock(&uq->uq_key);
743 curthread->td_rtcgen = 0;
748 * Convert userspace address into unique logical address.
751 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
753 struct thread *td = curthread;
755 vm_map_entry_t entry;
761 if (share == THREAD_SHARE) {
763 key->info.private.vs = td->td_proc->p_vmspace;
764 key->info.private.addr = (uintptr_t)addr;
766 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
767 map = &td->td_proc->p_vmspace->vm_map;
768 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
769 &entry, &key->info.shared.object, &pindex, &prot,
770 &wired) != KERN_SUCCESS) {
774 if ((share == PROCESS_SHARE) ||
775 (share == AUTO_SHARE &&
776 VM_INHERIT_SHARE == entry->inheritance)) {
778 key->info.shared.offset = (vm_offset_t)addr -
779 entry->start + entry->offset;
780 vm_object_reference(key->info.shared.object);
783 key->info.private.vs = td->td_proc->p_vmspace;
784 key->info.private.addr = (uintptr_t)addr;
786 vm_map_lookup_done(map, entry);
797 umtx_key_release(struct umtx_key *key)
800 vm_object_deallocate(key->info.shared.object);
803 #ifdef COMPAT_FREEBSD10
805 * Lock a umtx object.
808 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
809 const struct timespec *timeout)
811 struct umtx_abs_timeout timo;
819 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
822 * Care must be exercised when dealing with umtx structure. It
823 * can fault on any access.
827 * Try the uncontested case. This should be done in userland.
829 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
831 /* The acquire succeeded. */
832 if (owner == UMTX_UNOWNED)
835 /* The address was invalid. */
839 /* If no one owns it but it is contested try to acquire it. */
840 if (owner == UMTX_CONTESTED) {
841 owner = casuword(&umtx->u_owner,
842 UMTX_CONTESTED, id | UMTX_CONTESTED);
844 if (owner == UMTX_CONTESTED)
847 /* The address was invalid. */
851 error = thread_check_susp(td, false);
855 /* If this failed the lock has changed, restart. */
860 * If we caught a signal, we have retried and now
866 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
867 AUTO_SHARE, &uq->uq_key)) != 0)
870 umtxq_lock(&uq->uq_key);
871 umtxq_busy(&uq->uq_key);
873 umtxq_unbusy(&uq->uq_key);
874 umtxq_unlock(&uq->uq_key);
877 * Set the contested bit so that a release in user space
878 * knows to use the system call for unlock. If this fails
879 * either some one else has acquired the lock or it has been
882 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
884 /* The address was invalid. */
886 umtxq_lock(&uq->uq_key);
888 umtxq_unlock(&uq->uq_key);
889 umtx_key_release(&uq->uq_key);
894 * We set the contested bit, sleep. Otherwise the lock changed
895 * and we need to retry or we lost a race to the thread
896 * unlocking the umtx.
898 umtxq_lock(&uq->uq_key);
900 error = umtxq_sleep(uq, "umtx", timeout == NULL ? NULL :
903 umtxq_unlock(&uq->uq_key);
904 umtx_key_release(&uq->uq_key);
907 error = thread_check_susp(td, false);
910 if (timeout == NULL) {
911 /* Mutex locking is restarted if it is interrupted. */
915 /* Timed-locking is not restarted. */
916 if (error == ERESTART)
923 * Unlock a umtx object.
926 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
935 * Make sure we own this mtx.
937 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
941 if ((owner & ~UMTX_CONTESTED) != id)
944 /* This should be done in userland */
945 if ((owner & UMTX_CONTESTED) == 0) {
946 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
954 /* We should only ever be in here for contested locks */
955 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
961 count = umtxq_count(&key);
965 * When unlocking the umtx, it must be marked as unowned if
966 * there is zero or one thread only waiting for it.
967 * Otherwise, it must be marked as contested.
969 old = casuword(&umtx->u_owner, owner,
970 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
972 umtxq_signal(&key,1);
975 umtx_key_release(&key);
983 #ifdef COMPAT_FREEBSD32
986 * Lock a umtx object.
989 do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id,
990 const struct timespec *timeout)
992 struct umtx_abs_timeout timo;
1000 if (timeout != NULL)
1001 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
1004 * Care must be exercised when dealing with umtx structure. It
1005 * can fault on any access.
1009 * Try the uncontested case. This should be done in userland.
1011 owner = casuword32(m, UMUTEX_UNOWNED, id);
1013 /* The acquire succeeded. */
1014 if (owner == UMUTEX_UNOWNED)
1017 /* The address was invalid. */
1021 /* If no one owns it but it is contested try to acquire it. */
1022 if (owner == UMUTEX_CONTESTED) {
1023 owner = casuword32(m,
1024 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1025 if (owner == UMUTEX_CONTESTED)
1028 /* The address was invalid. */
1032 error = thread_check_susp(td, false);
1036 /* If this failed the lock has changed, restart. */
1041 * If we caught a signal, we have retried and now
1047 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
1048 AUTO_SHARE, &uq->uq_key)) != 0)
1051 umtxq_lock(&uq->uq_key);
1052 umtxq_busy(&uq->uq_key);
1054 umtxq_unbusy(&uq->uq_key);
1055 umtxq_unlock(&uq->uq_key);
1058 * Set the contested bit so that a release in user space
1059 * knows to use the system call for unlock. If this fails
1060 * either some one else has acquired the lock or it has been
1063 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
1065 /* The address was invalid. */
1067 umtxq_lock(&uq->uq_key);
1069 umtxq_unlock(&uq->uq_key);
1070 umtx_key_release(&uq->uq_key);
1075 * We set the contested bit, sleep. Otherwise the lock changed
1076 * and we need to retry or we lost a race to the thread
1077 * unlocking the umtx.
1079 umtxq_lock(&uq->uq_key);
1081 error = umtxq_sleep(uq, "umtx", timeout == NULL ?
1084 umtxq_unlock(&uq->uq_key);
1085 umtx_key_release(&uq->uq_key);
1088 error = thread_check_susp(td, false);
1091 if (timeout == NULL) {
1092 /* Mutex locking is restarted if it is interrupted. */
1096 /* Timed-locking is not restarted. */
1097 if (error == ERESTART)
1104 * Unlock a umtx object.
1107 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
1109 struct umtx_key key;
1116 * Make sure we own this mtx.
1118 owner = fuword32(m);
1122 if ((owner & ~UMUTEX_CONTESTED) != id)
1125 /* This should be done in userland */
1126 if ((owner & UMUTEX_CONTESTED) == 0) {
1127 old = casuword32(m, owner, UMUTEX_UNOWNED);
1135 /* We should only ever be in here for contested locks */
1136 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1142 count = umtxq_count(&key);
1146 * When unlocking the umtx, it must be marked as unowned if
1147 * there is zero or one thread only waiting for it.
1148 * Otherwise, it must be marked as contested.
1150 old = casuword32(m, owner,
1151 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1153 umtxq_signal(&key,1);
1156 umtx_key_release(&key);
1163 #endif /* COMPAT_FREEBSD32 */
1164 #endif /* COMPAT_FREEBSD10 */
1167 * Fetch and compare value, sleep on the address if value is not changed.
1170 do_wait(struct thread *td, void *addr, u_long id,
1171 struct _umtx_time *timeout, int compat32, int is_private)
1173 struct umtx_abs_timeout timo;
1180 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
1181 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
1184 if (timeout != NULL)
1185 umtx_abs_timeout_init2(&timo, timeout);
1187 umtxq_lock(&uq->uq_key);
1189 umtxq_unlock(&uq->uq_key);
1190 if (compat32 == 0) {
1191 error = fueword(addr, &tmp);
1195 error = fueword32(addr, &tmp32);
1201 umtxq_lock(&uq->uq_key);
1204 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
1206 if ((uq->uq_flags & UQF_UMTXQ) == 0)
1210 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
1213 umtxq_unlock(&uq->uq_key);
1214 umtx_key_release(&uq->uq_key);
1215 if (error == ERESTART)
1221 * Wake up threads sleeping on the specified address.
1224 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1226 struct umtx_key key;
1229 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1230 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1233 umtxq_signal(&key, n_wake);
1235 umtx_key_release(&key);
1240 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1243 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1244 struct _umtx_time *timeout, int mode)
1246 struct umtx_abs_timeout timo;
1248 uint32_t owner, old, id;
1254 if (timeout != NULL)
1255 umtx_abs_timeout_init2(&timo, timeout);
1258 * Care must be exercised when dealing with umtx structure. It
1259 * can fault on any access.
1262 rv = fueword32(&m->m_owner, &owner);
1265 if (mode == _UMUTEX_WAIT) {
1266 if (owner == UMUTEX_UNOWNED ||
1267 owner == UMUTEX_CONTESTED ||
1268 owner == UMUTEX_RB_OWNERDEAD ||
1269 owner == UMUTEX_RB_NOTRECOV)
1273 * Robust mutex terminated. Kernel duty is to
1274 * return EOWNERDEAD to the userspace. The
1275 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1276 * by the common userspace code.
1278 if (owner == UMUTEX_RB_OWNERDEAD) {
1279 rv = casueword32(&m->m_owner,
1280 UMUTEX_RB_OWNERDEAD, &owner,
1281 id | UMUTEX_CONTESTED);
1285 MPASS(owner == UMUTEX_RB_OWNERDEAD);
1286 return (EOWNERDEAD); /* success */
1289 rv = thread_check_susp(td, false);
1294 if (owner == UMUTEX_RB_NOTRECOV)
1295 return (ENOTRECOVERABLE);
1298 * Try the uncontested case. This should be
1301 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1303 /* The address was invalid. */
1307 /* The acquire succeeded. */
1309 MPASS(owner == UMUTEX_UNOWNED);
1314 * If no one owns it but it is contested try
1318 if (owner == UMUTEX_CONTESTED) {
1319 rv = casueword32(&m->m_owner,
1320 UMUTEX_CONTESTED, &owner,
1321 id | UMUTEX_CONTESTED);
1322 /* The address was invalid. */
1326 MPASS(owner == UMUTEX_CONTESTED);
1330 rv = thread_check_susp(td, false);
1336 * If this failed the lock has
1342 /* rv == 1 but not contested, likely store failure */
1343 rv = thread_check_susp(td, false);
1348 if (mode == _UMUTEX_TRY)
1352 * If we caught a signal, we have retried and now
1358 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1359 GET_SHARE(flags), &uq->uq_key)) != 0)
1362 umtxq_lock(&uq->uq_key);
1363 umtxq_busy(&uq->uq_key);
1365 umtxq_unlock(&uq->uq_key);
1368 * Set the contested bit so that a release in user space
1369 * knows to use the system call for unlock. If this fails
1370 * either some one else has acquired the lock or it has been
1373 rv = casueword32(&m->m_owner, owner, &old,
1374 owner | UMUTEX_CONTESTED);
1376 /* The address was invalid or casueword failed to store. */
1377 if (rv == -1 || rv == 1) {
1378 umtxq_lock(&uq->uq_key);
1380 umtxq_unbusy(&uq->uq_key);
1381 umtxq_unlock(&uq->uq_key);
1382 umtx_key_release(&uq->uq_key);
1386 rv = thread_check_susp(td, false);
1394 * We set the contested bit, sleep. Otherwise the lock changed
1395 * and we need to retry or we lost a race to the thread
1396 * unlocking the umtx.
1398 umtxq_lock(&uq->uq_key);
1399 umtxq_unbusy(&uq->uq_key);
1400 MPASS(old == owner);
1401 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1404 umtxq_unlock(&uq->uq_key);
1405 umtx_key_release(&uq->uq_key);
1408 error = thread_check_susp(td, false);
1415 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1418 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1420 struct umtx_key key;
1421 uint32_t owner, old, id, newlock;
1428 * Make sure we own this mtx.
1430 error = fueword32(&m->m_owner, &owner);
1434 if ((owner & ~UMUTEX_CONTESTED) != id)
1437 newlock = umtx_unlock_val(flags, rb);
1438 if ((owner & UMUTEX_CONTESTED) == 0) {
1439 error = casueword32(&m->m_owner, owner, &old, newlock);
1443 error = thread_check_susp(td, false);
1448 MPASS(old == owner);
1452 /* We should only ever be in here for contested locks */
1453 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1459 count = umtxq_count(&key);
1463 * When unlocking the umtx, it must be marked as unowned if
1464 * there is zero or one thread only waiting for it.
1465 * Otherwise, it must be marked as contested.
1468 newlock |= UMUTEX_CONTESTED;
1469 error = casueword32(&m->m_owner, owner, &old, newlock);
1471 umtxq_signal(&key, 1);
1474 umtx_key_release(&key);
1480 error = thread_check_susp(td, false);
1489 * Check if the mutex is available and wake up a waiter,
1490 * only for simple mutex.
1493 do_wake_umutex(struct thread *td, struct umutex *m)
1495 struct umtx_key key;
1502 error = fueword32(&m->m_owner, &owner);
1506 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1507 owner != UMUTEX_RB_NOTRECOV)
1510 error = fueword32(&m->m_flags, &flags);
1514 /* We should only ever be in here for contested locks */
1515 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1521 count = umtxq_count(&key);
1524 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1525 owner != UMUTEX_RB_NOTRECOV) {
1526 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1530 } else if (error == 1) {
1534 umtx_key_release(&key);
1535 error = thread_check_susp(td, false);
1543 if (error == 0 && count != 0) {
1544 MPASS((owner & ~UMUTEX_CONTESTED) == 0 ||
1545 owner == UMUTEX_RB_OWNERDEAD ||
1546 owner == UMUTEX_RB_NOTRECOV);
1547 umtxq_signal(&key, 1);
1551 umtx_key_release(&key);
1556 * Check if the mutex has waiters and tries to fix contention bit.
1559 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1561 struct umtx_key key;
1562 uint32_t owner, old;
1567 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1571 type = TYPE_NORMAL_UMUTEX;
1573 case UMUTEX_PRIO_INHERIT:
1574 type = TYPE_PI_UMUTEX;
1576 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1577 type = TYPE_PI_ROBUST_UMUTEX;
1579 case UMUTEX_PRIO_PROTECT:
1580 type = TYPE_PP_UMUTEX;
1582 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1583 type = TYPE_PP_ROBUST_UMUTEX;
1588 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1594 count = umtxq_count(&key);
1597 error = fueword32(&m->m_owner, &owner);
1602 * Only repair contention bit if there is a waiter, this means
1603 * the mutex is still being referenced by userland code,
1604 * otherwise don't update any memory.
1606 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0 &&
1607 (count > 1 || (count == 1 && (owner & ~UMUTEX_CONTESTED) != 0))) {
1608 error = casueword32(&m->m_owner, owner, &old,
1609 owner | UMUTEX_CONTESTED);
1615 MPASS(old == owner);
1619 error = thread_check_susp(td, false);
1623 if (error == EFAULT) {
1624 umtxq_signal(&key, INT_MAX);
1625 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1626 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1627 umtxq_signal(&key, 1);
1630 umtx_key_release(&key);
1634 static inline struct umtx_pi *
1635 umtx_pi_alloc(int flags)
1639 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1640 TAILQ_INIT(&pi->pi_blocked);
1641 atomic_add_int(&umtx_pi_allocated, 1);
1646 umtx_pi_free(struct umtx_pi *pi)
1648 uma_zfree(umtx_pi_zone, pi);
1649 atomic_add_int(&umtx_pi_allocated, -1);
1653 * Adjust the thread's position on a pi_state after its priority has been
1657 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1659 struct umtx_q *uq, *uq1, *uq2;
1662 mtx_assert(&umtx_lock, MA_OWNED);
1669 * Check if the thread needs to be moved on the blocked chain.
1670 * It needs to be moved if either its priority is lower than
1671 * the previous thread or higher than the next thread.
1673 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1674 uq2 = TAILQ_NEXT(uq, uq_lockq);
1675 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1676 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1678 * Remove thread from blocked chain and determine where
1679 * it should be moved to.
1681 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1682 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1683 td1 = uq1->uq_thread;
1684 MPASS(td1->td_proc->p_magic == P_MAGIC);
1685 if (UPRI(td1) > UPRI(td))
1690 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1692 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1697 static struct umtx_pi *
1698 umtx_pi_next(struct umtx_pi *pi)
1700 struct umtx_q *uq_owner;
1702 if (pi->pi_owner == NULL)
1704 uq_owner = pi->pi_owner->td_umtxq;
1705 if (uq_owner == NULL)
1707 return (uq_owner->uq_pi_blocked);
1711 * Floyd's Cycle-Finding Algorithm.
1714 umtx_pi_check_loop(struct umtx_pi *pi)
1716 struct umtx_pi *pi1; /* fast iterator */
1718 mtx_assert(&umtx_lock, MA_OWNED);
1723 pi = umtx_pi_next(pi);
1726 pi1 = umtx_pi_next(pi1);
1729 pi1 = umtx_pi_next(pi1);
1739 * Propagate priority when a thread is blocked on POSIX
1743 umtx_propagate_priority(struct thread *td)
1749 mtx_assert(&umtx_lock, MA_OWNED);
1752 pi = uq->uq_pi_blocked;
1755 if (umtx_pi_check_loop(pi))
1760 if (td == NULL || td == curthread)
1763 MPASS(td->td_proc != NULL);
1764 MPASS(td->td_proc->p_magic == P_MAGIC);
1767 if (td->td_lend_user_pri > pri)
1768 sched_lend_user_prio(td, pri);
1776 * Pick up the lock that td is blocked on.
1779 pi = uq->uq_pi_blocked;
1782 /* Resort td on the list if needed. */
1783 umtx_pi_adjust_thread(pi, td);
1788 * Unpropagate priority for a PI mutex when a thread blocked on
1789 * it is interrupted by signal or resumed by others.
1792 umtx_repropagate_priority(struct umtx_pi *pi)
1794 struct umtx_q *uq, *uq_owner;
1795 struct umtx_pi *pi2;
1798 mtx_assert(&umtx_lock, MA_OWNED);
1800 if (umtx_pi_check_loop(pi))
1802 while (pi != NULL && pi->pi_owner != NULL) {
1804 uq_owner = pi->pi_owner->td_umtxq;
1806 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1807 uq = TAILQ_FIRST(&pi2->pi_blocked);
1809 if (pri > UPRI(uq->uq_thread))
1810 pri = UPRI(uq->uq_thread);
1814 if (pri > uq_owner->uq_inherited_pri)
1815 pri = uq_owner->uq_inherited_pri;
1816 thread_lock(pi->pi_owner);
1817 sched_lend_user_prio(pi->pi_owner, pri);
1818 thread_unlock(pi->pi_owner);
1819 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1820 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1825 * Insert a PI mutex into owned list.
1828 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1830 struct umtx_q *uq_owner;
1832 uq_owner = owner->td_umtxq;
1833 mtx_assert(&umtx_lock, MA_OWNED);
1834 MPASS(pi->pi_owner == NULL);
1835 pi->pi_owner = owner;
1836 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1840 * Disown a PI mutex, and remove it from the owned list.
1843 umtx_pi_disown(struct umtx_pi *pi)
1846 mtx_assert(&umtx_lock, MA_OWNED);
1847 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1848 pi->pi_owner = NULL;
1852 * Claim ownership of a PI mutex.
1855 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1860 mtx_lock(&umtx_lock);
1861 if (pi->pi_owner == owner) {
1862 mtx_unlock(&umtx_lock);
1866 if (pi->pi_owner != NULL) {
1868 * userland may have already messed the mutex, sigh.
1870 mtx_unlock(&umtx_lock);
1873 umtx_pi_setowner(pi, owner);
1874 uq = TAILQ_FIRST(&pi->pi_blocked);
1876 pri = UPRI(uq->uq_thread);
1878 if (pri < UPRI(owner))
1879 sched_lend_user_prio(owner, pri);
1880 thread_unlock(owner);
1882 mtx_unlock(&umtx_lock);
1887 * Adjust a thread's order position in its blocked PI mutex,
1888 * this may result new priority propagating process.
1891 umtx_pi_adjust(struct thread *td, u_char oldpri)
1897 mtx_lock(&umtx_lock);
1899 * Pick up the lock that td is blocked on.
1901 pi = uq->uq_pi_blocked;
1903 umtx_pi_adjust_thread(pi, td);
1904 umtx_repropagate_priority(pi);
1906 mtx_unlock(&umtx_lock);
1910 * Sleep on a PI mutex.
1913 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
1914 const char *wmesg, struct umtx_abs_timeout *timo, bool shared)
1916 struct thread *td, *td1;
1920 struct umtxq_chain *uc;
1922 uc = umtxq_getchain(&pi->pi_key);
1926 KASSERT(td == curthread, ("inconsistent uq_thread"));
1927 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
1928 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1930 mtx_lock(&umtx_lock);
1931 if (pi->pi_owner == NULL) {
1932 mtx_unlock(&umtx_lock);
1933 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
1934 mtx_lock(&umtx_lock);
1936 if (pi->pi_owner == NULL)
1937 umtx_pi_setowner(pi, td1);
1938 PROC_UNLOCK(td1->td_proc);
1942 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1943 pri = UPRI(uq1->uq_thread);
1949 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1951 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1953 uq->uq_pi_blocked = pi;
1955 td->td_flags |= TDF_UPIBLOCKED;
1957 umtx_propagate_priority(td);
1958 mtx_unlock(&umtx_lock);
1959 umtxq_unbusy(&uq->uq_key);
1961 error = umtxq_sleep(uq, wmesg, timo);
1964 mtx_lock(&umtx_lock);
1965 uq->uq_pi_blocked = NULL;
1967 td->td_flags &= ~TDF_UPIBLOCKED;
1969 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1970 umtx_repropagate_priority(pi);
1971 mtx_unlock(&umtx_lock);
1972 umtxq_unlock(&uq->uq_key);
1978 * Add reference count for a PI mutex.
1981 umtx_pi_ref(struct umtx_pi *pi)
1984 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
1989 * Decrease reference count for a PI mutex, if the counter
1990 * is decreased to zero, its memory space is freed.
1993 umtx_pi_unref(struct umtx_pi *pi)
1995 struct umtxq_chain *uc;
1997 uc = umtxq_getchain(&pi->pi_key);
1998 UMTXQ_LOCKED_ASSERT(uc);
1999 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
2000 if (--pi->pi_refcount == 0) {
2001 mtx_lock(&umtx_lock);
2002 if (pi->pi_owner != NULL)
2004 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
2005 ("blocked queue not empty"));
2006 mtx_unlock(&umtx_lock);
2007 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
2013 * Find a PI mutex in hash table.
2015 static struct umtx_pi *
2016 umtx_pi_lookup(struct umtx_key *key)
2018 struct umtxq_chain *uc;
2021 uc = umtxq_getchain(key);
2022 UMTXQ_LOCKED_ASSERT(uc);
2024 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
2025 if (umtx_key_match(&pi->pi_key, key)) {
2033 * Insert a PI mutex into hash table.
2036 umtx_pi_insert(struct umtx_pi *pi)
2038 struct umtxq_chain *uc;
2040 uc = umtxq_getchain(&pi->pi_key);
2041 UMTXQ_LOCKED_ASSERT(uc);
2042 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
2049 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
2050 struct _umtx_time *timeout, int try)
2052 struct umtx_abs_timeout timo;
2054 struct umtx_pi *pi, *new_pi;
2055 uint32_t id, old_owner, owner, old;
2061 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2062 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2066 if (timeout != NULL)
2067 umtx_abs_timeout_init2(&timo, timeout);
2069 umtxq_lock(&uq->uq_key);
2070 pi = umtx_pi_lookup(&uq->uq_key);
2072 new_pi = umtx_pi_alloc(M_NOWAIT);
2073 if (new_pi == NULL) {
2074 umtxq_unlock(&uq->uq_key);
2075 new_pi = umtx_pi_alloc(M_WAITOK);
2076 umtxq_lock(&uq->uq_key);
2077 pi = umtx_pi_lookup(&uq->uq_key);
2079 umtx_pi_free(new_pi);
2083 if (new_pi != NULL) {
2084 new_pi->pi_key = uq->uq_key;
2085 umtx_pi_insert(new_pi);
2090 umtxq_unlock(&uq->uq_key);
2093 * Care must be exercised when dealing with umtx structure. It
2094 * can fault on any access.
2098 * Try the uncontested case. This should be done in userland.
2100 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
2101 /* The address was invalid. */
2106 /* The acquire succeeded. */
2108 MPASS(owner == UMUTEX_UNOWNED);
2113 if (owner == UMUTEX_RB_NOTRECOV) {
2114 error = ENOTRECOVERABLE;
2119 * Avoid overwriting a possible error from sleep due
2120 * to the pending signal with suspension check result.
2123 error = thread_check_susp(td, true);
2128 /* If no one owns it but it is contested try to acquire it. */
2129 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
2131 rv = casueword32(&m->m_owner, owner, &owner,
2132 id | UMUTEX_CONTESTED);
2133 /* The address was invalid. */
2140 error = thread_check_susp(td, true);
2146 * If this failed the lock could
2153 MPASS(owner == old_owner);
2154 umtxq_lock(&uq->uq_key);
2155 umtxq_busy(&uq->uq_key);
2156 error = umtx_pi_claim(pi, td);
2157 umtxq_unbusy(&uq->uq_key);
2158 umtxq_unlock(&uq->uq_key);
2161 * Since we're going to return an
2162 * error, restore the m_owner to its
2163 * previous, unowned state to avoid
2164 * compounding the problem.
2166 (void)casuword32(&m->m_owner,
2167 id | UMUTEX_CONTESTED, old_owner);
2169 if (error == 0 && old_owner == UMUTEX_RB_OWNERDEAD)
2174 if ((owner & ~UMUTEX_CONTESTED) == id) {
2185 * If we caught a signal, we have retried and now
2191 umtxq_lock(&uq->uq_key);
2192 umtxq_busy(&uq->uq_key);
2193 umtxq_unlock(&uq->uq_key);
2196 * Set the contested bit so that a release in user space
2197 * knows to use the system call for unlock. If this fails
2198 * either some one else has acquired the lock or it has been
2201 rv = casueword32(&m->m_owner, owner, &old, owner |
2204 /* The address was invalid. */
2206 umtxq_unbusy_unlocked(&uq->uq_key);
2211 umtxq_unbusy_unlocked(&uq->uq_key);
2212 error = thread_check_susp(td, true);
2217 * The lock changed and we need to retry or we
2218 * lost a race to the thread unlocking the
2219 * umtx. Note that the UMUTEX_RB_OWNERDEAD
2220 * value for owner is impossible there.
2225 umtxq_lock(&uq->uq_key);
2227 /* We set the contested bit, sleep. */
2228 MPASS(old == owner);
2229 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
2230 "umtxpi", timeout == NULL ? NULL : &timo,
2231 (flags & USYNC_PROCESS_SHARED) != 0);
2235 error = thread_check_susp(td, false);
2240 umtxq_lock(&uq->uq_key);
2242 umtxq_unlock(&uq->uq_key);
2244 umtx_key_release(&uq->uq_key);
2249 * Unlock a PI mutex.
2252 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2254 struct umtx_key key;
2255 struct umtx_q *uq_first, *uq_first2, *uq_me;
2256 struct umtx_pi *pi, *pi2;
2257 uint32_t id, new_owner, old, owner;
2258 int count, error, pri;
2264 * Make sure we own this mtx.
2266 error = fueword32(&m->m_owner, &owner);
2270 if ((owner & ~UMUTEX_CONTESTED) != id)
2273 new_owner = umtx_unlock_val(flags, rb);
2275 /* This should be done in userland */
2276 if ((owner & UMUTEX_CONTESTED) == 0) {
2277 error = casueword32(&m->m_owner, owner, &old, new_owner);
2281 error = thread_check_susp(td, true);
2291 /* We should only ever be in here for contested locks */
2292 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2293 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2299 count = umtxq_count_pi(&key, &uq_first);
2300 if (uq_first != NULL) {
2301 mtx_lock(&umtx_lock);
2302 pi = uq_first->uq_pi_blocked;
2303 KASSERT(pi != NULL, ("pi == NULL?"));
2304 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2305 mtx_unlock(&umtx_lock);
2308 umtx_key_release(&key);
2309 /* userland messed the mutex */
2312 uq_me = td->td_umtxq;
2313 if (pi->pi_owner == td)
2315 /* get highest priority thread which is still sleeping. */
2316 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2317 while (uq_first != NULL &&
2318 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2319 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2322 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2323 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2324 if (uq_first2 != NULL) {
2325 if (pri > UPRI(uq_first2->uq_thread))
2326 pri = UPRI(uq_first2->uq_thread);
2330 sched_lend_user_prio(td, pri);
2332 mtx_unlock(&umtx_lock);
2334 umtxq_signal_thread(uq_first);
2336 pi = umtx_pi_lookup(&key);
2338 * A umtx_pi can exist if a signal or timeout removed the
2339 * last waiter from the umtxq, but there is still
2340 * a thread in do_lock_pi() holding the umtx_pi.
2344 * The umtx_pi can be unowned, such as when a thread
2345 * has just entered do_lock_pi(), allocated the
2346 * umtx_pi, and unlocked the umtxq.
2347 * If the current thread owns it, it must disown it.
2349 mtx_lock(&umtx_lock);
2350 if (pi->pi_owner == td)
2352 mtx_unlock(&umtx_lock);
2358 * When unlocking the umtx, it must be marked as unowned if
2359 * there is zero or one thread only waiting for it.
2360 * Otherwise, it must be marked as contested.
2364 new_owner |= UMUTEX_CONTESTED;
2366 error = casueword32(&m->m_owner, owner, &old, new_owner);
2368 error = thread_check_susp(td, false);
2372 umtxq_unbusy_unlocked(&key);
2373 umtx_key_release(&key);
2376 if (error == 0 && old != owner)
2385 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2386 struct _umtx_time *timeout, int try)
2388 struct umtx_abs_timeout timo;
2389 struct umtx_q *uq, *uq2;
2393 int error, pri, old_inherited_pri, su, rv;
2397 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2398 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2402 if (timeout != NULL)
2403 umtx_abs_timeout_init2(&timo, timeout);
2405 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2407 old_inherited_pri = uq->uq_inherited_pri;
2408 umtxq_lock(&uq->uq_key);
2409 umtxq_busy(&uq->uq_key);
2410 umtxq_unlock(&uq->uq_key);
2412 rv = fueword32(&m->m_ceilings[0], &ceiling);
2417 ceiling = RTP_PRIO_MAX - ceiling;
2418 if (ceiling > RTP_PRIO_MAX) {
2423 mtx_lock(&umtx_lock);
2424 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2425 mtx_unlock(&umtx_lock);
2429 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2430 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2432 if (uq->uq_inherited_pri < UPRI(td))
2433 sched_lend_user_prio(td, uq->uq_inherited_pri);
2436 mtx_unlock(&umtx_lock);
2438 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2439 id | UMUTEX_CONTESTED);
2440 /* The address was invalid. */
2446 MPASS(owner == UMUTEX_CONTESTED);
2451 if (owner == UMUTEX_RB_OWNERDEAD) {
2452 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2453 &owner, id | UMUTEX_CONTESTED);
2459 MPASS(owner == UMUTEX_RB_OWNERDEAD);
2460 error = EOWNERDEAD; /* success */
2465 * rv == 1, only check for suspension if we
2466 * did not already catched a signal. If we
2467 * get an error from the check, the same
2468 * condition is checked by the umtxq_sleep()
2469 * call below, so we should obliterate the
2470 * error to not skip the last loop iteration.
2473 error = thread_check_susp(td, false);
2482 } else if (owner == UMUTEX_RB_NOTRECOV) {
2483 error = ENOTRECOVERABLE;
2490 * If we caught a signal, we have retried and now
2496 umtxq_lock(&uq->uq_key);
2498 umtxq_unbusy(&uq->uq_key);
2499 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2502 umtxq_unlock(&uq->uq_key);
2504 mtx_lock(&umtx_lock);
2505 uq->uq_inherited_pri = old_inherited_pri;
2507 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2508 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2510 if (pri > UPRI(uq2->uq_thread))
2511 pri = UPRI(uq2->uq_thread);
2514 if (pri > uq->uq_inherited_pri)
2515 pri = uq->uq_inherited_pri;
2517 sched_lend_user_prio(td, pri);
2519 mtx_unlock(&umtx_lock);
2522 if (error != 0 && error != EOWNERDEAD) {
2523 mtx_lock(&umtx_lock);
2524 uq->uq_inherited_pri = old_inherited_pri;
2526 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2527 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2529 if (pri > UPRI(uq2->uq_thread))
2530 pri = UPRI(uq2->uq_thread);
2533 if (pri > uq->uq_inherited_pri)
2534 pri = uq->uq_inherited_pri;
2536 sched_lend_user_prio(td, pri);
2538 mtx_unlock(&umtx_lock);
2542 umtxq_unbusy_unlocked(&uq->uq_key);
2543 umtx_key_release(&uq->uq_key);
2548 * Unlock a PP mutex.
2551 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2553 struct umtx_key key;
2554 struct umtx_q *uq, *uq2;
2556 uint32_t id, owner, rceiling;
2557 int error, pri, new_inherited_pri, su;
2561 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2564 * Make sure we own this mtx.
2566 error = fueword32(&m->m_owner, &owner);
2570 if ((owner & ~UMUTEX_CONTESTED) != id)
2573 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2578 new_inherited_pri = PRI_MAX;
2580 rceiling = RTP_PRIO_MAX - rceiling;
2581 if (rceiling > RTP_PRIO_MAX)
2583 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2586 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2587 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2594 * For priority protected mutex, always set unlocked state
2595 * to UMUTEX_CONTESTED, so that userland always enters kernel
2596 * to lock the mutex, it is necessary because thread priority
2597 * has to be adjusted for such mutex.
2599 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2604 umtxq_signal(&key, 1);
2611 mtx_lock(&umtx_lock);
2613 uq->uq_inherited_pri = new_inherited_pri;
2615 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2616 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2618 if (pri > UPRI(uq2->uq_thread))
2619 pri = UPRI(uq2->uq_thread);
2622 if (pri > uq->uq_inherited_pri)
2623 pri = uq->uq_inherited_pri;
2625 sched_lend_user_prio(td, pri);
2627 mtx_unlock(&umtx_lock);
2629 umtx_key_release(&key);
2634 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2635 uint32_t *old_ceiling)
2638 uint32_t flags, id, owner, save_ceiling;
2641 error = fueword32(&m->m_flags, &flags);
2644 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2646 if (ceiling > RTP_PRIO_MAX)
2650 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2651 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2655 umtxq_lock(&uq->uq_key);
2656 umtxq_busy(&uq->uq_key);
2657 umtxq_unlock(&uq->uq_key);
2659 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2665 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2666 id | UMUTEX_CONTESTED);
2673 MPASS(owner == UMUTEX_CONTESTED);
2674 rv = suword32(&m->m_ceilings[0], ceiling);
2675 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2676 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2680 if ((owner & ~UMUTEX_CONTESTED) == id) {
2681 rv = suword32(&m->m_ceilings[0], ceiling);
2682 error = rv == 0 ? 0 : EFAULT;
2686 if (owner == UMUTEX_RB_OWNERDEAD) {
2689 } else if (owner == UMUTEX_RB_NOTRECOV) {
2690 error = ENOTRECOVERABLE;
2695 * If we caught a signal, we have retried and now
2702 * We set the contested bit, sleep. Otherwise the lock changed
2703 * and we need to retry or we lost a race to the thread
2704 * unlocking the umtx.
2706 umtxq_lock(&uq->uq_key);
2708 umtxq_unbusy(&uq->uq_key);
2709 error = umtxq_sleep(uq, "umtxpp", NULL);
2711 umtxq_unlock(&uq->uq_key);
2713 umtxq_lock(&uq->uq_key);
2715 umtxq_signal(&uq->uq_key, INT_MAX);
2716 umtxq_unbusy(&uq->uq_key);
2717 umtxq_unlock(&uq->uq_key);
2718 umtx_key_release(&uq->uq_key);
2719 if (error == 0 && old_ceiling != NULL) {
2720 rv = suword32(old_ceiling, save_ceiling);
2721 error = rv == 0 ? 0 : EFAULT;
2727 * Lock a userland POSIX mutex.
2730 do_lock_umutex(struct thread *td, struct umutex *m,
2731 struct _umtx_time *timeout, int mode)
2736 error = fueword32(&m->m_flags, &flags);
2740 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2742 error = do_lock_normal(td, m, flags, timeout, mode);
2744 case UMUTEX_PRIO_INHERIT:
2745 error = do_lock_pi(td, m, flags, timeout, mode);
2747 case UMUTEX_PRIO_PROTECT:
2748 error = do_lock_pp(td, m, flags, timeout, mode);
2753 if (timeout == NULL) {
2754 if (error == EINTR && mode != _UMUTEX_WAIT)
2757 /* Timed-locking is not restarted. */
2758 if (error == ERESTART)
2765 * Unlock a userland POSIX mutex.
2768 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2773 error = fueword32(&m->m_flags, &flags);
2777 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2779 return (do_unlock_normal(td, m, flags, rb));
2780 case UMUTEX_PRIO_INHERIT:
2781 return (do_unlock_pi(td, m, flags, rb));
2782 case UMUTEX_PRIO_PROTECT:
2783 return (do_unlock_pp(td, m, flags, rb));
2790 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2791 struct timespec *timeout, u_long wflags)
2793 struct umtx_abs_timeout timo;
2795 uint32_t flags, clockid, hasw;
2799 error = fueword32(&cv->c_flags, &flags);
2802 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2806 if ((wflags & CVWAIT_CLOCKID) != 0) {
2807 error = fueword32(&cv->c_clockid, &clockid);
2809 umtx_key_release(&uq->uq_key);
2812 if (clockid < CLOCK_REALTIME ||
2813 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2814 /* hmm, only HW clock id will work. */
2815 umtx_key_release(&uq->uq_key);
2819 clockid = CLOCK_REALTIME;
2822 umtxq_lock(&uq->uq_key);
2823 umtxq_busy(&uq->uq_key);
2825 umtxq_unlock(&uq->uq_key);
2828 * Set c_has_waiters to 1 before releasing user mutex, also
2829 * don't modify cache line when unnecessary.
2831 error = fueword32(&cv->c_has_waiters, &hasw);
2832 if (error == 0 && hasw == 0)
2833 suword32(&cv->c_has_waiters, 1);
2835 umtxq_unbusy_unlocked(&uq->uq_key);
2837 error = do_unlock_umutex(td, m, false);
2839 if (timeout != NULL)
2840 umtx_abs_timeout_init(&timo, clockid,
2841 (wflags & CVWAIT_ABSTIME) != 0, timeout);
2843 umtxq_lock(&uq->uq_key);
2845 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2849 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2853 * This must be timeout,interrupted by signal or
2854 * surprious wakeup, clear c_has_waiter flag when
2857 umtxq_busy(&uq->uq_key);
2858 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2859 int oldlen = uq->uq_cur_queue->length;
2862 umtxq_unlock(&uq->uq_key);
2863 suword32(&cv->c_has_waiters, 0);
2864 umtxq_lock(&uq->uq_key);
2867 umtxq_unbusy(&uq->uq_key);
2868 if (error == ERESTART)
2872 umtxq_unlock(&uq->uq_key);
2873 umtx_key_release(&uq->uq_key);
2878 * Signal a userland condition variable.
2881 do_cv_signal(struct thread *td, struct ucond *cv)
2883 struct umtx_key key;
2884 int error, cnt, nwake;
2887 error = fueword32(&cv->c_flags, &flags);
2890 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2894 cnt = umtxq_count(&key);
2895 nwake = umtxq_signal(&key, 1);
2898 error = suword32(&cv->c_has_waiters, 0);
2905 umtx_key_release(&key);
2910 do_cv_broadcast(struct thread *td, struct ucond *cv)
2912 struct umtx_key key;
2916 error = fueword32(&cv->c_flags, &flags);
2919 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2924 umtxq_signal(&key, INT_MAX);
2927 error = suword32(&cv->c_has_waiters, 0);
2931 umtxq_unbusy_unlocked(&key);
2933 umtx_key_release(&key);
2938 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
2939 struct _umtx_time *timeout)
2941 struct umtx_abs_timeout timo;
2943 uint32_t flags, wrflags;
2944 int32_t state, oldstate;
2945 int32_t blocked_readers;
2946 int error, error1, rv;
2949 error = fueword32(&rwlock->rw_flags, &flags);
2952 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2956 if (timeout != NULL)
2957 umtx_abs_timeout_init2(&timo, timeout);
2959 wrflags = URWLOCK_WRITE_OWNER;
2960 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2961 wrflags |= URWLOCK_WRITE_WAITERS;
2964 rv = fueword32(&rwlock->rw_state, &state);
2966 umtx_key_release(&uq->uq_key);
2970 /* try to lock it */
2971 while (!(state & wrflags)) {
2972 if (__predict_false(URWLOCK_READER_COUNT(state) ==
2973 URWLOCK_MAX_READERS)) {
2974 umtx_key_release(&uq->uq_key);
2977 rv = casueword32(&rwlock->rw_state, state,
2978 &oldstate, state + 1);
2980 umtx_key_release(&uq->uq_key);
2984 MPASS(oldstate == state);
2985 umtx_key_release(&uq->uq_key);
2988 error = thread_check_susp(td, true);
2997 /* grab monitor lock */
2998 umtxq_lock(&uq->uq_key);
2999 umtxq_busy(&uq->uq_key);
3000 umtxq_unlock(&uq->uq_key);
3003 * re-read the state, in case it changed between the try-lock above
3004 * and the check below
3006 rv = fueword32(&rwlock->rw_state, &state);
3010 /* set read contention bit */
3011 while (error == 0 && (state & wrflags) &&
3012 !(state & URWLOCK_READ_WAITERS)) {
3013 rv = casueword32(&rwlock->rw_state, state,
3014 &oldstate, state | URWLOCK_READ_WAITERS);
3020 MPASS(oldstate == state);
3024 error = thread_check_susp(td, false);
3029 umtxq_unbusy_unlocked(&uq->uq_key);
3033 /* state is changed while setting flags, restart */
3034 if (!(state & wrflags)) {
3035 umtxq_unbusy_unlocked(&uq->uq_key);
3036 error = thread_check_susp(td, true);
3044 * Contention bit is set, before sleeping, increase
3045 * read waiter count.
3047 rv = fueword32(&rwlock->rw_blocked_readers,
3050 umtxq_unbusy_unlocked(&uq->uq_key);
3054 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
3056 while (state & wrflags) {
3057 umtxq_lock(&uq->uq_key);
3059 umtxq_unbusy(&uq->uq_key);
3061 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
3064 umtxq_busy(&uq->uq_key);
3066 umtxq_unlock(&uq->uq_key);
3069 rv = fueword32(&rwlock->rw_state, &state);
3076 /* decrease read waiter count, and may clear read contention bit */
3077 rv = fueword32(&rwlock->rw_blocked_readers,
3080 umtxq_unbusy_unlocked(&uq->uq_key);
3084 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
3085 if (blocked_readers == 1) {
3086 rv = fueword32(&rwlock->rw_state, &state);
3088 umtxq_unbusy_unlocked(&uq->uq_key);
3093 rv = casueword32(&rwlock->rw_state, state,
3094 &oldstate, state & ~URWLOCK_READ_WAITERS);
3100 MPASS(oldstate == state);
3104 error1 = thread_check_susp(td, false);
3113 umtxq_unbusy_unlocked(&uq->uq_key);
3117 umtx_key_release(&uq->uq_key);
3118 if (error == ERESTART)
3124 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
3126 struct umtx_abs_timeout timo;
3129 int32_t state, oldstate;
3130 int32_t blocked_writers;
3131 int32_t blocked_readers;
3132 int error, error1, rv;
3135 error = fueword32(&rwlock->rw_flags, &flags);
3138 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3142 if (timeout != NULL)
3143 umtx_abs_timeout_init2(&timo, timeout);
3145 blocked_readers = 0;
3147 rv = fueword32(&rwlock->rw_state, &state);
3149 umtx_key_release(&uq->uq_key);
3152 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
3153 URWLOCK_READER_COUNT(state) == 0) {
3154 rv = casueword32(&rwlock->rw_state, state,
3155 &oldstate, state | URWLOCK_WRITE_OWNER);
3157 umtx_key_release(&uq->uq_key);
3161 MPASS(oldstate == state);
3162 umtx_key_release(&uq->uq_key);
3166 error = thread_check_susp(td, true);
3172 if ((state & (URWLOCK_WRITE_OWNER |
3173 URWLOCK_WRITE_WAITERS)) == 0 &&
3174 blocked_readers != 0) {
3175 umtxq_lock(&uq->uq_key);
3176 umtxq_busy(&uq->uq_key);
3177 umtxq_signal_queue(&uq->uq_key, INT_MAX,
3179 umtxq_unbusy(&uq->uq_key);
3180 umtxq_unlock(&uq->uq_key);
3186 /* grab monitor lock */
3187 umtxq_lock(&uq->uq_key);
3188 umtxq_busy(&uq->uq_key);
3189 umtxq_unlock(&uq->uq_key);
3192 * Re-read the state, in case it changed between the
3193 * try-lock above and the check below.
3195 rv = fueword32(&rwlock->rw_state, &state);
3199 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
3200 URWLOCK_READER_COUNT(state) != 0) &&
3201 (state & URWLOCK_WRITE_WAITERS) == 0) {
3202 rv = casueword32(&rwlock->rw_state, state,
3203 &oldstate, state | URWLOCK_WRITE_WAITERS);
3209 MPASS(oldstate == state);
3213 error = thread_check_susp(td, false);
3218 umtxq_unbusy_unlocked(&uq->uq_key);
3222 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
3223 URWLOCK_READER_COUNT(state) == 0) {
3224 umtxq_unbusy_unlocked(&uq->uq_key);
3225 error = thread_check_susp(td, false);
3231 rv = fueword32(&rwlock->rw_blocked_writers,
3234 umtxq_unbusy_unlocked(&uq->uq_key);
3238 suword32(&rwlock->rw_blocked_writers, blocked_writers + 1);
3240 while ((state & URWLOCK_WRITE_OWNER) ||
3241 URWLOCK_READER_COUNT(state) != 0) {
3242 umtxq_lock(&uq->uq_key);
3243 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3244 umtxq_unbusy(&uq->uq_key);
3246 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
3249 umtxq_busy(&uq->uq_key);
3250 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3251 umtxq_unlock(&uq->uq_key);
3254 rv = fueword32(&rwlock->rw_state, &state);
3261 rv = fueword32(&rwlock->rw_blocked_writers,
3264 umtxq_unbusy_unlocked(&uq->uq_key);
3268 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
3269 if (blocked_writers == 1) {
3270 rv = fueword32(&rwlock->rw_state, &state);
3272 umtxq_unbusy_unlocked(&uq->uq_key);
3277 rv = casueword32(&rwlock->rw_state, state,
3278 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3284 MPASS(oldstate == state);
3288 error1 = thread_check_susp(td, false);
3290 * We are leaving the URWLOCK_WRITE_WAITERS
3291 * behind, but this should not harm the
3300 rv = fueword32(&rwlock->rw_blocked_readers,
3303 umtxq_unbusy_unlocked(&uq->uq_key);
3308 blocked_readers = 0;
3310 umtxq_unbusy_unlocked(&uq->uq_key);
3313 umtx_key_release(&uq->uq_key);
3314 if (error == ERESTART)
3320 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3324 int32_t state, oldstate;
3325 int error, rv, q, count;
3328 error = fueword32(&rwlock->rw_flags, &flags);
3331 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3335 error = fueword32(&rwlock->rw_state, &state);
3340 if (state & URWLOCK_WRITE_OWNER) {
3342 rv = casueword32(&rwlock->rw_state, state,
3343 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3350 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3354 error = thread_check_susp(td, true);
3360 } else if (URWLOCK_READER_COUNT(state) != 0) {
3362 rv = casueword32(&rwlock->rw_state, state,
3363 &oldstate, state - 1);
3370 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3374 error = thread_check_susp(td, true);
3387 if (!(flags & URWLOCK_PREFER_READER)) {
3388 if (state & URWLOCK_WRITE_WAITERS) {
3390 q = UMTX_EXCLUSIVE_QUEUE;
3391 } else if (state & URWLOCK_READ_WAITERS) {
3393 q = UMTX_SHARED_QUEUE;
3396 if (state & URWLOCK_READ_WAITERS) {
3398 q = UMTX_SHARED_QUEUE;
3399 } else if (state & URWLOCK_WRITE_WAITERS) {
3401 q = UMTX_EXCLUSIVE_QUEUE;
3406 umtxq_lock(&uq->uq_key);
3407 umtxq_busy(&uq->uq_key);
3408 umtxq_signal_queue(&uq->uq_key, count, q);
3409 umtxq_unbusy(&uq->uq_key);
3410 umtxq_unlock(&uq->uq_key);
3413 umtx_key_release(&uq->uq_key);
3417 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3419 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3421 struct umtx_abs_timeout timo;
3423 uint32_t flags, count, count1;
3427 error = fueword32(&sem->_flags, &flags);
3430 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3434 if (timeout != NULL)
3435 umtx_abs_timeout_init2(&timo, timeout);
3438 umtxq_lock(&uq->uq_key);
3439 umtxq_busy(&uq->uq_key);
3441 umtxq_unlock(&uq->uq_key);
3442 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3444 rv1 = fueword32(&sem->_count, &count);
3445 if (rv == -1 || (rv == 0 && (rv1 == -1 || count != 0)) ||
3446 (rv == 1 && count1 == 0)) {
3447 umtxq_lock(&uq->uq_key);
3448 umtxq_unbusy(&uq->uq_key);
3450 umtxq_unlock(&uq->uq_key);
3452 rv = thread_check_susp(td, true);
3460 error = rv == -1 ? EFAULT : 0;
3463 umtxq_lock(&uq->uq_key);
3464 umtxq_unbusy(&uq->uq_key);
3466 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3468 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3472 /* A relative timeout cannot be restarted. */
3473 if (error == ERESTART && timeout != NULL &&
3474 (timeout->_flags & UMTX_ABSTIME) == 0)
3477 umtxq_unlock(&uq->uq_key);
3479 umtx_key_release(&uq->uq_key);
3484 * Signal a userland semaphore.
3487 do_sem_wake(struct thread *td, struct _usem *sem)
3489 struct umtx_key key;
3493 error = fueword32(&sem->_flags, &flags);
3496 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3500 cnt = umtxq_count(&key);
3503 * Check if count is greater than 0, this means the memory is
3504 * still being referenced by user code, so we can safely
3505 * update _has_waiters flag.
3509 error = suword32(&sem->_has_waiters, 0);
3514 umtxq_signal(&key, 1);
3518 umtx_key_release(&key);
3524 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3526 struct umtx_abs_timeout timo;
3528 uint32_t count, flags;
3532 flags = fuword32(&sem->_flags);
3533 if (timeout != NULL)
3534 umtx_abs_timeout_init2(&timo, timeout);
3537 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3540 umtxq_lock(&uq->uq_key);
3541 umtxq_busy(&uq->uq_key);
3543 umtxq_unlock(&uq->uq_key);
3544 rv = fueword32(&sem->_count, &count);
3546 umtxq_lock(&uq->uq_key);
3547 umtxq_unbusy(&uq->uq_key);
3549 umtxq_unlock(&uq->uq_key);
3550 umtx_key_release(&uq->uq_key);
3554 if (USEM_COUNT(count) != 0) {
3555 umtxq_lock(&uq->uq_key);
3556 umtxq_unbusy(&uq->uq_key);
3558 umtxq_unlock(&uq->uq_key);
3559 umtx_key_release(&uq->uq_key);
3562 if (count == USEM_HAS_WAITERS)
3564 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3567 umtxq_lock(&uq->uq_key);
3568 umtxq_unbusy(&uq->uq_key);
3570 umtxq_unlock(&uq->uq_key);
3571 umtx_key_release(&uq->uq_key);
3574 rv = thread_check_susp(td, true);
3579 umtxq_lock(&uq->uq_key);
3580 umtxq_unbusy(&uq->uq_key);
3582 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3584 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3588 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3589 /* A relative timeout cannot be restarted. */
3590 if (error == ERESTART)
3592 if (error == EINTR) {
3593 umtx_abs_timeout_update(&timo);
3594 timespecsub(&timo.end, &timo.cur,
3595 &timeout->_timeout);
3599 umtxq_unlock(&uq->uq_key);
3600 umtx_key_release(&uq->uq_key);
3605 * Signal a userland semaphore.
3608 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3610 struct umtx_key key;
3612 uint32_t count, flags;
3614 rv = fueword32(&sem->_flags, &flags);
3617 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3621 cnt = umtxq_count(&key);
3624 * If this was the last sleeping thread, clear the waiters
3629 rv = fueword32(&sem->_count, &count);
3630 while (rv != -1 && count & USEM_HAS_WAITERS) {
3631 rv = casueword32(&sem->_count, count, &count,
3632 count & ~USEM_HAS_WAITERS);
3634 rv = thread_check_susp(td, true);
3647 umtxq_signal(&key, 1);
3651 umtx_key_release(&key);
3655 #ifdef COMPAT_FREEBSD10
3657 freebsd10__umtx_lock(struct thread *td, struct freebsd10__umtx_lock_args *uap)
3659 return (do_lock_umtx(td, uap->umtx, td->td_tid, 0));
3663 freebsd10__umtx_unlock(struct thread *td,
3664 struct freebsd10__umtx_unlock_args *uap)
3666 return (do_unlock_umtx(td, uap->umtx, td->td_tid));
3671 umtx_copyin_timeout(const void *uaddr, struct timespec *tsp)
3675 error = copyin(uaddr, tsp, sizeof(*tsp));
3677 if (tsp->tv_sec < 0 ||
3678 tsp->tv_nsec >= 1000000000 ||
3686 umtx_copyin_umtx_time(const void *uaddr, size_t size, struct _umtx_time *tp)
3690 if (size <= sizeof(tp->_timeout)) {
3691 tp->_clockid = CLOCK_REALTIME;
3693 error = copyin(uaddr, &tp->_timeout, sizeof(tp->_timeout));
3695 error = copyin(uaddr, tp, sizeof(*tp));
3698 if (tp->_timeout.tv_sec < 0 ||
3699 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3705 umtx_copyin_robust_lists(const void *uaddr, size_t size,
3706 struct umtx_robust_lists_params *rb)
3709 if (size > sizeof(*rb))
3711 return (copyin(uaddr, rb, size));
3715 umtx_copyout_timeout(void *uaddr, size_t sz, struct timespec *tsp)
3719 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
3720 * and we're only called if sz >= sizeof(timespec) as supplied in the
3723 KASSERT(sz >= sizeof(*tsp),
3724 ("umtx_copyops specifies incorrect sizes"));
3726 return (copyout(tsp, uaddr, sizeof(*tsp)));
3729 #ifdef COMPAT_FREEBSD10
3731 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap,
3732 const struct umtx_copyops *ops)
3734 struct timespec *ts, timeout;
3737 /* Allow a null timespec (wait forever). */
3738 if (uap->uaddr2 == NULL)
3741 error = ops->copyin_timeout(uap->uaddr2, &timeout);
3746 #ifdef COMPAT_FREEBSD32
3748 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
3750 return (do_lock_umtx(td, uap->obj, uap->val, ts));
3754 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap,
3755 const struct umtx_copyops *ops)
3757 #ifdef COMPAT_FREEBSD32
3759 return (do_unlock_umtx32(td, uap->obj, uap->val));
3761 return (do_unlock_umtx(td, uap->obj, uap->val));
3763 #endif /* COMPAT_FREEBSD10 */
3765 #if !defined(COMPAT_FREEBSD10)
3767 __umtx_op_unimpl(struct thread *td __unused, struct _umtx_op_args *uap __unused,
3768 const struct umtx_copyops *ops __unused)
3770 return (EOPNOTSUPP);
3772 #endif /* COMPAT_FREEBSD10 */
3775 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap,
3776 const struct umtx_copyops *ops)
3778 struct _umtx_time timeout, *tm_p;
3781 if (uap->uaddr2 == NULL)
3784 error = ops->copyin_umtx_time(
3785 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3790 return (do_wait(td, uap->obj, uap->val, tm_p, ops->compat32, 0));
3794 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap,
3795 const struct umtx_copyops *ops)
3797 struct _umtx_time timeout, *tm_p;
3800 if (uap->uaddr2 == NULL)
3803 error = ops->copyin_umtx_time(
3804 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3809 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3813 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap,
3814 const struct umtx_copyops *ops)
3816 struct _umtx_time *tm_p, timeout;
3819 if (uap->uaddr2 == NULL)
3822 error = ops->copyin_umtx_time(
3823 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3828 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3832 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap,
3833 const struct umtx_copyops *ops __unused)
3836 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3839 #define BATCH_SIZE 128
3841 __umtx_op_nwake_private_native(struct thread *td, struct _umtx_op_args *uap)
3843 char *uaddrs[BATCH_SIZE], **upp;
3844 int count, error, i, pos, tocopy;
3846 upp = (char **)uap->obj;
3848 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3850 tocopy = MIN(count, BATCH_SIZE);
3851 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3854 for (i = 0; i < tocopy; ++i) {
3855 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3863 __umtx_op_nwake_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3865 uint32_t uaddrs[BATCH_SIZE], *upp;
3866 int count, error, i, pos, tocopy;
3868 upp = (uint32_t *)uap->obj;
3870 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3872 tocopy = MIN(count, BATCH_SIZE);
3873 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
3876 for (i = 0; i < tocopy; ++i) {
3877 kern_umtx_wake(td, (void *)(uintptr_t)uaddrs[i],
3886 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap,
3887 const struct umtx_copyops *ops)
3891 return (__umtx_op_nwake_private_compat32(td, uap));
3892 return (__umtx_op_nwake_private_native(td, uap));
3896 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap,
3897 const struct umtx_copyops *ops __unused)
3900 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3904 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap,
3905 const struct umtx_copyops *ops)
3907 struct _umtx_time *tm_p, timeout;
3910 /* Allow a null timespec (wait forever). */
3911 if (uap->uaddr2 == NULL)
3914 error = ops->copyin_umtx_time(
3915 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3920 return (do_lock_umutex(td, uap->obj, tm_p, 0));
3924 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap,
3925 const struct umtx_copyops *ops __unused)
3928 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
3932 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap,
3933 const struct umtx_copyops *ops)
3935 struct _umtx_time *tm_p, timeout;
3938 /* Allow a null timespec (wait forever). */
3939 if (uap->uaddr2 == NULL)
3942 error = ops->copyin_umtx_time(
3943 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3948 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
3952 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap,
3953 const struct umtx_copyops *ops __unused)
3956 return (do_wake_umutex(td, uap->obj));
3960 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap,
3961 const struct umtx_copyops *ops __unused)
3964 return (do_unlock_umutex(td, uap->obj, false));
3968 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap,
3969 const struct umtx_copyops *ops __unused)
3972 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
3976 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap,
3977 const struct umtx_copyops *ops)
3979 struct timespec *ts, timeout;
3982 /* Allow a null timespec (wait forever). */
3983 if (uap->uaddr2 == NULL)
3986 error = ops->copyin_timeout(uap->uaddr2, &timeout);
3991 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3995 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap,
3996 const struct umtx_copyops *ops __unused)
3999 return (do_cv_signal(td, uap->obj));
4003 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap,
4004 const struct umtx_copyops *ops __unused)
4007 return (do_cv_broadcast(td, uap->obj));
4011 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap,
4012 const struct umtx_copyops *ops)
4014 struct _umtx_time timeout;
4017 /* Allow a null timespec (wait forever). */
4018 if (uap->uaddr2 == NULL) {
4019 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4021 error = ops->copyin_umtx_time(uap->uaddr2,
4022 (size_t)uap->uaddr1, &timeout);
4025 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4031 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap,
4032 const struct umtx_copyops *ops)
4034 struct _umtx_time timeout;
4037 /* Allow a null timespec (wait forever). */
4038 if (uap->uaddr2 == NULL) {
4039 error = do_rw_wrlock(td, uap->obj, 0);
4041 error = ops->copyin_umtx_time(uap->uaddr2,
4042 (size_t)uap->uaddr1, &timeout);
4046 error = do_rw_wrlock(td, uap->obj, &timeout);
4052 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap,
4053 const struct umtx_copyops *ops __unused)
4056 return (do_rw_unlock(td, uap->obj));
4059 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4061 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap,
4062 const struct umtx_copyops *ops)
4064 struct _umtx_time *tm_p, timeout;
4067 /* Allow a null timespec (wait forever). */
4068 if (uap->uaddr2 == NULL)
4071 error = ops->copyin_umtx_time(
4072 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4077 return (do_sem_wait(td, uap->obj, tm_p));
4081 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap,
4082 const struct umtx_copyops *ops __unused)
4085 return (do_sem_wake(td, uap->obj));
4090 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap,
4091 const struct umtx_copyops *ops __unused)
4094 return (do_wake2_umutex(td, uap->obj, uap->val));
4098 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap,
4099 const struct umtx_copyops *ops)
4101 struct _umtx_time *tm_p, timeout;
4105 /* Allow a null timespec (wait forever). */
4106 if (uap->uaddr2 == NULL) {
4110 uasize = (size_t)uap->uaddr1;
4111 error = ops->copyin_umtx_time(uap->uaddr2, uasize, &timeout);
4116 error = do_sem2_wait(td, uap->obj, tm_p);
4117 if (error == EINTR && uap->uaddr2 != NULL &&
4118 (timeout._flags & UMTX_ABSTIME) == 0 &&
4119 uasize >= ops->umtx_time_sz + ops->timespec_sz) {
4120 error = ops->copyout_timeout(
4121 (void *)((uintptr_t)uap->uaddr2 + ops->umtx_time_sz),
4122 uasize - ops->umtx_time_sz, &timeout._timeout);
4132 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap,
4133 const struct umtx_copyops *ops __unused)
4136 return (do_sem2_wake(td, uap->obj));
4139 #define USHM_OBJ_UMTX(o) \
4140 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
4142 #define USHMF_REG_LINKED 0x0001
4143 #define USHMF_OBJ_LINKED 0x0002
4144 struct umtx_shm_reg {
4145 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
4146 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
4147 struct umtx_key ushm_key;
4148 struct ucred *ushm_cred;
4149 struct shmfd *ushm_obj;
4154 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
4155 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
4157 static uma_zone_t umtx_shm_reg_zone;
4158 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
4159 static struct mtx umtx_shm_lock;
4160 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
4161 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
4163 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
4166 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
4168 struct umtx_shm_reg_head d;
4169 struct umtx_shm_reg *reg, *reg1;
4172 mtx_lock(&umtx_shm_lock);
4173 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
4174 mtx_unlock(&umtx_shm_lock);
4175 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
4176 TAILQ_REMOVE(&d, reg, ushm_reg_link);
4177 umtx_shm_free_reg(reg);
4181 static struct task umtx_shm_reg_delfree_task =
4182 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
4184 static struct umtx_shm_reg *
4185 umtx_shm_find_reg_locked(const struct umtx_key *key)
4187 struct umtx_shm_reg *reg;
4188 struct umtx_shm_reg_head *reg_head;
4190 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
4191 mtx_assert(&umtx_shm_lock, MA_OWNED);
4192 reg_head = &umtx_shm_registry[key->hash];
4193 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
4194 KASSERT(reg->ushm_key.shared,
4195 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
4196 if (reg->ushm_key.info.shared.object ==
4197 key->info.shared.object &&
4198 reg->ushm_key.info.shared.offset ==
4199 key->info.shared.offset) {
4200 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
4201 KASSERT(reg->ushm_refcnt > 0,
4202 ("reg %p refcnt 0 onlist", reg));
4203 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
4204 ("reg %p not linked", reg));
4212 static struct umtx_shm_reg *
4213 umtx_shm_find_reg(const struct umtx_key *key)
4215 struct umtx_shm_reg *reg;
4217 mtx_lock(&umtx_shm_lock);
4218 reg = umtx_shm_find_reg_locked(key);
4219 mtx_unlock(&umtx_shm_lock);
4224 umtx_shm_free_reg(struct umtx_shm_reg *reg)
4227 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
4228 crfree(reg->ushm_cred);
4229 shm_drop(reg->ushm_obj);
4230 uma_zfree(umtx_shm_reg_zone, reg);
4234 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
4238 mtx_assert(&umtx_shm_lock, MA_OWNED);
4239 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
4241 res = reg->ushm_refcnt == 0;
4243 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
4244 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
4245 reg, ushm_reg_link);
4246 reg->ushm_flags &= ~USHMF_REG_LINKED;
4248 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
4249 LIST_REMOVE(reg, ushm_obj_link);
4250 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
4257 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
4263 object = reg->ushm_obj->shm_object;
4264 VM_OBJECT_WLOCK(object);
4265 object->flags |= OBJ_UMTXDEAD;
4266 VM_OBJECT_WUNLOCK(object);
4268 mtx_lock(&umtx_shm_lock);
4269 dofree = umtx_shm_unref_reg_locked(reg, force);
4270 mtx_unlock(&umtx_shm_lock);
4272 umtx_shm_free_reg(reg);
4276 umtx_shm_object_init(vm_object_t object)
4279 LIST_INIT(USHM_OBJ_UMTX(object));
4283 umtx_shm_object_terminated(vm_object_t object)
4285 struct umtx_shm_reg *reg, *reg1;
4288 if (LIST_EMPTY(USHM_OBJ_UMTX(object)))
4292 mtx_lock(&umtx_shm_lock);
4293 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
4294 if (umtx_shm_unref_reg_locked(reg, true)) {
4295 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
4300 mtx_unlock(&umtx_shm_lock);
4302 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
4306 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
4307 struct umtx_shm_reg **res)
4309 struct umtx_shm_reg *reg, *reg1;
4313 reg = umtx_shm_find_reg(key);
4318 cred = td->td_ucred;
4319 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
4321 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
4322 reg->ushm_refcnt = 1;
4323 bcopy(key, ®->ushm_key, sizeof(*key));
4324 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR, false);
4325 reg->ushm_cred = crhold(cred);
4326 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
4328 umtx_shm_free_reg(reg);
4331 mtx_lock(&umtx_shm_lock);
4332 reg1 = umtx_shm_find_reg_locked(key);
4334 mtx_unlock(&umtx_shm_lock);
4335 umtx_shm_free_reg(reg);
4340 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
4341 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
4343 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
4344 mtx_unlock(&umtx_shm_lock);
4350 umtx_shm_alive(struct thread *td, void *addr)
4353 vm_map_entry_t entry;
4360 map = &td->td_proc->p_vmspace->vm_map;
4361 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
4362 &object, &pindex, &prot, &wired);
4363 if (res != KERN_SUCCESS)
4368 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
4369 vm_map_lookup_done(map, entry);
4378 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
4379 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
4380 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
4381 for (i = 0; i < nitems(umtx_shm_registry); i++)
4382 TAILQ_INIT(&umtx_shm_registry[i]);
4386 umtx_shm(struct thread *td, void *addr, u_int flags)
4388 struct umtx_key key;
4389 struct umtx_shm_reg *reg;
4393 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
4394 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
4396 if ((flags & UMTX_SHM_ALIVE) != 0)
4397 return (umtx_shm_alive(td, addr));
4398 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
4401 KASSERT(key.shared == 1, ("non-shared key"));
4402 if ((flags & UMTX_SHM_CREAT) != 0) {
4403 error = umtx_shm_create_reg(td, &key, ®);
4405 reg = umtx_shm_find_reg(&key);
4409 umtx_key_release(&key);
4412 KASSERT(reg != NULL, ("no reg"));
4413 if ((flags & UMTX_SHM_DESTROY) != 0) {
4414 umtx_shm_unref_reg(reg, true);
4418 error = mac_posixshm_check_open(td->td_ucred,
4419 reg->ushm_obj, FFLAGS(O_RDWR));
4422 error = shm_access(reg->ushm_obj, td->td_ucred,
4426 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
4428 shm_hold(reg->ushm_obj);
4429 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
4431 td->td_retval[0] = fd;
4435 umtx_shm_unref_reg(reg, false);
4440 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap,
4441 const struct umtx_copyops *ops __unused)
4444 return (umtx_shm(td, uap->uaddr1, uap->val));
4448 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap,
4449 const struct umtx_copyops *ops)
4451 struct umtx_robust_lists_params rb;
4454 if (ops->compat32) {
4455 if ((td->td_pflags2 & TDP2_COMPAT32RB) == 0 &&
4456 (td->td_rb_list != 0 || td->td_rbp_list != 0 ||
4457 td->td_rb_inact != 0))
4459 } else if ((td->td_pflags2 & TDP2_COMPAT32RB) != 0) {
4463 bzero(&rb, sizeof(rb));
4464 error = ops->copyin_robust_lists(uap->uaddr1, uap->val, &rb);
4469 td->td_pflags2 |= TDP2_COMPAT32RB;
4471 td->td_rb_list = rb.robust_list_offset;
4472 td->td_rbp_list = rb.robust_priv_list_offset;
4473 td->td_rb_inact = rb.robust_inact_offset;
4477 #if defined(__i386__) || defined(__amd64__)
4479 * Provide the standard 32-bit definitions for x86, since native/compat32 use a
4480 * 32-bit time_t there. Other architectures just need the i386 definitions
4481 * along with their standard compat32.
4483 struct timespecx32 {
4488 struct umtx_timex32 {
4489 struct timespecx32 _timeout;
4495 #define timespeci386 timespec32
4496 #define umtx_timei386 umtx_time32
4498 #else /* !__i386__ && !__amd64__ */
4499 /* 32-bit architectures can emulate i386, so define these almost everywhere. */
4500 struct timespeci386 {
4505 struct umtx_timei386 {
4506 struct timespeci386 _timeout;
4511 #if defined(__LP64__)
4512 #define timespecx32 timespec32
4513 #define umtx_timex32 umtx_time32
4518 umtx_copyin_robust_lists32(const void *uaddr, size_t size,
4519 struct umtx_robust_lists_params *rbp)
4521 struct umtx_robust_lists_params_compat32 rb32;
4524 if (size > sizeof(rb32))
4526 bzero(&rb32, sizeof(rb32));
4527 error = copyin(uaddr, &rb32, size);
4530 CP(rb32, *rbp, robust_list_offset);
4531 CP(rb32, *rbp, robust_priv_list_offset);
4532 CP(rb32, *rbp, robust_inact_offset);
4538 umtx_copyin_timeouti386(const void *uaddr, struct timespec *tsp)
4540 struct timespeci386 ts32;
4543 error = copyin(uaddr, &ts32, sizeof(ts32));
4545 if (ts32.tv_sec < 0 ||
4546 ts32.tv_nsec >= 1000000000 ||
4550 CP(ts32, *tsp, tv_sec);
4551 CP(ts32, *tsp, tv_nsec);
4558 umtx_copyin_umtx_timei386(const void *uaddr, size_t size, struct _umtx_time *tp)
4560 struct umtx_timei386 t32;
4563 t32._clockid = CLOCK_REALTIME;
4565 if (size <= sizeof(t32._timeout))
4566 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4568 error = copyin(uaddr, &t32, sizeof(t32));
4571 if (t32._timeout.tv_sec < 0 ||
4572 t32._timeout.tv_nsec >= 1000000000 || t32._timeout.tv_nsec < 0)
4574 TS_CP(t32, *tp, _timeout);
4575 CP(t32, *tp, _flags);
4576 CP(t32, *tp, _clockid);
4581 umtx_copyout_timeouti386(void *uaddr, size_t sz, struct timespec *tsp)
4583 struct timespeci386 remain32 = {
4584 .tv_sec = tsp->tv_sec,
4585 .tv_nsec = tsp->tv_nsec,
4589 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4590 * and we're only called if sz >= sizeof(timespec) as supplied in the
4593 KASSERT(sz >= sizeof(remain32),
4594 ("umtx_copyops specifies incorrect sizes"));
4596 return (copyout(&remain32, uaddr, sizeof(remain32)));
4598 #endif /* !__i386__ */
4600 #if defined(__i386__) || defined(__LP64__)
4602 umtx_copyin_timeoutx32(const void *uaddr, struct timespec *tsp)
4604 struct timespecx32 ts32;
4607 error = copyin(uaddr, &ts32, sizeof(ts32));
4609 if (ts32.tv_sec < 0 ||
4610 ts32.tv_nsec >= 1000000000 ||
4614 CP(ts32, *tsp, tv_sec);
4615 CP(ts32, *tsp, tv_nsec);
4622 umtx_copyin_umtx_timex32(const void *uaddr, size_t size, struct _umtx_time *tp)
4624 struct umtx_timex32 t32;
4627 t32._clockid = CLOCK_REALTIME;
4629 if (size <= sizeof(t32._timeout))
4630 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4632 error = copyin(uaddr, &t32, sizeof(t32));
4635 if (t32._timeout.tv_sec < 0 ||
4636 t32._timeout.tv_nsec >= 1000000000 || t32._timeout.tv_nsec < 0)
4638 TS_CP(t32, *tp, _timeout);
4639 CP(t32, *tp, _flags);
4640 CP(t32, *tp, _clockid);
4645 umtx_copyout_timeoutx32(void *uaddr, size_t sz, struct timespec *tsp)
4647 struct timespecx32 remain32 = {
4648 .tv_sec = tsp->tv_sec,
4649 .tv_nsec = tsp->tv_nsec,
4653 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4654 * and we're only called if sz >= sizeof(timespec) as supplied in the
4657 KASSERT(sz >= sizeof(remain32),
4658 ("umtx_copyops specifies incorrect sizes"));
4660 return (copyout(&remain32, uaddr, sizeof(remain32)));
4662 #endif /* __i386__ || __LP64__ */
4664 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap,
4665 const struct umtx_copyops *umtx_ops);
4667 static const _umtx_op_func op_table[] = {
4668 #ifdef COMPAT_FREEBSD10
4669 [UMTX_OP_LOCK] = __umtx_op_lock_umtx,
4670 [UMTX_OP_UNLOCK] = __umtx_op_unlock_umtx,
4672 [UMTX_OP_LOCK] = __umtx_op_unimpl,
4673 [UMTX_OP_UNLOCK] = __umtx_op_unimpl,
4675 [UMTX_OP_WAIT] = __umtx_op_wait,
4676 [UMTX_OP_WAKE] = __umtx_op_wake,
4677 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4678 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
4679 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4680 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4681 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
4682 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4683 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4684 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
4685 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
4686 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
4687 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4688 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4689 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4690 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4691 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4692 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4693 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4694 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4696 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4697 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4699 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4700 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4701 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4702 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4703 [UMTX_OP_SHM] = __umtx_op_shm,
4704 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4707 static const struct umtx_copyops umtx_native_ops = {
4708 .copyin_timeout = umtx_copyin_timeout,
4709 .copyin_umtx_time = umtx_copyin_umtx_time,
4710 .copyin_robust_lists = umtx_copyin_robust_lists,
4711 .copyout_timeout = umtx_copyout_timeout,
4712 .timespec_sz = sizeof(struct timespec),
4713 .umtx_time_sz = sizeof(struct _umtx_time),
4717 static const struct umtx_copyops umtx_native_opsi386 = {
4718 .copyin_timeout = umtx_copyin_timeouti386,
4719 .copyin_umtx_time = umtx_copyin_umtx_timei386,
4720 .copyin_robust_lists = umtx_copyin_robust_lists32,
4721 .copyout_timeout = umtx_copyout_timeouti386,
4722 .timespec_sz = sizeof(struct timespeci386),
4723 .umtx_time_sz = sizeof(struct umtx_timei386),
4728 #if defined(__i386__) || defined(__LP64__)
4729 /* i386 can emulate other 32-bit archs, too! */
4730 static const struct umtx_copyops umtx_native_opsx32 = {
4731 .copyin_timeout = umtx_copyin_timeoutx32,
4732 .copyin_umtx_time = umtx_copyin_umtx_timex32,
4733 .copyin_robust_lists = umtx_copyin_robust_lists32,
4734 .copyout_timeout = umtx_copyout_timeoutx32,
4735 .timespec_sz = sizeof(struct timespecx32),
4736 .umtx_time_sz = sizeof(struct umtx_timex32),
4740 #ifdef COMPAT_FREEBSD32
4742 #define umtx_native_ops32 umtx_native_opsi386
4744 #define umtx_native_ops32 umtx_native_opsx32
4746 #endif /* COMPAT_FREEBSD32 */
4747 #endif /* __i386__ || __LP64__ */
4749 #define UMTX_OP__FLAGS (UMTX_OP__32BIT | UMTX_OP__I386)
4752 kern__umtx_op(struct thread *td, void *obj, int op, unsigned long val,
4753 void *uaddr1, void *uaddr2, const struct umtx_copyops *ops)
4755 struct _umtx_op_args uap = {
4757 .op = op & ~UMTX_OP__FLAGS,
4763 if ((uap.op >= nitems(op_table)))
4765 return ((*op_table[uap.op])(td, &uap, ops));
4769 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4771 static const struct umtx_copyops *umtx_ops;
4773 umtx_ops = &umtx_native_ops;
4775 if ((uap->op & (UMTX_OP__32BIT | UMTX_OP__I386)) != 0) {
4776 if ((uap->op & UMTX_OP__I386) != 0)
4777 umtx_ops = &umtx_native_opsi386;
4779 umtx_ops = &umtx_native_opsx32;
4781 #elif !defined(__i386__)
4782 /* We consider UMTX_OP__32BIT a nop on !i386 ILP32. */
4783 if ((uap->op & UMTX_OP__I386) != 0)
4784 umtx_ops = &umtx_native_opsi386;
4786 /* Likewise, UMTX_OP__I386 is a nop on i386. */
4787 if ((uap->op & UMTX_OP__32BIT) != 0)
4788 umtx_ops = &umtx_native_opsx32;
4790 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr1,
4791 uap->uaddr2, umtx_ops));
4794 #ifdef COMPAT_FREEBSD32
4795 #ifdef COMPAT_FREEBSD10
4797 freebsd10_freebsd32_umtx_lock(struct thread *td,
4798 struct freebsd10_freebsd32_umtx_lock_args *uap)
4800 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
4804 freebsd10_freebsd32_umtx_unlock(struct thread *td,
4805 struct freebsd10_freebsd32_umtx_unlock_args *uap)
4807 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
4809 #endif /* COMPAT_FREEBSD10 */
4812 freebsd32__umtx_op(struct thread *td, struct freebsd32__umtx_op_args *uap)
4815 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr,
4816 uap->uaddr2, &umtx_native_ops32));
4818 #endif /* COMPAT_FREEBSD32 */
4821 umtx_thread_init(struct thread *td)
4824 td->td_umtxq = umtxq_alloc();
4825 td->td_umtxq->uq_thread = td;
4829 umtx_thread_fini(struct thread *td)
4832 umtxq_free(td->td_umtxq);
4836 * It will be called when new thread is created, e.g fork().
4839 umtx_thread_alloc(struct thread *td)
4844 uq->uq_inherited_pri = PRI_MAX;
4846 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4847 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4848 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4849 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4855 * Clear robust lists for all process' threads, not delaying the
4856 * cleanup to thread exit, since the relevant address space is
4857 * destroyed right now.
4860 umtx_exec(struct proc *p)
4864 KASSERT(p == curproc, ("need curproc"));
4865 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4866 (p->p_flag & P_STOPPED_SINGLE) != 0,
4867 ("curproc must be single-threaded"));
4869 * There is no need to lock the list as only this thread can be
4872 FOREACH_THREAD_IN_PROC(p, td) {
4873 KASSERT(td == curthread ||
4874 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4875 ("running thread %p %p", p, td));
4876 umtx_thread_cleanup(td);
4877 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4885 umtx_thread_exit(struct thread *td)
4888 umtx_thread_cleanup(td);
4892 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res, bool compat32)
4899 error = fueword32((void *)ptr, &res32);
4903 error = fueword((void *)ptr, &res1);
4913 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list,
4916 struct umutex32 m32;
4919 memcpy(&m32, m, sizeof(m32));
4920 *rb_list = m32.m_rb_lnk;
4922 *rb_list = m->m_rb_lnk;
4927 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact,
4933 KASSERT(td->td_proc == curproc, ("need current vmspace"));
4934 error = copyin((void *)rbp, &m, sizeof(m));
4937 if (rb_list != NULL)
4938 umtx_read_rb_list(td, &m, rb_list, compat32);
4939 if ((m.m_flags & UMUTEX_ROBUST) == 0)
4941 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
4942 /* inact is cleared after unlock, allow the inconsistency */
4943 return (inact ? 0 : EINVAL);
4944 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
4948 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
4949 const char *name, bool compat32)
4957 error = umtx_read_uptr(td, rb_list, &rbp, compat32);
4958 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
4959 if (rbp == *rb_inact) {
4964 error = umtx_handle_rb(td, rbp, &rbp, inact, compat32);
4966 if (i == umtx_max_rb && umtx_verbose_rb) {
4967 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
4968 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
4970 if (error != 0 && umtx_verbose_rb) {
4971 uprintf("comm %s pid %d: handling %srb error %d\n",
4972 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
4977 * Clean up umtx data.
4980 umtx_thread_cleanup(struct thread *td)
4988 * Disown pi mutexes.
4992 if (uq->uq_inherited_pri != PRI_MAX ||
4993 !TAILQ_EMPTY(&uq->uq_pi_contested)) {
4994 mtx_lock(&umtx_lock);
4995 uq->uq_inherited_pri = PRI_MAX;
4996 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
4997 pi->pi_owner = NULL;
4998 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
5000 mtx_unlock(&umtx_lock);
5002 sched_lend_user_prio_cond(td, PRI_MAX);
5005 compat32 = (td->td_pflags2 & TDP2_COMPAT32RB) != 0;
5006 td->td_pflags2 &= ~TDP2_COMPAT32RB;
5008 if (td->td_rb_inact == 0 && td->td_rb_list == 0 && td->td_rbp_list == 0)
5012 * Handle terminated robust mutexes. Must be done after
5013 * robust pi disown, otherwise unlock could see unowned
5016 rb_inact = td->td_rb_inact;
5018 (void)umtx_read_uptr(td, rb_inact, &rb_inact, compat32);
5019 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "", compat32);
5020 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ", compat32);
5022 (void)umtx_handle_rb(td, rb_inact, NULL, true, compat32);