2 * Copyright (c) 2015, 2016 The FreeBSD Foundation
3 * Copyright (c) 2004, David Xu <davidxu@freebsd.org>
4 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
7 * Portions of this software were developed by Konstantin Belousov
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice unmodified, this list of conditions, and the following
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
36 #include "opt_umtx_profiling.h"
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/fcntl.h>
42 #include <sys/filedesc.h>
43 #include <sys/limits.h>
45 #include <sys/malloc.h>
47 #include <sys/mutex.h>
50 #include <sys/resource.h>
51 #include <sys/resourcevar.h>
52 #include <sys/rwlock.h>
54 #include <sys/sched.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysent.h>
58 #include <sys/systm.h>
59 #include <sys/sysproto.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/taskqueue.h>
62 #include <sys/eventhandler.h>
65 #include <security/mac/mac_framework.h>
68 #include <vm/vm_param.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
73 #include <machine/cpu.h>
75 #ifdef COMPAT_FREEBSD32
76 #include <compat/freebsd32/freebsd32_proto.h>
80 #define _UMUTEX_WAIT 2
83 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
84 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
87 /* Priority inheritance mutex info. */
90 struct thread *pi_owner;
95 /* List entry to link umtx holding by thread */
96 TAILQ_ENTRY(umtx_pi) pi_link;
98 /* List entry in hash */
99 TAILQ_ENTRY(umtx_pi) pi_hashlink;
101 /* List for waiters */
102 TAILQ_HEAD(,umtx_q) pi_blocked;
104 /* Identify a userland lock object */
105 struct umtx_key pi_key;
108 /* A userland synchronous object user. */
110 /* Linked list for the hash. */
111 TAILQ_ENTRY(umtx_q) uq_link;
114 struct umtx_key uq_key;
118 #define UQF_UMTXQ 0x0001
120 /* The thread waits on. */
121 struct thread *uq_thread;
124 * Blocked on PI mutex. read can use chain lock
125 * or umtx_lock, write must have both chain lock and
126 * umtx_lock being hold.
128 struct umtx_pi *uq_pi_blocked;
130 /* On blocked list */
131 TAILQ_ENTRY(umtx_q) uq_lockq;
133 /* Thread contending with us */
134 TAILQ_HEAD(,umtx_pi) uq_pi_contested;
136 /* Inherited priority from PP mutex */
137 u_char uq_inherited_pri;
139 /* Spare queue ready to be reused */
140 struct umtxq_queue *uq_spare_queue;
142 /* The queue we on */
143 struct umtxq_queue *uq_cur_queue;
146 TAILQ_HEAD(umtxq_head, umtx_q);
148 /* Per-key wait-queue */
150 struct umtxq_head head;
152 LIST_ENTRY(umtxq_queue) link;
156 LIST_HEAD(umtxq_list, umtxq_queue);
158 /* Userland lock object's wait-queue chain */
160 /* Lock for this chain. */
163 /* List of sleep queues. */
164 struct umtxq_list uc_queue[2];
165 #define UMTX_SHARED_QUEUE 0
166 #define UMTX_EXCLUSIVE_QUEUE 1
168 LIST_HEAD(, umtxq_queue) uc_spare_queue;
173 /* Chain lock waiters */
176 /* All PI in the list */
177 TAILQ_HEAD(,umtx_pi) uc_pi_list;
179 #ifdef UMTX_PROFILING
185 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
188 * Don't propagate time-sharing priority, there is a security reason,
189 * a user can simply introduce PI-mutex, let thread A lock the mutex,
190 * and let another thread B block on the mutex, because B is
191 * sleeping, its priority will be boosted, this causes A's priority to
192 * be boosted via priority propagating too and will never be lowered even
193 * if it is using 100%CPU, this is unfair to other processes.
196 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
197 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
198 PRI_MAX_TIMESHARE : (td)->td_user_pri)
200 #define GOLDEN_RATIO_PRIME 2654404609U
201 #define UMTX_CHAINS 512
202 #define UMTX_SHIFTS (__WORD_BIT - 9)
204 #define GET_SHARE(flags) \
205 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
207 #define BUSY_SPINS 200
215 #ifdef COMPAT_FREEBSD32
217 volatile __lwpid_t m_owner; /* Owner of the mutex */
218 __uint32_t m_flags; /* Flags of the mutex */
219 __uint32_t m_ceilings[2]; /* Priority protect ceiling */
220 __uint32_t m_rb_lnk; /* Robust linkage */
222 __uint32_t m_spare[2];
225 _Static_assert(sizeof(struct umutex) == sizeof(struct umutex32), "umutex32");
226 _Static_assert(__offsetof(struct umutex, m_spare[0]) ==
227 __offsetof(struct umutex32, m_spare[0]), "m_spare32");
230 int umtx_shm_vnobj_persistent = 0;
231 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_vnode_persistent, CTLFLAG_RWTUN,
232 &umtx_shm_vnobj_persistent, 0,
233 "False forces destruction of umtx attached to file, on last close");
234 static int umtx_max_rb = 1000;
235 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_max_robust, CTLFLAG_RWTUN,
239 static uma_zone_t umtx_pi_zone;
240 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
241 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
242 static int umtx_pi_allocated;
244 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW, 0, "umtx debug");
245 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
246 &umtx_pi_allocated, 0, "Allocated umtx_pi");
247 static int umtx_verbose_rb = 1;
248 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
252 #ifdef UMTX_PROFILING
253 static long max_length;
254 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
255 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD, 0, "umtx chain stats");
258 static void umtx_shm_init(void);
259 static void umtxq_sysinit(void *);
260 static void umtxq_hash(struct umtx_key *key);
261 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
262 static void umtxq_lock(struct umtx_key *key);
263 static void umtxq_unlock(struct umtx_key *key);
264 static void umtxq_busy(struct umtx_key *key);
265 static void umtxq_unbusy(struct umtx_key *key);
266 static void umtxq_insert_queue(struct umtx_q *uq, int q);
267 static void umtxq_remove_queue(struct umtx_q *uq, int q);
268 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *);
269 static int umtxq_count(struct umtx_key *key);
270 static struct umtx_pi *umtx_pi_alloc(int);
271 static void umtx_pi_free(struct umtx_pi *pi);
272 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
274 static void umtx_thread_cleanup(struct thread *td);
275 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
276 struct image_params *imgp __unused);
277 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
279 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
280 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
281 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
283 static struct mtx umtx_lock;
285 #ifdef UMTX_PROFILING
287 umtx_init_profiling(void)
289 struct sysctl_oid *chain_oid;
293 for (i = 0; i < UMTX_CHAINS; ++i) {
294 snprintf(chain_name, sizeof(chain_name), "%d", i);
295 chain_oid = SYSCTL_ADD_NODE(NULL,
296 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
297 chain_name, CTLFLAG_RD, NULL, "umtx hash stats");
298 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
299 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
300 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
301 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
306 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
310 struct umtxq_chain *uc;
311 u_int fract, i, j, tot, whole;
312 u_int sf0, sf1, sf2, sf3, sf4;
313 u_int si0, si1, si2, si3, si4;
314 u_int sw0, sw1, sw2, sw3, sw4;
316 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
317 for (i = 0; i < 2; i++) {
319 for (j = 0; j < UMTX_CHAINS; ++j) {
320 uc = &umtxq_chains[i][j];
321 mtx_lock(&uc->uc_lock);
322 tot += uc->max_length;
323 mtx_unlock(&uc->uc_lock);
326 sbuf_printf(&sb, "%u) Empty ", i);
328 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
329 si0 = si1 = si2 = si3 = si4 = 0;
330 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
331 for (j = 0; j < UMTX_CHAINS; j++) {
332 uc = &umtxq_chains[i][j];
333 mtx_lock(&uc->uc_lock);
334 whole = uc->max_length * 100;
335 mtx_unlock(&uc->uc_lock);
336 fract = (whole % tot) * 100;
337 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
341 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
346 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
351 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
356 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
363 sbuf_printf(&sb, "queue %u:\n", i);
364 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
366 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
368 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
370 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
372 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
378 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
384 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
386 struct umtxq_chain *uc;
391 error = sysctl_handle_int(oidp, &clear, 0, req);
392 if (error != 0 || req->newptr == NULL)
396 for (i = 0; i < 2; ++i) {
397 for (j = 0; j < UMTX_CHAINS; ++j) {
398 uc = &umtxq_chains[i][j];
399 mtx_lock(&uc->uc_lock);
402 mtx_unlock(&uc->uc_lock);
409 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
410 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
411 sysctl_debug_umtx_chains_clear, "I", "Clear umtx chains statistics");
412 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
413 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
414 sysctl_debug_umtx_chains_peaks, "A", "Highest peaks in chains max length");
418 umtxq_sysinit(void *arg __unused)
422 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
423 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
424 for (i = 0; i < 2; ++i) {
425 for (j = 0; j < UMTX_CHAINS; ++j) {
426 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
427 MTX_DEF | MTX_DUPOK);
428 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
429 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
430 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
431 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
432 umtxq_chains[i][j].uc_busy = 0;
433 umtxq_chains[i][j].uc_waiters = 0;
434 #ifdef UMTX_PROFILING
435 umtxq_chains[i][j].length = 0;
436 umtxq_chains[i][j].max_length = 0;
440 #ifdef UMTX_PROFILING
441 umtx_init_profiling();
443 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
444 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
445 EVENTHANDLER_PRI_ANY);
454 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
455 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
457 TAILQ_INIT(&uq->uq_spare_queue->head);
458 TAILQ_INIT(&uq->uq_pi_contested);
459 uq->uq_inherited_pri = PRI_MAX;
464 umtxq_free(struct umtx_q *uq)
467 MPASS(uq->uq_spare_queue != NULL);
468 free(uq->uq_spare_queue, M_UMTX);
473 umtxq_hash(struct umtx_key *key)
477 n = (uintptr_t)key->info.both.a + key->info.both.b;
478 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
481 static inline struct umtxq_chain *
482 umtxq_getchain(struct umtx_key *key)
485 if (key->type <= TYPE_SEM)
486 return (&umtxq_chains[1][key->hash]);
487 return (&umtxq_chains[0][key->hash]);
494 umtxq_lock(struct umtx_key *key)
496 struct umtxq_chain *uc;
498 uc = umtxq_getchain(key);
499 mtx_lock(&uc->uc_lock);
506 umtxq_unlock(struct umtx_key *key)
508 struct umtxq_chain *uc;
510 uc = umtxq_getchain(key);
511 mtx_unlock(&uc->uc_lock);
515 * Set chain to busy state when following operation
516 * may be blocked (kernel mutex can not be used).
519 umtxq_busy(struct umtx_key *key)
521 struct umtxq_chain *uc;
523 uc = umtxq_getchain(key);
524 mtx_assert(&uc->uc_lock, MA_OWNED);
528 int count = BUSY_SPINS;
531 while (uc->uc_busy && --count > 0)
537 while (uc->uc_busy) {
539 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
550 umtxq_unbusy(struct umtx_key *key)
552 struct umtxq_chain *uc;
554 uc = umtxq_getchain(key);
555 mtx_assert(&uc->uc_lock, MA_OWNED);
556 KASSERT(uc->uc_busy != 0, ("not busy"));
563 umtxq_unbusy_unlocked(struct umtx_key *key)
571 static struct umtxq_queue *
572 umtxq_queue_lookup(struct umtx_key *key, int q)
574 struct umtxq_queue *uh;
575 struct umtxq_chain *uc;
577 uc = umtxq_getchain(key);
578 UMTXQ_LOCKED_ASSERT(uc);
579 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
580 if (umtx_key_match(&uh->key, key))
588 umtxq_insert_queue(struct umtx_q *uq, int q)
590 struct umtxq_queue *uh;
591 struct umtxq_chain *uc;
593 uc = umtxq_getchain(&uq->uq_key);
594 UMTXQ_LOCKED_ASSERT(uc);
595 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
596 uh = umtxq_queue_lookup(&uq->uq_key, q);
598 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
600 uh = uq->uq_spare_queue;
601 uh->key = uq->uq_key;
602 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
603 #ifdef UMTX_PROFILING
605 if (uc->length > uc->max_length) {
606 uc->max_length = uc->length;
607 if (uc->max_length > max_length)
608 max_length = uc->max_length;
612 uq->uq_spare_queue = NULL;
614 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
616 uq->uq_flags |= UQF_UMTXQ;
617 uq->uq_cur_queue = uh;
622 umtxq_remove_queue(struct umtx_q *uq, int q)
624 struct umtxq_chain *uc;
625 struct umtxq_queue *uh;
627 uc = umtxq_getchain(&uq->uq_key);
628 UMTXQ_LOCKED_ASSERT(uc);
629 if (uq->uq_flags & UQF_UMTXQ) {
630 uh = uq->uq_cur_queue;
631 TAILQ_REMOVE(&uh->head, uq, uq_link);
633 uq->uq_flags &= ~UQF_UMTXQ;
634 if (TAILQ_EMPTY(&uh->head)) {
635 KASSERT(uh->length == 0,
636 ("inconsistent umtxq_queue length"));
637 #ifdef UMTX_PROFILING
640 LIST_REMOVE(uh, link);
642 uh = LIST_FIRST(&uc->uc_spare_queue);
643 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
644 LIST_REMOVE(uh, link);
646 uq->uq_spare_queue = uh;
647 uq->uq_cur_queue = NULL;
652 * Check if there are multiple waiters
655 umtxq_count(struct umtx_key *key)
657 struct umtxq_chain *uc;
658 struct umtxq_queue *uh;
660 uc = umtxq_getchain(key);
661 UMTXQ_LOCKED_ASSERT(uc);
662 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
669 * Check if there are multiple PI waiters and returns first
673 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
675 struct umtxq_chain *uc;
676 struct umtxq_queue *uh;
679 uc = umtxq_getchain(key);
680 UMTXQ_LOCKED_ASSERT(uc);
681 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
683 *first = TAILQ_FIRST(&uh->head);
690 umtxq_check_susp(struct thread *td)
696 * The check for TDF_NEEDSUSPCHK is racy, but it is enough to
697 * eventually break the lockstep loop.
699 if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
704 if (P_SHOULDSTOP(p) ||
705 ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) {
706 if (p->p_flag & P_SINGLE_EXIT)
716 * Wake up threads waiting on an userland object.
720 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
722 struct umtxq_chain *uc;
723 struct umtxq_queue *uh;
728 uc = umtxq_getchain(key);
729 UMTXQ_LOCKED_ASSERT(uc);
730 uh = umtxq_queue_lookup(key, q);
732 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
733 umtxq_remove_queue(uq, q);
744 * Wake up specified thread.
747 umtxq_signal_thread(struct umtx_q *uq)
749 struct umtxq_chain *uc;
751 uc = umtxq_getchain(&uq->uq_key);
752 UMTXQ_LOCKED_ASSERT(uc);
758 tstohz(const struct timespec *tsp)
762 TIMESPEC_TO_TIMEVAL(&tv, tsp);
767 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
768 const struct timespec *timeout)
771 timo->clockid = clockid;
773 kern_clock_gettime(curthread, clockid, &timo->end);
774 timo->cur = timo->end;
775 timespecadd(&timo->end, timeout);
777 timo->end = *timeout;
778 kern_clock_gettime(curthread, clockid, &timo->cur);
783 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
786 abs_timeout_init(timo, umtxtime->_clockid,
787 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
791 abs_timeout_update(struct abs_timeout *timo)
794 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
798 abs_timeout_gethz(struct abs_timeout *timo)
802 if (timespeccmp(&timo->end, &timo->cur, <=))
805 timespecsub(&tts, &timo->cur);
806 return (tstohz(&tts));
810 umtx_unlock_val(uint32_t flags, bool rb)
814 return (UMUTEX_RB_OWNERDEAD);
815 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
816 return (UMUTEX_RB_NOTRECOV);
818 return (UMUTEX_UNOWNED);
823 * Put thread into sleep state, before sleeping, check if
824 * thread was removed from umtx queue.
827 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
829 struct umtxq_chain *uc;
832 uc = umtxq_getchain(&uq->uq_key);
833 UMTXQ_LOCKED_ASSERT(uc);
835 if (!(uq->uq_flags & UQF_UMTXQ))
837 if (abstime != NULL) {
838 timo = abs_timeout_gethz(abstime);
843 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
844 if (error != EWOULDBLOCK) {
845 umtxq_lock(&uq->uq_key);
849 abs_timeout_update(abstime);
850 umtxq_lock(&uq->uq_key);
856 * Convert userspace address into unique logical address.
859 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
861 struct thread *td = curthread;
863 vm_map_entry_t entry;
869 if (share == THREAD_SHARE) {
871 key->info.private.vs = td->td_proc->p_vmspace;
872 key->info.private.addr = (uintptr_t)addr;
874 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
875 map = &td->td_proc->p_vmspace->vm_map;
876 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
877 &entry, &key->info.shared.object, &pindex, &prot,
878 &wired) != KERN_SUCCESS) {
882 if ((share == PROCESS_SHARE) ||
883 (share == AUTO_SHARE &&
884 VM_INHERIT_SHARE == entry->inheritance)) {
886 key->info.shared.offset = (vm_offset_t)addr -
887 entry->start + entry->offset;
888 vm_object_reference(key->info.shared.object);
891 key->info.private.vs = td->td_proc->p_vmspace;
892 key->info.private.addr = (uintptr_t)addr;
894 vm_map_lookup_done(map, entry);
905 umtx_key_release(struct umtx_key *key)
908 vm_object_deallocate(key->info.shared.object);
912 * Fetch and compare value, sleep on the address if value is not changed.
915 do_wait(struct thread *td, void *addr, u_long id,
916 struct _umtx_time *timeout, int compat32, int is_private)
918 struct abs_timeout timo;
925 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
926 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
930 abs_timeout_init2(&timo, timeout);
932 umtxq_lock(&uq->uq_key);
934 umtxq_unlock(&uq->uq_key);
936 error = fueword(addr, &tmp);
940 error = fueword32(addr, &tmp32);
946 umtxq_lock(&uq->uq_key);
949 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
951 if ((uq->uq_flags & UQF_UMTXQ) == 0)
955 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
958 umtxq_unlock(&uq->uq_key);
959 umtx_key_release(&uq->uq_key);
960 if (error == ERESTART)
966 * Wake up threads sleeping on the specified address.
969 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
974 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
975 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
978 umtxq_signal(&key, n_wake);
980 umtx_key_release(&key);
985 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
988 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
989 struct _umtx_time *timeout, int mode)
991 struct abs_timeout timo;
993 uint32_t owner, old, id;
1000 abs_timeout_init2(&timo, timeout);
1003 * Care must be exercised when dealing with umtx structure. It
1004 * can fault on any access.
1007 rv = fueword32(&m->m_owner, &owner);
1010 if (mode == _UMUTEX_WAIT) {
1011 if (owner == UMUTEX_UNOWNED ||
1012 owner == UMUTEX_CONTESTED ||
1013 owner == UMUTEX_RB_OWNERDEAD ||
1014 owner == UMUTEX_RB_NOTRECOV)
1018 * Robust mutex terminated. Kernel duty is to
1019 * return EOWNERDEAD to the userspace. The
1020 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1021 * by the common userspace code.
1023 if (owner == UMUTEX_RB_OWNERDEAD) {
1024 rv = casueword32(&m->m_owner,
1025 UMUTEX_RB_OWNERDEAD, &owner,
1026 id | UMUTEX_CONTESTED);
1029 if (owner == UMUTEX_RB_OWNERDEAD)
1030 return (EOWNERDEAD); /* success */
1031 rv = umtxq_check_susp(td);
1036 if (owner == UMUTEX_RB_NOTRECOV)
1037 return (ENOTRECOVERABLE);
1041 * Try the uncontested case. This should be
1044 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1046 /* The address was invalid. */
1050 /* The acquire succeeded. */
1051 if (owner == UMUTEX_UNOWNED)
1055 * If no one owns it but it is contested try
1058 if (owner == UMUTEX_CONTESTED) {
1059 rv = casueword32(&m->m_owner,
1060 UMUTEX_CONTESTED, &owner,
1061 id | UMUTEX_CONTESTED);
1062 /* The address was invalid. */
1066 if (owner == UMUTEX_CONTESTED)
1069 rv = umtxq_check_susp(td);
1074 * If this failed the lock has
1081 if (mode == _UMUTEX_TRY)
1085 * If we caught a signal, we have retried and now
1091 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1092 GET_SHARE(flags), &uq->uq_key)) != 0)
1095 umtxq_lock(&uq->uq_key);
1096 umtxq_busy(&uq->uq_key);
1098 umtxq_unlock(&uq->uq_key);
1101 * Set the contested bit so that a release in user space
1102 * knows to use the system call for unlock. If this fails
1103 * either some one else has acquired the lock or it has been
1106 rv = casueword32(&m->m_owner, owner, &old,
1107 owner | UMUTEX_CONTESTED);
1109 /* The address was invalid. */
1111 umtxq_lock(&uq->uq_key);
1113 umtxq_unbusy(&uq->uq_key);
1114 umtxq_unlock(&uq->uq_key);
1115 umtx_key_release(&uq->uq_key);
1120 * We set the contested bit, sleep. Otherwise the lock changed
1121 * and we need to retry or we lost a race to the thread
1122 * unlocking the umtx.
1124 umtxq_lock(&uq->uq_key);
1125 umtxq_unbusy(&uq->uq_key);
1127 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1130 umtxq_unlock(&uq->uq_key);
1131 umtx_key_release(&uq->uq_key);
1134 error = umtxq_check_susp(td);
1141 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1144 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1146 struct umtx_key key;
1147 uint32_t owner, old, id, newlock;
1152 * Make sure we own this mtx.
1154 error = fueword32(&m->m_owner, &owner);
1158 if ((owner & ~UMUTEX_CONTESTED) != id)
1161 newlock = umtx_unlock_val(flags, rb);
1162 if ((owner & UMUTEX_CONTESTED) == 0) {
1163 error = casueword32(&m->m_owner, owner, &old, newlock);
1171 /* We should only ever be in here for contested locks */
1172 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1178 count = umtxq_count(&key);
1182 * When unlocking the umtx, it must be marked as unowned if
1183 * there is zero or one thread only waiting for it.
1184 * Otherwise, it must be marked as contested.
1187 newlock |= UMUTEX_CONTESTED;
1188 error = casueword32(&m->m_owner, owner, &old, newlock);
1190 umtxq_signal(&key, 1);
1193 umtx_key_release(&key);
1202 * Check if the mutex is available and wake up a waiter,
1203 * only for simple mutex.
1206 do_wake_umutex(struct thread *td, struct umutex *m)
1208 struct umtx_key key;
1214 error = fueword32(&m->m_owner, &owner);
1218 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1219 owner != UMUTEX_RB_NOTRECOV)
1222 error = fueword32(&m->m_flags, &flags);
1226 /* We should only ever be in here for contested locks */
1227 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1233 count = umtxq_count(&key);
1236 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1237 owner != UMUTEX_RB_NOTRECOV) {
1238 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1245 if (error == 0 && count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1246 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1247 umtxq_signal(&key, 1);
1250 umtx_key_release(&key);
1255 * Check if the mutex has waiters and tries to fix contention bit.
1258 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1260 struct umtx_key key;
1261 uint32_t owner, old;
1266 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1270 type = TYPE_NORMAL_UMUTEX;
1272 case UMUTEX_PRIO_INHERIT:
1273 type = TYPE_PI_UMUTEX;
1275 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1276 type = TYPE_PI_ROBUST_UMUTEX;
1278 case UMUTEX_PRIO_PROTECT:
1279 type = TYPE_PP_UMUTEX;
1281 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1282 type = TYPE_PP_ROBUST_UMUTEX;
1287 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1293 count = umtxq_count(&key);
1296 * Only repair contention bit if there is a waiter, this means the mutex
1297 * is still being referenced by userland code, otherwise don't update
1301 error = fueword32(&m->m_owner, &owner);
1304 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0) {
1305 error = casueword32(&m->m_owner, owner, &old,
1306 owner | UMUTEX_CONTESTED);
1314 error = umtxq_check_susp(td);
1318 } else if (count == 1) {
1319 error = fueword32(&m->m_owner, &owner);
1322 while (error == 0 && (owner & ~UMUTEX_CONTESTED) != 0 &&
1323 (owner & UMUTEX_CONTESTED) == 0) {
1324 error = casueword32(&m->m_owner, owner, &old,
1325 owner | UMUTEX_CONTESTED);
1333 error = umtxq_check_susp(td);
1339 if (error == EFAULT) {
1340 umtxq_signal(&key, INT_MAX);
1341 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1342 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1343 umtxq_signal(&key, 1);
1346 umtx_key_release(&key);
1350 static inline struct umtx_pi *
1351 umtx_pi_alloc(int flags)
1355 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1356 TAILQ_INIT(&pi->pi_blocked);
1357 atomic_add_int(&umtx_pi_allocated, 1);
1362 umtx_pi_free(struct umtx_pi *pi)
1364 uma_zfree(umtx_pi_zone, pi);
1365 atomic_add_int(&umtx_pi_allocated, -1);
1369 * Adjust the thread's position on a pi_state after its priority has been
1373 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1375 struct umtx_q *uq, *uq1, *uq2;
1378 mtx_assert(&umtx_lock, MA_OWNED);
1385 * Check if the thread needs to be moved on the blocked chain.
1386 * It needs to be moved if either its priority is lower than
1387 * the previous thread or higher than the next thread.
1389 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1390 uq2 = TAILQ_NEXT(uq, uq_lockq);
1391 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1392 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1394 * Remove thread from blocked chain and determine where
1395 * it should be moved to.
1397 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1398 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1399 td1 = uq1->uq_thread;
1400 MPASS(td1->td_proc->p_magic == P_MAGIC);
1401 if (UPRI(td1) > UPRI(td))
1406 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1408 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1413 static struct umtx_pi *
1414 umtx_pi_next(struct umtx_pi *pi)
1416 struct umtx_q *uq_owner;
1418 if (pi->pi_owner == NULL)
1420 uq_owner = pi->pi_owner->td_umtxq;
1421 if (uq_owner == NULL)
1423 return (uq_owner->uq_pi_blocked);
1427 * Floyd's Cycle-Finding Algorithm.
1430 umtx_pi_check_loop(struct umtx_pi *pi)
1432 struct umtx_pi *pi1; /* fast iterator */
1434 mtx_assert(&umtx_lock, MA_OWNED);
1439 pi = umtx_pi_next(pi);
1442 pi1 = umtx_pi_next(pi1);
1445 pi1 = umtx_pi_next(pi1);
1455 * Propagate priority when a thread is blocked on POSIX
1459 umtx_propagate_priority(struct thread *td)
1465 mtx_assert(&umtx_lock, MA_OWNED);
1468 pi = uq->uq_pi_blocked;
1471 if (umtx_pi_check_loop(pi))
1476 if (td == NULL || td == curthread)
1479 MPASS(td->td_proc != NULL);
1480 MPASS(td->td_proc->p_magic == P_MAGIC);
1483 if (td->td_lend_user_pri > pri)
1484 sched_lend_user_prio(td, pri);
1492 * Pick up the lock that td is blocked on.
1495 pi = uq->uq_pi_blocked;
1498 /* Resort td on the list if needed. */
1499 umtx_pi_adjust_thread(pi, td);
1504 * Unpropagate priority for a PI mutex when a thread blocked on
1505 * it is interrupted by signal or resumed by others.
1508 umtx_repropagate_priority(struct umtx_pi *pi)
1510 struct umtx_q *uq, *uq_owner;
1511 struct umtx_pi *pi2;
1514 mtx_assert(&umtx_lock, MA_OWNED);
1516 if (umtx_pi_check_loop(pi))
1518 while (pi != NULL && pi->pi_owner != NULL) {
1520 uq_owner = pi->pi_owner->td_umtxq;
1522 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1523 uq = TAILQ_FIRST(&pi2->pi_blocked);
1525 if (pri > UPRI(uq->uq_thread))
1526 pri = UPRI(uq->uq_thread);
1530 if (pri > uq_owner->uq_inherited_pri)
1531 pri = uq_owner->uq_inherited_pri;
1532 thread_lock(pi->pi_owner);
1533 sched_lend_user_prio(pi->pi_owner, pri);
1534 thread_unlock(pi->pi_owner);
1535 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1536 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1541 * Insert a PI mutex into owned list.
1544 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1546 struct umtx_q *uq_owner;
1548 uq_owner = owner->td_umtxq;
1549 mtx_assert(&umtx_lock, MA_OWNED);
1550 if (pi->pi_owner != NULL)
1551 panic("pi_owner != NULL");
1552 pi->pi_owner = owner;
1553 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1558 * Disown a PI mutex, and remove it from the owned list.
1561 umtx_pi_disown(struct umtx_pi *pi)
1564 mtx_assert(&umtx_lock, MA_OWNED);
1565 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1566 pi->pi_owner = NULL;
1570 * Claim ownership of a PI mutex.
1573 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1578 mtx_lock(&umtx_lock);
1579 if (pi->pi_owner == owner) {
1580 mtx_unlock(&umtx_lock);
1584 if (pi->pi_owner != NULL) {
1586 * userland may have already messed the mutex, sigh.
1588 mtx_unlock(&umtx_lock);
1591 umtx_pi_setowner(pi, owner);
1592 uq = TAILQ_FIRST(&pi->pi_blocked);
1594 pri = UPRI(uq->uq_thread);
1596 if (pri < UPRI(owner))
1597 sched_lend_user_prio(owner, pri);
1598 thread_unlock(owner);
1600 mtx_unlock(&umtx_lock);
1605 * Adjust a thread's order position in its blocked PI mutex,
1606 * this may result new priority propagating process.
1609 umtx_pi_adjust(struct thread *td, u_char oldpri)
1615 mtx_lock(&umtx_lock);
1617 * Pick up the lock that td is blocked on.
1619 pi = uq->uq_pi_blocked;
1621 umtx_pi_adjust_thread(pi, td);
1622 umtx_repropagate_priority(pi);
1624 mtx_unlock(&umtx_lock);
1628 * Sleep on a PI mutex.
1631 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
1632 const char *wmesg, struct abs_timeout *timo, bool shared)
1634 struct umtxq_chain *uc;
1635 struct thread *td, *td1;
1641 KASSERT(td == curthread, ("inconsistent uq_thread"));
1642 uc = umtxq_getchain(&uq->uq_key);
1643 UMTXQ_LOCKED_ASSERT(uc);
1644 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1646 mtx_lock(&umtx_lock);
1647 if (pi->pi_owner == NULL) {
1648 mtx_unlock(&umtx_lock);
1649 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
1650 mtx_lock(&umtx_lock);
1652 if (pi->pi_owner == NULL)
1653 umtx_pi_setowner(pi, td1);
1654 PROC_UNLOCK(td1->td_proc);
1658 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1659 pri = UPRI(uq1->uq_thread);
1665 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1667 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1669 uq->uq_pi_blocked = pi;
1671 td->td_flags |= TDF_UPIBLOCKED;
1673 umtx_propagate_priority(td);
1674 mtx_unlock(&umtx_lock);
1675 umtxq_unbusy(&uq->uq_key);
1677 error = umtxq_sleep(uq, wmesg, timo);
1680 mtx_lock(&umtx_lock);
1681 uq->uq_pi_blocked = NULL;
1683 td->td_flags &= ~TDF_UPIBLOCKED;
1685 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1686 umtx_repropagate_priority(pi);
1687 mtx_unlock(&umtx_lock);
1688 umtxq_unlock(&uq->uq_key);
1694 * Add reference count for a PI mutex.
1697 umtx_pi_ref(struct umtx_pi *pi)
1699 struct umtxq_chain *uc;
1701 uc = umtxq_getchain(&pi->pi_key);
1702 UMTXQ_LOCKED_ASSERT(uc);
1707 * Decrease reference count for a PI mutex, if the counter
1708 * is decreased to zero, its memory space is freed.
1711 umtx_pi_unref(struct umtx_pi *pi)
1713 struct umtxq_chain *uc;
1715 uc = umtxq_getchain(&pi->pi_key);
1716 UMTXQ_LOCKED_ASSERT(uc);
1717 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1718 if (--pi->pi_refcount == 0) {
1719 mtx_lock(&umtx_lock);
1720 if (pi->pi_owner != NULL)
1722 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1723 ("blocked queue not empty"));
1724 mtx_unlock(&umtx_lock);
1725 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1731 * Find a PI mutex in hash table.
1733 static struct umtx_pi *
1734 umtx_pi_lookup(struct umtx_key *key)
1736 struct umtxq_chain *uc;
1739 uc = umtxq_getchain(key);
1740 UMTXQ_LOCKED_ASSERT(uc);
1742 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1743 if (umtx_key_match(&pi->pi_key, key)) {
1751 * Insert a PI mutex into hash table.
1754 umtx_pi_insert(struct umtx_pi *pi)
1756 struct umtxq_chain *uc;
1758 uc = umtxq_getchain(&pi->pi_key);
1759 UMTXQ_LOCKED_ASSERT(uc);
1760 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1767 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1768 struct _umtx_time *timeout, int try)
1770 struct abs_timeout timo;
1772 struct umtx_pi *pi, *new_pi;
1773 uint32_t id, old_owner, owner, old;
1779 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
1780 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
1784 if (timeout != NULL)
1785 abs_timeout_init2(&timo, timeout);
1787 umtxq_lock(&uq->uq_key);
1788 pi = umtx_pi_lookup(&uq->uq_key);
1790 new_pi = umtx_pi_alloc(M_NOWAIT);
1791 if (new_pi == NULL) {
1792 umtxq_unlock(&uq->uq_key);
1793 new_pi = umtx_pi_alloc(M_WAITOK);
1794 umtxq_lock(&uq->uq_key);
1795 pi = umtx_pi_lookup(&uq->uq_key);
1797 umtx_pi_free(new_pi);
1801 if (new_pi != NULL) {
1802 new_pi->pi_key = uq->uq_key;
1803 umtx_pi_insert(new_pi);
1808 umtxq_unlock(&uq->uq_key);
1811 * Care must be exercised when dealing with umtx structure. It
1812 * can fault on any access.
1816 * Try the uncontested case. This should be done in userland.
1818 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
1819 /* The address was invalid. */
1825 /* The acquire succeeded. */
1826 if (owner == UMUTEX_UNOWNED) {
1831 if (owner == UMUTEX_RB_NOTRECOV) {
1832 error = ENOTRECOVERABLE;
1836 /* If no one owns it but it is contested try to acquire it. */
1837 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
1839 rv = casueword32(&m->m_owner, owner, &owner,
1840 id | UMUTEX_CONTESTED);
1841 /* The address was invalid. */
1847 if (owner == old_owner) {
1848 umtxq_lock(&uq->uq_key);
1849 umtxq_busy(&uq->uq_key);
1850 error = umtx_pi_claim(pi, td);
1851 umtxq_unbusy(&uq->uq_key);
1852 umtxq_unlock(&uq->uq_key);
1855 * Since we're going to return an
1856 * error, restore the m_owner to its
1857 * previous, unowned state to avoid
1858 * compounding the problem.
1860 (void)casuword32(&m->m_owner,
1861 id | UMUTEX_CONTESTED,
1865 old_owner == UMUTEX_RB_OWNERDEAD)
1870 error = umtxq_check_susp(td);
1874 /* If this failed the lock has changed, restart. */
1878 if ((owner & ~UMUTEX_CONTESTED) == id) {
1889 * If we caught a signal, we have retried and now
1895 umtxq_lock(&uq->uq_key);
1896 umtxq_busy(&uq->uq_key);
1897 umtxq_unlock(&uq->uq_key);
1900 * Set the contested bit so that a release in user space
1901 * knows to use the system call for unlock. If this fails
1902 * either some one else has acquired the lock or it has been
1905 rv = casueword32(&m->m_owner, owner, &old, owner |
1908 /* The address was invalid. */
1910 umtxq_unbusy_unlocked(&uq->uq_key);
1915 umtxq_lock(&uq->uq_key);
1917 * We set the contested bit, sleep. Otherwise the lock changed
1918 * and we need to retry or we lost a race to the thread
1919 * unlocking the umtx. Note that the UMUTEX_RB_OWNERDEAD
1920 * value for owner is impossible there.
1923 error = umtxq_sleep_pi(uq, pi,
1924 owner & ~UMUTEX_CONTESTED,
1925 "umtxpi", timeout == NULL ? NULL : &timo,
1926 (flags & USYNC_PROCESS_SHARED) != 0);
1930 umtxq_unbusy(&uq->uq_key);
1931 umtxq_unlock(&uq->uq_key);
1934 error = umtxq_check_susp(td);
1939 umtxq_lock(&uq->uq_key);
1941 umtxq_unlock(&uq->uq_key);
1943 umtx_key_release(&uq->uq_key);
1948 * Unlock a PI mutex.
1951 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1953 struct umtx_key key;
1954 struct umtx_q *uq_first, *uq_first2, *uq_me;
1955 struct umtx_pi *pi, *pi2;
1956 uint32_t id, new_owner, old, owner;
1957 int count, error, pri;
1961 * Make sure we own this mtx.
1963 error = fueword32(&m->m_owner, &owner);
1967 if ((owner & ~UMUTEX_CONTESTED) != id)
1970 new_owner = umtx_unlock_val(flags, rb);
1972 /* This should be done in userland */
1973 if ((owner & UMUTEX_CONTESTED) == 0) {
1974 error = casueword32(&m->m_owner, owner, &old, new_owner);
1982 /* We should only ever be in here for contested locks */
1983 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
1984 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
1990 count = umtxq_count_pi(&key, &uq_first);
1991 if (uq_first != NULL) {
1992 mtx_lock(&umtx_lock);
1993 pi = uq_first->uq_pi_blocked;
1994 KASSERT(pi != NULL, ("pi == NULL?"));
1995 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
1996 mtx_unlock(&umtx_lock);
1999 umtx_key_release(&key);
2000 /* userland messed the mutex */
2003 uq_me = td->td_umtxq;
2004 if (pi->pi_owner == td)
2006 /* get highest priority thread which is still sleeping. */
2007 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2008 while (uq_first != NULL &&
2009 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2010 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2013 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2014 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2015 if (uq_first2 != NULL) {
2016 if (pri > UPRI(uq_first2->uq_thread))
2017 pri = UPRI(uq_first2->uq_thread);
2021 sched_lend_user_prio(td, pri);
2023 mtx_unlock(&umtx_lock);
2025 umtxq_signal_thread(uq_first);
2027 pi = umtx_pi_lookup(&key);
2029 * A umtx_pi can exist if a signal or timeout removed the
2030 * last waiter from the umtxq, but there is still
2031 * a thread in do_lock_pi() holding the umtx_pi.
2035 * The umtx_pi can be unowned, such as when a thread
2036 * has just entered do_lock_pi(), allocated the
2037 * umtx_pi, and unlocked the umtxq.
2038 * If the current thread owns it, it must disown it.
2040 mtx_lock(&umtx_lock);
2041 if (pi->pi_owner == td)
2043 mtx_unlock(&umtx_lock);
2049 * When unlocking the umtx, it must be marked as unowned if
2050 * there is zero or one thread only waiting for it.
2051 * Otherwise, it must be marked as contested.
2055 new_owner |= UMUTEX_CONTESTED;
2056 error = casueword32(&m->m_owner, owner, &old, new_owner);
2058 umtxq_unbusy_unlocked(&key);
2059 umtx_key_release(&key);
2071 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2072 struct _umtx_time *timeout, int try)
2074 struct abs_timeout timo;
2075 struct umtx_q *uq, *uq2;
2079 int error, pri, old_inherited_pri, su, rv;
2083 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2084 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2088 if (timeout != NULL)
2089 abs_timeout_init2(&timo, timeout);
2091 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2093 old_inherited_pri = uq->uq_inherited_pri;
2094 umtxq_lock(&uq->uq_key);
2095 umtxq_busy(&uq->uq_key);
2096 umtxq_unlock(&uq->uq_key);
2098 rv = fueword32(&m->m_ceilings[0], &ceiling);
2103 ceiling = RTP_PRIO_MAX - ceiling;
2104 if (ceiling > RTP_PRIO_MAX) {
2109 mtx_lock(&umtx_lock);
2110 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2111 mtx_unlock(&umtx_lock);
2115 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2116 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2118 if (uq->uq_inherited_pri < UPRI(td))
2119 sched_lend_user_prio(td, uq->uq_inherited_pri);
2122 mtx_unlock(&umtx_lock);
2124 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2125 id | UMUTEX_CONTESTED);
2126 /* The address was invalid. */
2132 if (owner == UMUTEX_CONTESTED) {
2135 } else if (owner == UMUTEX_RB_OWNERDEAD) {
2136 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2137 &owner, id | UMUTEX_CONTESTED);
2142 if (owner == UMUTEX_RB_OWNERDEAD) {
2143 error = EOWNERDEAD; /* success */
2147 } else if (owner == UMUTEX_RB_NOTRECOV) {
2148 error = ENOTRECOVERABLE;
2158 * If we caught a signal, we have retried and now
2164 umtxq_lock(&uq->uq_key);
2166 umtxq_unbusy(&uq->uq_key);
2167 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2170 umtxq_unlock(&uq->uq_key);
2172 mtx_lock(&umtx_lock);
2173 uq->uq_inherited_pri = old_inherited_pri;
2175 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2176 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2178 if (pri > UPRI(uq2->uq_thread))
2179 pri = UPRI(uq2->uq_thread);
2182 if (pri > uq->uq_inherited_pri)
2183 pri = uq->uq_inherited_pri;
2185 sched_lend_user_prio(td, pri);
2187 mtx_unlock(&umtx_lock);
2190 if (error != 0 && error != EOWNERDEAD) {
2191 mtx_lock(&umtx_lock);
2192 uq->uq_inherited_pri = old_inherited_pri;
2194 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2195 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2197 if (pri > UPRI(uq2->uq_thread))
2198 pri = UPRI(uq2->uq_thread);
2201 if (pri > uq->uq_inherited_pri)
2202 pri = uq->uq_inherited_pri;
2204 sched_lend_user_prio(td, pri);
2206 mtx_unlock(&umtx_lock);
2210 umtxq_unbusy_unlocked(&uq->uq_key);
2211 umtx_key_release(&uq->uq_key);
2216 * Unlock a PP mutex.
2219 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2221 struct umtx_key key;
2222 struct umtx_q *uq, *uq2;
2224 uint32_t id, owner, rceiling;
2225 int error, pri, new_inherited_pri, su;
2229 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2232 * Make sure we own this mtx.
2234 error = fueword32(&m->m_owner, &owner);
2238 if ((owner & ~UMUTEX_CONTESTED) != id)
2241 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2246 new_inherited_pri = PRI_MAX;
2248 rceiling = RTP_PRIO_MAX - rceiling;
2249 if (rceiling > RTP_PRIO_MAX)
2251 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2254 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2255 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2262 * For priority protected mutex, always set unlocked state
2263 * to UMUTEX_CONTESTED, so that userland always enters kernel
2264 * to lock the mutex, it is necessary because thread priority
2265 * has to be adjusted for such mutex.
2267 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2272 umtxq_signal(&key, 1);
2279 mtx_lock(&umtx_lock);
2281 uq->uq_inherited_pri = new_inherited_pri;
2283 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2284 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2286 if (pri > UPRI(uq2->uq_thread))
2287 pri = UPRI(uq2->uq_thread);
2290 if (pri > uq->uq_inherited_pri)
2291 pri = uq->uq_inherited_pri;
2293 sched_lend_user_prio(td, pri);
2295 mtx_unlock(&umtx_lock);
2297 umtx_key_release(&key);
2302 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2303 uint32_t *old_ceiling)
2306 uint32_t flags, id, owner, save_ceiling;
2309 error = fueword32(&m->m_flags, &flags);
2312 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2314 if (ceiling > RTP_PRIO_MAX)
2318 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2319 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2323 umtxq_lock(&uq->uq_key);
2324 umtxq_busy(&uq->uq_key);
2325 umtxq_unlock(&uq->uq_key);
2327 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2333 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2334 id | UMUTEX_CONTESTED);
2340 if (owner == UMUTEX_CONTESTED) {
2341 rv = suword32(&m->m_ceilings[0], ceiling);
2342 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2343 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2347 if ((owner & ~UMUTEX_CONTESTED) == id) {
2348 rv = suword32(&m->m_ceilings[0], ceiling);
2349 error = rv == 0 ? 0 : EFAULT;
2353 if (owner == UMUTEX_RB_OWNERDEAD) {
2356 } else if (owner == UMUTEX_RB_NOTRECOV) {
2357 error = ENOTRECOVERABLE;
2362 * If we caught a signal, we have retried and now
2369 * We set the contested bit, sleep. Otherwise the lock changed
2370 * and we need to retry or we lost a race to the thread
2371 * unlocking the umtx.
2373 umtxq_lock(&uq->uq_key);
2375 umtxq_unbusy(&uq->uq_key);
2376 error = umtxq_sleep(uq, "umtxpp", NULL);
2378 umtxq_unlock(&uq->uq_key);
2380 umtxq_lock(&uq->uq_key);
2382 umtxq_signal(&uq->uq_key, INT_MAX);
2383 umtxq_unbusy(&uq->uq_key);
2384 umtxq_unlock(&uq->uq_key);
2385 umtx_key_release(&uq->uq_key);
2386 if (error == 0 && old_ceiling != NULL) {
2387 rv = suword32(old_ceiling, save_ceiling);
2388 error = rv == 0 ? 0 : EFAULT;
2394 * Lock a userland POSIX mutex.
2397 do_lock_umutex(struct thread *td, struct umutex *m,
2398 struct _umtx_time *timeout, int mode)
2403 error = fueword32(&m->m_flags, &flags);
2407 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2409 error = do_lock_normal(td, m, flags, timeout, mode);
2411 case UMUTEX_PRIO_INHERIT:
2412 error = do_lock_pi(td, m, flags, timeout, mode);
2414 case UMUTEX_PRIO_PROTECT:
2415 error = do_lock_pp(td, m, flags, timeout, mode);
2420 if (timeout == NULL) {
2421 if (error == EINTR && mode != _UMUTEX_WAIT)
2424 /* Timed-locking is not restarted. */
2425 if (error == ERESTART)
2432 * Unlock a userland POSIX mutex.
2435 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2440 error = fueword32(&m->m_flags, &flags);
2444 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2446 return (do_unlock_normal(td, m, flags, rb));
2447 case UMUTEX_PRIO_INHERIT:
2448 return (do_unlock_pi(td, m, flags, rb));
2449 case UMUTEX_PRIO_PROTECT:
2450 return (do_unlock_pp(td, m, flags, rb));
2457 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2458 struct timespec *timeout, u_long wflags)
2460 struct abs_timeout timo;
2462 uint32_t flags, clockid, hasw;
2466 error = fueword32(&cv->c_flags, &flags);
2469 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2473 if ((wflags & CVWAIT_CLOCKID) != 0) {
2474 error = fueword32(&cv->c_clockid, &clockid);
2476 umtx_key_release(&uq->uq_key);
2479 if (clockid < CLOCK_REALTIME ||
2480 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2481 /* hmm, only HW clock id will work. */
2482 umtx_key_release(&uq->uq_key);
2486 clockid = CLOCK_REALTIME;
2489 umtxq_lock(&uq->uq_key);
2490 umtxq_busy(&uq->uq_key);
2492 umtxq_unlock(&uq->uq_key);
2495 * Set c_has_waiters to 1 before releasing user mutex, also
2496 * don't modify cache line when unnecessary.
2498 error = fueword32(&cv->c_has_waiters, &hasw);
2499 if (error == 0 && hasw == 0)
2500 suword32(&cv->c_has_waiters, 1);
2502 umtxq_unbusy_unlocked(&uq->uq_key);
2504 error = do_unlock_umutex(td, m, false);
2506 if (timeout != NULL)
2507 abs_timeout_init(&timo, clockid, (wflags & CVWAIT_ABSTIME) != 0,
2510 umtxq_lock(&uq->uq_key);
2512 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2516 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2520 * This must be timeout,interrupted by signal or
2521 * surprious wakeup, clear c_has_waiter flag when
2524 umtxq_busy(&uq->uq_key);
2525 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2526 int oldlen = uq->uq_cur_queue->length;
2529 umtxq_unlock(&uq->uq_key);
2530 suword32(&cv->c_has_waiters, 0);
2531 umtxq_lock(&uq->uq_key);
2534 umtxq_unbusy(&uq->uq_key);
2535 if (error == ERESTART)
2539 umtxq_unlock(&uq->uq_key);
2540 umtx_key_release(&uq->uq_key);
2545 * Signal a userland condition variable.
2548 do_cv_signal(struct thread *td, struct ucond *cv)
2550 struct umtx_key key;
2551 int error, cnt, nwake;
2554 error = fueword32(&cv->c_flags, &flags);
2557 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2561 cnt = umtxq_count(&key);
2562 nwake = umtxq_signal(&key, 1);
2565 error = suword32(&cv->c_has_waiters, 0);
2572 umtx_key_release(&key);
2577 do_cv_broadcast(struct thread *td, struct ucond *cv)
2579 struct umtx_key key;
2583 error = fueword32(&cv->c_flags, &flags);
2586 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2591 umtxq_signal(&key, INT_MAX);
2594 error = suword32(&cv->c_has_waiters, 0);
2598 umtxq_unbusy_unlocked(&key);
2600 umtx_key_release(&key);
2605 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, struct _umtx_time *timeout)
2607 struct abs_timeout timo;
2609 uint32_t flags, wrflags;
2610 int32_t state, oldstate;
2611 int32_t blocked_readers;
2615 error = fueword32(&rwlock->rw_flags, &flags);
2618 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2622 if (timeout != NULL)
2623 abs_timeout_init2(&timo, timeout);
2625 wrflags = URWLOCK_WRITE_OWNER;
2626 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2627 wrflags |= URWLOCK_WRITE_WAITERS;
2630 rv = fueword32(&rwlock->rw_state, &state);
2632 umtx_key_release(&uq->uq_key);
2636 /* try to lock it */
2637 while (!(state & wrflags)) {
2638 if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) {
2639 umtx_key_release(&uq->uq_key);
2642 rv = casueword32(&rwlock->rw_state, state,
2643 &oldstate, state + 1);
2645 umtx_key_release(&uq->uq_key);
2648 if (oldstate == state) {
2649 umtx_key_release(&uq->uq_key);
2652 error = umtxq_check_susp(td);
2661 /* grab monitor lock */
2662 umtxq_lock(&uq->uq_key);
2663 umtxq_busy(&uq->uq_key);
2664 umtxq_unlock(&uq->uq_key);
2667 * re-read the state, in case it changed between the try-lock above
2668 * and the check below
2670 rv = fueword32(&rwlock->rw_state, &state);
2674 /* set read contention bit */
2675 while (error == 0 && (state & wrflags) &&
2676 !(state & URWLOCK_READ_WAITERS)) {
2677 rv = casueword32(&rwlock->rw_state, state,
2678 &oldstate, state | URWLOCK_READ_WAITERS);
2683 if (oldstate == state)
2686 error = umtxq_check_susp(td);
2691 umtxq_unbusy_unlocked(&uq->uq_key);
2695 /* state is changed while setting flags, restart */
2696 if (!(state & wrflags)) {
2697 umtxq_unbusy_unlocked(&uq->uq_key);
2698 error = umtxq_check_susp(td);
2705 /* contention bit is set, before sleeping, increase read waiter count */
2706 rv = fueword32(&rwlock->rw_blocked_readers,
2709 umtxq_unbusy_unlocked(&uq->uq_key);
2713 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2715 while (state & wrflags) {
2716 umtxq_lock(&uq->uq_key);
2718 umtxq_unbusy(&uq->uq_key);
2720 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2723 umtxq_busy(&uq->uq_key);
2725 umtxq_unlock(&uq->uq_key);
2728 rv = fueword32(&rwlock->rw_state, &state);
2735 /* decrease read waiter count, and may clear read contention bit */
2736 rv = fueword32(&rwlock->rw_blocked_readers,
2739 umtxq_unbusy_unlocked(&uq->uq_key);
2743 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2744 if (blocked_readers == 1) {
2745 rv = fueword32(&rwlock->rw_state, &state);
2747 umtxq_unbusy_unlocked(&uq->uq_key);
2752 rv = casueword32(&rwlock->rw_state, state,
2753 &oldstate, state & ~URWLOCK_READ_WAITERS);
2758 if (oldstate == state)
2761 error = umtxq_check_susp(td);
2767 umtxq_unbusy_unlocked(&uq->uq_key);
2771 umtx_key_release(&uq->uq_key);
2772 if (error == ERESTART)
2778 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2780 struct abs_timeout timo;
2783 int32_t state, oldstate;
2784 int32_t blocked_writers;
2785 int32_t blocked_readers;
2789 error = fueword32(&rwlock->rw_flags, &flags);
2792 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2796 if (timeout != NULL)
2797 abs_timeout_init2(&timo, timeout);
2799 blocked_readers = 0;
2801 rv = fueword32(&rwlock->rw_state, &state);
2803 umtx_key_release(&uq->uq_key);
2806 while (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2807 rv = casueword32(&rwlock->rw_state, state,
2808 &oldstate, state | URWLOCK_WRITE_OWNER);
2810 umtx_key_release(&uq->uq_key);
2813 if (oldstate == state) {
2814 umtx_key_release(&uq->uq_key);
2818 error = umtxq_check_susp(td);
2824 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) &&
2825 blocked_readers != 0) {
2826 umtxq_lock(&uq->uq_key);
2827 umtxq_busy(&uq->uq_key);
2828 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE);
2829 umtxq_unbusy(&uq->uq_key);
2830 umtxq_unlock(&uq->uq_key);
2836 /* grab monitor lock */
2837 umtxq_lock(&uq->uq_key);
2838 umtxq_busy(&uq->uq_key);
2839 umtxq_unlock(&uq->uq_key);
2842 * re-read the state, in case it changed between the try-lock above
2843 * and the check below
2845 rv = fueword32(&rwlock->rw_state, &state);
2849 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
2850 URWLOCK_READER_COUNT(state) != 0) &&
2851 (state & URWLOCK_WRITE_WAITERS) == 0) {
2852 rv = casueword32(&rwlock->rw_state, state,
2853 &oldstate, state | URWLOCK_WRITE_WAITERS);
2858 if (oldstate == state)
2861 error = umtxq_check_susp(td);
2866 umtxq_unbusy_unlocked(&uq->uq_key);
2870 if (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2871 umtxq_unbusy_unlocked(&uq->uq_key);
2872 error = umtxq_check_susp(td);
2878 rv = fueword32(&rwlock->rw_blocked_writers,
2881 umtxq_unbusy_unlocked(&uq->uq_key);
2885 suword32(&rwlock->rw_blocked_writers, blocked_writers+1);
2887 while ((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) {
2888 umtxq_lock(&uq->uq_key);
2889 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2890 umtxq_unbusy(&uq->uq_key);
2892 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
2895 umtxq_busy(&uq->uq_key);
2896 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2897 umtxq_unlock(&uq->uq_key);
2900 rv = fueword32(&rwlock->rw_state, &state);
2907 rv = fueword32(&rwlock->rw_blocked_writers,
2910 umtxq_unbusy_unlocked(&uq->uq_key);
2914 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
2915 if (blocked_writers == 1) {
2916 rv = fueword32(&rwlock->rw_state, &state);
2918 umtxq_unbusy_unlocked(&uq->uq_key);
2923 rv = casueword32(&rwlock->rw_state, state,
2924 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
2929 if (oldstate == state)
2932 error = umtxq_check_susp(td);
2934 * We are leaving the URWLOCK_WRITE_WAITERS
2935 * behind, but this should not harm the
2941 rv = fueword32(&rwlock->rw_blocked_readers,
2944 umtxq_unbusy_unlocked(&uq->uq_key);
2949 blocked_readers = 0;
2951 umtxq_unbusy_unlocked(&uq->uq_key);
2954 umtx_key_release(&uq->uq_key);
2955 if (error == ERESTART)
2961 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
2965 int32_t state, oldstate;
2966 int error, rv, q, count;
2969 error = fueword32(&rwlock->rw_flags, &flags);
2972 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2976 error = fueword32(&rwlock->rw_state, &state);
2981 if (state & URWLOCK_WRITE_OWNER) {
2983 rv = casueword32(&rwlock->rw_state, state,
2984 &oldstate, state & ~URWLOCK_WRITE_OWNER);
2989 if (oldstate != state) {
2991 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
2995 error = umtxq_check_susp(td);
3001 } else if (URWLOCK_READER_COUNT(state) != 0) {
3003 rv = casueword32(&rwlock->rw_state, state,
3004 &oldstate, state - 1);
3009 if (oldstate != state) {
3011 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3015 error = umtxq_check_susp(td);
3028 if (!(flags & URWLOCK_PREFER_READER)) {
3029 if (state & URWLOCK_WRITE_WAITERS) {
3031 q = UMTX_EXCLUSIVE_QUEUE;
3032 } else if (state & URWLOCK_READ_WAITERS) {
3034 q = UMTX_SHARED_QUEUE;
3037 if (state & URWLOCK_READ_WAITERS) {
3039 q = UMTX_SHARED_QUEUE;
3040 } else if (state & URWLOCK_WRITE_WAITERS) {
3042 q = UMTX_EXCLUSIVE_QUEUE;
3047 umtxq_lock(&uq->uq_key);
3048 umtxq_busy(&uq->uq_key);
3049 umtxq_signal_queue(&uq->uq_key, count, q);
3050 umtxq_unbusy(&uq->uq_key);
3051 umtxq_unlock(&uq->uq_key);
3054 umtx_key_release(&uq->uq_key);
3058 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3060 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3062 struct abs_timeout timo;
3064 uint32_t flags, count, count1;
3068 error = fueword32(&sem->_flags, &flags);
3071 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3075 if (timeout != NULL)
3076 abs_timeout_init2(&timo, timeout);
3078 umtxq_lock(&uq->uq_key);
3079 umtxq_busy(&uq->uq_key);
3081 umtxq_unlock(&uq->uq_key);
3082 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3084 rv = fueword32(&sem->_count, &count);
3085 if (rv == -1 || count != 0) {
3086 umtxq_lock(&uq->uq_key);
3087 umtxq_unbusy(&uq->uq_key);
3089 umtxq_unlock(&uq->uq_key);
3090 umtx_key_release(&uq->uq_key);
3091 return (rv == -1 ? EFAULT : 0);
3093 umtxq_lock(&uq->uq_key);
3094 umtxq_unbusy(&uq->uq_key);
3096 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3098 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3102 /* A relative timeout cannot be restarted. */
3103 if (error == ERESTART && timeout != NULL &&
3104 (timeout->_flags & UMTX_ABSTIME) == 0)
3107 umtxq_unlock(&uq->uq_key);
3108 umtx_key_release(&uq->uq_key);
3113 * Signal a userland semaphore.
3116 do_sem_wake(struct thread *td, struct _usem *sem)
3118 struct umtx_key key;
3122 error = fueword32(&sem->_flags, &flags);
3125 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3129 cnt = umtxq_count(&key);
3132 * Check if count is greater than 0, this means the memory is
3133 * still being referenced by user code, so we can safely
3134 * update _has_waiters flag.
3138 error = suword32(&sem->_has_waiters, 0);
3143 umtxq_signal(&key, 1);
3147 umtx_key_release(&key);
3153 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3155 struct abs_timeout timo;
3157 uint32_t count, flags;
3161 flags = fuword32(&sem->_flags);
3162 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3166 if (timeout != NULL)
3167 abs_timeout_init2(&timo, timeout);
3169 umtxq_lock(&uq->uq_key);
3170 umtxq_busy(&uq->uq_key);
3172 umtxq_unlock(&uq->uq_key);
3173 rv = fueword32(&sem->_count, &count);
3175 umtxq_lock(&uq->uq_key);
3176 umtxq_unbusy(&uq->uq_key);
3178 umtxq_unlock(&uq->uq_key);
3179 umtx_key_release(&uq->uq_key);
3183 if (USEM_COUNT(count) != 0) {
3184 umtxq_lock(&uq->uq_key);
3185 umtxq_unbusy(&uq->uq_key);
3187 umtxq_unlock(&uq->uq_key);
3188 umtx_key_release(&uq->uq_key);
3191 if (count == USEM_HAS_WAITERS)
3193 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3195 umtxq_lock(&uq->uq_key);
3196 umtxq_unbusy(&uq->uq_key);
3198 umtxq_unlock(&uq->uq_key);
3199 umtx_key_release(&uq->uq_key);
3205 umtxq_lock(&uq->uq_key);
3206 umtxq_unbusy(&uq->uq_key);
3208 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3210 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3214 /* A relative timeout cannot be restarted. */
3215 if (error == ERESTART && timeout != NULL &&
3216 (timeout->_flags & UMTX_ABSTIME) == 0)
3219 umtxq_unlock(&uq->uq_key);
3220 umtx_key_release(&uq->uq_key);
3225 * Signal a userland semaphore.
3228 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3230 struct umtx_key key;
3232 uint32_t count, flags;
3234 rv = fueword32(&sem->_flags, &flags);
3237 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3241 cnt = umtxq_count(&key);
3244 * If this was the last sleeping thread, clear the waiters
3249 rv = fueword32(&sem->_count, &count);
3250 while (rv != -1 && count & USEM_HAS_WAITERS)
3251 rv = casueword32(&sem->_count, count, &count,
3252 count & ~USEM_HAS_WAITERS);
3258 umtxq_signal(&key, 1);
3262 umtx_key_release(&key);
3267 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3271 error = copyin(addr, tsp, sizeof(struct timespec));
3273 if (tsp->tv_sec < 0 ||
3274 tsp->tv_nsec >= 1000000000 ||
3282 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3286 if (size <= sizeof(struct timespec)) {
3287 tp->_clockid = CLOCK_REALTIME;
3289 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3291 error = copyin(addr, tp, sizeof(struct _umtx_time));
3294 if (tp->_timeout.tv_sec < 0 ||
3295 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3301 __umtx_op_unimpl(struct thread *td, struct _umtx_op_args *uap)
3304 return (EOPNOTSUPP);
3308 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3310 struct _umtx_time timeout, *tm_p;
3313 if (uap->uaddr2 == NULL)
3316 error = umtx_copyin_umtx_time(
3317 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3322 return (do_wait(td, uap->obj, uap->val, tm_p, 0, 0));
3326 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3328 struct _umtx_time timeout, *tm_p;
3331 if (uap->uaddr2 == NULL)
3334 error = umtx_copyin_umtx_time(
3335 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3340 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3344 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3346 struct _umtx_time *tm_p, timeout;
3349 if (uap->uaddr2 == NULL)
3352 error = umtx_copyin_umtx_time(
3353 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3358 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3362 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3365 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3368 #define BATCH_SIZE 128
3370 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3372 char *uaddrs[BATCH_SIZE], **upp;
3373 int count, error, i, pos, tocopy;
3375 upp = (char **)uap->obj;
3377 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3379 tocopy = MIN(count, BATCH_SIZE);
3380 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3383 for (i = 0; i < tocopy; ++i)
3384 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3391 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3394 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3398 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3400 struct _umtx_time *tm_p, timeout;
3403 /* Allow a null timespec (wait forever). */
3404 if (uap->uaddr2 == NULL)
3407 error = umtx_copyin_umtx_time(
3408 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3413 return (do_lock_umutex(td, uap->obj, tm_p, 0));
3417 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3420 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
3424 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3426 struct _umtx_time *tm_p, timeout;
3429 /* Allow a null timespec (wait forever). */
3430 if (uap->uaddr2 == NULL)
3433 error = umtx_copyin_umtx_time(
3434 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3439 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
3443 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3446 return (do_wake_umutex(td, uap->obj));
3450 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3453 return (do_unlock_umutex(td, uap->obj, false));
3457 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3460 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
3464 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3466 struct timespec *ts, timeout;
3469 /* Allow a null timespec (wait forever). */
3470 if (uap->uaddr2 == NULL)
3473 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3478 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3482 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3485 return (do_cv_signal(td, uap->obj));
3489 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3492 return (do_cv_broadcast(td, uap->obj));
3496 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3498 struct _umtx_time timeout;
3501 /* Allow a null timespec (wait forever). */
3502 if (uap->uaddr2 == NULL) {
3503 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3505 error = umtx_copyin_umtx_time(uap->uaddr2,
3506 (size_t)uap->uaddr1, &timeout);
3509 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3515 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3517 struct _umtx_time timeout;
3520 /* Allow a null timespec (wait forever). */
3521 if (uap->uaddr2 == NULL) {
3522 error = do_rw_wrlock(td, uap->obj, 0);
3524 error = umtx_copyin_umtx_time(uap->uaddr2,
3525 (size_t)uap->uaddr1, &timeout);
3529 error = do_rw_wrlock(td, uap->obj, &timeout);
3535 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3538 return (do_rw_unlock(td, uap->obj));
3541 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3543 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3545 struct _umtx_time *tm_p, timeout;
3548 /* Allow a null timespec (wait forever). */
3549 if (uap->uaddr2 == NULL)
3552 error = umtx_copyin_umtx_time(
3553 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3558 return (do_sem_wait(td, uap->obj, tm_p));
3562 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3565 return (do_sem_wake(td, uap->obj));
3570 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3573 return (do_wake2_umutex(td, uap->obj, uap->val));
3577 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap)
3579 struct _umtx_time *tm_p, timeout;
3582 /* Allow a null timespec (wait forever). */
3583 if (uap->uaddr2 == NULL)
3586 error = umtx_copyin_umtx_time(
3587 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3592 return (do_sem2_wait(td, uap->obj, tm_p));
3596 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap)
3599 return (do_sem2_wake(td, uap->obj));
3602 #define USHM_OBJ_UMTX(o) \
3603 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
3605 #define USHMF_REG_LINKED 0x0001
3606 #define USHMF_OBJ_LINKED 0x0002
3607 struct umtx_shm_reg {
3608 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
3609 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
3610 struct umtx_key ushm_key;
3611 struct ucred *ushm_cred;
3612 struct shmfd *ushm_obj;
3617 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
3618 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
3620 static uma_zone_t umtx_shm_reg_zone;
3621 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
3622 static struct mtx umtx_shm_lock;
3623 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
3624 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
3626 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
3629 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
3631 struct umtx_shm_reg_head d;
3632 struct umtx_shm_reg *reg, *reg1;
3635 mtx_lock(&umtx_shm_lock);
3636 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
3637 mtx_unlock(&umtx_shm_lock);
3638 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
3639 TAILQ_REMOVE(&d, reg, ushm_reg_link);
3640 umtx_shm_free_reg(reg);
3644 static struct task umtx_shm_reg_delfree_task =
3645 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
3647 static struct umtx_shm_reg *
3648 umtx_shm_find_reg_locked(const struct umtx_key *key)
3650 struct umtx_shm_reg *reg;
3651 struct umtx_shm_reg_head *reg_head;
3653 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
3654 mtx_assert(&umtx_shm_lock, MA_OWNED);
3655 reg_head = &umtx_shm_registry[key->hash];
3656 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
3657 KASSERT(reg->ushm_key.shared,
3658 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
3659 if (reg->ushm_key.info.shared.object ==
3660 key->info.shared.object &&
3661 reg->ushm_key.info.shared.offset ==
3662 key->info.shared.offset) {
3663 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
3664 KASSERT(reg->ushm_refcnt > 0,
3665 ("reg %p refcnt 0 onlist", reg));
3666 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
3667 ("reg %p not linked", reg));
3675 static struct umtx_shm_reg *
3676 umtx_shm_find_reg(const struct umtx_key *key)
3678 struct umtx_shm_reg *reg;
3680 mtx_lock(&umtx_shm_lock);
3681 reg = umtx_shm_find_reg_locked(key);
3682 mtx_unlock(&umtx_shm_lock);
3687 umtx_shm_free_reg(struct umtx_shm_reg *reg)
3690 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
3691 crfree(reg->ushm_cred);
3692 shm_drop(reg->ushm_obj);
3693 uma_zfree(umtx_shm_reg_zone, reg);
3697 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
3701 mtx_assert(&umtx_shm_lock, MA_OWNED);
3702 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
3704 res = reg->ushm_refcnt == 0;
3706 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
3707 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
3708 reg, ushm_reg_link);
3709 reg->ushm_flags &= ~USHMF_REG_LINKED;
3711 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
3712 LIST_REMOVE(reg, ushm_obj_link);
3713 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
3720 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
3726 object = reg->ushm_obj->shm_object;
3727 VM_OBJECT_WLOCK(object);
3728 object->flags |= OBJ_UMTXDEAD;
3729 VM_OBJECT_WUNLOCK(object);
3731 mtx_lock(&umtx_shm_lock);
3732 dofree = umtx_shm_unref_reg_locked(reg, force);
3733 mtx_unlock(&umtx_shm_lock);
3735 umtx_shm_free_reg(reg);
3739 umtx_shm_object_init(vm_object_t object)
3742 LIST_INIT(USHM_OBJ_UMTX(object));
3746 umtx_shm_object_terminated(vm_object_t object)
3748 struct umtx_shm_reg *reg, *reg1;
3752 mtx_lock(&umtx_shm_lock);
3753 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
3754 if (umtx_shm_unref_reg_locked(reg, true)) {
3755 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
3760 mtx_unlock(&umtx_shm_lock);
3762 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
3766 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
3767 struct umtx_shm_reg **res)
3769 struct umtx_shm_reg *reg, *reg1;
3773 reg = umtx_shm_find_reg(key);
3778 cred = td->td_ucred;
3779 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
3781 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
3782 reg->ushm_refcnt = 1;
3783 bcopy(key, ®->ushm_key, sizeof(*key));
3784 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR);
3785 reg->ushm_cred = crhold(cred);
3786 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
3788 umtx_shm_free_reg(reg);
3791 mtx_lock(&umtx_shm_lock);
3792 reg1 = umtx_shm_find_reg_locked(key);
3794 mtx_unlock(&umtx_shm_lock);
3795 umtx_shm_free_reg(reg);
3800 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
3801 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
3803 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
3804 mtx_unlock(&umtx_shm_lock);
3810 umtx_shm_alive(struct thread *td, void *addr)
3813 vm_map_entry_t entry;
3820 map = &td->td_proc->p_vmspace->vm_map;
3821 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
3822 &object, &pindex, &prot, &wired);
3823 if (res != KERN_SUCCESS)
3828 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
3829 vm_map_lookup_done(map, entry);
3838 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
3839 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
3840 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
3841 for (i = 0; i < nitems(umtx_shm_registry); i++)
3842 TAILQ_INIT(&umtx_shm_registry[i]);
3846 umtx_shm(struct thread *td, void *addr, u_int flags)
3848 struct umtx_key key;
3849 struct umtx_shm_reg *reg;
3853 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
3854 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
3856 if ((flags & UMTX_SHM_ALIVE) != 0)
3857 return (umtx_shm_alive(td, addr));
3858 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
3861 KASSERT(key.shared == 1, ("non-shared key"));
3862 if ((flags & UMTX_SHM_CREAT) != 0) {
3863 error = umtx_shm_create_reg(td, &key, ®);
3865 reg = umtx_shm_find_reg(&key);
3869 umtx_key_release(&key);
3872 KASSERT(reg != NULL, ("no reg"));
3873 if ((flags & UMTX_SHM_DESTROY) != 0) {
3874 umtx_shm_unref_reg(reg, true);
3878 error = mac_posixshm_check_open(td->td_ucred,
3879 reg->ushm_obj, FFLAGS(O_RDWR));
3882 error = shm_access(reg->ushm_obj, td->td_ucred,
3886 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
3888 shm_hold(reg->ushm_obj);
3889 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
3891 td->td_retval[0] = fd;
3895 umtx_shm_unref_reg(reg, false);
3900 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap)
3903 return (umtx_shm(td, uap->uaddr1, uap->val));
3907 umtx_robust_lists(struct thread *td, struct umtx_robust_lists_params *rbp)
3910 td->td_rb_list = rbp->robust_list_offset;
3911 td->td_rbp_list = rbp->robust_priv_list_offset;
3912 td->td_rb_inact = rbp->robust_inact_offset;
3917 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap)
3919 struct umtx_robust_lists_params rb;
3922 if (uap->val > sizeof(rb))
3924 bzero(&rb, sizeof(rb));
3925 error = copyin(uap->uaddr1, &rb, uap->val);
3928 return (umtx_robust_lists(td, &rb));
3931 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
3933 static const _umtx_op_func op_table[] = {
3934 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
3935 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
3936 [UMTX_OP_WAIT] = __umtx_op_wait,
3937 [UMTX_OP_WAKE] = __umtx_op_wake,
3938 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
3939 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
3940 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
3941 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
3942 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
3943 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
3944 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
3945 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
3946 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
3947 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
3948 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
3949 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
3950 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
3951 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
3952 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
3953 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3954 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
3955 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
3957 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
3958 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
3960 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
3961 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
3962 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
3963 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
3964 [UMTX_OP_SHM] = __umtx_op_shm,
3965 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
3969 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
3972 if ((unsigned)uap->op < nitems(op_table))
3973 return (*op_table[uap->op])(td, uap);
3977 #ifdef COMPAT_FREEBSD32
3984 struct umtx_time32 {
3985 struct timespec32 timeout;
3991 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
3993 struct timespec32 ts32;
3996 error = copyin(addr, &ts32, sizeof(struct timespec32));
3998 if (ts32.tv_sec < 0 ||
3999 ts32.tv_nsec >= 1000000000 ||
4003 tsp->tv_sec = ts32.tv_sec;
4004 tsp->tv_nsec = ts32.tv_nsec;
4011 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
4013 struct umtx_time32 t32;
4016 t32.clockid = CLOCK_REALTIME;
4018 if (size <= sizeof(struct timespec32))
4019 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
4021 error = copyin(addr, &t32, sizeof(struct umtx_time32));
4024 if (t32.timeout.tv_sec < 0 ||
4025 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
4027 tp->_timeout.tv_sec = t32.timeout.tv_sec;
4028 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
4029 tp->_flags = t32.flags;
4030 tp->_clockid = t32.clockid;
4035 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4037 struct _umtx_time *tm_p, timeout;
4040 if (uap->uaddr2 == NULL)
4043 error = umtx_copyin_umtx_time32(uap->uaddr2,
4044 (size_t)uap->uaddr1, &timeout);
4049 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
4053 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4055 struct _umtx_time *tm_p, timeout;
4058 /* Allow a null timespec (wait forever). */
4059 if (uap->uaddr2 == NULL)
4062 error = umtx_copyin_umtx_time(uap->uaddr2,
4063 (size_t)uap->uaddr1, &timeout);
4068 return (do_lock_umutex(td, uap->obj, tm_p, 0));
4072 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4074 struct _umtx_time *tm_p, timeout;
4077 /* Allow a null timespec (wait forever). */
4078 if (uap->uaddr2 == NULL)
4081 error = umtx_copyin_umtx_time32(uap->uaddr2,
4082 (size_t)uap->uaddr1, &timeout);
4087 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
4091 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4093 struct timespec *ts, timeout;
4096 /* Allow a null timespec (wait forever). */
4097 if (uap->uaddr2 == NULL)
4100 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
4105 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
4109 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4111 struct _umtx_time timeout;
4114 /* Allow a null timespec (wait forever). */
4115 if (uap->uaddr2 == NULL) {
4116 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4118 error = umtx_copyin_umtx_time32(uap->uaddr2,
4119 (size_t)uap->uaddr1, &timeout);
4122 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4128 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4130 struct _umtx_time timeout;
4133 /* Allow a null timespec (wait forever). */
4134 if (uap->uaddr2 == NULL) {
4135 error = do_rw_wrlock(td, uap->obj, 0);
4137 error = umtx_copyin_umtx_time32(uap->uaddr2,
4138 (size_t)uap->uaddr1, &timeout);
4141 error = do_rw_wrlock(td, uap->obj, &timeout);
4147 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
4149 struct _umtx_time *tm_p, timeout;
4152 if (uap->uaddr2 == NULL)
4155 error = umtx_copyin_umtx_time32(
4156 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
4161 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
4164 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4166 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4168 struct _umtx_time *tm_p, timeout;
4171 /* Allow a null timespec (wait forever). */
4172 if (uap->uaddr2 == NULL)
4175 error = umtx_copyin_umtx_time32(uap->uaddr2,
4176 (size_t)uap->uaddr1, &timeout);
4181 return (do_sem_wait(td, uap->obj, tm_p));
4186 __umtx_op_sem2_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4188 struct _umtx_time *tm_p, timeout;
4191 /* Allow a null timespec (wait forever). */
4192 if (uap->uaddr2 == NULL)
4195 error = umtx_copyin_umtx_time32(uap->uaddr2,
4196 (size_t)uap->uaddr1, &timeout);
4201 return (do_sem2_wait(td, uap->obj, tm_p));
4205 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
4207 uint32_t uaddrs[BATCH_SIZE], **upp;
4208 int count, error, i, pos, tocopy;
4210 upp = (uint32_t **)uap->obj;
4212 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
4214 tocopy = MIN(count, BATCH_SIZE);
4215 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
4218 for (i = 0; i < tocopy; ++i)
4219 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
4226 struct umtx_robust_lists_params_compat32 {
4227 uint32_t robust_list_offset;
4228 uint32_t robust_priv_list_offset;
4229 uint32_t robust_inact_offset;
4233 __umtx_op_robust_lists_compat32(struct thread *td, struct _umtx_op_args *uap)
4235 struct umtx_robust_lists_params rb;
4236 struct umtx_robust_lists_params_compat32 rb32;
4239 if (uap->val > sizeof(rb32))
4241 bzero(&rb, sizeof(rb));
4242 bzero(&rb32, sizeof(rb32));
4243 error = copyin(uap->uaddr1, &rb32, uap->val);
4246 rb.robust_list_offset = rb32.robust_list_offset;
4247 rb.robust_priv_list_offset = rb32.robust_priv_list_offset;
4248 rb.robust_inact_offset = rb32.robust_inact_offset;
4249 return (umtx_robust_lists(td, &rb));
4252 static const _umtx_op_func op_table_compat32[] = {
4253 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
4254 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
4255 [UMTX_OP_WAIT] = __umtx_op_wait_compat32,
4256 [UMTX_OP_WAKE] = __umtx_op_wake,
4257 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4258 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex_compat32,
4259 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4260 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4261 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait_compat32,
4262 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4263 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4264 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_compat32,
4265 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock_compat32,
4266 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock_compat32,
4267 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4268 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private_compat32,
4269 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4270 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex_compat32,
4271 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4272 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4273 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait_compat32,
4274 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4276 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4277 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4279 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private32,
4280 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4281 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait_compat32,
4282 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4283 [UMTX_OP_SHM] = __umtx_op_shm,
4284 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists_compat32,
4288 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
4291 if ((unsigned)uap->op < nitems(op_table_compat32)) {
4292 return (*op_table_compat32[uap->op])(td,
4293 (struct _umtx_op_args *)uap);
4300 umtx_thread_init(struct thread *td)
4303 td->td_umtxq = umtxq_alloc();
4304 td->td_umtxq->uq_thread = td;
4308 umtx_thread_fini(struct thread *td)
4311 umtxq_free(td->td_umtxq);
4315 * It will be called when new thread is created, e.g fork().
4318 umtx_thread_alloc(struct thread *td)
4323 uq->uq_inherited_pri = PRI_MAX;
4325 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4326 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4327 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4328 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4334 * Clear robust lists for all process' threads, not delaying the
4335 * cleanup to thread_exit hook, since the relevant address space is
4336 * destroyed right now.
4339 umtx_exec_hook(void *arg __unused, struct proc *p,
4340 struct image_params *imgp __unused)
4344 KASSERT(p == curproc, ("need curproc"));
4346 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4347 (p->p_flag & P_STOPPED_SINGLE) != 0,
4348 ("curproc must be single-threaded"));
4349 FOREACH_THREAD_IN_PROC(p, td) {
4350 KASSERT(td == curthread ||
4351 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4352 ("running thread %p %p", p, td));
4354 umtx_thread_cleanup(td);
4356 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4362 * thread_exit() hook.
4365 umtx_thread_exit(struct thread *td)
4368 umtx_thread_cleanup(td);
4372 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res)
4375 #ifdef COMPAT_FREEBSD32
4380 #ifdef COMPAT_FREEBSD32
4381 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4382 error = fueword32((void *)ptr, &res32);
4388 error = fueword((void *)ptr, &res1);
4398 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list)
4400 #ifdef COMPAT_FREEBSD32
4401 struct umutex32 m32;
4403 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4404 memcpy(&m32, m, sizeof(m32));
4405 *rb_list = m32.m_rb_lnk;
4408 *rb_list = m->m_rb_lnk;
4412 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact)
4417 KASSERT(td->td_proc == curproc, ("need current vmspace"));
4418 error = copyin((void *)rbp, &m, sizeof(m));
4421 if (rb_list != NULL)
4422 umtx_read_rb_list(td, &m, rb_list);
4423 if ((m.m_flags & UMUTEX_ROBUST) == 0)
4425 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
4426 /* inact is cleared after unlock, allow the inconsistency */
4427 return (inact ? 0 : EINVAL);
4428 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
4432 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
4441 error = umtx_read_uptr(td, rb_list, &rbp);
4442 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
4443 if (rbp == *rb_inact) {
4448 error = umtx_handle_rb(td, rbp, &rbp, inact);
4450 if (i == umtx_max_rb && umtx_verbose_rb) {
4451 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
4452 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
4454 if (error != 0 && umtx_verbose_rb) {
4455 uprintf("comm %s pid %d: handling %srb error %d\n",
4456 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
4461 * Clean up umtx data.
4464 umtx_thread_cleanup(struct thread *td)
4471 * Disown pi mutexes.
4475 mtx_lock(&umtx_lock);
4476 uq->uq_inherited_pri = PRI_MAX;
4477 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
4478 pi->pi_owner = NULL;
4479 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
4481 mtx_unlock(&umtx_lock);
4483 sched_lend_user_prio(td, PRI_MAX);
4488 * Handle terminated robust mutexes. Must be done after
4489 * robust pi disown, otherwise unlock could see unowned
4492 rb_inact = td->td_rb_inact;
4494 (void)umtx_read_uptr(td, rb_inact, &rb_inact);
4495 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "");
4496 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ");
4498 (void)umtx_handle_rb(td, rb_inact, NULL, true);