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,
243 "Maximum number of robust mutexes allowed for each thread");
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 | CTLFLAG_MPSAFE, 0,
252 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
253 &umtx_pi_allocated, 0, "Allocated umtx_pi");
254 static int umtx_verbose_rb = 1;
255 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
259 #ifdef UMTX_PROFILING
260 static long max_length;
261 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
262 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
266 static void abs_timeout_update(struct abs_timeout *timo);
268 static void umtx_shm_init(void);
269 static void umtxq_sysinit(void *);
270 static void umtxq_hash(struct umtx_key *key);
271 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
272 static void umtxq_lock(struct umtx_key *key);
273 static void umtxq_unlock(struct umtx_key *key);
274 static void umtxq_busy(struct umtx_key *key);
275 static void umtxq_unbusy(struct umtx_key *key);
276 static void umtxq_insert_queue(struct umtx_q *uq, int q);
277 static void umtxq_remove_queue(struct umtx_q *uq, int q);
278 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *);
279 static int umtxq_count(struct umtx_key *key);
280 static struct umtx_pi *umtx_pi_alloc(int);
281 static void umtx_pi_free(struct umtx_pi *pi);
282 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
284 static void umtx_thread_cleanup(struct thread *td);
285 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
286 struct image_params *imgp __unused);
287 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
289 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
290 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
291 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
293 static struct mtx umtx_lock;
295 #ifdef UMTX_PROFILING
297 umtx_init_profiling(void)
299 struct sysctl_oid *chain_oid;
303 for (i = 0; i < UMTX_CHAINS; ++i) {
304 snprintf(chain_name, sizeof(chain_name), "%d", i);
305 chain_oid = SYSCTL_ADD_NODE(NULL,
306 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
307 chain_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
309 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
310 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
311 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
312 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
317 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
321 struct umtxq_chain *uc;
322 u_int fract, i, j, tot, whole;
323 u_int sf0, sf1, sf2, sf3, sf4;
324 u_int si0, si1, si2, si3, si4;
325 u_int sw0, sw1, sw2, sw3, sw4;
327 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
328 for (i = 0; i < 2; i++) {
330 for (j = 0; j < UMTX_CHAINS; ++j) {
331 uc = &umtxq_chains[i][j];
332 mtx_lock(&uc->uc_lock);
333 tot += uc->max_length;
334 mtx_unlock(&uc->uc_lock);
337 sbuf_printf(&sb, "%u) Empty ", i);
339 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
340 si0 = si1 = si2 = si3 = si4 = 0;
341 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
342 for (j = 0; j < UMTX_CHAINS; j++) {
343 uc = &umtxq_chains[i][j];
344 mtx_lock(&uc->uc_lock);
345 whole = uc->max_length * 100;
346 mtx_unlock(&uc->uc_lock);
347 fract = (whole % tot) * 100;
348 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
352 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
357 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
362 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
367 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
374 sbuf_printf(&sb, "queue %u:\n", i);
375 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
377 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
379 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
381 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
383 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
389 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
395 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
397 struct umtxq_chain *uc;
402 error = sysctl_handle_int(oidp, &clear, 0, req);
403 if (error != 0 || req->newptr == NULL)
407 for (i = 0; i < 2; ++i) {
408 for (j = 0; j < UMTX_CHAINS; ++j) {
409 uc = &umtxq_chains[i][j];
410 mtx_lock(&uc->uc_lock);
413 mtx_unlock(&uc->uc_lock);
420 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
421 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
422 sysctl_debug_umtx_chains_clear, "I",
423 "Clear umtx chains statistics");
424 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
425 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
426 sysctl_debug_umtx_chains_peaks, "A",
427 "Highest peaks in chains max length");
431 umtxq_sysinit(void *arg __unused)
435 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
436 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
437 for (i = 0; i < 2; ++i) {
438 for (j = 0; j < UMTX_CHAINS; ++j) {
439 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
440 MTX_DEF | MTX_DUPOK);
441 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
442 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
443 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
444 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
445 umtxq_chains[i][j].uc_busy = 0;
446 umtxq_chains[i][j].uc_waiters = 0;
447 #ifdef UMTX_PROFILING
448 umtxq_chains[i][j].length = 0;
449 umtxq_chains[i][j].max_length = 0;
453 #ifdef UMTX_PROFILING
454 umtx_init_profiling();
456 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
457 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
458 EVENTHANDLER_PRI_ANY);
467 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
468 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
470 TAILQ_INIT(&uq->uq_spare_queue->head);
471 TAILQ_INIT(&uq->uq_pi_contested);
472 uq->uq_inherited_pri = PRI_MAX;
477 umtxq_free(struct umtx_q *uq)
480 MPASS(uq->uq_spare_queue != NULL);
481 free(uq->uq_spare_queue, M_UMTX);
486 umtxq_hash(struct umtx_key *key)
490 n = (uintptr_t)key->info.both.a + key->info.both.b;
491 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
494 static inline struct umtxq_chain *
495 umtxq_getchain(struct umtx_key *key)
498 if (key->type <= TYPE_SEM)
499 return (&umtxq_chains[1][key->hash]);
500 return (&umtxq_chains[0][key->hash]);
507 umtxq_lock(struct umtx_key *key)
509 struct umtxq_chain *uc;
511 uc = umtxq_getchain(key);
512 mtx_lock(&uc->uc_lock);
519 umtxq_unlock(struct umtx_key *key)
521 struct umtxq_chain *uc;
523 uc = umtxq_getchain(key);
524 mtx_unlock(&uc->uc_lock);
528 * Set chain to busy state when following operation
529 * may be blocked (kernel mutex can not be used).
532 umtxq_busy(struct umtx_key *key)
534 struct umtxq_chain *uc;
536 uc = umtxq_getchain(key);
537 mtx_assert(&uc->uc_lock, MA_OWNED);
541 int count = BUSY_SPINS;
544 while (uc->uc_busy && --count > 0)
550 while (uc->uc_busy) {
552 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
563 umtxq_unbusy(struct umtx_key *key)
565 struct umtxq_chain *uc;
567 uc = umtxq_getchain(key);
568 mtx_assert(&uc->uc_lock, MA_OWNED);
569 KASSERT(uc->uc_busy != 0, ("not busy"));
576 umtxq_unbusy_unlocked(struct umtx_key *key)
584 static struct umtxq_queue *
585 umtxq_queue_lookup(struct umtx_key *key, int q)
587 struct umtxq_queue *uh;
588 struct umtxq_chain *uc;
590 uc = umtxq_getchain(key);
591 UMTXQ_LOCKED_ASSERT(uc);
592 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
593 if (umtx_key_match(&uh->key, key))
601 umtxq_insert_queue(struct umtx_q *uq, int q)
603 struct umtxq_queue *uh;
604 struct umtxq_chain *uc;
606 uc = umtxq_getchain(&uq->uq_key);
607 UMTXQ_LOCKED_ASSERT(uc);
608 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
609 uh = umtxq_queue_lookup(&uq->uq_key, q);
611 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
613 uh = uq->uq_spare_queue;
614 uh->key = uq->uq_key;
615 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
616 #ifdef UMTX_PROFILING
618 if (uc->length > uc->max_length) {
619 uc->max_length = uc->length;
620 if (uc->max_length > max_length)
621 max_length = uc->max_length;
625 uq->uq_spare_queue = NULL;
627 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
629 uq->uq_flags |= UQF_UMTXQ;
630 uq->uq_cur_queue = uh;
635 umtxq_remove_queue(struct umtx_q *uq, int q)
637 struct umtxq_chain *uc;
638 struct umtxq_queue *uh;
640 uc = umtxq_getchain(&uq->uq_key);
641 UMTXQ_LOCKED_ASSERT(uc);
642 if (uq->uq_flags & UQF_UMTXQ) {
643 uh = uq->uq_cur_queue;
644 TAILQ_REMOVE(&uh->head, uq, uq_link);
646 uq->uq_flags &= ~UQF_UMTXQ;
647 if (TAILQ_EMPTY(&uh->head)) {
648 KASSERT(uh->length == 0,
649 ("inconsistent umtxq_queue length"));
650 #ifdef UMTX_PROFILING
653 LIST_REMOVE(uh, link);
655 uh = LIST_FIRST(&uc->uc_spare_queue);
656 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
657 LIST_REMOVE(uh, link);
659 uq->uq_spare_queue = uh;
660 uq->uq_cur_queue = NULL;
665 * Check if there are multiple waiters
668 umtxq_count(struct umtx_key *key)
670 struct umtxq_queue *uh;
672 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
673 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
680 * Check if there are multiple PI waiters and returns first
684 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
686 struct umtxq_queue *uh;
689 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
690 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
692 *first = TAILQ_FIRST(&uh->head);
699 * Wake up threads waiting on an userland object.
703 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
705 struct umtxq_queue *uh;
710 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
711 uh = umtxq_queue_lookup(key, q);
713 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
714 umtxq_remove_queue(uq, q);
724 * Wake up specified thread.
727 umtxq_signal_thread(struct umtx_q *uq)
730 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
736 tstohz(const struct timespec *tsp)
740 TIMESPEC_TO_TIMEVAL(&tv, tsp);
745 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
746 const struct timespec *timeout)
749 timo->clockid = clockid;
751 timo->is_abs_real = false;
752 abs_timeout_update(timo);
753 timespecadd(&timo->cur, timeout, &timo->end);
755 timo->end = *timeout;
756 timo->is_abs_real = clockid == CLOCK_REALTIME ||
757 clockid == CLOCK_REALTIME_FAST ||
758 clockid == CLOCK_REALTIME_PRECISE;
760 * If is_abs_real, umtxq_sleep will read the clock
761 * after setting td_rtcgen; otherwise, read it here.
763 if (!timo->is_abs_real) {
764 abs_timeout_update(timo);
770 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
773 abs_timeout_init(timo, umtxtime->_clockid,
774 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
778 abs_timeout_update(struct abs_timeout *timo)
781 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
785 abs_timeout_gethz(struct abs_timeout *timo)
789 if (timespeccmp(&timo->end, &timo->cur, <=))
791 timespecsub(&timo->end, &timo->cur, &tts);
792 return (tstohz(&tts));
796 umtx_unlock_val(uint32_t flags, bool rb)
800 return (UMUTEX_RB_OWNERDEAD);
801 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
802 return (UMUTEX_RB_NOTRECOV);
804 return (UMUTEX_UNOWNED);
809 * Put thread into sleep state, before sleeping, check if
810 * thread was removed from umtx queue.
813 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
815 struct umtxq_chain *uc;
818 if (abstime != NULL && abstime->is_abs_real) {
819 curthread->td_rtcgen = atomic_load_acq_int(&rtc_generation);
820 abs_timeout_update(abstime);
823 uc = umtxq_getchain(&uq->uq_key);
824 UMTXQ_LOCKED_ASSERT(uc);
826 if (!(uq->uq_flags & UQF_UMTXQ)) {
830 if (abstime != NULL) {
831 timo = abs_timeout_gethz(abstime);
838 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
839 if (error == EINTR || error == ERESTART) {
840 umtxq_lock(&uq->uq_key);
843 if (abstime != NULL) {
844 if (abstime->is_abs_real)
845 curthread->td_rtcgen =
846 atomic_load_acq_int(&rtc_generation);
847 abs_timeout_update(abstime);
849 umtxq_lock(&uq->uq_key);
852 curthread->td_rtcgen = 0;
857 * Convert userspace address into unique logical address.
860 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
862 struct thread *td = curthread;
864 vm_map_entry_t entry;
870 if (share == THREAD_SHARE) {
872 key->info.private.vs = td->td_proc->p_vmspace;
873 key->info.private.addr = (uintptr_t)addr;
875 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
876 map = &td->td_proc->p_vmspace->vm_map;
877 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
878 &entry, &key->info.shared.object, &pindex, &prot,
879 &wired) != KERN_SUCCESS) {
883 if ((share == PROCESS_SHARE) ||
884 (share == AUTO_SHARE &&
885 VM_INHERIT_SHARE == entry->inheritance)) {
887 key->info.shared.offset = (vm_offset_t)addr -
888 entry->start + entry->offset;
889 vm_object_reference(key->info.shared.object);
892 key->info.private.vs = td->td_proc->p_vmspace;
893 key->info.private.addr = (uintptr_t)addr;
895 vm_map_lookup_done(map, entry);
906 umtx_key_release(struct umtx_key *key)
909 vm_object_deallocate(key->info.shared.object);
913 * Fetch and compare value, sleep on the address if value is not changed.
916 do_wait(struct thread *td, void *addr, u_long id,
917 struct _umtx_time *timeout, int compat32, int is_private)
919 struct abs_timeout timo;
926 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
927 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
931 abs_timeout_init2(&timo, timeout);
933 umtxq_lock(&uq->uq_key);
935 umtxq_unlock(&uq->uq_key);
937 error = fueword(addr, &tmp);
941 error = fueword32(addr, &tmp32);
947 umtxq_lock(&uq->uq_key);
950 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
952 if ((uq->uq_flags & UQF_UMTXQ) == 0)
956 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
959 umtxq_unlock(&uq->uq_key);
960 umtx_key_release(&uq->uq_key);
961 if (error == ERESTART)
967 * Wake up threads sleeping on the specified address.
970 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
975 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
976 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
979 umtxq_signal(&key, n_wake);
981 umtx_key_release(&key);
986 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
989 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
990 struct _umtx_time *timeout, int mode)
992 struct abs_timeout timo;
994 uint32_t owner, old, id;
1000 if (timeout != NULL)
1001 abs_timeout_init2(&timo, timeout);
1004 * Care must be exercised when dealing with umtx structure. It
1005 * can fault on any access.
1008 rv = fueword32(&m->m_owner, &owner);
1011 if (mode == _UMUTEX_WAIT) {
1012 if (owner == UMUTEX_UNOWNED ||
1013 owner == UMUTEX_CONTESTED ||
1014 owner == UMUTEX_RB_OWNERDEAD ||
1015 owner == UMUTEX_RB_NOTRECOV)
1019 * Robust mutex terminated. Kernel duty is to
1020 * return EOWNERDEAD to the userspace. The
1021 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1022 * by the common userspace code.
1024 if (owner == UMUTEX_RB_OWNERDEAD) {
1025 rv = casueword32(&m->m_owner,
1026 UMUTEX_RB_OWNERDEAD, &owner,
1027 id | UMUTEX_CONTESTED);
1031 MPASS(owner == UMUTEX_RB_OWNERDEAD);
1032 return (EOWNERDEAD); /* success */
1035 rv = thread_check_susp(td, false);
1040 if (owner == UMUTEX_RB_NOTRECOV)
1041 return (ENOTRECOVERABLE);
1044 * Try the uncontested case. This should be
1047 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1049 /* The address was invalid. */
1053 /* The acquire succeeded. */
1055 MPASS(owner == UMUTEX_UNOWNED);
1060 * If no one owns it but it is contested try
1064 if (owner == UMUTEX_CONTESTED) {
1065 rv = casueword32(&m->m_owner,
1066 UMUTEX_CONTESTED, &owner,
1067 id | UMUTEX_CONTESTED);
1068 /* The address was invalid. */
1072 MPASS(owner == UMUTEX_CONTESTED);
1076 rv = thread_check_susp(td, false);
1082 * If this failed the lock has
1088 /* rv == 1 but not contested, likely store failure */
1089 rv = thread_check_susp(td, false);
1094 if (mode == _UMUTEX_TRY)
1098 * If we caught a signal, we have retried and now
1104 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1105 GET_SHARE(flags), &uq->uq_key)) != 0)
1108 umtxq_lock(&uq->uq_key);
1109 umtxq_busy(&uq->uq_key);
1111 umtxq_unlock(&uq->uq_key);
1114 * Set the contested bit so that a release in user space
1115 * knows to use the system call for unlock. If this fails
1116 * either some one else has acquired the lock or it has been
1119 rv = casueword32(&m->m_owner, owner, &old,
1120 owner | UMUTEX_CONTESTED);
1122 /* The address was invalid or casueword failed to store. */
1123 if (rv == -1 || rv == 1) {
1124 umtxq_lock(&uq->uq_key);
1126 umtxq_unbusy(&uq->uq_key);
1127 umtxq_unlock(&uq->uq_key);
1128 umtx_key_release(&uq->uq_key);
1132 rv = thread_check_susp(td, false);
1140 * We set the contested bit, sleep. Otherwise the lock changed
1141 * and we need to retry or we lost a race to the thread
1142 * unlocking the umtx.
1144 umtxq_lock(&uq->uq_key);
1145 umtxq_unbusy(&uq->uq_key);
1146 MPASS(old == owner);
1147 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1150 umtxq_unlock(&uq->uq_key);
1151 umtx_key_release(&uq->uq_key);
1154 error = thread_check_susp(td, false);
1161 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1164 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1166 struct umtx_key key;
1167 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);
1189 error = thread_check_susp(td, false);
1194 MPASS(old == owner);
1198 /* We should only ever be in here for contested locks */
1199 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1205 count = umtxq_count(&key);
1209 * When unlocking the umtx, it must be marked as unowned if
1210 * there is zero or one thread only waiting for it.
1211 * Otherwise, it must be marked as contested.
1214 newlock |= UMUTEX_CONTESTED;
1215 error = casueword32(&m->m_owner, owner, &old, newlock);
1217 umtxq_signal(&key, 1);
1220 umtx_key_release(&key);
1226 error = thread_check_susp(td, false);
1235 * Check if the mutex is available and wake up a waiter,
1236 * only for simple mutex.
1239 do_wake_umutex(struct thread *td, struct umutex *m)
1241 struct umtx_key key;
1248 error = fueword32(&m->m_owner, &owner);
1252 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1253 owner != UMUTEX_RB_NOTRECOV)
1256 error = fueword32(&m->m_flags, &flags);
1260 /* We should only ever be in here for contested locks */
1261 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1267 count = umtxq_count(&key);
1270 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1271 owner != UMUTEX_RB_NOTRECOV) {
1272 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1276 } else if (error == 1) {
1280 umtx_key_release(&key);
1281 error = thread_check_susp(td, false);
1289 if (error == 0 && count != 0) {
1290 MPASS((owner & ~UMUTEX_CONTESTED) == 0 ||
1291 owner == UMUTEX_RB_OWNERDEAD ||
1292 owner == UMUTEX_RB_NOTRECOV);
1293 umtxq_signal(&key, 1);
1297 umtx_key_release(&key);
1302 * Check if the mutex has waiters and tries to fix contention bit.
1305 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1307 struct umtx_key key;
1308 uint32_t owner, old;
1313 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1317 type = TYPE_NORMAL_UMUTEX;
1319 case UMUTEX_PRIO_INHERIT:
1320 type = TYPE_PI_UMUTEX;
1322 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1323 type = TYPE_PI_ROBUST_UMUTEX;
1325 case UMUTEX_PRIO_PROTECT:
1326 type = TYPE_PP_UMUTEX;
1328 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1329 type = TYPE_PP_ROBUST_UMUTEX;
1334 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1340 count = umtxq_count(&key);
1343 error = fueword32(&m->m_owner, &owner);
1348 * Only repair contention bit if there is a waiter, this means
1349 * the mutex is still being referenced by userland code,
1350 * otherwise don't update any memory.
1352 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0 &&
1353 (count > 1 || (count == 1 && (owner & ~UMUTEX_CONTESTED) != 0))) {
1354 error = casueword32(&m->m_owner, owner, &old,
1355 owner | UMUTEX_CONTESTED);
1361 MPASS(old == owner);
1365 error = thread_check_susp(td, false);
1369 if (error == EFAULT) {
1370 umtxq_signal(&key, INT_MAX);
1371 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1372 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1373 umtxq_signal(&key, 1);
1376 umtx_key_release(&key);
1380 static inline struct umtx_pi *
1381 umtx_pi_alloc(int flags)
1385 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1386 TAILQ_INIT(&pi->pi_blocked);
1387 atomic_add_int(&umtx_pi_allocated, 1);
1392 umtx_pi_free(struct umtx_pi *pi)
1394 uma_zfree(umtx_pi_zone, pi);
1395 atomic_add_int(&umtx_pi_allocated, -1);
1399 * Adjust the thread's position on a pi_state after its priority has been
1403 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1405 struct umtx_q *uq, *uq1, *uq2;
1408 mtx_assert(&umtx_lock, MA_OWNED);
1415 * Check if the thread needs to be moved on the blocked chain.
1416 * It needs to be moved if either its priority is lower than
1417 * the previous thread or higher than the next thread.
1419 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1420 uq2 = TAILQ_NEXT(uq, uq_lockq);
1421 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1422 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1424 * Remove thread from blocked chain and determine where
1425 * it should be moved to.
1427 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1428 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1429 td1 = uq1->uq_thread;
1430 MPASS(td1->td_proc->p_magic == P_MAGIC);
1431 if (UPRI(td1) > UPRI(td))
1436 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1438 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1443 static struct umtx_pi *
1444 umtx_pi_next(struct umtx_pi *pi)
1446 struct umtx_q *uq_owner;
1448 if (pi->pi_owner == NULL)
1450 uq_owner = pi->pi_owner->td_umtxq;
1451 if (uq_owner == NULL)
1453 return (uq_owner->uq_pi_blocked);
1457 * Floyd's Cycle-Finding Algorithm.
1460 umtx_pi_check_loop(struct umtx_pi *pi)
1462 struct umtx_pi *pi1; /* fast iterator */
1464 mtx_assert(&umtx_lock, MA_OWNED);
1469 pi = umtx_pi_next(pi);
1472 pi1 = umtx_pi_next(pi1);
1475 pi1 = umtx_pi_next(pi1);
1485 * Propagate priority when a thread is blocked on POSIX
1489 umtx_propagate_priority(struct thread *td)
1495 mtx_assert(&umtx_lock, MA_OWNED);
1498 pi = uq->uq_pi_blocked;
1501 if (umtx_pi_check_loop(pi))
1506 if (td == NULL || td == curthread)
1509 MPASS(td->td_proc != NULL);
1510 MPASS(td->td_proc->p_magic == P_MAGIC);
1513 if (td->td_lend_user_pri > pri)
1514 sched_lend_user_prio(td, pri);
1522 * Pick up the lock that td is blocked on.
1525 pi = uq->uq_pi_blocked;
1528 /* Resort td on the list if needed. */
1529 umtx_pi_adjust_thread(pi, td);
1534 * Unpropagate priority for a PI mutex when a thread blocked on
1535 * it is interrupted by signal or resumed by others.
1538 umtx_repropagate_priority(struct umtx_pi *pi)
1540 struct umtx_q *uq, *uq_owner;
1541 struct umtx_pi *pi2;
1544 mtx_assert(&umtx_lock, MA_OWNED);
1546 if (umtx_pi_check_loop(pi))
1548 while (pi != NULL && pi->pi_owner != NULL) {
1550 uq_owner = pi->pi_owner->td_umtxq;
1552 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1553 uq = TAILQ_FIRST(&pi2->pi_blocked);
1555 if (pri > UPRI(uq->uq_thread))
1556 pri = UPRI(uq->uq_thread);
1560 if (pri > uq_owner->uq_inherited_pri)
1561 pri = uq_owner->uq_inherited_pri;
1562 thread_lock(pi->pi_owner);
1563 sched_lend_user_prio(pi->pi_owner, pri);
1564 thread_unlock(pi->pi_owner);
1565 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1566 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1571 * Insert a PI mutex into owned list.
1574 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1576 struct umtx_q *uq_owner;
1578 uq_owner = owner->td_umtxq;
1579 mtx_assert(&umtx_lock, MA_OWNED);
1580 MPASS(pi->pi_owner == NULL);
1581 pi->pi_owner = owner;
1582 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1586 * Disown a PI mutex, and remove it from the owned list.
1589 umtx_pi_disown(struct umtx_pi *pi)
1592 mtx_assert(&umtx_lock, MA_OWNED);
1593 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1594 pi->pi_owner = NULL;
1598 * Claim ownership of a PI mutex.
1601 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1606 mtx_lock(&umtx_lock);
1607 if (pi->pi_owner == owner) {
1608 mtx_unlock(&umtx_lock);
1612 if (pi->pi_owner != NULL) {
1614 * userland may have already messed the mutex, sigh.
1616 mtx_unlock(&umtx_lock);
1619 umtx_pi_setowner(pi, owner);
1620 uq = TAILQ_FIRST(&pi->pi_blocked);
1622 pri = UPRI(uq->uq_thread);
1624 if (pri < UPRI(owner))
1625 sched_lend_user_prio(owner, pri);
1626 thread_unlock(owner);
1628 mtx_unlock(&umtx_lock);
1633 * Adjust a thread's order position in its blocked PI mutex,
1634 * this may result new priority propagating process.
1637 umtx_pi_adjust(struct thread *td, u_char oldpri)
1643 mtx_lock(&umtx_lock);
1645 * Pick up the lock that td is blocked on.
1647 pi = uq->uq_pi_blocked;
1649 umtx_pi_adjust_thread(pi, td);
1650 umtx_repropagate_priority(pi);
1652 mtx_unlock(&umtx_lock);
1656 * Sleep on a PI mutex.
1659 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
1660 const char *wmesg, struct abs_timeout *timo, bool shared)
1662 struct thread *td, *td1;
1666 struct umtxq_chain *uc;
1668 uc = umtxq_getchain(&pi->pi_key);
1672 KASSERT(td == curthread, ("inconsistent uq_thread"));
1673 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
1674 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1676 mtx_lock(&umtx_lock);
1677 if (pi->pi_owner == NULL) {
1678 mtx_unlock(&umtx_lock);
1679 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
1680 mtx_lock(&umtx_lock);
1682 if (pi->pi_owner == NULL)
1683 umtx_pi_setowner(pi, td1);
1684 PROC_UNLOCK(td1->td_proc);
1688 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1689 pri = UPRI(uq1->uq_thread);
1695 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1697 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1699 uq->uq_pi_blocked = pi;
1701 td->td_flags |= TDF_UPIBLOCKED;
1703 umtx_propagate_priority(td);
1704 mtx_unlock(&umtx_lock);
1705 umtxq_unbusy(&uq->uq_key);
1707 error = umtxq_sleep(uq, wmesg, timo);
1710 mtx_lock(&umtx_lock);
1711 uq->uq_pi_blocked = NULL;
1713 td->td_flags &= ~TDF_UPIBLOCKED;
1715 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1716 umtx_repropagate_priority(pi);
1717 mtx_unlock(&umtx_lock);
1718 umtxq_unlock(&uq->uq_key);
1724 * Add reference count for a PI mutex.
1727 umtx_pi_ref(struct umtx_pi *pi)
1730 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
1735 * Decrease reference count for a PI mutex, if the counter
1736 * is decreased to zero, its memory space is freed.
1739 umtx_pi_unref(struct umtx_pi *pi)
1741 struct umtxq_chain *uc;
1743 uc = umtxq_getchain(&pi->pi_key);
1744 UMTXQ_LOCKED_ASSERT(uc);
1745 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1746 if (--pi->pi_refcount == 0) {
1747 mtx_lock(&umtx_lock);
1748 if (pi->pi_owner != NULL)
1750 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1751 ("blocked queue not empty"));
1752 mtx_unlock(&umtx_lock);
1753 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1759 * Find a PI mutex in hash table.
1761 static struct umtx_pi *
1762 umtx_pi_lookup(struct umtx_key *key)
1764 struct umtxq_chain *uc;
1767 uc = umtxq_getchain(key);
1768 UMTXQ_LOCKED_ASSERT(uc);
1770 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1771 if (umtx_key_match(&pi->pi_key, key)) {
1779 * Insert a PI mutex into hash table.
1782 umtx_pi_insert(struct umtx_pi *pi)
1784 struct umtxq_chain *uc;
1786 uc = umtxq_getchain(&pi->pi_key);
1787 UMTXQ_LOCKED_ASSERT(uc);
1788 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1795 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1796 struct _umtx_time *timeout, int try)
1798 struct abs_timeout timo;
1800 struct umtx_pi *pi, *new_pi;
1801 uint32_t id, old_owner, owner, old;
1807 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
1808 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
1812 if (timeout != NULL)
1813 abs_timeout_init2(&timo, timeout);
1815 umtxq_lock(&uq->uq_key);
1816 pi = umtx_pi_lookup(&uq->uq_key);
1818 new_pi = umtx_pi_alloc(M_NOWAIT);
1819 if (new_pi == NULL) {
1820 umtxq_unlock(&uq->uq_key);
1821 new_pi = umtx_pi_alloc(M_WAITOK);
1822 umtxq_lock(&uq->uq_key);
1823 pi = umtx_pi_lookup(&uq->uq_key);
1825 umtx_pi_free(new_pi);
1829 if (new_pi != NULL) {
1830 new_pi->pi_key = uq->uq_key;
1831 umtx_pi_insert(new_pi);
1836 umtxq_unlock(&uq->uq_key);
1839 * Care must be exercised when dealing with umtx structure. It
1840 * can fault on any access.
1844 * Try the uncontested case. This should be done in userland.
1846 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
1847 /* The address was invalid. */
1852 /* The acquire succeeded. */
1854 MPASS(owner == UMUTEX_UNOWNED);
1859 if (owner == UMUTEX_RB_NOTRECOV) {
1860 error = ENOTRECOVERABLE;
1865 * Avoid overwriting a possible error from sleep due
1866 * to the pending signal with suspension check result.
1869 error = thread_check_susp(td, true);
1874 /* If no one owns it but it is contested try to acquire it. */
1875 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
1877 rv = casueword32(&m->m_owner, owner, &owner,
1878 id | UMUTEX_CONTESTED);
1879 /* The address was invalid. */
1886 error = thread_check_susp(td, true);
1892 * If this failed the lock could
1899 MPASS(owner == old_owner);
1900 umtxq_lock(&uq->uq_key);
1901 umtxq_busy(&uq->uq_key);
1902 error = umtx_pi_claim(pi, td);
1903 umtxq_unbusy(&uq->uq_key);
1904 umtxq_unlock(&uq->uq_key);
1907 * Since we're going to return an
1908 * error, restore the m_owner to its
1909 * previous, unowned state to avoid
1910 * compounding the problem.
1912 (void)casuword32(&m->m_owner,
1913 id | UMUTEX_CONTESTED, old_owner);
1915 if (error == 0 && old_owner == UMUTEX_RB_OWNERDEAD)
1920 if ((owner & ~UMUTEX_CONTESTED) == id) {
1931 * If we caught a signal, we have retried and now
1937 umtxq_lock(&uq->uq_key);
1938 umtxq_busy(&uq->uq_key);
1939 umtxq_unlock(&uq->uq_key);
1942 * Set the contested bit so that a release in user space
1943 * knows to use the system call for unlock. If this fails
1944 * either some one else has acquired the lock or it has been
1947 rv = casueword32(&m->m_owner, owner, &old, owner |
1950 /* The address was invalid. */
1952 umtxq_unbusy_unlocked(&uq->uq_key);
1957 umtxq_unbusy_unlocked(&uq->uq_key);
1958 error = thread_check_susp(td, true);
1963 * The lock changed and we need to retry or we
1964 * lost a race to the thread unlocking the
1965 * umtx. Note that the UMUTEX_RB_OWNERDEAD
1966 * value for owner is impossible there.
1971 umtxq_lock(&uq->uq_key);
1973 /* We set the contested bit, sleep. */
1974 MPASS(old == owner);
1975 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
1976 "umtxpi", timeout == NULL ? NULL : &timo,
1977 (flags & USYNC_PROCESS_SHARED) != 0);
1981 error = thread_check_susp(td, false);
1986 umtxq_lock(&uq->uq_key);
1988 umtxq_unlock(&uq->uq_key);
1990 umtx_key_release(&uq->uq_key);
1995 * Unlock a PI mutex.
1998 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2000 struct umtx_key key;
2001 struct umtx_q *uq_first, *uq_first2, *uq_me;
2002 struct umtx_pi *pi, *pi2;
2003 uint32_t id, new_owner, old, owner;
2004 int count, error, pri;
2010 * Make sure we own this mtx.
2012 error = fueword32(&m->m_owner, &owner);
2016 if ((owner & ~UMUTEX_CONTESTED) != id)
2019 new_owner = umtx_unlock_val(flags, rb);
2021 /* This should be done in userland */
2022 if ((owner & UMUTEX_CONTESTED) == 0) {
2023 error = casueword32(&m->m_owner, owner, &old, new_owner);
2027 error = thread_check_susp(td, true);
2037 /* We should only ever be in here for contested locks */
2038 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2039 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2045 count = umtxq_count_pi(&key, &uq_first);
2046 if (uq_first != NULL) {
2047 mtx_lock(&umtx_lock);
2048 pi = uq_first->uq_pi_blocked;
2049 KASSERT(pi != NULL, ("pi == NULL?"));
2050 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2051 mtx_unlock(&umtx_lock);
2054 umtx_key_release(&key);
2055 /* userland messed the mutex */
2058 uq_me = td->td_umtxq;
2059 if (pi->pi_owner == td)
2061 /* get highest priority thread which is still sleeping. */
2062 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2063 while (uq_first != NULL &&
2064 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2065 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2068 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2069 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2070 if (uq_first2 != NULL) {
2071 if (pri > UPRI(uq_first2->uq_thread))
2072 pri = UPRI(uq_first2->uq_thread);
2076 sched_lend_user_prio(td, pri);
2078 mtx_unlock(&umtx_lock);
2080 umtxq_signal_thread(uq_first);
2082 pi = umtx_pi_lookup(&key);
2084 * A umtx_pi can exist if a signal or timeout removed the
2085 * last waiter from the umtxq, but there is still
2086 * a thread in do_lock_pi() holding the umtx_pi.
2090 * The umtx_pi can be unowned, such as when a thread
2091 * has just entered do_lock_pi(), allocated the
2092 * umtx_pi, and unlocked the umtxq.
2093 * If the current thread owns it, it must disown it.
2095 mtx_lock(&umtx_lock);
2096 if (pi->pi_owner == td)
2098 mtx_unlock(&umtx_lock);
2104 * When unlocking the umtx, it must be marked as unowned if
2105 * there is zero or one thread only waiting for it.
2106 * Otherwise, it must be marked as contested.
2110 new_owner |= UMUTEX_CONTESTED;
2112 error = casueword32(&m->m_owner, owner, &old, new_owner);
2114 error = thread_check_susp(td, false);
2118 umtxq_unbusy_unlocked(&key);
2119 umtx_key_release(&key);
2122 if (error == 0 && old != owner)
2131 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2132 struct _umtx_time *timeout, int try)
2134 struct abs_timeout timo;
2135 struct umtx_q *uq, *uq2;
2139 int error, pri, old_inherited_pri, su, rv;
2143 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2144 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2148 if (timeout != NULL)
2149 abs_timeout_init2(&timo, timeout);
2151 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2153 old_inherited_pri = uq->uq_inherited_pri;
2154 umtxq_lock(&uq->uq_key);
2155 umtxq_busy(&uq->uq_key);
2156 umtxq_unlock(&uq->uq_key);
2158 rv = fueword32(&m->m_ceilings[0], &ceiling);
2163 ceiling = RTP_PRIO_MAX - ceiling;
2164 if (ceiling > RTP_PRIO_MAX) {
2169 mtx_lock(&umtx_lock);
2170 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2171 mtx_unlock(&umtx_lock);
2175 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2176 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2178 if (uq->uq_inherited_pri < UPRI(td))
2179 sched_lend_user_prio(td, uq->uq_inherited_pri);
2182 mtx_unlock(&umtx_lock);
2184 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2185 id | UMUTEX_CONTESTED);
2186 /* The address was invalid. */
2192 MPASS(owner == UMUTEX_CONTESTED);
2197 if (owner == UMUTEX_RB_OWNERDEAD) {
2198 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2199 &owner, id | UMUTEX_CONTESTED);
2205 MPASS(owner == UMUTEX_RB_OWNERDEAD);
2206 error = EOWNERDEAD; /* success */
2211 * rv == 1, only check for suspension if we
2212 * did not already catched a signal. If we
2213 * get an error from the check, the same
2214 * condition is checked by the umtxq_sleep()
2215 * call below, so we should obliterate the
2216 * error to not skip the last loop iteration.
2219 error = thread_check_susp(td, false);
2228 } else if (owner == UMUTEX_RB_NOTRECOV) {
2229 error = ENOTRECOVERABLE;
2236 * If we caught a signal, we have retried and now
2242 umtxq_lock(&uq->uq_key);
2244 umtxq_unbusy(&uq->uq_key);
2245 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2248 umtxq_unlock(&uq->uq_key);
2250 mtx_lock(&umtx_lock);
2251 uq->uq_inherited_pri = old_inherited_pri;
2253 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2254 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2256 if (pri > UPRI(uq2->uq_thread))
2257 pri = UPRI(uq2->uq_thread);
2260 if (pri > uq->uq_inherited_pri)
2261 pri = uq->uq_inherited_pri;
2263 sched_lend_user_prio(td, pri);
2265 mtx_unlock(&umtx_lock);
2268 if (error != 0 && error != EOWNERDEAD) {
2269 mtx_lock(&umtx_lock);
2270 uq->uq_inherited_pri = old_inherited_pri;
2272 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2273 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2275 if (pri > UPRI(uq2->uq_thread))
2276 pri = UPRI(uq2->uq_thread);
2279 if (pri > uq->uq_inherited_pri)
2280 pri = uq->uq_inherited_pri;
2282 sched_lend_user_prio(td, pri);
2284 mtx_unlock(&umtx_lock);
2288 umtxq_unbusy_unlocked(&uq->uq_key);
2289 umtx_key_release(&uq->uq_key);
2294 * Unlock a PP mutex.
2297 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2299 struct umtx_key key;
2300 struct umtx_q *uq, *uq2;
2302 uint32_t id, owner, rceiling;
2303 int error, pri, new_inherited_pri, su;
2307 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2310 * Make sure we own this mtx.
2312 error = fueword32(&m->m_owner, &owner);
2316 if ((owner & ~UMUTEX_CONTESTED) != id)
2319 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2324 new_inherited_pri = PRI_MAX;
2326 rceiling = RTP_PRIO_MAX - rceiling;
2327 if (rceiling > RTP_PRIO_MAX)
2329 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2332 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2333 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2340 * For priority protected mutex, always set unlocked state
2341 * to UMUTEX_CONTESTED, so that userland always enters kernel
2342 * to lock the mutex, it is necessary because thread priority
2343 * has to be adjusted for such mutex.
2345 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2350 umtxq_signal(&key, 1);
2357 mtx_lock(&umtx_lock);
2359 uq->uq_inherited_pri = new_inherited_pri;
2361 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2362 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2364 if (pri > UPRI(uq2->uq_thread))
2365 pri = UPRI(uq2->uq_thread);
2368 if (pri > uq->uq_inherited_pri)
2369 pri = uq->uq_inherited_pri;
2371 sched_lend_user_prio(td, pri);
2373 mtx_unlock(&umtx_lock);
2375 umtx_key_release(&key);
2380 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2381 uint32_t *old_ceiling)
2384 uint32_t flags, id, owner, save_ceiling;
2387 error = fueword32(&m->m_flags, &flags);
2390 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2392 if (ceiling > RTP_PRIO_MAX)
2396 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2397 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2401 umtxq_lock(&uq->uq_key);
2402 umtxq_busy(&uq->uq_key);
2403 umtxq_unlock(&uq->uq_key);
2405 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2411 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2412 id | UMUTEX_CONTESTED);
2419 MPASS(owner == UMUTEX_CONTESTED);
2420 rv = suword32(&m->m_ceilings[0], ceiling);
2421 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2422 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2426 if ((owner & ~UMUTEX_CONTESTED) == id) {
2427 rv = suword32(&m->m_ceilings[0], ceiling);
2428 error = rv == 0 ? 0 : EFAULT;
2432 if (owner == UMUTEX_RB_OWNERDEAD) {
2435 } else if (owner == UMUTEX_RB_NOTRECOV) {
2436 error = ENOTRECOVERABLE;
2441 * If we caught a signal, we have retried and now
2448 * We set the contested bit, sleep. Otherwise the lock changed
2449 * and we need to retry or we lost a race to the thread
2450 * unlocking the umtx.
2452 umtxq_lock(&uq->uq_key);
2454 umtxq_unbusy(&uq->uq_key);
2455 error = umtxq_sleep(uq, "umtxpp", NULL);
2457 umtxq_unlock(&uq->uq_key);
2459 umtxq_lock(&uq->uq_key);
2461 umtxq_signal(&uq->uq_key, INT_MAX);
2462 umtxq_unbusy(&uq->uq_key);
2463 umtxq_unlock(&uq->uq_key);
2464 umtx_key_release(&uq->uq_key);
2465 if (error == 0 && old_ceiling != NULL) {
2466 rv = suword32(old_ceiling, save_ceiling);
2467 error = rv == 0 ? 0 : EFAULT;
2473 * Lock a userland POSIX mutex.
2476 do_lock_umutex(struct thread *td, struct umutex *m,
2477 struct _umtx_time *timeout, int mode)
2482 error = fueword32(&m->m_flags, &flags);
2486 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2488 error = do_lock_normal(td, m, flags, timeout, mode);
2490 case UMUTEX_PRIO_INHERIT:
2491 error = do_lock_pi(td, m, flags, timeout, mode);
2493 case UMUTEX_PRIO_PROTECT:
2494 error = do_lock_pp(td, m, flags, timeout, mode);
2499 if (timeout == NULL) {
2500 if (error == EINTR && mode != _UMUTEX_WAIT)
2503 /* Timed-locking is not restarted. */
2504 if (error == ERESTART)
2511 * Unlock a userland POSIX mutex.
2514 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2519 error = fueword32(&m->m_flags, &flags);
2523 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2525 return (do_unlock_normal(td, m, flags, rb));
2526 case UMUTEX_PRIO_INHERIT:
2527 return (do_unlock_pi(td, m, flags, rb));
2528 case UMUTEX_PRIO_PROTECT:
2529 return (do_unlock_pp(td, m, flags, rb));
2536 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2537 struct timespec *timeout, u_long wflags)
2539 struct abs_timeout timo;
2541 uint32_t flags, clockid, hasw;
2545 error = fueword32(&cv->c_flags, &flags);
2548 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2552 if ((wflags & CVWAIT_CLOCKID) != 0) {
2553 error = fueword32(&cv->c_clockid, &clockid);
2555 umtx_key_release(&uq->uq_key);
2558 if (clockid < CLOCK_REALTIME ||
2559 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2560 /* hmm, only HW clock id will work. */
2561 umtx_key_release(&uq->uq_key);
2565 clockid = CLOCK_REALTIME;
2568 umtxq_lock(&uq->uq_key);
2569 umtxq_busy(&uq->uq_key);
2571 umtxq_unlock(&uq->uq_key);
2574 * Set c_has_waiters to 1 before releasing user mutex, also
2575 * don't modify cache line when unnecessary.
2577 error = fueword32(&cv->c_has_waiters, &hasw);
2578 if (error == 0 && hasw == 0)
2579 suword32(&cv->c_has_waiters, 1);
2581 umtxq_unbusy_unlocked(&uq->uq_key);
2583 error = do_unlock_umutex(td, m, false);
2585 if (timeout != NULL)
2586 abs_timeout_init(&timo, clockid, (wflags & CVWAIT_ABSTIME) != 0,
2589 umtxq_lock(&uq->uq_key);
2591 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2595 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2599 * This must be timeout,interrupted by signal or
2600 * surprious wakeup, clear c_has_waiter flag when
2603 umtxq_busy(&uq->uq_key);
2604 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2605 int oldlen = uq->uq_cur_queue->length;
2608 umtxq_unlock(&uq->uq_key);
2609 suword32(&cv->c_has_waiters, 0);
2610 umtxq_lock(&uq->uq_key);
2613 umtxq_unbusy(&uq->uq_key);
2614 if (error == ERESTART)
2618 umtxq_unlock(&uq->uq_key);
2619 umtx_key_release(&uq->uq_key);
2624 * Signal a userland condition variable.
2627 do_cv_signal(struct thread *td, struct ucond *cv)
2629 struct umtx_key key;
2630 int error, cnt, nwake;
2633 error = fueword32(&cv->c_flags, &flags);
2636 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2640 cnt = umtxq_count(&key);
2641 nwake = umtxq_signal(&key, 1);
2644 error = suword32(&cv->c_has_waiters, 0);
2651 umtx_key_release(&key);
2656 do_cv_broadcast(struct thread *td, struct ucond *cv)
2658 struct umtx_key key;
2662 error = fueword32(&cv->c_flags, &flags);
2665 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2670 umtxq_signal(&key, INT_MAX);
2673 error = suword32(&cv->c_has_waiters, 0);
2677 umtxq_unbusy_unlocked(&key);
2679 umtx_key_release(&key);
2684 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
2685 struct _umtx_time *timeout)
2687 struct abs_timeout timo;
2689 uint32_t flags, wrflags;
2690 int32_t state, oldstate;
2691 int32_t blocked_readers;
2692 int error, error1, rv;
2695 error = fueword32(&rwlock->rw_flags, &flags);
2698 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2702 if (timeout != NULL)
2703 abs_timeout_init2(&timo, timeout);
2705 wrflags = URWLOCK_WRITE_OWNER;
2706 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2707 wrflags |= URWLOCK_WRITE_WAITERS;
2710 rv = fueword32(&rwlock->rw_state, &state);
2712 umtx_key_release(&uq->uq_key);
2716 /* try to lock it */
2717 while (!(state & wrflags)) {
2718 if (__predict_false(URWLOCK_READER_COUNT(state) ==
2719 URWLOCK_MAX_READERS)) {
2720 umtx_key_release(&uq->uq_key);
2723 rv = casueword32(&rwlock->rw_state, state,
2724 &oldstate, state + 1);
2726 umtx_key_release(&uq->uq_key);
2730 MPASS(oldstate == state);
2731 umtx_key_release(&uq->uq_key);
2734 error = thread_check_susp(td, true);
2743 /* grab monitor lock */
2744 umtxq_lock(&uq->uq_key);
2745 umtxq_busy(&uq->uq_key);
2746 umtxq_unlock(&uq->uq_key);
2749 * re-read the state, in case it changed between the try-lock above
2750 * and the check below
2752 rv = fueword32(&rwlock->rw_state, &state);
2756 /* set read contention bit */
2757 while (error == 0 && (state & wrflags) &&
2758 !(state & URWLOCK_READ_WAITERS)) {
2759 rv = casueword32(&rwlock->rw_state, state,
2760 &oldstate, state | URWLOCK_READ_WAITERS);
2766 MPASS(oldstate == state);
2770 error = thread_check_susp(td, false);
2775 umtxq_unbusy_unlocked(&uq->uq_key);
2779 /* state is changed while setting flags, restart */
2780 if (!(state & wrflags)) {
2781 umtxq_unbusy_unlocked(&uq->uq_key);
2782 error = thread_check_susp(td, true);
2790 * Contention bit is set, before sleeping, increase
2791 * read waiter count.
2793 rv = fueword32(&rwlock->rw_blocked_readers,
2796 umtxq_unbusy_unlocked(&uq->uq_key);
2800 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2802 while (state & wrflags) {
2803 umtxq_lock(&uq->uq_key);
2805 umtxq_unbusy(&uq->uq_key);
2807 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2810 umtxq_busy(&uq->uq_key);
2812 umtxq_unlock(&uq->uq_key);
2815 rv = fueword32(&rwlock->rw_state, &state);
2822 /* decrease read waiter count, and may clear read contention bit */
2823 rv = fueword32(&rwlock->rw_blocked_readers,
2826 umtxq_unbusy_unlocked(&uq->uq_key);
2830 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2831 if (blocked_readers == 1) {
2832 rv = fueword32(&rwlock->rw_state, &state);
2834 umtxq_unbusy_unlocked(&uq->uq_key);
2839 rv = casueword32(&rwlock->rw_state, state,
2840 &oldstate, state & ~URWLOCK_READ_WAITERS);
2846 MPASS(oldstate == state);
2850 error1 = thread_check_susp(td, false);
2859 umtxq_unbusy_unlocked(&uq->uq_key);
2863 umtx_key_release(&uq->uq_key);
2864 if (error == ERESTART)
2870 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2872 struct abs_timeout timo;
2875 int32_t state, oldstate;
2876 int32_t blocked_writers;
2877 int32_t blocked_readers;
2878 int error, error1, rv;
2881 error = fueword32(&rwlock->rw_flags, &flags);
2884 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2888 if (timeout != NULL)
2889 abs_timeout_init2(&timo, timeout);
2891 blocked_readers = 0;
2893 rv = fueword32(&rwlock->rw_state, &state);
2895 umtx_key_release(&uq->uq_key);
2898 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
2899 URWLOCK_READER_COUNT(state) == 0) {
2900 rv = casueword32(&rwlock->rw_state, state,
2901 &oldstate, state | URWLOCK_WRITE_OWNER);
2903 umtx_key_release(&uq->uq_key);
2907 MPASS(oldstate == state);
2908 umtx_key_release(&uq->uq_key);
2912 error = thread_check_susp(td, true);
2918 if ((state & (URWLOCK_WRITE_OWNER |
2919 URWLOCK_WRITE_WAITERS)) == 0 &&
2920 blocked_readers != 0) {
2921 umtxq_lock(&uq->uq_key);
2922 umtxq_busy(&uq->uq_key);
2923 umtxq_signal_queue(&uq->uq_key, INT_MAX,
2925 umtxq_unbusy(&uq->uq_key);
2926 umtxq_unlock(&uq->uq_key);
2932 /* grab monitor lock */
2933 umtxq_lock(&uq->uq_key);
2934 umtxq_busy(&uq->uq_key);
2935 umtxq_unlock(&uq->uq_key);
2938 * Re-read the state, in case it changed between the
2939 * try-lock above and the check below.
2941 rv = fueword32(&rwlock->rw_state, &state);
2945 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
2946 URWLOCK_READER_COUNT(state) != 0) &&
2947 (state & URWLOCK_WRITE_WAITERS) == 0) {
2948 rv = casueword32(&rwlock->rw_state, state,
2949 &oldstate, state | URWLOCK_WRITE_WAITERS);
2955 MPASS(oldstate == state);
2959 error = thread_check_susp(td, false);
2964 umtxq_unbusy_unlocked(&uq->uq_key);
2968 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
2969 URWLOCK_READER_COUNT(state) == 0) {
2970 umtxq_unbusy_unlocked(&uq->uq_key);
2971 error = thread_check_susp(td, false);
2977 rv = fueword32(&rwlock->rw_blocked_writers,
2980 umtxq_unbusy_unlocked(&uq->uq_key);
2984 suword32(&rwlock->rw_blocked_writers, blocked_writers + 1);
2986 while ((state & URWLOCK_WRITE_OWNER) ||
2987 URWLOCK_READER_COUNT(state) != 0) {
2988 umtxq_lock(&uq->uq_key);
2989 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2990 umtxq_unbusy(&uq->uq_key);
2992 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
2995 umtxq_busy(&uq->uq_key);
2996 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2997 umtxq_unlock(&uq->uq_key);
3000 rv = fueword32(&rwlock->rw_state, &state);
3007 rv = fueword32(&rwlock->rw_blocked_writers,
3010 umtxq_unbusy_unlocked(&uq->uq_key);
3014 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
3015 if (blocked_writers == 1) {
3016 rv = fueword32(&rwlock->rw_state, &state);
3018 umtxq_unbusy_unlocked(&uq->uq_key);
3023 rv = casueword32(&rwlock->rw_state, state,
3024 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3030 MPASS(oldstate == state);
3034 error1 = thread_check_susp(td, false);
3036 * We are leaving the URWLOCK_WRITE_WAITERS
3037 * behind, but this should not harm the
3046 rv = fueword32(&rwlock->rw_blocked_readers,
3049 umtxq_unbusy_unlocked(&uq->uq_key);
3054 blocked_readers = 0;
3056 umtxq_unbusy_unlocked(&uq->uq_key);
3059 umtx_key_release(&uq->uq_key);
3060 if (error == ERESTART)
3066 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3070 int32_t state, oldstate;
3071 int error, rv, q, count;
3074 error = fueword32(&rwlock->rw_flags, &flags);
3077 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3081 error = fueword32(&rwlock->rw_state, &state);
3086 if (state & URWLOCK_WRITE_OWNER) {
3088 rv = casueword32(&rwlock->rw_state, state,
3089 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3096 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3100 error = thread_check_susp(td, true);
3106 } else if (URWLOCK_READER_COUNT(state) != 0) {
3108 rv = casueword32(&rwlock->rw_state, state,
3109 &oldstate, state - 1);
3116 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3120 error = thread_check_susp(td, true);
3133 if (!(flags & URWLOCK_PREFER_READER)) {
3134 if (state & URWLOCK_WRITE_WAITERS) {
3136 q = UMTX_EXCLUSIVE_QUEUE;
3137 } else if (state & URWLOCK_READ_WAITERS) {
3139 q = UMTX_SHARED_QUEUE;
3142 if (state & URWLOCK_READ_WAITERS) {
3144 q = UMTX_SHARED_QUEUE;
3145 } else if (state & URWLOCK_WRITE_WAITERS) {
3147 q = UMTX_EXCLUSIVE_QUEUE;
3152 umtxq_lock(&uq->uq_key);
3153 umtxq_busy(&uq->uq_key);
3154 umtxq_signal_queue(&uq->uq_key, count, q);
3155 umtxq_unbusy(&uq->uq_key);
3156 umtxq_unlock(&uq->uq_key);
3159 umtx_key_release(&uq->uq_key);
3163 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3165 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3167 struct abs_timeout timo;
3169 uint32_t flags, count, count1;
3173 error = fueword32(&sem->_flags, &flags);
3176 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3180 if (timeout != NULL)
3181 abs_timeout_init2(&timo, timeout);
3184 umtxq_lock(&uq->uq_key);
3185 umtxq_busy(&uq->uq_key);
3187 umtxq_unlock(&uq->uq_key);
3188 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3190 rv1 = fueword32(&sem->_count, &count);
3191 if (rv == -1 || (rv == 0 && (rv1 == -1 || count != 0)) ||
3192 (rv == 1 && count1 == 0)) {
3193 umtxq_lock(&uq->uq_key);
3194 umtxq_unbusy(&uq->uq_key);
3196 umtxq_unlock(&uq->uq_key);
3198 rv = thread_check_susp(td, true);
3206 error = rv == -1 ? EFAULT : 0;
3209 umtxq_lock(&uq->uq_key);
3210 umtxq_unbusy(&uq->uq_key);
3212 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3214 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3218 /* A relative timeout cannot be restarted. */
3219 if (error == ERESTART && timeout != NULL &&
3220 (timeout->_flags & UMTX_ABSTIME) == 0)
3223 umtxq_unlock(&uq->uq_key);
3225 umtx_key_release(&uq->uq_key);
3230 * Signal a userland semaphore.
3233 do_sem_wake(struct thread *td, struct _usem *sem)
3235 struct umtx_key key;
3239 error = fueword32(&sem->_flags, &flags);
3242 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3246 cnt = umtxq_count(&key);
3249 * Check if count is greater than 0, this means the memory is
3250 * still being referenced by user code, so we can safely
3251 * update _has_waiters flag.
3255 error = suword32(&sem->_has_waiters, 0);
3260 umtxq_signal(&key, 1);
3264 umtx_key_release(&key);
3270 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3272 struct abs_timeout timo;
3274 uint32_t count, flags;
3278 flags = fuword32(&sem->_flags);
3279 if (timeout != NULL)
3280 abs_timeout_init2(&timo, timeout);
3283 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3286 umtxq_lock(&uq->uq_key);
3287 umtxq_busy(&uq->uq_key);
3289 umtxq_unlock(&uq->uq_key);
3290 rv = fueword32(&sem->_count, &count);
3292 umtxq_lock(&uq->uq_key);
3293 umtxq_unbusy(&uq->uq_key);
3295 umtxq_unlock(&uq->uq_key);
3296 umtx_key_release(&uq->uq_key);
3300 if (USEM_COUNT(count) != 0) {
3301 umtxq_lock(&uq->uq_key);
3302 umtxq_unbusy(&uq->uq_key);
3304 umtxq_unlock(&uq->uq_key);
3305 umtx_key_release(&uq->uq_key);
3308 if (count == USEM_HAS_WAITERS)
3310 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3313 umtxq_lock(&uq->uq_key);
3314 umtxq_unbusy(&uq->uq_key);
3316 umtxq_unlock(&uq->uq_key);
3317 umtx_key_release(&uq->uq_key);
3320 rv = thread_check_susp(td, true);
3325 umtxq_lock(&uq->uq_key);
3326 umtxq_unbusy(&uq->uq_key);
3328 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3330 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3334 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3335 /* A relative timeout cannot be restarted. */
3336 if (error == ERESTART)
3338 if (error == EINTR) {
3339 abs_timeout_update(&timo);
3340 timespecsub(&timo.end, &timo.cur,
3341 &timeout->_timeout);
3345 umtxq_unlock(&uq->uq_key);
3346 umtx_key_release(&uq->uq_key);
3351 * Signal a userland semaphore.
3354 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3356 struct umtx_key key;
3358 uint32_t count, flags;
3360 rv = fueword32(&sem->_flags, &flags);
3363 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3367 cnt = umtxq_count(&key);
3370 * If this was the last sleeping thread, clear the waiters
3375 rv = fueword32(&sem->_count, &count);
3376 while (rv != -1 && count & USEM_HAS_WAITERS) {
3377 rv = casueword32(&sem->_count, count, &count,
3378 count & ~USEM_HAS_WAITERS);
3380 rv = thread_check_susp(td, true);
3393 umtxq_signal(&key, 1);
3397 umtx_key_release(&key);
3402 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3406 error = copyin(addr, tsp, sizeof(struct timespec));
3408 if (tsp->tv_sec < 0 ||
3409 tsp->tv_nsec >= 1000000000 ||
3417 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3421 if (size <= sizeof(struct timespec)) {
3422 tp->_clockid = CLOCK_REALTIME;
3424 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3426 error = copyin(addr, tp, sizeof(struct _umtx_time));
3429 if (tp->_timeout.tv_sec < 0 ||
3430 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3436 __umtx_op_unimpl(struct thread *td, struct _umtx_op_args *uap)
3439 return (EOPNOTSUPP);
3443 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3445 struct _umtx_time timeout, *tm_p;
3448 if (uap->uaddr2 == NULL)
3451 error = umtx_copyin_umtx_time(
3452 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3457 return (do_wait(td, uap->obj, uap->val, tm_p, 0, 0));
3461 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3463 struct _umtx_time timeout, *tm_p;
3466 if (uap->uaddr2 == NULL)
3469 error = umtx_copyin_umtx_time(
3470 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3475 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3479 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3481 struct _umtx_time *tm_p, timeout;
3484 if (uap->uaddr2 == NULL)
3487 error = umtx_copyin_umtx_time(
3488 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3493 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3497 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3500 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3503 #define BATCH_SIZE 128
3505 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3507 char *uaddrs[BATCH_SIZE], **upp;
3508 int count, error, i, pos, tocopy;
3510 upp = (char **)uap->obj;
3512 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3514 tocopy = MIN(count, BATCH_SIZE);
3515 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3518 for (i = 0; i < tocopy; ++i)
3519 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3526 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3529 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3533 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3535 struct _umtx_time *tm_p, timeout;
3538 /* Allow a null timespec (wait forever). */
3539 if (uap->uaddr2 == NULL)
3542 error = umtx_copyin_umtx_time(
3543 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3548 return (do_lock_umutex(td, uap->obj, tm_p, 0));
3552 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3555 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
3559 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3561 struct _umtx_time *tm_p, timeout;
3564 /* Allow a null timespec (wait forever). */
3565 if (uap->uaddr2 == NULL)
3568 error = umtx_copyin_umtx_time(
3569 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3574 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
3578 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3581 return (do_wake_umutex(td, uap->obj));
3585 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3588 return (do_unlock_umutex(td, uap->obj, false));
3592 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3595 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
3599 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3601 struct timespec *ts, timeout;
3604 /* Allow a null timespec (wait forever). */
3605 if (uap->uaddr2 == NULL)
3608 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3613 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3617 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3620 return (do_cv_signal(td, uap->obj));
3624 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3627 return (do_cv_broadcast(td, uap->obj));
3631 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3633 struct _umtx_time timeout;
3636 /* Allow a null timespec (wait forever). */
3637 if (uap->uaddr2 == NULL) {
3638 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3640 error = umtx_copyin_umtx_time(uap->uaddr2,
3641 (size_t)uap->uaddr1, &timeout);
3644 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3650 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3652 struct _umtx_time timeout;
3655 /* Allow a null timespec (wait forever). */
3656 if (uap->uaddr2 == NULL) {
3657 error = do_rw_wrlock(td, uap->obj, 0);
3659 error = umtx_copyin_umtx_time(uap->uaddr2,
3660 (size_t)uap->uaddr1, &timeout);
3664 error = do_rw_wrlock(td, uap->obj, &timeout);
3670 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3673 return (do_rw_unlock(td, uap->obj));
3676 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3678 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3680 struct _umtx_time *tm_p, timeout;
3683 /* Allow a null timespec (wait forever). */
3684 if (uap->uaddr2 == NULL)
3687 error = umtx_copyin_umtx_time(
3688 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3693 return (do_sem_wait(td, uap->obj, tm_p));
3697 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3700 return (do_sem_wake(td, uap->obj));
3705 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3708 return (do_wake2_umutex(td, uap->obj, uap->val));
3712 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap)
3714 struct _umtx_time *tm_p, timeout;
3718 /* Allow a null timespec (wait forever). */
3719 if (uap->uaddr2 == NULL) {
3723 uasize = (size_t)uap->uaddr1;
3724 error = umtx_copyin_umtx_time(uap->uaddr2, uasize, &timeout);
3729 error = do_sem2_wait(td, uap->obj, tm_p);
3730 if (error == EINTR && uap->uaddr2 != NULL &&
3731 (timeout._flags & UMTX_ABSTIME) == 0 &&
3732 uasize >= sizeof(struct _umtx_time) + sizeof(struct timespec)) {
3733 error = copyout(&timeout._timeout,
3734 (struct _umtx_time *)uap->uaddr2 + 1,
3735 sizeof(struct timespec));
3745 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap)
3748 return (do_sem2_wake(td, uap->obj));
3751 #define USHM_OBJ_UMTX(o) \
3752 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
3754 #define USHMF_REG_LINKED 0x0001
3755 #define USHMF_OBJ_LINKED 0x0002
3756 struct umtx_shm_reg {
3757 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
3758 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
3759 struct umtx_key ushm_key;
3760 struct ucred *ushm_cred;
3761 struct shmfd *ushm_obj;
3766 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
3767 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
3769 static uma_zone_t umtx_shm_reg_zone;
3770 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
3771 static struct mtx umtx_shm_lock;
3772 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
3773 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
3775 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
3778 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
3780 struct umtx_shm_reg_head d;
3781 struct umtx_shm_reg *reg, *reg1;
3784 mtx_lock(&umtx_shm_lock);
3785 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
3786 mtx_unlock(&umtx_shm_lock);
3787 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
3788 TAILQ_REMOVE(&d, reg, ushm_reg_link);
3789 umtx_shm_free_reg(reg);
3793 static struct task umtx_shm_reg_delfree_task =
3794 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
3796 static struct umtx_shm_reg *
3797 umtx_shm_find_reg_locked(const struct umtx_key *key)
3799 struct umtx_shm_reg *reg;
3800 struct umtx_shm_reg_head *reg_head;
3802 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
3803 mtx_assert(&umtx_shm_lock, MA_OWNED);
3804 reg_head = &umtx_shm_registry[key->hash];
3805 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
3806 KASSERT(reg->ushm_key.shared,
3807 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
3808 if (reg->ushm_key.info.shared.object ==
3809 key->info.shared.object &&
3810 reg->ushm_key.info.shared.offset ==
3811 key->info.shared.offset) {
3812 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
3813 KASSERT(reg->ushm_refcnt > 0,
3814 ("reg %p refcnt 0 onlist", reg));
3815 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
3816 ("reg %p not linked", reg));
3824 static struct umtx_shm_reg *
3825 umtx_shm_find_reg(const struct umtx_key *key)
3827 struct umtx_shm_reg *reg;
3829 mtx_lock(&umtx_shm_lock);
3830 reg = umtx_shm_find_reg_locked(key);
3831 mtx_unlock(&umtx_shm_lock);
3836 umtx_shm_free_reg(struct umtx_shm_reg *reg)
3839 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
3840 crfree(reg->ushm_cred);
3841 shm_drop(reg->ushm_obj);
3842 uma_zfree(umtx_shm_reg_zone, reg);
3846 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
3850 mtx_assert(&umtx_shm_lock, MA_OWNED);
3851 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
3853 res = reg->ushm_refcnt == 0;
3855 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
3856 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
3857 reg, ushm_reg_link);
3858 reg->ushm_flags &= ~USHMF_REG_LINKED;
3860 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
3861 LIST_REMOVE(reg, ushm_obj_link);
3862 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
3869 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
3875 object = reg->ushm_obj->shm_object;
3876 VM_OBJECT_WLOCK(object);
3877 object->flags |= OBJ_UMTXDEAD;
3878 VM_OBJECT_WUNLOCK(object);
3880 mtx_lock(&umtx_shm_lock);
3881 dofree = umtx_shm_unref_reg_locked(reg, force);
3882 mtx_unlock(&umtx_shm_lock);
3884 umtx_shm_free_reg(reg);
3888 umtx_shm_object_init(vm_object_t object)
3891 LIST_INIT(USHM_OBJ_UMTX(object));
3895 umtx_shm_object_terminated(vm_object_t object)
3897 struct umtx_shm_reg *reg, *reg1;
3900 if (LIST_EMPTY(USHM_OBJ_UMTX(object)))
3904 mtx_lock(&umtx_shm_lock);
3905 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
3906 if (umtx_shm_unref_reg_locked(reg, true)) {
3907 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
3912 mtx_unlock(&umtx_shm_lock);
3914 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
3918 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
3919 struct umtx_shm_reg **res)
3921 struct umtx_shm_reg *reg, *reg1;
3925 reg = umtx_shm_find_reg(key);
3930 cred = td->td_ucred;
3931 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
3933 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
3934 reg->ushm_refcnt = 1;
3935 bcopy(key, ®->ushm_key, sizeof(*key));
3936 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR);
3937 reg->ushm_cred = crhold(cred);
3938 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
3940 umtx_shm_free_reg(reg);
3943 mtx_lock(&umtx_shm_lock);
3944 reg1 = umtx_shm_find_reg_locked(key);
3946 mtx_unlock(&umtx_shm_lock);
3947 umtx_shm_free_reg(reg);
3952 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
3953 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
3955 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
3956 mtx_unlock(&umtx_shm_lock);
3962 umtx_shm_alive(struct thread *td, void *addr)
3965 vm_map_entry_t entry;
3972 map = &td->td_proc->p_vmspace->vm_map;
3973 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
3974 &object, &pindex, &prot, &wired);
3975 if (res != KERN_SUCCESS)
3980 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
3981 vm_map_lookup_done(map, entry);
3990 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
3991 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
3992 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
3993 for (i = 0; i < nitems(umtx_shm_registry); i++)
3994 TAILQ_INIT(&umtx_shm_registry[i]);
3998 umtx_shm(struct thread *td, void *addr, u_int flags)
4000 struct umtx_key key;
4001 struct umtx_shm_reg *reg;
4005 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
4006 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
4008 if ((flags & UMTX_SHM_ALIVE) != 0)
4009 return (umtx_shm_alive(td, addr));
4010 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
4013 KASSERT(key.shared == 1, ("non-shared key"));
4014 if ((flags & UMTX_SHM_CREAT) != 0) {
4015 error = umtx_shm_create_reg(td, &key, ®);
4017 reg = umtx_shm_find_reg(&key);
4021 umtx_key_release(&key);
4024 KASSERT(reg != NULL, ("no reg"));
4025 if ((flags & UMTX_SHM_DESTROY) != 0) {
4026 umtx_shm_unref_reg(reg, true);
4030 error = mac_posixshm_check_open(td->td_ucred,
4031 reg->ushm_obj, FFLAGS(O_RDWR));
4034 error = shm_access(reg->ushm_obj, td->td_ucred,
4038 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
4040 shm_hold(reg->ushm_obj);
4041 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
4043 td->td_retval[0] = fd;
4047 umtx_shm_unref_reg(reg, false);
4052 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap)
4055 return (umtx_shm(td, uap->uaddr1, uap->val));
4059 umtx_robust_lists(struct thread *td, struct umtx_robust_lists_params *rbp)
4062 td->td_rb_list = rbp->robust_list_offset;
4063 td->td_rbp_list = rbp->robust_priv_list_offset;
4064 td->td_rb_inact = rbp->robust_inact_offset;
4069 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap)
4071 struct umtx_robust_lists_params rb;
4074 if (uap->val > sizeof(rb))
4076 bzero(&rb, sizeof(rb));
4077 error = copyin(uap->uaddr1, &rb, uap->val);
4080 return (umtx_robust_lists(td, &rb));
4083 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
4085 static const _umtx_op_func op_table[] = {
4086 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
4087 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
4088 [UMTX_OP_WAIT] = __umtx_op_wait,
4089 [UMTX_OP_WAKE] = __umtx_op_wake,
4090 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4091 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
4092 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4093 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4094 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
4095 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4096 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4097 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
4098 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
4099 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
4100 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4101 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4102 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4103 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4104 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4105 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4106 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4107 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4109 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4110 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4112 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4113 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4114 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4115 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4116 [UMTX_OP_SHM] = __umtx_op_shm,
4117 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4121 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4124 if ((unsigned)uap->op < nitems(op_table))
4125 return (*op_table[uap->op])(td, uap);
4129 #ifdef COMPAT_FREEBSD32
4136 struct umtx_time32 {
4137 struct timespec32 timeout;
4143 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
4145 struct timespec32 ts32;
4148 error = copyin(addr, &ts32, sizeof(struct timespec32));
4150 if (ts32.tv_sec < 0 ||
4151 ts32.tv_nsec >= 1000000000 ||
4155 tsp->tv_sec = ts32.tv_sec;
4156 tsp->tv_nsec = ts32.tv_nsec;
4163 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
4165 struct umtx_time32 t32;
4168 t32.clockid = CLOCK_REALTIME;
4170 if (size <= sizeof(struct timespec32))
4171 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
4173 error = copyin(addr, &t32, sizeof(struct umtx_time32));
4176 if (t32.timeout.tv_sec < 0 ||
4177 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
4179 tp->_timeout.tv_sec = t32.timeout.tv_sec;
4180 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
4181 tp->_flags = t32.flags;
4182 tp->_clockid = t32.clockid;
4187 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4189 struct _umtx_time *tm_p, timeout;
4192 if (uap->uaddr2 == NULL)
4195 error = umtx_copyin_umtx_time32(uap->uaddr2,
4196 (size_t)uap->uaddr1, &timeout);
4201 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
4205 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4207 struct _umtx_time *tm_p, timeout;
4210 /* Allow a null timespec (wait forever). */
4211 if (uap->uaddr2 == NULL)
4214 error = umtx_copyin_umtx_time32(uap->uaddr2,
4215 (size_t)uap->uaddr1, &timeout);
4220 return (do_lock_umutex(td, uap->obj, tm_p, 0));
4224 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4226 struct _umtx_time *tm_p, timeout;
4229 /* Allow a null timespec (wait forever). */
4230 if (uap->uaddr2 == NULL)
4233 error = umtx_copyin_umtx_time32(uap->uaddr2,
4234 (size_t)uap->uaddr1, &timeout);
4239 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
4243 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4245 struct timespec *ts, timeout;
4248 /* Allow a null timespec (wait forever). */
4249 if (uap->uaddr2 == NULL)
4252 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
4257 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
4261 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4263 struct _umtx_time timeout;
4266 /* Allow a null timespec (wait forever). */
4267 if (uap->uaddr2 == NULL) {
4268 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4270 error = umtx_copyin_umtx_time32(uap->uaddr2,
4271 (size_t)uap->uaddr1, &timeout);
4274 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4280 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4282 struct _umtx_time timeout;
4285 /* Allow a null timespec (wait forever). */
4286 if (uap->uaddr2 == NULL) {
4287 error = do_rw_wrlock(td, uap->obj, 0);
4289 error = umtx_copyin_umtx_time32(uap->uaddr2,
4290 (size_t)uap->uaddr1, &timeout);
4293 error = do_rw_wrlock(td, uap->obj, &timeout);
4299 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
4301 struct _umtx_time *tm_p, timeout;
4304 if (uap->uaddr2 == NULL)
4307 error = umtx_copyin_umtx_time32(
4308 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
4313 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
4316 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4318 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4320 struct _umtx_time *tm_p, timeout;
4323 /* Allow a null timespec (wait forever). */
4324 if (uap->uaddr2 == NULL)
4327 error = umtx_copyin_umtx_time32(uap->uaddr2,
4328 (size_t)uap->uaddr1, &timeout);
4333 return (do_sem_wait(td, uap->obj, tm_p));
4338 __umtx_op_sem2_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4340 struct _umtx_time *tm_p, timeout;
4344 /* Allow a null timespec (wait forever). */
4345 if (uap->uaddr2 == NULL) {
4349 uasize = (size_t)uap->uaddr1;
4350 error = umtx_copyin_umtx_time32(uap->uaddr2, uasize, &timeout);
4355 error = do_sem2_wait(td, uap->obj, tm_p);
4356 if (error == EINTR && uap->uaddr2 != NULL &&
4357 (timeout._flags & UMTX_ABSTIME) == 0 &&
4358 uasize >= sizeof(struct umtx_time32) + sizeof(struct timespec32)) {
4359 struct timespec32 remain32 = {
4360 .tv_sec = timeout._timeout.tv_sec,
4361 .tv_nsec = timeout._timeout.tv_nsec
4363 error = copyout(&remain32,
4364 (struct umtx_time32 *)uap->uaddr2 + 1,
4365 sizeof(struct timespec32));
4375 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
4377 uint32_t uaddrs[BATCH_SIZE], **upp;
4378 int count, error, i, pos, tocopy;
4380 upp = (uint32_t **)uap->obj;
4382 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
4384 tocopy = MIN(count, BATCH_SIZE);
4385 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
4388 for (i = 0; i < tocopy; ++i)
4389 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
4396 struct umtx_robust_lists_params_compat32 {
4397 uint32_t robust_list_offset;
4398 uint32_t robust_priv_list_offset;
4399 uint32_t robust_inact_offset;
4403 __umtx_op_robust_lists_compat32(struct thread *td, struct _umtx_op_args *uap)
4405 struct umtx_robust_lists_params rb;
4406 struct umtx_robust_lists_params_compat32 rb32;
4409 if (uap->val > sizeof(rb32))
4411 bzero(&rb, sizeof(rb));
4412 bzero(&rb32, sizeof(rb32));
4413 error = copyin(uap->uaddr1, &rb32, uap->val);
4416 rb.robust_list_offset = rb32.robust_list_offset;
4417 rb.robust_priv_list_offset = rb32.robust_priv_list_offset;
4418 rb.robust_inact_offset = rb32.robust_inact_offset;
4419 return (umtx_robust_lists(td, &rb));
4422 static const _umtx_op_func op_table_compat32[] = {
4423 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
4424 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
4425 [UMTX_OP_WAIT] = __umtx_op_wait_compat32,
4426 [UMTX_OP_WAKE] = __umtx_op_wake,
4427 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4428 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex_compat32,
4429 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4430 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4431 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait_compat32,
4432 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4433 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4434 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_compat32,
4435 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock_compat32,
4436 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock_compat32,
4437 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4438 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private_compat32,
4439 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4440 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex_compat32,
4441 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4442 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4443 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait_compat32,
4444 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4446 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4447 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4449 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private32,
4450 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4451 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait_compat32,
4452 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4453 [UMTX_OP_SHM] = __umtx_op_shm,
4454 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists_compat32,
4458 freebsd32__umtx_op(struct thread *td, struct freebsd32__umtx_op_args *uap)
4461 if ((unsigned)uap->op < nitems(op_table_compat32)) {
4462 return (*op_table_compat32[uap->op])(td,
4463 (struct _umtx_op_args *)uap);
4470 umtx_thread_init(struct thread *td)
4473 td->td_umtxq = umtxq_alloc();
4474 td->td_umtxq->uq_thread = td;
4478 umtx_thread_fini(struct thread *td)
4481 umtxq_free(td->td_umtxq);
4485 * It will be called when new thread is created, e.g fork().
4488 umtx_thread_alloc(struct thread *td)
4493 uq->uq_inherited_pri = PRI_MAX;
4495 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4496 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4497 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4498 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4504 * Clear robust lists for all process' threads, not delaying the
4505 * cleanup to thread_exit hook, since the relevant address space is
4506 * destroyed right now.
4509 umtx_exec_hook(void *arg __unused, struct proc *p,
4510 struct image_params *imgp __unused)
4514 KASSERT(p == curproc, ("need curproc"));
4515 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4516 (p->p_flag & P_STOPPED_SINGLE) != 0,
4517 ("curproc must be single-threaded"));
4519 * There is no need to lock the list as only this thread can be
4522 FOREACH_THREAD_IN_PROC(p, td) {
4523 KASSERT(td == curthread ||
4524 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4525 ("running thread %p %p", p, td));
4526 umtx_thread_cleanup(td);
4527 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4532 * thread_exit() hook.
4535 umtx_thread_exit(struct thread *td)
4538 umtx_thread_cleanup(td);
4542 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res)
4545 #ifdef COMPAT_FREEBSD32
4550 #ifdef COMPAT_FREEBSD32
4551 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4552 error = fueword32((void *)ptr, &res32);
4558 error = fueword((void *)ptr, &res1);
4568 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list)
4570 #ifdef COMPAT_FREEBSD32
4571 struct umutex32 m32;
4573 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4574 memcpy(&m32, m, sizeof(m32));
4575 *rb_list = m32.m_rb_lnk;
4578 *rb_list = m->m_rb_lnk;
4582 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact)
4587 KASSERT(td->td_proc == curproc, ("need current vmspace"));
4588 error = copyin((void *)rbp, &m, sizeof(m));
4591 if (rb_list != NULL)
4592 umtx_read_rb_list(td, &m, rb_list);
4593 if ((m.m_flags & UMUTEX_ROBUST) == 0)
4595 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
4596 /* inact is cleared after unlock, allow the inconsistency */
4597 return (inact ? 0 : EINVAL);
4598 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
4602 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
4611 error = umtx_read_uptr(td, rb_list, &rbp);
4612 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
4613 if (rbp == *rb_inact) {
4618 error = umtx_handle_rb(td, rbp, &rbp, inact);
4620 if (i == umtx_max_rb && umtx_verbose_rb) {
4621 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
4622 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
4624 if (error != 0 && umtx_verbose_rb) {
4625 uprintf("comm %s pid %d: handling %srb error %d\n",
4626 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
4631 * Clean up umtx data.
4634 umtx_thread_cleanup(struct thread *td)
4641 * Disown pi mutexes.
4645 if (uq->uq_inherited_pri != PRI_MAX ||
4646 !TAILQ_EMPTY(&uq->uq_pi_contested)) {
4647 mtx_lock(&umtx_lock);
4648 uq->uq_inherited_pri = PRI_MAX;
4649 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
4650 pi->pi_owner = NULL;
4651 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
4653 mtx_unlock(&umtx_lock);
4655 sched_lend_user_prio_cond(td, PRI_MAX);
4658 if (td->td_rb_inact == 0 && td->td_rb_list == 0 && td->td_rbp_list == 0)
4662 * Handle terminated robust mutexes. Must be done after
4663 * robust pi disown, otherwise unlock could see unowned
4666 rb_inact = td->td_rb_inact;
4668 (void)umtx_read_uptr(td, rb_inact, &rb_inact);
4669 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "");
4670 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ");
4672 (void)umtx_handle_rb(td, rb_inact, NULL, true);