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.h>
80 #include <compat/freebsd32/freebsd32_proto.h>
84 #define _UMUTEX_WAIT 2
87 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
88 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
91 /* Priority inheritance mutex info. */
94 struct thread *pi_owner;
99 /* List entry to link umtx holding by thread */
100 TAILQ_ENTRY(umtx_pi) pi_link;
102 /* List entry in hash */
103 TAILQ_ENTRY(umtx_pi) pi_hashlink;
105 /* List for waiters */
106 TAILQ_HEAD(,umtx_q) pi_blocked;
108 /* Identify a userland lock object */
109 struct umtx_key pi_key;
112 /* A userland synchronous object user. */
114 /* Linked list for the hash. */
115 TAILQ_ENTRY(umtx_q) uq_link;
118 struct umtx_key uq_key;
122 #define UQF_UMTXQ 0x0001
124 /* The thread waits on. */
125 struct thread *uq_thread;
128 * Blocked on PI mutex. read can use chain lock
129 * or umtx_lock, write must have both chain lock and
130 * umtx_lock being hold.
132 struct umtx_pi *uq_pi_blocked;
134 /* On blocked list */
135 TAILQ_ENTRY(umtx_q) uq_lockq;
137 /* Thread contending with us */
138 TAILQ_HEAD(,umtx_pi) uq_pi_contested;
140 /* Inherited priority from PP mutex */
141 u_char uq_inherited_pri;
143 /* Spare queue ready to be reused */
144 struct umtxq_queue *uq_spare_queue;
146 /* The queue we on */
147 struct umtxq_queue *uq_cur_queue;
150 TAILQ_HEAD(umtxq_head, umtx_q);
152 /* Per-key wait-queue */
154 struct umtxq_head head;
156 LIST_ENTRY(umtxq_queue) link;
160 LIST_HEAD(umtxq_list, umtxq_queue);
162 /* Userland lock object's wait-queue chain */
164 /* Lock for this chain. */
167 /* List of sleep queues. */
168 struct umtxq_list uc_queue[2];
169 #define UMTX_SHARED_QUEUE 0
170 #define UMTX_EXCLUSIVE_QUEUE 1
172 LIST_HEAD(, umtxq_queue) uc_spare_queue;
177 /* Chain lock waiters */
180 /* All PI in the list */
181 TAILQ_HEAD(,umtx_pi) uc_pi_list;
183 #ifdef UMTX_PROFILING
189 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
192 * Don't propagate time-sharing priority, there is a security reason,
193 * a user can simply introduce PI-mutex, let thread A lock the mutex,
194 * and let another thread B block on the mutex, because B is
195 * sleeping, its priority will be boosted, this causes A's priority to
196 * be boosted via priority propagating too and will never be lowered even
197 * if it is using 100%CPU, this is unfair to other processes.
200 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
201 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
202 PRI_MAX_TIMESHARE : (td)->td_user_pri)
204 #define GOLDEN_RATIO_PRIME 2654404609U
206 #define UMTX_CHAINS 512
208 #define UMTX_SHIFTS (__WORD_BIT - 9)
210 #define GET_SHARE(flags) \
211 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
213 #define BUSY_SPINS 200
217 bool is_abs_real; /* TIMER_ABSTIME && CLOCK_REALTIME* */
222 #ifdef COMPAT_FREEBSD32
223 _Static_assert(sizeof(struct umutex) == sizeof(struct umutex32), "umutex32");
224 _Static_assert(__offsetof(struct umutex, m_spare[0]) ==
225 __offsetof(struct umutex32, m_spare[0]), "m_spare32");
228 int umtx_shm_vnobj_persistent = 0;
229 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_vnode_persistent, CTLFLAG_RWTUN,
230 &umtx_shm_vnobj_persistent, 0,
231 "False forces destruction of umtx attached to file, on last close");
232 static int umtx_max_rb = 1000;
233 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_max_robust, CTLFLAG_RWTUN,
235 "Maximum number of robust mutexes allowed for each thread");
237 static uma_zone_t umtx_pi_zone;
238 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
239 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
240 static int umtx_pi_allocated;
242 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
244 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
245 &umtx_pi_allocated, 0, "Allocated umtx_pi");
246 static int umtx_verbose_rb = 1;
247 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
251 #ifdef UMTX_PROFILING
252 static long max_length;
253 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
254 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
258 static void abs_timeout_update(struct abs_timeout *timo);
260 static void umtx_shm_init(void);
261 static void umtxq_sysinit(void *);
262 static void umtxq_hash(struct umtx_key *key);
263 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
264 static void umtxq_lock(struct umtx_key *key);
265 static void umtxq_unlock(struct umtx_key *key);
266 static void umtxq_busy(struct umtx_key *key);
267 static void umtxq_unbusy(struct umtx_key *key);
268 static void umtxq_insert_queue(struct umtx_q *uq, int q);
269 static void umtxq_remove_queue(struct umtx_q *uq, int q);
270 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *);
271 static int umtxq_count(struct umtx_key *key);
272 static struct umtx_pi *umtx_pi_alloc(int);
273 static void umtx_pi_free(struct umtx_pi *pi);
274 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
276 static void umtx_thread_cleanup(struct thread *td);
277 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
278 struct image_params *imgp __unused);
279 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
281 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
282 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
283 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
285 static struct mtx umtx_lock;
287 #ifdef UMTX_PROFILING
289 umtx_init_profiling(void)
291 struct sysctl_oid *chain_oid;
295 for (i = 0; i < UMTX_CHAINS; ++i) {
296 snprintf(chain_name, sizeof(chain_name), "%d", i);
297 chain_oid = SYSCTL_ADD_NODE(NULL,
298 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
299 chain_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
301 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
302 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
303 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
304 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
309 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
313 struct umtxq_chain *uc;
314 u_int fract, i, j, tot, whole;
315 u_int sf0, sf1, sf2, sf3, sf4;
316 u_int si0, si1, si2, si3, si4;
317 u_int sw0, sw1, sw2, sw3, sw4;
319 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
320 for (i = 0; i < 2; i++) {
322 for (j = 0; j < UMTX_CHAINS; ++j) {
323 uc = &umtxq_chains[i][j];
324 mtx_lock(&uc->uc_lock);
325 tot += uc->max_length;
326 mtx_unlock(&uc->uc_lock);
329 sbuf_printf(&sb, "%u) Empty ", i);
331 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
332 si0 = si1 = si2 = si3 = si4 = 0;
333 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
334 for (j = 0; j < UMTX_CHAINS; j++) {
335 uc = &umtxq_chains[i][j];
336 mtx_lock(&uc->uc_lock);
337 whole = uc->max_length * 100;
338 mtx_unlock(&uc->uc_lock);
339 fract = (whole % tot) * 100;
340 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
344 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
349 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
354 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
359 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
366 sbuf_printf(&sb, "queue %u:\n", i);
367 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
369 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
371 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
373 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
375 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
381 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
387 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
389 struct umtxq_chain *uc;
394 error = sysctl_handle_int(oidp, &clear, 0, req);
395 if (error != 0 || req->newptr == NULL)
399 for (i = 0; i < 2; ++i) {
400 for (j = 0; j < UMTX_CHAINS; ++j) {
401 uc = &umtxq_chains[i][j];
402 mtx_lock(&uc->uc_lock);
405 mtx_unlock(&uc->uc_lock);
412 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
413 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
414 sysctl_debug_umtx_chains_clear, "I",
415 "Clear umtx chains statistics");
416 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
417 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
418 sysctl_debug_umtx_chains_peaks, "A",
419 "Highest peaks in chains max length");
423 umtxq_sysinit(void *arg __unused)
427 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
428 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
429 for (i = 0; i < 2; ++i) {
430 for (j = 0; j < UMTX_CHAINS; ++j) {
431 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
432 MTX_DEF | MTX_DUPOK);
433 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
434 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
435 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
436 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
437 umtxq_chains[i][j].uc_busy = 0;
438 umtxq_chains[i][j].uc_waiters = 0;
439 #ifdef UMTX_PROFILING
440 umtxq_chains[i][j].length = 0;
441 umtxq_chains[i][j].max_length = 0;
445 #ifdef UMTX_PROFILING
446 umtx_init_profiling();
448 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
449 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
450 EVENTHANDLER_PRI_ANY);
459 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
460 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
462 TAILQ_INIT(&uq->uq_spare_queue->head);
463 TAILQ_INIT(&uq->uq_pi_contested);
464 uq->uq_inherited_pri = PRI_MAX;
469 umtxq_free(struct umtx_q *uq)
472 MPASS(uq->uq_spare_queue != NULL);
473 free(uq->uq_spare_queue, M_UMTX);
478 umtxq_hash(struct umtx_key *key)
482 n = (uintptr_t)key->info.both.a + key->info.both.b;
483 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
486 static inline struct umtxq_chain *
487 umtxq_getchain(struct umtx_key *key)
490 if (key->type <= TYPE_SEM)
491 return (&umtxq_chains[1][key->hash]);
492 return (&umtxq_chains[0][key->hash]);
499 umtxq_lock(struct umtx_key *key)
501 struct umtxq_chain *uc;
503 uc = umtxq_getchain(key);
504 mtx_lock(&uc->uc_lock);
511 umtxq_unlock(struct umtx_key *key)
513 struct umtxq_chain *uc;
515 uc = umtxq_getchain(key);
516 mtx_unlock(&uc->uc_lock);
520 * Set chain to busy state when following operation
521 * may be blocked (kernel mutex can not be used).
524 umtxq_busy(struct umtx_key *key)
526 struct umtxq_chain *uc;
528 uc = umtxq_getchain(key);
529 mtx_assert(&uc->uc_lock, MA_OWNED);
533 int count = BUSY_SPINS;
536 while (uc->uc_busy && --count > 0)
542 while (uc->uc_busy) {
544 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
555 umtxq_unbusy(struct umtx_key *key)
557 struct umtxq_chain *uc;
559 uc = umtxq_getchain(key);
560 mtx_assert(&uc->uc_lock, MA_OWNED);
561 KASSERT(uc->uc_busy != 0, ("not busy"));
568 umtxq_unbusy_unlocked(struct umtx_key *key)
576 static struct umtxq_queue *
577 umtxq_queue_lookup(struct umtx_key *key, int q)
579 struct umtxq_queue *uh;
580 struct umtxq_chain *uc;
582 uc = umtxq_getchain(key);
583 UMTXQ_LOCKED_ASSERT(uc);
584 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
585 if (umtx_key_match(&uh->key, key))
593 umtxq_insert_queue(struct umtx_q *uq, int q)
595 struct umtxq_queue *uh;
596 struct umtxq_chain *uc;
598 uc = umtxq_getchain(&uq->uq_key);
599 UMTXQ_LOCKED_ASSERT(uc);
600 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
601 uh = umtxq_queue_lookup(&uq->uq_key, q);
603 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
605 uh = uq->uq_spare_queue;
606 uh->key = uq->uq_key;
607 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
608 #ifdef UMTX_PROFILING
610 if (uc->length > uc->max_length) {
611 uc->max_length = uc->length;
612 if (uc->max_length > max_length)
613 max_length = uc->max_length;
617 uq->uq_spare_queue = NULL;
619 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
621 uq->uq_flags |= UQF_UMTXQ;
622 uq->uq_cur_queue = uh;
627 umtxq_remove_queue(struct umtx_q *uq, int q)
629 struct umtxq_chain *uc;
630 struct umtxq_queue *uh;
632 uc = umtxq_getchain(&uq->uq_key);
633 UMTXQ_LOCKED_ASSERT(uc);
634 if (uq->uq_flags & UQF_UMTXQ) {
635 uh = uq->uq_cur_queue;
636 TAILQ_REMOVE(&uh->head, uq, uq_link);
638 uq->uq_flags &= ~UQF_UMTXQ;
639 if (TAILQ_EMPTY(&uh->head)) {
640 KASSERT(uh->length == 0,
641 ("inconsistent umtxq_queue length"));
642 #ifdef UMTX_PROFILING
645 LIST_REMOVE(uh, link);
647 uh = LIST_FIRST(&uc->uc_spare_queue);
648 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
649 LIST_REMOVE(uh, link);
651 uq->uq_spare_queue = uh;
652 uq->uq_cur_queue = NULL;
657 * Check if there are multiple waiters
660 umtxq_count(struct umtx_key *key)
662 struct umtxq_queue *uh;
664 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
665 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
672 * Check if there are multiple PI waiters and returns first
676 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
678 struct umtxq_queue *uh;
681 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
682 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
684 *first = TAILQ_FIRST(&uh->head);
691 * Wake up threads waiting on an userland object.
695 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
697 struct umtxq_queue *uh;
702 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
703 uh = umtxq_queue_lookup(key, q);
705 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
706 umtxq_remove_queue(uq, q);
716 * Wake up specified thread.
719 umtxq_signal_thread(struct umtx_q *uq)
722 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
728 tstohz(const struct timespec *tsp)
732 TIMESPEC_TO_TIMEVAL(&tv, tsp);
737 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
738 const struct timespec *timeout)
741 timo->clockid = clockid;
743 timo->is_abs_real = false;
744 abs_timeout_update(timo);
745 timespecadd(&timo->cur, timeout, &timo->end);
747 timo->end = *timeout;
748 timo->is_abs_real = clockid == CLOCK_REALTIME ||
749 clockid == CLOCK_REALTIME_FAST ||
750 clockid == CLOCK_REALTIME_PRECISE;
752 * If is_abs_real, umtxq_sleep will read the clock
753 * after setting td_rtcgen; otherwise, read it here.
755 if (!timo->is_abs_real) {
756 abs_timeout_update(timo);
762 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
765 abs_timeout_init(timo, umtxtime->_clockid,
766 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
770 abs_timeout_update(struct abs_timeout *timo)
773 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
777 abs_timeout_gethz(struct abs_timeout *timo)
781 if (timespeccmp(&timo->end, &timo->cur, <=))
783 timespecsub(&timo->end, &timo->cur, &tts);
784 return (tstohz(&tts));
788 umtx_unlock_val(uint32_t flags, bool rb)
792 return (UMUTEX_RB_OWNERDEAD);
793 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
794 return (UMUTEX_RB_NOTRECOV);
796 return (UMUTEX_UNOWNED);
801 * Put thread into sleep state, before sleeping, check if
802 * thread was removed from umtx queue.
805 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
807 struct umtxq_chain *uc;
810 if (abstime != NULL && abstime->is_abs_real) {
811 curthread->td_rtcgen = atomic_load_acq_int(&rtc_generation);
812 abs_timeout_update(abstime);
815 uc = umtxq_getchain(&uq->uq_key);
816 UMTXQ_LOCKED_ASSERT(uc);
818 if (!(uq->uq_flags & UQF_UMTXQ)) {
822 if (abstime != NULL) {
823 timo = abs_timeout_gethz(abstime);
830 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
831 if (error == EINTR || error == ERESTART) {
832 umtxq_lock(&uq->uq_key);
835 if (abstime != NULL) {
836 if (abstime->is_abs_real)
837 curthread->td_rtcgen =
838 atomic_load_acq_int(&rtc_generation);
839 abs_timeout_update(abstime);
841 umtxq_lock(&uq->uq_key);
844 curthread->td_rtcgen = 0;
849 * Convert userspace address into unique logical address.
852 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
854 struct thread *td = curthread;
856 vm_map_entry_t entry;
862 if (share == THREAD_SHARE) {
864 key->info.private.vs = td->td_proc->p_vmspace;
865 key->info.private.addr = (uintptr_t)addr;
867 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
868 map = &td->td_proc->p_vmspace->vm_map;
869 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
870 &entry, &key->info.shared.object, &pindex, &prot,
871 &wired) != KERN_SUCCESS) {
875 if ((share == PROCESS_SHARE) ||
876 (share == AUTO_SHARE &&
877 VM_INHERIT_SHARE == entry->inheritance)) {
879 key->info.shared.offset = (vm_offset_t)addr -
880 entry->start + entry->offset;
881 vm_object_reference(key->info.shared.object);
884 key->info.private.vs = td->td_proc->p_vmspace;
885 key->info.private.addr = (uintptr_t)addr;
887 vm_map_lookup_done(map, entry);
898 umtx_key_release(struct umtx_key *key)
901 vm_object_deallocate(key->info.shared.object);
905 * Fetch and compare value, sleep on the address if value is not changed.
908 do_wait(struct thread *td, void *addr, u_long id,
909 struct _umtx_time *timeout, int compat32, int is_private)
911 struct abs_timeout timo;
918 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
919 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
923 abs_timeout_init2(&timo, timeout);
925 umtxq_lock(&uq->uq_key);
927 umtxq_unlock(&uq->uq_key);
929 error = fueword(addr, &tmp);
933 error = fueword32(addr, &tmp32);
939 umtxq_lock(&uq->uq_key);
942 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
944 if ((uq->uq_flags & UQF_UMTXQ) == 0)
948 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
951 umtxq_unlock(&uq->uq_key);
952 umtx_key_release(&uq->uq_key);
953 if (error == ERESTART)
959 * Wake up threads sleeping on the specified address.
962 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
967 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
968 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
971 umtxq_signal(&key, n_wake);
973 umtx_key_release(&key);
978 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
981 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
982 struct _umtx_time *timeout, int mode)
984 struct abs_timeout timo;
986 uint32_t owner, old, id;
993 abs_timeout_init2(&timo, timeout);
996 * Care must be exercised when dealing with umtx structure. It
997 * can fault on any access.
1000 rv = fueword32(&m->m_owner, &owner);
1003 if (mode == _UMUTEX_WAIT) {
1004 if (owner == UMUTEX_UNOWNED ||
1005 owner == UMUTEX_CONTESTED ||
1006 owner == UMUTEX_RB_OWNERDEAD ||
1007 owner == UMUTEX_RB_NOTRECOV)
1011 * Robust mutex terminated. Kernel duty is to
1012 * return EOWNERDEAD to the userspace. The
1013 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1014 * by the common userspace code.
1016 if (owner == UMUTEX_RB_OWNERDEAD) {
1017 rv = casueword32(&m->m_owner,
1018 UMUTEX_RB_OWNERDEAD, &owner,
1019 id | UMUTEX_CONTESTED);
1023 MPASS(owner == UMUTEX_RB_OWNERDEAD);
1024 return (EOWNERDEAD); /* success */
1027 rv = thread_check_susp(td, false);
1032 if (owner == UMUTEX_RB_NOTRECOV)
1033 return (ENOTRECOVERABLE);
1036 * Try the uncontested case. This should be
1039 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1041 /* The address was invalid. */
1045 /* The acquire succeeded. */
1047 MPASS(owner == UMUTEX_UNOWNED);
1052 * If no one owns it but it is contested try
1056 if (owner == UMUTEX_CONTESTED) {
1057 rv = casueword32(&m->m_owner,
1058 UMUTEX_CONTESTED, &owner,
1059 id | UMUTEX_CONTESTED);
1060 /* The address was invalid. */
1064 MPASS(owner == UMUTEX_CONTESTED);
1068 rv = thread_check_susp(td, false);
1074 * If this failed the lock has
1080 /* rv == 1 but not contested, likely store failure */
1081 rv = thread_check_susp(td, false);
1086 if (mode == _UMUTEX_TRY)
1090 * If we caught a signal, we have retried and now
1096 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1097 GET_SHARE(flags), &uq->uq_key)) != 0)
1100 umtxq_lock(&uq->uq_key);
1101 umtxq_busy(&uq->uq_key);
1103 umtxq_unlock(&uq->uq_key);
1106 * Set the contested bit so that a release in user space
1107 * knows to use the system call for unlock. If this fails
1108 * either some one else has acquired the lock or it has been
1111 rv = casueword32(&m->m_owner, owner, &old,
1112 owner | UMUTEX_CONTESTED);
1114 /* The address was invalid or casueword failed to store. */
1115 if (rv == -1 || rv == 1) {
1116 umtxq_lock(&uq->uq_key);
1118 umtxq_unbusy(&uq->uq_key);
1119 umtxq_unlock(&uq->uq_key);
1120 umtx_key_release(&uq->uq_key);
1124 rv = thread_check_susp(td, false);
1132 * We set the contested bit, sleep. Otherwise the lock changed
1133 * and we need to retry or we lost a race to the thread
1134 * unlocking the umtx.
1136 umtxq_lock(&uq->uq_key);
1137 umtxq_unbusy(&uq->uq_key);
1138 MPASS(old == owner);
1139 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1142 umtxq_unlock(&uq->uq_key);
1143 umtx_key_release(&uq->uq_key);
1146 error = thread_check_susp(td, false);
1153 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1156 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1158 struct umtx_key key;
1159 uint32_t owner, old, id, newlock;
1166 * Make sure we own this mtx.
1168 error = fueword32(&m->m_owner, &owner);
1172 if ((owner & ~UMUTEX_CONTESTED) != id)
1175 newlock = umtx_unlock_val(flags, rb);
1176 if ((owner & UMUTEX_CONTESTED) == 0) {
1177 error = casueword32(&m->m_owner, owner, &old, newlock);
1181 error = thread_check_susp(td, false);
1186 MPASS(old == owner);
1190 /* We should only ever be in here for contested locks */
1191 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1197 count = umtxq_count(&key);
1201 * When unlocking the umtx, it must be marked as unowned if
1202 * there is zero or one thread only waiting for it.
1203 * Otherwise, it must be marked as contested.
1206 newlock |= UMUTEX_CONTESTED;
1207 error = casueword32(&m->m_owner, owner, &old, newlock);
1209 umtxq_signal(&key, 1);
1212 umtx_key_release(&key);
1218 error = thread_check_susp(td, false);
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;
1240 error = fueword32(&m->m_owner, &owner);
1244 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1245 owner != UMUTEX_RB_NOTRECOV)
1248 error = fueword32(&m->m_flags, &flags);
1252 /* We should only ever be in here for contested locks */
1253 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1259 count = umtxq_count(&key);
1262 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1263 owner != UMUTEX_RB_NOTRECOV) {
1264 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1268 } else if (error == 1) {
1272 umtx_key_release(&key);
1273 error = thread_check_susp(td, false);
1281 if (error == 0 && count != 0) {
1282 MPASS((owner & ~UMUTEX_CONTESTED) == 0 ||
1283 owner == UMUTEX_RB_OWNERDEAD ||
1284 owner == UMUTEX_RB_NOTRECOV);
1285 umtxq_signal(&key, 1);
1289 umtx_key_release(&key);
1294 * Check if the mutex has waiters and tries to fix contention bit.
1297 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1299 struct umtx_key key;
1300 uint32_t owner, old;
1305 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1309 type = TYPE_NORMAL_UMUTEX;
1311 case UMUTEX_PRIO_INHERIT:
1312 type = TYPE_PI_UMUTEX;
1314 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1315 type = TYPE_PI_ROBUST_UMUTEX;
1317 case UMUTEX_PRIO_PROTECT:
1318 type = TYPE_PP_UMUTEX;
1320 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1321 type = TYPE_PP_ROBUST_UMUTEX;
1326 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1332 count = umtxq_count(&key);
1335 error = fueword32(&m->m_owner, &owner);
1340 * Only repair contention bit if there is a waiter, this means
1341 * the mutex is still being referenced by userland code,
1342 * otherwise don't update any memory.
1344 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0 &&
1345 (count > 1 || (count == 1 && (owner & ~UMUTEX_CONTESTED) != 0))) {
1346 error = casueword32(&m->m_owner, owner, &old,
1347 owner | UMUTEX_CONTESTED);
1353 MPASS(old == owner);
1357 error = thread_check_susp(td, false);
1361 if (error == EFAULT) {
1362 umtxq_signal(&key, INT_MAX);
1363 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1364 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1365 umtxq_signal(&key, 1);
1368 umtx_key_release(&key);
1372 static inline struct umtx_pi *
1373 umtx_pi_alloc(int flags)
1377 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1378 TAILQ_INIT(&pi->pi_blocked);
1379 atomic_add_int(&umtx_pi_allocated, 1);
1384 umtx_pi_free(struct umtx_pi *pi)
1386 uma_zfree(umtx_pi_zone, pi);
1387 atomic_add_int(&umtx_pi_allocated, -1);
1391 * Adjust the thread's position on a pi_state after its priority has been
1395 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1397 struct umtx_q *uq, *uq1, *uq2;
1400 mtx_assert(&umtx_lock, MA_OWNED);
1407 * Check if the thread needs to be moved on the blocked chain.
1408 * It needs to be moved if either its priority is lower than
1409 * the previous thread or higher than the next thread.
1411 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1412 uq2 = TAILQ_NEXT(uq, uq_lockq);
1413 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1414 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1416 * Remove thread from blocked chain and determine where
1417 * it should be moved to.
1419 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1420 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1421 td1 = uq1->uq_thread;
1422 MPASS(td1->td_proc->p_magic == P_MAGIC);
1423 if (UPRI(td1) > UPRI(td))
1428 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1430 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1435 static struct umtx_pi *
1436 umtx_pi_next(struct umtx_pi *pi)
1438 struct umtx_q *uq_owner;
1440 if (pi->pi_owner == NULL)
1442 uq_owner = pi->pi_owner->td_umtxq;
1443 if (uq_owner == NULL)
1445 return (uq_owner->uq_pi_blocked);
1449 * Floyd's Cycle-Finding Algorithm.
1452 umtx_pi_check_loop(struct umtx_pi *pi)
1454 struct umtx_pi *pi1; /* fast iterator */
1456 mtx_assert(&umtx_lock, MA_OWNED);
1461 pi = umtx_pi_next(pi);
1464 pi1 = umtx_pi_next(pi1);
1467 pi1 = umtx_pi_next(pi1);
1477 * Propagate priority when a thread is blocked on POSIX
1481 umtx_propagate_priority(struct thread *td)
1487 mtx_assert(&umtx_lock, MA_OWNED);
1490 pi = uq->uq_pi_blocked;
1493 if (umtx_pi_check_loop(pi))
1498 if (td == NULL || td == curthread)
1501 MPASS(td->td_proc != NULL);
1502 MPASS(td->td_proc->p_magic == P_MAGIC);
1505 if (td->td_lend_user_pri > pri)
1506 sched_lend_user_prio(td, pri);
1514 * Pick up the lock that td is blocked on.
1517 pi = uq->uq_pi_blocked;
1520 /* Resort td on the list if needed. */
1521 umtx_pi_adjust_thread(pi, td);
1526 * Unpropagate priority for a PI mutex when a thread blocked on
1527 * it is interrupted by signal or resumed by others.
1530 umtx_repropagate_priority(struct umtx_pi *pi)
1532 struct umtx_q *uq, *uq_owner;
1533 struct umtx_pi *pi2;
1536 mtx_assert(&umtx_lock, MA_OWNED);
1538 if (umtx_pi_check_loop(pi))
1540 while (pi != NULL && pi->pi_owner != NULL) {
1542 uq_owner = pi->pi_owner->td_umtxq;
1544 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1545 uq = TAILQ_FIRST(&pi2->pi_blocked);
1547 if (pri > UPRI(uq->uq_thread))
1548 pri = UPRI(uq->uq_thread);
1552 if (pri > uq_owner->uq_inherited_pri)
1553 pri = uq_owner->uq_inherited_pri;
1554 thread_lock(pi->pi_owner);
1555 sched_lend_user_prio(pi->pi_owner, pri);
1556 thread_unlock(pi->pi_owner);
1557 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1558 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1563 * Insert a PI mutex into owned list.
1566 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1568 struct umtx_q *uq_owner;
1570 uq_owner = owner->td_umtxq;
1571 mtx_assert(&umtx_lock, MA_OWNED);
1572 MPASS(pi->pi_owner == NULL);
1573 pi->pi_owner = owner;
1574 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1578 * Disown a PI mutex, and remove it from the owned list.
1581 umtx_pi_disown(struct umtx_pi *pi)
1584 mtx_assert(&umtx_lock, MA_OWNED);
1585 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1586 pi->pi_owner = NULL;
1590 * Claim ownership of a PI mutex.
1593 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1598 mtx_lock(&umtx_lock);
1599 if (pi->pi_owner == owner) {
1600 mtx_unlock(&umtx_lock);
1604 if (pi->pi_owner != NULL) {
1606 * userland may have already messed the mutex, sigh.
1608 mtx_unlock(&umtx_lock);
1611 umtx_pi_setowner(pi, owner);
1612 uq = TAILQ_FIRST(&pi->pi_blocked);
1614 pri = UPRI(uq->uq_thread);
1616 if (pri < UPRI(owner))
1617 sched_lend_user_prio(owner, pri);
1618 thread_unlock(owner);
1620 mtx_unlock(&umtx_lock);
1625 * Adjust a thread's order position in its blocked PI mutex,
1626 * this may result new priority propagating process.
1629 umtx_pi_adjust(struct thread *td, u_char oldpri)
1635 mtx_lock(&umtx_lock);
1637 * Pick up the lock that td is blocked on.
1639 pi = uq->uq_pi_blocked;
1641 umtx_pi_adjust_thread(pi, td);
1642 umtx_repropagate_priority(pi);
1644 mtx_unlock(&umtx_lock);
1648 * Sleep on a PI mutex.
1651 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
1652 const char *wmesg, struct abs_timeout *timo, bool shared)
1654 struct thread *td, *td1;
1658 struct umtxq_chain *uc;
1660 uc = umtxq_getchain(&pi->pi_key);
1664 KASSERT(td == curthread, ("inconsistent uq_thread"));
1665 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
1666 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1668 mtx_lock(&umtx_lock);
1669 if (pi->pi_owner == NULL) {
1670 mtx_unlock(&umtx_lock);
1671 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
1672 mtx_lock(&umtx_lock);
1674 if (pi->pi_owner == NULL)
1675 umtx_pi_setowner(pi, td1);
1676 PROC_UNLOCK(td1->td_proc);
1680 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1681 pri = UPRI(uq1->uq_thread);
1687 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1689 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1691 uq->uq_pi_blocked = pi;
1693 td->td_flags |= TDF_UPIBLOCKED;
1695 umtx_propagate_priority(td);
1696 mtx_unlock(&umtx_lock);
1697 umtxq_unbusy(&uq->uq_key);
1699 error = umtxq_sleep(uq, wmesg, timo);
1702 mtx_lock(&umtx_lock);
1703 uq->uq_pi_blocked = NULL;
1705 td->td_flags &= ~TDF_UPIBLOCKED;
1707 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1708 umtx_repropagate_priority(pi);
1709 mtx_unlock(&umtx_lock);
1710 umtxq_unlock(&uq->uq_key);
1716 * Add reference count for a PI mutex.
1719 umtx_pi_ref(struct umtx_pi *pi)
1722 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
1727 * Decrease reference count for a PI mutex, if the counter
1728 * is decreased to zero, its memory space is freed.
1731 umtx_pi_unref(struct umtx_pi *pi)
1733 struct umtxq_chain *uc;
1735 uc = umtxq_getchain(&pi->pi_key);
1736 UMTXQ_LOCKED_ASSERT(uc);
1737 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1738 if (--pi->pi_refcount == 0) {
1739 mtx_lock(&umtx_lock);
1740 if (pi->pi_owner != NULL)
1742 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1743 ("blocked queue not empty"));
1744 mtx_unlock(&umtx_lock);
1745 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1751 * Find a PI mutex in hash table.
1753 static struct umtx_pi *
1754 umtx_pi_lookup(struct umtx_key *key)
1756 struct umtxq_chain *uc;
1759 uc = umtxq_getchain(key);
1760 UMTXQ_LOCKED_ASSERT(uc);
1762 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1763 if (umtx_key_match(&pi->pi_key, key)) {
1771 * Insert a PI mutex into hash table.
1774 umtx_pi_insert(struct umtx_pi *pi)
1776 struct umtxq_chain *uc;
1778 uc = umtxq_getchain(&pi->pi_key);
1779 UMTXQ_LOCKED_ASSERT(uc);
1780 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1787 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1788 struct _umtx_time *timeout, int try)
1790 struct abs_timeout timo;
1792 struct umtx_pi *pi, *new_pi;
1793 uint32_t id, old_owner, owner, old;
1799 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
1800 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
1804 if (timeout != NULL)
1805 abs_timeout_init2(&timo, timeout);
1807 umtxq_lock(&uq->uq_key);
1808 pi = umtx_pi_lookup(&uq->uq_key);
1810 new_pi = umtx_pi_alloc(M_NOWAIT);
1811 if (new_pi == NULL) {
1812 umtxq_unlock(&uq->uq_key);
1813 new_pi = umtx_pi_alloc(M_WAITOK);
1814 umtxq_lock(&uq->uq_key);
1815 pi = umtx_pi_lookup(&uq->uq_key);
1817 umtx_pi_free(new_pi);
1821 if (new_pi != NULL) {
1822 new_pi->pi_key = uq->uq_key;
1823 umtx_pi_insert(new_pi);
1828 umtxq_unlock(&uq->uq_key);
1831 * Care must be exercised when dealing with umtx structure. It
1832 * can fault on any access.
1836 * Try the uncontested case. This should be done in userland.
1838 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
1839 /* The address was invalid. */
1844 /* The acquire succeeded. */
1846 MPASS(owner == UMUTEX_UNOWNED);
1851 if (owner == UMUTEX_RB_NOTRECOV) {
1852 error = ENOTRECOVERABLE;
1857 * Avoid overwriting a possible error from sleep due
1858 * to the pending signal with suspension check result.
1861 error = thread_check_susp(td, true);
1866 /* If no one owns it but it is contested try to acquire it. */
1867 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
1869 rv = casueword32(&m->m_owner, owner, &owner,
1870 id | UMUTEX_CONTESTED);
1871 /* The address was invalid. */
1878 error = thread_check_susp(td, true);
1884 * If this failed the lock could
1891 MPASS(owner == old_owner);
1892 umtxq_lock(&uq->uq_key);
1893 umtxq_busy(&uq->uq_key);
1894 error = umtx_pi_claim(pi, td);
1895 umtxq_unbusy(&uq->uq_key);
1896 umtxq_unlock(&uq->uq_key);
1899 * Since we're going to return an
1900 * error, restore the m_owner to its
1901 * previous, unowned state to avoid
1902 * compounding the problem.
1904 (void)casuword32(&m->m_owner,
1905 id | UMUTEX_CONTESTED, old_owner);
1907 if (error == 0 && old_owner == UMUTEX_RB_OWNERDEAD)
1912 if ((owner & ~UMUTEX_CONTESTED) == id) {
1923 * If we caught a signal, we have retried and now
1929 umtxq_lock(&uq->uq_key);
1930 umtxq_busy(&uq->uq_key);
1931 umtxq_unlock(&uq->uq_key);
1934 * Set the contested bit so that a release in user space
1935 * knows to use the system call for unlock. If this fails
1936 * either some one else has acquired the lock or it has been
1939 rv = casueword32(&m->m_owner, owner, &old, owner |
1942 /* The address was invalid. */
1944 umtxq_unbusy_unlocked(&uq->uq_key);
1949 umtxq_unbusy_unlocked(&uq->uq_key);
1950 error = thread_check_susp(td, true);
1955 * The lock changed and we need to retry or we
1956 * lost a race to the thread unlocking the
1957 * umtx. Note that the UMUTEX_RB_OWNERDEAD
1958 * value for owner is impossible there.
1963 umtxq_lock(&uq->uq_key);
1965 /* We set the contested bit, sleep. */
1966 MPASS(old == owner);
1967 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
1968 "umtxpi", timeout == NULL ? NULL : &timo,
1969 (flags & USYNC_PROCESS_SHARED) != 0);
1973 error = thread_check_susp(td, false);
1978 umtxq_lock(&uq->uq_key);
1980 umtxq_unlock(&uq->uq_key);
1982 umtx_key_release(&uq->uq_key);
1987 * Unlock a PI mutex.
1990 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1992 struct umtx_key key;
1993 struct umtx_q *uq_first, *uq_first2, *uq_me;
1994 struct umtx_pi *pi, *pi2;
1995 uint32_t id, new_owner, old, owner;
1996 int count, error, pri;
2002 * Make sure we own this mtx.
2004 error = fueword32(&m->m_owner, &owner);
2008 if ((owner & ~UMUTEX_CONTESTED) != id)
2011 new_owner = umtx_unlock_val(flags, rb);
2013 /* This should be done in userland */
2014 if ((owner & UMUTEX_CONTESTED) == 0) {
2015 error = casueword32(&m->m_owner, owner, &old, new_owner);
2019 error = thread_check_susp(td, true);
2029 /* We should only ever be in here for contested locks */
2030 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2031 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2037 count = umtxq_count_pi(&key, &uq_first);
2038 if (uq_first != NULL) {
2039 mtx_lock(&umtx_lock);
2040 pi = uq_first->uq_pi_blocked;
2041 KASSERT(pi != NULL, ("pi == NULL?"));
2042 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2043 mtx_unlock(&umtx_lock);
2046 umtx_key_release(&key);
2047 /* userland messed the mutex */
2050 uq_me = td->td_umtxq;
2051 if (pi->pi_owner == td)
2053 /* get highest priority thread which is still sleeping. */
2054 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2055 while (uq_first != NULL &&
2056 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2057 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2060 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2061 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2062 if (uq_first2 != NULL) {
2063 if (pri > UPRI(uq_first2->uq_thread))
2064 pri = UPRI(uq_first2->uq_thread);
2068 sched_lend_user_prio(td, pri);
2070 mtx_unlock(&umtx_lock);
2072 umtxq_signal_thread(uq_first);
2074 pi = umtx_pi_lookup(&key);
2076 * A umtx_pi can exist if a signal or timeout removed the
2077 * last waiter from the umtxq, but there is still
2078 * a thread in do_lock_pi() holding the umtx_pi.
2082 * The umtx_pi can be unowned, such as when a thread
2083 * has just entered do_lock_pi(), allocated the
2084 * umtx_pi, and unlocked the umtxq.
2085 * If the current thread owns it, it must disown it.
2087 mtx_lock(&umtx_lock);
2088 if (pi->pi_owner == td)
2090 mtx_unlock(&umtx_lock);
2096 * When unlocking the umtx, it must be marked as unowned if
2097 * there is zero or one thread only waiting for it.
2098 * Otherwise, it must be marked as contested.
2102 new_owner |= UMUTEX_CONTESTED;
2104 error = casueword32(&m->m_owner, owner, &old, new_owner);
2106 error = thread_check_susp(td, false);
2110 umtxq_unbusy_unlocked(&key);
2111 umtx_key_release(&key);
2114 if (error == 0 && old != owner)
2123 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2124 struct _umtx_time *timeout, int try)
2126 struct abs_timeout timo;
2127 struct umtx_q *uq, *uq2;
2131 int error, pri, old_inherited_pri, su, rv;
2135 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2136 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2140 if (timeout != NULL)
2141 abs_timeout_init2(&timo, timeout);
2143 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2145 old_inherited_pri = uq->uq_inherited_pri;
2146 umtxq_lock(&uq->uq_key);
2147 umtxq_busy(&uq->uq_key);
2148 umtxq_unlock(&uq->uq_key);
2150 rv = fueword32(&m->m_ceilings[0], &ceiling);
2155 ceiling = RTP_PRIO_MAX - ceiling;
2156 if (ceiling > RTP_PRIO_MAX) {
2161 mtx_lock(&umtx_lock);
2162 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2163 mtx_unlock(&umtx_lock);
2167 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2168 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2170 if (uq->uq_inherited_pri < UPRI(td))
2171 sched_lend_user_prio(td, uq->uq_inherited_pri);
2174 mtx_unlock(&umtx_lock);
2176 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2177 id | UMUTEX_CONTESTED);
2178 /* The address was invalid. */
2184 MPASS(owner == UMUTEX_CONTESTED);
2189 if (owner == UMUTEX_RB_OWNERDEAD) {
2190 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2191 &owner, id | UMUTEX_CONTESTED);
2197 MPASS(owner == UMUTEX_RB_OWNERDEAD);
2198 error = EOWNERDEAD; /* success */
2203 * rv == 1, only check for suspension if we
2204 * did not already catched a signal. If we
2205 * get an error from the check, the same
2206 * condition is checked by the umtxq_sleep()
2207 * call below, so we should obliterate the
2208 * error to not skip the last loop iteration.
2211 error = thread_check_susp(td, false);
2220 } else if (owner == UMUTEX_RB_NOTRECOV) {
2221 error = ENOTRECOVERABLE;
2228 * If we caught a signal, we have retried and now
2234 umtxq_lock(&uq->uq_key);
2236 umtxq_unbusy(&uq->uq_key);
2237 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2240 umtxq_unlock(&uq->uq_key);
2242 mtx_lock(&umtx_lock);
2243 uq->uq_inherited_pri = old_inherited_pri;
2245 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2246 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2248 if (pri > UPRI(uq2->uq_thread))
2249 pri = UPRI(uq2->uq_thread);
2252 if (pri > uq->uq_inherited_pri)
2253 pri = uq->uq_inherited_pri;
2255 sched_lend_user_prio(td, pri);
2257 mtx_unlock(&umtx_lock);
2260 if (error != 0 && error != EOWNERDEAD) {
2261 mtx_lock(&umtx_lock);
2262 uq->uq_inherited_pri = old_inherited_pri;
2264 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2265 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2267 if (pri > UPRI(uq2->uq_thread))
2268 pri = UPRI(uq2->uq_thread);
2271 if (pri > uq->uq_inherited_pri)
2272 pri = uq->uq_inherited_pri;
2274 sched_lend_user_prio(td, pri);
2276 mtx_unlock(&umtx_lock);
2280 umtxq_unbusy_unlocked(&uq->uq_key);
2281 umtx_key_release(&uq->uq_key);
2286 * Unlock a PP mutex.
2289 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2291 struct umtx_key key;
2292 struct umtx_q *uq, *uq2;
2294 uint32_t id, owner, rceiling;
2295 int error, pri, new_inherited_pri, su;
2299 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2302 * Make sure we own this mtx.
2304 error = fueword32(&m->m_owner, &owner);
2308 if ((owner & ~UMUTEX_CONTESTED) != id)
2311 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2316 new_inherited_pri = PRI_MAX;
2318 rceiling = RTP_PRIO_MAX - rceiling;
2319 if (rceiling > RTP_PRIO_MAX)
2321 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2324 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2325 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2332 * For priority protected mutex, always set unlocked state
2333 * to UMUTEX_CONTESTED, so that userland always enters kernel
2334 * to lock the mutex, it is necessary because thread priority
2335 * has to be adjusted for such mutex.
2337 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2342 umtxq_signal(&key, 1);
2349 mtx_lock(&umtx_lock);
2351 uq->uq_inherited_pri = new_inherited_pri;
2353 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2354 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2356 if (pri > UPRI(uq2->uq_thread))
2357 pri = UPRI(uq2->uq_thread);
2360 if (pri > uq->uq_inherited_pri)
2361 pri = uq->uq_inherited_pri;
2363 sched_lend_user_prio(td, pri);
2365 mtx_unlock(&umtx_lock);
2367 umtx_key_release(&key);
2372 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2373 uint32_t *old_ceiling)
2376 uint32_t flags, id, owner, save_ceiling;
2379 error = fueword32(&m->m_flags, &flags);
2382 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2384 if (ceiling > RTP_PRIO_MAX)
2388 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2389 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2393 umtxq_lock(&uq->uq_key);
2394 umtxq_busy(&uq->uq_key);
2395 umtxq_unlock(&uq->uq_key);
2397 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2403 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2404 id | UMUTEX_CONTESTED);
2411 MPASS(owner == UMUTEX_CONTESTED);
2412 rv = suword32(&m->m_ceilings[0], ceiling);
2413 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2414 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2418 if ((owner & ~UMUTEX_CONTESTED) == id) {
2419 rv = suword32(&m->m_ceilings[0], ceiling);
2420 error = rv == 0 ? 0 : EFAULT;
2424 if (owner == UMUTEX_RB_OWNERDEAD) {
2427 } else if (owner == UMUTEX_RB_NOTRECOV) {
2428 error = ENOTRECOVERABLE;
2433 * If we caught a signal, we have retried and now
2440 * We set the contested bit, sleep. Otherwise the lock changed
2441 * and we need to retry or we lost a race to the thread
2442 * unlocking the umtx.
2444 umtxq_lock(&uq->uq_key);
2446 umtxq_unbusy(&uq->uq_key);
2447 error = umtxq_sleep(uq, "umtxpp", NULL);
2449 umtxq_unlock(&uq->uq_key);
2451 umtxq_lock(&uq->uq_key);
2453 umtxq_signal(&uq->uq_key, INT_MAX);
2454 umtxq_unbusy(&uq->uq_key);
2455 umtxq_unlock(&uq->uq_key);
2456 umtx_key_release(&uq->uq_key);
2457 if (error == 0 && old_ceiling != NULL) {
2458 rv = suword32(old_ceiling, save_ceiling);
2459 error = rv == 0 ? 0 : EFAULT;
2465 * Lock a userland POSIX mutex.
2468 do_lock_umutex(struct thread *td, struct umutex *m,
2469 struct _umtx_time *timeout, int mode)
2474 error = fueword32(&m->m_flags, &flags);
2478 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2480 error = do_lock_normal(td, m, flags, timeout, mode);
2482 case UMUTEX_PRIO_INHERIT:
2483 error = do_lock_pi(td, m, flags, timeout, mode);
2485 case UMUTEX_PRIO_PROTECT:
2486 error = do_lock_pp(td, m, flags, timeout, mode);
2491 if (timeout == NULL) {
2492 if (error == EINTR && mode != _UMUTEX_WAIT)
2495 /* Timed-locking is not restarted. */
2496 if (error == ERESTART)
2503 * Unlock a userland POSIX mutex.
2506 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2511 error = fueword32(&m->m_flags, &flags);
2515 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2517 return (do_unlock_normal(td, m, flags, rb));
2518 case UMUTEX_PRIO_INHERIT:
2519 return (do_unlock_pi(td, m, flags, rb));
2520 case UMUTEX_PRIO_PROTECT:
2521 return (do_unlock_pp(td, m, flags, rb));
2528 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2529 struct timespec *timeout, u_long wflags)
2531 struct abs_timeout timo;
2533 uint32_t flags, clockid, hasw;
2537 error = fueword32(&cv->c_flags, &flags);
2540 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2544 if ((wflags & CVWAIT_CLOCKID) != 0) {
2545 error = fueword32(&cv->c_clockid, &clockid);
2547 umtx_key_release(&uq->uq_key);
2550 if (clockid < CLOCK_REALTIME ||
2551 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2552 /* hmm, only HW clock id will work. */
2553 umtx_key_release(&uq->uq_key);
2557 clockid = CLOCK_REALTIME;
2560 umtxq_lock(&uq->uq_key);
2561 umtxq_busy(&uq->uq_key);
2563 umtxq_unlock(&uq->uq_key);
2566 * Set c_has_waiters to 1 before releasing user mutex, also
2567 * don't modify cache line when unnecessary.
2569 error = fueword32(&cv->c_has_waiters, &hasw);
2570 if (error == 0 && hasw == 0)
2571 suword32(&cv->c_has_waiters, 1);
2573 umtxq_unbusy_unlocked(&uq->uq_key);
2575 error = do_unlock_umutex(td, m, false);
2577 if (timeout != NULL)
2578 abs_timeout_init(&timo, clockid, (wflags & CVWAIT_ABSTIME) != 0,
2581 umtxq_lock(&uq->uq_key);
2583 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2587 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2591 * This must be timeout,interrupted by signal or
2592 * surprious wakeup, clear c_has_waiter flag when
2595 umtxq_busy(&uq->uq_key);
2596 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2597 int oldlen = uq->uq_cur_queue->length;
2600 umtxq_unlock(&uq->uq_key);
2601 suword32(&cv->c_has_waiters, 0);
2602 umtxq_lock(&uq->uq_key);
2605 umtxq_unbusy(&uq->uq_key);
2606 if (error == ERESTART)
2610 umtxq_unlock(&uq->uq_key);
2611 umtx_key_release(&uq->uq_key);
2616 * Signal a userland condition variable.
2619 do_cv_signal(struct thread *td, struct ucond *cv)
2621 struct umtx_key key;
2622 int error, cnt, nwake;
2625 error = fueword32(&cv->c_flags, &flags);
2628 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2632 cnt = umtxq_count(&key);
2633 nwake = umtxq_signal(&key, 1);
2636 error = suword32(&cv->c_has_waiters, 0);
2643 umtx_key_release(&key);
2648 do_cv_broadcast(struct thread *td, struct ucond *cv)
2650 struct umtx_key key;
2654 error = fueword32(&cv->c_flags, &flags);
2657 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2662 umtxq_signal(&key, INT_MAX);
2665 error = suword32(&cv->c_has_waiters, 0);
2669 umtxq_unbusy_unlocked(&key);
2671 umtx_key_release(&key);
2676 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
2677 struct _umtx_time *timeout)
2679 struct abs_timeout timo;
2681 uint32_t flags, wrflags;
2682 int32_t state, oldstate;
2683 int32_t blocked_readers;
2684 int error, error1, rv;
2687 error = fueword32(&rwlock->rw_flags, &flags);
2690 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2694 if (timeout != NULL)
2695 abs_timeout_init2(&timo, timeout);
2697 wrflags = URWLOCK_WRITE_OWNER;
2698 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2699 wrflags |= URWLOCK_WRITE_WAITERS;
2702 rv = fueword32(&rwlock->rw_state, &state);
2704 umtx_key_release(&uq->uq_key);
2708 /* try to lock it */
2709 while (!(state & wrflags)) {
2710 if (__predict_false(URWLOCK_READER_COUNT(state) ==
2711 URWLOCK_MAX_READERS)) {
2712 umtx_key_release(&uq->uq_key);
2715 rv = casueword32(&rwlock->rw_state, state,
2716 &oldstate, state + 1);
2718 umtx_key_release(&uq->uq_key);
2722 MPASS(oldstate == state);
2723 umtx_key_release(&uq->uq_key);
2726 error = thread_check_susp(td, true);
2735 /* grab monitor lock */
2736 umtxq_lock(&uq->uq_key);
2737 umtxq_busy(&uq->uq_key);
2738 umtxq_unlock(&uq->uq_key);
2741 * re-read the state, in case it changed between the try-lock above
2742 * and the check below
2744 rv = fueword32(&rwlock->rw_state, &state);
2748 /* set read contention bit */
2749 while (error == 0 && (state & wrflags) &&
2750 !(state & URWLOCK_READ_WAITERS)) {
2751 rv = casueword32(&rwlock->rw_state, state,
2752 &oldstate, state | URWLOCK_READ_WAITERS);
2758 MPASS(oldstate == state);
2762 error = thread_check_susp(td, false);
2767 umtxq_unbusy_unlocked(&uq->uq_key);
2771 /* state is changed while setting flags, restart */
2772 if (!(state & wrflags)) {
2773 umtxq_unbusy_unlocked(&uq->uq_key);
2774 error = thread_check_susp(td, true);
2782 * Contention bit is set, before sleeping, increase
2783 * read waiter count.
2785 rv = fueword32(&rwlock->rw_blocked_readers,
2788 umtxq_unbusy_unlocked(&uq->uq_key);
2792 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2794 while (state & wrflags) {
2795 umtxq_lock(&uq->uq_key);
2797 umtxq_unbusy(&uq->uq_key);
2799 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2802 umtxq_busy(&uq->uq_key);
2804 umtxq_unlock(&uq->uq_key);
2807 rv = fueword32(&rwlock->rw_state, &state);
2814 /* decrease read waiter count, and may clear read contention bit */
2815 rv = fueword32(&rwlock->rw_blocked_readers,
2818 umtxq_unbusy_unlocked(&uq->uq_key);
2822 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2823 if (blocked_readers == 1) {
2824 rv = fueword32(&rwlock->rw_state, &state);
2826 umtxq_unbusy_unlocked(&uq->uq_key);
2831 rv = casueword32(&rwlock->rw_state, state,
2832 &oldstate, state & ~URWLOCK_READ_WAITERS);
2838 MPASS(oldstate == state);
2842 error1 = thread_check_susp(td, false);
2851 umtxq_unbusy_unlocked(&uq->uq_key);
2855 umtx_key_release(&uq->uq_key);
2856 if (error == ERESTART)
2862 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2864 struct abs_timeout timo;
2867 int32_t state, oldstate;
2868 int32_t blocked_writers;
2869 int32_t blocked_readers;
2870 int error, error1, rv;
2873 error = fueword32(&rwlock->rw_flags, &flags);
2876 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2880 if (timeout != NULL)
2881 abs_timeout_init2(&timo, timeout);
2883 blocked_readers = 0;
2885 rv = fueword32(&rwlock->rw_state, &state);
2887 umtx_key_release(&uq->uq_key);
2890 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
2891 URWLOCK_READER_COUNT(state) == 0) {
2892 rv = casueword32(&rwlock->rw_state, state,
2893 &oldstate, state | URWLOCK_WRITE_OWNER);
2895 umtx_key_release(&uq->uq_key);
2899 MPASS(oldstate == state);
2900 umtx_key_release(&uq->uq_key);
2904 error = thread_check_susp(td, true);
2910 if ((state & (URWLOCK_WRITE_OWNER |
2911 URWLOCK_WRITE_WAITERS)) == 0 &&
2912 blocked_readers != 0) {
2913 umtxq_lock(&uq->uq_key);
2914 umtxq_busy(&uq->uq_key);
2915 umtxq_signal_queue(&uq->uq_key, INT_MAX,
2917 umtxq_unbusy(&uq->uq_key);
2918 umtxq_unlock(&uq->uq_key);
2924 /* grab monitor lock */
2925 umtxq_lock(&uq->uq_key);
2926 umtxq_busy(&uq->uq_key);
2927 umtxq_unlock(&uq->uq_key);
2930 * Re-read the state, in case it changed between the
2931 * try-lock above and the check below.
2933 rv = fueword32(&rwlock->rw_state, &state);
2937 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
2938 URWLOCK_READER_COUNT(state) != 0) &&
2939 (state & URWLOCK_WRITE_WAITERS) == 0) {
2940 rv = casueword32(&rwlock->rw_state, state,
2941 &oldstate, state | URWLOCK_WRITE_WAITERS);
2947 MPASS(oldstate == state);
2951 error = thread_check_susp(td, false);
2956 umtxq_unbusy_unlocked(&uq->uq_key);
2960 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
2961 URWLOCK_READER_COUNT(state) == 0) {
2962 umtxq_unbusy_unlocked(&uq->uq_key);
2963 error = thread_check_susp(td, false);
2969 rv = fueword32(&rwlock->rw_blocked_writers,
2972 umtxq_unbusy_unlocked(&uq->uq_key);
2976 suword32(&rwlock->rw_blocked_writers, blocked_writers + 1);
2978 while ((state & URWLOCK_WRITE_OWNER) ||
2979 URWLOCK_READER_COUNT(state) != 0) {
2980 umtxq_lock(&uq->uq_key);
2981 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2982 umtxq_unbusy(&uq->uq_key);
2984 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
2987 umtxq_busy(&uq->uq_key);
2988 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2989 umtxq_unlock(&uq->uq_key);
2992 rv = fueword32(&rwlock->rw_state, &state);
2999 rv = fueword32(&rwlock->rw_blocked_writers,
3002 umtxq_unbusy_unlocked(&uq->uq_key);
3006 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
3007 if (blocked_writers == 1) {
3008 rv = fueword32(&rwlock->rw_state, &state);
3010 umtxq_unbusy_unlocked(&uq->uq_key);
3015 rv = casueword32(&rwlock->rw_state, state,
3016 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3022 MPASS(oldstate == state);
3026 error1 = thread_check_susp(td, false);
3028 * We are leaving the URWLOCK_WRITE_WAITERS
3029 * behind, but this should not harm the
3038 rv = fueword32(&rwlock->rw_blocked_readers,
3041 umtxq_unbusy_unlocked(&uq->uq_key);
3046 blocked_readers = 0;
3048 umtxq_unbusy_unlocked(&uq->uq_key);
3051 umtx_key_release(&uq->uq_key);
3052 if (error == ERESTART)
3058 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3062 int32_t state, oldstate;
3063 int error, rv, q, count;
3066 error = fueword32(&rwlock->rw_flags, &flags);
3069 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3073 error = fueword32(&rwlock->rw_state, &state);
3078 if (state & URWLOCK_WRITE_OWNER) {
3080 rv = casueword32(&rwlock->rw_state, state,
3081 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3088 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3092 error = thread_check_susp(td, true);
3098 } else if (URWLOCK_READER_COUNT(state) != 0) {
3100 rv = casueword32(&rwlock->rw_state, state,
3101 &oldstate, state - 1);
3108 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3112 error = thread_check_susp(td, true);
3125 if (!(flags & URWLOCK_PREFER_READER)) {
3126 if (state & URWLOCK_WRITE_WAITERS) {
3128 q = UMTX_EXCLUSIVE_QUEUE;
3129 } else if (state & URWLOCK_READ_WAITERS) {
3131 q = UMTX_SHARED_QUEUE;
3134 if (state & URWLOCK_READ_WAITERS) {
3136 q = UMTX_SHARED_QUEUE;
3137 } else if (state & URWLOCK_WRITE_WAITERS) {
3139 q = UMTX_EXCLUSIVE_QUEUE;
3144 umtxq_lock(&uq->uq_key);
3145 umtxq_busy(&uq->uq_key);
3146 umtxq_signal_queue(&uq->uq_key, count, q);
3147 umtxq_unbusy(&uq->uq_key);
3148 umtxq_unlock(&uq->uq_key);
3151 umtx_key_release(&uq->uq_key);
3155 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3157 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3159 struct abs_timeout timo;
3161 uint32_t flags, count, count1;
3165 error = fueword32(&sem->_flags, &flags);
3168 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3172 if (timeout != NULL)
3173 abs_timeout_init2(&timo, timeout);
3176 umtxq_lock(&uq->uq_key);
3177 umtxq_busy(&uq->uq_key);
3179 umtxq_unlock(&uq->uq_key);
3180 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3182 rv1 = fueword32(&sem->_count, &count);
3183 if (rv == -1 || (rv == 0 && (rv1 == -1 || count != 0)) ||
3184 (rv == 1 && count1 == 0)) {
3185 umtxq_lock(&uq->uq_key);
3186 umtxq_unbusy(&uq->uq_key);
3188 umtxq_unlock(&uq->uq_key);
3190 rv = thread_check_susp(td, true);
3198 error = rv == -1 ? EFAULT : 0;
3201 umtxq_lock(&uq->uq_key);
3202 umtxq_unbusy(&uq->uq_key);
3204 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3206 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3210 /* A relative timeout cannot be restarted. */
3211 if (error == ERESTART && timeout != NULL &&
3212 (timeout->_flags & UMTX_ABSTIME) == 0)
3215 umtxq_unlock(&uq->uq_key);
3217 umtx_key_release(&uq->uq_key);
3222 * Signal a userland semaphore.
3225 do_sem_wake(struct thread *td, struct _usem *sem)
3227 struct umtx_key key;
3231 error = fueword32(&sem->_flags, &flags);
3234 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3238 cnt = umtxq_count(&key);
3241 * Check if count is greater than 0, this means the memory is
3242 * still being referenced by user code, so we can safely
3243 * update _has_waiters flag.
3247 error = suword32(&sem->_has_waiters, 0);
3252 umtxq_signal(&key, 1);
3256 umtx_key_release(&key);
3262 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3264 struct abs_timeout timo;
3266 uint32_t count, flags;
3270 flags = fuword32(&sem->_flags);
3271 if (timeout != NULL)
3272 abs_timeout_init2(&timo, timeout);
3275 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3278 umtxq_lock(&uq->uq_key);
3279 umtxq_busy(&uq->uq_key);
3281 umtxq_unlock(&uq->uq_key);
3282 rv = fueword32(&sem->_count, &count);
3284 umtxq_lock(&uq->uq_key);
3285 umtxq_unbusy(&uq->uq_key);
3287 umtxq_unlock(&uq->uq_key);
3288 umtx_key_release(&uq->uq_key);
3292 if (USEM_COUNT(count) != 0) {
3293 umtxq_lock(&uq->uq_key);
3294 umtxq_unbusy(&uq->uq_key);
3296 umtxq_unlock(&uq->uq_key);
3297 umtx_key_release(&uq->uq_key);
3300 if (count == USEM_HAS_WAITERS)
3302 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3305 umtxq_lock(&uq->uq_key);
3306 umtxq_unbusy(&uq->uq_key);
3308 umtxq_unlock(&uq->uq_key);
3309 umtx_key_release(&uq->uq_key);
3312 rv = thread_check_susp(td, true);
3317 umtxq_lock(&uq->uq_key);
3318 umtxq_unbusy(&uq->uq_key);
3320 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3322 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3326 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3327 /* A relative timeout cannot be restarted. */
3328 if (error == ERESTART)
3330 if (error == EINTR) {
3331 abs_timeout_update(&timo);
3332 timespecsub(&timo.end, &timo.cur,
3333 &timeout->_timeout);
3337 umtxq_unlock(&uq->uq_key);
3338 umtx_key_release(&uq->uq_key);
3343 * Signal a userland semaphore.
3346 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3348 struct umtx_key key;
3350 uint32_t count, flags;
3352 rv = fueword32(&sem->_flags, &flags);
3355 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3359 cnt = umtxq_count(&key);
3362 * If this was the last sleeping thread, clear the waiters
3367 rv = fueword32(&sem->_count, &count);
3368 while (rv != -1 && count & USEM_HAS_WAITERS) {
3369 rv = casueword32(&sem->_count, count, &count,
3370 count & ~USEM_HAS_WAITERS);
3372 rv = thread_check_susp(td, true);
3385 umtxq_signal(&key, 1);
3389 umtx_key_release(&key);
3394 umtx_copyin_timeout(const void *uaddr, struct timespec *tsp)
3398 error = copyin(uaddr, tsp, sizeof(*tsp));
3400 if (tsp->tv_sec < 0 ||
3401 tsp->tv_nsec >= 1000000000 ||
3409 umtx_copyin_umtx_time(const void *uaddr, size_t size, struct _umtx_time *tp)
3413 if (size <= sizeof(tp->_timeout)) {
3414 tp->_clockid = CLOCK_REALTIME;
3416 error = copyin(uaddr, &tp->_timeout, sizeof(tp->_timeout));
3418 error = copyin(uaddr, tp, sizeof(*tp));
3421 if (tp->_timeout.tv_sec < 0 ||
3422 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3428 umtx_copyin_robust_lists(const void *uaddr, size_t size,
3429 struct umtx_robust_lists_params *rb)
3432 if (size > sizeof(*rb))
3434 return (copyin(uaddr, rb, size));
3438 umtx_copyout_timeout(void *uaddr, size_t sz, struct timespec *tsp)
3442 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
3443 * and we're only called if sz >= sizeof(timespec) as supplied in the
3446 KASSERT(sz >= sizeof(*tsp),
3447 ("umtx_copyops specifies incorrect sizes"));
3449 return (copyout(tsp, uaddr, sizeof(*tsp)));
3453 __umtx_op_unimpl(struct thread *td, struct _umtx_op_args *uap,
3454 const struct umtx_copyops *ops __unused)
3457 return (EOPNOTSUPP);
3461 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap,
3462 const struct umtx_copyops *ops)
3464 struct _umtx_time timeout, *tm_p;
3467 if (uap->uaddr2 == NULL)
3470 error = ops->copyin_umtx_time(
3471 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3476 return (do_wait(td, uap->obj, uap->val, tm_p, ops->compat32, 0));
3480 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap,
3481 const struct umtx_copyops *ops)
3483 struct _umtx_time timeout, *tm_p;
3486 if (uap->uaddr2 == NULL)
3489 error = ops->copyin_umtx_time(
3490 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3495 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3499 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap,
3500 const struct umtx_copyops *ops)
3502 struct _umtx_time *tm_p, timeout;
3505 if (uap->uaddr2 == NULL)
3508 error = ops->copyin_umtx_time(
3509 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3514 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3518 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap,
3519 const struct umtx_copyops *ops __unused)
3522 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3525 #define BATCH_SIZE 128
3527 __umtx_op_nwake_private_native(struct thread *td, struct _umtx_op_args *uap)
3529 char *uaddrs[BATCH_SIZE], **upp;
3530 int count, error, i, pos, tocopy;
3532 upp = (char **)uap->obj;
3534 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3536 tocopy = MIN(count, BATCH_SIZE);
3537 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3540 for (i = 0; i < tocopy; ++i) {
3541 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3549 __umtx_op_nwake_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3551 uint32_t uaddrs[BATCH_SIZE], *upp;
3552 int count, error, i, pos, tocopy;
3554 upp = (uint32_t *)uap->obj;
3556 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3558 tocopy = MIN(count, BATCH_SIZE);
3559 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
3562 for (i = 0; i < tocopy; ++i) {
3563 kern_umtx_wake(td, (void *)(uintptr_t)uaddrs[i],
3572 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap,
3573 const struct umtx_copyops *ops)
3577 return (__umtx_op_nwake_private_compat32(td, uap));
3578 return (__umtx_op_nwake_private_native(td, uap));
3582 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap,
3583 const struct umtx_copyops *ops __unused)
3586 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3590 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap,
3591 const struct umtx_copyops *ops)
3593 struct _umtx_time *tm_p, timeout;
3596 /* Allow a null timespec (wait forever). */
3597 if (uap->uaddr2 == NULL)
3600 error = ops->copyin_umtx_time(
3601 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3606 return (do_lock_umutex(td, uap->obj, tm_p, 0));
3610 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap,
3611 const struct umtx_copyops *ops __unused)
3614 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
3618 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap,
3619 const struct umtx_copyops *ops)
3621 struct _umtx_time *tm_p, timeout;
3624 /* Allow a null timespec (wait forever). */
3625 if (uap->uaddr2 == NULL)
3628 error = ops->copyin_umtx_time(
3629 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3634 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
3638 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap,
3639 const struct umtx_copyops *ops __unused)
3642 return (do_wake_umutex(td, uap->obj));
3646 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap,
3647 const struct umtx_copyops *ops __unused)
3650 return (do_unlock_umutex(td, uap->obj, false));
3654 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap,
3655 const struct umtx_copyops *ops __unused)
3658 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
3662 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap,
3663 const struct umtx_copyops *ops)
3665 struct timespec *ts, timeout;
3668 /* Allow a null timespec (wait forever). */
3669 if (uap->uaddr2 == NULL)
3672 error = ops->copyin_timeout(uap->uaddr2, &timeout);
3677 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3681 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap,
3682 const struct umtx_copyops *ops __unused)
3685 return (do_cv_signal(td, uap->obj));
3689 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap,
3690 const struct umtx_copyops *ops __unused)
3693 return (do_cv_broadcast(td, uap->obj));
3697 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap,
3698 const struct umtx_copyops *ops)
3700 struct _umtx_time timeout;
3703 /* Allow a null timespec (wait forever). */
3704 if (uap->uaddr2 == NULL) {
3705 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3707 error = ops->copyin_umtx_time(uap->uaddr2,
3708 (size_t)uap->uaddr1, &timeout);
3711 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3717 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap,
3718 const struct umtx_copyops *ops)
3720 struct _umtx_time timeout;
3723 /* Allow a null timespec (wait forever). */
3724 if (uap->uaddr2 == NULL) {
3725 error = do_rw_wrlock(td, uap->obj, 0);
3727 error = ops->copyin_umtx_time(uap->uaddr2,
3728 (size_t)uap->uaddr1, &timeout);
3732 error = do_rw_wrlock(td, uap->obj, &timeout);
3738 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap,
3739 const struct umtx_copyops *ops __unused)
3742 return (do_rw_unlock(td, uap->obj));
3745 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3747 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap,
3748 const struct umtx_copyops *ops)
3750 struct _umtx_time *tm_p, timeout;
3753 /* Allow a null timespec (wait forever). */
3754 if (uap->uaddr2 == NULL)
3757 error = ops->copyin_umtx_time(
3758 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3763 return (do_sem_wait(td, uap->obj, tm_p));
3767 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap,
3768 const struct umtx_copyops *ops __unused)
3771 return (do_sem_wake(td, uap->obj));
3776 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap,
3777 const struct umtx_copyops *ops __unused)
3780 return (do_wake2_umutex(td, uap->obj, uap->val));
3784 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap,
3785 const struct umtx_copyops *ops)
3787 struct _umtx_time *tm_p, timeout;
3791 /* Allow a null timespec (wait forever). */
3792 if (uap->uaddr2 == NULL) {
3796 uasize = (size_t)uap->uaddr1;
3797 error = ops->copyin_umtx_time(uap->uaddr2, uasize, &timeout);
3802 error = do_sem2_wait(td, uap->obj, tm_p);
3803 if (error == EINTR && uap->uaddr2 != NULL &&
3804 (timeout._flags & UMTX_ABSTIME) == 0 &&
3805 uasize >= ops->umtx_time_sz + ops->timespec_sz) {
3806 error = ops->copyout_timeout(
3807 (void *)((uintptr_t)uap->uaddr2 + ops->umtx_time_sz),
3808 uasize - ops->umtx_time_sz, &timeout._timeout);
3818 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap,
3819 const struct umtx_copyops *ops __unused)
3822 return (do_sem2_wake(td, uap->obj));
3825 #define USHM_OBJ_UMTX(o) \
3826 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
3828 #define USHMF_REG_LINKED 0x0001
3829 #define USHMF_OBJ_LINKED 0x0002
3830 struct umtx_shm_reg {
3831 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
3832 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
3833 struct umtx_key ushm_key;
3834 struct ucred *ushm_cred;
3835 struct shmfd *ushm_obj;
3840 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
3841 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
3843 static uma_zone_t umtx_shm_reg_zone;
3844 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
3845 static struct mtx umtx_shm_lock;
3846 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
3847 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
3849 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
3852 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
3854 struct umtx_shm_reg_head d;
3855 struct umtx_shm_reg *reg, *reg1;
3858 mtx_lock(&umtx_shm_lock);
3859 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
3860 mtx_unlock(&umtx_shm_lock);
3861 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
3862 TAILQ_REMOVE(&d, reg, ushm_reg_link);
3863 umtx_shm_free_reg(reg);
3867 static struct task umtx_shm_reg_delfree_task =
3868 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
3870 static struct umtx_shm_reg *
3871 umtx_shm_find_reg_locked(const struct umtx_key *key)
3873 struct umtx_shm_reg *reg;
3874 struct umtx_shm_reg_head *reg_head;
3876 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
3877 mtx_assert(&umtx_shm_lock, MA_OWNED);
3878 reg_head = &umtx_shm_registry[key->hash];
3879 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
3880 KASSERT(reg->ushm_key.shared,
3881 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
3882 if (reg->ushm_key.info.shared.object ==
3883 key->info.shared.object &&
3884 reg->ushm_key.info.shared.offset ==
3885 key->info.shared.offset) {
3886 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
3887 KASSERT(reg->ushm_refcnt > 0,
3888 ("reg %p refcnt 0 onlist", reg));
3889 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
3890 ("reg %p not linked", reg));
3898 static struct umtx_shm_reg *
3899 umtx_shm_find_reg(const struct umtx_key *key)
3901 struct umtx_shm_reg *reg;
3903 mtx_lock(&umtx_shm_lock);
3904 reg = umtx_shm_find_reg_locked(key);
3905 mtx_unlock(&umtx_shm_lock);
3910 umtx_shm_free_reg(struct umtx_shm_reg *reg)
3913 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
3914 crfree(reg->ushm_cred);
3915 shm_drop(reg->ushm_obj);
3916 uma_zfree(umtx_shm_reg_zone, reg);
3920 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
3924 mtx_assert(&umtx_shm_lock, MA_OWNED);
3925 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
3927 res = reg->ushm_refcnt == 0;
3929 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
3930 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
3931 reg, ushm_reg_link);
3932 reg->ushm_flags &= ~USHMF_REG_LINKED;
3934 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
3935 LIST_REMOVE(reg, ushm_obj_link);
3936 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
3943 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
3949 object = reg->ushm_obj->shm_object;
3950 VM_OBJECT_WLOCK(object);
3951 object->flags |= OBJ_UMTXDEAD;
3952 VM_OBJECT_WUNLOCK(object);
3954 mtx_lock(&umtx_shm_lock);
3955 dofree = umtx_shm_unref_reg_locked(reg, force);
3956 mtx_unlock(&umtx_shm_lock);
3958 umtx_shm_free_reg(reg);
3962 umtx_shm_object_init(vm_object_t object)
3965 LIST_INIT(USHM_OBJ_UMTX(object));
3969 umtx_shm_object_terminated(vm_object_t object)
3971 struct umtx_shm_reg *reg, *reg1;
3974 if (LIST_EMPTY(USHM_OBJ_UMTX(object)))
3978 mtx_lock(&umtx_shm_lock);
3979 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
3980 if (umtx_shm_unref_reg_locked(reg, true)) {
3981 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
3986 mtx_unlock(&umtx_shm_lock);
3988 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
3992 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
3993 struct umtx_shm_reg **res)
3995 struct umtx_shm_reg *reg, *reg1;
3999 reg = umtx_shm_find_reg(key);
4004 cred = td->td_ucred;
4005 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
4007 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
4008 reg->ushm_refcnt = 1;
4009 bcopy(key, ®->ushm_key, sizeof(*key));
4010 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR, false);
4011 reg->ushm_cred = crhold(cred);
4012 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
4014 umtx_shm_free_reg(reg);
4017 mtx_lock(&umtx_shm_lock);
4018 reg1 = umtx_shm_find_reg_locked(key);
4020 mtx_unlock(&umtx_shm_lock);
4021 umtx_shm_free_reg(reg);
4026 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
4027 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
4029 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
4030 mtx_unlock(&umtx_shm_lock);
4036 umtx_shm_alive(struct thread *td, void *addr)
4039 vm_map_entry_t entry;
4046 map = &td->td_proc->p_vmspace->vm_map;
4047 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
4048 &object, &pindex, &prot, &wired);
4049 if (res != KERN_SUCCESS)
4054 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
4055 vm_map_lookup_done(map, entry);
4064 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
4065 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
4066 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
4067 for (i = 0; i < nitems(umtx_shm_registry); i++)
4068 TAILQ_INIT(&umtx_shm_registry[i]);
4072 umtx_shm(struct thread *td, void *addr, u_int flags)
4074 struct umtx_key key;
4075 struct umtx_shm_reg *reg;
4079 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
4080 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
4082 if ((flags & UMTX_SHM_ALIVE) != 0)
4083 return (umtx_shm_alive(td, addr));
4084 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
4087 KASSERT(key.shared == 1, ("non-shared key"));
4088 if ((flags & UMTX_SHM_CREAT) != 0) {
4089 error = umtx_shm_create_reg(td, &key, ®);
4091 reg = umtx_shm_find_reg(&key);
4095 umtx_key_release(&key);
4098 KASSERT(reg != NULL, ("no reg"));
4099 if ((flags & UMTX_SHM_DESTROY) != 0) {
4100 umtx_shm_unref_reg(reg, true);
4104 error = mac_posixshm_check_open(td->td_ucred,
4105 reg->ushm_obj, FFLAGS(O_RDWR));
4108 error = shm_access(reg->ushm_obj, td->td_ucred,
4112 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
4114 shm_hold(reg->ushm_obj);
4115 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
4117 td->td_retval[0] = fd;
4121 umtx_shm_unref_reg(reg, false);
4126 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap,
4127 const struct umtx_copyops *ops __unused)
4130 return (umtx_shm(td, uap->uaddr1, uap->val));
4134 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap,
4135 const struct umtx_copyops *ops)
4137 struct umtx_robust_lists_params rb;
4140 bzero(&rb, sizeof(rb));
4141 error = ops->copyin_robust_lists(uap->uaddr1, uap->val, &rb);
4146 td->td_pflags2 |= TDP2_COMPAT32RB;
4147 else if ((td->td_pflags2 & TDP2_COMPAT32RB) != 0)
4150 td->td_rb_list = rb.robust_list_offset;
4151 td->td_rbp_list = rb.robust_priv_list_offset;
4152 td->td_rb_inact = rb.robust_inact_offset;
4156 #ifdef COMPAT_FREEBSD32
4158 umtx_copyin_timeout32(const void *uaddr, struct timespec *tsp)
4160 struct timespec32 ts32;
4163 error = copyin(uaddr, &ts32, sizeof(ts32));
4165 if (ts32.tv_sec < 0 ||
4166 ts32.tv_nsec >= 1000000000 ||
4170 CP(ts32, *tsp, tv_sec);
4171 CP(ts32, *tsp, tv_nsec);
4178 umtx_copyin_umtx_time32(const void *uaddr, size_t size, struct _umtx_time *tp)
4180 struct umtx_time32 t32;
4183 t32._clockid = CLOCK_REALTIME;
4185 if (size <= sizeof(t32._timeout))
4186 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4188 error = copyin(uaddr, &t32, sizeof(t32));
4191 if (t32._timeout.tv_sec < 0 ||
4192 t32._timeout.tv_nsec >= 1000000000 || t32._timeout.tv_nsec < 0)
4194 TS_CP(t32, *tp, _timeout);
4195 CP(t32, *tp, _flags);
4196 CP(t32, *tp, _clockid);
4201 umtx_copyin_robust_lists32(const void *uaddr, size_t size,
4202 struct umtx_robust_lists_params *rbp)
4204 struct umtx_robust_lists_params_compat32 rb32;
4207 if (size > sizeof(rb32))
4209 bzero(&rb32, sizeof(rb32));
4210 error = copyin(uaddr, &rb32, size);
4213 CP(rb32, *rbp, robust_list_offset);
4214 CP(rb32, *rbp, robust_priv_list_offset);
4215 CP(rb32, *rbp, robust_inact_offset);
4220 umtx_copyout_timeout32(void *uaddr, size_t sz, struct timespec *tsp)
4222 struct timespec32 remain32 = {
4223 .tv_sec = tsp->tv_sec,
4224 .tv_nsec = tsp->tv_nsec,
4228 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4229 * and we're only called if sz >= sizeof(timespec) as supplied in the
4232 KASSERT(sz >= sizeof(remain32),
4233 ("umtx_copyops specifies incorrect sizes"));
4235 return (copyout(&remain32, uaddr, sizeof(remain32)));
4237 #endif /* COMPAT_FREEBSD32 */
4239 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap,
4240 const struct umtx_copyops *umtx_ops);
4242 static const _umtx_op_func op_table[] = {
4243 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
4244 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
4245 [UMTX_OP_WAIT] = __umtx_op_wait,
4246 [UMTX_OP_WAKE] = __umtx_op_wake,
4247 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4248 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
4249 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4250 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4251 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
4252 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4253 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4254 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
4255 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
4256 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
4257 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4258 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4259 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4260 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4261 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4262 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4263 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4264 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4266 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4267 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4269 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4270 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4271 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4272 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4273 [UMTX_OP_SHM] = __umtx_op_shm,
4274 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4277 static const struct umtx_copyops umtx_native_ops = {
4278 .copyin_timeout = umtx_copyin_timeout,
4279 .copyin_umtx_time = umtx_copyin_umtx_time,
4280 .copyin_robust_lists = umtx_copyin_robust_lists,
4281 .copyout_timeout = umtx_copyout_timeout,
4282 .timespec_sz = sizeof(struct timespec),
4283 .umtx_time_sz = sizeof(struct _umtx_time),
4286 #ifdef COMPAT_FREEBSD32
4287 const struct umtx_copyops umtx_native_ops32 = {
4288 .copyin_timeout = umtx_copyin_timeout32,
4289 .copyin_umtx_time = umtx_copyin_umtx_time32,
4290 .copyin_robust_lists = umtx_copyin_robust_lists32,
4291 .copyout_timeout = umtx_copyout_timeout32,
4292 .timespec_sz = sizeof(struct timespec32),
4293 .umtx_time_sz = sizeof(struct umtx_time32),
4299 kern__umtx_op(struct thread *td, void *obj, int op, unsigned long val,
4300 void *uaddr1, void *uaddr2, const struct umtx_copyops *ops)
4302 struct _umtx_op_args uap = {
4310 if ((uap.op >= nitems(op_table)))
4312 return ((*op_table[uap.op])(td, &uap, ops));
4316 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4319 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr1,
4320 uap->uaddr2, &umtx_native_ops));
4324 umtx_thread_init(struct thread *td)
4327 td->td_umtxq = umtxq_alloc();
4328 td->td_umtxq->uq_thread = td;
4332 umtx_thread_fini(struct thread *td)
4335 umtxq_free(td->td_umtxq);
4339 * It will be called when new thread is created, e.g fork().
4342 umtx_thread_alloc(struct thread *td)
4347 uq->uq_inherited_pri = PRI_MAX;
4349 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4350 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4351 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4352 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4358 * Clear robust lists for all process' threads, not delaying the
4359 * cleanup to thread_exit hook, since the relevant address space is
4360 * destroyed right now.
4363 umtx_exec_hook(void *arg __unused, struct proc *p,
4364 struct image_params *imgp __unused)
4368 KASSERT(p == curproc, ("need curproc"));
4369 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4370 (p->p_flag & P_STOPPED_SINGLE) != 0,
4371 ("curproc must be single-threaded"));
4373 * There is no need to lock the list as only this thread can be
4376 FOREACH_THREAD_IN_PROC(p, td) {
4377 KASSERT(td == curthread ||
4378 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4379 ("running thread %p %p", p, td));
4380 umtx_thread_cleanup(td);
4381 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4386 * thread_exit() hook.
4389 umtx_thread_exit(struct thread *td)
4392 umtx_thread_cleanup(td);
4396 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res, bool compat32)
4399 #ifdef COMPAT_FREEBSD32
4404 #ifdef COMPAT_FREEBSD32
4406 error = fueword32((void *)ptr, &res32);
4412 error = fueword((void *)ptr, &res1);
4422 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list,
4425 #ifdef COMPAT_FREEBSD32
4426 struct umutex32 m32;
4429 memcpy(&m32, m, sizeof(m32));
4430 *rb_list = m32.m_rb_lnk;
4433 *rb_list = m->m_rb_lnk;
4437 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact,
4443 KASSERT(td->td_proc == curproc, ("need current vmspace"));
4444 error = copyin((void *)rbp, &m, sizeof(m));
4447 if (rb_list != NULL)
4448 umtx_read_rb_list(td, &m, rb_list, compat32);
4449 if ((m.m_flags & UMUTEX_ROBUST) == 0)
4451 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
4452 /* inact is cleared after unlock, allow the inconsistency */
4453 return (inact ? 0 : EINVAL);
4454 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
4458 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
4459 const char *name, bool compat32)
4467 error = umtx_read_uptr(td, rb_list, &rbp, compat32);
4468 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
4469 if (rbp == *rb_inact) {
4474 error = umtx_handle_rb(td, rbp, &rbp, inact, compat32);
4476 if (i == umtx_max_rb && umtx_verbose_rb) {
4477 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
4478 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
4480 if (error != 0 && umtx_verbose_rb) {
4481 uprintf("comm %s pid %d: handling %srb error %d\n",
4482 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
4487 * Clean up umtx data.
4490 umtx_thread_cleanup(struct thread *td)
4498 * Disown pi mutexes.
4502 if (uq->uq_inherited_pri != PRI_MAX ||
4503 !TAILQ_EMPTY(&uq->uq_pi_contested)) {
4504 mtx_lock(&umtx_lock);
4505 uq->uq_inherited_pri = PRI_MAX;
4506 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
4507 pi->pi_owner = NULL;
4508 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
4510 mtx_unlock(&umtx_lock);
4512 sched_lend_user_prio_cond(td, PRI_MAX);
4515 compat32 = (td->td_pflags2 & TDP2_COMPAT32RB) != 0;
4516 td->td_pflags2 &= ~TDP2_COMPAT32RB;
4518 if (td->td_rb_inact == 0 && td->td_rb_list == 0 && td->td_rbp_list == 0)
4522 * Handle terminated robust mutexes. Must be done after
4523 * robust pi disown, otherwise unlock could see unowned
4526 rb_inact = td->td_rb_inact;
4528 (void)umtx_read_uptr(td, rb_inact, &rb_inact, compat32);
4529 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "", compat32);
4530 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ", compat32);
4532 (void)umtx_handle_rb(td, rb_inact, NULL, true, compat32);