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>
67 #include <security/mac/mac_framework.h>
70 #include <vm/vm_param.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_object.h>
75 #include <machine/atomic.h>
76 #include <machine/cpu.h>
78 #ifdef COMPAT_FREEBSD32
79 #include <compat/freebsd32/freebsd32_proto.h>
83 #define _UMUTEX_WAIT 2
86 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
87 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
90 /* Priority inheritance mutex info. */
93 struct thread *pi_owner;
98 /* List entry to link umtx holding by thread */
99 TAILQ_ENTRY(umtx_pi) pi_link;
101 /* List entry in hash */
102 TAILQ_ENTRY(umtx_pi) pi_hashlink;
104 /* List for waiters */
105 TAILQ_HEAD(,umtx_q) pi_blocked;
107 /* Identify a userland lock object */
108 struct umtx_key pi_key;
111 /* A userland synchronous object user. */
113 /* Linked list for the hash. */
114 TAILQ_ENTRY(umtx_q) uq_link;
117 struct umtx_key uq_key;
121 #define UQF_UMTXQ 0x0001
123 /* The thread waits on. */
124 struct thread *uq_thread;
127 * Blocked on PI mutex. read can use chain lock
128 * or umtx_lock, write must have both chain lock and
129 * umtx_lock being hold.
131 struct umtx_pi *uq_pi_blocked;
133 /* On blocked list */
134 TAILQ_ENTRY(umtx_q) uq_lockq;
136 /* Thread contending with us */
137 TAILQ_HEAD(,umtx_pi) uq_pi_contested;
139 /* Inherited priority from PP mutex */
140 u_char uq_inherited_pri;
142 /* Spare queue ready to be reused */
143 struct umtxq_queue *uq_spare_queue;
145 /* The queue we on */
146 struct umtxq_queue *uq_cur_queue;
149 TAILQ_HEAD(umtxq_head, umtx_q);
151 /* Per-key wait-queue */
153 struct umtxq_head head;
155 LIST_ENTRY(umtxq_queue) link;
159 LIST_HEAD(umtxq_list, umtxq_queue);
161 /* Userland lock object's wait-queue chain */
163 /* Lock for this chain. */
166 /* List of sleep queues. */
167 struct umtxq_list uc_queue[2];
168 #define UMTX_SHARED_QUEUE 0
169 #define UMTX_EXCLUSIVE_QUEUE 1
171 LIST_HEAD(, umtxq_queue) uc_spare_queue;
176 /* Chain lock waiters */
179 /* All PI in the list */
180 TAILQ_HEAD(,umtx_pi) uc_pi_list;
182 #ifdef UMTX_PROFILING
188 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
191 * Don't propagate time-sharing priority, there is a security reason,
192 * a user can simply introduce PI-mutex, let thread A lock the mutex,
193 * and let another thread B block on the mutex, because B is
194 * sleeping, its priority will be boosted, this causes A's priority to
195 * be boosted via priority propagating too and will never be lowered even
196 * if it is using 100%CPU, this is unfair to other processes.
199 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
200 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
201 PRI_MAX_TIMESHARE : (td)->td_user_pri)
203 #define GOLDEN_RATIO_PRIME 2654404609U
205 #define UMTX_CHAINS 512
207 #define UMTX_SHIFTS (__WORD_BIT - 9)
209 #define GET_SHARE(flags) \
210 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
212 #define BUSY_SPINS 200
216 bool is_abs_real; /* TIMER_ABSTIME && CLOCK_REALTIME* */
221 #ifdef COMPAT_FREEBSD32
223 volatile __lwpid_t m_owner; /* Owner of the mutex */
224 __uint32_t m_flags; /* Flags of the mutex */
225 __uint32_t m_ceilings[2]; /* Priority protect ceiling */
226 __uint32_t m_rb_lnk; /* Robust linkage */
228 __uint32_t m_spare[2];
231 _Static_assert(sizeof(struct umutex) == sizeof(struct umutex32), "umutex32");
232 _Static_assert(__offsetof(struct umutex, m_spare[0]) ==
233 __offsetof(struct umutex32, m_spare[0]), "m_spare32");
236 int umtx_shm_vnobj_persistent = 0;
237 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_vnode_persistent, CTLFLAG_RWTUN,
238 &umtx_shm_vnobj_persistent, 0,
239 "False forces destruction of umtx attached to file, on last close");
240 static int umtx_max_rb = 1000;
241 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_max_robust, CTLFLAG_RWTUN,
245 static uma_zone_t umtx_pi_zone;
246 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
247 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
248 static int umtx_pi_allocated;
250 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW, 0, "umtx debug");
251 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
252 &umtx_pi_allocated, 0, "Allocated umtx_pi");
253 static int umtx_verbose_rb = 1;
254 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
258 #ifdef UMTX_PROFILING
259 static long max_length;
260 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
261 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD, 0, "umtx chain stats");
264 static void abs_timeout_update(struct abs_timeout *timo);
266 static void umtx_shm_init(void);
267 static void umtxq_sysinit(void *);
268 static void umtxq_hash(struct umtx_key *key);
269 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
270 static void umtxq_lock(struct umtx_key *key);
271 static void umtxq_unlock(struct umtx_key *key);
272 static void umtxq_busy(struct umtx_key *key);
273 static void umtxq_unbusy(struct umtx_key *key);
274 static void umtxq_insert_queue(struct umtx_q *uq, int q);
275 static void umtxq_remove_queue(struct umtx_q *uq, int q);
276 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *);
277 static int umtxq_count(struct umtx_key *key);
278 static struct umtx_pi *umtx_pi_alloc(int);
279 static void umtx_pi_free(struct umtx_pi *pi);
280 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
282 static void umtx_thread_cleanup(struct thread *td);
283 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
284 struct image_params *imgp __unused);
285 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
287 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
288 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
289 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
291 static struct mtx umtx_lock;
293 #ifdef UMTX_PROFILING
295 umtx_init_profiling(void)
297 struct sysctl_oid *chain_oid;
301 for (i = 0; i < UMTX_CHAINS; ++i) {
302 snprintf(chain_name, sizeof(chain_name), "%d", i);
303 chain_oid = SYSCTL_ADD_NODE(NULL,
304 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
305 chain_name, CTLFLAG_RD, NULL, "umtx hash stats");
306 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
307 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
308 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
309 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
314 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
318 struct umtxq_chain *uc;
319 u_int fract, i, j, tot, whole;
320 u_int sf0, sf1, sf2, sf3, sf4;
321 u_int si0, si1, si2, si3, si4;
322 u_int sw0, sw1, sw2, sw3, sw4;
324 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
325 for (i = 0; i < 2; i++) {
327 for (j = 0; j < UMTX_CHAINS; ++j) {
328 uc = &umtxq_chains[i][j];
329 mtx_lock(&uc->uc_lock);
330 tot += uc->max_length;
331 mtx_unlock(&uc->uc_lock);
334 sbuf_printf(&sb, "%u) Empty ", i);
336 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
337 si0 = si1 = si2 = si3 = si4 = 0;
338 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
339 for (j = 0; j < UMTX_CHAINS; j++) {
340 uc = &umtxq_chains[i][j];
341 mtx_lock(&uc->uc_lock);
342 whole = uc->max_length * 100;
343 mtx_unlock(&uc->uc_lock);
344 fract = (whole % tot) * 100;
345 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
349 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
354 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
359 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
364 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
371 sbuf_printf(&sb, "queue %u:\n", i);
372 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
374 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
376 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
378 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
380 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
386 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
392 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
394 struct umtxq_chain *uc;
399 error = sysctl_handle_int(oidp, &clear, 0, req);
400 if (error != 0 || req->newptr == NULL)
404 for (i = 0; i < 2; ++i) {
405 for (j = 0; j < UMTX_CHAINS; ++j) {
406 uc = &umtxq_chains[i][j];
407 mtx_lock(&uc->uc_lock);
410 mtx_unlock(&uc->uc_lock);
417 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
418 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
419 sysctl_debug_umtx_chains_clear, "I", "Clear umtx chains statistics");
420 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
421 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
422 sysctl_debug_umtx_chains_peaks, "A", "Highest peaks in chains max length");
426 umtxq_sysinit(void *arg __unused)
430 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
431 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
432 for (i = 0; i < 2; ++i) {
433 for (j = 0; j < UMTX_CHAINS; ++j) {
434 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
435 MTX_DEF | MTX_DUPOK);
436 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
437 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
438 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
439 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
440 umtxq_chains[i][j].uc_busy = 0;
441 umtxq_chains[i][j].uc_waiters = 0;
442 #ifdef UMTX_PROFILING
443 umtxq_chains[i][j].length = 0;
444 umtxq_chains[i][j].max_length = 0;
448 #ifdef UMTX_PROFILING
449 umtx_init_profiling();
451 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
452 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
453 EVENTHANDLER_PRI_ANY);
462 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
463 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
465 TAILQ_INIT(&uq->uq_spare_queue->head);
466 TAILQ_INIT(&uq->uq_pi_contested);
467 uq->uq_inherited_pri = PRI_MAX;
472 umtxq_free(struct umtx_q *uq)
475 MPASS(uq->uq_spare_queue != NULL);
476 free(uq->uq_spare_queue, M_UMTX);
481 umtxq_hash(struct umtx_key *key)
485 n = (uintptr_t)key->info.both.a + key->info.both.b;
486 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
489 static inline struct umtxq_chain *
490 umtxq_getchain(struct umtx_key *key)
493 if (key->type <= TYPE_SEM)
494 return (&umtxq_chains[1][key->hash]);
495 return (&umtxq_chains[0][key->hash]);
502 umtxq_lock(struct umtx_key *key)
504 struct umtxq_chain *uc;
506 uc = umtxq_getchain(key);
507 mtx_lock(&uc->uc_lock);
514 umtxq_unlock(struct umtx_key *key)
516 struct umtxq_chain *uc;
518 uc = umtxq_getchain(key);
519 mtx_unlock(&uc->uc_lock);
523 * Set chain to busy state when following operation
524 * may be blocked (kernel mutex can not be used).
527 umtxq_busy(struct umtx_key *key)
529 struct umtxq_chain *uc;
531 uc = umtxq_getchain(key);
532 mtx_assert(&uc->uc_lock, MA_OWNED);
536 int count = BUSY_SPINS;
539 while (uc->uc_busy && --count > 0)
545 while (uc->uc_busy) {
547 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
558 umtxq_unbusy(struct umtx_key *key)
560 struct umtxq_chain *uc;
562 uc = umtxq_getchain(key);
563 mtx_assert(&uc->uc_lock, MA_OWNED);
564 KASSERT(uc->uc_busy != 0, ("not busy"));
571 umtxq_unbusy_unlocked(struct umtx_key *key)
579 static struct umtxq_queue *
580 umtxq_queue_lookup(struct umtx_key *key, int q)
582 struct umtxq_queue *uh;
583 struct umtxq_chain *uc;
585 uc = umtxq_getchain(key);
586 UMTXQ_LOCKED_ASSERT(uc);
587 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
588 if (umtx_key_match(&uh->key, key))
596 umtxq_insert_queue(struct umtx_q *uq, int q)
598 struct umtxq_queue *uh;
599 struct umtxq_chain *uc;
601 uc = umtxq_getchain(&uq->uq_key);
602 UMTXQ_LOCKED_ASSERT(uc);
603 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
604 uh = umtxq_queue_lookup(&uq->uq_key, q);
606 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
608 uh = uq->uq_spare_queue;
609 uh->key = uq->uq_key;
610 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
611 #ifdef UMTX_PROFILING
613 if (uc->length > uc->max_length) {
614 uc->max_length = uc->length;
615 if (uc->max_length > max_length)
616 max_length = uc->max_length;
620 uq->uq_spare_queue = NULL;
622 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
624 uq->uq_flags |= UQF_UMTXQ;
625 uq->uq_cur_queue = uh;
630 umtxq_remove_queue(struct umtx_q *uq, int q)
632 struct umtxq_chain *uc;
633 struct umtxq_queue *uh;
635 uc = umtxq_getchain(&uq->uq_key);
636 UMTXQ_LOCKED_ASSERT(uc);
637 if (uq->uq_flags & UQF_UMTXQ) {
638 uh = uq->uq_cur_queue;
639 TAILQ_REMOVE(&uh->head, uq, uq_link);
641 uq->uq_flags &= ~UQF_UMTXQ;
642 if (TAILQ_EMPTY(&uh->head)) {
643 KASSERT(uh->length == 0,
644 ("inconsistent umtxq_queue length"));
645 #ifdef UMTX_PROFILING
648 LIST_REMOVE(uh, link);
650 uh = LIST_FIRST(&uc->uc_spare_queue);
651 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
652 LIST_REMOVE(uh, link);
654 uq->uq_spare_queue = uh;
655 uq->uq_cur_queue = NULL;
660 * Check if there are multiple waiters
663 umtxq_count(struct umtx_key *key)
665 struct umtxq_queue *uh;
667 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
668 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
675 * Check if there are multiple PI waiters and returns first
679 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
681 struct umtxq_queue *uh;
684 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
685 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
687 *first = TAILQ_FIRST(&uh->head);
694 umtxq_check_susp(struct thread *td)
700 * The check for TDF_NEEDSUSPCHK is racy, but it is enough to
701 * eventually break the lockstep loop.
703 if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
708 if (P_SHOULDSTOP(p) ||
709 ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) {
710 if (p->p_flag & P_SINGLE_EXIT)
720 * Wake up threads waiting on an userland object.
724 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
726 struct umtxq_queue *uh;
731 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
732 uh = umtxq_queue_lookup(key, q);
734 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
735 umtxq_remove_queue(uq, q);
746 * Wake up specified thread.
749 umtxq_signal_thread(struct umtx_q *uq)
752 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
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 timo->is_abs_real = false;
774 abs_timeout_update(timo);
775 timespecadd(&timo->cur, timeout, &timo->end);
777 timo->end = *timeout;
778 timo->is_abs_real = clockid == CLOCK_REALTIME ||
779 clockid == CLOCK_REALTIME_FAST ||
780 clockid == CLOCK_REALTIME_PRECISE;
782 * If is_abs_real, umtxq_sleep will read the clock
783 * after setting td_rtcgen; otherwise, read it here.
785 if (!timo->is_abs_real) {
786 abs_timeout_update(timo);
792 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
795 abs_timeout_init(timo, umtxtime->_clockid,
796 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
800 abs_timeout_update(struct abs_timeout *timo)
803 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
807 abs_timeout_gethz(struct abs_timeout *timo)
811 if (timespeccmp(&timo->end, &timo->cur, <=))
813 timespecsub(&timo->end, &timo->cur, &tts);
814 return (tstohz(&tts));
818 umtx_unlock_val(uint32_t flags, bool rb)
822 return (UMUTEX_RB_OWNERDEAD);
823 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
824 return (UMUTEX_RB_NOTRECOV);
826 return (UMUTEX_UNOWNED);
831 * Put thread into sleep state, before sleeping, check if
832 * thread was removed from umtx queue.
835 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
837 struct umtxq_chain *uc;
840 if (abstime != NULL && abstime->is_abs_real) {
841 curthread->td_rtcgen = atomic_load_acq_int(&rtc_generation);
842 abs_timeout_update(abstime);
845 uc = umtxq_getchain(&uq->uq_key);
846 UMTXQ_LOCKED_ASSERT(uc);
848 if (!(uq->uq_flags & UQF_UMTXQ)) {
852 if (abstime != NULL) {
853 timo = abs_timeout_gethz(abstime);
860 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
861 if (error == EINTR || error == ERESTART) {
862 umtxq_lock(&uq->uq_key);
865 if (abstime != NULL) {
866 if (abstime->is_abs_real)
867 curthread->td_rtcgen =
868 atomic_load_acq_int(&rtc_generation);
869 abs_timeout_update(abstime);
871 umtxq_lock(&uq->uq_key);
874 curthread->td_rtcgen = 0;
879 * Convert userspace address into unique logical address.
882 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
884 struct thread *td = curthread;
886 vm_map_entry_t entry;
892 if (share == THREAD_SHARE) {
894 key->info.private.vs = td->td_proc->p_vmspace;
895 key->info.private.addr = (uintptr_t)addr;
897 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
898 map = &td->td_proc->p_vmspace->vm_map;
899 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
900 &entry, &key->info.shared.object, &pindex, &prot,
901 &wired) != KERN_SUCCESS) {
905 if ((share == PROCESS_SHARE) ||
906 (share == AUTO_SHARE &&
907 VM_INHERIT_SHARE == entry->inheritance)) {
909 key->info.shared.offset = (vm_offset_t)addr -
910 entry->start + entry->offset;
911 vm_object_reference(key->info.shared.object);
914 key->info.private.vs = td->td_proc->p_vmspace;
915 key->info.private.addr = (uintptr_t)addr;
917 vm_map_lookup_done(map, entry);
928 umtx_key_release(struct umtx_key *key)
931 vm_object_deallocate(key->info.shared.object);
935 * Fetch and compare value, sleep on the address if value is not changed.
938 do_wait(struct thread *td, void *addr, u_long id,
939 struct _umtx_time *timeout, int compat32, int is_private)
941 struct abs_timeout timo;
948 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
949 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
953 abs_timeout_init2(&timo, timeout);
955 umtxq_lock(&uq->uq_key);
957 umtxq_unlock(&uq->uq_key);
959 error = fueword(addr, &tmp);
963 error = fueword32(addr, &tmp32);
969 umtxq_lock(&uq->uq_key);
972 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
974 if ((uq->uq_flags & UQF_UMTXQ) == 0)
978 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
981 umtxq_unlock(&uq->uq_key);
982 umtx_key_release(&uq->uq_key);
983 if (error == ERESTART)
989 * Wake up threads sleeping on the specified address.
992 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
997 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
998 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1001 umtxq_signal(&key, n_wake);
1003 umtx_key_release(&key);
1008 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1011 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1012 struct _umtx_time *timeout, int mode)
1014 struct abs_timeout timo;
1016 uint32_t owner, old, id;
1022 if (timeout != NULL)
1023 abs_timeout_init2(&timo, timeout);
1026 * Care must be exercised when dealing with umtx structure. It
1027 * can fault on any access.
1030 rv = fueword32(&m->m_owner, &owner);
1033 if (mode == _UMUTEX_WAIT) {
1034 if (owner == UMUTEX_UNOWNED ||
1035 owner == UMUTEX_CONTESTED ||
1036 owner == UMUTEX_RB_OWNERDEAD ||
1037 owner == UMUTEX_RB_NOTRECOV)
1041 * Robust mutex terminated. Kernel duty is to
1042 * return EOWNERDEAD to the userspace. The
1043 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1044 * by the common userspace code.
1046 if (owner == UMUTEX_RB_OWNERDEAD) {
1047 rv = casueword32(&m->m_owner,
1048 UMUTEX_RB_OWNERDEAD, &owner,
1049 id | UMUTEX_CONTESTED);
1052 if (owner == UMUTEX_RB_OWNERDEAD)
1053 return (EOWNERDEAD); /* success */
1054 rv = umtxq_check_susp(td);
1059 if (owner == UMUTEX_RB_NOTRECOV)
1060 return (ENOTRECOVERABLE);
1063 * Try the uncontested case. This should be
1066 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1068 /* The address was invalid. */
1072 /* The acquire succeeded. */
1073 if (owner == UMUTEX_UNOWNED)
1077 * If no one owns it but it is contested try
1080 if (owner == UMUTEX_CONTESTED) {
1081 rv = casueword32(&m->m_owner,
1082 UMUTEX_CONTESTED, &owner,
1083 id | UMUTEX_CONTESTED);
1084 /* The address was invalid. */
1088 if (owner == UMUTEX_CONTESTED)
1091 rv = umtxq_check_susp(td);
1096 * If this failed the lock has
1103 if (mode == _UMUTEX_TRY)
1107 * If we caught a signal, we have retried and now
1113 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1114 GET_SHARE(flags), &uq->uq_key)) != 0)
1117 umtxq_lock(&uq->uq_key);
1118 umtxq_busy(&uq->uq_key);
1120 umtxq_unlock(&uq->uq_key);
1123 * Set the contested bit so that a release in user space
1124 * knows to use the system call for unlock. If this fails
1125 * either some one else has acquired the lock or it has been
1128 rv = casueword32(&m->m_owner, owner, &old,
1129 owner | UMUTEX_CONTESTED);
1131 /* The address was invalid. */
1133 umtxq_lock(&uq->uq_key);
1135 umtxq_unbusy(&uq->uq_key);
1136 umtxq_unlock(&uq->uq_key);
1137 umtx_key_release(&uq->uq_key);
1142 * We set the contested bit, sleep. Otherwise the lock changed
1143 * and we need to retry or we lost a race to the thread
1144 * unlocking the umtx.
1146 umtxq_lock(&uq->uq_key);
1147 umtxq_unbusy(&uq->uq_key);
1149 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1152 umtxq_unlock(&uq->uq_key);
1153 umtx_key_release(&uq->uq_key);
1156 error = umtxq_check_susp(td);
1163 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1166 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1168 struct umtx_key key;
1169 uint32_t owner, old, id, newlock;
1174 * Make sure we own this mtx.
1176 error = fueword32(&m->m_owner, &owner);
1180 if ((owner & ~UMUTEX_CONTESTED) != id)
1183 newlock = umtx_unlock_val(flags, rb);
1184 if ((owner & UMUTEX_CONTESTED) == 0) {
1185 error = casueword32(&m->m_owner, owner, &old, newlock);
1193 /* We should only ever be in here for contested locks */
1194 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1200 count = umtxq_count(&key);
1204 * When unlocking the umtx, it must be marked as unowned if
1205 * there is zero or one thread only waiting for it.
1206 * Otherwise, it must be marked as contested.
1209 newlock |= UMUTEX_CONTESTED;
1210 error = casueword32(&m->m_owner, owner, &old, newlock);
1212 umtxq_signal(&key, 1);
1215 umtx_key_release(&key);
1224 * Check if the mutex is available and wake up a waiter,
1225 * only for simple mutex.
1228 do_wake_umutex(struct thread *td, struct umutex *m)
1230 struct umtx_key key;
1236 error = fueword32(&m->m_owner, &owner);
1240 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1241 owner != UMUTEX_RB_NOTRECOV)
1244 error = fueword32(&m->m_flags, &flags);
1248 /* We should only ever be in here for contested locks */
1249 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1255 count = umtxq_count(&key);
1258 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1259 owner != UMUTEX_RB_NOTRECOV) {
1260 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1267 if (error == 0 && count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1268 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1269 umtxq_signal(&key, 1);
1272 umtx_key_release(&key);
1277 * Check if the mutex has waiters and tries to fix contention bit.
1280 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1282 struct umtx_key key;
1283 uint32_t owner, old;
1288 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1292 type = TYPE_NORMAL_UMUTEX;
1294 case UMUTEX_PRIO_INHERIT:
1295 type = TYPE_PI_UMUTEX;
1297 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1298 type = TYPE_PI_ROBUST_UMUTEX;
1300 case UMUTEX_PRIO_PROTECT:
1301 type = TYPE_PP_UMUTEX;
1303 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1304 type = TYPE_PP_ROBUST_UMUTEX;
1309 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1315 count = umtxq_count(&key);
1318 * Only repair contention bit if there is a waiter, this means the mutex
1319 * is still being referenced by userland code, otherwise don't update
1323 error = fueword32(&m->m_owner, &owner);
1326 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0) {
1327 error = casueword32(&m->m_owner, owner, &old,
1328 owner | UMUTEX_CONTESTED);
1336 error = umtxq_check_susp(td);
1340 } else if (count == 1) {
1341 error = fueword32(&m->m_owner, &owner);
1344 while (error == 0 && (owner & ~UMUTEX_CONTESTED) != 0 &&
1345 (owner & UMUTEX_CONTESTED) == 0) {
1346 error = casueword32(&m->m_owner, owner, &old,
1347 owner | UMUTEX_CONTESTED);
1355 error = umtxq_check_susp(td);
1361 if (error == EFAULT) {
1362 umtxq_signal(&key, INT_MAX);
1363 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1364 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1365 umtxq_signal(&key, 1);
1368 umtx_key_release(&key);
1372 static inline struct umtx_pi *
1373 umtx_pi_alloc(int flags)
1377 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1378 TAILQ_INIT(&pi->pi_blocked);
1379 atomic_add_int(&umtx_pi_allocated, 1);
1384 umtx_pi_free(struct umtx_pi *pi)
1386 uma_zfree(umtx_pi_zone, pi);
1387 atomic_add_int(&umtx_pi_allocated, -1);
1391 * Adjust the thread's position on a pi_state after its priority has been
1395 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1397 struct umtx_q *uq, *uq1, *uq2;
1400 mtx_assert(&umtx_lock, MA_OWNED);
1407 * Check if the thread needs to be moved on the blocked chain.
1408 * It needs to be moved if either its priority is lower than
1409 * the previous thread or higher than the next thread.
1411 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1412 uq2 = TAILQ_NEXT(uq, uq_lockq);
1413 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1414 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1416 * Remove thread from blocked chain and determine where
1417 * it should be moved to.
1419 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1420 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1421 td1 = uq1->uq_thread;
1422 MPASS(td1->td_proc->p_magic == P_MAGIC);
1423 if (UPRI(td1) > UPRI(td))
1428 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1430 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1435 static struct umtx_pi *
1436 umtx_pi_next(struct umtx_pi *pi)
1438 struct umtx_q *uq_owner;
1440 if (pi->pi_owner == NULL)
1442 uq_owner = pi->pi_owner->td_umtxq;
1443 if (uq_owner == NULL)
1445 return (uq_owner->uq_pi_blocked);
1449 * Floyd's Cycle-Finding Algorithm.
1452 umtx_pi_check_loop(struct umtx_pi *pi)
1454 struct umtx_pi *pi1; /* fast iterator */
1456 mtx_assert(&umtx_lock, MA_OWNED);
1461 pi = umtx_pi_next(pi);
1464 pi1 = umtx_pi_next(pi1);
1467 pi1 = umtx_pi_next(pi1);
1477 * Propagate priority when a thread is blocked on POSIX
1481 umtx_propagate_priority(struct thread *td)
1487 mtx_assert(&umtx_lock, MA_OWNED);
1490 pi = uq->uq_pi_blocked;
1493 if (umtx_pi_check_loop(pi))
1498 if (td == NULL || td == curthread)
1501 MPASS(td->td_proc != NULL);
1502 MPASS(td->td_proc->p_magic == P_MAGIC);
1505 if (td->td_lend_user_pri > pri)
1506 sched_lend_user_prio(td, pri);
1514 * Pick up the lock that td is blocked on.
1517 pi = uq->uq_pi_blocked;
1520 /* Resort td on the list if needed. */
1521 umtx_pi_adjust_thread(pi, td);
1526 * Unpropagate priority for a PI mutex when a thread blocked on
1527 * it is interrupted by signal or resumed by others.
1530 umtx_repropagate_priority(struct umtx_pi *pi)
1532 struct umtx_q *uq, *uq_owner;
1533 struct umtx_pi *pi2;
1536 mtx_assert(&umtx_lock, MA_OWNED);
1538 if (umtx_pi_check_loop(pi))
1540 while (pi != NULL && pi->pi_owner != NULL) {
1542 uq_owner = pi->pi_owner->td_umtxq;
1544 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1545 uq = TAILQ_FIRST(&pi2->pi_blocked);
1547 if (pri > UPRI(uq->uq_thread))
1548 pri = UPRI(uq->uq_thread);
1552 if (pri > uq_owner->uq_inherited_pri)
1553 pri = uq_owner->uq_inherited_pri;
1554 thread_lock(pi->pi_owner);
1555 sched_lend_user_prio(pi->pi_owner, pri);
1556 thread_unlock(pi->pi_owner);
1557 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1558 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1563 * Insert a PI mutex into owned list.
1566 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1568 struct umtx_q *uq_owner;
1570 uq_owner = owner->td_umtxq;
1571 mtx_assert(&umtx_lock, MA_OWNED);
1572 MPASS(pi->pi_owner == NULL);
1573 pi->pi_owner = owner;
1574 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1579 * Disown a PI mutex, and remove it from the owned list.
1582 umtx_pi_disown(struct umtx_pi *pi)
1585 mtx_assert(&umtx_lock, MA_OWNED);
1586 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1587 pi->pi_owner = NULL;
1591 * Claim ownership of a PI mutex.
1594 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1599 mtx_lock(&umtx_lock);
1600 if (pi->pi_owner == owner) {
1601 mtx_unlock(&umtx_lock);
1605 if (pi->pi_owner != NULL) {
1607 * userland may have already messed the mutex, sigh.
1609 mtx_unlock(&umtx_lock);
1612 umtx_pi_setowner(pi, owner);
1613 uq = TAILQ_FIRST(&pi->pi_blocked);
1615 pri = UPRI(uq->uq_thread);
1617 if (pri < UPRI(owner))
1618 sched_lend_user_prio(owner, pri);
1619 thread_unlock(owner);
1621 mtx_unlock(&umtx_lock);
1626 * Adjust a thread's order position in its blocked PI mutex,
1627 * this may result new priority propagating process.
1630 umtx_pi_adjust(struct thread *td, u_char oldpri)
1636 mtx_lock(&umtx_lock);
1638 * Pick up the lock that td is blocked on.
1640 pi = uq->uq_pi_blocked;
1642 umtx_pi_adjust_thread(pi, td);
1643 umtx_repropagate_priority(pi);
1645 mtx_unlock(&umtx_lock);
1649 * Sleep on a PI mutex.
1652 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
1653 const char *wmesg, struct abs_timeout *timo, bool shared)
1655 struct thread *td, *td1;
1659 struct umtxq_chain *uc;
1661 uc = umtxq_getchain(&pi->pi_key);
1665 KASSERT(td == curthread, ("inconsistent uq_thread"));
1666 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
1667 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1669 mtx_lock(&umtx_lock);
1670 if (pi->pi_owner == NULL) {
1671 mtx_unlock(&umtx_lock);
1672 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
1673 mtx_lock(&umtx_lock);
1675 if (pi->pi_owner == NULL)
1676 umtx_pi_setowner(pi, td1);
1677 PROC_UNLOCK(td1->td_proc);
1681 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1682 pri = UPRI(uq1->uq_thread);
1688 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1690 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1692 uq->uq_pi_blocked = pi;
1694 td->td_flags |= TDF_UPIBLOCKED;
1696 umtx_propagate_priority(td);
1697 mtx_unlock(&umtx_lock);
1698 umtxq_unbusy(&uq->uq_key);
1700 error = umtxq_sleep(uq, wmesg, timo);
1703 mtx_lock(&umtx_lock);
1704 uq->uq_pi_blocked = NULL;
1706 td->td_flags &= ~TDF_UPIBLOCKED;
1708 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1709 umtx_repropagate_priority(pi);
1710 mtx_unlock(&umtx_lock);
1711 umtxq_unlock(&uq->uq_key);
1717 * Add reference count for a PI mutex.
1720 umtx_pi_ref(struct umtx_pi *pi)
1723 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
1728 * Decrease reference count for a PI mutex, if the counter
1729 * is decreased to zero, its memory space is freed.
1732 umtx_pi_unref(struct umtx_pi *pi)
1734 struct umtxq_chain *uc;
1736 uc = umtxq_getchain(&pi->pi_key);
1737 UMTXQ_LOCKED_ASSERT(uc);
1738 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1739 if (--pi->pi_refcount == 0) {
1740 mtx_lock(&umtx_lock);
1741 if (pi->pi_owner != NULL)
1743 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1744 ("blocked queue not empty"));
1745 mtx_unlock(&umtx_lock);
1746 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1752 * Find a PI mutex in hash table.
1754 static struct umtx_pi *
1755 umtx_pi_lookup(struct umtx_key *key)
1757 struct umtxq_chain *uc;
1760 uc = umtxq_getchain(key);
1761 UMTXQ_LOCKED_ASSERT(uc);
1763 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1764 if (umtx_key_match(&pi->pi_key, key)) {
1772 * Insert a PI mutex into hash table.
1775 umtx_pi_insert(struct umtx_pi *pi)
1777 struct umtxq_chain *uc;
1779 uc = umtxq_getchain(&pi->pi_key);
1780 UMTXQ_LOCKED_ASSERT(uc);
1781 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1788 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1789 struct _umtx_time *timeout, int try)
1791 struct abs_timeout timo;
1793 struct umtx_pi *pi, *new_pi;
1794 uint32_t id, old_owner, owner, old;
1800 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
1801 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
1805 if (timeout != NULL)
1806 abs_timeout_init2(&timo, timeout);
1808 umtxq_lock(&uq->uq_key);
1809 pi = umtx_pi_lookup(&uq->uq_key);
1811 new_pi = umtx_pi_alloc(M_NOWAIT);
1812 if (new_pi == NULL) {
1813 umtxq_unlock(&uq->uq_key);
1814 new_pi = umtx_pi_alloc(M_WAITOK);
1815 umtxq_lock(&uq->uq_key);
1816 pi = umtx_pi_lookup(&uq->uq_key);
1818 umtx_pi_free(new_pi);
1822 if (new_pi != NULL) {
1823 new_pi->pi_key = uq->uq_key;
1824 umtx_pi_insert(new_pi);
1829 umtxq_unlock(&uq->uq_key);
1832 * Care must be exercised when dealing with umtx structure. It
1833 * can fault on any access.
1837 * Try the uncontested case. This should be done in userland.
1839 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
1840 /* The address was invalid. */
1846 /* The acquire succeeded. */
1847 if (owner == UMUTEX_UNOWNED) {
1852 if (owner == UMUTEX_RB_NOTRECOV) {
1853 error = ENOTRECOVERABLE;
1857 /* If no one owns it but it is contested try to acquire it. */
1858 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
1860 rv = casueword32(&m->m_owner, owner, &owner,
1861 id | UMUTEX_CONTESTED);
1862 /* The address was invalid. */
1868 if (owner == old_owner) {
1869 umtxq_lock(&uq->uq_key);
1870 umtxq_busy(&uq->uq_key);
1871 error = umtx_pi_claim(pi, td);
1872 umtxq_unbusy(&uq->uq_key);
1873 umtxq_unlock(&uq->uq_key);
1876 * Since we're going to return an
1877 * error, restore the m_owner to its
1878 * previous, unowned state to avoid
1879 * compounding the problem.
1881 (void)casuword32(&m->m_owner,
1882 id | UMUTEX_CONTESTED,
1886 old_owner == UMUTEX_RB_OWNERDEAD)
1891 error = umtxq_check_susp(td);
1895 /* If this failed the lock has changed, restart. */
1899 if ((owner & ~UMUTEX_CONTESTED) == id) {
1910 * If we caught a signal, we have retried and now
1916 umtxq_lock(&uq->uq_key);
1917 umtxq_busy(&uq->uq_key);
1918 umtxq_unlock(&uq->uq_key);
1921 * Set the contested bit so that a release in user space
1922 * knows to use the system call for unlock. If this fails
1923 * either some one else has acquired the lock or it has been
1926 rv = casueword32(&m->m_owner, owner, &old, owner |
1929 /* The address was invalid. */
1931 umtxq_unbusy_unlocked(&uq->uq_key);
1936 umtxq_lock(&uq->uq_key);
1938 * We set the contested bit, sleep. Otherwise the lock changed
1939 * and we need to retry or we lost a race to the thread
1940 * unlocking the umtx. Note that the UMUTEX_RB_OWNERDEAD
1941 * value for owner is impossible there.
1944 error = umtxq_sleep_pi(uq, pi,
1945 owner & ~UMUTEX_CONTESTED,
1946 "umtxpi", timeout == NULL ? NULL : &timo,
1947 (flags & USYNC_PROCESS_SHARED) != 0);
1951 umtxq_unbusy(&uq->uq_key);
1952 umtxq_unlock(&uq->uq_key);
1955 error = umtxq_check_susp(td);
1960 umtxq_lock(&uq->uq_key);
1962 umtxq_unlock(&uq->uq_key);
1964 umtx_key_release(&uq->uq_key);
1969 * Unlock a PI mutex.
1972 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1974 struct umtx_key key;
1975 struct umtx_q *uq_first, *uq_first2, *uq_me;
1976 struct umtx_pi *pi, *pi2;
1977 uint32_t id, new_owner, old, owner;
1978 int count, error, pri;
1982 * Make sure we own this mtx.
1984 error = fueword32(&m->m_owner, &owner);
1988 if ((owner & ~UMUTEX_CONTESTED) != id)
1991 new_owner = umtx_unlock_val(flags, rb);
1993 /* This should be done in userland */
1994 if ((owner & UMUTEX_CONTESTED) == 0) {
1995 error = casueword32(&m->m_owner, owner, &old, new_owner);
2003 /* We should only ever be in here for contested locks */
2004 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2005 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2011 count = umtxq_count_pi(&key, &uq_first);
2012 if (uq_first != NULL) {
2013 mtx_lock(&umtx_lock);
2014 pi = uq_first->uq_pi_blocked;
2015 KASSERT(pi != NULL, ("pi == NULL?"));
2016 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2017 mtx_unlock(&umtx_lock);
2020 umtx_key_release(&key);
2021 /* userland messed the mutex */
2024 uq_me = td->td_umtxq;
2025 if (pi->pi_owner == td)
2027 /* get highest priority thread which is still sleeping. */
2028 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2029 while (uq_first != NULL &&
2030 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2031 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2034 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2035 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2036 if (uq_first2 != NULL) {
2037 if (pri > UPRI(uq_first2->uq_thread))
2038 pri = UPRI(uq_first2->uq_thread);
2042 sched_lend_user_prio(td, pri);
2044 mtx_unlock(&umtx_lock);
2046 umtxq_signal_thread(uq_first);
2048 pi = umtx_pi_lookup(&key);
2050 * A umtx_pi can exist if a signal or timeout removed the
2051 * last waiter from the umtxq, but there is still
2052 * a thread in do_lock_pi() holding the umtx_pi.
2056 * The umtx_pi can be unowned, such as when a thread
2057 * has just entered do_lock_pi(), allocated the
2058 * umtx_pi, and unlocked the umtxq.
2059 * If the current thread owns it, it must disown it.
2061 mtx_lock(&umtx_lock);
2062 if (pi->pi_owner == td)
2064 mtx_unlock(&umtx_lock);
2070 * When unlocking the umtx, it must be marked as unowned if
2071 * there is zero or one thread only waiting for it.
2072 * Otherwise, it must be marked as contested.
2076 new_owner |= UMUTEX_CONTESTED;
2077 error = casueword32(&m->m_owner, owner, &old, new_owner);
2079 umtxq_unbusy_unlocked(&key);
2080 umtx_key_release(&key);
2092 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2093 struct _umtx_time *timeout, int try)
2095 struct abs_timeout timo;
2096 struct umtx_q *uq, *uq2;
2100 int error, pri, old_inherited_pri, su, rv;
2104 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2105 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2109 if (timeout != NULL)
2110 abs_timeout_init2(&timo, timeout);
2112 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2114 old_inherited_pri = uq->uq_inherited_pri;
2115 umtxq_lock(&uq->uq_key);
2116 umtxq_busy(&uq->uq_key);
2117 umtxq_unlock(&uq->uq_key);
2119 rv = fueword32(&m->m_ceilings[0], &ceiling);
2124 ceiling = RTP_PRIO_MAX - ceiling;
2125 if (ceiling > RTP_PRIO_MAX) {
2130 mtx_lock(&umtx_lock);
2131 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2132 mtx_unlock(&umtx_lock);
2136 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2137 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2139 if (uq->uq_inherited_pri < UPRI(td))
2140 sched_lend_user_prio(td, uq->uq_inherited_pri);
2143 mtx_unlock(&umtx_lock);
2145 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2146 id | UMUTEX_CONTESTED);
2147 /* The address was invalid. */
2153 if (owner == UMUTEX_CONTESTED) {
2156 } else if (owner == UMUTEX_RB_OWNERDEAD) {
2157 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2158 &owner, id | UMUTEX_CONTESTED);
2163 if (owner == UMUTEX_RB_OWNERDEAD) {
2164 error = EOWNERDEAD; /* success */
2168 } else if (owner == UMUTEX_RB_NOTRECOV) {
2169 error = ENOTRECOVERABLE;
2179 * If we caught a signal, we have retried and now
2185 umtxq_lock(&uq->uq_key);
2187 umtxq_unbusy(&uq->uq_key);
2188 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2191 umtxq_unlock(&uq->uq_key);
2193 mtx_lock(&umtx_lock);
2194 uq->uq_inherited_pri = old_inherited_pri;
2196 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2197 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2199 if (pri > UPRI(uq2->uq_thread))
2200 pri = UPRI(uq2->uq_thread);
2203 if (pri > uq->uq_inherited_pri)
2204 pri = uq->uq_inherited_pri;
2206 sched_lend_user_prio(td, pri);
2208 mtx_unlock(&umtx_lock);
2211 if (error != 0 && error != EOWNERDEAD) {
2212 mtx_lock(&umtx_lock);
2213 uq->uq_inherited_pri = old_inherited_pri;
2215 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2216 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2218 if (pri > UPRI(uq2->uq_thread))
2219 pri = UPRI(uq2->uq_thread);
2222 if (pri > uq->uq_inherited_pri)
2223 pri = uq->uq_inherited_pri;
2225 sched_lend_user_prio(td, pri);
2227 mtx_unlock(&umtx_lock);
2231 umtxq_unbusy_unlocked(&uq->uq_key);
2232 umtx_key_release(&uq->uq_key);
2237 * Unlock a PP mutex.
2240 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2242 struct umtx_key key;
2243 struct umtx_q *uq, *uq2;
2245 uint32_t id, owner, rceiling;
2246 int error, pri, new_inherited_pri, su;
2250 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2253 * Make sure we own this mtx.
2255 error = fueword32(&m->m_owner, &owner);
2259 if ((owner & ~UMUTEX_CONTESTED) != id)
2262 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2267 new_inherited_pri = PRI_MAX;
2269 rceiling = RTP_PRIO_MAX - rceiling;
2270 if (rceiling > RTP_PRIO_MAX)
2272 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2275 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2276 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2283 * For priority protected mutex, always set unlocked state
2284 * to UMUTEX_CONTESTED, so that userland always enters kernel
2285 * to lock the mutex, it is necessary because thread priority
2286 * has to be adjusted for such mutex.
2288 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2293 umtxq_signal(&key, 1);
2300 mtx_lock(&umtx_lock);
2302 uq->uq_inherited_pri = new_inherited_pri;
2304 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2305 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2307 if (pri > UPRI(uq2->uq_thread))
2308 pri = UPRI(uq2->uq_thread);
2311 if (pri > uq->uq_inherited_pri)
2312 pri = uq->uq_inherited_pri;
2314 sched_lend_user_prio(td, pri);
2316 mtx_unlock(&umtx_lock);
2318 umtx_key_release(&key);
2323 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2324 uint32_t *old_ceiling)
2327 uint32_t flags, id, owner, save_ceiling;
2330 error = fueword32(&m->m_flags, &flags);
2333 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2335 if (ceiling > RTP_PRIO_MAX)
2339 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2340 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2344 umtxq_lock(&uq->uq_key);
2345 umtxq_busy(&uq->uq_key);
2346 umtxq_unlock(&uq->uq_key);
2348 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2354 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2355 id | UMUTEX_CONTESTED);
2361 if (owner == UMUTEX_CONTESTED) {
2362 rv = suword32(&m->m_ceilings[0], ceiling);
2363 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2364 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2368 if ((owner & ~UMUTEX_CONTESTED) == id) {
2369 rv = suword32(&m->m_ceilings[0], ceiling);
2370 error = rv == 0 ? 0 : EFAULT;
2374 if (owner == UMUTEX_RB_OWNERDEAD) {
2377 } else if (owner == UMUTEX_RB_NOTRECOV) {
2378 error = ENOTRECOVERABLE;
2383 * If we caught a signal, we have retried and now
2390 * We set the contested bit, sleep. Otherwise the lock changed
2391 * and we need to retry or we lost a race to the thread
2392 * unlocking the umtx.
2394 umtxq_lock(&uq->uq_key);
2396 umtxq_unbusy(&uq->uq_key);
2397 error = umtxq_sleep(uq, "umtxpp", NULL);
2399 umtxq_unlock(&uq->uq_key);
2401 umtxq_lock(&uq->uq_key);
2403 umtxq_signal(&uq->uq_key, INT_MAX);
2404 umtxq_unbusy(&uq->uq_key);
2405 umtxq_unlock(&uq->uq_key);
2406 umtx_key_release(&uq->uq_key);
2407 if (error == 0 && old_ceiling != NULL) {
2408 rv = suword32(old_ceiling, save_ceiling);
2409 error = rv == 0 ? 0 : EFAULT;
2415 * Lock a userland POSIX mutex.
2418 do_lock_umutex(struct thread *td, struct umutex *m,
2419 struct _umtx_time *timeout, int mode)
2424 error = fueword32(&m->m_flags, &flags);
2428 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2430 error = do_lock_normal(td, m, flags, timeout, mode);
2432 case UMUTEX_PRIO_INHERIT:
2433 error = do_lock_pi(td, m, flags, timeout, mode);
2435 case UMUTEX_PRIO_PROTECT:
2436 error = do_lock_pp(td, m, flags, timeout, mode);
2441 if (timeout == NULL) {
2442 if (error == EINTR && mode != _UMUTEX_WAIT)
2445 /* Timed-locking is not restarted. */
2446 if (error == ERESTART)
2453 * Unlock a userland POSIX mutex.
2456 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2461 error = fueword32(&m->m_flags, &flags);
2465 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2467 return (do_unlock_normal(td, m, flags, rb));
2468 case UMUTEX_PRIO_INHERIT:
2469 return (do_unlock_pi(td, m, flags, rb));
2470 case UMUTEX_PRIO_PROTECT:
2471 return (do_unlock_pp(td, m, flags, rb));
2478 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2479 struct timespec *timeout, u_long wflags)
2481 struct abs_timeout timo;
2483 uint32_t flags, clockid, hasw;
2487 error = fueword32(&cv->c_flags, &flags);
2490 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2494 if ((wflags & CVWAIT_CLOCKID) != 0) {
2495 error = fueword32(&cv->c_clockid, &clockid);
2497 umtx_key_release(&uq->uq_key);
2500 if (clockid < CLOCK_REALTIME ||
2501 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2502 /* hmm, only HW clock id will work. */
2503 umtx_key_release(&uq->uq_key);
2507 clockid = CLOCK_REALTIME;
2510 umtxq_lock(&uq->uq_key);
2511 umtxq_busy(&uq->uq_key);
2513 umtxq_unlock(&uq->uq_key);
2516 * Set c_has_waiters to 1 before releasing user mutex, also
2517 * don't modify cache line when unnecessary.
2519 error = fueword32(&cv->c_has_waiters, &hasw);
2520 if (error == 0 && hasw == 0)
2521 suword32(&cv->c_has_waiters, 1);
2523 umtxq_unbusy_unlocked(&uq->uq_key);
2525 error = do_unlock_umutex(td, m, false);
2527 if (timeout != NULL)
2528 abs_timeout_init(&timo, clockid, (wflags & CVWAIT_ABSTIME) != 0,
2531 umtxq_lock(&uq->uq_key);
2533 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2537 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2541 * This must be timeout,interrupted by signal or
2542 * surprious wakeup, clear c_has_waiter flag when
2545 umtxq_busy(&uq->uq_key);
2546 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2547 int oldlen = uq->uq_cur_queue->length;
2550 umtxq_unlock(&uq->uq_key);
2551 suword32(&cv->c_has_waiters, 0);
2552 umtxq_lock(&uq->uq_key);
2555 umtxq_unbusy(&uq->uq_key);
2556 if (error == ERESTART)
2560 umtxq_unlock(&uq->uq_key);
2561 umtx_key_release(&uq->uq_key);
2566 * Signal a userland condition variable.
2569 do_cv_signal(struct thread *td, struct ucond *cv)
2571 struct umtx_key key;
2572 int error, cnt, nwake;
2575 error = fueword32(&cv->c_flags, &flags);
2578 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2582 cnt = umtxq_count(&key);
2583 nwake = umtxq_signal(&key, 1);
2586 error = suword32(&cv->c_has_waiters, 0);
2593 umtx_key_release(&key);
2598 do_cv_broadcast(struct thread *td, struct ucond *cv)
2600 struct umtx_key key;
2604 error = fueword32(&cv->c_flags, &flags);
2607 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2612 umtxq_signal(&key, INT_MAX);
2615 error = suword32(&cv->c_has_waiters, 0);
2619 umtxq_unbusy_unlocked(&key);
2621 umtx_key_release(&key);
2626 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
2627 struct _umtx_time *timeout)
2629 struct abs_timeout timo;
2631 uint32_t flags, wrflags;
2632 int32_t state, oldstate;
2633 int32_t blocked_readers;
2634 int error, error1, rv;
2637 error = fueword32(&rwlock->rw_flags, &flags);
2640 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2644 if (timeout != NULL)
2645 abs_timeout_init2(&timo, timeout);
2647 wrflags = URWLOCK_WRITE_OWNER;
2648 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2649 wrflags |= URWLOCK_WRITE_WAITERS;
2652 rv = fueword32(&rwlock->rw_state, &state);
2654 umtx_key_release(&uq->uq_key);
2658 /* try to lock it */
2659 while (!(state & wrflags)) {
2660 if (__predict_false(URWLOCK_READER_COUNT(state) ==
2661 URWLOCK_MAX_READERS)) {
2662 umtx_key_release(&uq->uq_key);
2665 rv = casueword32(&rwlock->rw_state, state,
2666 &oldstate, state + 1);
2668 umtx_key_release(&uq->uq_key);
2671 if (oldstate == state) {
2672 umtx_key_release(&uq->uq_key);
2675 error = umtxq_check_susp(td);
2684 /* grab monitor lock */
2685 umtxq_lock(&uq->uq_key);
2686 umtxq_busy(&uq->uq_key);
2687 umtxq_unlock(&uq->uq_key);
2690 * re-read the state, in case it changed between the try-lock above
2691 * and the check below
2693 rv = fueword32(&rwlock->rw_state, &state);
2697 /* set read contention bit */
2698 while (error == 0 && (state & wrflags) &&
2699 !(state & URWLOCK_READ_WAITERS)) {
2700 rv = casueword32(&rwlock->rw_state, state,
2701 &oldstate, state | URWLOCK_READ_WAITERS);
2706 if (oldstate == state)
2709 error = umtxq_check_susp(td);
2714 umtxq_unbusy_unlocked(&uq->uq_key);
2718 /* state is changed while setting flags, restart */
2719 if (!(state & wrflags)) {
2720 umtxq_unbusy_unlocked(&uq->uq_key);
2721 error = umtxq_check_susp(td);
2729 * Contention bit is set, before sleeping, increase
2730 * read waiter count.
2732 rv = fueword32(&rwlock->rw_blocked_readers,
2735 umtxq_unbusy_unlocked(&uq->uq_key);
2739 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2741 while (state & wrflags) {
2742 umtxq_lock(&uq->uq_key);
2744 umtxq_unbusy(&uq->uq_key);
2746 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2749 umtxq_busy(&uq->uq_key);
2751 umtxq_unlock(&uq->uq_key);
2754 rv = fueword32(&rwlock->rw_state, &state);
2761 /* decrease read waiter count, and may clear read contention bit */
2762 rv = fueword32(&rwlock->rw_blocked_readers,
2765 umtxq_unbusy_unlocked(&uq->uq_key);
2769 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2770 if (blocked_readers == 1) {
2771 rv = fueword32(&rwlock->rw_state, &state);
2773 umtxq_unbusy_unlocked(&uq->uq_key);
2778 rv = casueword32(&rwlock->rw_state, state,
2779 &oldstate, state & ~URWLOCK_READ_WAITERS);
2784 if (oldstate == state)
2787 error1 = umtxq_check_susp(td);
2796 umtxq_unbusy_unlocked(&uq->uq_key);
2800 umtx_key_release(&uq->uq_key);
2801 if (error == ERESTART)
2807 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2809 struct abs_timeout timo;
2812 int32_t state, oldstate;
2813 int32_t blocked_writers;
2814 int32_t blocked_readers;
2815 int error, error1, rv;
2818 error = fueword32(&rwlock->rw_flags, &flags);
2821 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2825 if (timeout != NULL)
2826 abs_timeout_init2(&timo, timeout);
2828 blocked_readers = 0;
2830 rv = fueword32(&rwlock->rw_state, &state);
2832 umtx_key_release(&uq->uq_key);
2835 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
2836 URWLOCK_READER_COUNT(state) == 0) {
2837 rv = casueword32(&rwlock->rw_state, state,
2838 &oldstate, state | URWLOCK_WRITE_OWNER);
2840 umtx_key_release(&uq->uq_key);
2843 if (oldstate == state) {
2844 umtx_key_release(&uq->uq_key);
2848 error = umtxq_check_susp(td);
2854 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) &&
2855 blocked_readers != 0) {
2856 umtxq_lock(&uq->uq_key);
2857 umtxq_busy(&uq->uq_key);
2858 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE);
2859 umtxq_unbusy(&uq->uq_key);
2860 umtxq_unlock(&uq->uq_key);
2866 /* grab monitor lock */
2867 umtxq_lock(&uq->uq_key);
2868 umtxq_busy(&uq->uq_key);
2869 umtxq_unlock(&uq->uq_key);
2872 * Re-read the state, in case it changed between the
2873 * try-lock above and the check below.
2875 rv = fueword32(&rwlock->rw_state, &state);
2879 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
2880 URWLOCK_READER_COUNT(state) != 0) &&
2881 (state & URWLOCK_WRITE_WAITERS) == 0) {
2882 rv = casueword32(&rwlock->rw_state, state,
2883 &oldstate, state | URWLOCK_WRITE_WAITERS);
2888 if (oldstate == state)
2891 error = umtxq_check_susp(td);
2896 umtxq_unbusy_unlocked(&uq->uq_key);
2900 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
2901 URWLOCK_READER_COUNT(state) == 0) {
2902 umtxq_unbusy_unlocked(&uq->uq_key);
2903 error = umtxq_check_susp(td);
2909 rv = fueword32(&rwlock->rw_blocked_writers,
2912 umtxq_unbusy_unlocked(&uq->uq_key);
2916 suword32(&rwlock->rw_blocked_writers, blocked_writers + 1);
2918 while ((state & URWLOCK_WRITE_OWNER) ||
2919 URWLOCK_READER_COUNT(state) != 0) {
2920 umtxq_lock(&uq->uq_key);
2921 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2922 umtxq_unbusy(&uq->uq_key);
2924 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
2927 umtxq_busy(&uq->uq_key);
2928 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2929 umtxq_unlock(&uq->uq_key);
2932 rv = fueword32(&rwlock->rw_state, &state);
2939 rv = fueword32(&rwlock->rw_blocked_writers,
2942 umtxq_unbusy_unlocked(&uq->uq_key);
2946 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
2947 if (blocked_writers == 1) {
2948 rv = fueword32(&rwlock->rw_state, &state);
2950 umtxq_unbusy_unlocked(&uq->uq_key);
2955 rv = casueword32(&rwlock->rw_state, state,
2956 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
2961 if (oldstate == state)
2964 error1 = umtxq_check_susp(td);
2966 * We are leaving the URWLOCK_WRITE_WAITERS
2967 * behind, but this should not harm the
2976 rv = fueword32(&rwlock->rw_blocked_readers,
2979 umtxq_unbusy_unlocked(&uq->uq_key);
2984 blocked_readers = 0;
2986 umtxq_unbusy_unlocked(&uq->uq_key);
2989 umtx_key_release(&uq->uq_key);
2990 if (error == ERESTART)
2996 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3000 int32_t state, oldstate;
3001 int error, rv, q, count;
3004 error = fueword32(&rwlock->rw_flags, &flags);
3007 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3011 error = fueword32(&rwlock->rw_state, &state);
3016 if (state & URWLOCK_WRITE_OWNER) {
3018 rv = casueword32(&rwlock->rw_state, state,
3019 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3024 if (oldstate != state) {
3026 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3030 error = umtxq_check_susp(td);
3036 } else if (URWLOCK_READER_COUNT(state) != 0) {
3038 rv = casueword32(&rwlock->rw_state, state,
3039 &oldstate, state - 1);
3044 if (oldstate != state) {
3046 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3050 error = umtxq_check_susp(td);
3063 if (!(flags & URWLOCK_PREFER_READER)) {
3064 if (state & URWLOCK_WRITE_WAITERS) {
3066 q = UMTX_EXCLUSIVE_QUEUE;
3067 } else if (state & URWLOCK_READ_WAITERS) {
3069 q = UMTX_SHARED_QUEUE;
3072 if (state & URWLOCK_READ_WAITERS) {
3074 q = UMTX_SHARED_QUEUE;
3075 } else if (state & URWLOCK_WRITE_WAITERS) {
3077 q = UMTX_EXCLUSIVE_QUEUE;
3082 umtxq_lock(&uq->uq_key);
3083 umtxq_busy(&uq->uq_key);
3084 umtxq_signal_queue(&uq->uq_key, count, q);
3085 umtxq_unbusy(&uq->uq_key);
3086 umtxq_unlock(&uq->uq_key);
3089 umtx_key_release(&uq->uq_key);
3093 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3095 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3097 struct abs_timeout timo;
3099 uint32_t flags, count, count1;
3103 error = fueword32(&sem->_flags, &flags);
3106 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3110 if (timeout != NULL)
3111 abs_timeout_init2(&timo, timeout);
3113 umtxq_lock(&uq->uq_key);
3114 umtxq_busy(&uq->uq_key);
3116 umtxq_unlock(&uq->uq_key);
3117 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3119 rv = fueword32(&sem->_count, &count);
3120 if (rv == -1 || count != 0) {
3121 umtxq_lock(&uq->uq_key);
3122 umtxq_unbusy(&uq->uq_key);
3124 umtxq_unlock(&uq->uq_key);
3125 umtx_key_release(&uq->uq_key);
3126 return (rv == -1 ? EFAULT : 0);
3128 umtxq_lock(&uq->uq_key);
3129 umtxq_unbusy(&uq->uq_key);
3131 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3133 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3137 /* A relative timeout cannot be restarted. */
3138 if (error == ERESTART && timeout != NULL &&
3139 (timeout->_flags & UMTX_ABSTIME) == 0)
3142 umtxq_unlock(&uq->uq_key);
3143 umtx_key_release(&uq->uq_key);
3148 * Signal a userland semaphore.
3151 do_sem_wake(struct thread *td, struct _usem *sem)
3153 struct umtx_key key;
3157 error = fueword32(&sem->_flags, &flags);
3160 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3164 cnt = umtxq_count(&key);
3167 * Check if count is greater than 0, this means the memory is
3168 * still being referenced by user code, so we can safely
3169 * update _has_waiters flag.
3173 error = suword32(&sem->_has_waiters, 0);
3178 umtxq_signal(&key, 1);
3182 umtx_key_release(&key);
3188 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3190 struct abs_timeout timo;
3192 uint32_t count, flags;
3196 flags = fuword32(&sem->_flags);
3197 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3201 if (timeout != NULL)
3202 abs_timeout_init2(&timo, timeout);
3204 umtxq_lock(&uq->uq_key);
3205 umtxq_busy(&uq->uq_key);
3207 umtxq_unlock(&uq->uq_key);
3208 rv = fueword32(&sem->_count, &count);
3210 umtxq_lock(&uq->uq_key);
3211 umtxq_unbusy(&uq->uq_key);
3213 umtxq_unlock(&uq->uq_key);
3214 umtx_key_release(&uq->uq_key);
3218 if (USEM_COUNT(count) != 0) {
3219 umtxq_lock(&uq->uq_key);
3220 umtxq_unbusy(&uq->uq_key);
3222 umtxq_unlock(&uq->uq_key);
3223 umtx_key_release(&uq->uq_key);
3226 if (count == USEM_HAS_WAITERS)
3228 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3230 umtxq_lock(&uq->uq_key);
3231 umtxq_unbusy(&uq->uq_key);
3233 umtxq_unlock(&uq->uq_key);
3234 umtx_key_release(&uq->uq_key);
3240 umtxq_lock(&uq->uq_key);
3241 umtxq_unbusy(&uq->uq_key);
3243 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3245 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3249 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3250 /* A relative timeout cannot be restarted. */
3251 if (error == ERESTART)
3253 if (error == EINTR) {
3254 abs_timeout_update(&timo);
3255 timespecsub(&timo.end, &timo.cur,
3256 &timeout->_timeout);
3260 umtxq_unlock(&uq->uq_key);
3261 umtx_key_release(&uq->uq_key);
3266 * Signal a userland semaphore.
3269 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3271 struct umtx_key key;
3273 uint32_t count, flags;
3275 rv = fueword32(&sem->_flags, &flags);
3278 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3282 cnt = umtxq_count(&key);
3285 * If this was the last sleeping thread, clear the waiters
3290 rv = fueword32(&sem->_count, &count);
3291 while (rv != -1 && count & USEM_HAS_WAITERS)
3292 rv = casueword32(&sem->_count, count, &count,
3293 count & ~USEM_HAS_WAITERS);
3299 umtxq_signal(&key, 1);
3303 umtx_key_release(&key);
3308 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3312 error = copyin(addr, tsp, sizeof(struct timespec));
3314 if (tsp->tv_sec < 0 ||
3315 tsp->tv_nsec >= 1000000000 ||
3323 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3327 if (size <= sizeof(struct timespec)) {
3328 tp->_clockid = CLOCK_REALTIME;
3330 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3332 error = copyin(addr, tp, sizeof(struct _umtx_time));
3335 if (tp->_timeout.tv_sec < 0 ||
3336 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3342 __umtx_op_unimpl(struct thread *td, struct _umtx_op_args *uap)
3345 return (EOPNOTSUPP);
3349 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3351 struct _umtx_time timeout, *tm_p;
3354 if (uap->uaddr2 == NULL)
3357 error = umtx_copyin_umtx_time(
3358 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3363 return (do_wait(td, uap->obj, uap->val, tm_p, 0, 0));
3367 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3369 struct _umtx_time timeout, *tm_p;
3372 if (uap->uaddr2 == NULL)
3375 error = umtx_copyin_umtx_time(
3376 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3381 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3385 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3387 struct _umtx_time *tm_p, timeout;
3390 if (uap->uaddr2 == NULL)
3393 error = umtx_copyin_umtx_time(
3394 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3399 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3403 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3406 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3409 #define BATCH_SIZE 128
3411 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3413 char *uaddrs[BATCH_SIZE], **upp;
3414 int count, error, i, pos, tocopy;
3416 upp = (char **)uap->obj;
3418 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3420 tocopy = MIN(count, BATCH_SIZE);
3421 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3424 for (i = 0; i < tocopy; ++i)
3425 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3432 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3435 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3439 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3441 struct _umtx_time *tm_p, timeout;
3444 /* Allow a null timespec (wait forever). */
3445 if (uap->uaddr2 == NULL)
3448 error = umtx_copyin_umtx_time(
3449 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3454 return (do_lock_umutex(td, uap->obj, tm_p, 0));
3458 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3461 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
3465 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3467 struct _umtx_time *tm_p, timeout;
3470 /* Allow a null timespec (wait forever). */
3471 if (uap->uaddr2 == NULL)
3474 error = umtx_copyin_umtx_time(
3475 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3480 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
3484 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3487 return (do_wake_umutex(td, uap->obj));
3491 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3494 return (do_unlock_umutex(td, uap->obj, false));
3498 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3501 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
3505 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3507 struct timespec *ts, timeout;
3510 /* Allow a null timespec (wait forever). */
3511 if (uap->uaddr2 == NULL)
3514 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3519 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3523 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3526 return (do_cv_signal(td, uap->obj));
3530 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3533 return (do_cv_broadcast(td, uap->obj));
3537 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3539 struct _umtx_time timeout;
3542 /* Allow a null timespec (wait forever). */
3543 if (uap->uaddr2 == NULL) {
3544 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3546 error = umtx_copyin_umtx_time(uap->uaddr2,
3547 (size_t)uap->uaddr1, &timeout);
3550 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3556 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3558 struct _umtx_time timeout;
3561 /* Allow a null timespec (wait forever). */
3562 if (uap->uaddr2 == NULL) {
3563 error = do_rw_wrlock(td, uap->obj, 0);
3565 error = umtx_copyin_umtx_time(uap->uaddr2,
3566 (size_t)uap->uaddr1, &timeout);
3570 error = do_rw_wrlock(td, uap->obj, &timeout);
3576 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3579 return (do_rw_unlock(td, uap->obj));
3582 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3584 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3586 struct _umtx_time *tm_p, timeout;
3589 /* Allow a null timespec (wait forever). */
3590 if (uap->uaddr2 == NULL)
3593 error = umtx_copyin_umtx_time(
3594 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3599 return (do_sem_wait(td, uap->obj, tm_p));
3603 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3606 return (do_sem_wake(td, uap->obj));
3611 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3614 return (do_wake2_umutex(td, uap->obj, uap->val));
3618 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap)
3620 struct _umtx_time *tm_p, timeout;
3624 /* Allow a null timespec (wait forever). */
3625 if (uap->uaddr2 == NULL) {
3629 uasize = (size_t)uap->uaddr1;
3630 error = umtx_copyin_umtx_time(uap->uaddr2, uasize, &timeout);
3635 error = do_sem2_wait(td, uap->obj, tm_p);
3636 if (error == EINTR && uap->uaddr2 != NULL &&
3637 (timeout._flags & UMTX_ABSTIME) == 0 &&
3638 uasize >= sizeof(struct _umtx_time) + sizeof(struct timespec)) {
3639 error = copyout(&timeout._timeout,
3640 (struct _umtx_time *)uap->uaddr2 + 1,
3641 sizeof(struct timespec));
3651 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap)
3654 return (do_sem2_wake(td, uap->obj));
3657 #define USHM_OBJ_UMTX(o) \
3658 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
3660 #define USHMF_REG_LINKED 0x0001
3661 #define USHMF_OBJ_LINKED 0x0002
3662 struct umtx_shm_reg {
3663 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
3664 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
3665 struct umtx_key ushm_key;
3666 struct ucred *ushm_cred;
3667 struct shmfd *ushm_obj;
3672 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
3673 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
3675 static uma_zone_t umtx_shm_reg_zone;
3676 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
3677 static struct mtx umtx_shm_lock;
3678 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
3679 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
3681 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
3684 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
3686 struct umtx_shm_reg_head d;
3687 struct umtx_shm_reg *reg, *reg1;
3690 mtx_lock(&umtx_shm_lock);
3691 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
3692 mtx_unlock(&umtx_shm_lock);
3693 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
3694 TAILQ_REMOVE(&d, reg, ushm_reg_link);
3695 umtx_shm_free_reg(reg);
3699 static struct task umtx_shm_reg_delfree_task =
3700 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
3702 static struct umtx_shm_reg *
3703 umtx_shm_find_reg_locked(const struct umtx_key *key)
3705 struct umtx_shm_reg *reg;
3706 struct umtx_shm_reg_head *reg_head;
3708 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
3709 mtx_assert(&umtx_shm_lock, MA_OWNED);
3710 reg_head = &umtx_shm_registry[key->hash];
3711 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
3712 KASSERT(reg->ushm_key.shared,
3713 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
3714 if (reg->ushm_key.info.shared.object ==
3715 key->info.shared.object &&
3716 reg->ushm_key.info.shared.offset ==
3717 key->info.shared.offset) {
3718 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
3719 KASSERT(reg->ushm_refcnt > 0,
3720 ("reg %p refcnt 0 onlist", reg));
3721 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
3722 ("reg %p not linked", reg));
3730 static struct umtx_shm_reg *
3731 umtx_shm_find_reg(const struct umtx_key *key)
3733 struct umtx_shm_reg *reg;
3735 mtx_lock(&umtx_shm_lock);
3736 reg = umtx_shm_find_reg_locked(key);
3737 mtx_unlock(&umtx_shm_lock);
3742 umtx_shm_free_reg(struct umtx_shm_reg *reg)
3745 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
3746 crfree(reg->ushm_cred);
3747 shm_drop(reg->ushm_obj);
3748 uma_zfree(umtx_shm_reg_zone, reg);
3752 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
3756 mtx_assert(&umtx_shm_lock, MA_OWNED);
3757 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
3759 res = reg->ushm_refcnt == 0;
3761 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
3762 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
3763 reg, ushm_reg_link);
3764 reg->ushm_flags &= ~USHMF_REG_LINKED;
3766 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
3767 LIST_REMOVE(reg, ushm_obj_link);
3768 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
3775 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
3781 object = reg->ushm_obj->shm_object;
3782 VM_OBJECT_WLOCK(object);
3783 object->flags |= OBJ_UMTXDEAD;
3784 VM_OBJECT_WUNLOCK(object);
3786 mtx_lock(&umtx_shm_lock);
3787 dofree = umtx_shm_unref_reg_locked(reg, force);
3788 mtx_unlock(&umtx_shm_lock);
3790 umtx_shm_free_reg(reg);
3794 umtx_shm_object_init(vm_object_t object)
3797 LIST_INIT(USHM_OBJ_UMTX(object));
3801 umtx_shm_object_terminated(vm_object_t object)
3803 struct umtx_shm_reg *reg, *reg1;
3807 mtx_lock(&umtx_shm_lock);
3808 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
3809 if (umtx_shm_unref_reg_locked(reg, true)) {
3810 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
3815 mtx_unlock(&umtx_shm_lock);
3817 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
3821 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
3822 struct umtx_shm_reg **res)
3824 struct umtx_shm_reg *reg, *reg1;
3828 reg = umtx_shm_find_reg(key);
3833 cred = td->td_ucred;
3834 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
3836 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
3837 reg->ushm_refcnt = 1;
3838 bcopy(key, ®->ushm_key, sizeof(*key));
3839 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR);
3840 reg->ushm_cred = crhold(cred);
3841 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
3843 umtx_shm_free_reg(reg);
3846 mtx_lock(&umtx_shm_lock);
3847 reg1 = umtx_shm_find_reg_locked(key);
3849 mtx_unlock(&umtx_shm_lock);
3850 umtx_shm_free_reg(reg);
3855 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
3856 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
3858 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
3859 mtx_unlock(&umtx_shm_lock);
3865 umtx_shm_alive(struct thread *td, void *addr)
3868 vm_map_entry_t entry;
3875 map = &td->td_proc->p_vmspace->vm_map;
3876 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
3877 &object, &pindex, &prot, &wired);
3878 if (res != KERN_SUCCESS)
3883 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
3884 vm_map_lookup_done(map, entry);
3893 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
3894 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
3895 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
3896 for (i = 0; i < nitems(umtx_shm_registry); i++)
3897 TAILQ_INIT(&umtx_shm_registry[i]);
3901 umtx_shm(struct thread *td, void *addr, u_int flags)
3903 struct umtx_key key;
3904 struct umtx_shm_reg *reg;
3908 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
3909 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
3911 if ((flags & UMTX_SHM_ALIVE) != 0)
3912 return (umtx_shm_alive(td, addr));
3913 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
3916 KASSERT(key.shared == 1, ("non-shared key"));
3917 if ((flags & UMTX_SHM_CREAT) != 0) {
3918 error = umtx_shm_create_reg(td, &key, ®);
3920 reg = umtx_shm_find_reg(&key);
3924 umtx_key_release(&key);
3927 KASSERT(reg != NULL, ("no reg"));
3928 if ((flags & UMTX_SHM_DESTROY) != 0) {
3929 umtx_shm_unref_reg(reg, true);
3933 error = mac_posixshm_check_open(td->td_ucred,
3934 reg->ushm_obj, FFLAGS(O_RDWR));
3937 error = shm_access(reg->ushm_obj, td->td_ucred,
3941 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
3943 shm_hold(reg->ushm_obj);
3944 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
3946 td->td_retval[0] = fd;
3950 umtx_shm_unref_reg(reg, false);
3955 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap)
3958 return (umtx_shm(td, uap->uaddr1, uap->val));
3962 umtx_robust_lists(struct thread *td, struct umtx_robust_lists_params *rbp)
3965 td->td_rb_list = rbp->robust_list_offset;
3966 td->td_rbp_list = rbp->robust_priv_list_offset;
3967 td->td_rb_inact = rbp->robust_inact_offset;
3972 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap)
3974 struct umtx_robust_lists_params rb;
3977 if (uap->val > sizeof(rb))
3979 bzero(&rb, sizeof(rb));
3980 error = copyin(uap->uaddr1, &rb, uap->val);
3983 return (umtx_robust_lists(td, &rb));
3986 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
3988 static const _umtx_op_func op_table[] = {
3989 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
3990 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
3991 [UMTX_OP_WAIT] = __umtx_op_wait,
3992 [UMTX_OP_WAKE] = __umtx_op_wake,
3993 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
3994 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
3995 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
3996 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
3997 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
3998 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
3999 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4000 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
4001 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
4002 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
4003 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4004 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4005 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4006 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4007 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4008 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4009 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4010 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4012 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4013 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4015 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4016 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4017 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4018 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4019 [UMTX_OP_SHM] = __umtx_op_shm,
4020 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4024 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4027 if ((unsigned)uap->op < nitems(op_table))
4028 return (*op_table[uap->op])(td, uap);
4032 #ifdef COMPAT_FREEBSD32
4039 struct umtx_time32 {
4040 struct timespec32 timeout;
4046 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
4048 struct timespec32 ts32;
4051 error = copyin(addr, &ts32, sizeof(struct timespec32));
4053 if (ts32.tv_sec < 0 ||
4054 ts32.tv_nsec >= 1000000000 ||
4058 tsp->tv_sec = ts32.tv_sec;
4059 tsp->tv_nsec = ts32.tv_nsec;
4066 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
4068 struct umtx_time32 t32;
4071 t32.clockid = CLOCK_REALTIME;
4073 if (size <= sizeof(struct timespec32))
4074 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
4076 error = copyin(addr, &t32, sizeof(struct umtx_time32));
4079 if (t32.timeout.tv_sec < 0 ||
4080 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
4082 tp->_timeout.tv_sec = t32.timeout.tv_sec;
4083 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
4084 tp->_flags = t32.flags;
4085 tp->_clockid = t32.clockid;
4090 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4092 struct _umtx_time *tm_p, timeout;
4095 if (uap->uaddr2 == NULL)
4098 error = umtx_copyin_umtx_time32(uap->uaddr2,
4099 (size_t)uap->uaddr1, &timeout);
4104 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
4108 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4110 struct _umtx_time *tm_p, timeout;
4113 /* Allow a null timespec (wait forever). */
4114 if (uap->uaddr2 == NULL)
4117 error = umtx_copyin_umtx_time32(uap->uaddr2,
4118 (size_t)uap->uaddr1, &timeout);
4123 return (do_lock_umutex(td, uap->obj, tm_p, 0));
4127 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4129 struct _umtx_time *tm_p, timeout;
4132 /* Allow a null timespec (wait forever). */
4133 if (uap->uaddr2 == NULL)
4136 error = umtx_copyin_umtx_time32(uap->uaddr2,
4137 (size_t)uap->uaddr1, &timeout);
4142 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
4146 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4148 struct timespec *ts, timeout;
4151 /* Allow a null timespec (wait forever). */
4152 if (uap->uaddr2 == NULL)
4155 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
4160 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
4164 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4166 struct _umtx_time timeout;
4169 /* Allow a null timespec (wait forever). */
4170 if (uap->uaddr2 == NULL) {
4171 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4173 error = umtx_copyin_umtx_time32(uap->uaddr2,
4174 (size_t)uap->uaddr1, &timeout);
4177 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4183 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4185 struct _umtx_time timeout;
4188 /* Allow a null timespec (wait forever). */
4189 if (uap->uaddr2 == NULL) {
4190 error = do_rw_wrlock(td, uap->obj, 0);
4192 error = umtx_copyin_umtx_time32(uap->uaddr2,
4193 (size_t)uap->uaddr1, &timeout);
4196 error = do_rw_wrlock(td, uap->obj, &timeout);
4202 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
4204 struct _umtx_time *tm_p, timeout;
4207 if (uap->uaddr2 == NULL)
4210 error = umtx_copyin_umtx_time32(
4211 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
4216 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
4219 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4221 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4223 struct _umtx_time *tm_p, timeout;
4226 /* Allow a null timespec (wait forever). */
4227 if (uap->uaddr2 == NULL)
4230 error = umtx_copyin_umtx_time32(uap->uaddr2,
4231 (size_t)uap->uaddr1, &timeout);
4236 return (do_sem_wait(td, uap->obj, tm_p));
4241 __umtx_op_sem2_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4243 struct _umtx_time *tm_p, timeout;
4247 /* Allow a null timespec (wait forever). */
4248 if (uap->uaddr2 == NULL) {
4252 uasize = (size_t)uap->uaddr1;
4253 error = umtx_copyin_umtx_time32(uap->uaddr2, uasize, &timeout);
4258 error = do_sem2_wait(td, uap->obj, tm_p);
4259 if (error == EINTR && uap->uaddr2 != NULL &&
4260 (timeout._flags & UMTX_ABSTIME) == 0 &&
4261 uasize >= sizeof(struct umtx_time32) + sizeof(struct timespec32)) {
4262 struct timespec32 remain32 = {
4263 .tv_sec = timeout._timeout.tv_sec,
4264 .tv_nsec = timeout._timeout.tv_nsec
4266 error = copyout(&remain32,
4267 (struct umtx_time32 *)uap->uaddr2 + 1,
4268 sizeof(struct timespec32));
4278 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
4280 uint32_t uaddrs[BATCH_SIZE], **upp;
4281 int count, error, i, pos, tocopy;
4283 upp = (uint32_t **)uap->obj;
4285 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
4287 tocopy = MIN(count, BATCH_SIZE);
4288 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
4291 for (i = 0; i < tocopy; ++i)
4292 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
4299 struct umtx_robust_lists_params_compat32 {
4300 uint32_t robust_list_offset;
4301 uint32_t robust_priv_list_offset;
4302 uint32_t robust_inact_offset;
4306 __umtx_op_robust_lists_compat32(struct thread *td, struct _umtx_op_args *uap)
4308 struct umtx_robust_lists_params rb;
4309 struct umtx_robust_lists_params_compat32 rb32;
4312 if (uap->val > sizeof(rb32))
4314 bzero(&rb, sizeof(rb));
4315 bzero(&rb32, sizeof(rb32));
4316 error = copyin(uap->uaddr1, &rb32, uap->val);
4319 rb.robust_list_offset = rb32.robust_list_offset;
4320 rb.robust_priv_list_offset = rb32.robust_priv_list_offset;
4321 rb.robust_inact_offset = rb32.robust_inact_offset;
4322 return (umtx_robust_lists(td, &rb));
4325 static const _umtx_op_func op_table_compat32[] = {
4326 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
4327 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
4328 [UMTX_OP_WAIT] = __umtx_op_wait_compat32,
4329 [UMTX_OP_WAKE] = __umtx_op_wake,
4330 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4331 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex_compat32,
4332 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4333 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4334 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait_compat32,
4335 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4336 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4337 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_compat32,
4338 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock_compat32,
4339 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock_compat32,
4340 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4341 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private_compat32,
4342 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4343 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex_compat32,
4344 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4345 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4346 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait_compat32,
4347 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4349 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4350 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4352 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private32,
4353 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4354 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait_compat32,
4355 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4356 [UMTX_OP_SHM] = __umtx_op_shm,
4357 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists_compat32,
4361 freebsd32__umtx_op(struct thread *td, struct freebsd32__umtx_op_args *uap)
4364 if ((unsigned)uap->op < nitems(op_table_compat32)) {
4365 return (*op_table_compat32[uap->op])(td,
4366 (struct _umtx_op_args *)uap);
4373 umtx_thread_init(struct thread *td)
4376 td->td_umtxq = umtxq_alloc();
4377 td->td_umtxq->uq_thread = td;
4381 umtx_thread_fini(struct thread *td)
4384 umtxq_free(td->td_umtxq);
4388 * It will be called when new thread is created, e.g fork().
4391 umtx_thread_alloc(struct thread *td)
4396 uq->uq_inherited_pri = PRI_MAX;
4398 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4399 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4400 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4401 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4407 * Clear robust lists for all process' threads, not delaying the
4408 * cleanup to thread_exit hook, since the relevant address space is
4409 * destroyed right now.
4412 umtx_exec_hook(void *arg __unused, struct proc *p,
4413 struct image_params *imgp __unused)
4417 KASSERT(p == curproc, ("need curproc"));
4419 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4420 (p->p_flag & P_STOPPED_SINGLE) != 0,
4421 ("curproc must be single-threaded"));
4422 FOREACH_THREAD_IN_PROC(p, td) {
4423 KASSERT(td == curthread ||
4424 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4425 ("running thread %p %p", p, td));
4427 umtx_thread_cleanup(td);
4429 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4435 * thread_exit() hook.
4438 umtx_thread_exit(struct thread *td)
4441 umtx_thread_cleanup(td);
4445 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res)
4448 #ifdef COMPAT_FREEBSD32
4453 #ifdef COMPAT_FREEBSD32
4454 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4455 error = fueword32((void *)ptr, &res32);
4461 error = fueword((void *)ptr, &res1);
4471 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list)
4473 #ifdef COMPAT_FREEBSD32
4474 struct umutex32 m32;
4476 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4477 memcpy(&m32, m, sizeof(m32));
4478 *rb_list = m32.m_rb_lnk;
4481 *rb_list = m->m_rb_lnk;
4485 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact)
4490 KASSERT(td->td_proc == curproc, ("need current vmspace"));
4491 error = copyin((void *)rbp, &m, sizeof(m));
4494 if (rb_list != NULL)
4495 umtx_read_rb_list(td, &m, rb_list);
4496 if ((m.m_flags & UMUTEX_ROBUST) == 0)
4498 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
4499 /* inact is cleared after unlock, allow the inconsistency */
4500 return (inact ? 0 : EINVAL);
4501 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
4505 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
4514 error = umtx_read_uptr(td, rb_list, &rbp);
4515 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
4516 if (rbp == *rb_inact) {
4521 error = umtx_handle_rb(td, rbp, &rbp, inact);
4523 if (i == umtx_max_rb && umtx_verbose_rb) {
4524 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
4525 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
4527 if (error != 0 && umtx_verbose_rb) {
4528 uprintf("comm %s pid %d: handling %srb error %d\n",
4529 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
4534 * Clean up umtx data.
4537 umtx_thread_cleanup(struct thread *td)
4544 * Disown pi mutexes.
4548 mtx_lock(&umtx_lock);
4549 uq->uq_inherited_pri = PRI_MAX;
4550 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
4551 pi->pi_owner = NULL;
4552 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
4554 mtx_unlock(&umtx_lock);
4556 sched_lend_user_prio(td, PRI_MAX);
4561 * Handle terminated robust mutexes. Must be done after
4562 * robust pi disown, otherwise unlock could see unowned
4565 rb_inact = td->td_rb_inact;
4567 (void)umtx_read_uptr(td, rb_inact, &rb_inact);
4568 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "");
4569 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ");
4571 (void)umtx_handle_rb(td, rb_inact, NULL, true);