2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015, 2016 The FreeBSD Foundation
5 * Copyright (c) 2004, David Xu <davidxu@freebsd.org>
6 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
9 * Portions of this software were developed by Konstantin Belousov
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice unmodified, this list of conditions, and the following
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_umtx_profiling.h"
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
43 #include <sys/filedesc.h>
44 #include <sys/limits.h>
46 #include <sys/malloc.h>
48 #include <sys/mutex.h>
51 #include <sys/resource.h>
52 #include <sys/resourcevar.h>
53 #include <sys/rwlock.h>
55 #include <sys/sched.h>
57 #include <sys/sysctl.h>
58 #include <sys/sysent.h>
59 #include <sys/systm.h>
60 #include <sys/sysproto.h>
61 #include <sys/syscallsubr.h>
62 #include <sys/taskqueue.h>
64 #include <sys/eventhandler.h>
67 #include <security/mac/mac_framework.h>
70 #include <vm/vm_param.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_object.h>
75 #include <machine/atomic.h>
76 #include <machine/cpu.h>
78 #ifdef COMPAT_FREEBSD32
79 #include <compat/freebsd32/freebsd32_proto.h>
83 #define _UMUTEX_WAIT 2
86 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
87 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
90 /* Priority inheritance mutex info. */
93 struct thread *pi_owner;
98 /* List entry to link umtx holding by thread */
99 TAILQ_ENTRY(umtx_pi) pi_link;
101 /* List entry in hash */
102 TAILQ_ENTRY(umtx_pi) pi_hashlink;
104 /* List for waiters */
105 TAILQ_HEAD(,umtx_q) pi_blocked;
107 /* Identify a userland lock object */
108 struct umtx_key pi_key;
111 /* A userland synchronous object user. */
113 /* Linked list for the hash. */
114 TAILQ_ENTRY(umtx_q) uq_link;
117 struct umtx_key uq_key;
121 #define UQF_UMTXQ 0x0001
123 /* The thread waits on. */
124 struct thread *uq_thread;
127 * Blocked on PI mutex. read can use chain lock
128 * or umtx_lock, write must have both chain lock and
129 * umtx_lock being hold.
131 struct umtx_pi *uq_pi_blocked;
133 /* On blocked list */
134 TAILQ_ENTRY(umtx_q) uq_lockq;
136 /* Thread contending with us */
137 TAILQ_HEAD(,umtx_pi) uq_pi_contested;
139 /* Inherited priority from PP mutex */
140 u_char uq_inherited_pri;
142 /* Spare queue ready to be reused */
143 struct umtxq_queue *uq_spare_queue;
145 /* The queue we on */
146 struct umtxq_queue *uq_cur_queue;
149 TAILQ_HEAD(umtxq_head, umtx_q);
151 /* Per-key wait-queue */
153 struct umtxq_head head;
155 LIST_ENTRY(umtxq_queue) link;
159 LIST_HEAD(umtxq_list, umtxq_queue);
161 /* Userland lock object's wait-queue chain */
163 /* Lock for this chain. */
166 /* List of sleep queues. */
167 struct umtxq_list uc_queue[2];
168 #define UMTX_SHARED_QUEUE 0
169 #define UMTX_EXCLUSIVE_QUEUE 1
171 LIST_HEAD(, umtxq_queue) uc_spare_queue;
176 /* Chain lock waiters */
179 /* All PI in the list */
180 TAILQ_HEAD(,umtx_pi) uc_pi_list;
182 #ifdef UMTX_PROFILING
188 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
191 * Don't propagate time-sharing priority, there is a security reason,
192 * a user can simply introduce PI-mutex, let thread A lock the mutex,
193 * and let another thread B block on the mutex, because B is
194 * sleeping, its priority will be boosted, this causes A's priority to
195 * be boosted via priority propagating too and will never be lowered even
196 * if it is using 100%CPU, this is unfair to other processes.
199 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
200 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
201 PRI_MAX_TIMESHARE : (td)->td_user_pri)
203 #define GOLDEN_RATIO_PRIME 2654404609U
205 #define UMTX_CHAINS 512
207 #define UMTX_SHIFTS (__WORD_BIT - 9)
209 #define GET_SHARE(flags) \
210 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
212 #define BUSY_SPINS 200
216 bool is_abs_real; /* TIMER_ABSTIME && CLOCK_REALTIME* */
221 #ifdef COMPAT_FREEBSD32
223 volatile __lwpid_t m_owner; /* Owner of the mutex */
224 __uint32_t m_flags; /* Flags of the mutex */
225 __uint32_t m_ceilings[2]; /* Priority protect ceiling */
226 __uint32_t m_rb_lnk; /* Robust linkage */
228 __uint32_t m_spare[2];
231 _Static_assert(sizeof(struct umutex) == sizeof(struct umutex32), "umutex32");
232 _Static_assert(__offsetof(struct umutex, m_spare[0]) ==
233 __offsetof(struct umutex32, m_spare[0]), "m_spare32");
236 int umtx_shm_vnobj_persistent = 0;
237 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_vnode_persistent, CTLFLAG_RWTUN,
238 &umtx_shm_vnobj_persistent, 0,
239 "False forces destruction of umtx attached to file, on last close");
240 static int umtx_max_rb = 1000;
241 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_max_robust, CTLFLAG_RWTUN,
245 static uma_zone_t umtx_pi_zone;
246 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
247 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
248 static int umtx_pi_allocated;
250 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW, 0, "umtx debug");
251 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
252 &umtx_pi_allocated, 0, "Allocated umtx_pi");
253 static int umtx_verbose_rb = 1;
254 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
258 #ifdef UMTX_PROFILING
259 static long max_length;
260 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
261 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD, 0, "umtx chain stats");
264 static void abs_timeout_update(struct abs_timeout *timo);
266 static void umtx_shm_init(void);
267 static void umtxq_sysinit(void *);
268 static void umtxq_hash(struct umtx_key *key);
269 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
270 static void umtxq_lock(struct umtx_key *key);
271 static void umtxq_unlock(struct umtx_key *key);
272 static void umtxq_busy(struct umtx_key *key);
273 static void umtxq_unbusy(struct umtx_key *key);
274 static void umtxq_insert_queue(struct umtx_q *uq, int q);
275 static void umtxq_remove_queue(struct umtx_q *uq, int q);
276 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *);
277 static int umtxq_count(struct umtx_key *key);
278 static struct umtx_pi *umtx_pi_alloc(int);
279 static void umtx_pi_free(struct umtx_pi *pi);
280 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
282 static void umtx_thread_cleanup(struct thread *td);
283 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
284 struct image_params *imgp __unused);
285 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
287 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
288 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
289 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
291 static struct mtx umtx_lock;
293 #ifdef UMTX_PROFILING
295 umtx_init_profiling(void)
297 struct sysctl_oid *chain_oid;
301 for (i = 0; i < UMTX_CHAINS; ++i) {
302 snprintf(chain_name, sizeof(chain_name), "%d", i);
303 chain_oid = SYSCTL_ADD_NODE(NULL,
304 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
305 chain_name, CTLFLAG_RD, NULL, "umtx hash stats");
306 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
307 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
308 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
309 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
314 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
318 struct umtxq_chain *uc;
319 u_int fract, i, j, tot, whole;
320 u_int sf0, sf1, sf2, sf3, sf4;
321 u_int si0, si1, si2, si3, si4;
322 u_int sw0, sw1, sw2, sw3, sw4;
324 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
325 for (i = 0; i < 2; i++) {
327 for (j = 0; j < UMTX_CHAINS; ++j) {
328 uc = &umtxq_chains[i][j];
329 mtx_lock(&uc->uc_lock);
330 tot += uc->max_length;
331 mtx_unlock(&uc->uc_lock);
334 sbuf_printf(&sb, "%u) Empty ", i);
336 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
337 si0 = si1 = si2 = si3 = si4 = 0;
338 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
339 for (j = 0; j < UMTX_CHAINS; j++) {
340 uc = &umtxq_chains[i][j];
341 mtx_lock(&uc->uc_lock);
342 whole = uc->max_length * 100;
343 mtx_unlock(&uc->uc_lock);
344 fract = (whole % tot) * 100;
345 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
349 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
354 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
359 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
364 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
371 sbuf_printf(&sb, "queue %u:\n", i);
372 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
374 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
376 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
378 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
380 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
386 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
392 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
394 struct umtxq_chain *uc;
399 error = sysctl_handle_int(oidp, &clear, 0, req);
400 if (error != 0 || req->newptr == NULL)
404 for (i = 0; i < 2; ++i) {
405 for (j = 0; j < UMTX_CHAINS; ++j) {
406 uc = &umtxq_chains[i][j];
407 mtx_lock(&uc->uc_lock);
410 mtx_unlock(&uc->uc_lock);
417 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
418 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
419 sysctl_debug_umtx_chains_clear, "I", "Clear umtx chains statistics");
420 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
421 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
422 sysctl_debug_umtx_chains_peaks, "A", "Highest peaks in chains max length");
426 umtxq_sysinit(void *arg __unused)
430 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
431 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
432 for (i = 0; i < 2; ++i) {
433 for (j = 0; j < UMTX_CHAINS; ++j) {
434 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
435 MTX_DEF | MTX_DUPOK);
436 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
437 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
438 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
439 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
440 umtxq_chains[i][j].uc_busy = 0;
441 umtxq_chains[i][j].uc_waiters = 0;
442 #ifdef UMTX_PROFILING
443 umtxq_chains[i][j].length = 0;
444 umtxq_chains[i][j].max_length = 0;
448 #ifdef UMTX_PROFILING
449 umtx_init_profiling();
451 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
452 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
453 EVENTHANDLER_PRI_ANY);
462 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
463 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
465 TAILQ_INIT(&uq->uq_spare_queue->head);
466 TAILQ_INIT(&uq->uq_pi_contested);
467 uq->uq_inherited_pri = PRI_MAX;
472 umtxq_free(struct umtx_q *uq)
475 MPASS(uq->uq_spare_queue != NULL);
476 free(uq->uq_spare_queue, M_UMTX);
481 umtxq_hash(struct umtx_key *key)
485 n = (uintptr_t)key->info.both.a + key->info.both.b;
486 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
489 static inline struct umtxq_chain *
490 umtxq_getchain(struct umtx_key *key)
493 if (key->type <= TYPE_SEM)
494 return (&umtxq_chains[1][key->hash]);
495 return (&umtxq_chains[0][key->hash]);
502 umtxq_lock(struct umtx_key *key)
504 struct umtxq_chain *uc;
506 uc = umtxq_getchain(key);
507 mtx_lock(&uc->uc_lock);
514 umtxq_unlock(struct umtx_key *key)
516 struct umtxq_chain *uc;
518 uc = umtxq_getchain(key);
519 mtx_unlock(&uc->uc_lock);
523 * Set chain to busy state when following operation
524 * may be blocked (kernel mutex can not be used).
527 umtxq_busy(struct umtx_key *key)
529 struct umtxq_chain *uc;
531 uc = umtxq_getchain(key);
532 mtx_assert(&uc->uc_lock, MA_OWNED);
536 int count = BUSY_SPINS;
539 while (uc->uc_busy && --count > 0)
545 while (uc->uc_busy) {
547 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
558 umtxq_unbusy(struct umtx_key *key)
560 struct umtxq_chain *uc;
562 uc = umtxq_getchain(key);
563 mtx_assert(&uc->uc_lock, MA_OWNED);
564 KASSERT(uc->uc_busy != 0, ("not busy"));
571 umtxq_unbusy_unlocked(struct umtx_key *key)
579 static struct umtxq_queue *
580 umtxq_queue_lookup(struct umtx_key *key, int q)
582 struct umtxq_queue *uh;
583 struct umtxq_chain *uc;
585 uc = umtxq_getchain(key);
586 UMTXQ_LOCKED_ASSERT(uc);
587 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
588 if (umtx_key_match(&uh->key, key))
596 umtxq_insert_queue(struct umtx_q *uq, int q)
598 struct umtxq_queue *uh;
599 struct umtxq_chain *uc;
601 uc = umtxq_getchain(&uq->uq_key);
602 UMTXQ_LOCKED_ASSERT(uc);
603 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
604 uh = umtxq_queue_lookup(&uq->uq_key, q);
606 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
608 uh = uq->uq_spare_queue;
609 uh->key = uq->uq_key;
610 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
611 #ifdef UMTX_PROFILING
613 if (uc->length > uc->max_length) {
614 uc->max_length = uc->length;
615 if (uc->max_length > max_length)
616 max_length = uc->max_length;
620 uq->uq_spare_queue = NULL;
622 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
624 uq->uq_flags |= UQF_UMTXQ;
625 uq->uq_cur_queue = uh;
630 umtxq_remove_queue(struct umtx_q *uq, int q)
632 struct umtxq_chain *uc;
633 struct umtxq_queue *uh;
635 uc = umtxq_getchain(&uq->uq_key);
636 UMTXQ_LOCKED_ASSERT(uc);
637 if (uq->uq_flags & UQF_UMTXQ) {
638 uh = uq->uq_cur_queue;
639 TAILQ_REMOVE(&uh->head, uq, uq_link);
641 uq->uq_flags &= ~UQF_UMTXQ;
642 if (TAILQ_EMPTY(&uh->head)) {
643 KASSERT(uh->length == 0,
644 ("inconsistent umtxq_queue length"));
645 #ifdef UMTX_PROFILING
648 LIST_REMOVE(uh, link);
650 uh = LIST_FIRST(&uc->uc_spare_queue);
651 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
652 LIST_REMOVE(uh, link);
654 uq->uq_spare_queue = uh;
655 uq->uq_cur_queue = NULL;
660 * Check if there are multiple waiters
663 umtxq_count(struct umtx_key *key)
665 struct umtxq_queue *uh;
667 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
668 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
675 * Check if there are multiple PI waiters and returns first
679 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
681 struct umtxq_queue *uh;
684 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
685 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
687 *first = TAILQ_FIRST(&uh->head);
694 * Wake up threads waiting on an userland object.
698 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
700 struct umtxq_queue *uh;
705 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
706 uh = umtxq_queue_lookup(key, q);
708 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
709 umtxq_remove_queue(uq, q);
719 * Wake up specified thread.
722 umtxq_signal_thread(struct umtx_q *uq)
725 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
731 tstohz(const struct timespec *tsp)
735 TIMESPEC_TO_TIMEVAL(&tv, tsp);
740 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
741 const struct timespec *timeout)
744 timo->clockid = clockid;
746 timo->is_abs_real = false;
747 abs_timeout_update(timo);
748 timespecadd(&timo->cur, timeout, &timo->end);
750 timo->end = *timeout;
751 timo->is_abs_real = clockid == CLOCK_REALTIME ||
752 clockid == CLOCK_REALTIME_FAST ||
753 clockid == CLOCK_REALTIME_PRECISE;
755 * If is_abs_real, umtxq_sleep will read the clock
756 * after setting td_rtcgen; otherwise, read it here.
758 if (!timo->is_abs_real) {
759 abs_timeout_update(timo);
765 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
768 abs_timeout_init(timo, umtxtime->_clockid,
769 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
773 abs_timeout_update(struct abs_timeout *timo)
776 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
780 abs_timeout_gethz(struct abs_timeout *timo)
784 if (timespeccmp(&timo->end, &timo->cur, <=))
786 timespecsub(&timo->end, &timo->cur, &tts);
787 return (tstohz(&tts));
791 umtx_unlock_val(uint32_t flags, bool rb)
795 return (UMUTEX_RB_OWNERDEAD);
796 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
797 return (UMUTEX_RB_NOTRECOV);
799 return (UMUTEX_UNOWNED);
804 * Put thread into sleep state, before sleeping, check if
805 * thread was removed from umtx queue.
808 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
810 struct umtxq_chain *uc;
813 if (abstime != NULL && abstime->is_abs_real) {
814 curthread->td_rtcgen = atomic_load_acq_int(&rtc_generation);
815 abs_timeout_update(abstime);
818 uc = umtxq_getchain(&uq->uq_key);
819 UMTXQ_LOCKED_ASSERT(uc);
821 if (!(uq->uq_flags & UQF_UMTXQ)) {
825 if (abstime != NULL) {
826 timo = abs_timeout_gethz(abstime);
833 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
834 if (error == EINTR || error == ERESTART) {
835 umtxq_lock(&uq->uq_key);
838 if (abstime != NULL) {
839 if (abstime->is_abs_real)
840 curthread->td_rtcgen =
841 atomic_load_acq_int(&rtc_generation);
842 abs_timeout_update(abstime);
844 umtxq_lock(&uq->uq_key);
847 curthread->td_rtcgen = 0;
852 * Convert userspace address into unique logical address.
855 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
857 struct thread *td = curthread;
859 vm_map_entry_t entry;
865 if (share == THREAD_SHARE) {
867 key->info.private.vs = td->td_proc->p_vmspace;
868 key->info.private.addr = (uintptr_t)addr;
870 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
871 map = &td->td_proc->p_vmspace->vm_map;
872 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
873 &entry, &key->info.shared.object, &pindex, &prot,
874 &wired) != KERN_SUCCESS) {
878 if ((share == PROCESS_SHARE) ||
879 (share == AUTO_SHARE &&
880 VM_INHERIT_SHARE == entry->inheritance)) {
882 key->info.shared.offset = (vm_offset_t)addr -
883 entry->start + entry->offset;
884 vm_object_reference(key->info.shared.object);
887 key->info.private.vs = td->td_proc->p_vmspace;
888 key->info.private.addr = (uintptr_t)addr;
890 vm_map_lookup_done(map, entry);
901 umtx_key_release(struct umtx_key *key)
904 vm_object_deallocate(key->info.shared.object);
908 * Fetch and compare value, sleep on the address if value is not changed.
911 do_wait(struct thread *td, void *addr, u_long id,
912 struct _umtx_time *timeout, int compat32, int is_private)
914 struct abs_timeout timo;
921 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
922 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
926 abs_timeout_init2(&timo, timeout);
928 umtxq_lock(&uq->uq_key);
930 umtxq_unlock(&uq->uq_key);
932 error = fueword(addr, &tmp);
936 error = fueword32(addr, &tmp32);
942 umtxq_lock(&uq->uq_key);
945 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
947 if ((uq->uq_flags & UQF_UMTXQ) == 0)
951 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
954 umtxq_unlock(&uq->uq_key);
955 umtx_key_release(&uq->uq_key);
956 if (error == ERESTART)
962 * Wake up threads sleeping on the specified address.
965 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
970 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
971 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
974 umtxq_signal(&key, n_wake);
976 umtx_key_release(&key);
981 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
984 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
985 struct _umtx_time *timeout, int mode)
987 struct abs_timeout timo;
989 uint32_t owner, old, id;
996 abs_timeout_init2(&timo, timeout);
999 * Care must be exercised when dealing with umtx structure. It
1000 * can fault on any access.
1003 rv = fueword32(&m->m_owner, &owner);
1006 if (mode == _UMUTEX_WAIT) {
1007 if (owner == UMUTEX_UNOWNED ||
1008 owner == UMUTEX_CONTESTED ||
1009 owner == UMUTEX_RB_OWNERDEAD ||
1010 owner == UMUTEX_RB_NOTRECOV)
1014 * Robust mutex terminated. Kernel duty is to
1015 * return EOWNERDEAD to the userspace. The
1016 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1017 * by the common userspace code.
1019 if (owner == UMUTEX_RB_OWNERDEAD) {
1020 rv = casueword32(&m->m_owner,
1021 UMUTEX_RB_OWNERDEAD, &owner,
1022 id | UMUTEX_CONTESTED);
1026 MPASS(owner == UMUTEX_RB_OWNERDEAD);
1027 return (EOWNERDEAD); /* success */
1030 rv = thread_check_susp(td, false);
1035 if (owner == UMUTEX_RB_NOTRECOV)
1036 return (ENOTRECOVERABLE);
1039 * Try the uncontested case. This should be
1042 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1044 /* The address was invalid. */
1048 /* The acquire succeeded. */
1050 MPASS(owner == UMUTEX_UNOWNED);
1055 * If no one owns it but it is contested try
1059 if (owner == UMUTEX_CONTESTED) {
1060 rv = casueword32(&m->m_owner,
1061 UMUTEX_CONTESTED, &owner,
1062 id | UMUTEX_CONTESTED);
1063 /* The address was invalid. */
1067 MPASS(owner == UMUTEX_CONTESTED);
1071 rv = thread_check_susp(td, false);
1077 * If this failed the lock has
1083 /* rv == 1 but not contested, likely store failure */
1084 rv = thread_check_susp(td, false);
1089 if (mode == _UMUTEX_TRY)
1093 * If we caught a signal, we have retried and now
1099 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1100 GET_SHARE(flags), &uq->uq_key)) != 0)
1103 umtxq_lock(&uq->uq_key);
1104 umtxq_busy(&uq->uq_key);
1106 umtxq_unlock(&uq->uq_key);
1109 * Set the contested bit so that a release in user space
1110 * knows to use the system call for unlock. If this fails
1111 * either some one else has acquired the lock or it has been
1114 rv = casueword32(&m->m_owner, owner, &old,
1115 owner | UMUTEX_CONTESTED);
1117 /* The address was invalid or casueword failed to store. */
1118 if (rv == -1 || rv == 1) {
1119 umtxq_lock(&uq->uq_key);
1121 umtxq_unbusy(&uq->uq_key);
1122 umtxq_unlock(&uq->uq_key);
1123 umtx_key_release(&uq->uq_key);
1127 rv = thread_check_susp(td, false);
1135 * We set the contested bit, sleep. Otherwise the lock changed
1136 * and we need to retry or we lost a race to the thread
1137 * unlocking the umtx.
1139 umtxq_lock(&uq->uq_key);
1140 umtxq_unbusy(&uq->uq_key);
1141 MPASS(old == owner);
1142 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1145 umtxq_unlock(&uq->uq_key);
1146 umtx_key_release(&uq->uq_key);
1149 error = thread_check_susp(td, false);
1156 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1159 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1161 struct umtx_key key;
1162 uint32_t owner, old, id, newlock;
1169 * Make sure we own this mtx.
1171 error = fueword32(&m->m_owner, &owner);
1175 if ((owner & ~UMUTEX_CONTESTED) != id)
1178 newlock = umtx_unlock_val(flags, rb);
1179 if ((owner & UMUTEX_CONTESTED) == 0) {
1180 error = casueword32(&m->m_owner, owner, &old, newlock);
1184 error = thread_check_susp(td, false);
1189 MPASS(old == owner);
1193 /* We should only ever be in here for contested locks */
1194 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1200 count = umtxq_count(&key);
1204 * When unlocking the umtx, it must be marked as unowned if
1205 * there is zero or one thread only waiting for it.
1206 * Otherwise, it must be marked as contested.
1209 newlock |= UMUTEX_CONTESTED;
1210 error = casueword32(&m->m_owner, owner, &old, newlock);
1212 umtxq_signal(&key, 1);
1215 umtx_key_release(&key);
1221 error = thread_check_susp(td, false);
1230 * Check if the mutex is available and wake up a waiter,
1231 * only for simple mutex.
1234 do_wake_umutex(struct thread *td, struct umutex *m)
1236 struct umtx_key key;
1243 error = fueword32(&m->m_owner, &owner);
1247 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1248 owner != UMUTEX_RB_NOTRECOV)
1251 error = fueword32(&m->m_flags, &flags);
1255 /* We should only ever be in here for contested locks */
1256 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1262 count = umtxq_count(&key);
1265 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1266 owner != UMUTEX_RB_NOTRECOV) {
1267 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1271 } else if (error == 1) {
1275 umtx_key_release(&key);
1276 error = thread_check_susp(td, false);
1284 if (error == 0 && count != 0) {
1285 MPASS((owner & ~UMUTEX_CONTESTED) == 0 ||
1286 owner == UMUTEX_RB_OWNERDEAD ||
1287 owner == UMUTEX_RB_NOTRECOV);
1288 umtxq_signal(&key, 1);
1292 umtx_key_release(&key);
1297 * Check if the mutex has waiters and tries to fix contention bit.
1300 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1302 struct umtx_key key;
1303 uint32_t owner, old;
1308 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1312 type = TYPE_NORMAL_UMUTEX;
1314 case UMUTEX_PRIO_INHERIT:
1315 type = TYPE_PI_UMUTEX;
1317 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1318 type = TYPE_PI_ROBUST_UMUTEX;
1320 case UMUTEX_PRIO_PROTECT:
1321 type = TYPE_PP_UMUTEX;
1323 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1324 type = TYPE_PP_ROBUST_UMUTEX;
1329 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1335 count = umtxq_count(&key);
1338 error = fueword32(&m->m_owner, &owner);
1343 * Only repair contention bit if there is a waiter, this means
1344 * the mutex is still being referenced by userland code,
1345 * otherwise don't update any memory.
1347 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0 &&
1348 (count > 1 || (count == 1 && (owner & ~UMUTEX_CONTESTED) != 0))) {
1349 error = casueword32(&m->m_owner, owner, &old,
1350 owner | UMUTEX_CONTESTED);
1356 MPASS(old == owner);
1360 error = thread_check_susp(td, false);
1364 if (error == EFAULT) {
1365 umtxq_signal(&key, INT_MAX);
1366 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1367 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1368 umtxq_signal(&key, 1);
1371 umtx_key_release(&key);
1375 static inline struct umtx_pi *
1376 umtx_pi_alloc(int flags)
1380 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1381 TAILQ_INIT(&pi->pi_blocked);
1382 atomic_add_int(&umtx_pi_allocated, 1);
1387 umtx_pi_free(struct umtx_pi *pi)
1389 uma_zfree(umtx_pi_zone, pi);
1390 atomic_add_int(&umtx_pi_allocated, -1);
1394 * Adjust the thread's position on a pi_state after its priority has been
1398 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1400 struct umtx_q *uq, *uq1, *uq2;
1403 mtx_assert(&umtx_lock, MA_OWNED);
1410 * Check if the thread needs to be moved on the blocked chain.
1411 * It needs to be moved if either its priority is lower than
1412 * the previous thread or higher than the next thread.
1414 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1415 uq2 = TAILQ_NEXT(uq, uq_lockq);
1416 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1417 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1419 * Remove thread from blocked chain and determine where
1420 * it should be moved to.
1422 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1423 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1424 td1 = uq1->uq_thread;
1425 MPASS(td1->td_proc->p_magic == P_MAGIC);
1426 if (UPRI(td1) > UPRI(td))
1431 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1433 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1438 static struct umtx_pi *
1439 umtx_pi_next(struct umtx_pi *pi)
1441 struct umtx_q *uq_owner;
1443 if (pi->pi_owner == NULL)
1445 uq_owner = pi->pi_owner->td_umtxq;
1446 if (uq_owner == NULL)
1448 return (uq_owner->uq_pi_blocked);
1452 * Floyd's Cycle-Finding Algorithm.
1455 umtx_pi_check_loop(struct umtx_pi *pi)
1457 struct umtx_pi *pi1; /* fast iterator */
1459 mtx_assert(&umtx_lock, MA_OWNED);
1464 pi = umtx_pi_next(pi);
1467 pi1 = umtx_pi_next(pi1);
1470 pi1 = umtx_pi_next(pi1);
1480 * Propagate priority when a thread is blocked on POSIX
1484 umtx_propagate_priority(struct thread *td)
1490 mtx_assert(&umtx_lock, MA_OWNED);
1493 pi = uq->uq_pi_blocked;
1496 if (umtx_pi_check_loop(pi))
1501 if (td == NULL || td == curthread)
1504 MPASS(td->td_proc != NULL);
1505 MPASS(td->td_proc->p_magic == P_MAGIC);
1508 if (td->td_lend_user_pri > pri)
1509 sched_lend_user_prio(td, pri);
1517 * Pick up the lock that td is blocked on.
1520 pi = uq->uq_pi_blocked;
1523 /* Resort td on the list if needed. */
1524 umtx_pi_adjust_thread(pi, td);
1529 * Unpropagate priority for a PI mutex when a thread blocked on
1530 * it is interrupted by signal or resumed by others.
1533 umtx_repropagate_priority(struct umtx_pi *pi)
1535 struct umtx_q *uq, *uq_owner;
1536 struct umtx_pi *pi2;
1539 mtx_assert(&umtx_lock, MA_OWNED);
1541 if (umtx_pi_check_loop(pi))
1543 while (pi != NULL && pi->pi_owner != NULL) {
1545 uq_owner = pi->pi_owner->td_umtxq;
1547 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1548 uq = TAILQ_FIRST(&pi2->pi_blocked);
1550 if (pri > UPRI(uq->uq_thread))
1551 pri = UPRI(uq->uq_thread);
1555 if (pri > uq_owner->uq_inherited_pri)
1556 pri = uq_owner->uq_inherited_pri;
1557 thread_lock(pi->pi_owner);
1558 sched_lend_user_prio(pi->pi_owner, pri);
1559 thread_unlock(pi->pi_owner);
1560 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1561 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1566 * Insert a PI mutex into owned list.
1569 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1571 struct umtx_q *uq_owner;
1573 uq_owner = owner->td_umtxq;
1574 mtx_assert(&umtx_lock, MA_OWNED);
1575 MPASS(pi->pi_owner == NULL);
1576 pi->pi_owner = owner;
1577 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1581 * Disown a PI mutex, and remove it from the owned list.
1584 umtx_pi_disown(struct umtx_pi *pi)
1587 mtx_assert(&umtx_lock, MA_OWNED);
1588 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1589 pi->pi_owner = NULL;
1593 * Claim ownership of a PI mutex.
1596 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1601 mtx_lock(&umtx_lock);
1602 if (pi->pi_owner == owner) {
1603 mtx_unlock(&umtx_lock);
1607 if (pi->pi_owner != NULL) {
1609 * userland may have already messed the mutex, sigh.
1611 mtx_unlock(&umtx_lock);
1614 umtx_pi_setowner(pi, owner);
1615 uq = TAILQ_FIRST(&pi->pi_blocked);
1617 pri = UPRI(uq->uq_thread);
1619 if (pri < UPRI(owner))
1620 sched_lend_user_prio(owner, pri);
1621 thread_unlock(owner);
1623 mtx_unlock(&umtx_lock);
1628 * Adjust a thread's order position in its blocked PI mutex,
1629 * this may result new priority propagating process.
1632 umtx_pi_adjust(struct thread *td, u_char oldpri)
1638 mtx_lock(&umtx_lock);
1640 * Pick up the lock that td is blocked on.
1642 pi = uq->uq_pi_blocked;
1644 umtx_pi_adjust_thread(pi, td);
1645 umtx_repropagate_priority(pi);
1647 mtx_unlock(&umtx_lock);
1651 * Sleep on a PI mutex.
1654 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
1655 const char *wmesg, struct abs_timeout *timo, bool shared)
1657 struct thread *td, *td1;
1661 struct umtxq_chain *uc;
1663 uc = umtxq_getchain(&pi->pi_key);
1667 KASSERT(td == curthread, ("inconsistent uq_thread"));
1668 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
1669 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1671 mtx_lock(&umtx_lock);
1672 if (pi->pi_owner == NULL) {
1673 mtx_unlock(&umtx_lock);
1674 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
1675 mtx_lock(&umtx_lock);
1677 if (pi->pi_owner == NULL)
1678 umtx_pi_setowner(pi, td1);
1679 PROC_UNLOCK(td1->td_proc);
1683 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1684 pri = UPRI(uq1->uq_thread);
1690 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1692 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1694 uq->uq_pi_blocked = pi;
1696 td->td_flags |= TDF_UPIBLOCKED;
1698 umtx_propagate_priority(td);
1699 mtx_unlock(&umtx_lock);
1700 umtxq_unbusy(&uq->uq_key);
1702 error = umtxq_sleep(uq, wmesg, timo);
1705 mtx_lock(&umtx_lock);
1706 uq->uq_pi_blocked = NULL;
1708 td->td_flags &= ~TDF_UPIBLOCKED;
1710 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1711 umtx_repropagate_priority(pi);
1712 mtx_unlock(&umtx_lock);
1713 umtxq_unlock(&uq->uq_key);
1719 * Add reference count for a PI mutex.
1722 umtx_pi_ref(struct umtx_pi *pi)
1725 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
1730 * Decrease reference count for a PI mutex, if the counter
1731 * is decreased to zero, its memory space is freed.
1734 umtx_pi_unref(struct umtx_pi *pi)
1736 struct umtxq_chain *uc;
1738 uc = umtxq_getchain(&pi->pi_key);
1739 UMTXQ_LOCKED_ASSERT(uc);
1740 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1741 if (--pi->pi_refcount == 0) {
1742 mtx_lock(&umtx_lock);
1743 if (pi->pi_owner != NULL)
1745 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1746 ("blocked queue not empty"));
1747 mtx_unlock(&umtx_lock);
1748 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1754 * Find a PI mutex in hash table.
1756 static struct umtx_pi *
1757 umtx_pi_lookup(struct umtx_key *key)
1759 struct umtxq_chain *uc;
1762 uc = umtxq_getchain(key);
1763 UMTXQ_LOCKED_ASSERT(uc);
1765 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1766 if (umtx_key_match(&pi->pi_key, key)) {
1774 * Insert a PI mutex into hash table.
1777 umtx_pi_insert(struct umtx_pi *pi)
1779 struct umtxq_chain *uc;
1781 uc = umtxq_getchain(&pi->pi_key);
1782 UMTXQ_LOCKED_ASSERT(uc);
1783 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1790 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1791 struct _umtx_time *timeout, int try)
1793 struct abs_timeout timo;
1795 struct umtx_pi *pi, *new_pi;
1796 uint32_t id, old_owner, owner, old;
1802 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
1803 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
1807 if (timeout != NULL)
1808 abs_timeout_init2(&timo, timeout);
1810 umtxq_lock(&uq->uq_key);
1811 pi = umtx_pi_lookup(&uq->uq_key);
1813 new_pi = umtx_pi_alloc(M_NOWAIT);
1814 if (new_pi == NULL) {
1815 umtxq_unlock(&uq->uq_key);
1816 new_pi = umtx_pi_alloc(M_WAITOK);
1817 umtxq_lock(&uq->uq_key);
1818 pi = umtx_pi_lookup(&uq->uq_key);
1820 umtx_pi_free(new_pi);
1824 if (new_pi != NULL) {
1825 new_pi->pi_key = uq->uq_key;
1826 umtx_pi_insert(new_pi);
1831 umtxq_unlock(&uq->uq_key);
1834 * Care must be exercised when dealing with umtx structure. It
1835 * can fault on any access.
1839 * Try the uncontested case. This should be done in userland.
1841 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
1842 /* The address was invalid. */
1847 /* The acquire succeeded. */
1849 MPASS(owner == UMUTEX_UNOWNED);
1854 if (owner == UMUTEX_RB_NOTRECOV) {
1855 error = ENOTRECOVERABLE;
1860 * Avoid overwriting a possible error from sleep due
1861 * to the pending signal with suspension check result.
1864 error = thread_check_susp(td, true);
1869 /* If no one owns it but it is contested try to acquire it. */
1870 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
1872 rv = casueword32(&m->m_owner, owner, &owner,
1873 id | UMUTEX_CONTESTED);
1874 /* The address was invalid. */
1881 error = thread_check_susp(td, true);
1887 * If this failed the lock could
1894 MPASS(owner == old_owner);
1895 umtxq_lock(&uq->uq_key);
1896 umtxq_busy(&uq->uq_key);
1897 error = umtx_pi_claim(pi, td);
1898 umtxq_unbusy(&uq->uq_key);
1899 umtxq_unlock(&uq->uq_key);
1902 * Since we're going to return an
1903 * error, restore the m_owner to its
1904 * previous, unowned state to avoid
1905 * compounding the problem.
1907 (void)casuword32(&m->m_owner,
1908 id | UMUTEX_CONTESTED, old_owner);
1910 if (error == 0 && old_owner == UMUTEX_RB_OWNERDEAD)
1915 if ((owner & ~UMUTEX_CONTESTED) == id) {
1926 * If we caught a signal, we have retried and now
1932 umtxq_lock(&uq->uq_key);
1933 umtxq_busy(&uq->uq_key);
1934 umtxq_unlock(&uq->uq_key);
1937 * Set the contested bit so that a release in user space
1938 * knows to use the system call for unlock. If this fails
1939 * either some one else has acquired the lock or it has been
1942 rv = casueword32(&m->m_owner, owner, &old, owner |
1945 /* The address was invalid. */
1947 umtxq_unbusy_unlocked(&uq->uq_key);
1952 umtxq_unbusy_unlocked(&uq->uq_key);
1953 error = thread_check_susp(td, true);
1958 * The lock changed and we need to retry or we
1959 * lost a race to the thread unlocking the
1960 * umtx. Note that the UMUTEX_RB_OWNERDEAD
1961 * value for owner is impossible there.
1966 umtxq_lock(&uq->uq_key);
1968 /* We set the contested bit, sleep. */
1969 MPASS(old == owner);
1970 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
1971 "umtxpi", timeout == NULL ? NULL : &timo,
1972 (flags & USYNC_PROCESS_SHARED) != 0);
1976 error = thread_check_susp(td, false);
1981 umtxq_lock(&uq->uq_key);
1983 umtxq_unlock(&uq->uq_key);
1985 umtx_key_release(&uq->uq_key);
1990 * Unlock a PI mutex.
1993 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1995 struct umtx_key key;
1996 struct umtx_q *uq_first, *uq_first2, *uq_me;
1997 struct umtx_pi *pi, *pi2;
1998 uint32_t id, new_owner, old, owner;
1999 int count, error, pri;
2005 * Make sure we own this mtx.
2007 error = fueword32(&m->m_owner, &owner);
2011 if ((owner & ~UMUTEX_CONTESTED) != id)
2014 new_owner = umtx_unlock_val(flags, rb);
2016 /* This should be done in userland */
2017 if ((owner & UMUTEX_CONTESTED) == 0) {
2018 error = casueword32(&m->m_owner, owner, &old, new_owner);
2022 error = thread_check_susp(td, true);
2032 /* We should only ever be in here for contested locks */
2033 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2034 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2040 count = umtxq_count_pi(&key, &uq_first);
2041 if (uq_first != NULL) {
2042 mtx_lock(&umtx_lock);
2043 pi = uq_first->uq_pi_blocked;
2044 KASSERT(pi != NULL, ("pi == NULL?"));
2045 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2046 mtx_unlock(&umtx_lock);
2049 umtx_key_release(&key);
2050 /* userland messed the mutex */
2053 uq_me = td->td_umtxq;
2054 if (pi->pi_owner == td)
2056 /* get highest priority thread which is still sleeping. */
2057 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2058 while (uq_first != NULL &&
2059 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2060 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2063 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2064 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2065 if (uq_first2 != NULL) {
2066 if (pri > UPRI(uq_first2->uq_thread))
2067 pri = UPRI(uq_first2->uq_thread);
2071 sched_lend_user_prio(td, pri);
2073 mtx_unlock(&umtx_lock);
2075 umtxq_signal_thread(uq_first);
2077 pi = umtx_pi_lookup(&key);
2079 * A umtx_pi can exist if a signal or timeout removed the
2080 * last waiter from the umtxq, but there is still
2081 * a thread in do_lock_pi() holding the umtx_pi.
2085 * The umtx_pi can be unowned, such as when a thread
2086 * has just entered do_lock_pi(), allocated the
2087 * umtx_pi, and unlocked the umtxq.
2088 * If the current thread owns it, it must disown it.
2090 mtx_lock(&umtx_lock);
2091 if (pi->pi_owner == td)
2093 mtx_unlock(&umtx_lock);
2099 * When unlocking the umtx, it must be marked as unowned if
2100 * there is zero or one thread only waiting for it.
2101 * Otherwise, it must be marked as contested.
2105 new_owner |= UMUTEX_CONTESTED;
2107 error = casueword32(&m->m_owner, owner, &old, new_owner);
2109 error = thread_check_susp(td, false);
2113 umtxq_unbusy_unlocked(&key);
2114 umtx_key_release(&key);
2117 if (error == 0 && old != owner)
2126 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2127 struct _umtx_time *timeout, int try)
2129 struct abs_timeout timo;
2130 struct umtx_q *uq, *uq2;
2134 int error, pri, old_inherited_pri, su, rv;
2138 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2139 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2143 if (timeout != NULL)
2144 abs_timeout_init2(&timo, timeout);
2146 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2148 old_inherited_pri = uq->uq_inherited_pri;
2149 umtxq_lock(&uq->uq_key);
2150 umtxq_busy(&uq->uq_key);
2151 umtxq_unlock(&uq->uq_key);
2153 rv = fueword32(&m->m_ceilings[0], &ceiling);
2158 ceiling = RTP_PRIO_MAX - ceiling;
2159 if (ceiling > RTP_PRIO_MAX) {
2164 mtx_lock(&umtx_lock);
2165 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2166 mtx_unlock(&umtx_lock);
2170 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2171 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2173 if (uq->uq_inherited_pri < UPRI(td))
2174 sched_lend_user_prio(td, uq->uq_inherited_pri);
2177 mtx_unlock(&umtx_lock);
2179 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2180 id | UMUTEX_CONTESTED);
2181 /* The address was invalid. */
2187 MPASS(owner == UMUTEX_CONTESTED);
2192 if (owner == UMUTEX_RB_OWNERDEAD) {
2193 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2194 &owner, id | UMUTEX_CONTESTED);
2200 MPASS(owner == UMUTEX_RB_OWNERDEAD);
2201 error = EOWNERDEAD; /* success */
2206 * rv == 1, only check for suspension if we
2207 * did not already catched a signal. If we
2208 * get an error from the check, the same
2209 * condition is checked by the umtxq_sleep()
2210 * call below, so we should obliterate the
2211 * error to not skip the last loop iteration.
2214 error = thread_check_susp(td, false);
2223 } else if (owner == UMUTEX_RB_NOTRECOV) {
2224 error = ENOTRECOVERABLE;
2231 * If we caught a signal, we have retried and now
2237 umtxq_lock(&uq->uq_key);
2239 umtxq_unbusy(&uq->uq_key);
2240 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2243 umtxq_unlock(&uq->uq_key);
2245 mtx_lock(&umtx_lock);
2246 uq->uq_inherited_pri = old_inherited_pri;
2248 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2249 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2251 if (pri > UPRI(uq2->uq_thread))
2252 pri = UPRI(uq2->uq_thread);
2255 if (pri > uq->uq_inherited_pri)
2256 pri = uq->uq_inherited_pri;
2258 sched_lend_user_prio(td, pri);
2260 mtx_unlock(&umtx_lock);
2263 if (error != 0 && error != EOWNERDEAD) {
2264 mtx_lock(&umtx_lock);
2265 uq->uq_inherited_pri = old_inherited_pri;
2267 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2268 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2270 if (pri > UPRI(uq2->uq_thread))
2271 pri = UPRI(uq2->uq_thread);
2274 if (pri > uq->uq_inherited_pri)
2275 pri = uq->uq_inherited_pri;
2277 sched_lend_user_prio(td, pri);
2279 mtx_unlock(&umtx_lock);
2283 umtxq_unbusy_unlocked(&uq->uq_key);
2284 umtx_key_release(&uq->uq_key);
2289 * Unlock a PP mutex.
2292 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2294 struct umtx_key key;
2295 struct umtx_q *uq, *uq2;
2297 uint32_t id, owner, rceiling;
2298 int error, pri, new_inherited_pri, su;
2302 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2305 * Make sure we own this mtx.
2307 error = fueword32(&m->m_owner, &owner);
2311 if ((owner & ~UMUTEX_CONTESTED) != id)
2314 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2319 new_inherited_pri = PRI_MAX;
2321 rceiling = RTP_PRIO_MAX - rceiling;
2322 if (rceiling > RTP_PRIO_MAX)
2324 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2327 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2328 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2335 * For priority protected mutex, always set unlocked state
2336 * to UMUTEX_CONTESTED, so that userland always enters kernel
2337 * to lock the mutex, it is necessary because thread priority
2338 * has to be adjusted for such mutex.
2340 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2345 umtxq_signal(&key, 1);
2352 mtx_lock(&umtx_lock);
2354 uq->uq_inherited_pri = new_inherited_pri;
2356 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2357 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2359 if (pri > UPRI(uq2->uq_thread))
2360 pri = UPRI(uq2->uq_thread);
2363 if (pri > uq->uq_inherited_pri)
2364 pri = uq->uq_inherited_pri;
2366 sched_lend_user_prio(td, pri);
2368 mtx_unlock(&umtx_lock);
2370 umtx_key_release(&key);
2375 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2376 uint32_t *old_ceiling)
2379 uint32_t flags, id, owner, save_ceiling;
2382 error = fueword32(&m->m_flags, &flags);
2385 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2387 if (ceiling > RTP_PRIO_MAX)
2391 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2392 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2396 umtxq_lock(&uq->uq_key);
2397 umtxq_busy(&uq->uq_key);
2398 umtxq_unlock(&uq->uq_key);
2400 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2406 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2407 id | UMUTEX_CONTESTED);
2414 MPASS(owner == UMUTEX_CONTESTED);
2415 rv = suword32(&m->m_ceilings[0], ceiling);
2416 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2417 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2421 if ((owner & ~UMUTEX_CONTESTED) == id) {
2422 rv = suword32(&m->m_ceilings[0], ceiling);
2423 error = rv == 0 ? 0 : EFAULT;
2427 if (owner == UMUTEX_RB_OWNERDEAD) {
2430 } else if (owner == UMUTEX_RB_NOTRECOV) {
2431 error = ENOTRECOVERABLE;
2436 * If we caught a signal, we have retried and now
2443 * We set the contested bit, sleep. Otherwise the lock changed
2444 * and we need to retry or we lost a race to the thread
2445 * unlocking the umtx.
2447 umtxq_lock(&uq->uq_key);
2449 umtxq_unbusy(&uq->uq_key);
2450 error = umtxq_sleep(uq, "umtxpp", NULL);
2452 umtxq_unlock(&uq->uq_key);
2454 umtxq_lock(&uq->uq_key);
2456 umtxq_signal(&uq->uq_key, INT_MAX);
2457 umtxq_unbusy(&uq->uq_key);
2458 umtxq_unlock(&uq->uq_key);
2459 umtx_key_release(&uq->uq_key);
2460 if (error == 0 && old_ceiling != NULL) {
2461 rv = suword32(old_ceiling, save_ceiling);
2462 error = rv == 0 ? 0 : EFAULT;
2468 * Lock a userland POSIX mutex.
2471 do_lock_umutex(struct thread *td, struct umutex *m,
2472 struct _umtx_time *timeout, int mode)
2477 error = fueword32(&m->m_flags, &flags);
2481 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2483 error = do_lock_normal(td, m, flags, timeout, mode);
2485 case UMUTEX_PRIO_INHERIT:
2486 error = do_lock_pi(td, m, flags, timeout, mode);
2488 case UMUTEX_PRIO_PROTECT:
2489 error = do_lock_pp(td, m, flags, timeout, mode);
2494 if (timeout == NULL) {
2495 if (error == EINTR && mode != _UMUTEX_WAIT)
2498 /* Timed-locking is not restarted. */
2499 if (error == ERESTART)
2506 * Unlock a userland POSIX mutex.
2509 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2514 error = fueword32(&m->m_flags, &flags);
2518 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2520 return (do_unlock_normal(td, m, flags, rb));
2521 case UMUTEX_PRIO_INHERIT:
2522 return (do_unlock_pi(td, m, flags, rb));
2523 case UMUTEX_PRIO_PROTECT:
2524 return (do_unlock_pp(td, m, flags, rb));
2531 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2532 struct timespec *timeout, u_long wflags)
2534 struct abs_timeout timo;
2536 uint32_t flags, clockid, hasw;
2540 error = fueword32(&cv->c_flags, &flags);
2543 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2547 if ((wflags & CVWAIT_CLOCKID) != 0) {
2548 error = fueword32(&cv->c_clockid, &clockid);
2550 umtx_key_release(&uq->uq_key);
2553 if (clockid < CLOCK_REALTIME ||
2554 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2555 /* hmm, only HW clock id will work. */
2556 umtx_key_release(&uq->uq_key);
2560 clockid = CLOCK_REALTIME;
2563 umtxq_lock(&uq->uq_key);
2564 umtxq_busy(&uq->uq_key);
2566 umtxq_unlock(&uq->uq_key);
2569 * Set c_has_waiters to 1 before releasing user mutex, also
2570 * don't modify cache line when unnecessary.
2572 error = fueword32(&cv->c_has_waiters, &hasw);
2573 if (error == 0 && hasw == 0)
2574 suword32(&cv->c_has_waiters, 1);
2576 umtxq_unbusy_unlocked(&uq->uq_key);
2578 error = do_unlock_umutex(td, m, false);
2580 if (timeout != NULL)
2581 abs_timeout_init(&timo, clockid, (wflags & CVWAIT_ABSTIME) != 0,
2584 umtxq_lock(&uq->uq_key);
2586 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2590 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2594 * This must be timeout,interrupted by signal or
2595 * surprious wakeup, clear c_has_waiter flag when
2598 umtxq_busy(&uq->uq_key);
2599 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2600 int oldlen = uq->uq_cur_queue->length;
2603 umtxq_unlock(&uq->uq_key);
2604 suword32(&cv->c_has_waiters, 0);
2605 umtxq_lock(&uq->uq_key);
2608 umtxq_unbusy(&uq->uq_key);
2609 if (error == ERESTART)
2613 umtxq_unlock(&uq->uq_key);
2614 umtx_key_release(&uq->uq_key);
2619 * Signal a userland condition variable.
2622 do_cv_signal(struct thread *td, struct ucond *cv)
2624 struct umtx_key key;
2625 int error, cnt, nwake;
2628 error = fueword32(&cv->c_flags, &flags);
2631 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2635 cnt = umtxq_count(&key);
2636 nwake = umtxq_signal(&key, 1);
2639 error = suword32(&cv->c_has_waiters, 0);
2646 umtx_key_release(&key);
2651 do_cv_broadcast(struct thread *td, struct ucond *cv)
2653 struct umtx_key key;
2657 error = fueword32(&cv->c_flags, &flags);
2660 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2665 umtxq_signal(&key, INT_MAX);
2668 error = suword32(&cv->c_has_waiters, 0);
2672 umtxq_unbusy_unlocked(&key);
2674 umtx_key_release(&key);
2679 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
2680 struct _umtx_time *timeout)
2682 struct abs_timeout timo;
2684 uint32_t flags, wrflags;
2685 int32_t state, oldstate;
2686 int32_t blocked_readers;
2687 int error, error1, rv;
2690 error = fueword32(&rwlock->rw_flags, &flags);
2693 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2697 if (timeout != NULL)
2698 abs_timeout_init2(&timo, timeout);
2700 wrflags = URWLOCK_WRITE_OWNER;
2701 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2702 wrflags |= URWLOCK_WRITE_WAITERS;
2705 rv = fueword32(&rwlock->rw_state, &state);
2707 umtx_key_release(&uq->uq_key);
2711 /* try to lock it */
2712 while (!(state & wrflags)) {
2713 if (__predict_false(URWLOCK_READER_COUNT(state) ==
2714 URWLOCK_MAX_READERS)) {
2715 umtx_key_release(&uq->uq_key);
2718 rv = casueword32(&rwlock->rw_state, state,
2719 &oldstate, state + 1);
2721 umtx_key_release(&uq->uq_key);
2725 MPASS(oldstate == state);
2726 umtx_key_release(&uq->uq_key);
2729 error = thread_check_susp(td, true);
2738 /* grab monitor lock */
2739 umtxq_lock(&uq->uq_key);
2740 umtxq_busy(&uq->uq_key);
2741 umtxq_unlock(&uq->uq_key);
2744 * re-read the state, in case it changed between the try-lock above
2745 * and the check below
2747 rv = fueword32(&rwlock->rw_state, &state);
2751 /* set read contention bit */
2752 while (error == 0 && (state & wrflags) &&
2753 !(state & URWLOCK_READ_WAITERS)) {
2754 rv = casueword32(&rwlock->rw_state, state,
2755 &oldstate, state | URWLOCK_READ_WAITERS);
2761 MPASS(oldstate == state);
2765 error = thread_check_susp(td, false);
2770 umtxq_unbusy_unlocked(&uq->uq_key);
2774 /* state is changed while setting flags, restart */
2775 if (!(state & wrflags)) {
2776 umtxq_unbusy_unlocked(&uq->uq_key);
2777 error = thread_check_susp(td, true);
2785 * Contention bit is set, before sleeping, increase
2786 * read waiter count.
2788 rv = fueword32(&rwlock->rw_blocked_readers,
2791 umtxq_unbusy_unlocked(&uq->uq_key);
2795 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2797 while (state & wrflags) {
2798 umtxq_lock(&uq->uq_key);
2800 umtxq_unbusy(&uq->uq_key);
2802 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2805 umtxq_busy(&uq->uq_key);
2807 umtxq_unlock(&uq->uq_key);
2810 rv = fueword32(&rwlock->rw_state, &state);
2817 /* decrease read waiter count, and may clear read contention bit */
2818 rv = fueword32(&rwlock->rw_blocked_readers,
2821 umtxq_unbusy_unlocked(&uq->uq_key);
2825 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2826 if (blocked_readers == 1) {
2827 rv = fueword32(&rwlock->rw_state, &state);
2829 umtxq_unbusy_unlocked(&uq->uq_key);
2834 rv = casueword32(&rwlock->rw_state, state,
2835 &oldstate, state & ~URWLOCK_READ_WAITERS);
2841 MPASS(oldstate == state);
2845 error1 = thread_check_susp(td, false);
2854 umtxq_unbusy_unlocked(&uq->uq_key);
2858 umtx_key_release(&uq->uq_key);
2859 if (error == ERESTART)
2865 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2867 struct abs_timeout timo;
2870 int32_t state, oldstate;
2871 int32_t blocked_writers;
2872 int32_t blocked_readers;
2873 int error, error1, rv;
2876 error = fueword32(&rwlock->rw_flags, &flags);
2879 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2883 if (timeout != NULL)
2884 abs_timeout_init2(&timo, timeout);
2886 blocked_readers = 0;
2888 rv = fueword32(&rwlock->rw_state, &state);
2890 umtx_key_release(&uq->uq_key);
2893 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
2894 URWLOCK_READER_COUNT(state) == 0) {
2895 rv = casueword32(&rwlock->rw_state, state,
2896 &oldstate, state | URWLOCK_WRITE_OWNER);
2898 umtx_key_release(&uq->uq_key);
2902 MPASS(oldstate == state);
2903 umtx_key_release(&uq->uq_key);
2907 error = thread_check_susp(td, true);
2913 if ((state & (URWLOCK_WRITE_OWNER |
2914 URWLOCK_WRITE_WAITERS)) == 0 &&
2915 blocked_readers != 0) {
2916 umtxq_lock(&uq->uq_key);
2917 umtxq_busy(&uq->uq_key);
2918 umtxq_signal_queue(&uq->uq_key, INT_MAX,
2920 umtxq_unbusy(&uq->uq_key);
2921 umtxq_unlock(&uq->uq_key);
2927 /* grab monitor lock */
2928 umtxq_lock(&uq->uq_key);
2929 umtxq_busy(&uq->uq_key);
2930 umtxq_unlock(&uq->uq_key);
2933 * Re-read the state, in case it changed between the
2934 * try-lock above and the check below.
2936 rv = fueword32(&rwlock->rw_state, &state);
2940 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
2941 URWLOCK_READER_COUNT(state) != 0) &&
2942 (state & URWLOCK_WRITE_WAITERS) == 0) {
2943 rv = casueword32(&rwlock->rw_state, state,
2944 &oldstate, state | URWLOCK_WRITE_WAITERS);
2950 MPASS(oldstate == state);
2954 error = thread_check_susp(td, false);
2959 umtxq_unbusy_unlocked(&uq->uq_key);
2963 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
2964 URWLOCK_READER_COUNT(state) == 0) {
2965 umtxq_unbusy_unlocked(&uq->uq_key);
2966 error = thread_check_susp(td, false);
2972 rv = fueword32(&rwlock->rw_blocked_writers,
2975 umtxq_unbusy_unlocked(&uq->uq_key);
2979 suword32(&rwlock->rw_blocked_writers, blocked_writers + 1);
2981 while ((state & URWLOCK_WRITE_OWNER) ||
2982 URWLOCK_READER_COUNT(state) != 0) {
2983 umtxq_lock(&uq->uq_key);
2984 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2985 umtxq_unbusy(&uq->uq_key);
2987 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
2990 umtxq_busy(&uq->uq_key);
2991 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2992 umtxq_unlock(&uq->uq_key);
2995 rv = fueword32(&rwlock->rw_state, &state);
3002 rv = fueword32(&rwlock->rw_blocked_writers,
3005 umtxq_unbusy_unlocked(&uq->uq_key);
3009 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
3010 if (blocked_writers == 1) {
3011 rv = fueword32(&rwlock->rw_state, &state);
3013 umtxq_unbusy_unlocked(&uq->uq_key);
3018 rv = casueword32(&rwlock->rw_state, state,
3019 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3025 MPASS(oldstate == state);
3029 error1 = thread_check_susp(td, false);
3031 * We are leaving the URWLOCK_WRITE_WAITERS
3032 * behind, but this should not harm the
3041 rv = fueword32(&rwlock->rw_blocked_readers,
3044 umtxq_unbusy_unlocked(&uq->uq_key);
3049 blocked_readers = 0;
3051 umtxq_unbusy_unlocked(&uq->uq_key);
3054 umtx_key_release(&uq->uq_key);
3055 if (error == ERESTART)
3061 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3065 int32_t state, oldstate;
3066 int error, rv, q, count;
3069 error = fueword32(&rwlock->rw_flags, &flags);
3072 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3076 error = fueword32(&rwlock->rw_state, &state);
3081 if (state & URWLOCK_WRITE_OWNER) {
3083 rv = casueword32(&rwlock->rw_state, state,
3084 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3091 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3095 error = thread_check_susp(td, true);
3101 } else if (URWLOCK_READER_COUNT(state) != 0) {
3103 rv = casueword32(&rwlock->rw_state, state,
3104 &oldstate, state - 1);
3111 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3115 error = thread_check_susp(td, true);
3128 if (!(flags & URWLOCK_PREFER_READER)) {
3129 if (state & URWLOCK_WRITE_WAITERS) {
3131 q = UMTX_EXCLUSIVE_QUEUE;
3132 } else if (state & URWLOCK_READ_WAITERS) {
3134 q = UMTX_SHARED_QUEUE;
3137 if (state & URWLOCK_READ_WAITERS) {
3139 q = UMTX_SHARED_QUEUE;
3140 } else if (state & URWLOCK_WRITE_WAITERS) {
3142 q = UMTX_EXCLUSIVE_QUEUE;
3147 umtxq_lock(&uq->uq_key);
3148 umtxq_busy(&uq->uq_key);
3149 umtxq_signal_queue(&uq->uq_key, count, q);
3150 umtxq_unbusy(&uq->uq_key);
3151 umtxq_unlock(&uq->uq_key);
3154 umtx_key_release(&uq->uq_key);
3158 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3160 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3162 struct abs_timeout timo;
3164 uint32_t flags, count, count1;
3168 error = fueword32(&sem->_flags, &flags);
3171 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3175 if (timeout != NULL)
3176 abs_timeout_init2(&timo, timeout);
3179 umtxq_lock(&uq->uq_key);
3180 umtxq_busy(&uq->uq_key);
3182 umtxq_unlock(&uq->uq_key);
3183 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3185 rv1 = fueword32(&sem->_count, &count);
3186 if (rv == -1 || (rv == 0 && (rv1 == -1 || count != 0)) ||
3187 (rv == 1 && count1 == 0)) {
3188 umtxq_lock(&uq->uq_key);
3189 umtxq_unbusy(&uq->uq_key);
3191 umtxq_unlock(&uq->uq_key);
3193 rv = thread_check_susp(td, true);
3201 error = rv == -1 ? EFAULT : 0;
3204 umtxq_lock(&uq->uq_key);
3205 umtxq_unbusy(&uq->uq_key);
3207 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3209 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3213 /* A relative timeout cannot be restarted. */
3214 if (error == ERESTART && timeout != NULL &&
3215 (timeout->_flags & UMTX_ABSTIME) == 0)
3218 umtxq_unlock(&uq->uq_key);
3220 umtx_key_release(&uq->uq_key);
3225 * Signal a userland semaphore.
3228 do_sem_wake(struct thread *td, struct _usem *sem)
3230 struct umtx_key key;
3234 error = fueword32(&sem->_flags, &flags);
3237 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3241 cnt = umtxq_count(&key);
3244 * Check if count is greater than 0, this means the memory is
3245 * still being referenced by user code, so we can safely
3246 * update _has_waiters flag.
3250 error = suword32(&sem->_has_waiters, 0);
3255 umtxq_signal(&key, 1);
3259 umtx_key_release(&key);
3265 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3267 struct abs_timeout timo;
3269 uint32_t count, flags;
3273 flags = fuword32(&sem->_flags);
3274 if (timeout != NULL)
3275 abs_timeout_init2(&timo, timeout);
3278 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3281 umtxq_lock(&uq->uq_key);
3282 umtxq_busy(&uq->uq_key);
3284 umtxq_unlock(&uq->uq_key);
3285 rv = fueword32(&sem->_count, &count);
3287 umtxq_lock(&uq->uq_key);
3288 umtxq_unbusy(&uq->uq_key);
3290 umtxq_unlock(&uq->uq_key);
3291 umtx_key_release(&uq->uq_key);
3295 if (USEM_COUNT(count) != 0) {
3296 umtxq_lock(&uq->uq_key);
3297 umtxq_unbusy(&uq->uq_key);
3299 umtxq_unlock(&uq->uq_key);
3300 umtx_key_release(&uq->uq_key);
3303 if (count == USEM_HAS_WAITERS)
3305 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3308 umtxq_lock(&uq->uq_key);
3309 umtxq_unbusy(&uq->uq_key);
3311 umtxq_unlock(&uq->uq_key);
3312 umtx_key_release(&uq->uq_key);
3315 rv = thread_check_susp(td, true);
3320 umtxq_lock(&uq->uq_key);
3321 umtxq_unbusy(&uq->uq_key);
3323 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3325 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3329 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3330 /* A relative timeout cannot be restarted. */
3331 if (error == ERESTART)
3333 if (error == EINTR) {
3334 abs_timeout_update(&timo);
3335 timespecsub(&timo.end, &timo.cur,
3336 &timeout->_timeout);
3340 umtxq_unlock(&uq->uq_key);
3341 umtx_key_release(&uq->uq_key);
3346 * Signal a userland semaphore.
3349 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3351 struct umtx_key key;
3353 uint32_t count, flags;
3355 rv = fueword32(&sem->_flags, &flags);
3358 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3362 cnt = umtxq_count(&key);
3365 * If this was the last sleeping thread, clear the waiters
3370 rv = fueword32(&sem->_count, &count);
3371 while (rv != -1 && count & USEM_HAS_WAITERS) {
3372 rv = casueword32(&sem->_count, count, &count,
3373 count & ~USEM_HAS_WAITERS);
3375 rv = thread_check_susp(td, true);
3388 umtxq_signal(&key, 1);
3392 umtx_key_release(&key);
3397 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3401 error = copyin(addr, tsp, sizeof(struct timespec));
3403 if (tsp->tv_sec < 0 ||
3404 tsp->tv_nsec >= 1000000000 ||
3412 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3416 if (size <= sizeof(struct timespec)) {
3417 tp->_clockid = CLOCK_REALTIME;
3419 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3421 error = copyin(addr, tp, sizeof(struct _umtx_time));
3424 if (tp->_timeout.tv_sec < 0 ||
3425 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3431 __umtx_op_unimpl(struct thread *td, struct _umtx_op_args *uap)
3434 return (EOPNOTSUPP);
3438 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3440 struct _umtx_time timeout, *tm_p;
3443 if (uap->uaddr2 == NULL)
3446 error = umtx_copyin_umtx_time(
3447 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3452 return (do_wait(td, uap->obj, uap->val, tm_p, 0, 0));
3456 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3458 struct _umtx_time timeout, *tm_p;
3461 if (uap->uaddr2 == NULL)
3464 error = umtx_copyin_umtx_time(
3465 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3470 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3474 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3476 struct _umtx_time *tm_p, timeout;
3479 if (uap->uaddr2 == NULL)
3482 error = umtx_copyin_umtx_time(
3483 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3488 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3492 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3495 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3498 #define BATCH_SIZE 128
3500 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3502 char *uaddrs[BATCH_SIZE], **upp;
3503 int count, error, i, pos, tocopy;
3505 upp = (char **)uap->obj;
3507 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3509 tocopy = MIN(count, BATCH_SIZE);
3510 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3513 for (i = 0; i < tocopy; ++i)
3514 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3521 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3524 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3528 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3530 struct _umtx_time *tm_p, timeout;
3533 /* Allow a null timespec (wait forever). */
3534 if (uap->uaddr2 == NULL)
3537 error = umtx_copyin_umtx_time(
3538 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3543 return (do_lock_umutex(td, uap->obj, tm_p, 0));
3547 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3550 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
3554 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3556 struct _umtx_time *tm_p, timeout;
3559 /* Allow a null timespec (wait forever). */
3560 if (uap->uaddr2 == NULL)
3563 error = umtx_copyin_umtx_time(
3564 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3569 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
3573 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3576 return (do_wake_umutex(td, uap->obj));
3580 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3583 return (do_unlock_umutex(td, uap->obj, false));
3587 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3590 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
3594 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3596 struct timespec *ts, timeout;
3599 /* Allow a null timespec (wait forever). */
3600 if (uap->uaddr2 == NULL)
3603 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3608 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3612 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3615 return (do_cv_signal(td, uap->obj));
3619 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3622 return (do_cv_broadcast(td, uap->obj));
3626 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3628 struct _umtx_time timeout;
3631 /* Allow a null timespec (wait forever). */
3632 if (uap->uaddr2 == NULL) {
3633 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3635 error = umtx_copyin_umtx_time(uap->uaddr2,
3636 (size_t)uap->uaddr1, &timeout);
3639 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3645 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3647 struct _umtx_time timeout;
3650 /* Allow a null timespec (wait forever). */
3651 if (uap->uaddr2 == NULL) {
3652 error = do_rw_wrlock(td, uap->obj, 0);
3654 error = umtx_copyin_umtx_time(uap->uaddr2,
3655 (size_t)uap->uaddr1, &timeout);
3659 error = do_rw_wrlock(td, uap->obj, &timeout);
3665 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3668 return (do_rw_unlock(td, uap->obj));
3671 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3673 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3675 struct _umtx_time *tm_p, timeout;
3678 /* Allow a null timespec (wait forever). */
3679 if (uap->uaddr2 == NULL)
3682 error = umtx_copyin_umtx_time(
3683 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3688 return (do_sem_wait(td, uap->obj, tm_p));
3692 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3695 return (do_sem_wake(td, uap->obj));
3700 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3703 return (do_wake2_umutex(td, uap->obj, uap->val));
3707 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap)
3709 struct _umtx_time *tm_p, timeout;
3713 /* Allow a null timespec (wait forever). */
3714 if (uap->uaddr2 == NULL) {
3718 uasize = (size_t)uap->uaddr1;
3719 error = umtx_copyin_umtx_time(uap->uaddr2, uasize, &timeout);
3724 error = do_sem2_wait(td, uap->obj, tm_p);
3725 if (error == EINTR && uap->uaddr2 != NULL &&
3726 (timeout._flags & UMTX_ABSTIME) == 0 &&
3727 uasize >= sizeof(struct _umtx_time) + sizeof(struct timespec)) {
3728 error = copyout(&timeout._timeout,
3729 (struct _umtx_time *)uap->uaddr2 + 1,
3730 sizeof(struct timespec));
3740 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap)
3743 return (do_sem2_wake(td, uap->obj));
3746 #define USHM_OBJ_UMTX(o) \
3747 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
3749 #define USHMF_REG_LINKED 0x0001
3750 #define USHMF_OBJ_LINKED 0x0002
3751 struct umtx_shm_reg {
3752 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
3753 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
3754 struct umtx_key ushm_key;
3755 struct ucred *ushm_cred;
3756 struct shmfd *ushm_obj;
3761 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
3762 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
3764 static uma_zone_t umtx_shm_reg_zone;
3765 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
3766 static struct mtx umtx_shm_lock;
3767 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
3768 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
3770 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
3773 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
3775 struct umtx_shm_reg_head d;
3776 struct umtx_shm_reg *reg, *reg1;
3779 mtx_lock(&umtx_shm_lock);
3780 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
3781 mtx_unlock(&umtx_shm_lock);
3782 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
3783 TAILQ_REMOVE(&d, reg, ushm_reg_link);
3784 umtx_shm_free_reg(reg);
3788 static struct task umtx_shm_reg_delfree_task =
3789 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
3791 static struct umtx_shm_reg *
3792 umtx_shm_find_reg_locked(const struct umtx_key *key)
3794 struct umtx_shm_reg *reg;
3795 struct umtx_shm_reg_head *reg_head;
3797 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
3798 mtx_assert(&umtx_shm_lock, MA_OWNED);
3799 reg_head = &umtx_shm_registry[key->hash];
3800 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
3801 KASSERT(reg->ushm_key.shared,
3802 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
3803 if (reg->ushm_key.info.shared.object ==
3804 key->info.shared.object &&
3805 reg->ushm_key.info.shared.offset ==
3806 key->info.shared.offset) {
3807 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
3808 KASSERT(reg->ushm_refcnt > 0,
3809 ("reg %p refcnt 0 onlist", reg));
3810 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
3811 ("reg %p not linked", reg));
3819 static struct umtx_shm_reg *
3820 umtx_shm_find_reg(const struct umtx_key *key)
3822 struct umtx_shm_reg *reg;
3824 mtx_lock(&umtx_shm_lock);
3825 reg = umtx_shm_find_reg_locked(key);
3826 mtx_unlock(&umtx_shm_lock);
3831 umtx_shm_free_reg(struct umtx_shm_reg *reg)
3834 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
3835 crfree(reg->ushm_cred);
3836 shm_drop(reg->ushm_obj);
3837 uma_zfree(umtx_shm_reg_zone, reg);
3841 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
3845 mtx_assert(&umtx_shm_lock, MA_OWNED);
3846 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
3848 res = reg->ushm_refcnt == 0;
3850 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
3851 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
3852 reg, ushm_reg_link);
3853 reg->ushm_flags &= ~USHMF_REG_LINKED;
3855 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
3856 LIST_REMOVE(reg, ushm_obj_link);
3857 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
3864 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
3870 object = reg->ushm_obj->shm_object;
3871 VM_OBJECT_WLOCK(object);
3872 object->flags |= OBJ_UMTXDEAD;
3873 VM_OBJECT_WUNLOCK(object);
3875 mtx_lock(&umtx_shm_lock);
3876 dofree = umtx_shm_unref_reg_locked(reg, force);
3877 mtx_unlock(&umtx_shm_lock);
3879 umtx_shm_free_reg(reg);
3883 umtx_shm_object_init(vm_object_t object)
3886 LIST_INIT(USHM_OBJ_UMTX(object));
3890 umtx_shm_object_terminated(vm_object_t object)
3892 struct umtx_shm_reg *reg, *reg1;
3895 if (LIST_EMPTY(USHM_OBJ_UMTX(object)))
3899 mtx_lock(&umtx_shm_lock);
3900 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
3901 if (umtx_shm_unref_reg_locked(reg, true)) {
3902 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
3907 mtx_unlock(&umtx_shm_lock);
3909 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
3913 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
3914 struct umtx_shm_reg **res)
3916 struct umtx_shm_reg *reg, *reg1;
3920 reg = umtx_shm_find_reg(key);
3925 cred = td->td_ucred;
3926 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
3928 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
3929 reg->ushm_refcnt = 1;
3930 bcopy(key, ®->ushm_key, sizeof(*key));
3931 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR);
3932 reg->ushm_cred = crhold(cred);
3933 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
3935 umtx_shm_free_reg(reg);
3938 mtx_lock(&umtx_shm_lock);
3939 reg1 = umtx_shm_find_reg_locked(key);
3941 mtx_unlock(&umtx_shm_lock);
3942 umtx_shm_free_reg(reg);
3947 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
3948 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
3950 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
3951 mtx_unlock(&umtx_shm_lock);
3957 umtx_shm_alive(struct thread *td, void *addr)
3960 vm_map_entry_t entry;
3967 map = &td->td_proc->p_vmspace->vm_map;
3968 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
3969 &object, &pindex, &prot, &wired);
3970 if (res != KERN_SUCCESS)
3975 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
3976 vm_map_lookup_done(map, entry);
3985 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
3986 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
3987 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
3988 for (i = 0; i < nitems(umtx_shm_registry); i++)
3989 TAILQ_INIT(&umtx_shm_registry[i]);
3993 umtx_shm(struct thread *td, void *addr, u_int flags)
3995 struct umtx_key key;
3996 struct umtx_shm_reg *reg;
4000 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
4001 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
4003 if ((flags & UMTX_SHM_ALIVE) != 0)
4004 return (umtx_shm_alive(td, addr));
4005 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
4008 KASSERT(key.shared == 1, ("non-shared key"));
4009 if ((flags & UMTX_SHM_CREAT) != 0) {
4010 error = umtx_shm_create_reg(td, &key, ®);
4012 reg = umtx_shm_find_reg(&key);
4016 umtx_key_release(&key);
4019 KASSERT(reg != NULL, ("no reg"));
4020 if ((flags & UMTX_SHM_DESTROY) != 0) {
4021 umtx_shm_unref_reg(reg, true);
4025 error = mac_posixshm_check_open(td->td_ucred,
4026 reg->ushm_obj, FFLAGS(O_RDWR));
4029 error = shm_access(reg->ushm_obj, td->td_ucred,
4033 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
4035 shm_hold(reg->ushm_obj);
4036 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
4038 td->td_retval[0] = fd;
4042 umtx_shm_unref_reg(reg, false);
4047 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap)
4050 return (umtx_shm(td, uap->uaddr1, uap->val));
4054 umtx_robust_lists(struct thread *td, struct umtx_robust_lists_params *rbp)
4057 td->td_rb_list = rbp->robust_list_offset;
4058 td->td_rbp_list = rbp->robust_priv_list_offset;
4059 td->td_rb_inact = rbp->robust_inact_offset;
4064 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap)
4066 struct umtx_robust_lists_params rb;
4069 if (uap->val > sizeof(rb))
4071 bzero(&rb, sizeof(rb));
4072 error = copyin(uap->uaddr1, &rb, uap->val);
4075 return (umtx_robust_lists(td, &rb));
4078 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
4080 static const _umtx_op_func op_table[] = {
4081 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
4082 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
4083 [UMTX_OP_WAIT] = __umtx_op_wait,
4084 [UMTX_OP_WAKE] = __umtx_op_wake,
4085 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4086 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
4087 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4088 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4089 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
4090 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4091 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4092 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
4093 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
4094 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
4095 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4096 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4097 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4098 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4099 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4100 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4101 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4102 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4104 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4105 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4107 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4108 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4109 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4110 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4111 [UMTX_OP_SHM] = __umtx_op_shm,
4112 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4116 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4119 if ((unsigned)uap->op < nitems(op_table))
4120 return (*op_table[uap->op])(td, uap);
4124 #ifdef COMPAT_FREEBSD32
4131 struct umtx_time32 {
4132 struct timespec32 timeout;
4138 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
4140 struct timespec32 ts32;
4143 error = copyin(addr, &ts32, sizeof(struct timespec32));
4145 if (ts32.tv_sec < 0 ||
4146 ts32.tv_nsec >= 1000000000 ||
4150 tsp->tv_sec = ts32.tv_sec;
4151 tsp->tv_nsec = ts32.tv_nsec;
4158 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
4160 struct umtx_time32 t32;
4163 t32.clockid = CLOCK_REALTIME;
4165 if (size <= sizeof(struct timespec32))
4166 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
4168 error = copyin(addr, &t32, sizeof(struct umtx_time32));
4171 if (t32.timeout.tv_sec < 0 ||
4172 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
4174 tp->_timeout.tv_sec = t32.timeout.tv_sec;
4175 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
4176 tp->_flags = t32.flags;
4177 tp->_clockid = t32.clockid;
4182 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4184 struct _umtx_time *tm_p, timeout;
4187 if (uap->uaddr2 == NULL)
4190 error = umtx_copyin_umtx_time32(uap->uaddr2,
4191 (size_t)uap->uaddr1, &timeout);
4196 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
4200 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4202 struct _umtx_time *tm_p, timeout;
4205 /* Allow a null timespec (wait forever). */
4206 if (uap->uaddr2 == NULL)
4209 error = umtx_copyin_umtx_time32(uap->uaddr2,
4210 (size_t)uap->uaddr1, &timeout);
4215 return (do_lock_umutex(td, uap->obj, tm_p, 0));
4219 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4221 struct _umtx_time *tm_p, timeout;
4224 /* Allow a null timespec (wait forever). */
4225 if (uap->uaddr2 == NULL)
4228 error = umtx_copyin_umtx_time32(uap->uaddr2,
4229 (size_t)uap->uaddr1, &timeout);
4234 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
4238 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4240 struct timespec *ts, timeout;
4243 /* Allow a null timespec (wait forever). */
4244 if (uap->uaddr2 == NULL)
4247 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
4252 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
4256 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4258 struct _umtx_time timeout;
4261 /* Allow a null timespec (wait forever). */
4262 if (uap->uaddr2 == NULL) {
4263 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4265 error = umtx_copyin_umtx_time32(uap->uaddr2,
4266 (size_t)uap->uaddr1, &timeout);
4269 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4275 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4277 struct _umtx_time timeout;
4280 /* Allow a null timespec (wait forever). */
4281 if (uap->uaddr2 == NULL) {
4282 error = do_rw_wrlock(td, uap->obj, 0);
4284 error = umtx_copyin_umtx_time32(uap->uaddr2,
4285 (size_t)uap->uaddr1, &timeout);
4288 error = do_rw_wrlock(td, uap->obj, &timeout);
4294 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
4296 struct _umtx_time *tm_p, timeout;
4299 if (uap->uaddr2 == NULL)
4302 error = umtx_copyin_umtx_time32(
4303 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
4308 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
4311 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4313 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4315 struct _umtx_time *tm_p, timeout;
4318 /* Allow a null timespec (wait forever). */
4319 if (uap->uaddr2 == NULL)
4322 error = umtx_copyin_umtx_time32(uap->uaddr2,
4323 (size_t)uap->uaddr1, &timeout);
4328 return (do_sem_wait(td, uap->obj, tm_p));
4333 __umtx_op_sem2_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4335 struct _umtx_time *tm_p, timeout;
4339 /* Allow a null timespec (wait forever). */
4340 if (uap->uaddr2 == NULL) {
4344 uasize = (size_t)uap->uaddr1;
4345 error = umtx_copyin_umtx_time32(uap->uaddr2, uasize, &timeout);
4350 error = do_sem2_wait(td, uap->obj, tm_p);
4351 if (error == EINTR && uap->uaddr2 != NULL &&
4352 (timeout._flags & UMTX_ABSTIME) == 0 &&
4353 uasize >= sizeof(struct umtx_time32) + sizeof(struct timespec32)) {
4354 struct timespec32 remain32 = {
4355 .tv_sec = timeout._timeout.tv_sec,
4356 .tv_nsec = timeout._timeout.tv_nsec
4358 error = copyout(&remain32,
4359 (struct umtx_time32 *)uap->uaddr2 + 1,
4360 sizeof(struct timespec32));
4370 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
4372 uint32_t uaddrs[BATCH_SIZE], **upp;
4373 int count, error, i, pos, tocopy;
4375 upp = (uint32_t **)uap->obj;
4377 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
4379 tocopy = MIN(count, BATCH_SIZE);
4380 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
4383 for (i = 0; i < tocopy; ++i)
4384 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
4391 struct umtx_robust_lists_params_compat32 {
4392 uint32_t robust_list_offset;
4393 uint32_t robust_priv_list_offset;
4394 uint32_t robust_inact_offset;
4398 __umtx_op_robust_lists_compat32(struct thread *td, struct _umtx_op_args *uap)
4400 struct umtx_robust_lists_params rb;
4401 struct umtx_robust_lists_params_compat32 rb32;
4404 if (uap->val > sizeof(rb32))
4406 bzero(&rb, sizeof(rb));
4407 bzero(&rb32, sizeof(rb32));
4408 error = copyin(uap->uaddr1, &rb32, uap->val);
4411 rb.robust_list_offset = rb32.robust_list_offset;
4412 rb.robust_priv_list_offset = rb32.robust_priv_list_offset;
4413 rb.robust_inact_offset = rb32.robust_inact_offset;
4414 return (umtx_robust_lists(td, &rb));
4417 static const _umtx_op_func op_table_compat32[] = {
4418 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
4419 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
4420 [UMTX_OP_WAIT] = __umtx_op_wait_compat32,
4421 [UMTX_OP_WAKE] = __umtx_op_wake,
4422 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4423 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex_compat32,
4424 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4425 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4426 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait_compat32,
4427 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4428 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4429 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_compat32,
4430 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock_compat32,
4431 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock_compat32,
4432 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4433 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private_compat32,
4434 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4435 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex_compat32,
4436 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4437 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4438 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait_compat32,
4439 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4441 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4442 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4444 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private32,
4445 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4446 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait_compat32,
4447 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4448 [UMTX_OP_SHM] = __umtx_op_shm,
4449 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists_compat32,
4453 freebsd32__umtx_op(struct thread *td, struct freebsd32__umtx_op_args *uap)
4456 if ((unsigned)uap->op < nitems(op_table_compat32)) {
4457 return (*op_table_compat32[uap->op])(td,
4458 (struct _umtx_op_args *)uap);
4465 umtx_thread_init(struct thread *td)
4468 td->td_umtxq = umtxq_alloc();
4469 td->td_umtxq->uq_thread = td;
4473 umtx_thread_fini(struct thread *td)
4476 umtxq_free(td->td_umtxq);
4480 * It will be called when new thread is created, e.g fork().
4483 umtx_thread_alloc(struct thread *td)
4488 uq->uq_inherited_pri = PRI_MAX;
4490 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4491 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4492 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4493 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4499 * Clear robust lists for all process' threads, not delaying the
4500 * cleanup to thread_exit hook, since the relevant address space is
4501 * destroyed right now.
4504 umtx_exec_hook(void *arg __unused, struct proc *p,
4505 struct image_params *imgp __unused)
4509 KASSERT(p == curproc, ("need curproc"));
4510 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4511 (p->p_flag & P_STOPPED_SINGLE) != 0,
4512 ("curproc must be single-threaded"));
4514 * There is no need to lock the list as only this thread can be
4517 FOREACH_THREAD_IN_PROC(p, td) {
4518 KASSERT(td == curthread ||
4519 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4520 ("running thread %p %p", p, td));
4521 umtx_thread_cleanup(td);
4522 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4527 * thread_exit() hook.
4530 umtx_thread_exit(struct thread *td)
4533 umtx_thread_cleanup(td);
4537 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res)
4540 #ifdef COMPAT_FREEBSD32
4545 #ifdef COMPAT_FREEBSD32
4546 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4547 error = fueword32((void *)ptr, &res32);
4553 error = fueword((void *)ptr, &res1);
4563 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list)
4565 #ifdef COMPAT_FREEBSD32
4566 struct umutex32 m32;
4568 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4569 memcpy(&m32, m, sizeof(m32));
4570 *rb_list = m32.m_rb_lnk;
4573 *rb_list = m->m_rb_lnk;
4577 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact)
4582 KASSERT(td->td_proc == curproc, ("need current vmspace"));
4583 error = copyin((void *)rbp, &m, sizeof(m));
4586 if (rb_list != NULL)
4587 umtx_read_rb_list(td, &m, rb_list);
4588 if ((m.m_flags & UMUTEX_ROBUST) == 0)
4590 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
4591 /* inact is cleared after unlock, allow the inconsistency */
4592 return (inact ? 0 : EINVAL);
4593 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
4597 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
4606 error = umtx_read_uptr(td, rb_list, &rbp);
4607 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
4608 if (rbp == *rb_inact) {
4613 error = umtx_handle_rb(td, rbp, &rbp, inact);
4615 if (i == umtx_max_rb && umtx_verbose_rb) {
4616 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
4617 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
4619 if (error != 0 && umtx_verbose_rb) {
4620 uprintf("comm %s pid %d: handling %srb error %d\n",
4621 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
4626 * Clean up umtx data.
4629 umtx_thread_cleanup(struct thread *td)
4636 * Disown pi mutexes.
4640 if (uq->uq_inherited_pri != PRI_MAX ||
4641 !TAILQ_EMPTY(&uq->uq_pi_contested)) {
4642 mtx_lock(&umtx_lock);
4643 uq->uq_inherited_pri = PRI_MAX;
4644 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
4645 pi->pi_owner = NULL;
4646 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
4648 mtx_unlock(&umtx_lock);
4650 sched_lend_user_prio_cond(td, PRI_MAX);
4653 if (td->td_rb_inact == 0 && td->td_rb_list == 0 && td->td_rbp_list == 0)
4657 * Handle terminated robust mutexes. Must be done after
4658 * robust pi disown, otherwise unlock could see unowned
4661 rb_inact = td->td_rb_inact;
4663 (void)umtx_read_uptr(td, rb_inact, &rb_inact);
4664 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "");
4665 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ");
4667 (void)umtx_handle_rb(td, rb_inact, NULL, true);