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 struct umtx_copyops {
223 int (*copyin_timeout)(const void *uaddr, struct timespec *tsp);
224 int (*copyin_umtx_time)(const void *uaddr, size_t size,
225 struct _umtx_time *tp);
226 int (*copyin_robust_lists)(const void *uaddr, size_t size,
227 struct umtx_robust_lists_params *rbp);
228 int (*copyout_timeout)(void *uaddr, size_t size,
229 struct timespec *tsp);
230 const size_t timespec_sz;
231 const size_t umtx_time_sz;
235 #ifdef COMPAT_FREEBSD32
237 struct timespec32 _timeout;
242 struct umtx_robust_lists_params_compat32 {
243 uint32_t robust_list_offset;
244 uint32_t robust_priv_list_offset;
245 uint32_t robust_inact_offset;
249 volatile __lwpid_t m_owner; /* Owner of the mutex */
250 __uint32_t m_flags; /* Flags of the mutex */
251 __uint32_t m_ceilings[2]; /* Priority protect ceiling */
252 __uint32_t m_rb_lnk; /* Robust linkage */
254 __uint32_t m_spare[2];
257 _Static_assert(sizeof(struct umutex) == sizeof(struct umutex32), "umutex32");
258 _Static_assert(__offsetof(struct umutex, m_spare[0]) ==
259 __offsetof(struct umutex32, m_spare[0]), "m_spare32");
262 int umtx_shm_vnobj_persistent = 0;
263 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_vnode_persistent, CTLFLAG_RWTUN,
264 &umtx_shm_vnobj_persistent, 0,
265 "False forces destruction of umtx attached to file, on last close");
266 static int umtx_max_rb = 1000;
267 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_max_robust, CTLFLAG_RWTUN,
269 "Maximum number of robust mutexes allowed for each thread");
271 static uma_zone_t umtx_pi_zone;
272 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
273 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
274 static int umtx_pi_allocated;
276 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
278 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
279 &umtx_pi_allocated, 0, "Allocated umtx_pi");
280 static int umtx_verbose_rb = 1;
281 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
285 #ifdef UMTX_PROFILING
286 static long max_length;
287 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
288 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
292 static void abs_timeout_update(struct abs_timeout *timo);
294 static void umtx_shm_init(void);
295 static void umtxq_sysinit(void *);
296 static void umtxq_hash(struct umtx_key *key);
297 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
298 static void umtxq_lock(struct umtx_key *key);
299 static void umtxq_unlock(struct umtx_key *key);
300 static void umtxq_busy(struct umtx_key *key);
301 static void umtxq_unbusy(struct umtx_key *key);
302 static void umtxq_insert_queue(struct umtx_q *uq, int q);
303 static void umtxq_remove_queue(struct umtx_q *uq, int q);
304 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *);
305 static int umtxq_count(struct umtx_key *key);
306 static struct umtx_pi *umtx_pi_alloc(int);
307 static void umtx_pi_free(struct umtx_pi *pi);
308 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
310 static void umtx_thread_cleanup(struct thread *td);
311 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
313 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
314 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
315 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
317 static struct mtx umtx_lock;
319 #ifdef UMTX_PROFILING
321 umtx_init_profiling(void)
323 struct sysctl_oid *chain_oid;
327 for (i = 0; i < UMTX_CHAINS; ++i) {
328 snprintf(chain_name, sizeof(chain_name), "%d", i);
329 chain_oid = SYSCTL_ADD_NODE(NULL,
330 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
331 chain_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
333 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
334 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
335 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
336 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
341 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
345 struct umtxq_chain *uc;
346 u_int fract, i, j, tot, whole;
347 u_int sf0, sf1, sf2, sf3, sf4;
348 u_int si0, si1, si2, si3, si4;
349 u_int sw0, sw1, sw2, sw3, sw4;
351 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
352 for (i = 0; i < 2; i++) {
354 for (j = 0; j < UMTX_CHAINS; ++j) {
355 uc = &umtxq_chains[i][j];
356 mtx_lock(&uc->uc_lock);
357 tot += uc->max_length;
358 mtx_unlock(&uc->uc_lock);
361 sbuf_printf(&sb, "%u) Empty ", i);
363 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
364 si0 = si1 = si2 = si3 = si4 = 0;
365 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
366 for (j = 0; j < UMTX_CHAINS; j++) {
367 uc = &umtxq_chains[i][j];
368 mtx_lock(&uc->uc_lock);
369 whole = uc->max_length * 100;
370 mtx_unlock(&uc->uc_lock);
371 fract = (whole % tot) * 100;
372 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
376 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
381 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
386 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
391 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
398 sbuf_printf(&sb, "queue %u:\n", i);
399 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
401 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
403 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
405 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
407 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
413 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
419 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
421 struct umtxq_chain *uc;
426 error = sysctl_handle_int(oidp, &clear, 0, req);
427 if (error != 0 || req->newptr == NULL)
431 for (i = 0; i < 2; ++i) {
432 for (j = 0; j < UMTX_CHAINS; ++j) {
433 uc = &umtxq_chains[i][j];
434 mtx_lock(&uc->uc_lock);
437 mtx_unlock(&uc->uc_lock);
444 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
445 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
446 sysctl_debug_umtx_chains_clear, "I",
447 "Clear umtx chains statistics");
448 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
449 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
450 sysctl_debug_umtx_chains_peaks, "A",
451 "Highest peaks in chains max length");
455 umtxq_sysinit(void *arg __unused)
459 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
460 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
461 for (i = 0; i < 2; ++i) {
462 for (j = 0; j < UMTX_CHAINS; ++j) {
463 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
464 MTX_DEF | MTX_DUPOK);
465 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
466 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
467 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
468 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
469 umtxq_chains[i][j].uc_busy = 0;
470 umtxq_chains[i][j].uc_waiters = 0;
471 #ifdef UMTX_PROFILING
472 umtxq_chains[i][j].length = 0;
473 umtxq_chains[i][j].max_length = 0;
477 #ifdef UMTX_PROFILING
478 umtx_init_profiling();
480 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
489 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
490 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
492 TAILQ_INIT(&uq->uq_spare_queue->head);
493 TAILQ_INIT(&uq->uq_pi_contested);
494 uq->uq_inherited_pri = PRI_MAX;
499 umtxq_free(struct umtx_q *uq)
502 MPASS(uq->uq_spare_queue != NULL);
503 free(uq->uq_spare_queue, M_UMTX);
508 umtxq_hash(struct umtx_key *key)
512 n = (uintptr_t)key->info.both.a + key->info.both.b;
513 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
516 static inline struct umtxq_chain *
517 umtxq_getchain(struct umtx_key *key)
520 if (key->type <= TYPE_SEM)
521 return (&umtxq_chains[1][key->hash]);
522 return (&umtxq_chains[0][key->hash]);
529 umtxq_lock(struct umtx_key *key)
531 struct umtxq_chain *uc;
533 uc = umtxq_getchain(key);
534 mtx_lock(&uc->uc_lock);
541 umtxq_unlock(struct umtx_key *key)
543 struct umtxq_chain *uc;
545 uc = umtxq_getchain(key);
546 mtx_unlock(&uc->uc_lock);
550 * Set chain to busy state when following operation
551 * may be blocked (kernel mutex can not be used).
554 umtxq_busy(struct umtx_key *key)
556 struct umtxq_chain *uc;
558 uc = umtxq_getchain(key);
559 mtx_assert(&uc->uc_lock, MA_OWNED);
563 int count = BUSY_SPINS;
566 while (uc->uc_busy && --count > 0)
572 while (uc->uc_busy) {
574 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
585 umtxq_unbusy(struct umtx_key *key)
587 struct umtxq_chain *uc;
589 uc = umtxq_getchain(key);
590 mtx_assert(&uc->uc_lock, MA_OWNED);
591 KASSERT(uc->uc_busy != 0, ("not busy"));
598 umtxq_unbusy_unlocked(struct umtx_key *key)
606 static struct umtxq_queue *
607 umtxq_queue_lookup(struct umtx_key *key, int q)
609 struct umtxq_queue *uh;
610 struct umtxq_chain *uc;
612 uc = umtxq_getchain(key);
613 UMTXQ_LOCKED_ASSERT(uc);
614 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
615 if (umtx_key_match(&uh->key, key))
623 umtxq_insert_queue(struct umtx_q *uq, int q)
625 struct umtxq_queue *uh;
626 struct umtxq_chain *uc;
628 uc = umtxq_getchain(&uq->uq_key);
629 UMTXQ_LOCKED_ASSERT(uc);
630 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
631 uh = umtxq_queue_lookup(&uq->uq_key, q);
633 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
635 uh = uq->uq_spare_queue;
636 uh->key = uq->uq_key;
637 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
638 #ifdef UMTX_PROFILING
640 if (uc->length > uc->max_length) {
641 uc->max_length = uc->length;
642 if (uc->max_length > max_length)
643 max_length = uc->max_length;
647 uq->uq_spare_queue = NULL;
649 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
651 uq->uq_flags |= UQF_UMTXQ;
652 uq->uq_cur_queue = uh;
657 umtxq_remove_queue(struct umtx_q *uq, int q)
659 struct umtxq_chain *uc;
660 struct umtxq_queue *uh;
662 uc = umtxq_getchain(&uq->uq_key);
663 UMTXQ_LOCKED_ASSERT(uc);
664 if (uq->uq_flags & UQF_UMTXQ) {
665 uh = uq->uq_cur_queue;
666 TAILQ_REMOVE(&uh->head, uq, uq_link);
668 uq->uq_flags &= ~UQF_UMTXQ;
669 if (TAILQ_EMPTY(&uh->head)) {
670 KASSERT(uh->length == 0,
671 ("inconsistent umtxq_queue length"));
672 #ifdef UMTX_PROFILING
675 LIST_REMOVE(uh, link);
677 uh = LIST_FIRST(&uc->uc_spare_queue);
678 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
679 LIST_REMOVE(uh, link);
681 uq->uq_spare_queue = uh;
682 uq->uq_cur_queue = NULL;
687 * Check if there are multiple waiters
690 umtxq_count(struct umtx_key *key)
692 struct umtxq_queue *uh;
694 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
695 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
702 * Check if there are multiple PI waiters and returns first
706 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
708 struct umtxq_queue *uh;
711 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
712 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
714 *first = TAILQ_FIRST(&uh->head);
721 * Wake up threads waiting on an userland object.
725 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
727 struct umtxq_queue *uh;
732 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
733 uh = umtxq_queue_lookup(key, q);
735 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
736 umtxq_remove_queue(uq, q);
746 * Wake up specified thread.
749 umtxq_signal_thread(struct umtx_q *uq)
752 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
758 tstohz(const struct timespec *tsp)
762 TIMESPEC_TO_TIMEVAL(&tv, tsp);
767 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
768 const struct timespec *timeout)
771 timo->clockid = clockid;
773 timo->is_abs_real = false;
774 abs_timeout_update(timo);
775 timespecadd(&timo->cur, timeout, &timo->end);
777 timo->end = *timeout;
778 timo->is_abs_real = clockid == CLOCK_REALTIME ||
779 clockid == CLOCK_REALTIME_FAST ||
780 clockid == CLOCK_REALTIME_PRECISE;
782 * If is_abs_real, umtxq_sleep will read the clock
783 * after setting td_rtcgen; otherwise, read it here.
785 if (!timo->is_abs_real) {
786 abs_timeout_update(timo);
792 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
795 abs_timeout_init(timo, umtxtime->_clockid,
796 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
800 abs_timeout_update(struct abs_timeout *timo)
803 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
807 abs_timeout_gethz(struct abs_timeout *timo)
811 if (timespeccmp(&timo->end, &timo->cur, <=))
813 timespecsub(&timo->end, &timo->cur, &tts);
814 return (tstohz(&tts));
818 umtx_unlock_val(uint32_t flags, bool rb)
822 return (UMUTEX_RB_OWNERDEAD);
823 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
824 return (UMUTEX_RB_NOTRECOV);
826 return (UMUTEX_UNOWNED);
831 * Put thread into sleep state, before sleeping, check if
832 * thread was removed from umtx queue.
835 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
837 struct umtxq_chain *uc;
840 if (abstime != NULL && abstime->is_abs_real) {
841 curthread->td_rtcgen = atomic_load_acq_int(&rtc_generation);
842 abs_timeout_update(abstime);
845 uc = umtxq_getchain(&uq->uq_key);
846 UMTXQ_LOCKED_ASSERT(uc);
848 if (!(uq->uq_flags & UQF_UMTXQ)) {
852 if (abstime != NULL) {
853 timo = abs_timeout_gethz(abstime);
860 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
861 if (error == EINTR || error == ERESTART) {
862 umtxq_lock(&uq->uq_key);
865 if (abstime != NULL) {
866 if (abstime->is_abs_real)
867 curthread->td_rtcgen =
868 atomic_load_acq_int(&rtc_generation);
869 abs_timeout_update(abstime);
871 umtxq_lock(&uq->uq_key);
874 curthread->td_rtcgen = 0;
879 * Convert userspace address into unique logical address.
882 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
884 struct thread *td = curthread;
886 vm_map_entry_t entry;
892 if (share == THREAD_SHARE) {
894 key->info.private.vs = td->td_proc->p_vmspace;
895 key->info.private.addr = (uintptr_t)addr;
897 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
898 map = &td->td_proc->p_vmspace->vm_map;
899 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
900 &entry, &key->info.shared.object, &pindex, &prot,
901 &wired) != KERN_SUCCESS) {
905 if ((share == PROCESS_SHARE) ||
906 (share == AUTO_SHARE &&
907 VM_INHERIT_SHARE == entry->inheritance)) {
909 key->info.shared.offset = (vm_offset_t)addr -
910 entry->start + entry->offset;
911 vm_object_reference(key->info.shared.object);
914 key->info.private.vs = td->td_proc->p_vmspace;
915 key->info.private.addr = (uintptr_t)addr;
917 vm_map_lookup_done(map, entry);
928 umtx_key_release(struct umtx_key *key)
931 vm_object_deallocate(key->info.shared.object);
935 * Fetch and compare value, sleep on the address if value is not changed.
938 do_wait(struct thread *td, void *addr, u_long id,
939 struct _umtx_time *timeout, int compat32, int is_private)
941 struct abs_timeout timo;
948 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
949 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
953 abs_timeout_init2(&timo, timeout);
955 umtxq_lock(&uq->uq_key);
957 umtxq_unlock(&uq->uq_key);
959 error = fueword(addr, &tmp);
963 error = fueword32(addr, &tmp32);
969 umtxq_lock(&uq->uq_key);
972 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
974 if ((uq->uq_flags & UQF_UMTXQ) == 0)
978 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
981 umtxq_unlock(&uq->uq_key);
982 umtx_key_release(&uq->uq_key);
983 if (error == ERESTART)
989 * Wake up threads sleeping on the specified address.
992 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
997 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
998 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1001 umtxq_signal(&key, n_wake);
1003 umtx_key_release(&key);
1008 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1011 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1012 struct _umtx_time *timeout, int mode)
1014 struct abs_timeout timo;
1016 uint32_t owner, old, id;
1022 if (timeout != NULL)
1023 abs_timeout_init2(&timo, timeout);
1026 * Care must be exercised when dealing with umtx structure. It
1027 * can fault on any access.
1030 rv = fueword32(&m->m_owner, &owner);
1033 if (mode == _UMUTEX_WAIT) {
1034 if (owner == UMUTEX_UNOWNED ||
1035 owner == UMUTEX_CONTESTED ||
1036 owner == UMUTEX_RB_OWNERDEAD ||
1037 owner == UMUTEX_RB_NOTRECOV)
1041 * Robust mutex terminated. Kernel duty is to
1042 * return EOWNERDEAD to the userspace. The
1043 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1044 * by the common userspace code.
1046 if (owner == UMUTEX_RB_OWNERDEAD) {
1047 rv = casueword32(&m->m_owner,
1048 UMUTEX_RB_OWNERDEAD, &owner,
1049 id | UMUTEX_CONTESTED);
1053 MPASS(owner == UMUTEX_RB_OWNERDEAD);
1054 return (EOWNERDEAD); /* success */
1057 rv = thread_check_susp(td, false);
1062 if (owner == UMUTEX_RB_NOTRECOV)
1063 return (ENOTRECOVERABLE);
1066 * Try the uncontested case. This should be
1069 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1071 /* The address was invalid. */
1075 /* The acquire succeeded. */
1077 MPASS(owner == UMUTEX_UNOWNED);
1082 * If no one owns it but it is contested try
1086 if (owner == UMUTEX_CONTESTED) {
1087 rv = casueword32(&m->m_owner,
1088 UMUTEX_CONTESTED, &owner,
1089 id | UMUTEX_CONTESTED);
1090 /* The address was invalid. */
1094 MPASS(owner == UMUTEX_CONTESTED);
1098 rv = thread_check_susp(td, false);
1104 * If this failed the lock has
1110 /* rv == 1 but not contested, likely store failure */
1111 rv = thread_check_susp(td, false);
1116 if (mode == _UMUTEX_TRY)
1120 * If we caught a signal, we have retried and now
1126 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1127 GET_SHARE(flags), &uq->uq_key)) != 0)
1130 umtxq_lock(&uq->uq_key);
1131 umtxq_busy(&uq->uq_key);
1133 umtxq_unlock(&uq->uq_key);
1136 * Set the contested bit so that a release in user space
1137 * knows to use the system call for unlock. If this fails
1138 * either some one else has acquired the lock or it has been
1141 rv = casueword32(&m->m_owner, owner, &old,
1142 owner | UMUTEX_CONTESTED);
1144 /* The address was invalid or casueword failed to store. */
1145 if (rv == -1 || rv == 1) {
1146 umtxq_lock(&uq->uq_key);
1148 umtxq_unbusy(&uq->uq_key);
1149 umtxq_unlock(&uq->uq_key);
1150 umtx_key_release(&uq->uq_key);
1154 rv = thread_check_susp(td, false);
1162 * We set the contested bit, sleep. Otherwise the lock changed
1163 * and we need to retry or we lost a race to the thread
1164 * unlocking the umtx.
1166 umtxq_lock(&uq->uq_key);
1167 umtxq_unbusy(&uq->uq_key);
1168 MPASS(old == owner);
1169 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1172 umtxq_unlock(&uq->uq_key);
1173 umtx_key_release(&uq->uq_key);
1176 error = thread_check_susp(td, false);
1183 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1186 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1188 struct umtx_key key;
1189 uint32_t owner, old, id, newlock;
1196 * Make sure we own this mtx.
1198 error = fueword32(&m->m_owner, &owner);
1202 if ((owner & ~UMUTEX_CONTESTED) != id)
1205 newlock = umtx_unlock_val(flags, rb);
1206 if ((owner & UMUTEX_CONTESTED) == 0) {
1207 error = casueword32(&m->m_owner, owner, &old, newlock);
1211 error = thread_check_susp(td, false);
1216 MPASS(old == owner);
1220 /* We should only ever be in here for contested locks */
1221 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1227 count = umtxq_count(&key);
1231 * When unlocking the umtx, it must be marked as unowned if
1232 * there is zero or one thread only waiting for it.
1233 * Otherwise, it must be marked as contested.
1236 newlock |= UMUTEX_CONTESTED;
1237 error = casueword32(&m->m_owner, owner, &old, newlock);
1239 umtxq_signal(&key, 1);
1242 umtx_key_release(&key);
1248 error = thread_check_susp(td, false);
1257 * Check if the mutex is available and wake up a waiter,
1258 * only for simple mutex.
1261 do_wake_umutex(struct thread *td, struct umutex *m)
1263 struct umtx_key key;
1270 error = fueword32(&m->m_owner, &owner);
1274 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1275 owner != UMUTEX_RB_NOTRECOV)
1278 error = fueword32(&m->m_flags, &flags);
1282 /* We should only ever be in here for contested locks */
1283 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1289 count = umtxq_count(&key);
1292 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1293 owner != UMUTEX_RB_NOTRECOV) {
1294 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1298 } else if (error == 1) {
1302 umtx_key_release(&key);
1303 error = thread_check_susp(td, false);
1311 if (error == 0 && count != 0) {
1312 MPASS((owner & ~UMUTEX_CONTESTED) == 0 ||
1313 owner == UMUTEX_RB_OWNERDEAD ||
1314 owner == UMUTEX_RB_NOTRECOV);
1315 umtxq_signal(&key, 1);
1319 umtx_key_release(&key);
1324 * Check if the mutex has waiters and tries to fix contention bit.
1327 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1329 struct umtx_key key;
1330 uint32_t owner, old;
1335 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1339 type = TYPE_NORMAL_UMUTEX;
1341 case UMUTEX_PRIO_INHERIT:
1342 type = TYPE_PI_UMUTEX;
1344 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1345 type = TYPE_PI_ROBUST_UMUTEX;
1347 case UMUTEX_PRIO_PROTECT:
1348 type = TYPE_PP_UMUTEX;
1350 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1351 type = TYPE_PP_ROBUST_UMUTEX;
1356 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1362 count = umtxq_count(&key);
1365 error = fueword32(&m->m_owner, &owner);
1370 * Only repair contention bit if there is a waiter, this means
1371 * the mutex is still being referenced by userland code,
1372 * otherwise don't update any memory.
1374 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0 &&
1375 (count > 1 || (count == 1 && (owner & ~UMUTEX_CONTESTED) != 0))) {
1376 error = casueword32(&m->m_owner, owner, &old,
1377 owner | UMUTEX_CONTESTED);
1383 MPASS(old == owner);
1387 error = thread_check_susp(td, false);
1391 if (error == EFAULT) {
1392 umtxq_signal(&key, INT_MAX);
1393 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1394 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1395 umtxq_signal(&key, 1);
1398 umtx_key_release(&key);
1402 static inline struct umtx_pi *
1403 umtx_pi_alloc(int flags)
1407 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1408 TAILQ_INIT(&pi->pi_blocked);
1409 atomic_add_int(&umtx_pi_allocated, 1);
1414 umtx_pi_free(struct umtx_pi *pi)
1416 uma_zfree(umtx_pi_zone, pi);
1417 atomic_add_int(&umtx_pi_allocated, -1);
1421 * Adjust the thread's position on a pi_state after its priority has been
1425 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1427 struct umtx_q *uq, *uq1, *uq2;
1430 mtx_assert(&umtx_lock, MA_OWNED);
1437 * Check if the thread needs to be moved on the blocked chain.
1438 * It needs to be moved if either its priority is lower than
1439 * the previous thread or higher than the next thread.
1441 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1442 uq2 = TAILQ_NEXT(uq, uq_lockq);
1443 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1444 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1446 * Remove thread from blocked chain and determine where
1447 * it should be moved to.
1449 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1450 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1451 td1 = uq1->uq_thread;
1452 MPASS(td1->td_proc->p_magic == P_MAGIC);
1453 if (UPRI(td1) > UPRI(td))
1458 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1460 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1465 static struct umtx_pi *
1466 umtx_pi_next(struct umtx_pi *pi)
1468 struct umtx_q *uq_owner;
1470 if (pi->pi_owner == NULL)
1472 uq_owner = pi->pi_owner->td_umtxq;
1473 if (uq_owner == NULL)
1475 return (uq_owner->uq_pi_blocked);
1479 * Floyd's Cycle-Finding Algorithm.
1482 umtx_pi_check_loop(struct umtx_pi *pi)
1484 struct umtx_pi *pi1; /* fast iterator */
1486 mtx_assert(&umtx_lock, MA_OWNED);
1491 pi = umtx_pi_next(pi);
1494 pi1 = umtx_pi_next(pi1);
1497 pi1 = umtx_pi_next(pi1);
1507 * Propagate priority when a thread is blocked on POSIX
1511 umtx_propagate_priority(struct thread *td)
1517 mtx_assert(&umtx_lock, MA_OWNED);
1520 pi = uq->uq_pi_blocked;
1523 if (umtx_pi_check_loop(pi))
1528 if (td == NULL || td == curthread)
1531 MPASS(td->td_proc != NULL);
1532 MPASS(td->td_proc->p_magic == P_MAGIC);
1535 if (td->td_lend_user_pri > pri)
1536 sched_lend_user_prio(td, pri);
1544 * Pick up the lock that td is blocked on.
1547 pi = uq->uq_pi_blocked;
1550 /* Resort td on the list if needed. */
1551 umtx_pi_adjust_thread(pi, td);
1556 * Unpropagate priority for a PI mutex when a thread blocked on
1557 * it is interrupted by signal or resumed by others.
1560 umtx_repropagate_priority(struct umtx_pi *pi)
1562 struct umtx_q *uq, *uq_owner;
1563 struct umtx_pi *pi2;
1566 mtx_assert(&umtx_lock, MA_OWNED);
1568 if (umtx_pi_check_loop(pi))
1570 while (pi != NULL && pi->pi_owner != NULL) {
1572 uq_owner = pi->pi_owner->td_umtxq;
1574 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1575 uq = TAILQ_FIRST(&pi2->pi_blocked);
1577 if (pri > UPRI(uq->uq_thread))
1578 pri = UPRI(uq->uq_thread);
1582 if (pri > uq_owner->uq_inherited_pri)
1583 pri = uq_owner->uq_inherited_pri;
1584 thread_lock(pi->pi_owner);
1585 sched_lend_user_prio(pi->pi_owner, pri);
1586 thread_unlock(pi->pi_owner);
1587 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1588 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1593 * Insert a PI mutex into owned list.
1596 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1598 struct umtx_q *uq_owner;
1600 uq_owner = owner->td_umtxq;
1601 mtx_assert(&umtx_lock, MA_OWNED);
1602 MPASS(pi->pi_owner == NULL);
1603 pi->pi_owner = owner;
1604 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1608 * Disown a PI mutex, and remove it from the owned list.
1611 umtx_pi_disown(struct umtx_pi *pi)
1614 mtx_assert(&umtx_lock, MA_OWNED);
1615 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1616 pi->pi_owner = NULL;
1620 * Claim ownership of a PI mutex.
1623 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1628 mtx_lock(&umtx_lock);
1629 if (pi->pi_owner == owner) {
1630 mtx_unlock(&umtx_lock);
1634 if (pi->pi_owner != NULL) {
1636 * userland may have already messed the mutex, sigh.
1638 mtx_unlock(&umtx_lock);
1641 umtx_pi_setowner(pi, owner);
1642 uq = TAILQ_FIRST(&pi->pi_blocked);
1644 pri = UPRI(uq->uq_thread);
1646 if (pri < UPRI(owner))
1647 sched_lend_user_prio(owner, pri);
1648 thread_unlock(owner);
1650 mtx_unlock(&umtx_lock);
1655 * Adjust a thread's order position in its blocked PI mutex,
1656 * this may result new priority propagating process.
1659 umtx_pi_adjust(struct thread *td, u_char oldpri)
1665 mtx_lock(&umtx_lock);
1667 * Pick up the lock that td is blocked on.
1669 pi = uq->uq_pi_blocked;
1671 umtx_pi_adjust_thread(pi, td);
1672 umtx_repropagate_priority(pi);
1674 mtx_unlock(&umtx_lock);
1678 * Sleep on a PI mutex.
1681 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
1682 const char *wmesg, struct abs_timeout *timo, bool shared)
1684 struct thread *td, *td1;
1688 struct umtxq_chain *uc;
1690 uc = umtxq_getchain(&pi->pi_key);
1694 KASSERT(td == curthread, ("inconsistent uq_thread"));
1695 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
1696 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1698 mtx_lock(&umtx_lock);
1699 if (pi->pi_owner == NULL) {
1700 mtx_unlock(&umtx_lock);
1701 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
1702 mtx_lock(&umtx_lock);
1704 if (pi->pi_owner == NULL)
1705 umtx_pi_setowner(pi, td1);
1706 PROC_UNLOCK(td1->td_proc);
1710 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1711 pri = UPRI(uq1->uq_thread);
1717 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1719 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1721 uq->uq_pi_blocked = pi;
1723 td->td_flags |= TDF_UPIBLOCKED;
1725 umtx_propagate_priority(td);
1726 mtx_unlock(&umtx_lock);
1727 umtxq_unbusy(&uq->uq_key);
1729 error = umtxq_sleep(uq, wmesg, timo);
1732 mtx_lock(&umtx_lock);
1733 uq->uq_pi_blocked = NULL;
1735 td->td_flags &= ~TDF_UPIBLOCKED;
1737 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1738 umtx_repropagate_priority(pi);
1739 mtx_unlock(&umtx_lock);
1740 umtxq_unlock(&uq->uq_key);
1746 * Add reference count for a PI mutex.
1749 umtx_pi_ref(struct umtx_pi *pi)
1752 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
1757 * Decrease reference count for a PI mutex, if the counter
1758 * is decreased to zero, its memory space is freed.
1761 umtx_pi_unref(struct umtx_pi *pi)
1763 struct umtxq_chain *uc;
1765 uc = umtxq_getchain(&pi->pi_key);
1766 UMTXQ_LOCKED_ASSERT(uc);
1767 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1768 if (--pi->pi_refcount == 0) {
1769 mtx_lock(&umtx_lock);
1770 if (pi->pi_owner != NULL)
1772 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1773 ("blocked queue not empty"));
1774 mtx_unlock(&umtx_lock);
1775 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1781 * Find a PI mutex in hash table.
1783 static struct umtx_pi *
1784 umtx_pi_lookup(struct umtx_key *key)
1786 struct umtxq_chain *uc;
1789 uc = umtxq_getchain(key);
1790 UMTXQ_LOCKED_ASSERT(uc);
1792 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1793 if (umtx_key_match(&pi->pi_key, key)) {
1801 * Insert a PI mutex into hash table.
1804 umtx_pi_insert(struct umtx_pi *pi)
1806 struct umtxq_chain *uc;
1808 uc = umtxq_getchain(&pi->pi_key);
1809 UMTXQ_LOCKED_ASSERT(uc);
1810 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1817 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1818 struct _umtx_time *timeout, int try)
1820 struct abs_timeout timo;
1822 struct umtx_pi *pi, *new_pi;
1823 uint32_t id, old_owner, owner, old;
1829 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
1830 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
1834 if (timeout != NULL)
1835 abs_timeout_init2(&timo, timeout);
1837 umtxq_lock(&uq->uq_key);
1838 pi = umtx_pi_lookup(&uq->uq_key);
1840 new_pi = umtx_pi_alloc(M_NOWAIT);
1841 if (new_pi == NULL) {
1842 umtxq_unlock(&uq->uq_key);
1843 new_pi = umtx_pi_alloc(M_WAITOK);
1844 umtxq_lock(&uq->uq_key);
1845 pi = umtx_pi_lookup(&uq->uq_key);
1847 umtx_pi_free(new_pi);
1851 if (new_pi != NULL) {
1852 new_pi->pi_key = uq->uq_key;
1853 umtx_pi_insert(new_pi);
1858 umtxq_unlock(&uq->uq_key);
1861 * Care must be exercised when dealing with umtx structure. It
1862 * can fault on any access.
1866 * Try the uncontested case. This should be done in userland.
1868 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
1869 /* The address was invalid. */
1874 /* The acquire succeeded. */
1876 MPASS(owner == UMUTEX_UNOWNED);
1881 if (owner == UMUTEX_RB_NOTRECOV) {
1882 error = ENOTRECOVERABLE;
1887 * Avoid overwriting a possible error from sleep due
1888 * to the pending signal with suspension check result.
1891 error = thread_check_susp(td, true);
1896 /* If no one owns it but it is contested try to acquire it. */
1897 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
1899 rv = casueword32(&m->m_owner, owner, &owner,
1900 id | UMUTEX_CONTESTED);
1901 /* The address was invalid. */
1908 error = thread_check_susp(td, true);
1914 * If this failed the lock could
1921 MPASS(owner == old_owner);
1922 umtxq_lock(&uq->uq_key);
1923 umtxq_busy(&uq->uq_key);
1924 error = umtx_pi_claim(pi, td);
1925 umtxq_unbusy(&uq->uq_key);
1926 umtxq_unlock(&uq->uq_key);
1929 * Since we're going to return an
1930 * error, restore the m_owner to its
1931 * previous, unowned state to avoid
1932 * compounding the problem.
1934 (void)casuword32(&m->m_owner,
1935 id | UMUTEX_CONTESTED, old_owner);
1937 if (error == 0 && old_owner == UMUTEX_RB_OWNERDEAD)
1942 if ((owner & ~UMUTEX_CONTESTED) == id) {
1953 * If we caught a signal, we have retried and now
1959 umtxq_lock(&uq->uq_key);
1960 umtxq_busy(&uq->uq_key);
1961 umtxq_unlock(&uq->uq_key);
1964 * Set the contested bit so that a release in user space
1965 * knows to use the system call for unlock. If this fails
1966 * either some one else has acquired the lock or it has been
1969 rv = casueword32(&m->m_owner, owner, &old, owner |
1972 /* The address was invalid. */
1974 umtxq_unbusy_unlocked(&uq->uq_key);
1979 umtxq_unbusy_unlocked(&uq->uq_key);
1980 error = thread_check_susp(td, true);
1985 * The lock changed and we need to retry or we
1986 * lost a race to the thread unlocking the
1987 * umtx. Note that the UMUTEX_RB_OWNERDEAD
1988 * value for owner is impossible there.
1993 umtxq_lock(&uq->uq_key);
1995 /* We set the contested bit, sleep. */
1996 MPASS(old == owner);
1997 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
1998 "umtxpi", timeout == NULL ? NULL : &timo,
1999 (flags & USYNC_PROCESS_SHARED) != 0);
2003 error = thread_check_susp(td, false);
2008 umtxq_lock(&uq->uq_key);
2010 umtxq_unlock(&uq->uq_key);
2012 umtx_key_release(&uq->uq_key);
2017 * Unlock a PI mutex.
2020 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2022 struct umtx_key key;
2023 struct umtx_q *uq_first, *uq_first2, *uq_me;
2024 struct umtx_pi *pi, *pi2;
2025 uint32_t id, new_owner, old, owner;
2026 int count, error, pri;
2032 * Make sure we own this mtx.
2034 error = fueword32(&m->m_owner, &owner);
2038 if ((owner & ~UMUTEX_CONTESTED) != id)
2041 new_owner = umtx_unlock_val(flags, rb);
2043 /* This should be done in userland */
2044 if ((owner & UMUTEX_CONTESTED) == 0) {
2045 error = casueword32(&m->m_owner, owner, &old, new_owner);
2049 error = thread_check_susp(td, true);
2059 /* We should only ever be in here for contested locks */
2060 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2061 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2067 count = umtxq_count_pi(&key, &uq_first);
2068 if (uq_first != NULL) {
2069 mtx_lock(&umtx_lock);
2070 pi = uq_first->uq_pi_blocked;
2071 KASSERT(pi != NULL, ("pi == NULL?"));
2072 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2073 mtx_unlock(&umtx_lock);
2076 umtx_key_release(&key);
2077 /* userland messed the mutex */
2080 uq_me = td->td_umtxq;
2081 if (pi->pi_owner == td)
2083 /* get highest priority thread which is still sleeping. */
2084 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2085 while (uq_first != NULL &&
2086 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2087 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2090 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2091 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2092 if (uq_first2 != NULL) {
2093 if (pri > UPRI(uq_first2->uq_thread))
2094 pri = UPRI(uq_first2->uq_thread);
2098 sched_lend_user_prio(td, pri);
2100 mtx_unlock(&umtx_lock);
2102 umtxq_signal_thread(uq_first);
2104 pi = umtx_pi_lookup(&key);
2106 * A umtx_pi can exist if a signal or timeout removed the
2107 * last waiter from the umtxq, but there is still
2108 * a thread in do_lock_pi() holding the umtx_pi.
2112 * The umtx_pi can be unowned, such as when a thread
2113 * has just entered do_lock_pi(), allocated the
2114 * umtx_pi, and unlocked the umtxq.
2115 * If the current thread owns it, it must disown it.
2117 mtx_lock(&umtx_lock);
2118 if (pi->pi_owner == td)
2120 mtx_unlock(&umtx_lock);
2126 * When unlocking the umtx, it must be marked as unowned if
2127 * there is zero or one thread only waiting for it.
2128 * Otherwise, it must be marked as contested.
2132 new_owner |= UMUTEX_CONTESTED;
2134 error = casueword32(&m->m_owner, owner, &old, new_owner);
2136 error = thread_check_susp(td, false);
2140 umtxq_unbusy_unlocked(&key);
2141 umtx_key_release(&key);
2144 if (error == 0 && old != owner)
2153 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2154 struct _umtx_time *timeout, int try)
2156 struct abs_timeout timo;
2157 struct umtx_q *uq, *uq2;
2161 int error, pri, old_inherited_pri, su, rv;
2165 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2166 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2170 if (timeout != NULL)
2171 abs_timeout_init2(&timo, timeout);
2173 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2175 old_inherited_pri = uq->uq_inherited_pri;
2176 umtxq_lock(&uq->uq_key);
2177 umtxq_busy(&uq->uq_key);
2178 umtxq_unlock(&uq->uq_key);
2180 rv = fueword32(&m->m_ceilings[0], &ceiling);
2185 ceiling = RTP_PRIO_MAX - ceiling;
2186 if (ceiling > RTP_PRIO_MAX) {
2191 mtx_lock(&umtx_lock);
2192 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2193 mtx_unlock(&umtx_lock);
2197 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2198 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2200 if (uq->uq_inherited_pri < UPRI(td))
2201 sched_lend_user_prio(td, uq->uq_inherited_pri);
2204 mtx_unlock(&umtx_lock);
2206 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2207 id | UMUTEX_CONTESTED);
2208 /* The address was invalid. */
2214 MPASS(owner == UMUTEX_CONTESTED);
2219 if (owner == UMUTEX_RB_OWNERDEAD) {
2220 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2221 &owner, id | UMUTEX_CONTESTED);
2227 MPASS(owner == UMUTEX_RB_OWNERDEAD);
2228 error = EOWNERDEAD; /* success */
2233 * rv == 1, only check for suspension if we
2234 * did not already catched a signal. If we
2235 * get an error from the check, the same
2236 * condition is checked by the umtxq_sleep()
2237 * call below, so we should obliterate the
2238 * error to not skip the last loop iteration.
2241 error = thread_check_susp(td, false);
2250 } else if (owner == UMUTEX_RB_NOTRECOV) {
2251 error = ENOTRECOVERABLE;
2258 * If we caught a signal, we have retried and now
2264 umtxq_lock(&uq->uq_key);
2266 umtxq_unbusy(&uq->uq_key);
2267 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2270 umtxq_unlock(&uq->uq_key);
2272 mtx_lock(&umtx_lock);
2273 uq->uq_inherited_pri = old_inherited_pri;
2275 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2276 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2278 if (pri > UPRI(uq2->uq_thread))
2279 pri = UPRI(uq2->uq_thread);
2282 if (pri > uq->uq_inherited_pri)
2283 pri = uq->uq_inherited_pri;
2285 sched_lend_user_prio(td, pri);
2287 mtx_unlock(&umtx_lock);
2290 if (error != 0 && error != EOWNERDEAD) {
2291 mtx_lock(&umtx_lock);
2292 uq->uq_inherited_pri = old_inherited_pri;
2294 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2295 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2297 if (pri > UPRI(uq2->uq_thread))
2298 pri = UPRI(uq2->uq_thread);
2301 if (pri > uq->uq_inherited_pri)
2302 pri = uq->uq_inherited_pri;
2304 sched_lend_user_prio(td, pri);
2306 mtx_unlock(&umtx_lock);
2310 umtxq_unbusy_unlocked(&uq->uq_key);
2311 umtx_key_release(&uq->uq_key);
2316 * Unlock a PP mutex.
2319 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2321 struct umtx_key key;
2322 struct umtx_q *uq, *uq2;
2324 uint32_t id, owner, rceiling;
2325 int error, pri, new_inherited_pri, su;
2329 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2332 * Make sure we own this mtx.
2334 error = fueword32(&m->m_owner, &owner);
2338 if ((owner & ~UMUTEX_CONTESTED) != id)
2341 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2346 new_inherited_pri = PRI_MAX;
2348 rceiling = RTP_PRIO_MAX - rceiling;
2349 if (rceiling > RTP_PRIO_MAX)
2351 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2354 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2355 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2362 * For priority protected mutex, always set unlocked state
2363 * to UMUTEX_CONTESTED, so that userland always enters kernel
2364 * to lock the mutex, it is necessary because thread priority
2365 * has to be adjusted for such mutex.
2367 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2372 umtxq_signal(&key, 1);
2379 mtx_lock(&umtx_lock);
2381 uq->uq_inherited_pri = new_inherited_pri;
2383 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2384 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2386 if (pri > UPRI(uq2->uq_thread))
2387 pri = UPRI(uq2->uq_thread);
2390 if (pri > uq->uq_inherited_pri)
2391 pri = uq->uq_inherited_pri;
2393 sched_lend_user_prio(td, pri);
2395 mtx_unlock(&umtx_lock);
2397 umtx_key_release(&key);
2402 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2403 uint32_t *old_ceiling)
2406 uint32_t flags, id, owner, save_ceiling;
2409 error = fueword32(&m->m_flags, &flags);
2412 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2414 if (ceiling > RTP_PRIO_MAX)
2418 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2419 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2423 umtxq_lock(&uq->uq_key);
2424 umtxq_busy(&uq->uq_key);
2425 umtxq_unlock(&uq->uq_key);
2427 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2433 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2434 id | UMUTEX_CONTESTED);
2441 MPASS(owner == UMUTEX_CONTESTED);
2442 rv = suword32(&m->m_ceilings[0], ceiling);
2443 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2444 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2448 if ((owner & ~UMUTEX_CONTESTED) == id) {
2449 rv = suword32(&m->m_ceilings[0], ceiling);
2450 error = rv == 0 ? 0 : EFAULT;
2454 if (owner == UMUTEX_RB_OWNERDEAD) {
2457 } else if (owner == UMUTEX_RB_NOTRECOV) {
2458 error = ENOTRECOVERABLE;
2463 * If we caught a signal, we have retried and now
2470 * We set the contested bit, sleep. Otherwise the lock changed
2471 * and we need to retry or we lost a race to the thread
2472 * unlocking the umtx.
2474 umtxq_lock(&uq->uq_key);
2476 umtxq_unbusy(&uq->uq_key);
2477 error = umtxq_sleep(uq, "umtxpp", NULL);
2479 umtxq_unlock(&uq->uq_key);
2481 umtxq_lock(&uq->uq_key);
2483 umtxq_signal(&uq->uq_key, INT_MAX);
2484 umtxq_unbusy(&uq->uq_key);
2485 umtxq_unlock(&uq->uq_key);
2486 umtx_key_release(&uq->uq_key);
2487 if (error == 0 && old_ceiling != NULL) {
2488 rv = suword32(old_ceiling, save_ceiling);
2489 error = rv == 0 ? 0 : EFAULT;
2495 * Lock a userland POSIX mutex.
2498 do_lock_umutex(struct thread *td, struct umutex *m,
2499 struct _umtx_time *timeout, int mode)
2504 error = fueword32(&m->m_flags, &flags);
2508 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2510 error = do_lock_normal(td, m, flags, timeout, mode);
2512 case UMUTEX_PRIO_INHERIT:
2513 error = do_lock_pi(td, m, flags, timeout, mode);
2515 case UMUTEX_PRIO_PROTECT:
2516 error = do_lock_pp(td, m, flags, timeout, mode);
2521 if (timeout == NULL) {
2522 if (error == EINTR && mode != _UMUTEX_WAIT)
2525 /* Timed-locking is not restarted. */
2526 if (error == ERESTART)
2533 * Unlock a userland POSIX mutex.
2536 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2541 error = fueword32(&m->m_flags, &flags);
2545 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2547 return (do_unlock_normal(td, m, flags, rb));
2548 case UMUTEX_PRIO_INHERIT:
2549 return (do_unlock_pi(td, m, flags, rb));
2550 case UMUTEX_PRIO_PROTECT:
2551 return (do_unlock_pp(td, m, flags, rb));
2558 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2559 struct timespec *timeout, u_long wflags)
2561 struct abs_timeout timo;
2563 uint32_t flags, clockid, hasw;
2567 error = fueword32(&cv->c_flags, &flags);
2570 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2574 if ((wflags & CVWAIT_CLOCKID) != 0) {
2575 error = fueword32(&cv->c_clockid, &clockid);
2577 umtx_key_release(&uq->uq_key);
2580 if (clockid < CLOCK_REALTIME ||
2581 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2582 /* hmm, only HW clock id will work. */
2583 umtx_key_release(&uq->uq_key);
2587 clockid = CLOCK_REALTIME;
2590 umtxq_lock(&uq->uq_key);
2591 umtxq_busy(&uq->uq_key);
2593 umtxq_unlock(&uq->uq_key);
2596 * Set c_has_waiters to 1 before releasing user mutex, also
2597 * don't modify cache line when unnecessary.
2599 error = fueword32(&cv->c_has_waiters, &hasw);
2600 if (error == 0 && hasw == 0)
2601 suword32(&cv->c_has_waiters, 1);
2603 umtxq_unbusy_unlocked(&uq->uq_key);
2605 error = do_unlock_umutex(td, m, false);
2607 if (timeout != NULL)
2608 abs_timeout_init(&timo, clockid, (wflags & CVWAIT_ABSTIME) != 0,
2611 umtxq_lock(&uq->uq_key);
2613 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2617 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2621 * This must be timeout,interrupted by signal or
2622 * surprious wakeup, clear c_has_waiter flag when
2625 umtxq_busy(&uq->uq_key);
2626 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2627 int oldlen = uq->uq_cur_queue->length;
2630 umtxq_unlock(&uq->uq_key);
2631 suword32(&cv->c_has_waiters, 0);
2632 umtxq_lock(&uq->uq_key);
2635 umtxq_unbusy(&uq->uq_key);
2636 if (error == ERESTART)
2640 umtxq_unlock(&uq->uq_key);
2641 umtx_key_release(&uq->uq_key);
2646 * Signal a userland condition variable.
2649 do_cv_signal(struct thread *td, struct ucond *cv)
2651 struct umtx_key key;
2652 int error, cnt, nwake;
2655 error = fueword32(&cv->c_flags, &flags);
2658 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2662 cnt = umtxq_count(&key);
2663 nwake = umtxq_signal(&key, 1);
2666 error = suword32(&cv->c_has_waiters, 0);
2673 umtx_key_release(&key);
2678 do_cv_broadcast(struct thread *td, struct ucond *cv)
2680 struct umtx_key key;
2684 error = fueword32(&cv->c_flags, &flags);
2687 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2692 umtxq_signal(&key, INT_MAX);
2695 error = suword32(&cv->c_has_waiters, 0);
2699 umtxq_unbusy_unlocked(&key);
2701 umtx_key_release(&key);
2706 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
2707 struct _umtx_time *timeout)
2709 struct abs_timeout timo;
2711 uint32_t flags, wrflags;
2712 int32_t state, oldstate;
2713 int32_t blocked_readers;
2714 int error, error1, rv;
2717 error = fueword32(&rwlock->rw_flags, &flags);
2720 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2724 if (timeout != NULL)
2725 abs_timeout_init2(&timo, timeout);
2727 wrflags = URWLOCK_WRITE_OWNER;
2728 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2729 wrflags |= URWLOCK_WRITE_WAITERS;
2732 rv = fueword32(&rwlock->rw_state, &state);
2734 umtx_key_release(&uq->uq_key);
2738 /* try to lock it */
2739 while (!(state & wrflags)) {
2740 if (__predict_false(URWLOCK_READER_COUNT(state) ==
2741 URWLOCK_MAX_READERS)) {
2742 umtx_key_release(&uq->uq_key);
2745 rv = casueword32(&rwlock->rw_state, state,
2746 &oldstate, state + 1);
2748 umtx_key_release(&uq->uq_key);
2752 MPASS(oldstate == state);
2753 umtx_key_release(&uq->uq_key);
2756 error = thread_check_susp(td, true);
2765 /* grab monitor lock */
2766 umtxq_lock(&uq->uq_key);
2767 umtxq_busy(&uq->uq_key);
2768 umtxq_unlock(&uq->uq_key);
2771 * re-read the state, in case it changed between the try-lock above
2772 * and the check below
2774 rv = fueword32(&rwlock->rw_state, &state);
2778 /* set read contention bit */
2779 while (error == 0 && (state & wrflags) &&
2780 !(state & URWLOCK_READ_WAITERS)) {
2781 rv = casueword32(&rwlock->rw_state, state,
2782 &oldstate, state | URWLOCK_READ_WAITERS);
2788 MPASS(oldstate == state);
2792 error = thread_check_susp(td, false);
2797 umtxq_unbusy_unlocked(&uq->uq_key);
2801 /* state is changed while setting flags, restart */
2802 if (!(state & wrflags)) {
2803 umtxq_unbusy_unlocked(&uq->uq_key);
2804 error = thread_check_susp(td, true);
2812 * Contention bit is set, before sleeping, increase
2813 * read waiter count.
2815 rv = fueword32(&rwlock->rw_blocked_readers,
2818 umtxq_unbusy_unlocked(&uq->uq_key);
2822 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2824 while (state & wrflags) {
2825 umtxq_lock(&uq->uq_key);
2827 umtxq_unbusy(&uq->uq_key);
2829 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2832 umtxq_busy(&uq->uq_key);
2834 umtxq_unlock(&uq->uq_key);
2837 rv = fueword32(&rwlock->rw_state, &state);
2844 /* decrease read waiter count, and may clear read contention bit */
2845 rv = fueword32(&rwlock->rw_blocked_readers,
2848 umtxq_unbusy_unlocked(&uq->uq_key);
2852 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2853 if (blocked_readers == 1) {
2854 rv = fueword32(&rwlock->rw_state, &state);
2856 umtxq_unbusy_unlocked(&uq->uq_key);
2861 rv = casueword32(&rwlock->rw_state, state,
2862 &oldstate, state & ~URWLOCK_READ_WAITERS);
2868 MPASS(oldstate == state);
2872 error1 = thread_check_susp(td, false);
2881 umtxq_unbusy_unlocked(&uq->uq_key);
2885 umtx_key_release(&uq->uq_key);
2886 if (error == ERESTART)
2892 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2894 struct abs_timeout timo;
2897 int32_t state, oldstate;
2898 int32_t blocked_writers;
2899 int32_t blocked_readers;
2900 int error, error1, rv;
2903 error = fueword32(&rwlock->rw_flags, &flags);
2906 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2910 if (timeout != NULL)
2911 abs_timeout_init2(&timo, timeout);
2913 blocked_readers = 0;
2915 rv = fueword32(&rwlock->rw_state, &state);
2917 umtx_key_release(&uq->uq_key);
2920 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
2921 URWLOCK_READER_COUNT(state) == 0) {
2922 rv = casueword32(&rwlock->rw_state, state,
2923 &oldstate, state | URWLOCK_WRITE_OWNER);
2925 umtx_key_release(&uq->uq_key);
2929 MPASS(oldstate == state);
2930 umtx_key_release(&uq->uq_key);
2934 error = thread_check_susp(td, true);
2940 if ((state & (URWLOCK_WRITE_OWNER |
2941 URWLOCK_WRITE_WAITERS)) == 0 &&
2942 blocked_readers != 0) {
2943 umtxq_lock(&uq->uq_key);
2944 umtxq_busy(&uq->uq_key);
2945 umtxq_signal_queue(&uq->uq_key, INT_MAX,
2947 umtxq_unbusy(&uq->uq_key);
2948 umtxq_unlock(&uq->uq_key);
2954 /* grab monitor lock */
2955 umtxq_lock(&uq->uq_key);
2956 umtxq_busy(&uq->uq_key);
2957 umtxq_unlock(&uq->uq_key);
2960 * Re-read the state, in case it changed between the
2961 * try-lock above and the check below.
2963 rv = fueword32(&rwlock->rw_state, &state);
2967 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
2968 URWLOCK_READER_COUNT(state) != 0) &&
2969 (state & URWLOCK_WRITE_WAITERS) == 0) {
2970 rv = casueword32(&rwlock->rw_state, state,
2971 &oldstate, state | URWLOCK_WRITE_WAITERS);
2977 MPASS(oldstate == state);
2981 error = thread_check_susp(td, false);
2986 umtxq_unbusy_unlocked(&uq->uq_key);
2990 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
2991 URWLOCK_READER_COUNT(state) == 0) {
2992 umtxq_unbusy_unlocked(&uq->uq_key);
2993 error = thread_check_susp(td, false);
2999 rv = fueword32(&rwlock->rw_blocked_writers,
3002 umtxq_unbusy_unlocked(&uq->uq_key);
3006 suword32(&rwlock->rw_blocked_writers, blocked_writers + 1);
3008 while ((state & URWLOCK_WRITE_OWNER) ||
3009 URWLOCK_READER_COUNT(state) != 0) {
3010 umtxq_lock(&uq->uq_key);
3011 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3012 umtxq_unbusy(&uq->uq_key);
3014 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
3017 umtxq_busy(&uq->uq_key);
3018 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3019 umtxq_unlock(&uq->uq_key);
3022 rv = fueword32(&rwlock->rw_state, &state);
3029 rv = fueword32(&rwlock->rw_blocked_writers,
3032 umtxq_unbusy_unlocked(&uq->uq_key);
3036 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
3037 if (blocked_writers == 1) {
3038 rv = fueword32(&rwlock->rw_state, &state);
3040 umtxq_unbusy_unlocked(&uq->uq_key);
3045 rv = casueword32(&rwlock->rw_state, state,
3046 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3052 MPASS(oldstate == state);
3056 error1 = thread_check_susp(td, false);
3058 * We are leaving the URWLOCK_WRITE_WAITERS
3059 * behind, but this should not harm the
3068 rv = fueword32(&rwlock->rw_blocked_readers,
3071 umtxq_unbusy_unlocked(&uq->uq_key);
3076 blocked_readers = 0;
3078 umtxq_unbusy_unlocked(&uq->uq_key);
3081 umtx_key_release(&uq->uq_key);
3082 if (error == ERESTART)
3088 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3092 int32_t state, oldstate;
3093 int error, rv, q, count;
3096 error = fueword32(&rwlock->rw_flags, &flags);
3099 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3103 error = fueword32(&rwlock->rw_state, &state);
3108 if (state & URWLOCK_WRITE_OWNER) {
3110 rv = casueword32(&rwlock->rw_state, state,
3111 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3118 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3122 error = thread_check_susp(td, true);
3128 } else if (URWLOCK_READER_COUNT(state) != 0) {
3130 rv = casueword32(&rwlock->rw_state, state,
3131 &oldstate, state - 1);
3138 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3142 error = thread_check_susp(td, true);
3155 if (!(flags & URWLOCK_PREFER_READER)) {
3156 if (state & URWLOCK_WRITE_WAITERS) {
3158 q = UMTX_EXCLUSIVE_QUEUE;
3159 } else if (state & URWLOCK_READ_WAITERS) {
3161 q = UMTX_SHARED_QUEUE;
3164 if (state & URWLOCK_READ_WAITERS) {
3166 q = UMTX_SHARED_QUEUE;
3167 } else if (state & URWLOCK_WRITE_WAITERS) {
3169 q = UMTX_EXCLUSIVE_QUEUE;
3174 umtxq_lock(&uq->uq_key);
3175 umtxq_busy(&uq->uq_key);
3176 umtxq_signal_queue(&uq->uq_key, count, q);
3177 umtxq_unbusy(&uq->uq_key);
3178 umtxq_unlock(&uq->uq_key);
3181 umtx_key_release(&uq->uq_key);
3185 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3187 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3189 struct abs_timeout timo;
3191 uint32_t flags, count, count1;
3195 error = fueword32(&sem->_flags, &flags);
3198 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3202 if (timeout != NULL)
3203 abs_timeout_init2(&timo, timeout);
3206 umtxq_lock(&uq->uq_key);
3207 umtxq_busy(&uq->uq_key);
3209 umtxq_unlock(&uq->uq_key);
3210 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3212 rv1 = fueword32(&sem->_count, &count);
3213 if (rv == -1 || (rv == 0 && (rv1 == -1 || count != 0)) ||
3214 (rv == 1 && count1 == 0)) {
3215 umtxq_lock(&uq->uq_key);
3216 umtxq_unbusy(&uq->uq_key);
3218 umtxq_unlock(&uq->uq_key);
3220 rv = thread_check_susp(td, true);
3228 error = rv == -1 ? EFAULT : 0;
3231 umtxq_lock(&uq->uq_key);
3232 umtxq_unbusy(&uq->uq_key);
3234 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3236 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3240 /* A relative timeout cannot be restarted. */
3241 if (error == ERESTART && timeout != NULL &&
3242 (timeout->_flags & UMTX_ABSTIME) == 0)
3245 umtxq_unlock(&uq->uq_key);
3247 umtx_key_release(&uq->uq_key);
3252 * Signal a userland semaphore.
3255 do_sem_wake(struct thread *td, struct _usem *sem)
3257 struct umtx_key key;
3261 error = fueword32(&sem->_flags, &flags);
3264 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3268 cnt = umtxq_count(&key);
3271 * Check if count is greater than 0, this means the memory is
3272 * still being referenced by user code, so we can safely
3273 * update _has_waiters flag.
3277 error = suword32(&sem->_has_waiters, 0);
3282 umtxq_signal(&key, 1);
3286 umtx_key_release(&key);
3292 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3294 struct abs_timeout timo;
3296 uint32_t count, flags;
3300 flags = fuword32(&sem->_flags);
3301 if (timeout != NULL)
3302 abs_timeout_init2(&timo, timeout);
3305 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3308 umtxq_lock(&uq->uq_key);
3309 umtxq_busy(&uq->uq_key);
3311 umtxq_unlock(&uq->uq_key);
3312 rv = fueword32(&sem->_count, &count);
3314 umtxq_lock(&uq->uq_key);
3315 umtxq_unbusy(&uq->uq_key);
3317 umtxq_unlock(&uq->uq_key);
3318 umtx_key_release(&uq->uq_key);
3322 if (USEM_COUNT(count) != 0) {
3323 umtxq_lock(&uq->uq_key);
3324 umtxq_unbusy(&uq->uq_key);
3326 umtxq_unlock(&uq->uq_key);
3327 umtx_key_release(&uq->uq_key);
3330 if (count == USEM_HAS_WAITERS)
3332 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3335 umtxq_lock(&uq->uq_key);
3336 umtxq_unbusy(&uq->uq_key);
3338 umtxq_unlock(&uq->uq_key);
3339 umtx_key_release(&uq->uq_key);
3342 rv = thread_check_susp(td, true);
3347 umtxq_lock(&uq->uq_key);
3348 umtxq_unbusy(&uq->uq_key);
3350 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3352 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3356 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3357 /* A relative timeout cannot be restarted. */
3358 if (error == ERESTART)
3360 if (error == EINTR) {
3361 abs_timeout_update(&timo);
3362 timespecsub(&timo.end, &timo.cur,
3363 &timeout->_timeout);
3367 umtxq_unlock(&uq->uq_key);
3368 umtx_key_release(&uq->uq_key);
3373 * Signal a userland semaphore.
3376 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3378 struct umtx_key key;
3380 uint32_t count, flags;
3382 rv = fueword32(&sem->_flags, &flags);
3385 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3389 cnt = umtxq_count(&key);
3392 * If this was the last sleeping thread, clear the waiters
3397 rv = fueword32(&sem->_count, &count);
3398 while (rv != -1 && count & USEM_HAS_WAITERS) {
3399 rv = casueword32(&sem->_count, count, &count,
3400 count & ~USEM_HAS_WAITERS);
3402 rv = thread_check_susp(td, true);
3415 umtxq_signal(&key, 1);
3419 umtx_key_release(&key);
3424 umtx_copyin_timeout(const void *uaddr, struct timespec *tsp)
3428 error = copyin(uaddr, tsp, sizeof(*tsp));
3430 if (tsp->tv_sec < 0 ||
3431 tsp->tv_nsec >= 1000000000 ||
3439 umtx_copyin_umtx_time(const void *uaddr, size_t size, struct _umtx_time *tp)
3443 if (size <= sizeof(tp->_timeout)) {
3444 tp->_clockid = CLOCK_REALTIME;
3446 error = copyin(uaddr, &tp->_timeout, sizeof(tp->_timeout));
3448 error = copyin(uaddr, tp, sizeof(*tp));
3451 if (tp->_timeout.tv_sec < 0 ||
3452 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3458 umtx_copyin_robust_lists(const void *uaddr, size_t size,
3459 struct umtx_robust_lists_params *rb)
3462 if (size > sizeof(*rb))
3464 return (copyin(uaddr, rb, size));
3468 umtx_copyout_timeout(void *uaddr, size_t sz, struct timespec *tsp)
3472 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
3473 * and we're only called if sz >= sizeof(timespec) as supplied in the
3476 KASSERT(sz >= sizeof(*tsp),
3477 ("umtx_copyops specifies incorrect sizes"));
3479 return (copyout(tsp, uaddr, sizeof(*tsp)));
3483 __umtx_op_unimpl(struct thread *td, struct _umtx_op_args *uap,
3484 const struct umtx_copyops *ops __unused)
3487 return (EOPNOTSUPP);
3491 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap,
3492 const struct umtx_copyops *ops)
3494 struct _umtx_time timeout, *tm_p;
3497 if (uap->uaddr2 == NULL)
3500 error = ops->copyin_umtx_time(
3501 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3506 return (do_wait(td, uap->obj, uap->val, tm_p, ops->compat32, 0));
3510 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap,
3511 const struct umtx_copyops *ops)
3513 struct _umtx_time timeout, *tm_p;
3516 if (uap->uaddr2 == NULL)
3519 error = ops->copyin_umtx_time(
3520 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3525 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3529 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap,
3530 const struct umtx_copyops *ops)
3532 struct _umtx_time *tm_p, timeout;
3535 if (uap->uaddr2 == NULL)
3538 error = ops->copyin_umtx_time(
3539 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3544 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3548 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap,
3549 const struct umtx_copyops *ops __unused)
3552 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3555 #define BATCH_SIZE 128
3557 __umtx_op_nwake_private_native(struct thread *td, struct _umtx_op_args *uap)
3559 char *uaddrs[BATCH_SIZE], **upp;
3560 int count, error, i, pos, tocopy;
3562 upp = (char **)uap->obj;
3564 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3566 tocopy = MIN(count, BATCH_SIZE);
3567 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3570 for (i = 0; i < tocopy; ++i) {
3571 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3579 __umtx_op_nwake_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3581 uint32_t uaddrs[BATCH_SIZE], *upp;
3582 int count, error, i, pos, tocopy;
3584 upp = (uint32_t *)uap->obj;
3586 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3588 tocopy = MIN(count, BATCH_SIZE);
3589 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
3592 for (i = 0; i < tocopy; ++i) {
3593 kern_umtx_wake(td, (void *)(uintptr_t)uaddrs[i],
3602 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap,
3603 const struct umtx_copyops *ops)
3607 return (__umtx_op_nwake_private_compat32(td, uap));
3608 return (__umtx_op_nwake_private_native(td, uap));
3612 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap,
3613 const struct umtx_copyops *ops __unused)
3616 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3620 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap,
3621 const struct umtx_copyops *ops)
3623 struct _umtx_time *tm_p, timeout;
3626 /* Allow a null timespec (wait forever). */
3627 if (uap->uaddr2 == NULL)
3630 error = ops->copyin_umtx_time(
3631 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3636 return (do_lock_umutex(td, uap->obj, tm_p, 0));
3640 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap,
3641 const struct umtx_copyops *ops __unused)
3644 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
3648 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap,
3649 const struct umtx_copyops *ops)
3651 struct _umtx_time *tm_p, timeout;
3654 /* Allow a null timespec (wait forever). */
3655 if (uap->uaddr2 == NULL)
3658 error = ops->copyin_umtx_time(
3659 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3664 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
3668 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap,
3669 const struct umtx_copyops *ops __unused)
3672 return (do_wake_umutex(td, uap->obj));
3676 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap,
3677 const struct umtx_copyops *ops __unused)
3680 return (do_unlock_umutex(td, uap->obj, false));
3684 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap,
3685 const struct umtx_copyops *ops __unused)
3688 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
3692 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap,
3693 const struct umtx_copyops *ops)
3695 struct timespec *ts, timeout;
3698 /* Allow a null timespec (wait forever). */
3699 if (uap->uaddr2 == NULL)
3702 error = ops->copyin_timeout(uap->uaddr2, &timeout);
3707 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3711 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap,
3712 const struct umtx_copyops *ops __unused)
3715 return (do_cv_signal(td, uap->obj));
3719 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap,
3720 const struct umtx_copyops *ops __unused)
3723 return (do_cv_broadcast(td, uap->obj));
3727 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap,
3728 const struct umtx_copyops *ops)
3730 struct _umtx_time timeout;
3733 /* Allow a null timespec (wait forever). */
3734 if (uap->uaddr2 == NULL) {
3735 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3737 error = ops->copyin_umtx_time(uap->uaddr2,
3738 (size_t)uap->uaddr1, &timeout);
3741 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3747 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap,
3748 const struct umtx_copyops *ops)
3750 struct _umtx_time timeout;
3753 /* Allow a null timespec (wait forever). */
3754 if (uap->uaddr2 == NULL) {
3755 error = do_rw_wrlock(td, uap->obj, 0);
3757 error = ops->copyin_umtx_time(uap->uaddr2,
3758 (size_t)uap->uaddr1, &timeout);
3762 error = do_rw_wrlock(td, uap->obj, &timeout);
3768 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap,
3769 const struct umtx_copyops *ops __unused)
3772 return (do_rw_unlock(td, uap->obj));
3775 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3777 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap,
3778 const struct umtx_copyops *ops)
3780 struct _umtx_time *tm_p, timeout;
3783 /* Allow a null timespec (wait forever). */
3784 if (uap->uaddr2 == NULL)
3787 error = ops->copyin_umtx_time(
3788 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3793 return (do_sem_wait(td, uap->obj, tm_p));
3797 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap,
3798 const struct umtx_copyops *ops __unused)
3801 return (do_sem_wake(td, uap->obj));
3806 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap,
3807 const struct umtx_copyops *ops __unused)
3810 return (do_wake2_umutex(td, uap->obj, uap->val));
3814 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap,
3815 const struct umtx_copyops *ops)
3817 struct _umtx_time *tm_p, timeout;
3821 /* Allow a null timespec (wait forever). */
3822 if (uap->uaddr2 == NULL) {
3826 uasize = (size_t)uap->uaddr1;
3827 error = ops->copyin_umtx_time(uap->uaddr2, uasize, &timeout);
3832 error = do_sem2_wait(td, uap->obj, tm_p);
3833 if (error == EINTR && uap->uaddr2 != NULL &&
3834 (timeout._flags & UMTX_ABSTIME) == 0 &&
3835 uasize >= ops->umtx_time_sz + ops->timespec_sz) {
3836 error = ops->copyout_timeout(
3837 (void *)((uintptr_t)uap->uaddr2 + ops->umtx_time_sz),
3838 uasize - ops->umtx_time_sz, &timeout._timeout);
3848 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap,
3849 const struct umtx_copyops *ops __unused)
3852 return (do_sem2_wake(td, uap->obj));
3855 #define USHM_OBJ_UMTX(o) \
3856 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
3858 #define USHMF_REG_LINKED 0x0001
3859 #define USHMF_OBJ_LINKED 0x0002
3860 struct umtx_shm_reg {
3861 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
3862 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
3863 struct umtx_key ushm_key;
3864 struct ucred *ushm_cred;
3865 struct shmfd *ushm_obj;
3870 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
3871 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
3873 static uma_zone_t umtx_shm_reg_zone;
3874 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
3875 static struct mtx umtx_shm_lock;
3876 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
3877 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
3879 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
3882 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
3884 struct umtx_shm_reg_head d;
3885 struct umtx_shm_reg *reg, *reg1;
3888 mtx_lock(&umtx_shm_lock);
3889 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
3890 mtx_unlock(&umtx_shm_lock);
3891 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
3892 TAILQ_REMOVE(&d, reg, ushm_reg_link);
3893 umtx_shm_free_reg(reg);
3897 static struct task umtx_shm_reg_delfree_task =
3898 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
3900 static struct umtx_shm_reg *
3901 umtx_shm_find_reg_locked(const struct umtx_key *key)
3903 struct umtx_shm_reg *reg;
3904 struct umtx_shm_reg_head *reg_head;
3906 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
3907 mtx_assert(&umtx_shm_lock, MA_OWNED);
3908 reg_head = &umtx_shm_registry[key->hash];
3909 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
3910 KASSERT(reg->ushm_key.shared,
3911 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
3912 if (reg->ushm_key.info.shared.object ==
3913 key->info.shared.object &&
3914 reg->ushm_key.info.shared.offset ==
3915 key->info.shared.offset) {
3916 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
3917 KASSERT(reg->ushm_refcnt > 0,
3918 ("reg %p refcnt 0 onlist", reg));
3919 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
3920 ("reg %p not linked", reg));
3928 static struct umtx_shm_reg *
3929 umtx_shm_find_reg(const struct umtx_key *key)
3931 struct umtx_shm_reg *reg;
3933 mtx_lock(&umtx_shm_lock);
3934 reg = umtx_shm_find_reg_locked(key);
3935 mtx_unlock(&umtx_shm_lock);
3940 umtx_shm_free_reg(struct umtx_shm_reg *reg)
3943 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
3944 crfree(reg->ushm_cred);
3945 shm_drop(reg->ushm_obj);
3946 uma_zfree(umtx_shm_reg_zone, reg);
3950 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
3954 mtx_assert(&umtx_shm_lock, MA_OWNED);
3955 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
3957 res = reg->ushm_refcnt == 0;
3959 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
3960 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
3961 reg, ushm_reg_link);
3962 reg->ushm_flags &= ~USHMF_REG_LINKED;
3964 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
3965 LIST_REMOVE(reg, ushm_obj_link);
3966 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
3973 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
3979 object = reg->ushm_obj->shm_object;
3980 VM_OBJECT_WLOCK(object);
3981 object->flags |= OBJ_UMTXDEAD;
3982 VM_OBJECT_WUNLOCK(object);
3984 mtx_lock(&umtx_shm_lock);
3985 dofree = umtx_shm_unref_reg_locked(reg, force);
3986 mtx_unlock(&umtx_shm_lock);
3988 umtx_shm_free_reg(reg);
3992 umtx_shm_object_init(vm_object_t object)
3995 LIST_INIT(USHM_OBJ_UMTX(object));
3999 umtx_shm_object_terminated(vm_object_t object)
4001 struct umtx_shm_reg *reg, *reg1;
4004 if (LIST_EMPTY(USHM_OBJ_UMTX(object)))
4008 mtx_lock(&umtx_shm_lock);
4009 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
4010 if (umtx_shm_unref_reg_locked(reg, true)) {
4011 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
4016 mtx_unlock(&umtx_shm_lock);
4018 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
4022 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
4023 struct umtx_shm_reg **res)
4025 struct umtx_shm_reg *reg, *reg1;
4029 reg = umtx_shm_find_reg(key);
4034 cred = td->td_ucred;
4035 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
4037 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
4038 reg->ushm_refcnt = 1;
4039 bcopy(key, ®->ushm_key, sizeof(*key));
4040 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR, false);
4041 reg->ushm_cred = crhold(cred);
4042 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
4044 umtx_shm_free_reg(reg);
4047 mtx_lock(&umtx_shm_lock);
4048 reg1 = umtx_shm_find_reg_locked(key);
4050 mtx_unlock(&umtx_shm_lock);
4051 umtx_shm_free_reg(reg);
4056 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
4057 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
4059 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
4060 mtx_unlock(&umtx_shm_lock);
4066 umtx_shm_alive(struct thread *td, void *addr)
4069 vm_map_entry_t entry;
4076 map = &td->td_proc->p_vmspace->vm_map;
4077 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
4078 &object, &pindex, &prot, &wired);
4079 if (res != KERN_SUCCESS)
4084 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
4085 vm_map_lookup_done(map, entry);
4094 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
4095 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
4096 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
4097 for (i = 0; i < nitems(umtx_shm_registry); i++)
4098 TAILQ_INIT(&umtx_shm_registry[i]);
4102 umtx_shm(struct thread *td, void *addr, u_int flags)
4104 struct umtx_key key;
4105 struct umtx_shm_reg *reg;
4109 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
4110 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
4112 if ((flags & UMTX_SHM_ALIVE) != 0)
4113 return (umtx_shm_alive(td, addr));
4114 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
4117 KASSERT(key.shared == 1, ("non-shared key"));
4118 if ((flags & UMTX_SHM_CREAT) != 0) {
4119 error = umtx_shm_create_reg(td, &key, ®);
4121 reg = umtx_shm_find_reg(&key);
4125 umtx_key_release(&key);
4128 KASSERT(reg != NULL, ("no reg"));
4129 if ((flags & UMTX_SHM_DESTROY) != 0) {
4130 umtx_shm_unref_reg(reg, true);
4134 error = mac_posixshm_check_open(td->td_ucred,
4135 reg->ushm_obj, FFLAGS(O_RDWR));
4138 error = shm_access(reg->ushm_obj, td->td_ucred,
4142 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
4144 shm_hold(reg->ushm_obj);
4145 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
4147 td->td_retval[0] = fd;
4151 umtx_shm_unref_reg(reg, false);
4156 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap,
4157 const struct umtx_copyops *ops __unused)
4160 return (umtx_shm(td, uap->uaddr1, uap->val));
4164 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap,
4165 const struct umtx_copyops *ops)
4167 struct umtx_robust_lists_params rb;
4170 bzero(&rb, sizeof(rb));
4171 error = ops->copyin_robust_lists(uap->uaddr1, uap->val, &rb);
4176 td->td_pflags2 |= TDP2_COMPAT32RB;
4177 else if ((td->td_pflags2 & TDP2_COMPAT32RB) != 0)
4180 td->td_rb_list = rb.robust_list_offset;
4181 td->td_rbp_list = rb.robust_priv_list_offset;
4182 td->td_rb_inact = rb.robust_inact_offset;
4186 #ifdef COMPAT_FREEBSD32
4188 umtx_copyin_timeout32(const void *uaddr, struct timespec *tsp)
4190 struct timespec32 ts32;
4193 error = copyin(uaddr, &ts32, sizeof(ts32));
4195 if (ts32.tv_sec < 0 ||
4196 ts32.tv_nsec >= 1000000000 ||
4200 CP(ts32, *tsp, tv_sec);
4201 CP(ts32, *tsp, tv_nsec);
4208 umtx_copyin_umtx_time32(const void *uaddr, size_t size, struct _umtx_time *tp)
4210 struct umtx_time32 t32;
4213 t32._clockid = CLOCK_REALTIME;
4215 if (size <= sizeof(t32._timeout))
4216 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4218 error = copyin(uaddr, &t32, sizeof(t32));
4221 if (t32._timeout.tv_sec < 0 ||
4222 t32._timeout.tv_nsec >= 1000000000 || t32._timeout.tv_nsec < 0)
4224 TS_CP(t32, *tp, _timeout);
4225 CP(t32, *tp, _flags);
4226 CP(t32, *tp, _clockid);
4231 umtx_copyin_robust_lists32(const void *uaddr, size_t size,
4232 struct umtx_robust_lists_params *rbp)
4234 struct umtx_robust_lists_params_compat32 rb32;
4237 if (size > sizeof(rb32))
4239 bzero(&rb32, sizeof(rb32));
4240 error = copyin(uaddr, &rb32, size);
4243 CP(rb32, *rbp, robust_list_offset);
4244 CP(rb32, *rbp, robust_priv_list_offset);
4245 CP(rb32, *rbp, robust_inact_offset);
4250 umtx_copyout_timeout32(void *uaddr, size_t sz, struct timespec *tsp)
4252 struct timespec32 remain32 = {
4253 .tv_sec = tsp->tv_sec,
4254 .tv_nsec = tsp->tv_nsec,
4258 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4259 * and we're only called if sz >= sizeof(timespec) as supplied in the
4262 KASSERT(sz >= sizeof(remain32),
4263 ("umtx_copyops specifies incorrect sizes"));
4265 return (copyout(&remain32, uaddr, sizeof(remain32)));
4267 #endif /* COMPAT_FREEBSD32 */
4269 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap,
4270 const struct umtx_copyops *umtx_ops);
4272 static const _umtx_op_func op_table[] = {
4273 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
4274 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
4275 [UMTX_OP_WAIT] = __umtx_op_wait,
4276 [UMTX_OP_WAKE] = __umtx_op_wake,
4277 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4278 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
4279 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4280 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4281 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
4282 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4283 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4284 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
4285 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
4286 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
4287 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4288 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4289 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4290 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4291 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4292 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4293 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4294 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4296 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4297 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4299 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4300 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4301 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4302 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4303 [UMTX_OP_SHM] = __umtx_op_shm,
4304 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4307 static const struct umtx_copyops umtx_native_ops = {
4308 .copyin_timeout = umtx_copyin_timeout,
4309 .copyin_umtx_time = umtx_copyin_umtx_time,
4310 .copyin_robust_lists = umtx_copyin_robust_lists,
4311 .copyout_timeout = umtx_copyout_timeout,
4312 .timespec_sz = sizeof(struct timespec),
4313 .umtx_time_sz = sizeof(struct _umtx_time),
4316 #ifdef COMPAT_FREEBSD32
4317 const struct umtx_copyops umtx_native_ops32 = {
4318 .copyin_timeout = umtx_copyin_timeout32,
4319 .copyin_umtx_time = umtx_copyin_umtx_time32,
4320 .copyin_robust_lists = umtx_copyin_robust_lists32,
4321 .copyout_timeout = umtx_copyout_timeout32,
4322 .timespec_sz = sizeof(struct timespec32),
4323 .umtx_time_sz = sizeof(struct umtx_time32),
4329 kern__umtx_op(struct thread *td, void *obj, int op, unsigned long val,
4330 void *uaddr1, void *uaddr2, const struct umtx_copyops *ops)
4332 struct _umtx_op_args uap = {
4340 if ((uap.op >= nitems(op_table)))
4342 return ((*op_table[uap.op])(td, &uap, ops));
4346 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4349 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr1,
4350 uap->uaddr2, &umtx_native_ops));
4353 #ifdef COMPAT_FREEBSD32
4355 freebsd32__umtx_op(struct thread *td, struct freebsd32__umtx_op_args *uap)
4358 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr,
4359 uap->uaddr2, &umtx_native_ops32));
4364 umtx_thread_init(struct thread *td)
4367 td->td_umtxq = umtxq_alloc();
4368 td->td_umtxq->uq_thread = td;
4372 umtx_thread_fini(struct thread *td)
4375 umtxq_free(td->td_umtxq);
4379 * It will be called when new thread is created, e.g fork().
4382 umtx_thread_alloc(struct thread *td)
4387 uq->uq_inherited_pri = PRI_MAX;
4389 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4390 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4391 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4392 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4398 * Clear robust lists for all process' threads, not delaying the
4399 * cleanup to thread exit, since the relevant address space is
4400 * destroyed right now.
4403 umtx_exec(struct proc *p)
4407 KASSERT(p == curproc, ("need curproc"));
4408 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4409 (p->p_flag & P_STOPPED_SINGLE) != 0,
4410 ("curproc must be single-threaded"));
4412 * There is no need to lock the list as only this thread can be
4415 FOREACH_THREAD_IN_PROC(p, td) {
4416 KASSERT(td == curthread ||
4417 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4418 ("running thread %p %p", p, td));
4419 umtx_thread_cleanup(td);
4420 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4428 umtx_thread_exit(struct thread *td)
4431 umtx_thread_cleanup(td);
4435 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res, bool compat32)
4438 #ifdef COMPAT_FREEBSD32
4443 #ifdef COMPAT_FREEBSD32
4445 error = fueword32((void *)ptr, &res32);
4451 error = fueword((void *)ptr, &res1);
4461 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list,
4464 #ifdef COMPAT_FREEBSD32
4465 struct umutex32 m32;
4468 memcpy(&m32, m, sizeof(m32));
4469 *rb_list = m32.m_rb_lnk;
4472 *rb_list = m->m_rb_lnk;
4476 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact,
4482 KASSERT(td->td_proc == curproc, ("need current vmspace"));
4483 error = copyin((void *)rbp, &m, sizeof(m));
4486 if (rb_list != NULL)
4487 umtx_read_rb_list(td, &m, rb_list, compat32);
4488 if ((m.m_flags & UMUTEX_ROBUST) == 0)
4490 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
4491 /* inact is cleared after unlock, allow the inconsistency */
4492 return (inact ? 0 : EINVAL);
4493 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
4497 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
4498 const char *name, bool compat32)
4506 error = umtx_read_uptr(td, rb_list, &rbp, compat32);
4507 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
4508 if (rbp == *rb_inact) {
4513 error = umtx_handle_rb(td, rbp, &rbp, inact, compat32);
4515 if (i == umtx_max_rb && umtx_verbose_rb) {
4516 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
4517 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
4519 if (error != 0 && umtx_verbose_rb) {
4520 uprintf("comm %s pid %d: handling %srb error %d\n",
4521 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
4526 * Clean up umtx data.
4529 umtx_thread_cleanup(struct thread *td)
4537 * Disown pi mutexes.
4541 if (uq->uq_inherited_pri != PRI_MAX ||
4542 !TAILQ_EMPTY(&uq->uq_pi_contested)) {
4543 mtx_lock(&umtx_lock);
4544 uq->uq_inherited_pri = PRI_MAX;
4545 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
4546 pi->pi_owner = NULL;
4547 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
4549 mtx_unlock(&umtx_lock);
4551 sched_lend_user_prio_cond(td, PRI_MAX);
4554 compat32 = (td->td_pflags2 & TDP2_COMPAT32RB) != 0;
4555 td->td_pflags2 &= ~TDP2_COMPAT32RB;
4557 if (td->td_rb_inact == 0 && td->td_rb_list == 0 && td->td_rbp_list == 0)
4561 * Handle terminated robust mutexes. Must be done after
4562 * robust pi disown, otherwise unlock could see unowned
4565 rb_inact = td->td_rb_inact;
4567 (void)umtx_read_uptr(td, rb_inact, &rb_inact, compat32);
4568 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "", compat32);
4569 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ", compat32);
4571 (void)umtx_handle_rb(td, rb_inact, NULL, true, compat32);