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 timo->end = timo->cur;
776 timespecadd(&timo->end, timeout);
778 timo->end = *timeout;
779 timo->is_abs_real = clockid == CLOCK_REALTIME ||
780 clockid == CLOCK_REALTIME_FAST ||
781 clockid == CLOCK_REALTIME_PRECISE;
783 * If is_abs_real, umtxq_sleep will read the clock
784 * after setting td_rtcgen; otherwise, read it here.
786 if (!timo->is_abs_real) {
787 abs_timeout_update(timo);
793 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
796 abs_timeout_init(timo, umtxtime->_clockid,
797 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
801 abs_timeout_update(struct abs_timeout *timo)
804 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
808 abs_timeout_gethz(struct abs_timeout *timo)
812 if (timespeccmp(&timo->end, &timo->cur, <=))
815 timespecsub(&tts, &timo->cur);
816 return (tstohz(&tts));
820 umtx_unlock_val(uint32_t flags, bool rb)
824 return (UMUTEX_RB_OWNERDEAD);
825 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
826 return (UMUTEX_RB_NOTRECOV);
828 return (UMUTEX_UNOWNED);
833 * Put thread into sleep state, before sleeping, check if
834 * thread was removed from umtx queue.
837 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
839 struct umtxq_chain *uc;
842 if (abstime != NULL && abstime->is_abs_real) {
843 curthread->td_rtcgen = atomic_load_acq_int(&rtc_generation);
844 abs_timeout_update(abstime);
847 uc = umtxq_getchain(&uq->uq_key);
848 UMTXQ_LOCKED_ASSERT(uc);
850 if (!(uq->uq_flags & UQF_UMTXQ)) {
854 if (abstime != NULL) {
855 timo = abs_timeout_gethz(abstime);
862 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
863 if (error == EINTR || error == ERESTART) {
864 umtxq_lock(&uq->uq_key);
867 if (abstime != NULL) {
868 if (abstime->is_abs_real)
869 curthread->td_rtcgen =
870 atomic_load_acq_int(&rtc_generation);
871 abs_timeout_update(abstime);
873 umtxq_lock(&uq->uq_key);
876 curthread->td_rtcgen = 0;
881 * Convert userspace address into unique logical address.
884 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
886 struct thread *td = curthread;
888 vm_map_entry_t entry;
894 if (share == THREAD_SHARE) {
896 key->info.private.vs = td->td_proc->p_vmspace;
897 key->info.private.addr = (uintptr_t)addr;
899 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
900 map = &td->td_proc->p_vmspace->vm_map;
901 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
902 &entry, &key->info.shared.object, &pindex, &prot,
903 &wired) != KERN_SUCCESS) {
907 if ((share == PROCESS_SHARE) ||
908 (share == AUTO_SHARE &&
909 VM_INHERIT_SHARE == entry->inheritance)) {
911 key->info.shared.offset = (vm_offset_t)addr -
912 entry->start + entry->offset;
913 vm_object_reference(key->info.shared.object);
916 key->info.private.vs = td->td_proc->p_vmspace;
917 key->info.private.addr = (uintptr_t)addr;
919 vm_map_lookup_done(map, entry);
930 umtx_key_release(struct umtx_key *key)
933 vm_object_deallocate(key->info.shared.object);
937 * Fetch and compare value, sleep on the address if value is not changed.
940 do_wait(struct thread *td, void *addr, u_long id,
941 struct _umtx_time *timeout, int compat32, int is_private)
943 struct abs_timeout timo;
950 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
951 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
955 abs_timeout_init2(&timo, timeout);
957 umtxq_lock(&uq->uq_key);
959 umtxq_unlock(&uq->uq_key);
961 error = fueword(addr, &tmp);
965 error = fueword32(addr, &tmp32);
971 umtxq_lock(&uq->uq_key);
974 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
976 if ((uq->uq_flags & UQF_UMTXQ) == 0)
980 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
983 umtxq_unlock(&uq->uq_key);
984 umtx_key_release(&uq->uq_key);
985 if (error == ERESTART)
991 * Wake up threads sleeping on the specified address.
994 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
999 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1000 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1003 umtxq_signal(&key, n_wake);
1005 umtx_key_release(&key);
1010 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1013 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1014 struct _umtx_time *timeout, int mode)
1016 struct abs_timeout timo;
1018 uint32_t owner, old, id;
1024 if (timeout != NULL)
1025 abs_timeout_init2(&timo, timeout);
1028 * Care must be exercised when dealing with umtx structure. It
1029 * can fault on any access.
1032 rv = fueword32(&m->m_owner, &owner);
1035 if (mode == _UMUTEX_WAIT) {
1036 if (owner == UMUTEX_UNOWNED ||
1037 owner == UMUTEX_CONTESTED ||
1038 owner == UMUTEX_RB_OWNERDEAD ||
1039 owner == UMUTEX_RB_NOTRECOV)
1043 * Robust mutex terminated. Kernel duty is to
1044 * return EOWNERDEAD to the userspace. The
1045 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1046 * by the common userspace code.
1048 if (owner == UMUTEX_RB_OWNERDEAD) {
1049 rv = casueword32(&m->m_owner,
1050 UMUTEX_RB_OWNERDEAD, &owner,
1051 id | UMUTEX_CONTESTED);
1054 if (owner == UMUTEX_RB_OWNERDEAD)
1055 return (EOWNERDEAD); /* success */
1056 rv = umtxq_check_susp(td);
1061 if (owner == UMUTEX_RB_NOTRECOV)
1062 return (ENOTRECOVERABLE);
1066 * Try the uncontested case. This should be
1069 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1071 /* The address was invalid. */
1075 /* The acquire succeeded. */
1076 if (owner == UMUTEX_UNOWNED)
1080 * If no one owns it but it is contested try
1083 if (owner == UMUTEX_CONTESTED) {
1084 rv = casueword32(&m->m_owner,
1085 UMUTEX_CONTESTED, &owner,
1086 id | UMUTEX_CONTESTED);
1087 /* The address was invalid. */
1091 if (owner == UMUTEX_CONTESTED)
1094 rv = umtxq_check_susp(td);
1099 * If this failed the lock has
1106 if (mode == _UMUTEX_TRY)
1110 * If we caught a signal, we have retried and now
1116 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1117 GET_SHARE(flags), &uq->uq_key)) != 0)
1120 umtxq_lock(&uq->uq_key);
1121 umtxq_busy(&uq->uq_key);
1123 umtxq_unlock(&uq->uq_key);
1126 * Set the contested bit so that a release in user space
1127 * knows to use the system call for unlock. If this fails
1128 * either some one else has acquired the lock or it has been
1131 rv = casueword32(&m->m_owner, owner, &old,
1132 owner | UMUTEX_CONTESTED);
1134 /* The address was invalid. */
1136 umtxq_lock(&uq->uq_key);
1138 umtxq_unbusy(&uq->uq_key);
1139 umtxq_unlock(&uq->uq_key);
1140 umtx_key_release(&uq->uq_key);
1145 * We set the contested bit, sleep. Otherwise the lock changed
1146 * and we need to retry or we lost a race to the thread
1147 * unlocking the umtx.
1149 umtxq_lock(&uq->uq_key);
1150 umtxq_unbusy(&uq->uq_key);
1152 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1155 umtxq_unlock(&uq->uq_key);
1156 umtx_key_release(&uq->uq_key);
1159 error = umtxq_check_susp(td);
1166 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1169 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1171 struct umtx_key key;
1172 uint32_t owner, old, id, newlock;
1177 * Make sure we own this mtx.
1179 error = fueword32(&m->m_owner, &owner);
1183 if ((owner & ~UMUTEX_CONTESTED) != id)
1186 newlock = umtx_unlock_val(flags, rb);
1187 if ((owner & UMUTEX_CONTESTED) == 0) {
1188 error = casueword32(&m->m_owner, owner, &old, newlock);
1196 /* We should only ever be in here for contested locks */
1197 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1203 count = umtxq_count(&key);
1207 * When unlocking the umtx, it must be marked as unowned if
1208 * there is zero or one thread only waiting for it.
1209 * Otherwise, it must be marked as contested.
1212 newlock |= UMUTEX_CONTESTED;
1213 error = casueword32(&m->m_owner, owner, &old, newlock);
1215 umtxq_signal(&key, 1);
1218 umtx_key_release(&key);
1227 * Check if the mutex is available and wake up a waiter,
1228 * only for simple mutex.
1231 do_wake_umutex(struct thread *td, struct umutex *m)
1233 struct umtx_key key;
1239 error = fueword32(&m->m_owner, &owner);
1243 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1244 owner != UMUTEX_RB_NOTRECOV)
1247 error = fueword32(&m->m_flags, &flags);
1251 /* We should only ever be in here for contested locks */
1252 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1258 count = umtxq_count(&key);
1261 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1262 owner != UMUTEX_RB_NOTRECOV) {
1263 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1270 if (error == 0 && count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1271 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1272 umtxq_signal(&key, 1);
1275 umtx_key_release(&key);
1280 * Check if the mutex has waiters and tries to fix contention bit.
1283 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1285 struct umtx_key key;
1286 uint32_t owner, old;
1291 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1295 type = TYPE_NORMAL_UMUTEX;
1297 case UMUTEX_PRIO_INHERIT:
1298 type = TYPE_PI_UMUTEX;
1300 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1301 type = TYPE_PI_ROBUST_UMUTEX;
1303 case UMUTEX_PRIO_PROTECT:
1304 type = TYPE_PP_UMUTEX;
1306 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1307 type = TYPE_PP_ROBUST_UMUTEX;
1312 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1318 count = umtxq_count(&key);
1321 * Only repair contention bit if there is a waiter, this means the mutex
1322 * is still being referenced by userland code, otherwise don't update
1326 error = fueword32(&m->m_owner, &owner);
1329 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0) {
1330 error = casueword32(&m->m_owner, owner, &old,
1331 owner | UMUTEX_CONTESTED);
1339 error = umtxq_check_susp(td);
1343 } else if (count == 1) {
1344 error = fueword32(&m->m_owner, &owner);
1347 while (error == 0 && (owner & ~UMUTEX_CONTESTED) != 0 &&
1348 (owner & UMUTEX_CONTESTED) == 0) {
1349 error = casueword32(&m->m_owner, owner, &old,
1350 owner | UMUTEX_CONTESTED);
1358 error = umtxq_check_susp(td);
1364 if (error == EFAULT) {
1365 umtxq_signal(&key, INT_MAX);
1366 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1367 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1368 umtxq_signal(&key, 1);
1371 umtx_key_release(&key);
1375 static inline struct umtx_pi *
1376 umtx_pi_alloc(int flags)
1380 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1381 TAILQ_INIT(&pi->pi_blocked);
1382 atomic_add_int(&umtx_pi_allocated, 1);
1387 umtx_pi_free(struct umtx_pi *pi)
1389 uma_zfree(umtx_pi_zone, pi);
1390 atomic_add_int(&umtx_pi_allocated, -1);
1394 * Adjust the thread's position on a pi_state after its priority has been
1398 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1400 struct umtx_q *uq, *uq1, *uq2;
1403 mtx_assert(&umtx_lock, MA_OWNED);
1410 * Check if the thread needs to be moved on the blocked chain.
1411 * It needs to be moved if either its priority is lower than
1412 * the previous thread or higher than the next thread.
1414 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1415 uq2 = TAILQ_NEXT(uq, uq_lockq);
1416 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1417 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1419 * Remove thread from blocked chain and determine where
1420 * it should be moved to.
1422 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1423 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1424 td1 = uq1->uq_thread;
1425 MPASS(td1->td_proc->p_magic == P_MAGIC);
1426 if (UPRI(td1) > UPRI(td))
1431 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1433 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1438 static struct umtx_pi *
1439 umtx_pi_next(struct umtx_pi *pi)
1441 struct umtx_q *uq_owner;
1443 if (pi->pi_owner == NULL)
1445 uq_owner = pi->pi_owner->td_umtxq;
1446 if (uq_owner == NULL)
1448 return (uq_owner->uq_pi_blocked);
1452 * Floyd's Cycle-Finding Algorithm.
1455 umtx_pi_check_loop(struct umtx_pi *pi)
1457 struct umtx_pi *pi1; /* fast iterator */
1459 mtx_assert(&umtx_lock, MA_OWNED);
1464 pi = umtx_pi_next(pi);
1467 pi1 = umtx_pi_next(pi1);
1470 pi1 = umtx_pi_next(pi1);
1480 * Propagate priority when a thread is blocked on POSIX
1484 umtx_propagate_priority(struct thread *td)
1490 mtx_assert(&umtx_lock, MA_OWNED);
1493 pi = uq->uq_pi_blocked;
1496 if (umtx_pi_check_loop(pi))
1501 if (td == NULL || td == curthread)
1504 MPASS(td->td_proc != NULL);
1505 MPASS(td->td_proc->p_magic == P_MAGIC);
1508 if (td->td_lend_user_pri > pri)
1509 sched_lend_user_prio(td, pri);
1517 * Pick up the lock that td is blocked on.
1520 pi = uq->uq_pi_blocked;
1523 /* Resort td on the list if needed. */
1524 umtx_pi_adjust_thread(pi, td);
1529 * Unpropagate priority for a PI mutex when a thread blocked on
1530 * it is interrupted by signal or resumed by others.
1533 umtx_repropagate_priority(struct umtx_pi *pi)
1535 struct umtx_q *uq, *uq_owner;
1536 struct umtx_pi *pi2;
1539 mtx_assert(&umtx_lock, MA_OWNED);
1541 if (umtx_pi_check_loop(pi))
1543 while (pi != NULL && pi->pi_owner != NULL) {
1545 uq_owner = pi->pi_owner->td_umtxq;
1547 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1548 uq = TAILQ_FIRST(&pi2->pi_blocked);
1550 if (pri > UPRI(uq->uq_thread))
1551 pri = UPRI(uq->uq_thread);
1555 if (pri > uq_owner->uq_inherited_pri)
1556 pri = uq_owner->uq_inherited_pri;
1557 thread_lock(pi->pi_owner);
1558 sched_lend_user_prio(pi->pi_owner, pri);
1559 thread_unlock(pi->pi_owner);
1560 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1561 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1566 * Insert a PI mutex into owned list.
1569 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1571 struct umtx_q *uq_owner;
1573 uq_owner = owner->td_umtxq;
1574 mtx_assert(&umtx_lock, MA_OWNED);
1575 MPASS(pi->pi_owner == NULL);
1576 pi->pi_owner = owner;
1577 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1582 * Disown a PI mutex, and remove it from the owned list.
1585 umtx_pi_disown(struct umtx_pi *pi)
1588 mtx_assert(&umtx_lock, MA_OWNED);
1589 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1590 pi->pi_owner = NULL;
1594 * Claim ownership of a PI mutex.
1597 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1602 mtx_lock(&umtx_lock);
1603 if (pi->pi_owner == owner) {
1604 mtx_unlock(&umtx_lock);
1608 if (pi->pi_owner != NULL) {
1610 * userland may have already messed the mutex, sigh.
1612 mtx_unlock(&umtx_lock);
1615 umtx_pi_setowner(pi, owner);
1616 uq = TAILQ_FIRST(&pi->pi_blocked);
1618 pri = UPRI(uq->uq_thread);
1620 if (pri < UPRI(owner))
1621 sched_lend_user_prio(owner, pri);
1622 thread_unlock(owner);
1624 mtx_unlock(&umtx_lock);
1629 * Adjust a thread's order position in its blocked PI mutex,
1630 * this may result new priority propagating process.
1633 umtx_pi_adjust(struct thread *td, u_char oldpri)
1639 mtx_lock(&umtx_lock);
1641 * Pick up the lock that td is blocked on.
1643 pi = uq->uq_pi_blocked;
1645 umtx_pi_adjust_thread(pi, td);
1646 umtx_repropagate_priority(pi);
1648 mtx_unlock(&umtx_lock);
1652 * Sleep on a PI mutex.
1655 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
1656 const char *wmesg, struct abs_timeout *timo, bool shared)
1658 struct thread *td, *td1;
1662 struct umtxq_chain *uc;
1664 uc = umtxq_getchain(&pi->pi_key);
1668 KASSERT(td == curthread, ("inconsistent uq_thread"));
1669 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
1670 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1672 mtx_lock(&umtx_lock);
1673 if (pi->pi_owner == NULL) {
1674 mtx_unlock(&umtx_lock);
1675 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
1676 mtx_lock(&umtx_lock);
1678 if (pi->pi_owner == NULL)
1679 umtx_pi_setowner(pi, td1);
1680 PROC_UNLOCK(td1->td_proc);
1684 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1685 pri = UPRI(uq1->uq_thread);
1691 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1693 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1695 uq->uq_pi_blocked = pi;
1697 td->td_flags |= TDF_UPIBLOCKED;
1699 umtx_propagate_priority(td);
1700 mtx_unlock(&umtx_lock);
1701 umtxq_unbusy(&uq->uq_key);
1703 error = umtxq_sleep(uq, wmesg, timo);
1706 mtx_lock(&umtx_lock);
1707 uq->uq_pi_blocked = NULL;
1709 td->td_flags &= ~TDF_UPIBLOCKED;
1711 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1712 umtx_repropagate_priority(pi);
1713 mtx_unlock(&umtx_lock);
1714 umtxq_unlock(&uq->uq_key);
1720 * Add reference count for a PI mutex.
1723 umtx_pi_ref(struct umtx_pi *pi)
1726 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
1731 * Decrease reference count for a PI mutex, if the counter
1732 * is decreased to zero, its memory space is freed.
1735 umtx_pi_unref(struct umtx_pi *pi)
1737 struct umtxq_chain *uc;
1739 uc = umtxq_getchain(&pi->pi_key);
1740 UMTXQ_LOCKED_ASSERT(uc);
1741 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1742 if (--pi->pi_refcount == 0) {
1743 mtx_lock(&umtx_lock);
1744 if (pi->pi_owner != NULL)
1746 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1747 ("blocked queue not empty"));
1748 mtx_unlock(&umtx_lock);
1749 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1755 * Find a PI mutex in hash table.
1757 static struct umtx_pi *
1758 umtx_pi_lookup(struct umtx_key *key)
1760 struct umtxq_chain *uc;
1763 uc = umtxq_getchain(key);
1764 UMTXQ_LOCKED_ASSERT(uc);
1766 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1767 if (umtx_key_match(&pi->pi_key, key)) {
1775 * Insert a PI mutex into hash table.
1778 umtx_pi_insert(struct umtx_pi *pi)
1780 struct umtxq_chain *uc;
1782 uc = umtxq_getchain(&pi->pi_key);
1783 UMTXQ_LOCKED_ASSERT(uc);
1784 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1791 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1792 struct _umtx_time *timeout, int try)
1794 struct abs_timeout timo;
1796 struct umtx_pi *pi, *new_pi;
1797 uint32_t id, old_owner, owner, old;
1803 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
1804 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
1808 if (timeout != NULL)
1809 abs_timeout_init2(&timo, timeout);
1811 umtxq_lock(&uq->uq_key);
1812 pi = umtx_pi_lookup(&uq->uq_key);
1814 new_pi = umtx_pi_alloc(M_NOWAIT);
1815 if (new_pi == NULL) {
1816 umtxq_unlock(&uq->uq_key);
1817 new_pi = umtx_pi_alloc(M_WAITOK);
1818 umtxq_lock(&uq->uq_key);
1819 pi = umtx_pi_lookup(&uq->uq_key);
1821 umtx_pi_free(new_pi);
1825 if (new_pi != NULL) {
1826 new_pi->pi_key = uq->uq_key;
1827 umtx_pi_insert(new_pi);
1832 umtxq_unlock(&uq->uq_key);
1835 * Care must be exercised when dealing with umtx structure. It
1836 * can fault on any access.
1840 * Try the uncontested case. This should be done in userland.
1842 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
1843 /* The address was invalid. */
1849 /* The acquire succeeded. */
1850 if (owner == UMUTEX_UNOWNED) {
1855 if (owner == UMUTEX_RB_NOTRECOV) {
1856 error = ENOTRECOVERABLE;
1860 /* If no one owns it but it is contested try to acquire it. */
1861 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
1863 rv = casueword32(&m->m_owner, owner, &owner,
1864 id | UMUTEX_CONTESTED);
1865 /* The address was invalid. */
1871 if (owner == old_owner) {
1872 umtxq_lock(&uq->uq_key);
1873 umtxq_busy(&uq->uq_key);
1874 error = umtx_pi_claim(pi, td);
1875 umtxq_unbusy(&uq->uq_key);
1876 umtxq_unlock(&uq->uq_key);
1879 * Since we're going to return an
1880 * error, restore the m_owner to its
1881 * previous, unowned state to avoid
1882 * compounding the problem.
1884 (void)casuword32(&m->m_owner,
1885 id | UMUTEX_CONTESTED,
1889 old_owner == UMUTEX_RB_OWNERDEAD)
1894 error = umtxq_check_susp(td);
1898 /* If this failed the lock has changed, restart. */
1902 if ((owner & ~UMUTEX_CONTESTED) == id) {
1913 * If we caught a signal, we have retried and now
1919 umtxq_lock(&uq->uq_key);
1920 umtxq_busy(&uq->uq_key);
1921 umtxq_unlock(&uq->uq_key);
1924 * Set the contested bit so that a release in user space
1925 * knows to use the system call for unlock. If this fails
1926 * either some one else has acquired the lock or it has been
1929 rv = casueword32(&m->m_owner, owner, &old, owner |
1932 /* The address was invalid. */
1934 umtxq_unbusy_unlocked(&uq->uq_key);
1939 umtxq_lock(&uq->uq_key);
1941 * We set the contested bit, sleep. Otherwise the lock changed
1942 * and we need to retry or we lost a race to the thread
1943 * unlocking the umtx. Note that the UMUTEX_RB_OWNERDEAD
1944 * value for owner is impossible there.
1947 error = umtxq_sleep_pi(uq, pi,
1948 owner & ~UMUTEX_CONTESTED,
1949 "umtxpi", timeout == NULL ? NULL : &timo,
1950 (flags & USYNC_PROCESS_SHARED) != 0);
1954 umtxq_unbusy(&uq->uq_key);
1955 umtxq_unlock(&uq->uq_key);
1958 error = umtxq_check_susp(td);
1963 umtxq_lock(&uq->uq_key);
1965 umtxq_unlock(&uq->uq_key);
1967 umtx_key_release(&uq->uq_key);
1972 * Unlock a PI mutex.
1975 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1977 struct umtx_key key;
1978 struct umtx_q *uq_first, *uq_first2, *uq_me;
1979 struct umtx_pi *pi, *pi2;
1980 uint32_t id, new_owner, old, owner;
1981 int count, error, pri;
1985 * Make sure we own this mtx.
1987 error = fueword32(&m->m_owner, &owner);
1991 if ((owner & ~UMUTEX_CONTESTED) != id)
1994 new_owner = umtx_unlock_val(flags, rb);
1996 /* This should be done in userland */
1997 if ((owner & UMUTEX_CONTESTED) == 0) {
1998 error = casueword32(&m->m_owner, owner, &old, new_owner);
2006 /* We should only ever be in here for contested locks */
2007 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2008 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2014 count = umtxq_count_pi(&key, &uq_first);
2015 if (uq_first != NULL) {
2016 mtx_lock(&umtx_lock);
2017 pi = uq_first->uq_pi_blocked;
2018 KASSERT(pi != NULL, ("pi == NULL?"));
2019 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2020 mtx_unlock(&umtx_lock);
2023 umtx_key_release(&key);
2024 /* userland messed the mutex */
2027 uq_me = td->td_umtxq;
2028 if (pi->pi_owner == td)
2030 /* get highest priority thread which is still sleeping. */
2031 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2032 while (uq_first != NULL &&
2033 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2034 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2037 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2038 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2039 if (uq_first2 != NULL) {
2040 if (pri > UPRI(uq_first2->uq_thread))
2041 pri = UPRI(uq_first2->uq_thread);
2045 sched_lend_user_prio(td, pri);
2047 mtx_unlock(&umtx_lock);
2049 umtxq_signal_thread(uq_first);
2051 pi = umtx_pi_lookup(&key);
2053 * A umtx_pi can exist if a signal or timeout removed the
2054 * last waiter from the umtxq, but there is still
2055 * a thread in do_lock_pi() holding the umtx_pi.
2059 * The umtx_pi can be unowned, such as when a thread
2060 * has just entered do_lock_pi(), allocated the
2061 * umtx_pi, and unlocked the umtxq.
2062 * If the current thread owns it, it must disown it.
2064 mtx_lock(&umtx_lock);
2065 if (pi->pi_owner == td)
2067 mtx_unlock(&umtx_lock);
2073 * When unlocking the umtx, it must be marked as unowned if
2074 * there is zero or one thread only waiting for it.
2075 * Otherwise, it must be marked as contested.
2079 new_owner |= UMUTEX_CONTESTED;
2080 error = casueword32(&m->m_owner, owner, &old, new_owner);
2082 umtxq_unbusy_unlocked(&key);
2083 umtx_key_release(&key);
2095 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2096 struct _umtx_time *timeout, int try)
2098 struct abs_timeout timo;
2099 struct umtx_q *uq, *uq2;
2103 int error, pri, old_inherited_pri, su, rv;
2107 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2108 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2112 if (timeout != NULL)
2113 abs_timeout_init2(&timo, timeout);
2115 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2117 old_inherited_pri = uq->uq_inherited_pri;
2118 umtxq_lock(&uq->uq_key);
2119 umtxq_busy(&uq->uq_key);
2120 umtxq_unlock(&uq->uq_key);
2122 rv = fueword32(&m->m_ceilings[0], &ceiling);
2127 ceiling = RTP_PRIO_MAX - ceiling;
2128 if (ceiling > RTP_PRIO_MAX) {
2133 mtx_lock(&umtx_lock);
2134 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2135 mtx_unlock(&umtx_lock);
2139 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2140 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2142 if (uq->uq_inherited_pri < UPRI(td))
2143 sched_lend_user_prio(td, uq->uq_inherited_pri);
2146 mtx_unlock(&umtx_lock);
2148 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2149 id | UMUTEX_CONTESTED);
2150 /* The address was invalid. */
2156 if (owner == UMUTEX_CONTESTED) {
2159 } else if (owner == UMUTEX_RB_OWNERDEAD) {
2160 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2161 &owner, id | UMUTEX_CONTESTED);
2166 if (owner == UMUTEX_RB_OWNERDEAD) {
2167 error = EOWNERDEAD; /* success */
2171 } else if (owner == UMUTEX_RB_NOTRECOV) {
2172 error = ENOTRECOVERABLE;
2182 * If we caught a signal, we have retried and now
2188 umtxq_lock(&uq->uq_key);
2190 umtxq_unbusy(&uq->uq_key);
2191 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2194 umtxq_unlock(&uq->uq_key);
2196 mtx_lock(&umtx_lock);
2197 uq->uq_inherited_pri = old_inherited_pri;
2199 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2200 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2202 if (pri > UPRI(uq2->uq_thread))
2203 pri = UPRI(uq2->uq_thread);
2206 if (pri > uq->uq_inherited_pri)
2207 pri = uq->uq_inherited_pri;
2209 sched_lend_user_prio(td, pri);
2211 mtx_unlock(&umtx_lock);
2214 if (error != 0 && error != EOWNERDEAD) {
2215 mtx_lock(&umtx_lock);
2216 uq->uq_inherited_pri = old_inherited_pri;
2218 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2219 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2221 if (pri > UPRI(uq2->uq_thread))
2222 pri = UPRI(uq2->uq_thread);
2225 if (pri > uq->uq_inherited_pri)
2226 pri = uq->uq_inherited_pri;
2228 sched_lend_user_prio(td, pri);
2230 mtx_unlock(&umtx_lock);
2234 umtxq_unbusy_unlocked(&uq->uq_key);
2235 umtx_key_release(&uq->uq_key);
2240 * Unlock a PP mutex.
2243 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2245 struct umtx_key key;
2246 struct umtx_q *uq, *uq2;
2248 uint32_t id, owner, rceiling;
2249 int error, pri, new_inherited_pri, su;
2253 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2256 * Make sure we own this mtx.
2258 error = fueword32(&m->m_owner, &owner);
2262 if ((owner & ~UMUTEX_CONTESTED) != id)
2265 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2270 new_inherited_pri = PRI_MAX;
2272 rceiling = RTP_PRIO_MAX - rceiling;
2273 if (rceiling > RTP_PRIO_MAX)
2275 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2278 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2279 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2286 * For priority protected mutex, always set unlocked state
2287 * to UMUTEX_CONTESTED, so that userland always enters kernel
2288 * to lock the mutex, it is necessary because thread priority
2289 * has to be adjusted for such mutex.
2291 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2296 umtxq_signal(&key, 1);
2303 mtx_lock(&umtx_lock);
2305 uq->uq_inherited_pri = new_inherited_pri;
2307 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2308 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2310 if (pri > UPRI(uq2->uq_thread))
2311 pri = UPRI(uq2->uq_thread);
2314 if (pri > uq->uq_inherited_pri)
2315 pri = uq->uq_inherited_pri;
2317 sched_lend_user_prio(td, pri);
2319 mtx_unlock(&umtx_lock);
2321 umtx_key_release(&key);
2326 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2327 uint32_t *old_ceiling)
2330 uint32_t flags, id, owner, save_ceiling;
2333 error = fueword32(&m->m_flags, &flags);
2336 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2338 if (ceiling > RTP_PRIO_MAX)
2342 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2343 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2347 umtxq_lock(&uq->uq_key);
2348 umtxq_busy(&uq->uq_key);
2349 umtxq_unlock(&uq->uq_key);
2351 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2357 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2358 id | UMUTEX_CONTESTED);
2364 if (owner == UMUTEX_CONTESTED) {
2365 rv = suword32(&m->m_ceilings[0], ceiling);
2366 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2367 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2371 if ((owner & ~UMUTEX_CONTESTED) == id) {
2372 rv = suword32(&m->m_ceilings[0], ceiling);
2373 error = rv == 0 ? 0 : EFAULT;
2377 if (owner == UMUTEX_RB_OWNERDEAD) {
2380 } else if (owner == UMUTEX_RB_NOTRECOV) {
2381 error = ENOTRECOVERABLE;
2386 * If we caught a signal, we have retried and now
2393 * We set the contested bit, sleep. Otherwise the lock changed
2394 * and we need to retry or we lost a race to the thread
2395 * unlocking the umtx.
2397 umtxq_lock(&uq->uq_key);
2399 umtxq_unbusy(&uq->uq_key);
2400 error = umtxq_sleep(uq, "umtxpp", NULL);
2402 umtxq_unlock(&uq->uq_key);
2404 umtxq_lock(&uq->uq_key);
2406 umtxq_signal(&uq->uq_key, INT_MAX);
2407 umtxq_unbusy(&uq->uq_key);
2408 umtxq_unlock(&uq->uq_key);
2409 umtx_key_release(&uq->uq_key);
2410 if (error == 0 && old_ceiling != NULL) {
2411 rv = suword32(old_ceiling, save_ceiling);
2412 error = rv == 0 ? 0 : EFAULT;
2418 * Lock a userland POSIX mutex.
2421 do_lock_umutex(struct thread *td, struct umutex *m,
2422 struct _umtx_time *timeout, int mode)
2427 error = fueword32(&m->m_flags, &flags);
2431 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2433 error = do_lock_normal(td, m, flags, timeout, mode);
2435 case UMUTEX_PRIO_INHERIT:
2436 error = do_lock_pi(td, m, flags, timeout, mode);
2438 case UMUTEX_PRIO_PROTECT:
2439 error = do_lock_pp(td, m, flags, timeout, mode);
2444 if (timeout == NULL) {
2445 if (error == EINTR && mode != _UMUTEX_WAIT)
2448 /* Timed-locking is not restarted. */
2449 if (error == ERESTART)
2456 * Unlock a userland POSIX mutex.
2459 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2464 error = fueword32(&m->m_flags, &flags);
2468 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2470 return (do_unlock_normal(td, m, flags, rb));
2471 case UMUTEX_PRIO_INHERIT:
2472 return (do_unlock_pi(td, m, flags, rb));
2473 case UMUTEX_PRIO_PROTECT:
2474 return (do_unlock_pp(td, m, flags, rb));
2481 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2482 struct timespec *timeout, u_long wflags)
2484 struct abs_timeout timo;
2486 uint32_t flags, clockid, hasw;
2490 error = fueword32(&cv->c_flags, &flags);
2493 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2497 if ((wflags & CVWAIT_CLOCKID) != 0) {
2498 error = fueword32(&cv->c_clockid, &clockid);
2500 umtx_key_release(&uq->uq_key);
2503 if (clockid < CLOCK_REALTIME ||
2504 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2505 /* hmm, only HW clock id will work. */
2506 umtx_key_release(&uq->uq_key);
2510 clockid = CLOCK_REALTIME;
2513 umtxq_lock(&uq->uq_key);
2514 umtxq_busy(&uq->uq_key);
2516 umtxq_unlock(&uq->uq_key);
2519 * Set c_has_waiters to 1 before releasing user mutex, also
2520 * don't modify cache line when unnecessary.
2522 error = fueword32(&cv->c_has_waiters, &hasw);
2523 if (error == 0 && hasw == 0)
2524 suword32(&cv->c_has_waiters, 1);
2526 umtxq_unbusy_unlocked(&uq->uq_key);
2528 error = do_unlock_umutex(td, m, false);
2530 if (timeout != NULL)
2531 abs_timeout_init(&timo, clockid, (wflags & CVWAIT_ABSTIME) != 0,
2534 umtxq_lock(&uq->uq_key);
2536 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2540 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2544 * This must be timeout,interrupted by signal or
2545 * surprious wakeup, clear c_has_waiter flag when
2548 umtxq_busy(&uq->uq_key);
2549 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2550 int oldlen = uq->uq_cur_queue->length;
2553 umtxq_unlock(&uq->uq_key);
2554 suword32(&cv->c_has_waiters, 0);
2555 umtxq_lock(&uq->uq_key);
2558 umtxq_unbusy(&uq->uq_key);
2559 if (error == ERESTART)
2563 umtxq_unlock(&uq->uq_key);
2564 umtx_key_release(&uq->uq_key);
2569 * Signal a userland condition variable.
2572 do_cv_signal(struct thread *td, struct ucond *cv)
2574 struct umtx_key key;
2575 int error, cnt, nwake;
2578 error = fueword32(&cv->c_flags, &flags);
2581 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2585 cnt = umtxq_count(&key);
2586 nwake = umtxq_signal(&key, 1);
2589 error = suword32(&cv->c_has_waiters, 0);
2596 umtx_key_release(&key);
2601 do_cv_broadcast(struct thread *td, struct ucond *cv)
2603 struct umtx_key key;
2607 error = fueword32(&cv->c_flags, &flags);
2610 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2615 umtxq_signal(&key, INT_MAX);
2618 error = suword32(&cv->c_has_waiters, 0);
2622 umtxq_unbusy_unlocked(&key);
2624 umtx_key_release(&key);
2629 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, struct _umtx_time *timeout)
2631 struct abs_timeout timo;
2633 uint32_t flags, wrflags;
2634 int32_t state, oldstate;
2635 int32_t blocked_readers;
2636 int error, error1, rv;
2639 error = fueword32(&rwlock->rw_flags, &flags);
2642 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2646 if (timeout != NULL)
2647 abs_timeout_init2(&timo, timeout);
2649 wrflags = URWLOCK_WRITE_OWNER;
2650 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2651 wrflags |= URWLOCK_WRITE_WAITERS;
2654 rv = fueword32(&rwlock->rw_state, &state);
2656 umtx_key_release(&uq->uq_key);
2660 /* try to lock it */
2661 while (!(state & wrflags)) {
2662 if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) {
2663 umtx_key_release(&uq->uq_key);
2666 rv = casueword32(&rwlock->rw_state, state,
2667 &oldstate, state + 1);
2669 umtx_key_release(&uq->uq_key);
2672 if (oldstate == state) {
2673 umtx_key_release(&uq->uq_key);
2676 error = umtxq_check_susp(td);
2685 /* grab monitor lock */
2686 umtxq_lock(&uq->uq_key);
2687 umtxq_busy(&uq->uq_key);
2688 umtxq_unlock(&uq->uq_key);
2691 * re-read the state, in case it changed between the try-lock above
2692 * and the check below
2694 rv = fueword32(&rwlock->rw_state, &state);
2698 /* set read contention bit */
2699 while (error == 0 && (state & wrflags) &&
2700 !(state & URWLOCK_READ_WAITERS)) {
2701 rv = casueword32(&rwlock->rw_state, state,
2702 &oldstate, state | URWLOCK_READ_WAITERS);
2707 if (oldstate == state)
2710 error = umtxq_check_susp(td);
2715 umtxq_unbusy_unlocked(&uq->uq_key);
2719 /* state is changed while setting flags, restart */
2720 if (!(state & wrflags)) {
2721 umtxq_unbusy_unlocked(&uq->uq_key);
2722 error = umtxq_check_susp(td);
2729 /* contention bit is set, before sleeping, increase read waiter count */
2730 rv = fueword32(&rwlock->rw_blocked_readers,
2733 umtxq_unbusy_unlocked(&uq->uq_key);
2737 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2739 while (state & wrflags) {
2740 umtxq_lock(&uq->uq_key);
2742 umtxq_unbusy(&uq->uq_key);
2744 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2747 umtxq_busy(&uq->uq_key);
2749 umtxq_unlock(&uq->uq_key);
2752 rv = fueword32(&rwlock->rw_state, &state);
2759 /* decrease read waiter count, and may clear read contention bit */
2760 rv = fueword32(&rwlock->rw_blocked_readers,
2763 umtxq_unbusy_unlocked(&uq->uq_key);
2767 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2768 if (blocked_readers == 1) {
2769 rv = fueword32(&rwlock->rw_state, &state);
2771 umtxq_unbusy_unlocked(&uq->uq_key);
2776 rv = casueword32(&rwlock->rw_state, state,
2777 &oldstate, state & ~URWLOCK_READ_WAITERS);
2782 if (oldstate == state)
2785 error1 = umtxq_check_susp(td);
2794 umtxq_unbusy_unlocked(&uq->uq_key);
2798 umtx_key_release(&uq->uq_key);
2799 if (error == ERESTART)
2805 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2807 struct abs_timeout timo;
2810 int32_t state, oldstate;
2811 int32_t blocked_writers;
2812 int32_t blocked_readers;
2813 int error, error1, rv;
2816 error = fueword32(&rwlock->rw_flags, &flags);
2819 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2823 if (timeout != NULL)
2824 abs_timeout_init2(&timo, timeout);
2826 blocked_readers = 0;
2828 rv = fueword32(&rwlock->rw_state, &state);
2830 umtx_key_release(&uq->uq_key);
2833 while (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2834 rv = casueword32(&rwlock->rw_state, state,
2835 &oldstate, state | URWLOCK_WRITE_OWNER);
2837 umtx_key_release(&uq->uq_key);
2840 if (oldstate == state) {
2841 umtx_key_release(&uq->uq_key);
2845 error = umtxq_check_susp(td);
2851 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) &&
2852 blocked_readers != 0) {
2853 umtxq_lock(&uq->uq_key);
2854 umtxq_busy(&uq->uq_key);
2855 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE);
2856 umtxq_unbusy(&uq->uq_key);
2857 umtxq_unlock(&uq->uq_key);
2863 /* grab monitor lock */
2864 umtxq_lock(&uq->uq_key);
2865 umtxq_busy(&uq->uq_key);
2866 umtxq_unlock(&uq->uq_key);
2869 * re-read the state, in case it changed between the try-lock above
2870 * and the check below
2872 rv = fueword32(&rwlock->rw_state, &state);
2876 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
2877 URWLOCK_READER_COUNT(state) != 0) &&
2878 (state & URWLOCK_WRITE_WAITERS) == 0) {
2879 rv = casueword32(&rwlock->rw_state, state,
2880 &oldstate, state | URWLOCK_WRITE_WAITERS);
2885 if (oldstate == state)
2888 error = umtxq_check_susp(td);
2893 umtxq_unbusy_unlocked(&uq->uq_key);
2897 if (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2898 umtxq_unbusy_unlocked(&uq->uq_key);
2899 error = umtxq_check_susp(td);
2905 rv = fueword32(&rwlock->rw_blocked_writers,
2908 umtxq_unbusy_unlocked(&uq->uq_key);
2912 suword32(&rwlock->rw_blocked_writers, blocked_writers+1);
2914 while ((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) {
2915 umtxq_lock(&uq->uq_key);
2916 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2917 umtxq_unbusy(&uq->uq_key);
2919 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
2922 umtxq_busy(&uq->uq_key);
2923 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2924 umtxq_unlock(&uq->uq_key);
2927 rv = fueword32(&rwlock->rw_state, &state);
2934 rv = fueword32(&rwlock->rw_blocked_writers,
2937 umtxq_unbusy_unlocked(&uq->uq_key);
2941 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
2942 if (blocked_writers == 1) {
2943 rv = fueword32(&rwlock->rw_state, &state);
2945 umtxq_unbusy_unlocked(&uq->uq_key);
2950 rv = casueword32(&rwlock->rw_state, state,
2951 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
2956 if (oldstate == state)
2959 error1 = umtxq_check_susp(td);
2961 * We are leaving the URWLOCK_WRITE_WAITERS
2962 * behind, but this should not harm the
2971 rv = fueword32(&rwlock->rw_blocked_readers,
2974 umtxq_unbusy_unlocked(&uq->uq_key);
2979 blocked_readers = 0;
2981 umtxq_unbusy_unlocked(&uq->uq_key);
2984 umtx_key_release(&uq->uq_key);
2985 if (error == ERESTART)
2991 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
2995 int32_t state, oldstate;
2996 int error, rv, q, count;
2999 error = fueword32(&rwlock->rw_flags, &flags);
3002 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3006 error = fueword32(&rwlock->rw_state, &state);
3011 if (state & URWLOCK_WRITE_OWNER) {
3013 rv = casueword32(&rwlock->rw_state, state,
3014 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3019 if (oldstate != state) {
3021 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3025 error = umtxq_check_susp(td);
3031 } else if (URWLOCK_READER_COUNT(state) != 0) {
3033 rv = casueword32(&rwlock->rw_state, state,
3034 &oldstate, state - 1);
3039 if (oldstate != state) {
3041 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3045 error = umtxq_check_susp(td);
3058 if (!(flags & URWLOCK_PREFER_READER)) {
3059 if (state & URWLOCK_WRITE_WAITERS) {
3061 q = UMTX_EXCLUSIVE_QUEUE;
3062 } else if (state & URWLOCK_READ_WAITERS) {
3064 q = UMTX_SHARED_QUEUE;
3067 if (state & URWLOCK_READ_WAITERS) {
3069 q = UMTX_SHARED_QUEUE;
3070 } else if (state & URWLOCK_WRITE_WAITERS) {
3072 q = UMTX_EXCLUSIVE_QUEUE;
3077 umtxq_lock(&uq->uq_key);
3078 umtxq_busy(&uq->uq_key);
3079 umtxq_signal_queue(&uq->uq_key, count, q);
3080 umtxq_unbusy(&uq->uq_key);
3081 umtxq_unlock(&uq->uq_key);
3084 umtx_key_release(&uq->uq_key);
3088 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3090 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3092 struct abs_timeout timo;
3094 uint32_t flags, count, count1;
3098 error = fueword32(&sem->_flags, &flags);
3101 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3105 if (timeout != NULL)
3106 abs_timeout_init2(&timo, timeout);
3108 umtxq_lock(&uq->uq_key);
3109 umtxq_busy(&uq->uq_key);
3111 umtxq_unlock(&uq->uq_key);
3112 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3114 rv = fueword32(&sem->_count, &count);
3115 if (rv == -1 || count != 0) {
3116 umtxq_lock(&uq->uq_key);
3117 umtxq_unbusy(&uq->uq_key);
3119 umtxq_unlock(&uq->uq_key);
3120 umtx_key_release(&uq->uq_key);
3121 return (rv == -1 ? EFAULT : 0);
3123 umtxq_lock(&uq->uq_key);
3124 umtxq_unbusy(&uq->uq_key);
3126 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3128 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3132 /* A relative timeout cannot be restarted. */
3133 if (error == ERESTART && timeout != NULL &&
3134 (timeout->_flags & UMTX_ABSTIME) == 0)
3137 umtxq_unlock(&uq->uq_key);
3138 umtx_key_release(&uq->uq_key);
3143 * Signal a userland semaphore.
3146 do_sem_wake(struct thread *td, struct _usem *sem)
3148 struct umtx_key key;
3152 error = fueword32(&sem->_flags, &flags);
3155 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3159 cnt = umtxq_count(&key);
3162 * Check if count is greater than 0, this means the memory is
3163 * still being referenced by user code, so we can safely
3164 * update _has_waiters flag.
3168 error = suword32(&sem->_has_waiters, 0);
3173 umtxq_signal(&key, 1);
3177 umtx_key_release(&key);
3183 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3185 struct abs_timeout timo;
3187 uint32_t count, flags;
3191 flags = fuword32(&sem->_flags);
3192 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3196 if (timeout != NULL)
3197 abs_timeout_init2(&timo, timeout);
3199 umtxq_lock(&uq->uq_key);
3200 umtxq_busy(&uq->uq_key);
3202 umtxq_unlock(&uq->uq_key);
3203 rv = fueword32(&sem->_count, &count);
3205 umtxq_lock(&uq->uq_key);
3206 umtxq_unbusy(&uq->uq_key);
3208 umtxq_unlock(&uq->uq_key);
3209 umtx_key_release(&uq->uq_key);
3213 if (USEM_COUNT(count) != 0) {
3214 umtxq_lock(&uq->uq_key);
3215 umtxq_unbusy(&uq->uq_key);
3217 umtxq_unlock(&uq->uq_key);
3218 umtx_key_release(&uq->uq_key);
3221 if (count == USEM_HAS_WAITERS)
3223 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3225 umtxq_lock(&uq->uq_key);
3226 umtxq_unbusy(&uq->uq_key);
3228 umtxq_unlock(&uq->uq_key);
3229 umtx_key_release(&uq->uq_key);
3235 umtxq_lock(&uq->uq_key);
3236 umtxq_unbusy(&uq->uq_key);
3238 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3240 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3244 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3245 /* A relative timeout cannot be restarted. */
3246 if (error == ERESTART)
3248 if (error == EINTR) {
3249 abs_timeout_update(&timo);
3250 timeout->_timeout = timo.end;
3251 timespecsub(&timeout->_timeout, &timo.cur);
3255 umtxq_unlock(&uq->uq_key);
3256 umtx_key_release(&uq->uq_key);
3261 * Signal a userland semaphore.
3264 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3266 struct umtx_key key;
3268 uint32_t count, flags;
3270 rv = fueword32(&sem->_flags, &flags);
3273 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3277 cnt = umtxq_count(&key);
3280 * If this was the last sleeping thread, clear the waiters
3285 rv = fueword32(&sem->_count, &count);
3286 while (rv != -1 && count & USEM_HAS_WAITERS)
3287 rv = casueword32(&sem->_count, count, &count,
3288 count & ~USEM_HAS_WAITERS);
3294 umtxq_signal(&key, 1);
3298 umtx_key_release(&key);
3303 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3307 error = copyin(addr, tsp, sizeof(struct timespec));
3309 if (tsp->tv_sec < 0 ||
3310 tsp->tv_nsec >= 1000000000 ||
3318 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3322 if (size <= sizeof(struct timespec)) {
3323 tp->_clockid = CLOCK_REALTIME;
3325 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3327 error = copyin(addr, tp, sizeof(struct _umtx_time));
3330 if (tp->_timeout.tv_sec < 0 ||
3331 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3337 __umtx_op_unimpl(struct thread *td, struct _umtx_op_args *uap)
3340 return (EOPNOTSUPP);
3344 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3346 struct _umtx_time timeout, *tm_p;
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, 0, 0));
3362 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3364 struct _umtx_time timeout, *tm_p;
3367 if (uap->uaddr2 == NULL)
3370 error = umtx_copyin_umtx_time(
3371 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3376 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3380 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3382 struct _umtx_time *tm_p, timeout;
3385 if (uap->uaddr2 == NULL)
3388 error = umtx_copyin_umtx_time(
3389 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3394 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3398 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3401 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3404 #define BATCH_SIZE 128
3406 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3408 char *uaddrs[BATCH_SIZE], **upp;
3409 int count, error, i, pos, tocopy;
3411 upp = (char **)uap->obj;
3413 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3415 tocopy = MIN(count, BATCH_SIZE);
3416 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3419 for (i = 0; i < tocopy; ++i)
3420 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3427 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3430 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3434 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3436 struct _umtx_time *tm_p, timeout;
3439 /* Allow a null timespec (wait forever). */
3440 if (uap->uaddr2 == NULL)
3443 error = umtx_copyin_umtx_time(
3444 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3449 return (do_lock_umutex(td, uap->obj, tm_p, 0));
3453 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3456 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
3460 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3462 struct _umtx_time *tm_p, timeout;
3465 /* Allow a null timespec (wait forever). */
3466 if (uap->uaddr2 == NULL)
3469 error = umtx_copyin_umtx_time(
3470 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3475 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
3479 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3482 return (do_wake_umutex(td, uap->obj));
3486 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3489 return (do_unlock_umutex(td, uap->obj, false));
3493 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3496 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
3500 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3502 struct timespec *ts, timeout;
3505 /* Allow a null timespec (wait forever). */
3506 if (uap->uaddr2 == NULL)
3509 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3514 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3518 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3521 return (do_cv_signal(td, uap->obj));
3525 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3528 return (do_cv_broadcast(td, uap->obj));
3532 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3534 struct _umtx_time timeout;
3537 /* Allow a null timespec (wait forever). */
3538 if (uap->uaddr2 == NULL) {
3539 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3541 error = umtx_copyin_umtx_time(uap->uaddr2,
3542 (size_t)uap->uaddr1, &timeout);
3545 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3551 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3553 struct _umtx_time timeout;
3556 /* Allow a null timespec (wait forever). */
3557 if (uap->uaddr2 == NULL) {
3558 error = do_rw_wrlock(td, uap->obj, 0);
3560 error = umtx_copyin_umtx_time(uap->uaddr2,
3561 (size_t)uap->uaddr1, &timeout);
3565 error = do_rw_wrlock(td, uap->obj, &timeout);
3571 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3574 return (do_rw_unlock(td, uap->obj));
3577 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3579 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3581 struct _umtx_time *tm_p, timeout;
3584 /* Allow a null timespec (wait forever). */
3585 if (uap->uaddr2 == NULL)
3588 error = umtx_copyin_umtx_time(
3589 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3594 return (do_sem_wait(td, uap->obj, tm_p));
3598 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3601 return (do_sem_wake(td, uap->obj));
3606 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3609 return (do_wake2_umutex(td, uap->obj, uap->val));
3613 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap)
3615 struct _umtx_time *tm_p, timeout;
3619 /* Allow a null timespec (wait forever). */
3620 if (uap->uaddr2 == NULL) {
3624 uasize = (size_t)uap->uaddr1;
3625 error = umtx_copyin_umtx_time(uap->uaddr2, uasize, &timeout);
3630 error = do_sem2_wait(td, uap->obj, tm_p);
3631 if (error == EINTR && uap->uaddr2 != NULL &&
3632 (timeout._flags & UMTX_ABSTIME) == 0 &&
3633 uasize >= sizeof(struct _umtx_time) + sizeof(struct timespec)) {
3634 error = copyout(&timeout._timeout,
3635 (struct _umtx_time *)uap->uaddr2 + 1,
3636 sizeof(struct timespec));
3646 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap)
3649 return (do_sem2_wake(td, uap->obj));
3652 #define USHM_OBJ_UMTX(o) \
3653 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
3655 #define USHMF_REG_LINKED 0x0001
3656 #define USHMF_OBJ_LINKED 0x0002
3657 struct umtx_shm_reg {
3658 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
3659 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
3660 struct umtx_key ushm_key;
3661 struct ucred *ushm_cred;
3662 struct shmfd *ushm_obj;
3667 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
3668 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
3670 static uma_zone_t umtx_shm_reg_zone;
3671 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
3672 static struct mtx umtx_shm_lock;
3673 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
3674 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
3676 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
3679 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
3681 struct umtx_shm_reg_head d;
3682 struct umtx_shm_reg *reg, *reg1;
3685 mtx_lock(&umtx_shm_lock);
3686 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
3687 mtx_unlock(&umtx_shm_lock);
3688 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
3689 TAILQ_REMOVE(&d, reg, ushm_reg_link);
3690 umtx_shm_free_reg(reg);
3694 static struct task umtx_shm_reg_delfree_task =
3695 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
3697 static struct umtx_shm_reg *
3698 umtx_shm_find_reg_locked(const struct umtx_key *key)
3700 struct umtx_shm_reg *reg;
3701 struct umtx_shm_reg_head *reg_head;
3703 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
3704 mtx_assert(&umtx_shm_lock, MA_OWNED);
3705 reg_head = &umtx_shm_registry[key->hash];
3706 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
3707 KASSERT(reg->ushm_key.shared,
3708 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
3709 if (reg->ushm_key.info.shared.object ==
3710 key->info.shared.object &&
3711 reg->ushm_key.info.shared.offset ==
3712 key->info.shared.offset) {
3713 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
3714 KASSERT(reg->ushm_refcnt > 0,
3715 ("reg %p refcnt 0 onlist", reg));
3716 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
3717 ("reg %p not linked", reg));
3725 static struct umtx_shm_reg *
3726 umtx_shm_find_reg(const struct umtx_key *key)
3728 struct umtx_shm_reg *reg;
3730 mtx_lock(&umtx_shm_lock);
3731 reg = umtx_shm_find_reg_locked(key);
3732 mtx_unlock(&umtx_shm_lock);
3737 umtx_shm_free_reg(struct umtx_shm_reg *reg)
3740 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
3741 crfree(reg->ushm_cred);
3742 shm_drop(reg->ushm_obj);
3743 uma_zfree(umtx_shm_reg_zone, reg);
3747 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
3751 mtx_assert(&umtx_shm_lock, MA_OWNED);
3752 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
3754 res = reg->ushm_refcnt == 0;
3756 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
3757 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
3758 reg, ushm_reg_link);
3759 reg->ushm_flags &= ~USHMF_REG_LINKED;
3761 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
3762 LIST_REMOVE(reg, ushm_obj_link);
3763 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
3770 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
3776 object = reg->ushm_obj->shm_object;
3777 VM_OBJECT_WLOCK(object);
3778 object->flags |= OBJ_UMTXDEAD;
3779 VM_OBJECT_WUNLOCK(object);
3781 mtx_lock(&umtx_shm_lock);
3782 dofree = umtx_shm_unref_reg_locked(reg, force);
3783 mtx_unlock(&umtx_shm_lock);
3785 umtx_shm_free_reg(reg);
3789 umtx_shm_object_init(vm_object_t object)
3792 LIST_INIT(USHM_OBJ_UMTX(object));
3796 umtx_shm_object_terminated(vm_object_t object)
3798 struct umtx_shm_reg *reg, *reg1;
3802 mtx_lock(&umtx_shm_lock);
3803 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
3804 if (umtx_shm_unref_reg_locked(reg, true)) {
3805 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
3810 mtx_unlock(&umtx_shm_lock);
3812 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
3816 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
3817 struct umtx_shm_reg **res)
3819 struct umtx_shm_reg *reg, *reg1;
3823 reg = umtx_shm_find_reg(key);
3828 cred = td->td_ucred;
3829 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
3831 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
3832 reg->ushm_refcnt = 1;
3833 bcopy(key, ®->ushm_key, sizeof(*key));
3834 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR);
3835 reg->ushm_cred = crhold(cred);
3836 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
3838 umtx_shm_free_reg(reg);
3841 mtx_lock(&umtx_shm_lock);
3842 reg1 = umtx_shm_find_reg_locked(key);
3844 mtx_unlock(&umtx_shm_lock);
3845 umtx_shm_free_reg(reg);
3850 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
3851 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
3853 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
3854 mtx_unlock(&umtx_shm_lock);
3860 umtx_shm_alive(struct thread *td, void *addr)
3863 vm_map_entry_t entry;
3870 map = &td->td_proc->p_vmspace->vm_map;
3871 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
3872 &object, &pindex, &prot, &wired);
3873 if (res != KERN_SUCCESS)
3878 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
3879 vm_map_lookup_done(map, entry);
3888 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
3889 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
3890 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
3891 for (i = 0; i < nitems(umtx_shm_registry); i++)
3892 TAILQ_INIT(&umtx_shm_registry[i]);
3896 umtx_shm(struct thread *td, void *addr, u_int flags)
3898 struct umtx_key key;
3899 struct umtx_shm_reg *reg;
3903 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
3904 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
3906 if ((flags & UMTX_SHM_ALIVE) != 0)
3907 return (umtx_shm_alive(td, addr));
3908 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
3911 KASSERT(key.shared == 1, ("non-shared key"));
3912 if ((flags & UMTX_SHM_CREAT) != 0) {
3913 error = umtx_shm_create_reg(td, &key, ®);
3915 reg = umtx_shm_find_reg(&key);
3919 umtx_key_release(&key);
3922 KASSERT(reg != NULL, ("no reg"));
3923 if ((flags & UMTX_SHM_DESTROY) != 0) {
3924 umtx_shm_unref_reg(reg, true);
3928 error = mac_posixshm_check_open(td->td_ucred,
3929 reg->ushm_obj, FFLAGS(O_RDWR));
3932 error = shm_access(reg->ushm_obj, td->td_ucred,
3936 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
3938 shm_hold(reg->ushm_obj);
3939 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
3941 td->td_retval[0] = fd;
3945 umtx_shm_unref_reg(reg, false);
3950 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap)
3953 return (umtx_shm(td, uap->uaddr1, uap->val));
3957 umtx_robust_lists(struct thread *td, struct umtx_robust_lists_params *rbp)
3960 td->td_rb_list = rbp->robust_list_offset;
3961 td->td_rbp_list = rbp->robust_priv_list_offset;
3962 td->td_rb_inact = rbp->robust_inact_offset;
3967 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap)
3969 struct umtx_robust_lists_params rb;
3972 if (uap->val > sizeof(rb))
3974 bzero(&rb, sizeof(rb));
3975 error = copyin(uap->uaddr1, &rb, uap->val);
3978 return (umtx_robust_lists(td, &rb));
3981 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
3983 static const _umtx_op_func op_table[] = {
3984 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
3985 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
3986 [UMTX_OP_WAIT] = __umtx_op_wait,
3987 [UMTX_OP_WAKE] = __umtx_op_wake,
3988 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
3989 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
3990 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
3991 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
3992 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
3993 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
3994 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
3995 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
3996 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
3997 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
3998 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
3999 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4000 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4001 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4002 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4003 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4004 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4005 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4007 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4008 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4010 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4011 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4012 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4013 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4014 [UMTX_OP_SHM] = __umtx_op_shm,
4015 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4019 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4022 if ((unsigned)uap->op < nitems(op_table))
4023 return (*op_table[uap->op])(td, uap);
4027 #ifdef COMPAT_FREEBSD32
4034 struct umtx_time32 {
4035 struct timespec32 timeout;
4041 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
4043 struct timespec32 ts32;
4046 error = copyin(addr, &ts32, sizeof(struct timespec32));
4048 if (ts32.tv_sec < 0 ||
4049 ts32.tv_nsec >= 1000000000 ||
4053 tsp->tv_sec = ts32.tv_sec;
4054 tsp->tv_nsec = ts32.tv_nsec;
4061 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
4063 struct umtx_time32 t32;
4066 t32.clockid = CLOCK_REALTIME;
4068 if (size <= sizeof(struct timespec32))
4069 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
4071 error = copyin(addr, &t32, sizeof(struct umtx_time32));
4074 if (t32.timeout.tv_sec < 0 ||
4075 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
4077 tp->_timeout.tv_sec = t32.timeout.tv_sec;
4078 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
4079 tp->_flags = t32.flags;
4080 tp->_clockid = t32.clockid;
4085 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4087 struct _umtx_time *tm_p, timeout;
4090 if (uap->uaddr2 == NULL)
4093 error = umtx_copyin_umtx_time32(uap->uaddr2,
4094 (size_t)uap->uaddr1, &timeout);
4099 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
4103 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4105 struct _umtx_time *tm_p, timeout;
4108 /* Allow a null timespec (wait forever). */
4109 if (uap->uaddr2 == NULL)
4112 error = umtx_copyin_umtx_time32(uap->uaddr2,
4113 (size_t)uap->uaddr1, &timeout);
4118 return (do_lock_umutex(td, uap->obj, tm_p, 0));
4122 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4124 struct _umtx_time *tm_p, timeout;
4127 /* Allow a null timespec (wait forever). */
4128 if (uap->uaddr2 == NULL)
4131 error = umtx_copyin_umtx_time32(uap->uaddr2,
4132 (size_t)uap->uaddr1, &timeout);
4137 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
4141 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4143 struct timespec *ts, timeout;
4146 /* Allow a null timespec (wait forever). */
4147 if (uap->uaddr2 == NULL)
4150 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
4155 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
4159 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4161 struct _umtx_time timeout;
4164 /* Allow a null timespec (wait forever). */
4165 if (uap->uaddr2 == NULL) {
4166 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4168 error = umtx_copyin_umtx_time32(uap->uaddr2,
4169 (size_t)uap->uaddr1, &timeout);
4172 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4178 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4180 struct _umtx_time timeout;
4183 /* Allow a null timespec (wait forever). */
4184 if (uap->uaddr2 == NULL) {
4185 error = do_rw_wrlock(td, uap->obj, 0);
4187 error = umtx_copyin_umtx_time32(uap->uaddr2,
4188 (size_t)uap->uaddr1, &timeout);
4191 error = do_rw_wrlock(td, uap->obj, &timeout);
4197 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
4199 struct _umtx_time *tm_p, timeout;
4202 if (uap->uaddr2 == NULL)
4205 error = umtx_copyin_umtx_time32(
4206 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
4211 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
4214 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4216 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4218 struct _umtx_time *tm_p, timeout;
4221 /* Allow a null timespec (wait forever). */
4222 if (uap->uaddr2 == NULL)
4225 error = umtx_copyin_umtx_time32(uap->uaddr2,
4226 (size_t)uap->uaddr1, &timeout);
4231 return (do_sem_wait(td, uap->obj, tm_p));
4236 __umtx_op_sem2_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4238 struct _umtx_time *tm_p, timeout;
4242 /* Allow a null timespec (wait forever). */
4243 if (uap->uaddr2 == NULL) {
4247 uasize = (size_t)uap->uaddr1;
4248 error = umtx_copyin_umtx_time32(uap->uaddr2, uasize, &timeout);
4253 error = do_sem2_wait(td, uap->obj, tm_p);
4254 if (error == EINTR && uap->uaddr2 != NULL &&
4255 (timeout._flags & UMTX_ABSTIME) == 0 &&
4256 uasize >= sizeof(struct umtx_time32) + sizeof(struct timespec32)) {
4257 struct timespec32 remain32 = {
4258 .tv_sec = timeout._timeout.tv_sec,
4259 .tv_nsec = timeout._timeout.tv_nsec
4261 error = copyout(&remain32,
4262 (struct umtx_time32 *)uap->uaddr2 + 1,
4263 sizeof(struct timespec32));
4273 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
4275 uint32_t uaddrs[BATCH_SIZE], **upp;
4276 int count, error, i, pos, tocopy;
4278 upp = (uint32_t **)uap->obj;
4280 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
4282 tocopy = MIN(count, BATCH_SIZE);
4283 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
4286 for (i = 0; i < tocopy; ++i)
4287 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
4294 struct umtx_robust_lists_params_compat32 {
4295 uint32_t robust_list_offset;
4296 uint32_t robust_priv_list_offset;
4297 uint32_t robust_inact_offset;
4301 __umtx_op_robust_lists_compat32(struct thread *td, struct _umtx_op_args *uap)
4303 struct umtx_robust_lists_params rb;
4304 struct umtx_robust_lists_params_compat32 rb32;
4307 if (uap->val > sizeof(rb32))
4309 bzero(&rb, sizeof(rb));
4310 bzero(&rb32, sizeof(rb32));
4311 error = copyin(uap->uaddr1, &rb32, uap->val);
4314 rb.robust_list_offset = rb32.robust_list_offset;
4315 rb.robust_priv_list_offset = rb32.robust_priv_list_offset;
4316 rb.robust_inact_offset = rb32.robust_inact_offset;
4317 return (umtx_robust_lists(td, &rb));
4320 static const _umtx_op_func op_table_compat32[] = {
4321 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
4322 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
4323 [UMTX_OP_WAIT] = __umtx_op_wait_compat32,
4324 [UMTX_OP_WAKE] = __umtx_op_wake,
4325 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4326 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex_compat32,
4327 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4328 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4329 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait_compat32,
4330 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4331 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4332 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_compat32,
4333 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock_compat32,
4334 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock_compat32,
4335 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4336 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private_compat32,
4337 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4338 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex_compat32,
4339 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4340 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4341 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait_compat32,
4342 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4344 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4345 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4347 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private32,
4348 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4349 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait_compat32,
4350 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4351 [UMTX_OP_SHM] = __umtx_op_shm,
4352 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists_compat32,
4356 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
4359 if ((unsigned)uap->op < nitems(op_table_compat32)) {
4360 return (*op_table_compat32[uap->op])(td,
4361 (struct _umtx_op_args *)uap);
4368 umtx_thread_init(struct thread *td)
4371 td->td_umtxq = umtxq_alloc();
4372 td->td_umtxq->uq_thread = td;
4376 umtx_thread_fini(struct thread *td)
4379 umtxq_free(td->td_umtxq);
4383 * It will be called when new thread is created, e.g fork().
4386 umtx_thread_alloc(struct thread *td)
4391 uq->uq_inherited_pri = PRI_MAX;
4393 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4394 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4395 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4396 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4402 * Clear robust lists for all process' threads, not delaying the
4403 * cleanup to thread_exit hook, since the relevant address space is
4404 * destroyed right now.
4407 umtx_exec_hook(void *arg __unused, struct proc *p,
4408 struct image_params *imgp __unused)
4412 KASSERT(p == curproc, ("need curproc"));
4414 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4415 (p->p_flag & P_STOPPED_SINGLE) != 0,
4416 ("curproc must be single-threaded"));
4417 FOREACH_THREAD_IN_PROC(p, td) {
4418 KASSERT(td == curthread ||
4419 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4420 ("running thread %p %p", p, td));
4422 umtx_thread_cleanup(td);
4424 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4430 * thread_exit() hook.
4433 umtx_thread_exit(struct thread *td)
4436 umtx_thread_cleanup(td);
4440 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res)
4443 #ifdef COMPAT_FREEBSD32
4448 #ifdef COMPAT_FREEBSD32
4449 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4450 error = fueword32((void *)ptr, &res32);
4456 error = fueword((void *)ptr, &res1);
4466 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list)
4468 #ifdef COMPAT_FREEBSD32
4469 struct umutex32 m32;
4471 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4472 memcpy(&m32, m, sizeof(m32));
4473 *rb_list = m32.m_rb_lnk;
4476 *rb_list = m->m_rb_lnk;
4480 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact)
4485 KASSERT(td->td_proc == curproc, ("need current vmspace"));
4486 error = copyin((void *)rbp, &m, sizeof(m));
4489 if (rb_list != NULL)
4490 umtx_read_rb_list(td, &m, rb_list);
4491 if ((m.m_flags & UMUTEX_ROBUST) == 0)
4493 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
4494 /* inact is cleared after unlock, allow the inconsistency */
4495 return (inact ? 0 : EINVAL);
4496 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
4500 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
4509 error = umtx_read_uptr(td, rb_list, &rbp);
4510 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
4511 if (rbp == *rb_inact) {
4516 error = umtx_handle_rb(td, rbp, &rbp, inact);
4518 if (i == umtx_max_rb && umtx_verbose_rb) {
4519 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
4520 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
4522 if (error != 0 && umtx_verbose_rb) {
4523 uprintf("comm %s pid %d: handling %srb error %d\n",
4524 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
4529 * Clean up umtx data.
4532 umtx_thread_cleanup(struct thread *td)
4539 * Disown pi mutexes.
4543 mtx_lock(&umtx_lock);
4544 uq->uq_inherited_pri = PRI_MAX;
4545 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
4546 pi->pi_owner = NULL;
4547 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
4549 mtx_unlock(&umtx_lock);
4551 sched_lend_user_prio(td, PRI_MAX);
4556 * Handle terminated robust mutexes. Must be done after
4557 * robust pi disown, otherwise unlock could see unowned
4560 rb_inact = td->td_rb_inact;
4562 (void)umtx_read_uptr(td, rb_inact, &rb_inact);
4563 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "");
4564 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ");
4566 (void)umtx_handle_rb(td, rb_inact, NULL, true);