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);
720 * Wake up specified thread.
723 umtxq_signal_thread(struct umtx_q *uq)
726 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
732 tstohz(const struct timespec *tsp)
736 TIMESPEC_TO_TIMEVAL(&tv, tsp);
741 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
742 const struct timespec *timeout)
745 timo->clockid = clockid;
747 timo->is_abs_real = false;
748 abs_timeout_update(timo);
749 timespecadd(&timo->cur, timeout, &timo->end);
751 timo->end = *timeout;
752 timo->is_abs_real = clockid == CLOCK_REALTIME ||
753 clockid == CLOCK_REALTIME_FAST ||
754 clockid == CLOCK_REALTIME_PRECISE;
756 * If is_abs_real, umtxq_sleep will read the clock
757 * after setting td_rtcgen; otherwise, read it here.
759 if (!timo->is_abs_real) {
760 abs_timeout_update(timo);
766 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
769 abs_timeout_init(timo, umtxtime->_clockid,
770 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
774 abs_timeout_update(struct abs_timeout *timo)
777 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
781 abs_timeout_gethz(struct abs_timeout *timo)
785 if (timespeccmp(&timo->end, &timo->cur, <=))
787 timespecsub(&timo->end, &timo->cur, &tts);
788 return (tstohz(&tts));
792 umtx_unlock_val(uint32_t flags, bool rb)
796 return (UMUTEX_RB_OWNERDEAD);
797 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
798 return (UMUTEX_RB_NOTRECOV);
800 return (UMUTEX_UNOWNED);
805 * Put thread into sleep state, before sleeping, check if
806 * thread was removed from umtx queue.
809 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
811 struct umtxq_chain *uc;
814 if (abstime != NULL && abstime->is_abs_real) {
815 curthread->td_rtcgen = atomic_load_acq_int(&rtc_generation);
816 abs_timeout_update(abstime);
819 uc = umtxq_getchain(&uq->uq_key);
820 UMTXQ_LOCKED_ASSERT(uc);
822 if (!(uq->uq_flags & UQF_UMTXQ)) {
826 if (abstime != NULL) {
827 timo = abs_timeout_gethz(abstime);
834 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
835 if (error == EINTR || error == ERESTART) {
836 umtxq_lock(&uq->uq_key);
839 if (abstime != NULL) {
840 if (abstime->is_abs_real)
841 curthread->td_rtcgen =
842 atomic_load_acq_int(&rtc_generation);
843 abs_timeout_update(abstime);
845 umtxq_lock(&uq->uq_key);
848 curthread->td_rtcgen = 0;
853 * Convert userspace address into unique logical address.
856 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
858 struct thread *td = curthread;
860 vm_map_entry_t entry;
866 if (share == THREAD_SHARE) {
868 key->info.private.vs = td->td_proc->p_vmspace;
869 key->info.private.addr = (uintptr_t)addr;
871 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
872 map = &td->td_proc->p_vmspace->vm_map;
873 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
874 &entry, &key->info.shared.object, &pindex, &prot,
875 &wired) != KERN_SUCCESS) {
879 if ((share == PROCESS_SHARE) ||
880 (share == AUTO_SHARE &&
881 VM_INHERIT_SHARE == entry->inheritance)) {
883 key->info.shared.offset = (vm_offset_t)addr -
884 entry->start + entry->offset;
885 vm_object_reference(key->info.shared.object);
888 key->info.private.vs = td->td_proc->p_vmspace;
889 key->info.private.addr = (uintptr_t)addr;
891 vm_map_lookup_done(map, entry);
902 umtx_key_release(struct umtx_key *key)
905 vm_object_deallocate(key->info.shared.object);
909 * Fetch and compare value, sleep on the address if value is not changed.
912 do_wait(struct thread *td, void *addr, u_long id,
913 struct _umtx_time *timeout, int compat32, int is_private)
915 struct abs_timeout timo;
922 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
923 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
927 abs_timeout_init2(&timo, timeout);
929 umtxq_lock(&uq->uq_key);
931 umtxq_unlock(&uq->uq_key);
933 error = fueword(addr, &tmp);
937 error = fueword32(addr, &tmp32);
943 umtxq_lock(&uq->uq_key);
946 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
948 if ((uq->uq_flags & UQF_UMTXQ) == 0)
952 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
955 umtxq_unlock(&uq->uq_key);
956 umtx_key_release(&uq->uq_key);
957 if (error == ERESTART)
963 * Wake up threads sleeping on the specified address.
966 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
971 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
972 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
975 umtxq_signal(&key, n_wake);
977 umtx_key_release(&key);
982 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
985 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
986 struct _umtx_time *timeout, int mode)
988 struct abs_timeout timo;
990 uint32_t owner, old, id;
997 abs_timeout_init2(&timo, timeout);
1000 * Care must be exercised when dealing with umtx structure. It
1001 * can fault on any access.
1004 rv = fueword32(&m->m_owner, &owner);
1007 if (mode == _UMUTEX_WAIT) {
1008 if (owner == UMUTEX_UNOWNED ||
1009 owner == UMUTEX_CONTESTED ||
1010 owner == UMUTEX_RB_OWNERDEAD ||
1011 owner == UMUTEX_RB_NOTRECOV)
1015 * Robust mutex terminated. Kernel duty is to
1016 * return EOWNERDEAD to the userspace. The
1017 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1018 * by the common userspace code.
1020 if (owner == UMUTEX_RB_OWNERDEAD) {
1021 rv = casueword32(&m->m_owner,
1022 UMUTEX_RB_OWNERDEAD, &owner,
1023 id | UMUTEX_CONTESTED);
1027 MPASS(owner == UMUTEX_RB_OWNERDEAD);
1028 return (EOWNERDEAD); /* success */
1031 rv = thread_check_susp(td, false);
1036 if (owner == UMUTEX_RB_NOTRECOV)
1037 return (ENOTRECOVERABLE);
1040 * Try the uncontested case. This should be
1043 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1045 /* The address was invalid. */
1049 /* The acquire succeeded. */
1051 MPASS(owner == UMUTEX_UNOWNED);
1056 * If no one owns it but it is contested try
1060 if (owner == UMUTEX_CONTESTED) {
1061 rv = casueword32(&m->m_owner,
1062 UMUTEX_CONTESTED, &owner,
1063 id | UMUTEX_CONTESTED);
1064 /* The address was invalid. */
1068 MPASS(owner == UMUTEX_CONTESTED);
1072 rv = thread_check_susp(td, false);
1078 * If this failed the lock has
1084 /* rv == 1 but not contested, likely store failure */
1085 rv = thread_check_susp(td, false);
1090 if (mode == _UMUTEX_TRY)
1094 * If we caught a signal, we have retried and now
1100 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1101 GET_SHARE(flags), &uq->uq_key)) != 0)
1104 umtxq_lock(&uq->uq_key);
1105 umtxq_busy(&uq->uq_key);
1107 umtxq_unlock(&uq->uq_key);
1110 * Set the contested bit so that a release in user space
1111 * knows to use the system call for unlock. If this fails
1112 * either some one else has acquired the lock or it has been
1115 rv = casueword32(&m->m_owner, owner, &old,
1116 owner | UMUTEX_CONTESTED);
1118 /* The address was invalid or casueword failed to store. */
1119 if (rv == -1 || rv == 1) {
1120 umtxq_lock(&uq->uq_key);
1122 umtxq_unbusy(&uq->uq_key);
1123 umtxq_unlock(&uq->uq_key);
1124 umtx_key_release(&uq->uq_key);
1128 rv = thread_check_susp(td, false);
1136 * We set the contested bit, sleep. Otherwise the lock changed
1137 * and we need to retry or we lost a race to the thread
1138 * unlocking the umtx.
1140 umtxq_lock(&uq->uq_key);
1141 umtxq_unbusy(&uq->uq_key);
1142 MPASS(old == owner);
1143 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1146 umtxq_unlock(&uq->uq_key);
1147 umtx_key_release(&uq->uq_key);
1150 error = thread_check_susp(td, false);
1157 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1160 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1162 struct umtx_key key;
1163 uint32_t owner, old, id, newlock;
1170 * Make sure we own this mtx.
1172 error = fueword32(&m->m_owner, &owner);
1176 if ((owner & ~UMUTEX_CONTESTED) != id)
1179 newlock = umtx_unlock_val(flags, rb);
1180 if ((owner & UMUTEX_CONTESTED) == 0) {
1181 error = casueword32(&m->m_owner, owner, &old, newlock);
1185 error = thread_check_susp(td, false);
1190 MPASS(old == owner);
1194 /* We should only ever be in here for contested locks */
1195 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1201 count = umtxq_count(&key);
1205 * When unlocking the umtx, it must be marked as unowned if
1206 * there is zero or one thread only waiting for it.
1207 * Otherwise, it must be marked as contested.
1210 newlock |= UMUTEX_CONTESTED;
1211 error = casueword32(&m->m_owner, owner, &old, newlock);
1213 umtxq_signal(&key, 1);
1216 umtx_key_release(&key);
1222 error = thread_check_susp(td, false);
1231 * Check if the mutex is available and wake up a waiter,
1232 * only for simple mutex.
1235 do_wake_umutex(struct thread *td, struct umutex *m)
1237 struct umtx_key key;
1244 error = fueword32(&m->m_owner, &owner);
1248 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1249 owner != UMUTEX_RB_NOTRECOV)
1252 error = fueword32(&m->m_flags, &flags);
1256 /* We should only ever be in here for contested locks */
1257 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1263 count = umtxq_count(&key);
1266 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1267 owner != UMUTEX_RB_NOTRECOV) {
1268 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1272 } else if (error == 1) {
1276 umtx_key_release(&key);
1277 error = thread_check_susp(td, false);
1285 if (error == 0 && count != 0) {
1286 MPASS((owner & ~UMUTEX_CONTESTED) == 0 ||
1287 owner == UMUTEX_RB_OWNERDEAD ||
1288 owner == UMUTEX_RB_NOTRECOV);
1289 umtxq_signal(&key, 1);
1293 umtx_key_release(&key);
1298 * Check if the mutex has waiters and tries to fix contention bit.
1301 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1303 struct umtx_key key;
1304 uint32_t owner, old;
1309 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1313 type = TYPE_NORMAL_UMUTEX;
1315 case UMUTEX_PRIO_INHERIT:
1316 type = TYPE_PI_UMUTEX;
1318 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1319 type = TYPE_PI_ROBUST_UMUTEX;
1321 case UMUTEX_PRIO_PROTECT:
1322 type = TYPE_PP_UMUTEX;
1324 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1325 type = TYPE_PP_ROBUST_UMUTEX;
1330 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1336 count = umtxq_count(&key);
1339 error = fueword32(&m->m_owner, &owner);
1344 * Only repair contention bit if there is a waiter, this means
1345 * the mutex is still being referenced by userland code,
1346 * otherwise don't update any memory.
1348 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0 &&
1349 (count > 1 || (count == 1 && (owner & ~UMUTEX_CONTESTED) != 0))) {
1350 error = casueword32(&m->m_owner, owner, &old,
1351 owner | UMUTEX_CONTESTED);
1357 MPASS(old == owner);
1361 error = thread_check_susp(td, false);
1365 if (error == EFAULT) {
1366 umtxq_signal(&key, INT_MAX);
1367 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1368 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1369 umtxq_signal(&key, 1);
1372 umtx_key_release(&key);
1376 static inline struct umtx_pi *
1377 umtx_pi_alloc(int flags)
1381 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1382 TAILQ_INIT(&pi->pi_blocked);
1383 atomic_add_int(&umtx_pi_allocated, 1);
1388 umtx_pi_free(struct umtx_pi *pi)
1390 uma_zfree(umtx_pi_zone, pi);
1391 atomic_add_int(&umtx_pi_allocated, -1);
1395 * Adjust the thread's position on a pi_state after its priority has been
1399 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1401 struct umtx_q *uq, *uq1, *uq2;
1404 mtx_assert(&umtx_lock, MA_OWNED);
1411 * Check if the thread needs to be moved on the blocked chain.
1412 * It needs to be moved if either its priority is lower than
1413 * the previous thread or higher than the next thread.
1415 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1416 uq2 = TAILQ_NEXT(uq, uq_lockq);
1417 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1418 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1420 * Remove thread from blocked chain and determine where
1421 * it should be moved to.
1423 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1424 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1425 td1 = uq1->uq_thread;
1426 MPASS(td1->td_proc->p_magic == P_MAGIC);
1427 if (UPRI(td1) > UPRI(td))
1432 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1434 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1439 static struct umtx_pi *
1440 umtx_pi_next(struct umtx_pi *pi)
1442 struct umtx_q *uq_owner;
1444 if (pi->pi_owner == NULL)
1446 uq_owner = pi->pi_owner->td_umtxq;
1447 if (uq_owner == NULL)
1449 return (uq_owner->uq_pi_blocked);
1453 * Floyd's Cycle-Finding Algorithm.
1456 umtx_pi_check_loop(struct umtx_pi *pi)
1458 struct umtx_pi *pi1; /* fast iterator */
1460 mtx_assert(&umtx_lock, MA_OWNED);
1465 pi = umtx_pi_next(pi);
1468 pi1 = umtx_pi_next(pi1);
1471 pi1 = umtx_pi_next(pi1);
1481 * Propagate priority when a thread is blocked on POSIX
1485 umtx_propagate_priority(struct thread *td)
1491 mtx_assert(&umtx_lock, MA_OWNED);
1494 pi = uq->uq_pi_blocked;
1497 if (umtx_pi_check_loop(pi))
1502 if (td == NULL || td == curthread)
1505 MPASS(td->td_proc != NULL);
1506 MPASS(td->td_proc->p_magic == P_MAGIC);
1509 if (td->td_lend_user_pri > pri)
1510 sched_lend_user_prio(td, pri);
1518 * Pick up the lock that td is blocked on.
1521 pi = uq->uq_pi_blocked;
1524 /* Resort td on the list if needed. */
1525 umtx_pi_adjust_thread(pi, td);
1530 * Unpropagate priority for a PI mutex when a thread blocked on
1531 * it is interrupted by signal or resumed by others.
1534 umtx_repropagate_priority(struct umtx_pi *pi)
1536 struct umtx_q *uq, *uq_owner;
1537 struct umtx_pi *pi2;
1540 mtx_assert(&umtx_lock, MA_OWNED);
1542 if (umtx_pi_check_loop(pi))
1544 while (pi != NULL && pi->pi_owner != NULL) {
1546 uq_owner = pi->pi_owner->td_umtxq;
1548 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1549 uq = TAILQ_FIRST(&pi2->pi_blocked);
1551 if (pri > UPRI(uq->uq_thread))
1552 pri = UPRI(uq->uq_thread);
1556 if (pri > uq_owner->uq_inherited_pri)
1557 pri = uq_owner->uq_inherited_pri;
1558 thread_lock(pi->pi_owner);
1559 sched_lend_user_prio(pi->pi_owner, pri);
1560 thread_unlock(pi->pi_owner);
1561 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1562 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1567 * Insert a PI mutex into owned list.
1570 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1572 struct umtx_q *uq_owner;
1574 uq_owner = owner->td_umtxq;
1575 mtx_assert(&umtx_lock, MA_OWNED);
1576 MPASS(pi->pi_owner == NULL);
1577 pi->pi_owner = owner;
1578 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1583 * Disown a PI mutex, and remove it from the owned list.
1586 umtx_pi_disown(struct umtx_pi *pi)
1589 mtx_assert(&umtx_lock, MA_OWNED);
1590 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1591 pi->pi_owner = NULL;
1595 * Claim ownership of a PI mutex.
1598 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1603 mtx_lock(&umtx_lock);
1604 if (pi->pi_owner == owner) {
1605 mtx_unlock(&umtx_lock);
1609 if (pi->pi_owner != NULL) {
1611 * userland may have already messed the mutex, sigh.
1613 mtx_unlock(&umtx_lock);
1616 umtx_pi_setowner(pi, owner);
1617 uq = TAILQ_FIRST(&pi->pi_blocked);
1619 pri = UPRI(uq->uq_thread);
1621 if (pri < UPRI(owner))
1622 sched_lend_user_prio(owner, pri);
1623 thread_unlock(owner);
1625 mtx_unlock(&umtx_lock);
1630 * Adjust a thread's order position in its blocked PI mutex,
1631 * this may result new priority propagating process.
1634 umtx_pi_adjust(struct thread *td, u_char oldpri)
1640 mtx_lock(&umtx_lock);
1642 * Pick up the lock that td is blocked on.
1644 pi = uq->uq_pi_blocked;
1646 umtx_pi_adjust_thread(pi, td);
1647 umtx_repropagate_priority(pi);
1649 mtx_unlock(&umtx_lock);
1653 * Sleep on a PI mutex.
1656 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
1657 const char *wmesg, struct abs_timeout *timo, bool shared)
1659 struct thread *td, *td1;
1663 struct umtxq_chain *uc;
1665 uc = umtxq_getchain(&pi->pi_key);
1669 KASSERT(td == curthread, ("inconsistent uq_thread"));
1670 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
1671 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1673 mtx_lock(&umtx_lock);
1674 if (pi->pi_owner == NULL) {
1675 mtx_unlock(&umtx_lock);
1676 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
1677 mtx_lock(&umtx_lock);
1679 if (pi->pi_owner == NULL)
1680 umtx_pi_setowner(pi, td1);
1681 PROC_UNLOCK(td1->td_proc);
1685 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1686 pri = UPRI(uq1->uq_thread);
1692 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1694 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1696 uq->uq_pi_blocked = pi;
1698 td->td_flags |= TDF_UPIBLOCKED;
1700 umtx_propagate_priority(td);
1701 mtx_unlock(&umtx_lock);
1702 umtxq_unbusy(&uq->uq_key);
1704 error = umtxq_sleep(uq, wmesg, timo);
1707 mtx_lock(&umtx_lock);
1708 uq->uq_pi_blocked = NULL;
1710 td->td_flags &= ~TDF_UPIBLOCKED;
1712 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1713 umtx_repropagate_priority(pi);
1714 mtx_unlock(&umtx_lock);
1715 umtxq_unlock(&uq->uq_key);
1721 * Add reference count for a PI mutex.
1724 umtx_pi_ref(struct umtx_pi *pi)
1727 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
1732 * Decrease reference count for a PI mutex, if the counter
1733 * is decreased to zero, its memory space is freed.
1736 umtx_pi_unref(struct umtx_pi *pi)
1738 struct umtxq_chain *uc;
1740 uc = umtxq_getchain(&pi->pi_key);
1741 UMTXQ_LOCKED_ASSERT(uc);
1742 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1743 if (--pi->pi_refcount == 0) {
1744 mtx_lock(&umtx_lock);
1745 if (pi->pi_owner != NULL)
1747 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1748 ("blocked queue not empty"));
1749 mtx_unlock(&umtx_lock);
1750 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1756 * Find a PI mutex in hash table.
1758 static struct umtx_pi *
1759 umtx_pi_lookup(struct umtx_key *key)
1761 struct umtxq_chain *uc;
1764 uc = umtxq_getchain(key);
1765 UMTXQ_LOCKED_ASSERT(uc);
1767 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1768 if (umtx_key_match(&pi->pi_key, key)) {
1776 * Insert a PI mutex into hash table.
1779 umtx_pi_insert(struct umtx_pi *pi)
1781 struct umtxq_chain *uc;
1783 uc = umtxq_getchain(&pi->pi_key);
1784 UMTXQ_LOCKED_ASSERT(uc);
1785 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1792 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1793 struct _umtx_time *timeout, int try)
1795 struct abs_timeout timo;
1797 struct umtx_pi *pi, *new_pi;
1798 uint32_t id, old_owner, owner, old;
1804 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
1805 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
1809 if (timeout != NULL)
1810 abs_timeout_init2(&timo, timeout);
1812 umtxq_lock(&uq->uq_key);
1813 pi = umtx_pi_lookup(&uq->uq_key);
1815 new_pi = umtx_pi_alloc(M_NOWAIT);
1816 if (new_pi == NULL) {
1817 umtxq_unlock(&uq->uq_key);
1818 new_pi = umtx_pi_alloc(M_WAITOK);
1819 umtxq_lock(&uq->uq_key);
1820 pi = umtx_pi_lookup(&uq->uq_key);
1822 umtx_pi_free(new_pi);
1826 if (new_pi != NULL) {
1827 new_pi->pi_key = uq->uq_key;
1828 umtx_pi_insert(new_pi);
1833 umtxq_unlock(&uq->uq_key);
1836 * Care must be exercised when dealing with umtx structure. It
1837 * can fault on any access.
1841 * Try the uncontested case. This should be done in userland.
1843 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
1844 /* The address was invalid. */
1849 /* The acquire succeeded. */
1851 MPASS(owner == UMUTEX_UNOWNED);
1856 if (owner == UMUTEX_RB_NOTRECOV) {
1857 error = ENOTRECOVERABLE;
1862 * Avoid overwriting a possible error from sleep due
1863 * to the pending signal with suspension check result.
1866 error = thread_check_susp(td, true);
1871 /* If no one owns it but it is contested try to acquire it. */
1872 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
1874 rv = casueword32(&m->m_owner, owner, &owner,
1875 id | UMUTEX_CONTESTED);
1876 /* The address was invalid. */
1883 error = thread_check_susp(td, true);
1889 * If this failed the lock could
1896 MPASS(owner == old_owner);
1897 umtxq_lock(&uq->uq_key);
1898 umtxq_busy(&uq->uq_key);
1899 error = umtx_pi_claim(pi, td);
1900 umtxq_unbusy(&uq->uq_key);
1901 umtxq_unlock(&uq->uq_key);
1904 * Since we're going to return an
1905 * error, restore the m_owner to its
1906 * previous, unowned state to avoid
1907 * compounding the problem.
1909 (void)casuword32(&m->m_owner,
1910 id | UMUTEX_CONTESTED, old_owner);
1912 if (error == 0 && old_owner == UMUTEX_RB_OWNERDEAD)
1917 if ((owner & ~UMUTEX_CONTESTED) == id) {
1928 * If we caught a signal, we have retried and now
1934 umtxq_lock(&uq->uq_key);
1935 umtxq_busy(&uq->uq_key);
1936 umtxq_unlock(&uq->uq_key);
1939 * Set the contested bit so that a release in user space
1940 * knows to use the system call for unlock. If this fails
1941 * either some one else has acquired the lock or it has been
1944 rv = casueword32(&m->m_owner, owner, &old, owner |
1947 /* The address was invalid. */
1949 umtxq_unbusy_unlocked(&uq->uq_key);
1954 umtxq_unbusy_unlocked(&uq->uq_key);
1955 error = thread_check_susp(td, true);
1960 * The lock changed and we need to retry or we
1961 * lost a race to the thread unlocking the
1962 * umtx. Note that the UMUTEX_RB_OWNERDEAD
1963 * value for owner is impossible there.
1968 umtxq_lock(&uq->uq_key);
1970 /* We set the contested bit, sleep. */
1971 MPASS(old == owner);
1972 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
1973 "umtxpi", timeout == NULL ? NULL : &timo,
1974 (flags & USYNC_PROCESS_SHARED) != 0);
1978 error = thread_check_susp(td, false);
1983 umtxq_lock(&uq->uq_key);
1985 umtxq_unlock(&uq->uq_key);
1987 umtx_key_release(&uq->uq_key);
1992 * Unlock a PI mutex.
1995 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1997 struct umtx_key key;
1998 struct umtx_q *uq_first, *uq_first2, *uq_me;
1999 struct umtx_pi *pi, *pi2;
2000 uint32_t id, new_owner, old, owner;
2001 int count, error, pri;
2007 * Make sure we own this mtx.
2009 error = fueword32(&m->m_owner, &owner);
2013 if ((owner & ~UMUTEX_CONTESTED) != id)
2016 new_owner = umtx_unlock_val(flags, rb);
2018 /* This should be done in userland */
2019 if ((owner & UMUTEX_CONTESTED) == 0) {
2020 error = casueword32(&m->m_owner, owner, &old, new_owner);
2024 error = thread_check_susp(td, true);
2034 /* We should only ever be in here for contested locks */
2035 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2036 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2042 count = umtxq_count_pi(&key, &uq_first);
2043 if (uq_first != NULL) {
2044 mtx_lock(&umtx_lock);
2045 pi = uq_first->uq_pi_blocked;
2046 KASSERT(pi != NULL, ("pi == NULL?"));
2047 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2048 mtx_unlock(&umtx_lock);
2051 umtx_key_release(&key);
2052 /* userland messed the mutex */
2055 uq_me = td->td_umtxq;
2056 if (pi->pi_owner == td)
2058 /* get highest priority thread which is still sleeping. */
2059 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2060 while (uq_first != NULL &&
2061 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2062 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2065 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2066 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2067 if (uq_first2 != NULL) {
2068 if (pri > UPRI(uq_first2->uq_thread))
2069 pri = UPRI(uq_first2->uq_thread);
2073 sched_lend_user_prio(td, pri);
2075 mtx_unlock(&umtx_lock);
2077 umtxq_signal_thread(uq_first);
2079 pi = umtx_pi_lookup(&key);
2081 * A umtx_pi can exist if a signal or timeout removed the
2082 * last waiter from the umtxq, but there is still
2083 * a thread in do_lock_pi() holding the umtx_pi.
2087 * The umtx_pi can be unowned, such as when a thread
2088 * has just entered do_lock_pi(), allocated the
2089 * umtx_pi, and unlocked the umtxq.
2090 * If the current thread owns it, it must disown it.
2092 mtx_lock(&umtx_lock);
2093 if (pi->pi_owner == td)
2095 mtx_unlock(&umtx_lock);
2101 * When unlocking the umtx, it must be marked as unowned if
2102 * there is zero or one thread only waiting for it.
2103 * Otherwise, it must be marked as contested.
2107 new_owner |= UMUTEX_CONTESTED;
2109 error = casueword32(&m->m_owner, owner, &old, new_owner);
2111 error = thread_check_susp(td, false);
2115 umtxq_unbusy_unlocked(&key);
2116 umtx_key_release(&key);
2119 if (error == 0 && old != owner)
2128 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2129 struct _umtx_time *timeout, int try)
2131 struct abs_timeout timo;
2132 struct umtx_q *uq, *uq2;
2136 int error, pri, old_inherited_pri, su, rv;
2140 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2141 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2145 if (timeout != NULL)
2146 abs_timeout_init2(&timo, timeout);
2148 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2150 old_inherited_pri = uq->uq_inherited_pri;
2151 umtxq_lock(&uq->uq_key);
2152 umtxq_busy(&uq->uq_key);
2153 umtxq_unlock(&uq->uq_key);
2155 rv = fueword32(&m->m_ceilings[0], &ceiling);
2160 ceiling = RTP_PRIO_MAX - ceiling;
2161 if (ceiling > RTP_PRIO_MAX) {
2166 mtx_lock(&umtx_lock);
2167 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2168 mtx_unlock(&umtx_lock);
2172 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2173 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2175 if (uq->uq_inherited_pri < UPRI(td))
2176 sched_lend_user_prio(td, uq->uq_inherited_pri);
2179 mtx_unlock(&umtx_lock);
2181 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2182 id | UMUTEX_CONTESTED);
2183 /* The address was invalid. */
2189 MPASS(owner == UMUTEX_CONTESTED);
2194 if (owner == UMUTEX_RB_OWNERDEAD) {
2195 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2196 &owner, id | UMUTEX_CONTESTED);
2202 MPASS(owner == UMUTEX_RB_OWNERDEAD);
2203 error = EOWNERDEAD; /* success */
2208 * rv == 1, only check for suspension if we
2209 * did not already catched a signal. If we
2210 * get an error from the check, the same
2211 * condition is checked by the umtxq_sleep()
2212 * call below, so we should obliterate the
2213 * error to not skip the last loop iteration.
2216 error = thread_check_susp(td, false);
2225 } else if (owner == UMUTEX_RB_NOTRECOV) {
2226 error = ENOTRECOVERABLE;
2233 * If we caught a signal, we have retried and now
2239 umtxq_lock(&uq->uq_key);
2241 umtxq_unbusy(&uq->uq_key);
2242 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2245 umtxq_unlock(&uq->uq_key);
2247 mtx_lock(&umtx_lock);
2248 uq->uq_inherited_pri = old_inherited_pri;
2250 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2251 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2253 if (pri > UPRI(uq2->uq_thread))
2254 pri = UPRI(uq2->uq_thread);
2257 if (pri > uq->uq_inherited_pri)
2258 pri = uq->uq_inherited_pri;
2260 sched_lend_user_prio(td, pri);
2262 mtx_unlock(&umtx_lock);
2265 if (error != 0 && error != EOWNERDEAD) {
2266 mtx_lock(&umtx_lock);
2267 uq->uq_inherited_pri = old_inherited_pri;
2269 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2270 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2272 if (pri > UPRI(uq2->uq_thread))
2273 pri = UPRI(uq2->uq_thread);
2276 if (pri > uq->uq_inherited_pri)
2277 pri = uq->uq_inherited_pri;
2279 sched_lend_user_prio(td, pri);
2281 mtx_unlock(&umtx_lock);
2285 umtxq_unbusy_unlocked(&uq->uq_key);
2286 umtx_key_release(&uq->uq_key);
2291 * Unlock a PP mutex.
2294 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2296 struct umtx_key key;
2297 struct umtx_q *uq, *uq2;
2299 uint32_t id, owner, rceiling;
2300 int error, pri, new_inherited_pri, su;
2304 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2307 * Make sure we own this mtx.
2309 error = fueword32(&m->m_owner, &owner);
2313 if ((owner & ~UMUTEX_CONTESTED) != id)
2316 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2321 new_inherited_pri = PRI_MAX;
2323 rceiling = RTP_PRIO_MAX - rceiling;
2324 if (rceiling > RTP_PRIO_MAX)
2326 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2329 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2330 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2337 * For priority protected mutex, always set unlocked state
2338 * to UMUTEX_CONTESTED, so that userland always enters kernel
2339 * to lock the mutex, it is necessary because thread priority
2340 * has to be adjusted for such mutex.
2342 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2347 umtxq_signal(&key, 1);
2354 mtx_lock(&umtx_lock);
2356 uq->uq_inherited_pri = new_inherited_pri;
2358 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2359 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2361 if (pri > UPRI(uq2->uq_thread))
2362 pri = UPRI(uq2->uq_thread);
2365 if (pri > uq->uq_inherited_pri)
2366 pri = uq->uq_inherited_pri;
2368 sched_lend_user_prio(td, pri);
2370 mtx_unlock(&umtx_lock);
2372 umtx_key_release(&key);
2377 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2378 uint32_t *old_ceiling)
2381 uint32_t flags, id, owner, save_ceiling;
2384 error = fueword32(&m->m_flags, &flags);
2387 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2389 if (ceiling > RTP_PRIO_MAX)
2393 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2394 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2398 umtxq_lock(&uq->uq_key);
2399 umtxq_busy(&uq->uq_key);
2400 umtxq_unlock(&uq->uq_key);
2402 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2408 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2409 id | UMUTEX_CONTESTED);
2416 MPASS(owner == UMUTEX_CONTESTED);
2417 rv = suword32(&m->m_ceilings[0], ceiling);
2418 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2419 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2423 if ((owner & ~UMUTEX_CONTESTED) == id) {
2424 rv = suword32(&m->m_ceilings[0], ceiling);
2425 error = rv == 0 ? 0 : EFAULT;
2429 if (owner == UMUTEX_RB_OWNERDEAD) {
2432 } else if (owner == UMUTEX_RB_NOTRECOV) {
2433 error = ENOTRECOVERABLE;
2438 * If we caught a signal, we have retried and now
2445 * We set the contested bit, sleep. Otherwise the lock changed
2446 * and we need to retry or we lost a race to the thread
2447 * unlocking the umtx.
2449 umtxq_lock(&uq->uq_key);
2451 umtxq_unbusy(&uq->uq_key);
2452 error = umtxq_sleep(uq, "umtxpp", NULL);
2454 umtxq_unlock(&uq->uq_key);
2456 umtxq_lock(&uq->uq_key);
2458 umtxq_signal(&uq->uq_key, INT_MAX);
2459 umtxq_unbusy(&uq->uq_key);
2460 umtxq_unlock(&uq->uq_key);
2461 umtx_key_release(&uq->uq_key);
2462 if (error == 0 && old_ceiling != NULL) {
2463 rv = suword32(old_ceiling, save_ceiling);
2464 error = rv == 0 ? 0 : EFAULT;
2470 * Lock a userland POSIX mutex.
2473 do_lock_umutex(struct thread *td, struct umutex *m,
2474 struct _umtx_time *timeout, int mode)
2479 error = fueword32(&m->m_flags, &flags);
2483 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2485 error = do_lock_normal(td, m, flags, timeout, mode);
2487 case UMUTEX_PRIO_INHERIT:
2488 error = do_lock_pi(td, m, flags, timeout, mode);
2490 case UMUTEX_PRIO_PROTECT:
2491 error = do_lock_pp(td, m, flags, timeout, mode);
2496 if (timeout == NULL) {
2497 if (error == EINTR && mode != _UMUTEX_WAIT)
2500 /* Timed-locking is not restarted. */
2501 if (error == ERESTART)
2508 * Unlock a userland POSIX mutex.
2511 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2516 error = fueword32(&m->m_flags, &flags);
2520 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2522 return (do_unlock_normal(td, m, flags, rb));
2523 case UMUTEX_PRIO_INHERIT:
2524 return (do_unlock_pi(td, m, flags, rb));
2525 case UMUTEX_PRIO_PROTECT:
2526 return (do_unlock_pp(td, m, flags, rb));
2533 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2534 struct timespec *timeout, u_long wflags)
2536 struct abs_timeout timo;
2538 uint32_t flags, clockid, hasw;
2542 error = fueword32(&cv->c_flags, &flags);
2545 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2549 if ((wflags & CVWAIT_CLOCKID) != 0) {
2550 error = fueword32(&cv->c_clockid, &clockid);
2552 umtx_key_release(&uq->uq_key);
2555 if (clockid < CLOCK_REALTIME ||
2556 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2557 /* hmm, only HW clock id will work. */
2558 umtx_key_release(&uq->uq_key);
2562 clockid = CLOCK_REALTIME;
2565 umtxq_lock(&uq->uq_key);
2566 umtxq_busy(&uq->uq_key);
2568 umtxq_unlock(&uq->uq_key);
2571 * Set c_has_waiters to 1 before releasing user mutex, also
2572 * don't modify cache line when unnecessary.
2574 error = fueword32(&cv->c_has_waiters, &hasw);
2575 if (error == 0 && hasw == 0)
2576 suword32(&cv->c_has_waiters, 1);
2578 umtxq_unbusy_unlocked(&uq->uq_key);
2580 error = do_unlock_umutex(td, m, false);
2582 if (timeout != NULL)
2583 abs_timeout_init(&timo, clockid, (wflags & CVWAIT_ABSTIME) != 0,
2586 umtxq_lock(&uq->uq_key);
2588 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2592 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2596 * This must be timeout,interrupted by signal or
2597 * surprious wakeup, clear c_has_waiter flag when
2600 umtxq_busy(&uq->uq_key);
2601 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2602 int oldlen = uq->uq_cur_queue->length;
2605 umtxq_unlock(&uq->uq_key);
2606 suword32(&cv->c_has_waiters, 0);
2607 umtxq_lock(&uq->uq_key);
2610 umtxq_unbusy(&uq->uq_key);
2611 if (error == ERESTART)
2615 umtxq_unlock(&uq->uq_key);
2616 umtx_key_release(&uq->uq_key);
2621 * Signal a userland condition variable.
2624 do_cv_signal(struct thread *td, struct ucond *cv)
2626 struct umtx_key key;
2627 int error, cnt, nwake;
2630 error = fueword32(&cv->c_flags, &flags);
2633 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2637 cnt = umtxq_count(&key);
2638 nwake = umtxq_signal(&key, 1);
2641 error = suword32(&cv->c_has_waiters, 0);
2648 umtx_key_release(&key);
2653 do_cv_broadcast(struct thread *td, struct ucond *cv)
2655 struct umtx_key key;
2659 error = fueword32(&cv->c_flags, &flags);
2662 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2667 umtxq_signal(&key, INT_MAX);
2670 error = suword32(&cv->c_has_waiters, 0);
2674 umtxq_unbusy_unlocked(&key);
2676 umtx_key_release(&key);
2681 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
2682 struct _umtx_time *timeout)
2684 struct abs_timeout timo;
2686 uint32_t flags, wrflags;
2687 int32_t state, oldstate;
2688 int32_t blocked_readers;
2689 int error, error1, rv;
2692 error = fueword32(&rwlock->rw_flags, &flags);
2695 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2699 if (timeout != NULL)
2700 abs_timeout_init2(&timo, timeout);
2702 wrflags = URWLOCK_WRITE_OWNER;
2703 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2704 wrflags |= URWLOCK_WRITE_WAITERS;
2707 rv = fueword32(&rwlock->rw_state, &state);
2709 umtx_key_release(&uq->uq_key);
2713 /* try to lock it */
2714 while (!(state & wrflags)) {
2715 if (__predict_false(URWLOCK_READER_COUNT(state) ==
2716 URWLOCK_MAX_READERS)) {
2717 umtx_key_release(&uq->uq_key);
2720 rv = casueword32(&rwlock->rw_state, state,
2721 &oldstate, state + 1);
2723 umtx_key_release(&uq->uq_key);
2727 MPASS(oldstate == state);
2728 umtx_key_release(&uq->uq_key);
2731 error = thread_check_susp(td, true);
2740 /* grab monitor lock */
2741 umtxq_lock(&uq->uq_key);
2742 umtxq_busy(&uq->uq_key);
2743 umtxq_unlock(&uq->uq_key);
2746 * re-read the state, in case it changed between the try-lock above
2747 * and the check below
2749 rv = fueword32(&rwlock->rw_state, &state);
2753 /* set read contention bit */
2754 while (error == 0 && (state & wrflags) &&
2755 !(state & URWLOCK_READ_WAITERS)) {
2756 rv = casueword32(&rwlock->rw_state, state,
2757 &oldstate, state | URWLOCK_READ_WAITERS);
2763 MPASS(oldstate == state);
2767 error = thread_check_susp(td, false);
2772 umtxq_unbusy_unlocked(&uq->uq_key);
2776 /* state is changed while setting flags, restart */
2777 if (!(state & wrflags)) {
2778 umtxq_unbusy_unlocked(&uq->uq_key);
2779 error = thread_check_susp(td, true);
2787 * Contention bit is set, before sleeping, increase
2788 * read waiter count.
2790 rv = fueword32(&rwlock->rw_blocked_readers,
2793 umtxq_unbusy_unlocked(&uq->uq_key);
2797 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2799 while (state & wrflags) {
2800 umtxq_lock(&uq->uq_key);
2802 umtxq_unbusy(&uq->uq_key);
2804 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2807 umtxq_busy(&uq->uq_key);
2809 umtxq_unlock(&uq->uq_key);
2812 rv = fueword32(&rwlock->rw_state, &state);
2819 /* decrease read waiter count, and may clear read contention bit */
2820 rv = fueword32(&rwlock->rw_blocked_readers,
2823 umtxq_unbusy_unlocked(&uq->uq_key);
2827 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2828 if (blocked_readers == 1) {
2829 rv = fueword32(&rwlock->rw_state, &state);
2831 umtxq_unbusy_unlocked(&uq->uq_key);
2836 rv = casueword32(&rwlock->rw_state, state,
2837 &oldstate, state & ~URWLOCK_READ_WAITERS);
2843 MPASS(oldstate == state);
2847 error1 = thread_check_susp(td, false);
2856 umtxq_unbusy_unlocked(&uq->uq_key);
2860 umtx_key_release(&uq->uq_key);
2861 if (error == ERESTART)
2867 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2869 struct abs_timeout timo;
2872 int32_t state, oldstate;
2873 int32_t blocked_writers;
2874 int32_t blocked_readers;
2875 int error, error1, rv;
2878 error = fueword32(&rwlock->rw_flags, &flags);
2881 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2885 if (timeout != NULL)
2886 abs_timeout_init2(&timo, timeout);
2888 blocked_readers = 0;
2890 rv = fueword32(&rwlock->rw_state, &state);
2892 umtx_key_release(&uq->uq_key);
2895 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
2896 URWLOCK_READER_COUNT(state) == 0) {
2897 rv = casueword32(&rwlock->rw_state, state,
2898 &oldstate, state | URWLOCK_WRITE_OWNER);
2900 umtx_key_release(&uq->uq_key);
2904 MPASS(oldstate == state);
2905 umtx_key_release(&uq->uq_key);
2909 error = thread_check_susp(td, true);
2915 if ((state & (URWLOCK_WRITE_OWNER |
2916 URWLOCK_WRITE_WAITERS)) == 0 &&
2917 blocked_readers != 0) {
2918 umtxq_lock(&uq->uq_key);
2919 umtxq_busy(&uq->uq_key);
2920 umtxq_signal_queue(&uq->uq_key, INT_MAX,
2922 umtxq_unbusy(&uq->uq_key);
2923 umtxq_unlock(&uq->uq_key);
2929 /* grab monitor lock */
2930 umtxq_lock(&uq->uq_key);
2931 umtxq_busy(&uq->uq_key);
2932 umtxq_unlock(&uq->uq_key);
2935 * Re-read the state, in case it changed between the
2936 * try-lock above and the check below.
2938 rv = fueword32(&rwlock->rw_state, &state);
2942 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
2943 URWLOCK_READER_COUNT(state) != 0) &&
2944 (state & URWLOCK_WRITE_WAITERS) == 0) {
2945 rv = casueword32(&rwlock->rw_state, state,
2946 &oldstate, state | URWLOCK_WRITE_WAITERS);
2952 MPASS(oldstate == state);
2956 error = thread_check_susp(td, false);
2961 umtxq_unbusy_unlocked(&uq->uq_key);
2965 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
2966 URWLOCK_READER_COUNT(state) == 0) {
2967 umtxq_unbusy_unlocked(&uq->uq_key);
2968 error = thread_check_susp(td, false);
2974 rv = fueword32(&rwlock->rw_blocked_writers,
2977 umtxq_unbusy_unlocked(&uq->uq_key);
2981 suword32(&rwlock->rw_blocked_writers, blocked_writers + 1);
2983 while ((state & URWLOCK_WRITE_OWNER) ||
2984 URWLOCK_READER_COUNT(state) != 0) {
2985 umtxq_lock(&uq->uq_key);
2986 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2987 umtxq_unbusy(&uq->uq_key);
2989 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
2992 umtxq_busy(&uq->uq_key);
2993 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2994 umtxq_unlock(&uq->uq_key);
2997 rv = fueword32(&rwlock->rw_state, &state);
3004 rv = fueword32(&rwlock->rw_blocked_writers,
3007 umtxq_unbusy_unlocked(&uq->uq_key);
3011 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
3012 if (blocked_writers == 1) {
3013 rv = fueword32(&rwlock->rw_state, &state);
3015 umtxq_unbusy_unlocked(&uq->uq_key);
3020 rv = casueword32(&rwlock->rw_state, state,
3021 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3027 MPASS(oldstate == state);
3031 error1 = thread_check_susp(td, false);
3033 * We are leaving the URWLOCK_WRITE_WAITERS
3034 * behind, but this should not harm the
3043 rv = fueword32(&rwlock->rw_blocked_readers,
3046 umtxq_unbusy_unlocked(&uq->uq_key);
3051 blocked_readers = 0;
3053 umtxq_unbusy_unlocked(&uq->uq_key);
3056 umtx_key_release(&uq->uq_key);
3057 if (error == ERESTART)
3063 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3067 int32_t state, oldstate;
3068 int error, rv, q, count;
3071 error = fueword32(&rwlock->rw_flags, &flags);
3074 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3078 error = fueword32(&rwlock->rw_state, &state);
3083 if (state & URWLOCK_WRITE_OWNER) {
3085 rv = casueword32(&rwlock->rw_state, state,
3086 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3093 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3097 error = thread_check_susp(td, true);
3103 } else if (URWLOCK_READER_COUNT(state) != 0) {
3105 rv = casueword32(&rwlock->rw_state, state,
3106 &oldstate, state - 1);
3113 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3117 error = thread_check_susp(td, true);
3130 if (!(flags & URWLOCK_PREFER_READER)) {
3131 if (state & URWLOCK_WRITE_WAITERS) {
3133 q = UMTX_EXCLUSIVE_QUEUE;
3134 } else if (state & URWLOCK_READ_WAITERS) {
3136 q = UMTX_SHARED_QUEUE;
3139 if (state & URWLOCK_READ_WAITERS) {
3141 q = UMTX_SHARED_QUEUE;
3142 } else if (state & URWLOCK_WRITE_WAITERS) {
3144 q = UMTX_EXCLUSIVE_QUEUE;
3149 umtxq_lock(&uq->uq_key);
3150 umtxq_busy(&uq->uq_key);
3151 umtxq_signal_queue(&uq->uq_key, count, q);
3152 umtxq_unbusy(&uq->uq_key);
3153 umtxq_unlock(&uq->uq_key);
3156 umtx_key_release(&uq->uq_key);
3160 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3162 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3164 struct abs_timeout timo;
3166 uint32_t flags, count, count1;
3170 error = fueword32(&sem->_flags, &flags);
3173 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3177 if (timeout != NULL)
3178 abs_timeout_init2(&timo, timeout);
3181 umtxq_lock(&uq->uq_key);
3182 umtxq_busy(&uq->uq_key);
3184 umtxq_unlock(&uq->uq_key);
3185 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3187 rv1 = fueword32(&sem->_count, &count);
3188 if (rv == -1 || (rv == 0 && (rv1 == -1 || count != 0)) ||
3189 (rv == 1 && count1 == 0)) {
3190 umtxq_lock(&uq->uq_key);
3191 umtxq_unbusy(&uq->uq_key);
3193 umtxq_unlock(&uq->uq_key);
3195 rv = thread_check_susp(td, true);
3203 error = rv == -1 ? EFAULT : 0;
3206 umtxq_lock(&uq->uq_key);
3207 umtxq_unbusy(&uq->uq_key);
3209 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3211 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3215 /* A relative timeout cannot be restarted. */
3216 if (error == ERESTART && timeout != NULL &&
3217 (timeout->_flags & UMTX_ABSTIME) == 0)
3220 umtxq_unlock(&uq->uq_key);
3222 umtx_key_release(&uq->uq_key);
3227 * Signal a userland semaphore.
3230 do_sem_wake(struct thread *td, struct _usem *sem)
3232 struct umtx_key key;
3236 error = fueword32(&sem->_flags, &flags);
3239 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3243 cnt = umtxq_count(&key);
3246 * Check if count is greater than 0, this means the memory is
3247 * still being referenced by user code, so we can safely
3248 * update _has_waiters flag.
3252 error = suword32(&sem->_has_waiters, 0);
3257 umtxq_signal(&key, 1);
3261 umtx_key_release(&key);
3267 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3269 struct abs_timeout timo;
3271 uint32_t count, flags;
3275 flags = fuword32(&sem->_flags);
3276 if (timeout != NULL)
3277 abs_timeout_init2(&timo, timeout);
3280 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3283 umtxq_lock(&uq->uq_key);
3284 umtxq_busy(&uq->uq_key);
3286 umtxq_unlock(&uq->uq_key);
3287 rv = fueword32(&sem->_count, &count);
3289 umtxq_lock(&uq->uq_key);
3290 umtxq_unbusy(&uq->uq_key);
3292 umtxq_unlock(&uq->uq_key);
3293 umtx_key_release(&uq->uq_key);
3297 if (USEM_COUNT(count) != 0) {
3298 umtxq_lock(&uq->uq_key);
3299 umtxq_unbusy(&uq->uq_key);
3301 umtxq_unlock(&uq->uq_key);
3302 umtx_key_release(&uq->uq_key);
3305 if (count == USEM_HAS_WAITERS)
3307 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3310 umtxq_lock(&uq->uq_key);
3311 umtxq_unbusy(&uq->uq_key);
3313 umtxq_unlock(&uq->uq_key);
3314 umtx_key_release(&uq->uq_key);
3317 rv = thread_check_susp(td, true);
3322 umtxq_lock(&uq->uq_key);
3323 umtxq_unbusy(&uq->uq_key);
3325 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3327 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3331 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3332 /* A relative timeout cannot be restarted. */
3333 if (error == ERESTART)
3335 if (error == EINTR) {
3336 abs_timeout_update(&timo);
3337 timespecsub(&timo.end, &timo.cur,
3338 &timeout->_timeout);
3342 umtxq_unlock(&uq->uq_key);
3343 umtx_key_release(&uq->uq_key);
3348 * Signal a userland semaphore.
3351 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3353 struct umtx_key key;
3355 uint32_t count, flags;
3357 rv = fueword32(&sem->_flags, &flags);
3360 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3364 cnt = umtxq_count(&key);
3367 * If this was the last sleeping thread, clear the waiters
3372 rv = fueword32(&sem->_count, &count);
3373 while (rv != -1 && count & USEM_HAS_WAITERS) {
3374 rv = casueword32(&sem->_count, count, &count,
3375 count & ~USEM_HAS_WAITERS);
3377 rv = thread_check_susp(td, true);
3390 umtxq_signal(&key, 1);
3394 umtx_key_release(&key);
3399 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3403 error = copyin(addr, tsp, sizeof(struct timespec));
3405 if (tsp->tv_sec < 0 ||
3406 tsp->tv_nsec >= 1000000000 ||
3414 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3418 if (size <= sizeof(struct timespec)) {
3419 tp->_clockid = CLOCK_REALTIME;
3421 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3423 error = copyin(addr, tp, sizeof(struct _umtx_time));
3426 if (tp->_timeout.tv_sec < 0 ||
3427 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3433 __umtx_op_unimpl(struct thread *td, struct _umtx_op_args *uap)
3436 return (EOPNOTSUPP);
3440 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3442 struct _umtx_time timeout, *tm_p;
3445 if (uap->uaddr2 == NULL)
3448 error = umtx_copyin_umtx_time(
3449 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3454 return (do_wait(td, uap->obj, uap->val, tm_p, 0, 0));
3458 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3460 struct _umtx_time timeout, *tm_p;
3463 if (uap->uaddr2 == NULL)
3466 error = umtx_copyin_umtx_time(
3467 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3472 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3476 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3478 struct _umtx_time *tm_p, timeout;
3481 if (uap->uaddr2 == NULL)
3484 error = umtx_copyin_umtx_time(
3485 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3490 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3494 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3497 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3500 #define BATCH_SIZE 128
3502 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3504 char *uaddrs[BATCH_SIZE], **upp;
3505 int count, error, i, pos, tocopy;
3507 upp = (char **)uap->obj;
3509 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3511 tocopy = MIN(count, BATCH_SIZE);
3512 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3515 for (i = 0; i < tocopy; ++i)
3516 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3523 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3526 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3530 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3532 struct _umtx_time *tm_p, timeout;
3535 /* Allow a null timespec (wait forever). */
3536 if (uap->uaddr2 == NULL)
3539 error = umtx_copyin_umtx_time(
3540 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3545 return (do_lock_umutex(td, uap->obj, tm_p, 0));
3549 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3552 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
3556 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3558 struct _umtx_time *tm_p, timeout;
3561 /* Allow a null timespec (wait forever). */
3562 if (uap->uaddr2 == NULL)
3565 error = umtx_copyin_umtx_time(
3566 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3571 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
3575 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3578 return (do_wake_umutex(td, uap->obj));
3582 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3585 return (do_unlock_umutex(td, uap->obj, false));
3589 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3592 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
3596 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3598 struct timespec *ts, timeout;
3601 /* Allow a null timespec (wait forever). */
3602 if (uap->uaddr2 == NULL)
3605 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3610 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3614 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3617 return (do_cv_signal(td, uap->obj));
3621 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3624 return (do_cv_broadcast(td, uap->obj));
3628 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3630 struct _umtx_time timeout;
3633 /* Allow a null timespec (wait forever). */
3634 if (uap->uaddr2 == NULL) {
3635 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3637 error = umtx_copyin_umtx_time(uap->uaddr2,
3638 (size_t)uap->uaddr1, &timeout);
3641 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3647 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3649 struct _umtx_time timeout;
3652 /* Allow a null timespec (wait forever). */
3653 if (uap->uaddr2 == NULL) {
3654 error = do_rw_wrlock(td, uap->obj, 0);
3656 error = umtx_copyin_umtx_time(uap->uaddr2,
3657 (size_t)uap->uaddr1, &timeout);
3661 error = do_rw_wrlock(td, uap->obj, &timeout);
3667 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3670 return (do_rw_unlock(td, uap->obj));
3673 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3675 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3677 struct _umtx_time *tm_p, timeout;
3680 /* Allow a null timespec (wait forever). */
3681 if (uap->uaddr2 == NULL)
3684 error = umtx_copyin_umtx_time(
3685 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3690 return (do_sem_wait(td, uap->obj, tm_p));
3694 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3697 return (do_sem_wake(td, uap->obj));
3702 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3705 return (do_wake2_umutex(td, uap->obj, uap->val));
3709 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap)
3711 struct _umtx_time *tm_p, timeout;
3715 /* Allow a null timespec (wait forever). */
3716 if (uap->uaddr2 == NULL) {
3720 uasize = (size_t)uap->uaddr1;
3721 error = umtx_copyin_umtx_time(uap->uaddr2, uasize, &timeout);
3726 error = do_sem2_wait(td, uap->obj, tm_p);
3727 if (error == EINTR && uap->uaddr2 != NULL &&
3728 (timeout._flags & UMTX_ABSTIME) == 0 &&
3729 uasize >= sizeof(struct _umtx_time) + sizeof(struct timespec)) {
3730 error = copyout(&timeout._timeout,
3731 (struct _umtx_time *)uap->uaddr2 + 1,
3732 sizeof(struct timespec));
3742 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap)
3745 return (do_sem2_wake(td, uap->obj));
3748 #define USHM_OBJ_UMTX(o) \
3749 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
3751 #define USHMF_REG_LINKED 0x0001
3752 #define USHMF_OBJ_LINKED 0x0002
3753 struct umtx_shm_reg {
3754 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
3755 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
3756 struct umtx_key ushm_key;
3757 struct ucred *ushm_cred;
3758 struct shmfd *ushm_obj;
3763 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
3764 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
3766 static uma_zone_t umtx_shm_reg_zone;
3767 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
3768 static struct mtx umtx_shm_lock;
3769 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
3770 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
3772 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
3775 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
3777 struct umtx_shm_reg_head d;
3778 struct umtx_shm_reg *reg, *reg1;
3781 mtx_lock(&umtx_shm_lock);
3782 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
3783 mtx_unlock(&umtx_shm_lock);
3784 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
3785 TAILQ_REMOVE(&d, reg, ushm_reg_link);
3786 umtx_shm_free_reg(reg);
3790 static struct task umtx_shm_reg_delfree_task =
3791 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
3793 static struct umtx_shm_reg *
3794 umtx_shm_find_reg_locked(const struct umtx_key *key)
3796 struct umtx_shm_reg *reg;
3797 struct umtx_shm_reg_head *reg_head;
3799 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
3800 mtx_assert(&umtx_shm_lock, MA_OWNED);
3801 reg_head = &umtx_shm_registry[key->hash];
3802 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
3803 KASSERT(reg->ushm_key.shared,
3804 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
3805 if (reg->ushm_key.info.shared.object ==
3806 key->info.shared.object &&
3807 reg->ushm_key.info.shared.offset ==
3808 key->info.shared.offset) {
3809 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
3810 KASSERT(reg->ushm_refcnt > 0,
3811 ("reg %p refcnt 0 onlist", reg));
3812 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
3813 ("reg %p not linked", reg));
3821 static struct umtx_shm_reg *
3822 umtx_shm_find_reg(const struct umtx_key *key)
3824 struct umtx_shm_reg *reg;
3826 mtx_lock(&umtx_shm_lock);
3827 reg = umtx_shm_find_reg_locked(key);
3828 mtx_unlock(&umtx_shm_lock);
3833 umtx_shm_free_reg(struct umtx_shm_reg *reg)
3836 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
3837 crfree(reg->ushm_cred);
3838 shm_drop(reg->ushm_obj);
3839 uma_zfree(umtx_shm_reg_zone, reg);
3843 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
3847 mtx_assert(&umtx_shm_lock, MA_OWNED);
3848 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
3850 res = reg->ushm_refcnt == 0;
3852 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
3853 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
3854 reg, ushm_reg_link);
3855 reg->ushm_flags &= ~USHMF_REG_LINKED;
3857 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
3858 LIST_REMOVE(reg, ushm_obj_link);
3859 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
3866 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
3872 object = reg->ushm_obj->shm_object;
3873 VM_OBJECT_WLOCK(object);
3874 object->flags |= OBJ_UMTXDEAD;
3875 VM_OBJECT_WUNLOCK(object);
3877 mtx_lock(&umtx_shm_lock);
3878 dofree = umtx_shm_unref_reg_locked(reg, force);
3879 mtx_unlock(&umtx_shm_lock);
3881 umtx_shm_free_reg(reg);
3885 umtx_shm_object_init(vm_object_t object)
3888 LIST_INIT(USHM_OBJ_UMTX(object));
3892 umtx_shm_object_terminated(vm_object_t object)
3894 struct umtx_shm_reg *reg, *reg1;
3897 if (LIST_EMPTY(USHM_OBJ_UMTX(object)))
3901 mtx_lock(&umtx_shm_lock);
3902 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
3903 if (umtx_shm_unref_reg_locked(reg, true)) {
3904 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
3909 mtx_unlock(&umtx_shm_lock);
3911 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
3915 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
3916 struct umtx_shm_reg **res)
3918 struct umtx_shm_reg *reg, *reg1;
3922 reg = umtx_shm_find_reg(key);
3927 cred = td->td_ucred;
3928 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
3930 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
3931 reg->ushm_refcnt = 1;
3932 bcopy(key, ®->ushm_key, sizeof(*key));
3933 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR);
3934 reg->ushm_cred = crhold(cred);
3935 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
3937 umtx_shm_free_reg(reg);
3940 mtx_lock(&umtx_shm_lock);
3941 reg1 = umtx_shm_find_reg_locked(key);
3943 mtx_unlock(&umtx_shm_lock);
3944 umtx_shm_free_reg(reg);
3949 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
3950 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
3952 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
3953 mtx_unlock(&umtx_shm_lock);
3959 umtx_shm_alive(struct thread *td, void *addr)
3962 vm_map_entry_t entry;
3969 map = &td->td_proc->p_vmspace->vm_map;
3970 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
3971 &object, &pindex, &prot, &wired);
3972 if (res != KERN_SUCCESS)
3977 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
3978 vm_map_lookup_done(map, entry);
3987 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
3988 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
3989 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
3990 for (i = 0; i < nitems(umtx_shm_registry); i++)
3991 TAILQ_INIT(&umtx_shm_registry[i]);
3995 umtx_shm(struct thread *td, void *addr, u_int flags)
3997 struct umtx_key key;
3998 struct umtx_shm_reg *reg;
4002 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
4003 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
4005 if ((flags & UMTX_SHM_ALIVE) != 0)
4006 return (umtx_shm_alive(td, addr));
4007 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
4010 KASSERT(key.shared == 1, ("non-shared key"));
4011 if ((flags & UMTX_SHM_CREAT) != 0) {
4012 error = umtx_shm_create_reg(td, &key, ®);
4014 reg = umtx_shm_find_reg(&key);
4018 umtx_key_release(&key);
4021 KASSERT(reg != NULL, ("no reg"));
4022 if ((flags & UMTX_SHM_DESTROY) != 0) {
4023 umtx_shm_unref_reg(reg, true);
4027 error = mac_posixshm_check_open(td->td_ucred,
4028 reg->ushm_obj, FFLAGS(O_RDWR));
4031 error = shm_access(reg->ushm_obj, td->td_ucred,
4035 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
4037 shm_hold(reg->ushm_obj);
4038 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
4040 td->td_retval[0] = fd;
4044 umtx_shm_unref_reg(reg, false);
4049 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap)
4052 return (umtx_shm(td, uap->uaddr1, uap->val));
4056 umtx_robust_lists(struct thread *td, struct umtx_robust_lists_params *rbp)
4059 td->td_rb_list = rbp->robust_list_offset;
4060 td->td_rbp_list = rbp->robust_priv_list_offset;
4061 td->td_rb_inact = rbp->robust_inact_offset;
4066 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap)
4068 struct umtx_robust_lists_params rb;
4071 if (uap->val > sizeof(rb))
4073 bzero(&rb, sizeof(rb));
4074 error = copyin(uap->uaddr1, &rb, uap->val);
4077 return (umtx_robust_lists(td, &rb));
4080 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
4082 static const _umtx_op_func op_table[] = {
4083 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
4084 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
4085 [UMTX_OP_WAIT] = __umtx_op_wait,
4086 [UMTX_OP_WAKE] = __umtx_op_wake,
4087 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4088 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
4089 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4090 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4091 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
4092 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4093 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4094 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
4095 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
4096 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
4097 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4098 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4099 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4100 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4101 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4102 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4103 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4104 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4106 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4107 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4109 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4110 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4111 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4112 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4113 [UMTX_OP_SHM] = __umtx_op_shm,
4114 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4118 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4121 if ((unsigned)uap->op < nitems(op_table))
4122 return (*op_table[uap->op])(td, uap);
4126 #ifdef COMPAT_FREEBSD32
4133 struct umtx_time32 {
4134 struct timespec32 timeout;
4140 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
4142 struct timespec32 ts32;
4145 error = copyin(addr, &ts32, sizeof(struct timespec32));
4147 if (ts32.tv_sec < 0 ||
4148 ts32.tv_nsec >= 1000000000 ||
4152 tsp->tv_sec = ts32.tv_sec;
4153 tsp->tv_nsec = ts32.tv_nsec;
4160 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
4162 struct umtx_time32 t32;
4165 t32.clockid = CLOCK_REALTIME;
4167 if (size <= sizeof(struct timespec32))
4168 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
4170 error = copyin(addr, &t32, sizeof(struct umtx_time32));
4173 if (t32.timeout.tv_sec < 0 ||
4174 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
4176 tp->_timeout.tv_sec = t32.timeout.tv_sec;
4177 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
4178 tp->_flags = t32.flags;
4179 tp->_clockid = t32.clockid;
4184 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4186 struct _umtx_time *tm_p, timeout;
4189 if (uap->uaddr2 == NULL)
4192 error = umtx_copyin_umtx_time32(uap->uaddr2,
4193 (size_t)uap->uaddr1, &timeout);
4198 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
4202 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4204 struct _umtx_time *tm_p, timeout;
4207 /* Allow a null timespec (wait forever). */
4208 if (uap->uaddr2 == NULL)
4211 error = umtx_copyin_umtx_time32(uap->uaddr2,
4212 (size_t)uap->uaddr1, &timeout);
4217 return (do_lock_umutex(td, uap->obj, tm_p, 0));
4221 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4223 struct _umtx_time *tm_p, timeout;
4226 /* Allow a null timespec (wait forever). */
4227 if (uap->uaddr2 == NULL)
4230 error = umtx_copyin_umtx_time32(uap->uaddr2,
4231 (size_t)uap->uaddr1, &timeout);
4236 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
4240 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4242 struct timespec *ts, timeout;
4245 /* Allow a null timespec (wait forever). */
4246 if (uap->uaddr2 == NULL)
4249 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
4254 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
4258 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4260 struct _umtx_time timeout;
4263 /* Allow a null timespec (wait forever). */
4264 if (uap->uaddr2 == NULL) {
4265 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4267 error = umtx_copyin_umtx_time32(uap->uaddr2,
4268 (size_t)uap->uaddr1, &timeout);
4271 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4277 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4279 struct _umtx_time timeout;
4282 /* Allow a null timespec (wait forever). */
4283 if (uap->uaddr2 == NULL) {
4284 error = do_rw_wrlock(td, uap->obj, 0);
4286 error = umtx_copyin_umtx_time32(uap->uaddr2,
4287 (size_t)uap->uaddr1, &timeout);
4290 error = do_rw_wrlock(td, uap->obj, &timeout);
4296 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
4298 struct _umtx_time *tm_p, timeout;
4301 if (uap->uaddr2 == NULL)
4304 error = umtx_copyin_umtx_time32(
4305 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
4310 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
4313 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4315 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4317 struct _umtx_time *tm_p, timeout;
4320 /* Allow a null timespec (wait forever). */
4321 if (uap->uaddr2 == NULL)
4324 error = umtx_copyin_umtx_time32(uap->uaddr2,
4325 (size_t)uap->uaddr1, &timeout);
4330 return (do_sem_wait(td, uap->obj, tm_p));
4335 __umtx_op_sem2_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4337 struct _umtx_time *tm_p, timeout;
4341 /* Allow a null timespec (wait forever). */
4342 if (uap->uaddr2 == NULL) {
4346 uasize = (size_t)uap->uaddr1;
4347 error = umtx_copyin_umtx_time32(uap->uaddr2, uasize, &timeout);
4352 error = do_sem2_wait(td, uap->obj, tm_p);
4353 if (error == EINTR && uap->uaddr2 != NULL &&
4354 (timeout._flags & UMTX_ABSTIME) == 0 &&
4355 uasize >= sizeof(struct umtx_time32) + sizeof(struct timespec32)) {
4356 struct timespec32 remain32 = {
4357 .tv_sec = timeout._timeout.tv_sec,
4358 .tv_nsec = timeout._timeout.tv_nsec
4360 error = copyout(&remain32,
4361 (struct umtx_time32 *)uap->uaddr2 + 1,
4362 sizeof(struct timespec32));
4372 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
4374 uint32_t uaddrs[BATCH_SIZE], **upp;
4375 int count, error, i, pos, tocopy;
4377 upp = (uint32_t **)uap->obj;
4379 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
4381 tocopy = MIN(count, BATCH_SIZE);
4382 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
4385 for (i = 0; i < tocopy; ++i)
4386 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
4393 struct umtx_robust_lists_params_compat32 {
4394 uint32_t robust_list_offset;
4395 uint32_t robust_priv_list_offset;
4396 uint32_t robust_inact_offset;
4400 __umtx_op_robust_lists_compat32(struct thread *td, struct _umtx_op_args *uap)
4402 struct umtx_robust_lists_params rb;
4403 struct umtx_robust_lists_params_compat32 rb32;
4406 if (uap->val > sizeof(rb32))
4408 bzero(&rb, sizeof(rb));
4409 bzero(&rb32, sizeof(rb32));
4410 error = copyin(uap->uaddr1, &rb32, uap->val);
4413 rb.robust_list_offset = rb32.robust_list_offset;
4414 rb.robust_priv_list_offset = rb32.robust_priv_list_offset;
4415 rb.robust_inact_offset = rb32.robust_inact_offset;
4416 return (umtx_robust_lists(td, &rb));
4419 static const _umtx_op_func op_table_compat32[] = {
4420 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
4421 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
4422 [UMTX_OP_WAIT] = __umtx_op_wait_compat32,
4423 [UMTX_OP_WAKE] = __umtx_op_wake,
4424 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4425 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex_compat32,
4426 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4427 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4428 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait_compat32,
4429 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4430 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4431 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_compat32,
4432 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock_compat32,
4433 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock_compat32,
4434 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4435 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private_compat32,
4436 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4437 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex_compat32,
4438 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4439 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4440 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait_compat32,
4441 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4443 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4444 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4446 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private32,
4447 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4448 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait_compat32,
4449 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4450 [UMTX_OP_SHM] = __umtx_op_shm,
4451 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists_compat32,
4455 freebsd32__umtx_op(struct thread *td, struct freebsd32__umtx_op_args *uap)
4458 if ((unsigned)uap->op < nitems(op_table_compat32)) {
4459 return (*op_table_compat32[uap->op])(td,
4460 (struct _umtx_op_args *)uap);
4467 umtx_thread_init(struct thread *td)
4470 td->td_umtxq = umtxq_alloc();
4471 td->td_umtxq->uq_thread = td;
4475 umtx_thread_fini(struct thread *td)
4478 umtxq_free(td->td_umtxq);
4482 * It will be called when new thread is created, e.g fork().
4485 umtx_thread_alloc(struct thread *td)
4490 uq->uq_inherited_pri = PRI_MAX;
4492 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4493 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4494 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4495 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4501 * Clear robust lists for all process' threads, not delaying the
4502 * cleanup to thread_exit hook, since the relevant address space is
4503 * destroyed right now.
4506 umtx_exec_hook(void *arg __unused, struct proc *p,
4507 struct image_params *imgp __unused)
4511 KASSERT(p == curproc, ("need curproc"));
4512 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4513 (p->p_flag & P_STOPPED_SINGLE) != 0,
4514 ("curproc must be single-threaded"));
4516 * There is no need to lock the list as only this thread can be
4519 FOREACH_THREAD_IN_PROC(p, td) {
4520 KASSERT(td == curthread ||
4521 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4522 ("running thread %p %p", p, td));
4523 umtx_thread_cleanup(td);
4524 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4529 * thread_exit() hook.
4532 umtx_thread_exit(struct thread *td)
4535 umtx_thread_cleanup(td);
4539 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res)
4542 #ifdef COMPAT_FREEBSD32
4547 #ifdef COMPAT_FREEBSD32
4548 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4549 error = fueword32((void *)ptr, &res32);
4555 error = fueword((void *)ptr, &res1);
4565 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list)
4567 #ifdef COMPAT_FREEBSD32
4568 struct umutex32 m32;
4570 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4571 memcpy(&m32, m, sizeof(m32));
4572 *rb_list = m32.m_rb_lnk;
4575 *rb_list = m->m_rb_lnk;
4579 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact)
4584 KASSERT(td->td_proc == curproc, ("need current vmspace"));
4585 error = copyin((void *)rbp, &m, sizeof(m));
4588 if (rb_list != NULL)
4589 umtx_read_rb_list(td, &m, rb_list);
4590 if ((m.m_flags & UMUTEX_ROBUST) == 0)
4592 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
4593 /* inact is cleared after unlock, allow the inconsistency */
4594 return (inact ? 0 : EINVAL);
4595 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
4599 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
4608 error = umtx_read_uptr(td, rb_list, &rbp);
4609 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
4610 if (rbp == *rb_inact) {
4615 error = umtx_handle_rb(td, rbp, &rbp, inact);
4617 if (i == umtx_max_rb && umtx_verbose_rb) {
4618 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
4619 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
4621 if (error != 0 && umtx_verbose_rb) {
4622 uprintf("comm %s pid %d: handling %srb error %d\n",
4623 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
4628 * Clean up umtx data.
4631 umtx_thread_cleanup(struct thread *td)
4638 * Disown pi mutexes.
4642 if (uq->uq_inherited_pri != PRI_MAX ||
4643 !TAILQ_EMPTY(&uq->uq_pi_contested)) {
4644 mtx_lock(&umtx_lock);
4645 uq->uq_inherited_pri = PRI_MAX;
4646 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
4647 pi->pi_owner = NULL;
4648 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
4650 mtx_unlock(&umtx_lock);
4652 sched_lend_user_prio_cond(td, PRI_MAX);
4655 if (td->td_rb_inact == 0 && td->td_rb_list == 0 && td->td_rbp_list == 0)
4659 * Handle terminated robust mutexes. Must be done after
4660 * robust pi disown, otherwise unlock could see unowned
4663 rb_inact = td->td_rb_inact;
4665 (void)umtx_read_uptr(td, rb_inact, &rb_inact);
4666 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "");
4667 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ");
4669 (void)umtx_handle_rb(td, rb_inact, NULL, true);