2 * Copyright (c) 2015, 2016 The FreeBSD Foundation
3 * Copyright (c) 2004, David Xu <davidxu@freebsd.org>
4 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
7 * Portions of this software were developed by Konstantin Belousov
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice unmodified, this list of conditions, and the following
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
36 #include "opt_umtx_profiling.h"
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/fcntl.h>
42 #include <sys/filedesc.h>
43 #include <sys/limits.h>
45 #include <sys/malloc.h>
47 #include <sys/mutex.h>
50 #include <sys/resource.h>
51 #include <sys/resourcevar.h>
52 #include <sys/rwlock.h>
54 #include <sys/sched.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysent.h>
58 #include <sys/systm.h>
59 #include <sys/sysproto.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/taskqueue.h>
62 #include <sys/eventhandler.h>
65 #include <security/mac/mac_framework.h>
68 #include <vm/vm_param.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
73 #include <machine/cpu.h>
75 #ifdef COMPAT_FREEBSD32
76 #include <compat/freebsd32/freebsd32_proto.h>
80 #define _UMUTEX_WAIT 2
83 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
84 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
87 /* Priority inheritance mutex info. */
90 struct thread *pi_owner;
95 /* List entry to link umtx holding by thread */
96 TAILQ_ENTRY(umtx_pi) pi_link;
98 /* List entry in hash */
99 TAILQ_ENTRY(umtx_pi) pi_hashlink;
101 /* List for waiters */
102 TAILQ_HEAD(,umtx_q) pi_blocked;
104 /* Identify a userland lock object */
105 struct umtx_key pi_key;
108 /* A userland synchronous object user. */
110 /* Linked list for the hash. */
111 TAILQ_ENTRY(umtx_q) uq_link;
114 struct umtx_key uq_key;
118 #define UQF_UMTXQ 0x0001
120 /* The thread waits on. */
121 struct thread *uq_thread;
124 * Blocked on PI mutex. read can use chain lock
125 * or umtx_lock, write must have both chain lock and
126 * umtx_lock being hold.
128 struct umtx_pi *uq_pi_blocked;
130 /* On blocked list */
131 TAILQ_ENTRY(umtx_q) uq_lockq;
133 /* Thread contending with us */
134 TAILQ_HEAD(,umtx_pi) uq_pi_contested;
136 /* Inherited priority from PP mutex */
137 u_char uq_inherited_pri;
139 /* Spare queue ready to be reused */
140 struct umtxq_queue *uq_spare_queue;
142 /* The queue we on */
143 struct umtxq_queue *uq_cur_queue;
146 TAILQ_HEAD(umtxq_head, umtx_q);
148 /* Per-key wait-queue */
150 struct umtxq_head head;
152 LIST_ENTRY(umtxq_queue) link;
156 LIST_HEAD(umtxq_list, umtxq_queue);
158 /* Userland lock object's wait-queue chain */
160 /* Lock for this chain. */
163 /* List of sleep queues. */
164 struct umtxq_list uc_queue[2];
165 #define UMTX_SHARED_QUEUE 0
166 #define UMTX_EXCLUSIVE_QUEUE 1
168 LIST_HEAD(, umtxq_queue) uc_spare_queue;
173 /* Chain lock waiters */
176 /* All PI in the list */
177 TAILQ_HEAD(,umtx_pi) uc_pi_list;
179 #ifdef UMTX_PROFILING
185 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
188 * Don't propagate time-sharing priority, there is a security reason,
189 * a user can simply introduce PI-mutex, let thread A lock the mutex,
190 * and let another thread B block on the mutex, because B is
191 * sleeping, its priority will be boosted, this causes A's priority to
192 * be boosted via priority propagating too and will never be lowered even
193 * if it is using 100%CPU, this is unfair to other processes.
196 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
197 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
198 PRI_MAX_TIMESHARE : (td)->td_user_pri)
200 #define GOLDEN_RATIO_PRIME 2654404609U
202 #define UMTX_CHAINS 512
204 #define UMTX_SHIFTS (__WORD_BIT - 9)
206 #define GET_SHARE(flags) \
207 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
209 #define BUSY_SPINS 200
217 #ifdef COMPAT_FREEBSD32
219 volatile __lwpid_t m_owner; /* Owner of the mutex */
220 __uint32_t m_flags; /* Flags of the mutex */
221 __uint32_t m_ceilings[2]; /* Priority protect ceiling */
222 __uint32_t m_rb_lnk; /* Robust linkage */
224 __uint32_t m_spare[2];
227 _Static_assert(sizeof(struct umutex) == sizeof(struct umutex32), "umutex32");
228 _Static_assert(__offsetof(struct umutex, m_spare[0]) ==
229 __offsetof(struct umutex32, m_spare[0]), "m_spare32");
232 int umtx_shm_vnobj_persistent = 0;
233 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_vnode_persistent, CTLFLAG_RWTUN,
234 &umtx_shm_vnobj_persistent, 0,
235 "False forces destruction of umtx attached to file, on last close");
236 static int umtx_max_rb = 1000;
237 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_max_robust, CTLFLAG_RWTUN,
241 static uma_zone_t umtx_pi_zone;
242 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
243 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
244 static int umtx_pi_allocated;
246 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW, 0, "umtx debug");
247 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
248 &umtx_pi_allocated, 0, "Allocated umtx_pi");
249 static int umtx_verbose_rb = 1;
250 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
254 #ifdef UMTX_PROFILING
255 static long max_length;
256 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
257 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD, 0, "umtx chain stats");
260 static void umtx_shm_init(void);
261 static void umtxq_sysinit(void *);
262 static void umtxq_hash(struct umtx_key *key);
263 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
264 static void umtxq_lock(struct umtx_key *key);
265 static void umtxq_unlock(struct umtx_key *key);
266 static void umtxq_busy(struct umtx_key *key);
267 static void umtxq_unbusy(struct umtx_key *key);
268 static void umtxq_insert_queue(struct umtx_q *uq, int q);
269 static void umtxq_remove_queue(struct umtx_q *uq, int q);
270 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *);
271 static int umtxq_count(struct umtx_key *key);
272 static struct umtx_pi *umtx_pi_alloc(int);
273 static void umtx_pi_free(struct umtx_pi *pi);
274 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
276 static void umtx_thread_cleanup(struct thread *td);
277 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
278 struct image_params *imgp __unused);
279 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
281 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
282 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
283 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
285 static struct mtx umtx_lock;
287 #ifdef UMTX_PROFILING
289 umtx_init_profiling(void)
291 struct sysctl_oid *chain_oid;
295 for (i = 0; i < UMTX_CHAINS; ++i) {
296 snprintf(chain_name, sizeof(chain_name), "%d", i);
297 chain_oid = SYSCTL_ADD_NODE(NULL,
298 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
299 chain_name, CTLFLAG_RD, NULL, "umtx hash stats");
300 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
301 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
302 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
303 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
308 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
312 struct umtxq_chain *uc;
313 u_int fract, i, j, tot, whole;
314 u_int sf0, sf1, sf2, sf3, sf4;
315 u_int si0, si1, si2, si3, si4;
316 u_int sw0, sw1, sw2, sw3, sw4;
318 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
319 for (i = 0; i < 2; i++) {
321 for (j = 0; j < UMTX_CHAINS; ++j) {
322 uc = &umtxq_chains[i][j];
323 mtx_lock(&uc->uc_lock);
324 tot += uc->max_length;
325 mtx_unlock(&uc->uc_lock);
328 sbuf_printf(&sb, "%u) Empty ", i);
330 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
331 si0 = si1 = si2 = si3 = si4 = 0;
332 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
333 for (j = 0; j < UMTX_CHAINS; j++) {
334 uc = &umtxq_chains[i][j];
335 mtx_lock(&uc->uc_lock);
336 whole = uc->max_length * 100;
337 mtx_unlock(&uc->uc_lock);
338 fract = (whole % tot) * 100;
339 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
343 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
348 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
353 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
358 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
365 sbuf_printf(&sb, "queue %u:\n", i);
366 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
368 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
370 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
372 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
374 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
380 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
386 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
388 struct umtxq_chain *uc;
393 error = sysctl_handle_int(oidp, &clear, 0, req);
394 if (error != 0 || req->newptr == NULL)
398 for (i = 0; i < 2; ++i) {
399 for (j = 0; j < UMTX_CHAINS; ++j) {
400 uc = &umtxq_chains[i][j];
401 mtx_lock(&uc->uc_lock);
404 mtx_unlock(&uc->uc_lock);
411 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
412 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
413 sysctl_debug_umtx_chains_clear, "I", "Clear umtx chains statistics");
414 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
415 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
416 sysctl_debug_umtx_chains_peaks, "A", "Highest peaks in chains max length");
420 umtxq_sysinit(void *arg __unused)
424 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
425 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
426 for (i = 0; i < 2; ++i) {
427 for (j = 0; j < UMTX_CHAINS; ++j) {
428 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
429 MTX_DEF | MTX_DUPOK);
430 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
431 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
432 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
433 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
434 umtxq_chains[i][j].uc_busy = 0;
435 umtxq_chains[i][j].uc_waiters = 0;
436 #ifdef UMTX_PROFILING
437 umtxq_chains[i][j].length = 0;
438 umtxq_chains[i][j].max_length = 0;
442 #ifdef UMTX_PROFILING
443 umtx_init_profiling();
445 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
446 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
447 EVENTHANDLER_PRI_ANY);
456 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
457 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
459 TAILQ_INIT(&uq->uq_spare_queue->head);
460 TAILQ_INIT(&uq->uq_pi_contested);
461 uq->uq_inherited_pri = PRI_MAX;
466 umtxq_free(struct umtx_q *uq)
469 MPASS(uq->uq_spare_queue != NULL);
470 free(uq->uq_spare_queue, M_UMTX);
475 umtxq_hash(struct umtx_key *key)
479 n = (uintptr_t)key->info.both.a + key->info.both.b;
480 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
483 static inline struct umtxq_chain *
484 umtxq_getchain(struct umtx_key *key)
487 if (key->type <= TYPE_SEM)
488 return (&umtxq_chains[1][key->hash]);
489 return (&umtxq_chains[0][key->hash]);
496 umtxq_lock(struct umtx_key *key)
498 struct umtxq_chain *uc;
500 uc = umtxq_getchain(key);
501 mtx_lock(&uc->uc_lock);
508 umtxq_unlock(struct umtx_key *key)
510 struct umtxq_chain *uc;
512 uc = umtxq_getchain(key);
513 mtx_unlock(&uc->uc_lock);
517 * Set chain to busy state when following operation
518 * may be blocked (kernel mutex can not be used).
521 umtxq_busy(struct umtx_key *key)
523 struct umtxq_chain *uc;
525 uc = umtxq_getchain(key);
526 mtx_assert(&uc->uc_lock, MA_OWNED);
530 int count = BUSY_SPINS;
533 while (uc->uc_busy && --count > 0)
539 while (uc->uc_busy) {
541 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
552 umtxq_unbusy(struct umtx_key *key)
554 struct umtxq_chain *uc;
556 uc = umtxq_getchain(key);
557 mtx_assert(&uc->uc_lock, MA_OWNED);
558 KASSERT(uc->uc_busy != 0, ("not busy"));
565 umtxq_unbusy_unlocked(struct umtx_key *key)
573 static struct umtxq_queue *
574 umtxq_queue_lookup(struct umtx_key *key, int q)
576 struct umtxq_queue *uh;
577 struct umtxq_chain *uc;
579 uc = umtxq_getchain(key);
580 UMTXQ_LOCKED_ASSERT(uc);
581 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
582 if (umtx_key_match(&uh->key, key))
590 umtxq_insert_queue(struct umtx_q *uq, int q)
592 struct umtxq_queue *uh;
593 struct umtxq_chain *uc;
595 uc = umtxq_getchain(&uq->uq_key);
596 UMTXQ_LOCKED_ASSERT(uc);
597 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
598 uh = umtxq_queue_lookup(&uq->uq_key, q);
600 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
602 uh = uq->uq_spare_queue;
603 uh->key = uq->uq_key;
604 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
605 #ifdef UMTX_PROFILING
607 if (uc->length > uc->max_length) {
608 uc->max_length = uc->length;
609 if (uc->max_length > max_length)
610 max_length = uc->max_length;
614 uq->uq_spare_queue = NULL;
616 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
618 uq->uq_flags |= UQF_UMTXQ;
619 uq->uq_cur_queue = uh;
624 umtxq_remove_queue(struct umtx_q *uq, int q)
626 struct umtxq_chain *uc;
627 struct umtxq_queue *uh;
629 uc = umtxq_getchain(&uq->uq_key);
630 UMTXQ_LOCKED_ASSERT(uc);
631 if (uq->uq_flags & UQF_UMTXQ) {
632 uh = uq->uq_cur_queue;
633 TAILQ_REMOVE(&uh->head, uq, uq_link);
635 uq->uq_flags &= ~UQF_UMTXQ;
636 if (TAILQ_EMPTY(&uh->head)) {
637 KASSERT(uh->length == 0,
638 ("inconsistent umtxq_queue length"));
639 #ifdef UMTX_PROFILING
642 LIST_REMOVE(uh, link);
644 uh = LIST_FIRST(&uc->uc_spare_queue);
645 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
646 LIST_REMOVE(uh, link);
648 uq->uq_spare_queue = uh;
649 uq->uq_cur_queue = NULL;
654 * Check if there are multiple waiters
657 umtxq_count(struct umtx_key *key)
659 struct umtxq_chain *uc;
660 struct umtxq_queue *uh;
662 uc = umtxq_getchain(key);
663 UMTXQ_LOCKED_ASSERT(uc);
664 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
671 * Check if there are multiple PI waiters and returns first
675 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
677 struct umtxq_chain *uc;
678 struct umtxq_queue *uh;
681 uc = umtxq_getchain(key);
682 UMTXQ_LOCKED_ASSERT(uc);
683 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
685 *first = TAILQ_FIRST(&uh->head);
692 umtxq_check_susp(struct thread *td)
698 * The check for TDF_NEEDSUSPCHK is racy, but it is enough to
699 * eventually break the lockstep loop.
701 if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
706 if (P_SHOULDSTOP(p) ||
707 ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) {
708 if (p->p_flag & P_SINGLE_EXIT)
718 * Wake up threads waiting on an userland object.
722 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
724 struct umtxq_chain *uc;
725 struct umtxq_queue *uh;
730 uc = umtxq_getchain(key);
731 UMTXQ_LOCKED_ASSERT(uc);
732 uh = umtxq_queue_lookup(key, q);
734 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
735 umtxq_remove_queue(uq, q);
746 * Wake up specified thread.
749 umtxq_signal_thread(struct umtx_q *uq)
751 struct umtxq_chain *uc;
753 uc = umtxq_getchain(&uq->uq_key);
754 UMTXQ_LOCKED_ASSERT(uc);
760 tstohz(const struct timespec *tsp)
764 TIMESPEC_TO_TIMEVAL(&tv, tsp);
769 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
770 const struct timespec *timeout)
773 timo->clockid = clockid;
775 kern_clock_gettime(curthread, clockid, &timo->end);
776 timo->cur = timo->end;
777 timespecadd(&timo->end, timeout);
779 timo->end = *timeout;
780 kern_clock_gettime(curthread, clockid, &timo->cur);
785 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
788 abs_timeout_init(timo, umtxtime->_clockid,
789 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
793 abs_timeout_update(struct abs_timeout *timo)
796 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
800 abs_timeout_gethz(struct abs_timeout *timo)
804 if (timespeccmp(&timo->end, &timo->cur, <=))
807 timespecsub(&tts, &timo->cur);
808 return (tstohz(&tts));
812 umtx_unlock_val(uint32_t flags, bool rb)
816 return (UMUTEX_RB_OWNERDEAD);
817 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
818 return (UMUTEX_RB_NOTRECOV);
820 return (UMUTEX_UNOWNED);
825 * Put thread into sleep state, before sleeping, check if
826 * thread was removed from umtx queue.
829 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
831 struct umtxq_chain *uc;
834 uc = umtxq_getchain(&uq->uq_key);
835 UMTXQ_LOCKED_ASSERT(uc);
837 if (!(uq->uq_flags & UQF_UMTXQ))
839 if (abstime != NULL) {
840 timo = abs_timeout_gethz(abstime);
845 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
846 if (error != EWOULDBLOCK) {
847 umtxq_lock(&uq->uq_key);
851 abs_timeout_update(abstime);
852 umtxq_lock(&uq->uq_key);
858 * Convert userspace address into unique logical address.
861 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
863 struct thread *td = curthread;
865 vm_map_entry_t entry;
871 if (share == THREAD_SHARE) {
873 key->info.private.vs = td->td_proc->p_vmspace;
874 key->info.private.addr = (uintptr_t)addr;
876 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
877 map = &td->td_proc->p_vmspace->vm_map;
878 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
879 &entry, &key->info.shared.object, &pindex, &prot,
880 &wired) != KERN_SUCCESS) {
884 if ((share == PROCESS_SHARE) ||
885 (share == AUTO_SHARE &&
886 VM_INHERIT_SHARE == entry->inheritance)) {
888 key->info.shared.offset = (vm_offset_t)addr -
889 entry->start + entry->offset;
890 vm_object_reference(key->info.shared.object);
893 key->info.private.vs = td->td_proc->p_vmspace;
894 key->info.private.addr = (uintptr_t)addr;
896 vm_map_lookup_done(map, entry);
907 umtx_key_release(struct umtx_key *key)
910 vm_object_deallocate(key->info.shared.object);
914 * Fetch and compare value, sleep on the address if value is not changed.
917 do_wait(struct thread *td, void *addr, u_long id,
918 struct _umtx_time *timeout, int compat32, int is_private)
920 struct abs_timeout timo;
927 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
928 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
932 abs_timeout_init2(&timo, timeout);
934 umtxq_lock(&uq->uq_key);
936 umtxq_unlock(&uq->uq_key);
938 error = fueword(addr, &tmp);
942 error = fueword32(addr, &tmp32);
948 umtxq_lock(&uq->uq_key);
951 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
953 if ((uq->uq_flags & UQF_UMTXQ) == 0)
957 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
960 umtxq_unlock(&uq->uq_key);
961 umtx_key_release(&uq->uq_key);
962 if (error == ERESTART)
968 * Wake up threads sleeping on the specified address.
971 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
976 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
977 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
980 umtxq_signal(&key, n_wake);
982 umtx_key_release(&key);
987 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
990 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
991 struct _umtx_time *timeout, int mode)
993 struct abs_timeout timo;
995 uint32_t owner, old, id;
1001 if (timeout != NULL)
1002 abs_timeout_init2(&timo, timeout);
1005 * Care must be exercised when dealing with umtx structure. It
1006 * can fault on any access.
1009 rv = fueword32(&m->m_owner, &owner);
1012 if (mode == _UMUTEX_WAIT) {
1013 if (owner == UMUTEX_UNOWNED ||
1014 owner == UMUTEX_CONTESTED ||
1015 owner == UMUTEX_RB_OWNERDEAD ||
1016 owner == UMUTEX_RB_NOTRECOV)
1020 * Robust mutex terminated. Kernel duty is to
1021 * return EOWNERDEAD to the userspace. The
1022 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1023 * by the common userspace code.
1025 if (owner == UMUTEX_RB_OWNERDEAD) {
1026 rv = casueword32(&m->m_owner,
1027 UMUTEX_RB_OWNERDEAD, &owner,
1028 id | UMUTEX_CONTESTED);
1031 if (owner == UMUTEX_RB_OWNERDEAD)
1032 return (EOWNERDEAD); /* success */
1033 rv = umtxq_check_susp(td);
1038 if (owner == UMUTEX_RB_NOTRECOV)
1039 return (ENOTRECOVERABLE);
1043 * Try the uncontested case. This should be
1046 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1048 /* The address was invalid. */
1052 /* The acquire succeeded. */
1053 if (owner == UMUTEX_UNOWNED)
1057 * 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 if (owner == UMUTEX_CONTESTED)
1071 rv = umtxq_check_susp(td);
1076 * If this failed the lock has
1083 if (mode == _UMUTEX_TRY)
1087 * If we caught a signal, we have retried and now
1093 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1094 GET_SHARE(flags), &uq->uq_key)) != 0)
1097 umtxq_lock(&uq->uq_key);
1098 umtxq_busy(&uq->uq_key);
1100 umtxq_unlock(&uq->uq_key);
1103 * Set the contested bit so that a release in user space
1104 * knows to use the system call for unlock. If this fails
1105 * either some one else has acquired the lock or it has been
1108 rv = casueword32(&m->m_owner, owner, &old,
1109 owner | UMUTEX_CONTESTED);
1111 /* The address was invalid. */
1113 umtxq_lock(&uq->uq_key);
1115 umtxq_unbusy(&uq->uq_key);
1116 umtxq_unlock(&uq->uq_key);
1117 umtx_key_release(&uq->uq_key);
1122 * We set the contested bit, sleep. Otherwise the lock changed
1123 * and we need to retry or we lost a race to the thread
1124 * unlocking the umtx.
1126 umtxq_lock(&uq->uq_key);
1127 umtxq_unbusy(&uq->uq_key);
1129 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1132 umtxq_unlock(&uq->uq_key);
1133 umtx_key_release(&uq->uq_key);
1136 error = umtxq_check_susp(td);
1143 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1146 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1148 struct umtx_key key;
1149 uint32_t owner, old, id, newlock;
1154 * Make sure we own this mtx.
1156 error = fueword32(&m->m_owner, &owner);
1160 if ((owner & ~UMUTEX_CONTESTED) != id)
1163 newlock = umtx_unlock_val(flags, rb);
1164 if ((owner & UMUTEX_CONTESTED) == 0) {
1165 error = casueword32(&m->m_owner, owner, &old, newlock);
1173 /* We should only ever be in here for contested locks */
1174 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1180 count = umtxq_count(&key);
1184 * When unlocking the umtx, it must be marked as unowned if
1185 * there is zero or one thread only waiting for it.
1186 * Otherwise, it must be marked as contested.
1189 newlock |= UMUTEX_CONTESTED;
1190 error = casueword32(&m->m_owner, owner, &old, newlock);
1192 umtxq_signal(&key, 1);
1195 umtx_key_release(&key);
1204 * Check if the mutex is available and wake up a waiter,
1205 * only for simple mutex.
1208 do_wake_umutex(struct thread *td, struct umutex *m)
1210 struct umtx_key key;
1216 error = fueword32(&m->m_owner, &owner);
1220 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1221 owner != UMUTEX_RB_NOTRECOV)
1224 error = fueword32(&m->m_flags, &flags);
1228 /* We should only ever be in here for contested locks */
1229 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1235 count = umtxq_count(&key);
1238 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1239 owner != UMUTEX_RB_NOTRECOV) {
1240 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1247 if (error == 0 && count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1248 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1249 umtxq_signal(&key, 1);
1252 umtx_key_release(&key);
1257 * Check if the mutex has waiters and tries to fix contention bit.
1260 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1262 struct umtx_key key;
1263 uint32_t owner, old;
1268 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1272 type = TYPE_NORMAL_UMUTEX;
1274 case UMUTEX_PRIO_INHERIT:
1275 type = TYPE_PI_UMUTEX;
1277 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1278 type = TYPE_PI_ROBUST_UMUTEX;
1280 case UMUTEX_PRIO_PROTECT:
1281 type = TYPE_PP_UMUTEX;
1283 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1284 type = TYPE_PP_ROBUST_UMUTEX;
1289 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1295 count = umtxq_count(&key);
1298 * Only repair contention bit if there is a waiter, this means the mutex
1299 * is still being referenced by userland code, otherwise don't update
1303 error = fueword32(&m->m_owner, &owner);
1306 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0) {
1307 error = casueword32(&m->m_owner, owner, &old,
1308 owner | UMUTEX_CONTESTED);
1316 error = umtxq_check_susp(td);
1320 } else if (count == 1) {
1321 error = fueword32(&m->m_owner, &owner);
1324 while (error == 0 && (owner & ~UMUTEX_CONTESTED) != 0 &&
1325 (owner & UMUTEX_CONTESTED) == 0) {
1326 error = casueword32(&m->m_owner, owner, &old,
1327 owner | UMUTEX_CONTESTED);
1335 error = umtxq_check_susp(td);
1341 if (error == EFAULT) {
1342 umtxq_signal(&key, INT_MAX);
1343 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1344 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1345 umtxq_signal(&key, 1);
1348 umtx_key_release(&key);
1352 static inline struct umtx_pi *
1353 umtx_pi_alloc(int flags)
1357 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1358 TAILQ_INIT(&pi->pi_blocked);
1359 atomic_add_int(&umtx_pi_allocated, 1);
1364 umtx_pi_free(struct umtx_pi *pi)
1366 uma_zfree(umtx_pi_zone, pi);
1367 atomic_add_int(&umtx_pi_allocated, -1);
1371 * Adjust the thread's position on a pi_state after its priority has been
1375 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1377 struct umtx_q *uq, *uq1, *uq2;
1380 mtx_assert(&umtx_lock, MA_OWNED);
1387 * Check if the thread needs to be moved on the blocked chain.
1388 * It needs to be moved if either its priority is lower than
1389 * the previous thread or higher than the next thread.
1391 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1392 uq2 = TAILQ_NEXT(uq, uq_lockq);
1393 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1394 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1396 * Remove thread from blocked chain and determine where
1397 * it should be moved to.
1399 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1400 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1401 td1 = uq1->uq_thread;
1402 MPASS(td1->td_proc->p_magic == P_MAGIC);
1403 if (UPRI(td1) > UPRI(td))
1408 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1410 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1415 static struct umtx_pi *
1416 umtx_pi_next(struct umtx_pi *pi)
1418 struct umtx_q *uq_owner;
1420 if (pi->pi_owner == NULL)
1422 uq_owner = pi->pi_owner->td_umtxq;
1423 if (uq_owner == NULL)
1425 return (uq_owner->uq_pi_blocked);
1429 * Floyd's Cycle-Finding Algorithm.
1432 umtx_pi_check_loop(struct umtx_pi *pi)
1434 struct umtx_pi *pi1; /* fast iterator */
1436 mtx_assert(&umtx_lock, MA_OWNED);
1441 pi = umtx_pi_next(pi);
1444 pi1 = umtx_pi_next(pi1);
1447 pi1 = umtx_pi_next(pi1);
1457 * Propagate priority when a thread is blocked on POSIX
1461 umtx_propagate_priority(struct thread *td)
1467 mtx_assert(&umtx_lock, MA_OWNED);
1470 pi = uq->uq_pi_blocked;
1473 if (umtx_pi_check_loop(pi))
1478 if (td == NULL || td == curthread)
1481 MPASS(td->td_proc != NULL);
1482 MPASS(td->td_proc->p_magic == P_MAGIC);
1485 if (td->td_lend_user_pri > pri)
1486 sched_lend_user_prio(td, pri);
1494 * Pick up the lock that td is blocked on.
1497 pi = uq->uq_pi_blocked;
1500 /* Resort td on the list if needed. */
1501 umtx_pi_adjust_thread(pi, td);
1506 * Unpropagate priority for a PI mutex when a thread blocked on
1507 * it is interrupted by signal or resumed by others.
1510 umtx_repropagate_priority(struct umtx_pi *pi)
1512 struct umtx_q *uq, *uq_owner;
1513 struct umtx_pi *pi2;
1516 mtx_assert(&umtx_lock, MA_OWNED);
1518 if (umtx_pi_check_loop(pi))
1520 while (pi != NULL && pi->pi_owner != NULL) {
1522 uq_owner = pi->pi_owner->td_umtxq;
1524 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1525 uq = TAILQ_FIRST(&pi2->pi_blocked);
1527 if (pri > UPRI(uq->uq_thread))
1528 pri = UPRI(uq->uq_thread);
1532 if (pri > uq_owner->uq_inherited_pri)
1533 pri = uq_owner->uq_inherited_pri;
1534 thread_lock(pi->pi_owner);
1535 sched_lend_user_prio(pi->pi_owner, pri);
1536 thread_unlock(pi->pi_owner);
1537 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1538 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1543 * Insert a PI mutex into owned list.
1546 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1548 struct umtx_q *uq_owner;
1550 uq_owner = owner->td_umtxq;
1551 mtx_assert(&umtx_lock, MA_OWNED);
1552 if (pi->pi_owner != NULL)
1553 panic("pi_owner != NULL");
1554 pi->pi_owner = owner;
1555 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1560 * Disown a PI mutex, and remove it from the owned list.
1563 umtx_pi_disown(struct umtx_pi *pi)
1566 mtx_assert(&umtx_lock, MA_OWNED);
1567 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1568 pi->pi_owner = NULL;
1572 * Claim ownership of a PI mutex.
1575 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1580 mtx_lock(&umtx_lock);
1581 if (pi->pi_owner == owner) {
1582 mtx_unlock(&umtx_lock);
1586 if (pi->pi_owner != NULL) {
1588 * userland may have already messed the mutex, sigh.
1590 mtx_unlock(&umtx_lock);
1593 umtx_pi_setowner(pi, owner);
1594 uq = TAILQ_FIRST(&pi->pi_blocked);
1596 pri = UPRI(uq->uq_thread);
1598 if (pri < UPRI(owner))
1599 sched_lend_user_prio(owner, pri);
1600 thread_unlock(owner);
1602 mtx_unlock(&umtx_lock);
1607 * Adjust a thread's order position in its blocked PI mutex,
1608 * this may result new priority propagating process.
1611 umtx_pi_adjust(struct thread *td, u_char oldpri)
1617 mtx_lock(&umtx_lock);
1619 * Pick up the lock that td is blocked on.
1621 pi = uq->uq_pi_blocked;
1623 umtx_pi_adjust_thread(pi, td);
1624 umtx_repropagate_priority(pi);
1626 mtx_unlock(&umtx_lock);
1630 * Sleep on a PI mutex.
1633 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
1634 const char *wmesg, struct abs_timeout *timo, bool shared)
1636 struct umtxq_chain *uc;
1637 struct thread *td, *td1;
1643 KASSERT(td == curthread, ("inconsistent uq_thread"));
1644 uc = umtxq_getchain(&uq->uq_key);
1645 UMTXQ_LOCKED_ASSERT(uc);
1646 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1648 mtx_lock(&umtx_lock);
1649 if (pi->pi_owner == NULL) {
1650 mtx_unlock(&umtx_lock);
1651 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
1652 mtx_lock(&umtx_lock);
1654 if (pi->pi_owner == NULL)
1655 umtx_pi_setowner(pi, td1);
1656 PROC_UNLOCK(td1->td_proc);
1660 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1661 pri = UPRI(uq1->uq_thread);
1667 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1669 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1671 uq->uq_pi_blocked = pi;
1673 td->td_flags |= TDF_UPIBLOCKED;
1675 umtx_propagate_priority(td);
1676 mtx_unlock(&umtx_lock);
1677 umtxq_unbusy(&uq->uq_key);
1679 error = umtxq_sleep(uq, wmesg, timo);
1682 mtx_lock(&umtx_lock);
1683 uq->uq_pi_blocked = NULL;
1685 td->td_flags &= ~TDF_UPIBLOCKED;
1687 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1688 umtx_repropagate_priority(pi);
1689 mtx_unlock(&umtx_lock);
1690 umtxq_unlock(&uq->uq_key);
1696 * Add reference count for a PI mutex.
1699 umtx_pi_ref(struct umtx_pi *pi)
1701 struct umtxq_chain *uc;
1703 uc = umtxq_getchain(&pi->pi_key);
1704 UMTXQ_LOCKED_ASSERT(uc);
1709 * Decrease reference count for a PI mutex, if the counter
1710 * is decreased to zero, its memory space is freed.
1713 umtx_pi_unref(struct umtx_pi *pi)
1715 struct umtxq_chain *uc;
1717 uc = umtxq_getchain(&pi->pi_key);
1718 UMTXQ_LOCKED_ASSERT(uc);
1719 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1720 if (--pi->pi_refcount == 0) {
1721 mtx_lock(&umtx_lock);
1722 if (pi->pi_owner != NULL)
1724 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1725 ("blocked queue not empty"));
1726 mtx_unlock(&umtx_lock);
1727 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1733 * Find a PI mutex in hash table.
1735 static struct umtx_pi *
1736 umtx_pi_lookup(struct umtx_key *key)
1738 struct umtxq_chain *uc;
1741 uc = umtxq_getchain(key);
1742 UMTXQ_LOCKED_ASSERT(uc);
1744 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1745 if (umtx_key_match(&pi->pi_key, key)) {
1753 * Insert a PI mutex into hash table.
1756 umtx_pi_insert(struct umtx_pi *pi)
1758 struct umtxq_chain *uc;
1760 uc = umtxq_getchain(&pi->pi_key);
1761 UMTXQ_LOCKED_ASSERT(uc);
1762 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1769 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1770 struct _umtx_time *timeout, int try)
1772 struct abs_timeout timo;
1774 struct umtx_pi *pi, *new_pi;
1775 uint32_t id, old_owner, owner, old;
1781 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
1782 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
1786 if (timeout != NULL)
1787 abs_timeout_init2(&timo, timeout);
1789 umtxq_lock(&uq->uq_key);
1790 pi = umtx_pi_lookup(&uq->uq_key);
1792 new_pi = umtx_pi_alloc(M_NOWAIT);
1793 if (new_pi == NULL) {
1794 umtxq_unlock(&uq->uq_key);
1795 new_pi = umtx_pi_alloc(M_WAITOK);
1796 umtxq_lock(&uq->uq_key);
1797 pi = umtx_pi_lookup(&uq->uq_key);
1799 umtx_pi_free(new_pi);
1803 if (new_pi != NULL) {
1804 new_pi->pi_key = uq->uq_key;
1805 umtx_pi_insert(new_pi);
1810 umtxq_unlock(&uq->uq_key);
1813 * Care must be exercised when dealing with umtx structure. It
1814 * can fault on any access.
1818 * Try the uncontested case. This should be done in userland.
1820 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
1821 /* The address was invalid. */
1827 /* The acquire succeeded. */
1828 if (owner == UMUTEX_UNOWNED) {
1833 if (owner == UMUTEX_RB_NOTRECOV) {
1834 error = ENOTRECOVERABLE;
1838 /* If no one owns it but it is contested try to acquire it. */
1839 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
1841 rv = casueword32(&m->m_owner, owner, &owner,
1842 id | UMUTEX_CONTESTED);
1843 /* The address was invalid. */
1849 if (owner == old_owner) {
1850 umtxq_lock(&uq->uq_key);
1851 umtxq_busy(&uq->uq_key);
1852 error = umtx_pi_claim(pi, td);
1853 umtxq_unbusy(&uq->uq_key);
1854 umtxq_unlock(&uq->uq_key);
1857 * Since we're going to return an
1858 * error, restore the m_owner to its
1859 * previous, unowned state to avoid
1860 * compounding the problem.
1862 (void)casuword32(&m->m_owner,
1863 id | UMUTEX_CONTESTED,
1867 old_owner == UMUTEX_RB_OWNERDEAD)
1872 error = umtxq_check_susp(td);
1876 /* If this failed the lock has changed, restart. */
1880 if ((owner & ~UMUTEX_CONTESTED) == id) {
1891 * If we caught a signal, we have retried and now
1897 umtxq_lock(&uq->uq_key);
1898 umtxq_busy(&uq->uq_key);
1899 umtxq_unlock(&uq->uq_key);
1902 * Set the contested bit so that a release in user space
1903 * knows to use the system call for unlock. If this fails
1904 * either some one else has acquired the lock or it has been
1907 rv = casueword32(&m->m_owner, owner, &old, owner |
1910 /* The address was invalid. */
1912 umtxq_unbusy_unlocked(&uq->uq_key);
1917 umtxq_lock(&uq->uq_key);
1919 * We set the contested bit, sleep. Otherwise the lock changed
1920 * and we need to retry or we lost a race to the thread
1921 * unlocking the umtx. Note that the UMUTEX_RB_OWNERDEAD
1922 * value for owner is impossible there.
1925 error = umtxq_sleep_pi(uq, pi,
1926 owner & ~UMUTEX_CONTESTED,
1927 "umtxpi", timeout == NULL ? NULL : &timo,
1928 (flags & USYNC_PROCESS_SHARED) != 0);
1932 umtxq_unbusy(&uq->uq_key);
1933 umtxq_unlock(&uq->uq_key);
1936 error = umtxq_check_susp(td);
1941 umtxq_lock(&uq->uq_key);
1943 umtxq_unlock(&uq->uq_key);
1945 umtx_key_release(&uq->uq_key);
1950 * Unlock a PI mutex.
1953 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1955 struct umtx_key key;
1956 struct umtx_q *uq_first, *uq_first2, *uq_me;
1957 struct umtx_pi *pi, *pi2;
1958 uint32_t id, new_owner, old, owner;
1959 int count, error, pri;
1963 * Make sure we own this mtx.
1965 error = fueword32(&m->m_owner, &owner);
1969 if ((owner & ~UMUTEX_CONTESTED) != id)
1972 new_owner = umtx_unlock_val(flags, rb);
1974 /* This should be done in userland */
1975 if ((owner & UMUTEX_CONTESTED) == 0) {
1976 error = casueword32(&m->m_owner, owner, &old, new_owner);
1984 /* We should only ever be in here for contested locks */
1985 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
1986 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
1992 count = umtxq_count_pi(&key, &uq_first);
1993 if (uq_first != NULL) {
1994 mtx_lock(&umtx_lock);
1995 pi = uq_first->uq_pi_blocked;
1996 KASSERT(pi != NULL, ("pi == NULL?"));
1997 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
1998 mtx_unlock(&umtx_lock);
2001 umtx_key_release(&key);
2002 /* userland messed the mutex */
2005 uq_me = td->td_umtxq;
2006 if (pi->pi_owner == td)
2008 /* get highest priority thread which is still sleeping. */
2009 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2010 while (uq_first != NULL &&
2011 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2012 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2015 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2016 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2017 if (uq_first2 != NULL) {
2018 if (pri > UPRI(uq_first2->uq_thread))
2019 pri = UPRI(uq_first2->uq_thread);
2023 sched_lend_user_prio(td, pri);
2025 mtx_unlock(&umtx_lock);
2027 umtxq_signal_thread(uq_first);
2029 pi = umtx_pi_lookup(&key);
2031 * A umtx_pi can exist if a signal or timeout removed the
2032 * last waiter from the umtxq, but there is still
2033 * a thread in do_lock_pi() holding the umtx_pi.
2037 * The umtx_pi can be unowned, such as when a thread
2038 * has just entered do_lock_pi(), allocated the
2039 * umtx_pi, and unlocked the umtxq.
2040 * If the current thread owns it, it must disown it.
2042 mtx_lock(&umtx_lock);
2043 if (pi->pi_owner == td)
2045 mtx_unlock(&umtx_lock);
2051 * When unlocking the umtx, it must be marked as unowned if
2052 * there is zero or one thread only waiting for it.
2053 * Otherwise, it must be marked as contested.
2057 new_owner |= UMUTEX_CONTESTED;
2058 error = casueword32(&m->m_owner, owner, &old, new_owner);
2060 umtxq_unbusy_unlocked(&key);
2061 umtx_key_release(&key);
2073 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2074 struct _umtx_time *timeout, int try)
2076 struct abs_timeout timo;
2077 struct umtx_q *uq, *uq2;
2081 int error, pri, old_inherited_pri, su, rv;
2085 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2086 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2090 if (timeout != NULL)
2091 abs_timeout_init2(&timo, timeout);
2093 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2095 old_inherited_pri = uq->uq_inherited_pri;
2096 umtxq_lock(&uq->uq_key);
2097 umtxq_busy(&uq->uq_key);
2098 umtxq_unlock(&uq->uq_key);
2100 rv = fueword32(&m->m_ceilings[0], &ceiling);
2105 ceiling = RTP_PRIO_MAX - ceiling;
2106 if (ceiling > RTP_PRIO_MAX) {
2111 mtx_lock(&umtx_lock);
2112 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2113 mtx_unlock(&umtx_lock);
2117 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2118 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2120 if (uq->uq_inherited_pri < UPRI(td))
2121 sched_lend_user_prio(td, uq->uq_inherited_pri);
2124 mtx_unlock(&umtx_lock);
2126 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2127 id | UMUTEX_CONTESTED);
2128 /* The address was invalid. */
2134 if (owner == UMUTEX_CONTESTED) {
2137 } else if (owner == UMUTEX_RB_OWNERDEAD) {
2138 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2139 &owner, id | UMUTEX_CONTESTED);
2144 if (owner == UMUTEX_RB_OWNERDEAD) {
2145 error = EOWNERDEAD; /* success */
2149 } else if (owner == UMUTEX_RB_NOTRECOV) {
2150 error = ENOTRECOVERABLE;
2160 * If we caught a signal, we have retried and now
2166 umtxq_lock(&uq->uq_key);
2168 umtxq_unbusy(&uq->uq_key);
2169 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2172 umtxq_unlock(&uq->uq_key);
2174 mtx_lock(&umtx_lock);
2175 uq->uq_inherited_pri = old_inherited_pri;
2177 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2178 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2180 if (pri > UPRI(uq2->uq_thread))
2181 pri = UPRI(uq2->uq_thread);
2184 if (pri > uq->uq_inherited_pri)
2185 pri = uq->uq_inherited_pri;
2187 sched_lend_user_prio(td, pri);
2189 mtx_unlock(&umtx_lock);
2192 if (error != 0 && error != EOWNERDEAD) {
2193 mtx_lock(&umtx_lock);
2194 uq->uq_inherited_pri = old_inherited_pri;
2196 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2197 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2199 if (pri > UPRI(uq2->uq_thread))
2200 pri = UPRI(uq2->uq_thread);
2203 if (pri > uq->uq_inherited_pri)
2204 pri = uq->uq_inherited_pri;
2206 sched_lend_user_prio(td, pri);
2208 mtx_unlock(&umtx_lock);
2212 umtxq_unbusy_unlocked(&uq->uq_key);
2213 umtx_key_release(&uq->uq_key);
2218 * Unlock a PP mutex.
2221 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2223 struct umtx_key key;
2224 struct umtx_q *uq, *uq2;
2226 uint32_t id, owner, rceiling;
2227 int error, pri, new_inherited_pri, su;
2231 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2234 * Make sure we own this mtx.
2236 error = fueword32(&m->m_owner, &owner);
2240 if ((owner & ~UMUTEX_CONTESTED) != id)
2243 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2248 new_inherited_pri = PRI_MAX;
2250 rceiling = RTP_PRIO_MAX - rceiling;
2251 if (rceiling > RTP_PRIO_MAX)
2253 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2256 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2257 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2264 * For priority protected mutex, always set unlocked state
2265 * to UMUTEX_CONTESTED, so that userland always enters kernel
2266 * to lock the mutex, it is necessary because thread priority
2267 * has to be adjusted for such mutex.
2269 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2274 umtxq_signal(&key, 1);
2281 mtx_lock(&umtx_lock);
2283 uq->uq_inherited_pri = new_inherited_pri;
2285 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2286 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2288 if (pri > UPRI(uq2->uq_thread))
2289 pri = UPRI(uq2->uq_thread);
2292 if (pri > uq->uq_inherited_pri)
2293 pri = uq->uq_inherited_pri;
2295 sched_lend_user_prio(td, pri);
2297 mtx_unlock(&umtx_lock);
2299 umtx_key_release(&key);
2304 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2305 uint32_t *old_ceiling)
2308 uint32_t flags, id, owner, save_ceiling;
2311 error = fueword32(&m->m_flags, &flags);
2314 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2316 if (ceiling > RTP_PRIO_MAX)
2320 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2321 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2325 umtxq_lock(&uq->uq_key);
2326 umtxq_busy(&uq->uq_key);
2327 umtxq_unlock(&uq->uq_key);
2329 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2335 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2336 id | UMUTEX_CONTESTED);
2342 if (owner == UMUTEX_CONTESTED) {
2343 rv = suword32(&m->m_ceilings[0], ceiling);
2344 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2345 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2349 if ((owner & ~UMUTEX_CONTESTED) == id) {
2350 rv = suword32(&m->m_ceilings[0], ceiling);
2351 error = rv == 0 ? 0 : EFAULT;
2355 if (owner == UMUTEX_RB_OWNERDEAD) {
2358 } else if (owner == UMUTEX_RB_NOTRECOV) {
2359 error = ENOTRECOVERABLE;
2364 * If we caught a signal, we have retried and now
2371 * We set the contested bit, sleep. Otherwise the lock changed
2372 * and we need to retry or we lost a race to the thread
2373 * unlocking the umtx.
2375 umtxq_lock(&uq->uq_key);
2377 umtxq_unbusy(&uq->uq_key);
2378 error = umtxq_sleep(uq, "umtxpp", NULL);
2380 umtxq_unlock(&uq->uq_key);
2382 umtxq_lock(&uq->uq_key);
2384 umtxq_signal(&uq->uq_key, INT_MAX);
2385 umtxq_unbusy(&uq->uq_key);
2386 umtxq_unlock(&uq->uq_key);
2387 umtx_key_release(&uq->uq_key);
2388 if (error == 0 && old_ceiling != NULL) {
2389 rv = suword32(old_ceiling, save_ceiling);
2390 error = rv == 0 ? 0 : EFAULT;
2396 * Lock a userland POSIX mutex.
2399 do_lock_umutex(struct thread *td, struct umutex *m,
2400 struct _umtx_time *timeout, int mode)
2405 error = fueword32(&m->m_flags, &flags);
2409 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2411 error = do_lock_normal(td, m, flags, timeout, mode);
2413 case UMUTEX_PRIO_INHERIT:
2414 error = do_lock_pi(td, m, flags, timeout, mode);
2416 case UMUTEX_PRIO_PROTECT:
2417 error = do_lock_pp(td, m, flags, timeout, mode);
2422 if (timeout == NULL) {
2423 if (error == EINTR && mode != _UMUTEX_WAIT)
2426 /* Timed-locking is not restarted. */
2427 if (error == ERESTART)
2434 * Unlock a userland POSIX mutex.
2437 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2442 error = fueword32(&m->m_flags, &flags);
2446 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2448 return (do_unlock_normal(td, m, flags, rb));
2449 case UMUTEX_PRIO_INHERIT:
2450 return (do_unlock_pi(td, m, flags, rb));
2451 case UMUTEX_PRIO_PROTECT:
2452 return (do_unlock_pp(td, m, flags, rb));
2459 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2460 struct timespec *timeout, u_long wflags)
2462 struct abs_timeout timo;
2464 uint32_t flags, clockid, hasw;
2468 error = fueword32(&cv->c_flags, &flags);
2471 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2475 if ((wflags & CVWAIT_CLOCKID) != 0) {
2476 error = fueword32(&cv->c_clockid, &clockid);
2478 umtx_key_release(&uq->uq_key);
2481 if (clockid < CLOCK_REALTIME ||
2482 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2483 /* hmm, only HW clock id will work. */
2484 umtx_key_release(&uq->uq_key);
2488 clockid = CLOCK_REALTIME;
2491 umtxq_lock(&uq->uq_key);
2492 umtxq_busy(&uq->uq_key);
2494 umtxq_unlock(&uq->uq_key);
2497 * Set c_has_waiters to 1 before releasing user mutex, also
2498 * don't modify cache line when unnecessary.
2500 error = fueword32(&cv->c_has_waiters, &hasw);
2501 if (error == 0 && hasw == 0)
2502 suword32(&cv->c_has_waiters, 1);
2504 umtxq_unbusy_unlocked(&uq->uq_key);
2506 error = do_unlock_umutex(td, m, false);
2508 if (timeout != NULL)
2509 abs_timeout_init(&timo, clockid, (wflags & CVWAIT_ABSTIME) != 0,
2512 umtxq_lock(&uq->uq_key);
2514 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2518 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2522 * This must be timeout,interrupted by signal or
2523 * surprious wakeup, clear c_has_waiter flag when
2526 umtxq_busy(&uq->uq_key);
2527 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2528 int oldlen = uq->uq_cur_queue->length;
2531 umtxq_unlock(&uq->uq_key);
2532 suword32(&cv->c_has_waiters, 0);
2533 umtxq_lock(&uq->uq_key);
2536 umtxq_unbusy(&uq->uq_key);
2537 if (error == ERESTART)
2541 umtxq_unlock(&uq->uq_key);
2542 umtx_key_release(&uq->uq_key);
2547 * Signal a userland condition variable.
2550 do_cv_signal(struct thread *td, struct ucond *cv)
2552 struct umtx_key key;
2553 int error, cnt, nwake;
2556 error = fueword32(&cv->c_flags, &flags);
2559 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2563 cnt = umtxq_count(&key);
2564 nwake = umtxq_signal(&key, 1);
2567 error = suword32(&cv->c_has_waiters, 0);
2574 umtx_key_release(&key);
2579 do_cv_broadcast(struct thread *td, struct ucond *cv)
2581 struct umtx_key key;
2585 error = fueword32(&cv->c_flags, &flags);
2588 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2593 umtxq_signal(&key, INT_MAX);
2596 error = suword32(&cv->c_has_waiters, 0);
2600 umtxq_unbusy_unlocked(&key);
2602 umtx_key_release(&key);
2607 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, struct _umtx_time *timeout)
2609 struct abs_timeout timo;
2611 uint32_t flags, wrflags;
2612 int32_t state, oldstate;
2613 int32_t blocked_readers;
2614 int error, error1, rv;
2617 error = fueword32(&rwlock->rw_flags, &flags);
2620 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2624 if (timeout != NULL)
2625 abs_timeout_init2(&timo, timeout);
2627 wrflags = URWLOCK_WRITE_OWNER;
2628 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2629 wrflags |= URWLOCK_WRITE_WAITERS;
2632 rv = fueword32(&rwlock->rw_state, &state);
2634 umtx_key_release(&uq->uq_key);
2638 /* try to lock it */
2639 while (!(state & wrflags)) {
2640 if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) {
2641 umtx_key_release(&uq->uq_key);
2644 rv = casueword32(&rwlock->rw_state, state,
2645 &oldstate, state + 1);
2647 umtx_key_release(&uq->uq_key);
2650 if (oldstate == state) {
2651 umtx_key_release(&uq->uq_key);
2654 error = umtxq_check_susp(td);
2663 /* grab monitor lock */
2664 umtxq_lock(&uq->uq_key);
2665 umtxq_busy(&uq->uq_key);
2666 umtxq_unlock(&uq->uq_key);
2669 * re-read the state, in case it changed between the try-lock above
2670 * and the check below
2672 rv = fueword32(&rwlock->rw_state, &state);
2676 /* set read contention bit */
2677 while (error == 0 && (state & wrflags) &&
2678 !(state & URWLOCK_READ_WAITERS)) {
2679 rv = casueword32(&rwlock->rw_state, state,
2680 &oldstate, state | URWLOCK_READ_WAITERS);
2685 if (oldstate == state)
2688 error = umtxq_check_susp(td);
2693 umtxq_unbusy_unlocked(&uq->uq_key);
2697 /* state is changed while setting flags, restart */
2698 if (!(state & wrflags)) {
2699 umtxq_unbusy_unlocked(&uq->uq_key);
2700 error = umtxq_check_susp(td);
2707 /* contention bit is set, before sleeping, increase read waiter count */
2708 rv = fueword32(&rwlock->rw_blocked_readers,
2711 umtxq_unbusy_unlocked(&uq->uq_key);
2715 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2717 while (state & wrflags) {
2718 umtxq_lock(&uq->uq_key);
2720 umtxq_unbusy(&uq->uq_key);
2722 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2725 umtxq_busy(&uq->uq_key);
2727 umtxq_unlock(&uq->uq_key);
2730 rv = fueword32(&rwlock->rw_state, &state);
2737 /* decrease read waiter count, and may clear read contention bit */
2738 rv = fueword32(&rwlock->rw_blocked_readers,
2741 umtxq_unbusy_unlocked(&uq->uq_key);
2745 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2746 if (blocked_readers == 1) {
2747 rv = fueword32(&rwlock->rw_state, &state);
2749 umtxq_unbusy_unlocked(&uq->uq_key);
2754 rv = casueword32(&rwlock->rw_state, state,
2755 &oldstate, state & ~URWLOCK_READ_WAITERS);
2760 if (oldstate == state)
2763 error1 = umtxq_check_susp(td);
2772 umtxq_unbusy_unlocked(&uq->uq_key);
2776 umtx_key_release(&uq->uq_key);
2777 if (error == ERESTART)
2783 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2785 struct abs_timeout timo;
2788 int32_t state, oldstate;
2789 int32_t blocked_writers;
2790 int32_t blocked_readers;
2791 int error, error1, rv;
2794 error = fueword32(&rwlock->rw_flags, &flags);
2797 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2801 if (timeout != NULL)
2802 abs_timeout_init2(&timo, timeout);
2804 blocked_readers = 0;
2806 rv = fueword32(&rwlock->rw_state, &state);
2808 umtx_key_release(&uq->uq_key);
2811 while (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2812 rv = casueword32(&rwlock->rw_state, state,
2813 &oldstate, state | URWLOCK_WRITE_OWNER);
2815 umtx_key_release(&uq->uq_key);
2818 if (oldstate == state) {
2819 umtx_key_release(&uq->uq_key);
2823 error = umtxq_check_susp(td);
2829 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) &&
2830 blocked_readers != 0) {
2831 umtxq_lock(&uq->uq_key);
2832 umtxq_busy(&uq->uq_key);
2833 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE);
2834 umtxq_unbusy(&uq->uq_key);
2835 umtxq_unlock(&uq->uq_key);
2841 /* grab monitor lock */
2842 umtxq_lock(&uq->uq_key);
2843 umtxq_busy(&uq->uq_key);
2844 umtxq_unlock(&uq->uq_key);
2847 * re-read the state, in case it changed between the try-lock above
2848 * and the check below
2850 rv = fueword32(&rwlock->rw_state, &state);
2854 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
2855 URWLOCK_READER_COUNT(state) != 0) &&
2856 (state & URWLOCK_WRITE_WAITERS) == 0) {
2857 rv = casueword32(&rwlock->rw_state, state,
2858 &oldstate, state | URWLOCK_WRITE_WAITERS);
2863 if (oldstate == state)
2866 error = umtxq_check_susp(td);
2871 umtxq_unbusy_unlocked(&uq->uq_key);
2875 if (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2876 umtxq_unbusy_unlocked(&uq->uq_key);
2877 error = umtxq_check_susp(td);
2883 rv = fueword32(&rwlock->rw_blocked_writers,
2886 umtxq_unbusy_unlocked(&uq->uq_key);
2890 suword32(&rwlock->rw_blocked_writers, blocked_writers+1);
2892 while ((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) {
2893 umtxq_lock(&uq->uq_key);
2894 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2895 umtxq_unbusy(&uq->uq_key);
2897 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
2900 umtxq_busy(&uq->uq_key);
2901 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2902 umtxq_unlock(&uq->uq_key);
2905 rv = fueword32(&rwlock->rw_state, &state);
2912 rv = fueword32(&rwlock->rw_blocked_writers,
2915 umtxq_unbusy_unlocked(&uq->uq_key);
2919 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
2920 if (blocked_writers == 1) {
2921 rv = fueword32(&rwlock->rw_state, &state);
2923 umtxq_unbusy_unlocked(&uq->uq_key);
2928 rv = casueword32(&rwlock->rw_state, state,
2929 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
2934 if (oldstate == state)
2937 error1 = umtxq_check_susp(td);
2939 * We are leaving the URWLOCK_WRITE_WAITERS
2940 * behind, but this should not harm the
2949 rv = fueword32(&rwlock->rw_blocked_readers,
2952 umtxq_unbusy_unlocked(&uq->uq_key);
2957 blocked_readers = 0;
2959 umtxq_unbusy_unlocked(&uq->uq_key);
2962 umtx_key_release(&uq->uq_key);
2963 if (error == ERESTART)
2969 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
2973 int32_t state, oldstate;
2974 int error, rv, q, count;
2977 error = fueword32(&rwlock->rw_flags, &flags);
2980 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2984 error = fueword32(&rwlock->rw_state, &state);
2989 if (state & URWLOCK_WRITE_OWNER) {
2991 rv = casueword32(&rwlock->rw_state, state,
2992 &oldstate, state & ~URWLOCK_WRITE_OWNER);
2997 if (oldstate != state) {
2999 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3003 error = umtxq_check_susp(td);
3009 } else if (URWLOCK_READER_COUNT(state) != 0) {
3011 rv = casueword32(&rwlock->rw_state, state,
3012 &oldstate, state - 1);
3017 if (oldstate != state) {
3019 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3023 error = umtxq_check_susp(td);
3036 if (!(flags & URWLOCK_PREFER_READER)) {
3037 if (state & URWLOCK_WRITE_WAITERS) {
3039 q = UMTX_EXCLUSIVE_QUEUE;
3040 } else if (state & URWLOCK_READ_WAITERS) {
3042 q = UMTX_SHARED_QUEUE;
3045 if (state & URWLOCK_READ_WAITERS) {
3047 q = UMTX_SHARED_QUEUE;
3048 } else if (state & URWLOCK_WRITE_WAITERS) {
3050 q = UMTX_EXCLUSIVE_QUEUE;
3055 umtxq_lock(&uq->uq_key);
3056 umtxq_busy(&uq->uq_key);
3057 umtxq_signal_queue(&uq->uq_key, count, q);
3058 umtxq_unbusy(&uq->uq_key);
3059 umtxq_unlock(&uq->uq_key);
3062 umtx_key_release(&uq->uq_key);
3066 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3068 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3070 struct abs_timeout timo;
3072 uint32_t flags, count, count1;
3076 error = fueword32(&sem->_flags, &flags);
3079 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3083 if (timeout != NULL)
3084 abs_timeout_init2(&timo, timeout);
3086 umtxq_lock(&uq->uq_key);
3087 umtxq_busy(&uq->uq_key);
3089 umtxq_unlock(&uq->uq_key);
3090 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3092 rv = fueword32(&sem->_count, &count);
3093 if (rv == -1 || count != 0) {
3094 umtxq_lock(&uq->uq_key);
3095 umtxq_unbusy(&uq->uq_key);
3097 umtxq_unlock(&uq->uq_key);
3098 umtx_key_release(&uq->uq_key);
3099 return (rv == -1 ? EFAULT : 0);
3101 umtxq_lock(&uq->uq_key);
3102 umtxq_unbusy(&uq->uq_key);
3104 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3106 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3110 /* A relative timeout cannot be restarted. */
3111 if (error == ERESTART && timeout != NULL &&
3112 (timeout->_flags & UMTX_ABSTIME) == 0)
3115 umtxq_unlock(&uq->uq_key);
3116 umtx_key_release(&uq->uq_key);
3121 * Signal a userland semaphore.
3124 do_sem_wake(struct thread *td, struct _usem *sem)
3126 struct umtx_key key;
3130 error = fueword32(&sem->_flags, &flags);
3133 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3137 cnt = umtxq_count(&key);
3140 * Check if count is greater than 0, this means the memory is
3141 * still being referenced by user code, so we can safely
3142 * update _has_waiters flag.
3146 error = suword32(&sem->_has_waiters, 0);
3151 umtxq_signal(&key, 1);
3155 umtx_key_release(&key);
3161 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3163 struct abs_timeout timo;
3165 uint32_t count, flags;
3169 flags = fuword32(&sem->_flags);
3170 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3174 if (timeout != NULL)
3175 abs_timeout_init2(&timo, timeout);
3177 umtxq_lock(&uq->uq_key);
3178 umtxq_busy(&uq->uq_key);
3180 umtxq_unlock(&uq->uq_key);
3181 rv = fueword32(&sem->_count, &count);
3183 umtxq_lock(&uq->uq_key);
3184 umtxq_unbusy(&uq->uq_key);
3186 umtxq_unlock(&uq->uq_key);
3187 umtx_key_release(&uq->uq_key);
3191 if (USEM_COUNT(count) != 0) {
3192 umtxq_lock(&uq->uq_key);
3193 umtxq_unbusy(&uq->uq_key);
3195 umtxq_unlock(&uq->uq_key);
3196 umtx_key_release(&uq->uq_key);
3199 if (count == USEM_HAS_WAITERS)
3201 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3203 umtxq_lock(&uq->uq_key);
3204 umtxq_unbusy(&uq->uq_key);
3206 umtxq_unlock(&uq->uq_key);
3207 umtx_key_release(&uq->uq_key);
3213 umtxq_lock(&uq->uq_key);
3214 umtxq_unbusy(&uq->uq_key);
3216 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3218 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3222 /* A relative timeout cannot be restarted. */
3223 if (error == ERESTART && timeout != NULL &&
3224 (timeout->_flags & UMTX_ABSTIME) == 0)
3227 umtxq_unlock(&uq->uq_key);
3228 umtx_key_release(&uq->uq_key);
3233 * Signal a userland semaphore.
3236 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3238 struct umtx_key key;
3240 uint32_t count, flags;
3242 rv = fueword32(&sem->_flags, &flags);
3245 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3249 cnt = umtxq_count(&key);
3252 * If this was the last sleeping thread, clear the waiters
3257 rv = fueword32(&sem->_count, &count);
3258 while (rv != -1 && count & USEM_HAS_WAITERS)
3259 rv = casueword32(&sem->_count, count, &count,
3260 count & ~USEM_HAS_WAITERS);
3266 umtxq_signal(&key, 1);
3270 umtx_key_release(&key);
3275 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3279 error = copyin(addr, tsp, sizeof(struct timespec));
3281 if (tsp->tv_sec < 0 ||
3282 tsp->tv_nsec >= 1000000000 ||
3290 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3294 if (size <= sizeof(struct timespec)) {
3295 tp->_clockid = CLOCK_REALTIME;
3297 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3299 error = copyin(addr, tp, sizeof(struct _umtx_time));
3302 if (tp->_timeout.tv_sec < 0 ||
3303 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3309 __umtx_op_unimpl(struct thread *td, struct _umtx_op_args *uap)
3312 return (EOPNOTSUPP);
3316 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3318 struct _umtx_time timeout, *tm_p;
3321 if (uap->uaddr2 == NULL)
3324 error = umtx_copyin_umtx_time(
3325 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3330 return (do_wait(td, uap->obj, uap->val, tm_p, 0, 0));
3334 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3336 struct _umtx_time timeout, *tm_p;
3339 if (uap->uaddr2 == NULL)
3342 error = umtx_copyin_umtx_time(
3343 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3348 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3352 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3354 struct _umtx_time *tm_p, timeout;
3357 if (uap->uaddr2 == NULL)
3360 error = umtx_copyin_umtx_time(
3361 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3366 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3370 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3373 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3376 #define BATCH_SIZE 128
3378 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3380 char *uaddrs[BATCH_SIZE], **upp;
3381 int count, error, i, pos, tocopy;
3383 upp = (char **)uap->obj;
3385 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3387 tocopy = MIN(count, BATCH_SIZE);
3388 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3391 for (i = 0; i < tocopy; ++i)
3392 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3399 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3402 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3406 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3408 struct _umtx_time *tm_p, timeout;
3411 /* Allow a null timespec (wait forever). */
3412 if (uap->uaddr2 == NULL)
3415 error = umtx_copyin_umtx_time(
3416 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3421 return (do_lock_umutex(td, uap->obj, tm_p, 0));
3425 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3428 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
3432 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3434 struct _umtx_time *tm_p, timeout;
3437 /* Allow a null timespec (wait forever). */
3438 if (uap->uaddr2 == NULL)
3441 error = umtx_copyin_umtx_time(
3442 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3447 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
3451 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3454 return (do_wake_umutex(td, uap->obj));
3458 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3461 return (do_unlock_umutex(td, uap->obj, false));
3465 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3468 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
3472 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3474 struct timespec *ts, timeout;
3477 /* Allow a null timespec (wait forever). */
3478 if (uap->uaddr2 == NULL)
3481 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3486 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3490 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3493 return (do_cv_signal(td, uap->obj));
3497 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3500 return (do_cv_broadcast(td, uap->obj));
3504 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3506 struct _umtx_time timeout;
3509 /* Allow a null timespec (wait forever). */
3510 if (uap->uaddr2 == NULL) {
3511 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3513 error = umtx_copyin_umtx_time(uap->uaddr2,
3514 (size_t)uap->uaddr1, &timeout);
3517 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3523 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3525 struct _umtx_time timeout;
3528 /* Allow a null timespec (wait forever). */
3529 if (uap->uaddr2 == NULL) {
3530 error = do_rw_wrlock(td, uap->obj, 0);
3532 error = umtx_copyin_umtx_time(uap->uaddr2,
3533 (size_t)uap->uaddr1, &timeout);
3537 error = do_rw_wrlock(td, uap->obj, &timeout);
3543 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3546 return (do_rw_unlock(td, uap->obj));
3549 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3551 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3553 struct _umtx_time *tm_p, timeout;
3556 /* Allow a null timespec (wait forever). */
3557 if (uap->uaddr2 == NULL)
3560 error = umtx_copyin_umtx_time(
3561 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3566 return (do_sem_wait(td, uap->obj, tm_p));
3570 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3573 return (do_sem_wake(td, uap->obj));
3578 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3581 return (do_wake2_umutex(td, uap->obj, uap->val));
3585 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap)
3587 struct _umtx_time *tm_p, timeout;
3590 /* Allow a null timespec (wait forever). */
3591 if (uap->uaddr2 == NULL)
3594 error = umtx_copyin_umtx_time(
3595 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3600 return (do_sem2_wait(td, uap->obj, tm_p));
3604 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap)
3607 return (do_sem2_wake(td, uap->obj));
3610 #define USHM_OBJ_UMTX(o) \
3611 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
3613 #define USHMF_REG_LINKED 0x0001
3614 #define USHMF_OBJ_LINKED 0x0002
3615 struct umtx_shm_reg {
3616 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
3617 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
3618 struct umtx_key ushm_key;
3619 struct ucred *ushm_cred;
3620 struct shmfd *ushm_obj;
3625 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
3626 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
3628 static uma_zone_t umtx_shm_reg_zone;
3629 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
3630 static struct mtx umtx_shm_lock;
3631 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
3632 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
3634 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
3637 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
3639 struct umtx_shm_reg_head d;
3640 struct umtx_shm_reg *reg, *reg1;
3643 mtx_lock(&umtx_shm_lock);
3644 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
3645 mtx_unlock(&umtx_shm_lock);
3646 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
3647 TAILQ_REMOVE(&d, reg, ushm_reg_link);
3648 umtx_shm_free_reg(reg);
3652 static struct task umtx_shm_reg_delfree_task =
3653 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
3655 static struct umtx_shm_reg *
3656 umtx_shm_find_reg_locked(const struct umtx_key *key)
3658 struct umtx_shm_reg *reg;
3659 struct umtx_shm_reg_head *reg_head;
3661 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
3662 mtx_assert(&umtx_shm_lock, MA_OWNED);
3663 reg_head = &umtx_shm_registry[key->hash];
3664 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
3665 KASSERT(reg->ushm_key.shared,
3666 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
3667 if (reg->ushm_key.info.shared.object ==
3668 key->info.shared.object &&
3669 reg->ushm_key.info.shared.offset ==
3670 key->info.shared.offset) {
3671 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
3672 KASSERT(reg->ushm_refcnt > 0,
3673 ("reg %p refcnt 0 onlist", reg));
3674 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
3675 ("reg %p not linked", reg));
3683 static struct umtx_shm_reg *
3684 umtx_shm_find_reg(const struct umtx_key *key)
3686 struct umtx_shm_reg *reg;
3688 mtx_lock(&umtx_shm_lock);
3689 reg = umtx_shm_find_reg_locked(key);
3690 mtx_unlock(&umtx_shm_lock);
3695 umtx_shm_free_reg(struct umtx_shm_reg *reg)
3698 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
3699 crfree(reg->ushm_cred);
3700 shm_drop(reg->ushm_obj);
3701 uma_zfree(umtx_shm_reg_zone, reg);
3705 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
3709 mtx_assert(&umtx_shm_lock, MA_OWNED);
3710 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
3712 res = reg->ushm_refcnt == 0;
3714 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
3715 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
3716 reg, ushm_reg_link);
3717 reg->ushm_flags &= ~USHMF_REG_LINKED;
3719 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
3720 LIST_REMOVE(reg, ushm_obj_link);
3721 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
3728 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
3734 object = reg->ushm_obj->shm_object;
3735 VM_OBJECT_WLOCK(object);
3736 object->flags |= OBJ_UMTXDEAD;
3737 VM_OBJECT_WUNLOCK(object);
3739 mtx_lock(&umtx_shm_lock);
3740 dofree = umtx_shm_unref_reg_locked(reg, force);
3741 mtx_unlock(&umtx_shm_lock);
3743 umtx_shm_free_reg(reg);
3747 umtx_shm_object_init(vm_object_t object)
3750 LIST_INIT(USHM_OBJ_UMTX(object));
3754 umtx_shm_object_terminated(vm_object_t object)
3756 struct umtx_shm_reg *reg, *reg1;
3760 mtx_lock(&umtx_shm_lock);
3761 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
3762 if (umtx_shm_unref_reg_locked(reg, true)) {
3763 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
3768 mtx_unlock(&umtx_shm_lock);
3770 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
3774 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
3775 struct umtx_shm_reg **res)
3777 struct umtx_shm_reg *reg, *reg1;
3781 reg = umtx_shm_find_reg(key);
3786 cred = td->td_ucred;
3787 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
3789 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
3790 reg->ushm_refcnt = 1;
3791 bcopy(key, ®->ushm_key, sizeof(*key));
3792 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR);
3793 reg->ushm_cred = crhold(cred);
3794 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
3796 umtx_shm_free_reg(reg);
3799 mtx_lock(&umtx_shm_lock);
3800 reg1 = umtx_shm_find_reg_locked(key);
3802 mtx_unlock(&umtx_shm_lock);
3803 umtx_shm_free_reg(reg);
3808 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
3809 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
3811 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
3812 mtx_unlock(&umtx_shm_lock);
3818 umtx_shm_alive(struct thread *td, void *addr)
3821 vm_map_entry_t entry;
3828 map = &td->td_proc->p_vmspace->vm_map;
3829 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
3830 &object, &pindex, &prot, &wired);
3831 if (res != KERN_SUCCESS)
3836 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
3837 vm_map_lookup_done(map, entry);
3846 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
3847 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
3848 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
3849 for (i = 0; i < nitems(umtx_shm_registry); i++)
3850 TAILQ_INIT(&umtx_shm_registry[i]);
3854 umtx_shm(struct thread *td, void *addr, u_int flags)
3856 struct umtx_key key;
3857 struct umtx_shm_reg *reg;
3861 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
3862 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
3864 if ((flags & UMTX_SHM_ALIVE) != 0)
3865 return (umtx_shm_alive(td, addr));
3866 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
3869 KASSERT(key.shared == 1, ("non-shared key"));
3870 if ((flags & UMTX_SHM_CREAT) != 0) {
3871 error = umtx_shm_create_reg(td, &key, ®);
3873 reg = umtx_shm_find_reg(&key);
3877 umtx_key_release(&key);
3880 KASSERT(reg != NULL, ("no reg"));
3881 if ((flags & UMTX_SHM_DESTROY) != 0) {
3882 umtx_shm_unref_reg(reg, true);
3886 error = mac_posixshm_check_open(td->td_ucred,
3887 reg->ushm_obj, FFLAGS(O_RDWR));
3890 error = shm_access(reg->ushm_obj, td->td_ucred,
3894 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
3896 shm_hold(reg->ushm_obj);
3897 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
3899 td->td_retval[0] = fd;
3903 umtx_shm_unref_reg(reg, false);
3908 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap)
3911 return (umtx_shm(td, uap->uaddr1, uap->val));
3915 umtx_robust_lists(struct thread *td, struct umtx_robust_lists_params *rbp)
3918 td->td_rb_list = rbp->robust_list_offset;
3919 td->td_rbp_list = rbp->robust_priv_list_offset;
3920 td->td_rb_inact = rbp->robust_inact_offset;
3925 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap)
3927 struct umtx_robust_lists_params rb;
3930 if (uap->val > sizeof(rb))
3932 bzero(&rb, sizeof(rb));
3933 error = copyin(uap->uaddr1, &rb, uap->val);
3936 return (umtx_robust_lists(td, &rb));
3939 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
3941 static const _umtx_op_func op_table[] = {
3942 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
3943 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
3944 [UMTX_OP_WAIT] = __umtx_op_wait,
3945 [UMTX_OP_WAKE] = __umtx_op_wake,
3946 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
3947 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
3948 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
3949 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
3950 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
3951 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
3952 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
3953 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
3954 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
3955 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
3956 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
3957 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
3958 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
3959 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
3960 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
3961 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3962 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
3963 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
3965 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
3966 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
3968 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
3969 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
3970 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
3971 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
3972 [UMTX_OP_SHM] = __umtx_op_shm,
3973 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
3977 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
3980 if ((unsigned)uap->op < nitems(op_table))
3981 return (*op_table[uap->op])(td, uap);
3985 #ifdef COMPAT_FREEBSD32
3992 struct umtx_time32 {
3993 struct timespec32 timeout;
3999 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
4001 struct timespec32 ts32;
4004 error = copyin(addr, &ts32, sizeof(struct timespec32));
4006 if (ts32.tv_sec < 0 ||
4007 ts32.tv_nsec >= 1000000000 ||
4011 tsp->tv_sec = ts32.tv_sec;
4012 tsp->tv_nsec = ts32.tv_nsec;
4019 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
4021 struct umtx_time32 t32;
4024 t32.clockid = CLOCK_REALTIME;
4026 if (size <= sizeof(struct timespec32))
4027 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
4029 error = copyin(addr, &t32, sizeof(struct umtx_time32));
4032 if (t32.timeout.tv_sec < 0 ||
4033 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
4035 tp->_timeout.tv_sec = t32.timeout.tv_sec;
4036 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
4037 tp->_flags = t32.flags;
4038 tp->_clockid = t32.clockid;
4043 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4045 struct _umtx_time *tm_p, timeout;
4048 if (uap->uaddr2 == NULL)
4051 error = umtx_copyin_umtx_time32(uap->uaddr2,
4052 (size_t)uap->uaddr1, &timeout);
4057 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
4061 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4063 struct _umtx_time *tm_p, timeout;
4066 /* Allow a null timespec (wait forever). */
4067 if (uap->uaddr2 == NULL)
4070 error = umtx_copyin_umtx_time(uap->uaddr2,
4071 (size_t)uap->uaddr1, &timeout);
4076 return (do_lock_umutex(td, uap->obj, tm_p, 0));
4080 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4082 struct _umtx_time *tm_p, timeout;
4085 /* Allow a null timespec (wait forever). */
4086 if (uap->uaddr2 == NULL)
4089 error = umtx_copyin_umtx_time32(uap->uaddr2,
4090 (size_t)uap->uaddr1, &timeout);
4095 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
4099 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4101 struct timespec *ts, timeout;
4104 /* Allow a null timespec (wait forever). */
4105 if (uap->uaddr2 == NULL)
4108 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
4113 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
4117 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4119 struct _umtx_time timeout;
4122 /* Allow a null timespec (wait forever). */
4123 if (uap->uaddr2 == NULL) {
4124 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4126 error = umtx_copyin_umtx_time32(uap->uaddr2,
4127 (size_t)uap->uaddr1, &timeout);
4130 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4136 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4138 struct _umtx_time timeout;
4141 /* Allow a null timespec (wait forever). */
4142 if (uap->uaddr2 == NULL) {
4143 error = do_rw_wrlock(td, uap->obj, 0);
4145 error = umtx_copyin_umtx_time32(uap->uaddr2,
4146 (size_t)uap->uaddr1, &timeout);
4149 error = do_rw_wrlock(td, uap->obj, &timeout);
4155 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
4157 struct _umtx_time *tm_p, timeout;
4160 if (uap->uaddr2 == NULL)
4163 error = umtx_copyin_umtx_time32(
4164 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
4169 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
4172 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4174 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4176 struct _umtx_time *tm_p, timeout;
4179 /* Allow a null timespec (wait forever). */
4180 if (uap->uaddr2 == NULL)
4183 error = umtx_copyin_umtx_time32(uap->uaddr2,
4184 (size_t)uap->uaddr1, &timeout);
4189 return (do_sem_wait(td, uap->obj, tm_p));
4194 __umtx_op_sem2_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4196 struct _umtx_time *tm_p, timeout;
4199 /* Allow a null timespec (wait forever). */
4200 if (uap->uaddr2 == NULL)
4203 error = umtx_copyin_umtx_time32(uap->uaddr2,
4204 (size_t)uap->uaddr1, &timeout);
4209 return (do_sem2_wait(td, uap->obj, tm_p));
4213 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
4215 uint32_t uaddrs[BATCH_SIZE], **upp;
4216 int count, error, i, pos, tocopy;
4218 upp = (uint32_t **)uap->obj;
4220 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
4222 tocopy = MIN(count, BATCH_SIZE);
4223 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
4226 for (i = 0; i < tocopy; ++i)
4227 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
4234 struct umtx_robust_lists_params_compat32 {
4235 uint32_t robust_list_offset;
4236 uint32_t robust_priv_list_offset;
4237 uint32_t robust_inact_offset;
4241 __umtx_op_robust_lists_compat32(struct thread *td, struct _umtx_op_args *uap)
4243 struct umtx_robust_lists_params rb;
4244 struct umtx_robust_lists_params_compat32 rb32;
4247 if (uap->val > sizeof(rb32))
4249 bzero(&rb, sizeof(rb));
4250 bzero(&rb32, sizeof(rb32));
4251 error = copyin(uap->uaddr1, &rb32, uap->val);
4254 rb.robust_list_offset = rb32.robust_list_offset;
4255 rb.robust_priv_list_offset = rb32.robust_priv_list_offset;
4256 rb.robust_inact_offset = rb32.robust_inact_offset;
4257 return (umtx_robust_lists(td, &rb));
4260 static const _umtx_op_func op_table_compat32[] = {
4261 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
4262 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
4263 [UMTX_OP_WAIT] = __umtx_op_wait_compat32,
4264 [UMTX_OP_WAKE] = __umtx_op_wake,
4265 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4266 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex_compat32,
4267 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4268 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4269 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait_compat32,
4270 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4271 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4272 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_compat32,
4273 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock_compat32,
4274 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock_compat32,
4275 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4276 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private_compat32,
4277 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4278 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex_compat32,
4279 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4280 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4281 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait_compat32,
4282 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4284 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4285 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4287 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private32,
4288 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4289 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait_compat32,
4290 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4291 [UMTX_OP_SHM] = __umtx_op_shm,
4292 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists_compat32,
4296 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
4299 if ((unsigned)uap->op < nitems(op_table_compat32)) {
4300 return (*op_table_compat32[uap->op])(td,
4301 (struct _umtx_op_args *)uap);
4308 umtx_thread_init(struct thread *td)
4311 td->td_umtxq = umtxq_alloc();
4312 td->td_umtxq->uq_thread = td;
4316 umtx_thread_fini(struct thread *td)
4319 umtxq_free(td->td_umtxq);
4323 * It will be called when new thread is created, e.g fork().
4326 umtx_thread_alloc(struct thread *td)
4331 uq->uq_inherited_pri = PRI_MAX;
4333 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4334 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4335 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4336 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4342 * Clear robust lists for all process' threads, not delaying the
4343 * cleanup to thread_exit hook, since the relevant address space is
4344 * destroyed right now.
4347 umtx_exec_hook(void *arg __unused, struct proc *p,
4348 struct image_params *imgp __unused)
4352 KASSERT(p == curproc, ("need curproc"));
4354 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4355 (p->p_flag & P_STOPPED_SINGLE) != 0,
4356 ("curproc must be single-threaded"));
4357 FOREACH_THREAD_IN_PROC(p, td) {
4358 KASSERT(td == curthread ||
4359 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4360 ("running thread %p %p", p, td));
4362 umtx_thread_cleanup(td);
4364 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4370 * thread_exit() hook.
4373 umtx_thread_exit(struct thread *td)
4376 umtx_thread_cleanup(td);
4380 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res)
4383 #ifdef COMPAT_FREEBSD32
4388 #ifdef COMPAT_FREEBSD32
4389 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4390 error = fueword32((void *)ptr, &res32);
4396 error = fueword((void *)ptr, &res1);
4406 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list)
4408 #ifdef COMPAT_FREEBSD32
4409 struct umutex32 m32;
4411 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4412 memcpy(&m32, m, sizeof(m32));
4413 *rb_list = m32.m_rb_lnk;
4416 *rb_list = m->m_rb_lnk;
4420 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact)
4425 KASSERT(td->td_proc == curproc, ("need current vmspace"));
4426 error = copyin((void *)rbp, &m, sizeof(m));
4429 if (rb_list != NULL)
4430 umtx_read_rb_list(td, &m, rb_list);
4431 if ((m.m_flags & UMUTEX_ROBUST) == 0)
4433 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
4434 /* inact is cleared after unlock, allow the inconsistency */
4435 return (inact ? 0 : EINVAL);
4436 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
4440 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
4449 error = umtx_read_uptr(td, rb_list, &rbp);
4450 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
4451 if (rbp == *rb_inact) {
4456 error = umtx_handle_rb(td, rbp, &rbp, inact);
4458 if (i == umtx_max_rb && umtx_verbose_rb) {
4459 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
4460 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
4462 if (error != 0 && umtx_verbose_rb) {
4463 uprintf("comm %s pid %d: handling %srb error %d\n",
4464 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
4469 * Clean up umtx data.
4472 umtx_thread_cleanup(struct thread *td)
4479 * Disown pi mutexes.
4483 mtx_lock(&umtx_lock);
4484 uq->uq_inherited_pri = PRI_MAX;
4485 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
4486 pi->pi_owner = NULL;
4487 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
4489 mtx_unlock(&umtx_lock);
4491 sched_lend_user_prio(td, PRI_MAX);
4496 * Handle terminated robust mutexes. Must be done after
4497 * robust pi disown, otherwise unlock could see unowned
4500 rb_inact = td->td_rb_inact;
4502 (void)umtx_read_uptr(td, rb_inact, &rb_inact);
4503 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "");
4504 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ");
4506 (void)umtx_handle_rb(td, rb_inact, NULL, true);