2 * Copyright (c) 2004, David Xu <davidxu@freebsd.org>
3 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_compat.h"
32 #include "opt_umtx_profiling.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
43 #include <sys/sched.h>
45 #include <sys/sysctl.h>
46 #include <sys/sysent.h>
47 #include <sys/systm.h>
48 #include <sys/sysproto.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/eventhandler.h>
54 #include <vm/vm_param.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_object.h>
59 #include <machine/cpu.h>
61 #ifdef COMPAT_FREEBSD32
62 #include <compat/freebsd32/freebsd32_proto.h>
66 #define _UMUTEX_WAIT 2
69 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
70 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
73 /* Priority inheritance mutex info. */
76 struct thread *pi_owner;
81 /* List entry to link umtx holding by thread */
82 TAILQ_ENTRY(umtx_pi) pi_link;
84 /* List entry in hash */
85 TAILQ_ENTRY(umtx_pi) pi_hashlink;
87 /* List for waiters */
88 TAILQ_HEAD(,umtx_q) pi_blocked;
90 /* Identify a userland lock object */
91 struct umtx_key pi_key;
94 /* A userland synchronous object user. */
96 /* Linked list for the hash. */
97 TAILQ_ENTRY(umtx_q) uq_link;
100 struct umtx_key uq_key;
104 #define UQF_UMTXQ 0x0001
106 /* The thread waits on. */
107 struct thread *uq_thread;
110 * Blocked on PI mutex. read can use chain lock
111 * or umtx_lock, write must have both chain lock and
112 * umtx_lock being hold.
114 struct umtx_pi *uq_pi_blocked;
116 /* On blocked list */
117 TAILQ_ENTRY(umtx_q) uq_lockq;
119 /* Thread contending with us */
120 TAILQ_HEAD(,umtx_pi) uq_pi_contested;
122 /* Inherited priority from PP mutex */
123 u_char uq_inherited_pri;
125 /* Spare queue ready to be reused */
126 struct umtxq_queue *uq_spare_queue;
128 /* The queue we on */
129 struct umtxq_queue *uq_cur_queue;
132 TAILQ_HEAD(umtxq_head, umtx_q);
134 /* Per-key wait-queue */
136 struct umtxq_head head;
138 LIST_ENTRY(umtxq_queue) link;
142 LIST_HEAD(umtxq_list, umtxq_queue);
144 /* Userland lock object's wait-queue chain */
146 /* Lock for this chain. */
149 /* List of sleep queues. */
150 struct umtxq_list uc_queue[2];
151 #define UMTX_SHARED_QUEUE 0
152 #define UMTX_EXCLUSIVE_QUEUE 1
154 LIST_HEAD(, umtxq_queue) uc_spare_queue;
159 /* Chain lock waiters */
162 /* All PI in the list */
163 TAILQ_HEAD(,umtx_pi) uc_pi_list;
165 #ifdef UMTX_PROFILING
171 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
174 * Don't propagate time-sharing priority, there is a security reason,
175 * a user can simply introduce PI-mutex, let thread A lock the mutex,
176 * and let another thread B block on the mutex, because B is
177 * sleeping, its priority will be boosted, this causes A's priority to
178 * be boosted via priority propagating too and will never be lowered even
179 * if it is using 100%CPU, this is unfair to other processes.
182 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
183 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
184 PRI_MAX_TIMESHARE : (td)->td_user_pri)
186 #define GOLDEN_RATIO_PRIME 2654404609U
187 #define UMTX_CHAINS 512
188 #define UMTX_SHIFTS (__WORD_BIT - 9)
190 #define GET_SHARE(flags) \
191 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
193 #define BUSY_SPINS 200
201 static uma_zone_t umtx_pi_zone;
202 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
203 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
204 static int umtx_pi_allocated;
206 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW, 0, "umtx debug");
207 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
208 &umtx_pi_allocated, 0, "Allocated umtx_pi");
210 #ifdef UMTX_PROFILING
211 static long max_length;
212 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
213 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD, 0, "umtx chain stats");
216 static void umtxq_sysinit(void *);
217 static void umtxq_hash(struct umtx_key *key);
218 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
219 static void umtxq_lock(struct umtx_key *key);
220 static void umtxq_unlock(struct umtx_key *key);
221 static void umtxq_busy(struct umtx_key *key);
222 static void umtxq_unbusy(struct umtx_key *key);
223 static void umtxq_insert_queue(struct umtx_q *uq, int q);
224 static void umtxq_remove_queue(struct umtx_q *uq, int q);
225 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *);
226 static int umtxq_count(struct umtx_key *key);
227 static struct umtx_pi *umtx_pi_alloc(int);
228 static void umtx_pi_free(struct umtx_pi *pi);
229 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags);
230 static void umtx_thread_cleanup(struct thread *td);
231 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
232 struct image_params *imgp __unused);
233 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
235 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
236 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
237 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
239 static struct mtx umtx_lock;
241 #ifdef UMTX_PROFILING
243 umtx_init_profiling(void)
245 struct sysctl_oid *chain_oid;
249 for (i = 0; i < UMTX_CHAINS; ++i) {
250 snprintf(chain_name, sizeof(chain_name), "%d", i);
251 chain_oid = SYSCTL_ADD_NODE(NULL,
252 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
253 chain_name, CTLFLAG_RD, NULL, "umtx hash stats");
254 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
255 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
256 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
257 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
262 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
266 struct umtxq_chain *uc;
267 u_int fract, i, j, tot, whole;
268 u_int sf0, sf1, sf2, sf3, sf4;
269 u_int si0, si1, si2, si3, si4;
270 u_int sw0, sw1, sw2, sw3, sw4;
272 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
273 for (i = 0; i < 2; i++) {
275 for (j = 0; j < UMTX_CHAINS; ++j) {
276 uc = &umtxq_chains[i][j];
277 mtx_lock(&uc->uc_lock);
278 tot += uc->max_length;
279 mtx_unlock(&uc->uc_lock);
282 sbuf_printf(&sb, "%u) Empty ", i);
284 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
285 si0 = si1 = si2 = si3 = si4 = 0;
286 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
287 for (j = 0; j < UMTX_CHAINS; j++) {
288 uc = &umtxq_chains[i][j];
289 mtx_lock(&uc->uc_lock);
290 whole = uc->max_length * 100;
291 mtx_unlock(&uc->uc_lock);
292 fract = (whole % tot) * 100;
293 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
297 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
302 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
307 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
312 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
319 sbuf_printf(&sb, "queue %u:\n", i);
320 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
322 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
324 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
326 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
328 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
334 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
340 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
342 struct umtxq_chain *uc;
347 error = sysctl_handle_int(oidp, &clear, 0, req);
348 if (error != 0 || req->newptr == NULL)
352 for (i = 0; i < 2; ++i) {
353 for (j = 0; j < UMTX_CHAINS; ++j) {
354 uc = &umtxq_chains[i][j];
355 mtx_lock(&uc->uc_lock);
358 mtx_unlock(&uc->uc_lock);
365 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
366 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
367 sysctl_debug_umtx_chains_clear, "I", "Clear umtx chains statistics");
368 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
369 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
370 sysctl_debug_umtx_chains_peaks, "A", "Highest peaks in chains max length");
374 umtxq_sysinit(void *arg __unused)
378 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
379 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
380 for (i = 0; i < 2; ++i) {
381 for (j = 0; j < UMTX_CHAINS; ++j) {
382 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
383 MTX_DEF | MTX_DUPOK);
384 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
385 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
386 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
387 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
388 umtxq_chains[i][j].uc_busy = 0;
389 umtxq_chains[i][j].uc_waiters = 0;
390 #ifdef UMTX_PROFILING
391 umtxq_chains[i][j].length = 0;
392 umtxq_chains[i][j].max_length = 0;
396 #ifdef UMTX_PROFILING
397 umtx_init_profiling();
399 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
400 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
401 EVENTHANDLER_PRI_ANY);
409 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
410 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX, M_WAITOK | M_ZERO);
411 TAILQ_INIT(&uq->uq_spare_queue->head);
412 TAILQ_INIT(&uq->uq_pi_contested);
413 uq->uq_inherited_pri = PRI_MAX;
418 umtxq_free(struct umtx_q *uq)
420 MPASS(uq->uq_spare_queue != NULL);
421 free(uq->uq_spare_queue, M_UMTX);
426 umtxq_hash(struct umtx_key *key)
428 unsigned n = (uintptr_t)key->info.both.a + key->info.both.b;
429 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
432 static inline struct umtxq_chain *
433 umtxq_getchain(struct umtx_key *key)
435 if (key->type <= TYPE_SEM)
436 return (&umtxq_chains[1][key->hash]);
437 return (&umtxq_chains[0][key->hash]);
444 umtxq_lock(struct umtx_key *key)
446 struct umtxq_chain *uc;
448 uc = umtxq_getchain(key);
449 mtx_lock(&uc->uc_lock);
456 umtxq_unlock(struct umtx_key *key)
458 struct umtxq_chain *uc;
460 uc = umtxq_getchain(key);
461 mtx_unlock(&uc->uc_lock);
465 * Set chain to busy state when following operation
466 * may be blocked (kernel mutex can not be used).
469 umtxq_busy(struct umtx_key *key)
471 struct umtxq_chain *uc;
473 uc = umtxq_getchain(key);
474 mtx_assert(&uc->uc_lock, MA_OWNED);
478 int count = BUSY_SPINS;
481 while (uc->uc_busy && --count > 0)
487 while (uc->uc_busy) {
489 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
500 umtxq_unbusy(struct umtx_key *key)
502 struct umtxq_chain *uc;
504 uc = umtxq_getchain(key);
505 mtx_assert(&uc->uc_lock, MA_OWNED);
506 KASSERT(uc->uc_busy != 0, ("not busy"));
513 umtxq_unbusy_unlocked(struct umtx_key *key)
521 static struct umtxq_queue *
522 umtxq_queue_lookup(struct umtx_key *key, int q)
524 struct umtxq_queue *uh;
525 struct umtxq_chain *uc;
527 uc = umtxq_getchain(key);
528 UMTXQ_LOCKED_ASSERT(uc);
529 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
530 if (umtx_key_match(&uh->key, key))
538 umtxq_insert_queue(struct umtx_q *uq, int q)
540 struct umtxq_queue *uh;
541 struct umtxq_chain *uc;
543 uc = umtxq_getchain(&uq->uq_key);
544 UMTXQ_LOCKED_ASSERT(uc);
545 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
546 uh = umtxq_queue_lookup(&uq->uq_key, q);
548 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
550 uh = uq->uq_spare_queue;
551 uh->key = uq->uq_key;
552 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
553 #ifdef UMTX_PROFILING
555 if (uc->length > uc->max_length) {
556 uc->max_length = uc->length;
557 if (uc->max_length > max_length)
558 max_length = uc->max_length;
562 uq->uq_spare_queue = NULL;
564 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
566 uq->uq_flags |= UQF_UMTXQ;
567 uq->uq_cur_queue = uh;
572 umtxq_remove_queue(struct umtx_q *uq, int q)
574 struct umtxq_chain *uc;
575 struct umtxq_queue *uh;
577 uc = umtxq_getchain(&uq->uq_key);
578 UMTXQ_LOCKED_ASSERT(uc);
579 if (uq->uq_flags & UQF_UMTXQ) {
580 uh = uq->uq_cur_queue;
581 TAILQ_REMOVE(&uh->head, uq, uq_link);
583 uq->uq_flags &= ~UQF_UMTXQ;
584 if (TAILQ_EMPTY(&uh->head)) {
585 KASSERT(uh->length == 0,
586 ("inconsistent umtxq_queue length"));
587 #ifdef UMTX_PROFILING
590 LIST_REMOVE(uh, link);
592 uh = LIST_FIRST(&uc->uc_spare_queue);
593 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
594 LIST_REMOVE(uh, link);
596 uq->uq_spare_queue = uh;
597 uq->uq_cur_queue = NULL;
602 * Check if there are multiple waiters
605 umtxq_count(struct umtx_key *key)
607 struct umtxq_chain *uc;
608 struct umtxq_queue *uh;
610 uc = umtxq_getchain(key);
611 UMTXQ_LOCKED_ASSERT(uc);
612 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
619 * Check if there are multiple PI waiters and returns first
623 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
625 struct umtxq_chain *uc;
626 struct umtxq_queue *uh;
629 uc = umtxq_getchain(key);
630 UMTXQ_LOCKED_ASSERT(uc);
631 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
633 *first = TAILQ_FIRST(&uh->head);
640 umtxq_check_susp(struct thread *td)
646 * The check for TDF_NEEDSUSPCHK is racy, but it is enough to
647 * eventually break the lockstep loop.
649 if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
654 if (P_SHOULDSTOP(p) ||
655 ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) {
656 if (p->p_flag & P_SINGLE_EXIT)
666 * Wake up threads waiting on an userland object.
670 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
672 struct umtxq_chain *uc;
673 struct umtxq_queue *uh;
678 uc = umtxq_getchain(key);
679 UMTXQ_LOCKED_ASSERT(uc);
680 uh = umtxq_queue_lookup(key, q);
682 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
683 umtxq_remove_queue(uq, q);
694 * Wake up specified thread.
697 umtxq_signal_thread(struct umtx_q *uq)
699 struct umtxq_chain *uc;
701 uc = umtxq_getchain(&uq->uq_key);
702 UMTXQ_LOCKED_ASSERT(uc);
708 tstohz(const struct timespec *tsp)
712 TIMESPEC_TO_TIMEVAL(&tv, tsp);
717 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
718 const struct timespec *timeout)
721 timo->clockid = clockid;
723 kern_clock_gettime(curthread, clockid, &timo->end);
724 timo->cur = timo->end;
725 timespecadd(&timo->end, timeout);
727 timo->end = *timeout;
728 kern_clock_gettime(curthread, clockid, &timo->cur);
733 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
736 abs_timeout_init(timo, umtxtime->_clockid,
737 (umtxtime->_flags & UMTX_ABSTIME) != 0,
738 &umtxtime->_timeout);
742 abs_timeout_update(struct abs_timeout *timo)
744 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
748 abs_timeout_gethz(struct abs_timeout *timo)
752 if (timespeccmp(&timo->end, &timo->cur, <=))
755 timespecsub(&tts, &timo->cur);
756 return (tstohz(&tts));
760 * Put thread into sleep state, before sleeping, check if
761 * thread was removed from umtx queue.
764 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
766 struct umtxq_chain *uc;
769 uc = umtxq_getchain(&uq->uq_key);
770 UMTXQ_LOCKED_ASSERT(uc);
772 if (!(uq->uq_flags & UQF_UMTXQ))
774 if (abstime != NULL) {
775 timo = abs_timeout_gethz(abstime);
780 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
781 if (error != EWOULDBLOCK) {
782 umtxq_lock(&uq->uq_key);
786 abs_timeout_update(abstime);
787 umtxq_lock(&uq->uq_key);
793 * Convert userspace address into unique logical address.
796 umtx_key_get(void *addr, int type, int share, struct umtx_key *key)
798 struct thread *td = curthread;
800 vm_map_entry_t entry;
806 if (share == THREAD_SHARE) {
808 key->info.private.vs = td->td_proc->p_vmspace;
809 key->info.private.addr = (uintptr_t)addr;
811 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
812 map = &td->td_proc->p_vmspace->vm_map;
813 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
814 &entry, &key->info.shared.object, &pindex, &prot,
815 &wired) != KERN_SUCCESS) {
819 if ((share == PROCESS_SHARE) ||
820 (share == AUTO_SHARE &&
821 VM_INHERIT_SHARE == entry->inheritance)) {
823 key->info.shared.offset = entry->offset + entry->start -
825 vm_object_reference(key->info.shared.object);
828 key->info.private.vs = td->td_proc->p_vmspace;
829 key->info.private.addr = (uintptr_t)addr;
831 vm_map_lookup_done(map, entry);
842 umtx_key_release(struct umtx_key *key)
845 vm_object_deallocate(key->info.shared.object);
849 * Lock a umtx object.
852 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
853 const struct timespec *timeout)
855 struct abs_timeout timo;
863 abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
866 * Care must be exercised when dealing with umtx structure. It
867 * can fault on any access.
871 * Try the uncontested case. This should be done in userland.
873 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
875 /* The acquire succeeded. */
876 if (owner == UMTX_UNOWNED)
879 /* The address was invalid. */
883 /* If no one owns it but it is contested try to acquire it. */
884 if (owner == UMTX_CONTESTED) {
885 owner = casuword(&umtx->u_owner,
886 UMTX_CONTESTED, id | UMTX_CONTESTED);
888 if (owner == UMTX_CONTESTED)
891 /* The address was invalid. */
895 error = umtxq_check_susp(td);
899 /* If this failed the lock has changed, restart. */
904 * If we caught a signal, we have retried and now
910 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
911 AUTO_SHARE, &uq->uq_key)) != 0)
914 umtxq_lock(&uq->uq_key);
915 umtxq_busy(&uq->uq_key);
917 umtxq_unbusy(&uq->uq_key);
918 umtxq_unlock(&uq->uq_key);
921 * Set the contested bit so that a release in user space
922 * knows to use the system call for unlock. If this fails
923 * either some one else has acquired the lock or it has been
926 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
928 /* The address was invalid. */
930 umtxq_lock(&uq->uq_key);
932 umtxq_unlock(&uq->uq_key);
933 umtx_key_release(&uq->uq_key);
938 * We set the contested bit, sleep. Otherwise the lock changed
939 * and we need to retry or we lost a race to the thread
940 * unlocking the umtx.
942 umtxq_lock(&uq->uq_key);
944 error = umtxq_sleep(uq, "umtx", timeout == NULL ? NULL :
947 umtxq_unlock(&uq->uq_key);
948 umtx_key_release(&uq->uq_key);
951 error = umtxq_check_susp(td);
954 if (timeout == NULL) {
955 /* Mutex locking is restarted if it is interrupted. */
959 /* Timed-locking is not restarted. */
960 if (error == ERESTART)
967 * Unlock a umtx object.
970 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
979 * Make sure we own this mtx.
981 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
985 if ((owner & ~UMTX_CONTESTED) != id)
988 /* This should be done in userland */
989 if ((owner & UMTX_CONTESTED) == 0) {
990 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
998 /* We should only ever be in here for contested locks */
999 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1005 count = umtxq_count(&key);
1009 * When unlocking the umtx, it must be marked as unowned if
1010 * there is zero or one thread only waiting for it.
1011 * Otherwise, it must be marked as contested.
1013 old = casuword(&umtx->u_owner, owner,
1014 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
1016 umtxq_signal(&key,1);
1019 umtx_key_release(&key);
1027 #ifdef COMPAT_FREEBSD32
1030 * Lock a umtx object.
1033 do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id,
1034 const struct timespec *timeout)
1036 struct abs_timeout timo;
1044 if (timeout != NULL)
1045 abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
1048 * Care must be exercised when dealing with umtx structure. It
1049 * can fault on any access.
1053 * Try the uncontested case. This should be done in userland.
1055 owner = casuword32(m, UMUTEX_UNOWNED, id);
1057 /* The acquire succeeded. */
1058 if (owner == UMUTEX_UNOWNED)
1061 /* The address was invalid. */
1065 /* If no one owns it but it is contested try to acquire it. */
1066 if (owner == UMUTEX_CONTESTED) {
1067 owner = casuword32(m,
1068 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1069 if (owner == UMUTEX_CONTESTED)
1072 /* The address was invalid. */
1076 error = umtxq_check_susp(td);
1080 /* If this failed the lock has changed, restart. */
1085 * If we caught a signal, we have retried and now
1091 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
1092 AUTO_SHARE, &uq->uq_key)) != 0)
1095 umtxq_lock(&uq->uq_key);
1096 umtxq_busy(&uq->uq_key);
1098 umtxq_unbusy(&uq->uq_key);
1099 umtxq_unlock(&uq->uq_key);
1102 * Set the contested bit so that a release in user space
1103 * knows to use the system call for unlock. If this fails
1104 * either some one else has acquired the lock or it has been
1107 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
1109 /* The address was invalid. */
1111 umtxq_lock(&uq->uq_key);
1113 umtxq_unlock(&uq->uq_key);
1114 umtx_key_release(&uq->uq_key);
1119 * We set the contested bit, sleep. Otherwise the lock changed
1120 * and we need to retry or we lost a race to the thread
1121 * unlocking the umtx.
1123 umtxq_lock(&uq->uq_key);
1125 error = umtxq_sleep(uq, "umtx", timeout == NULL ?
1128 umtxq_unlock(&uq->uq_key);
1129 umtx_key_release(&uq->uq_key);
1132 error = umtxq_check_susp(td);
1135 if (timeout == NULL) {
1136 /* Mutex locking is restarted if it is interrupted. */
1140 /* Timed-locking is not restarted. */
1141 if (error == ERESTART)
1148 * Unlock a umtx object.
1151 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
1153 struct umtx_key key;
1160 * Make sure we own this mtx.
1162 owner = fuword32(m);
1166 if ((owner & ~UMUTEX_CONTESTED) != id)
1169 /* This should be done in userland */
1170 if ((owner & UMUTEX_CONTESTED) == 0) {
1171 old = casuword32(m, owner, UMUTEX_UNOWNED);
1179 /* We should only ever be in here for contested locks */
1180 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1186 count = umtxq_count(&key);
1190 * When unlocking the umtx, it must be marked as unowned if
1191 * there is zero or one thread only waiting for it.
1192 * Otherwise, it must be marked as contested.
1194 old = casuword32(m, owner,
1195 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1197 umtxq_signal(&key,1);
1200 umtx_key_release(&key);
1210 * Fetch and compare value, sleep on the address if value is not changed.
1213 do_wait(struct thread *td, void *addr, u_long id,
1214 struct _umtx_time *timeout, int compat32, int is_private)
1216 struct abs_timeout timo;
1223 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
1224 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
1227 if (timeout != NULL)
1228 abs_timeout_init2(&timo, timeout);
1230 umtxq_lock(&uq->uq_key);
1232 umtxq_unlock(&uq->uq_key);
1233 if (compat32 == 0) {
1234 error = fueword(addr, &tmp);
1238 error = fueword32(addr, &tmp32);
1244 umtxq_lock(&uq->uq_key);
1247 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
1249 if ((uq->uq_flags & UQF_UMTXQ) == 0)
1253 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
1256 umtxq_unlock(&uq->uq_key);
1257 umtx_key_release(&uq->uq_key);
1258 if (error == ERESTART)
1264 * Wake up threads sleeping on the specified address.
1267 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1269 struct umtx_key key;
1272 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1273 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1276 umtxq_signal(&key, n_wake);
1278 umtx_key_release(&key);
1283 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1286 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1287 struct _umtx_time *timeout, int mode)
1289 struct abs_timeout timo;
1291 uint32_t owner, old, id;
1297 if (timeout != NULL)
1298 abs_timeout_init2(&timo, timeout);
1301 * Care must be exercised when dealing with umtx structure. It
1302 * can fault on any access.
1305 rv = fueword32(&m->m_owner, &owner);
1308 if (mode == _UMUTEX_WAIT) {
1309 if (owner == UMUTEX_UNOWNED || owner == UMUTEX_CONTESTED)
1313 * Try the uncontested case. This should be done in userland.
1315 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1317 /* The address was invalid. */
1321 /* The acquire succeeded. */
1322 if (owner == UMUTEX_UNOWNED)
1325 /* If no one owns it but it is contested try to acquire it. */
1326 if (owner == UMUTEX_CONTESTED) {
1327 rv = casueword32(&m->m_owner,
1328 UMUTEX_CONTESTED, &owner,
1329 id | UMUTEX_CONTESTED);
1330 /* The address was invalid. */
1334 if (owner == UMUTEX_CONTESTED)
1337 rv = umtxq_check_susp(td);
1341 /* If this failed the lock has changed, restart. */
1346 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1347 (owner & ~UMUTEX_CONTESTED) == id)
1350 if (mode == _UMUTEX_TRY)
1354 * If we caught a signal, we have retried and now
1360 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1361 GET_SHARE(flags), &uq->uq_key)) != 0)
1364 umtxq_lock(&uq->uq_key);
1365 umtxq_busy(&uq->uq_key);
1367 umtxq_unlock(&uq->uq_key);
1370 * Set the contested bit so that a release in user space
1371 * knows to use the system call for unlock. If this fails
1372 * either some one else has acquired the lock or it has been
1375 rv = casueword32(&m->m_owner, owner, &old,
1376 owner | UMUTEX_CONTESTED);
1378 /* The address was invalid. */
1380 umtxq_lock(&uq->uq_key);
1382 umtxq_unbusy(&uq->uq_key);
1383 umtxq_unlock(&uq->uq_key);
1384 umtx_key_release(&uq->uq_key);
1389 * We set the contested bit, sleep. Otherwise the lock changed
1390 * and we need to retry or we lost a race to the thread
1391 * unlocking the umtx.
1393 umtxq_lock(&uq->uq_key);
1394 umtxq_unbusy(&uq->uq_key);
1396 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1399 umtxq_unlock(&uq->uq_key);
1400 umtx_key_release(&uq->uq_key);
1403 error = umtxq_check_susp(td);
1410 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1413 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags)
1415 struct umtx_key key;
1416 uint32_t owner, old, id;
1422 * Make sure we own this mtx.
1424 error = fueword32(&m->m_owner, &owner);
1428 if ((owner & ~UMUTEX_CONTESTED) != id)
1431 if ((owner & UMUTEX_CONTESTED) == 0) {
1432 error = casueword32(&m->m_owner, owner, &old, UMUTEX_UNOWNED);
1440 /* We should only ever be in here for contested locks */
1441 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1447 count = umtxq_count(&key);
1451 * When unlocking the umtx, it must be marked as unowned if
1452 * there is zero or one thread only waiting for it.
1453 * Otherwise, it must be marked as contested.
1455 error = casueword32(&m->m_owner, owner, &old,
1456 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1458 umtxq_signal(&key,1);
1461 umtx_key_release(&key);
1470 * Check if the mutex is available and wake up a waiter,
1471 * only for simple mutex.
1474 do_wake_umutex(struct thread *td, struct umutex *m)
1476 struct umtx_key key;
1482 error = fueword32(&m->m_owner, &owner);
1486 if ((owner & ~UMUTEX_CONTESTED) != 0)
1489 error = fueword32(&m->m_flags, &flags);
1493 /* We should only ever be in here for contested locks */
1494 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1500 count = umtxq_count(&key);
1504 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1511 if (error == 0 && count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1512 umtxq_signal(&key, 1);
1515 umtx_key_release(&key);
1520 * Check if the mutex has waiters and tries to fix contention bit.
1523 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1525 struct umtx_key key;
1526 uint32_t owner, old;
1531 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
1533 type = TYPE_NORMAL_UMUTEX;
1535 case UMUTEX_PRIO_INHERIT:
1536 type = TYPE_PI_UMUTEX;
1538 case UMUTEX_PRIO_PROTECT:
1539 type = TYPE_PP_UMUTEX;
1544 if ((error = umtx_key_get(m, type, GET_SHARE(flags),
1551 count = umtxq_count(&key);
1554 * Only repair contention bit if there is a waiter, this means the mutex
1555 * is still being referenced by userland code, otherwise don't update
1559 error = fueword32(&m->m_owner, &owner);
1562 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0) {
1563 error = casueword32(&m->m_owner, owner, &old,
1564 owner | UMUTEX_CONTESTED);
1572 error = umtxq_check_susp(td);
1576 } else if (count == 1) {
1577 error = fueword32(&m->m_owner, &owner);
1580 while (error == 0 && (owner & ~UMUTEX_CONTESTED) != 0 &&
1581 (owner & UMUTEX_CONTESTED) == 0) {
1582 error = casueword32(&m->m_owner, owner, &old,
1583 owner | UMUTEX_CONTESTED);
1591 error = umtxq_check_susp(td);
1597 if (error == EFAULT) {
1598 umtxq_signal(&key, INT_MAX);
1599 } else if (count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1600 umtxq_signal(&key, 1);
1603 umtx_key_release(&key);
1607 static inline struct umtx_pi *
1608 umtx_pi_alloc(int flags)
1612 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1613 TAILQ_INIT(&pi->pi_blocked);
1614 atomic_add_int(&umtx_pi_allocated, 1);
1619 umtx_pi_free(struct umtx_pi *pi)
1621 uma_zfree(umtx_pi_zone, pi);
1622 atomic_add_int(&umtx_pi_allocated, -1);
1626 * Adjust the thread's position on a pi_state after its priority has been
1630 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1632 struct umtx_q *uq, *uq1, *uq2;
1635 mtx_assert(&umtx_lock, MA_OWNED);
1642 * Check if the thread needs to be moved on the blocked chain.
1643 * It needs to be moved if either its priority is lower than
1644 * the previous thread or higher than the next thread.
1646 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1647 uq2 = TAILQ_NEXT(uq, uq_lockq);
1648 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1649 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1651 * Remove thread from blocked chain and determine where
1652 * it should be moved to.
1654 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1655 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1656 td1 = uq1->uq_thread;
1657 MPASS(td1->td_proc->p_magic == P_MAGIC);
1658 if (UPRI(td1) > UPRI(td))
1663 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1665 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1670 static struct umtx_pi *
1671 umtx_pi_next(struct umtx_pi *pi)
1673 struct umtx_q *uq_owner;
1675 if (pi->pi_owner == NULL)
1677 uq_owner = pi->pi_owner->td_umtxq;
1678 if (uq_owner == NULL)
1680 return (uq_owner->uq_pi_blocked);
1684 * Floyd's Cycle-Finding Algorithm.
1687 umtx_pi_check_loop(struct umtx_pi *pi)
1689 struct umtx_pi *pi1; /* fast iterator */
1691 mtx_assert(&umtx_lock, MA_OWNED);
1696 pi = umtx_pi_next(pi);
1699 pi1 = umtx_pi_next(pi1);
1702 pi1 = umtx_pi_next(pi1);
1712 * Propagate priority when a thread is blocked on POSIX
1716 umtx_propagate_priority(struct thread *td)
1722 mtx_assert(&umtx_lock, MA_OWNED);
1725 pi = uq->uq_pi_blocked;
1728 if (umtx_pi_check_loop(pi))
1733 if (td == NULL || td == curthread)
1736 MPASS(td->td_proc != NULL);
1737 MPASS(td->td_proc->p_magic == P_MAGIC);
1740 if (td->td_lend_user_pri > pri)
1741 sched_lend_user_prio(td, pri);
1749 * Pick up the lock that td is blocked on.
1752 pi = uq->uq_pi_blocked;
1755 /* Resort td on the list if needed. */
1756 umtx_pi_adjust_thread(pi, td);
1761 * Unpropagate priority for a PI mutex when a thread blocked on
1762 * it is interrupted by signal or resumed by others.
1765 umtx_repropagate_priority(struct umtx_pi *pi)
1767 struct umtx_q *uq, *uq_owner;
1768 struct umtx_pi *pi2;
1771 mtx_assert(&umtx_lock, MA_OWNED);
1773 if (umtx_pi_check_loop(pi))
1775 while (pi != NULL && pi->pi_owner != NULL) {
1777 uq_owner = pi->pi_owner->td_umtxq;
1779 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1780 uq = TAILQ_FIRST(&pi2->pi_blocked);
1782 if (pri > UPRI(uq->uq_thread))
1783 pri = UPRI(uq->uq_thread);
1787 if (pri > uq_owner->uq_inherited_pri)
1788 pri = uq_owner->uq_inherited_pri;
1789 thread_lock(pi->pi_owner);
1790 sched_lend_user_prio(pi->pi_owner, pri);
1791 thread_unlock(pi->pi_owner);
1792 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1793 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1798 * Insert a PI mutex into owned list.
1801 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1803 struct umtx_q *uq_owner;
1805 uq_owner = owner->td_umtxq;
1806 mtx_assert(&umtx_lock, MA_OWNED);
1807 if (pi->pi_owner != NULL)
1808 panic("pi_owner != NULL");
1809 pi->pi_owner = owner;
1810 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1815 * Disown a PI mutex, and remove it from the owned list.
1818 umtx_pi_disown(struct umtx_pi *pi)
1821 mtx_assert(&umtx_lock, MA_OWNED);
1822 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1823 pi->pi_owner = NULL;
1827 * Claim ownership of a PI mutex.
1830 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1834 mtx_lock(&umtx_lock);
1835 if (pi->pi_owner == owner) {
1836 mtx_unlock(&umtx_lock);
1840 if (pi->pi_owner != NULL) {
1842 * userland may have already messed the mutex, sigh.
1844 mtx_unlock(&umtx_lock);
1847 umtx_pi_setowner(pi, owner);
1848 uq = TAILQ_FIRST(&pi->pi_blocked);
1852 pri = UPRI(uq->uq_thread);
1854 if (pri < UPRI(owner))
1855 sched_lend_user_prio(owner, pri);
1856 thread_unlock(owner);
1858 mtx_unlock(&umtx_lock);
1863 * Adjust a thread's order position in its blocked PI mutex,
1864 * this may result new priority propagating process.
1867 umtx_pi_adjust(struct thread *td, u_char oldpri)
1873 mtx_lock(&umtx_lock);
1875 * Pick up the lock that td is blocked on.
1877 pi = uq->uq_pi_blocked;
1879 umtx_pi_adjust_thread(pi, td);
1880 umtx_repropagate_priority(pi);
1882 mtx_unlock(&umtx_lock);
1886 * Sleep on a PI mutex.
1889 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi,
1890 uint32_t owner, const char *wmesg, struct abs_timeout *timo)
1892 struct umtxq_chain *uc;
1893 struct thread *td, *td1;
1899 KASSERT(td == curthread, ("inconsistent uq_thread"));
1900 uc = umtxq_getchain(&uq->uq_key);
1901 UMTXQ_LOCKED_ASSERT(uc);
1902 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1904 mtx_lock(&umtx_lock);
1905 if (pi->pi_owner == NULL) {
1906 mtx_unlock(&umtx_lock);
1907 /* XXX Only look up thread in current process. */
1908 td1 = tdfind(owner, curproc->p_pid);
1909 mtx_lock(&umtx_lock);
1911 if (pi->pi_owner == NULL)
1912 umtx_pi_setowner(pi, td1);
1913 PROC_UNLOCK(td1->td_proc);
1917 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1918 pri = UPRI(uq1->uq_thread);
1924 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1926 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1928 uq->uq_pi_blocked = pi;
1930 td->td_flags |= TDF_UPIBLOCKED;
1932 umtx_propagate_priority(td);
1933 mtx_unlock(&umtx_lock);
1934 umtxq_unbusy(&uq->uq_key);
1936 error = umtxq_sleep(uq, wmesg, timo);
1939 mtx_lock(&umtx_lock);
1940 uq->uq_pi_blocked = NULL;
1942 td->td_flags &= ~TDF_UPIBLOCKED;
1944 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1945 umtx_repropagate_priority(pi);
1946 mtx_unlock(&umtx_lock);
1947 umtxq_unlock(&uq->uq_key);
1953 * Add reference count for a PI mutex.
1956 umtx_pi_ref(struct umtx_pi *pi)
1958 struct umtxq_chain *uc;
1960 uc = umtxq_getchain(&pi->pi_key);
1961 UMTXQ_LOCKED_ASSERT(uc);
1966 * Decrease reference count for a PI mutex, if the counter
1967 * is decreased to zero, its memory space is freed.
1970 umtx_pi_unref(struct umtx_pi *pi)
1972 struct umtxq_chain *uc;
1974 uc = umtxq_getchain(&pi->pi_key);
1975 UMTXQ_LOCKED_ASSERT(uc);
1976 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1977 if (--pi->pi_refcount == 0) {
1978 mtx_lock(&umtx_lock);
1979 if (pi->pi_owner != NULL)
1981 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1982 ("blocked queue not empty"));
1983 mtx_unlock(&umtx_lock);
1984 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1990 * Find a PI mutex in hash table.
1992 static struct umtx_pi *
1993 umtx_pi_lookup(struct umtx_key *key)
1995 struct umtxq_chain *uc;
1998 uc = umtxq_getchain(key);
1999 UMTXQ_LOCKED_ASSERT(uc);
2001 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
2002 if (umtx_key_match(&pi->pi_key, key)) {
2010 * Insert a PI mutex into hash table.
2013 umtx_pi_insert(struct umtx_pi *pi)
2015 struct umtxq_chain *uc;
2017 uc = umtxq_getchain(&pi->pi_key);
2018 UMTXQ_LOCKED_ASSERT(uc);
2019 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
2026 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
2027 struct _umtx_time *timeout, int try)
2029 struct abs_timeout timo;
2031 struct umtx_pi *pi, *new_pi;
2032 uint32_t id, owner, old;
2038 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
2042 if (timeout != NULL)
2043 abs_timeout_init2(&timo, timeout);
2045 umtxq_lock(&uq->uq_key);
2046 pi = umtx_pi_lookup(&uq->uq_key);
2048 new_pi = umtx_pi_alloc(M_NOWAIT);
2049 if (new_pi == NULL) {
2050 umtxq_unlock(&uq->uq_key);
2051 new_pi = umtx_pi_alloc(M_WAITOK);
2052 umtxq_lock(&uq->uq_key);
2053 pi = umtx_pi_lookup(&uq->uq_key);
2055 umtx_pi_free(new_pi);
2059 if (new_pi != NULL) {
2060 new_pi->pi_key = uq->uq_key;
2061 umtx_pi_insert(new_pi);
2066 umtxq_unlock(&uq->uq_key);
2069 * Care must be exercised when dealing with umtx structure. It
2070 * can fault on any access.
2074 * Try the uncontested case. This should be done in userland.
2076 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
2077 /* The address was invalid. */
2083 /* The acquire succeeded. */
2084 if (owner == UMUTEX_UNOWNED) {
2089 /* If no one owns it but it is contested try to acquire it. */
2090 if (owner == UMUTEX_CONTESTED) {
2091 rv = casueword32(&m->m_owner,
2092 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
2093 /* The address was invalid. */
2099 if (owner == UMUTEX_CONTESTED) {
2100 umtxq_lock(&uq->uq_key);
2101 umtxq_busy(&uq->uq_key);
2102 error = umtx_pi_claim(pi, td);
2103 umtxq_unbusy(&uq->uq_key);
2104 umtxq_unlock(&uq->uq_key);
2107 * Since we're going to return an
2108 * error, restore the m_owner to its
2109 * previous, unowned state to avoid
2110 * compounding the problem.
2112 (void)casuword32(&m->m_owner,
2113 id | UMUTEX_CONTESTED,
2119 error = umtxq_check_susp(td);
2123 /* If this failed the lock has changed, restart. */
2127 if ((owner & ~UMUTEX_CONTESTED) == id) {
2138 * If we caught a signal, we have retried and now
2144 umtxq_lock(&uq->uq_key);
2145 umtxq_busy(&uq->uq_key);
2146 umtxq_unlock(&uq->uq_key);
2149 * Set the contested bit so that a release in user space
2150 * knows to use the system call for unlock. If this fails
2151 * either some one else has acquired the lock or it has been
2154 rv = casueword32(&m->m_owner, owner, &old,
2155 owner | UMUTEX_CONTESTED);
2157 /* The address was invalid. */
2159 umtxq_unbusy_unlocked(&uq->uq_key);
2164 umtxq_lock(&uq->uq_key);
2166 * We set the contested bit, sleep. Otherwise the lock changed
2167 * and we need to retry or we lost a race to the thread
2168 * unlocking the umtx.
2171 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
2172 "umtxpi", timeout == NULL ? NULL : &timo);
2176 umtxq_unbusy(&uq->uq_key);
2177 umtxq_unlock(&uq->uq_key);
2180 error = umtxq_check_susp(td);
2185 umtxq_lock(&uq->uq_key);
2187 umtxq_unlock(&uq->uq_key);
2189 umtx_key_release(&uq->uq_key);
2194 * Unlock a PI mutex.
2197 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags)
2199 struct umtx_key key;
2200 struct umtx_q *uq_first, *uq_first2, *uq_me;
2201 struct umtx_pi *pi, *pi2;
2202 uint32_t owner, old, id;
2209 * Make sure we own this mtx.
2211 error = fueword32(&m->m_owner, &owner);
2215 if ((owner & ~UMUTEX_CONTESTED) != id)
2218 /* This should be done in userland */
2219 if ((owner & UMUTEX_CONTESTED) == 0) {
2220 error = casueword32(&m->m_owner, owner, &old, UMUTEX_UNOWNED);
2228 /* We should only ever be in here for contested locks */
2229 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
2235 count = umtxq_count_pi(&key, &uq_first);
2236 if (uq_first != NULL) {
2237 mtx_lock(&umtx_lock);
2238 pi = uq_first->uq_pi_blocked;
2239 KASSERT(pi != NULL, ("pi == NULL?"));
2240 if (pi->pi_owner != td) {
2241 mtx_unlock(&umtx_lock);
2244 umtx_key_release(&key);
2245 /* userland messed the mutex */
2248 uq_me = td->td_umtxq;
2250 /* get highest priority thread which is still sleeping. */
2251 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2252 while (uq_first != NULL &&
2253 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2254 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2257 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2258 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2259 if (uq_first2 != NULL) {
2260 if (pri > UPRI(uq_first2->uq_thread))
2261 pri = UPRI(uq_first2->uq_thread);
2265 sched_lend_user_prio(td, pri);
2267 mtx_unlock(&umtx_lock);
2269 umtxq_signal_thread(uq_first);
2271 pi = umtx_pi_lookup(&key);
2273 * A umtx_pi can exist if a signal or timeout removed the
2274 * last waiter from the umtxq, but there is still
2275 * a thread in do_lock_pi() holding the umtx_pi.
2279 * The umtx_pi can be unowned, such as when a thread
2280 * has just entered do_lock_pi(), allocated the
2281 * umtx_pi, and unlocked the umtxq.
2282 * If the current thread owns it, it must disown it.
2284 mtx_lock(&umtx_lock);
2285 if (pi->pi_owner == td)
2287 mtx_unlock(&umtx_lock);
2293 * When unlocking the umtx, it must be marked as unowned if
2294 * there is zero or one thread only waiting for it.
2295 * Otherwise, it must be marked as contested.
2297 error = casueword32(&m->m_owner, owner, &old,
2298 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
2300 umtxq_unbusy_unlocked(&key);
2301 umtx_key_release(&key);
2313 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2314 struct _umtx_time *timeout, int try)
2316 struct abs_timeout timo;
2317 struct umtx_q *uq, *uq2;
2321 int error, pri, old_inherited_pri, su, rv;
2325 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2329 if (timeout != NULL)
2330 abs_timeout_init2(&timo, timeout);
2332 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2334 old_inherited_pri = uq->uq_inherited_pri;
2335 umtxq_lock(&uq->uq_key);
2336 umtxq_busy(&uq->uq_key);
2337 umtxq_unlock(&uq->uq_key);
2339 rv = fueword32(&m->m_ceilings[0], &ceiling);
2344 ceiling = RTP_PRIO_MAX - ceiling;
2345 if (ceiling > RTP_PRIO_MAX) {
2350 mtx_lock(&umtx_lock);
2351 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2352 mtx_unlock(&umtx_lock);
2356 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2357 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2359 if (uq->uq_inherited_pri < UPRI(td))
2360 sched_lend_user_prio(td, uq->uq_inherited_pri);
2363 mtx_unlock(&umtx_lock);
2365 rv = casueword32(&m->m_owner,
2366 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
2367 /* The address was invalid. */
2373 if (owner == UMUTEX_CONTESTED) {
2378 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
2379 (owner & ~UMUTEX_CONTESTED) == id) {
2390 * If we caught a signal, we have retried and now
2396 umtxq_lock(&uq->uq_key);
2398 umtxq_unbusy(&uq->uq_key);
2399 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2402 umtxq_unlock(&uq->uq_key);
2404 mtx_lock(&umtx_lock);
2405 uq->uq_inherited_pri = old_inherited_pri;
2407 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2408 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2410 if (pri > UPRI(uq2->uq_thread))
2411 pri = UPRI(uq2->uq_thread);
2414 if (pri > uq->uq_inherited_pri)
2415 pri = uq->uq_inherited_pri;
2417 sched_lend_user_prio(td, pri);
2419 mtx_unlock(&umtx_lock);
2423 mtx_lock(&umtx_lock);
2424 uq->uq_inherited_pri = old_inherited_pri;
2426 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2427 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2429 if (pri > UPRI(uq2->uq_thread))
2430 pri = UPRI(uq2->uq_thread);
2433 if (pri > uq->uq_inherited_pri)
2434 pri = uq->uq_inherited_pri;
2436 sched_lend_user_prio(td, pri);
2438 mtx_unlock(&umtx_lock);
2442 umtxq_unbusy_unlocked(&uq->uq_key);
2443 umtx_key_release(&uq->uq_key);
2448 * Unlock a PP mutex.
2451 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags)
2453 struct umtx_key key;
2454 struct umtx_q *uq, *uq2;
2458 int error, pri, new_inherited_pri, su;
2462 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2465 * Make sure we own this mtx.
2467 error = fueword32(&m->m_owner, &owner);
2471 if ((owner & ~UMUTEX_CONTESTED) != id)
2474 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2479 new_inherited_pri = PRI_MAX;
2481 rceiling = RTP_PRIO_MAX - rceiling;
2482 if (rceiling > RTP_PRIO_MAX)
2484 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2487 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2494 * For priority protected mutex, always set unlocked state
2495 * to UMUTEX_CONTESTED, so that userland always enters kernel
2496 * to lock the mutex, it is necessary because thread priority
2497 * has to be adjusted for such mutex.
2499 error = suword32(&m->m_owner, UMUTEX_CONTESTED);
2503 umtxq_signal(&key, 1);
2510 mtx_lock(&umtx_lock);
2512 uq->uq_inherited_pri = new_inherited_pri;
2514 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2515 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2517 if (pri > UPRI(uq2->uq_thread))
2518 pri = UPRI(uq2->uq_thread);
2521 if (pri > uq->uq_inherited_pri)
2522 pri = uq->uq_inherited_pri;
2524 sched_lend_user_prio(td, pri);
2526 mtx_unlock(&umtx_lock);
2528 umtx_key_release(&key);
2533 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2534 uint32_t *old_ceiling)
2537 uint32_t save_ceiling;
2542 error = fueword32(&m->m_flags, &flags);
2545 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2547 if (ceiling > RTP_PRIO_MAX)
2551 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2555 umtxq_lock(&uq->uq_key);
2556 umtxq_busy(&uq->uq_key);
2557 umtxq_unlock(&uq->uq_key);
2559 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2565 rv = casueword32(&m->m_owner,
2566 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
2572 if (owner == UMUTEX_CONTESTED) {
2573 suword32(&m->m_ceilings[0], ceiling);
2574 suword32(&m->m_owner, UMUTEX_CONTESTED);
2579 if ((owner & ~UMUTEX_CONTESTED) == id) {
2580 suword32(&m->m_ceilings[0], ceiling);
2586 * If we caught a signal, we have retried and now
2593 * We set the contested bit, sleep. Otherwise the lock changed
2594 * and we need to retry or we lost a race to the thread
2595 * unlocking the umtx.
2597 umtxq_lock(&uq->uq_key);
2599 umtxq_unbusy(&uq->uq_key);
2600 error = umtxq_sleep(uq, "umtxpp", NULL);
2602 umtxq_unlock(&uq->uq_key);
2604 umtxq_lock(&uq->uq_key);
2606 umtxq_signal(&uq->uq_key, INT_MAX);
2607 umtxq_unbusy(&uq->uq_key);
2608 umtxq_unlock(&uq->uq_key);
2609 umtx_key_release(&uq->uq_key);
2610 if (error == 0 && old_ceiling != NULL)
2611 suword32(old_ceiling, save_ceiling);
2616 * Lock a userland POSIX mutex.
2619 do_lock_umutex(struct thread *td, struct umutex *m,
2620 struct _umtx_time *timeout, int mode)
2625 error = fueword32(&m->m_flags, &flags);
2629 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2631 error = do_lock_normal(td, m, flags, timeout, mode);
2633 case UMUTEX_PRIO_INHERIT:
2634 error = do_lock_pi(td, m, flags, timeout, mode);
2636 case UMUTEX_PRIO_PROTECT:
2637 error = do_lock_pp(td, m, flags, timeout, mode);
2642 if (timeout == NULL) {
2643 if (error == EINTR && mode != _UMUTEX_WAIT)
2646 /* Timed-locking is not restarted. */
2647 if (error == ERESTART)
2654 * Unlock a userland POSIX mutex.
2657 do_unlock_umutex(struct thread *td, struct umutex *m)
2662 error = fueword32(&m->m_flags, &flags);
2666 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2668 return (do_unlock_normal(td, m, flags));
2669 case UMUTEX_PRIO_INHERIT:
2670 return (do_unlock_pi(td, m, flags));
2671 case UMUTEX_PRIO_PROTECT:
2672 return (do_unlock_pp(td, m, flags));
2679 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2680 struct timespec *timeout, u_long wflags)
2682 struct abs_timeout timo;
2684 uint32_t flags, clockid, hasw;
2688 error = fueword32(&cv->c_flags, &flags);
2691 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2695 if ((wflags & CVWAIT_CLOCKID) != 0) {
2696 error = fueword32(&cv->c_clockid, &clockid);
2698 umtx_key_release(&uq->uq_key);
2701 if (clockid < CLOCK_REALTIME ||
2702 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2703 /* hmm, only HW clock id will work. */
2704 umtx_key_release(&uq->uq_key);
2708 clockid = CLOCK_REALTIME;
2711 umtxq_lock(&uq->uq_key);
2712 umtxq_busy(&uq->uq_key);
2714 umtxq_unlock(&uq->uq_key);
2717 * Set c_has_waiters to 1 before releasing user mutex, also
2718 * don't modify cache line when unnecessary.
2720 error = fueword32(&cv->c_has_waiters, &hasw);
2721 if (error == 0 && hasw == 0)
2722 suword32(&cv->c_has_waiters, 1);
2724 umtxq_unbusy_unlocked(&uq->uq_key);
2726 error = do_unlock_umutex(td, m);
2728 if (timeout != NULL)
2729 abs_timeout_init(&timo, clockid, ((wflags & CVWAIT_ABSTIME) != 0),
2732 umtxq_lock(&uq->uq_key);
2734 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2738 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2742 * This must be timeout,interrupted by signal or
2743 * surprious wakeup, clear c_has_waiter flag when
2746 umtxq_busy(&uq->uq_key);
2747 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2748 int oldlen = uq->uq_cur_queue->length;
2751 umtxq_unlock(&uq->uq_key);
2752 suword32(&cv->c_has_waiters, 0);
2753 umtxq_lock(&uq->uq_key);
2756 umtxq_unbusy(&uq->uq_key);
2757 if (error == ERESTART)
2761 umtxq_unlock(&uq->uq_key);
2762 umtx_key_release(&uq->uq_key);
2767 * Signal a userland condition variable.
2770 do_cv_signal(struct thread *td, struct ucond *cv)
2772 struct umtx_key key;
2773 int error, cnt, nwake;
2776 error = fueword32(&cv->c_flags, &flags);
2779 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2783 cnt = umtxq_count(&key);
2784 nwake = umtxq_signal(&key, 1);
2787 error = suword32(&cv->c_has_waiters, 0);
2794 umtx_key_release(&key);
2799 do_cv_broadcast(struct thread *td, struct ucond *cv)
2801 struct umtx_key key;
2805 error = fueword32(&cv->c_flags, &flags);
2808 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2813 umtxq_signal(&key, INT_MAX);
2816 error = suword32(&cv->c_has_waiters, 0);
2820 umtxq_unbusy_unlocked(&key);
2822 umtx_key_release(&key);
2827 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, struct _umtx_time *timeout)
2829 struct abs_timeout timo;
2831 uint32_t flags, wrflags;
2832 int32_t state, oldstate;
2833 int32_t blocked_readers;
2837 error = fueword32(&rwlock->rw_flags, &flags);
2840 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2844 if (timeout != NULL)
2845 abs_timeout_init2(&timo, timeout);
2847 wrflags = URWLOCK_WRITE_OWNER;
2848 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2849 wrflags |= URWLOCK_WRITE_WAITERS;
2852 rv = fueword32(&rwlock->rw_state, &state);
2854 umtx_key_release(&uq->uq_key);
2858 /* try to lock it */
2859 while (!(state & wrflags)) {
2860 if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) {
2861 umtx_key_release(&uq->uq_key);
2864 rv = casueword32(&rwlock->rw_state, state,
2865 &oldstate, state + 1);
2867 umtx_key_release(&uq->uq_key);
2870 if (oldstate == state) {
2871 umtx_key_release(&uq->uq_key);
2874 error = umtxq_check_susp(td);
2883 /* grab monitor lock */
2884 umtxq_lock(&uq->uq_key);
2885 umtxq_busy(&uq->uq_key);
2886 umtxq_unlock(&uq->uq_key);
2889 * re-read the state, in case it changed between the try-lock above
2890 * and the check below
2892 rv = fueword32(&rwlock->rw_state, &state);
2896 /* set read contention bit */
2897 while (error == 0 && (state & wrflags) &&
2898 !(state & URWLOCK_READ_WAITERS)) {
2899 rv = casueword32(&rwlock->rw_state, state,
2900 &oldstate, state | URWLOCK_READ_WAITERS);
2905 if (oldstate == state)
2908 error = umtxq_check_susp(td);
2913 umtxq_unbusy_unlocked(&uq->uq_key);
2917 /* state is changed while setting flags, restart */
2918 if (!(state & wrflags)) {
2919 umtxq_unbusy_unlocked(&uq->uq_key);
2920 error = umtxq_check_susp(td);
2927 /* contention bit is set, before sleeping, increase read waiter count */
2928 rv = fueword32(&rwlock->rw_blocked_readers,
2931 umtxq_unbusy_unlocked(&uq->uq_key);
2935 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2937 while (state & wrflags) {
2938 umtxq_lock(&uq->uq_key);
2940 umtxq_unbusy(&uq->uq_key);
2942 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2945 umtxq_busy(&uq->uq_key);
2947 umtxq_unlock(&uq->uq_key);
2950 rv = fueword32(&rwlock->rw_state, &state);
2957 /* decrease read waiter count, and may clear read contention bit */
2958 rv = fueword32(&rwlock->rw_blocked_readers,
2961 umtxq_unbusy_unlocked(&uq->uq_key);
2965 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2966 if (blocked_readers == 1) {
2967 rv = fueword32(&rwlock->rw_state, &state);
2970 while (error == 0) {
2971 rv = casueword32(&rwlock->rw_state, state,
2972 &oldstate, state & ~URWLOCK_READ_WAITERS);
2977 if (oldstate == state)
2980 error = umtxq_check_susp(td);
2984 umtxq_unbusy_unlocked(&uq->uq_key);
2988 umtx_key_release(&uq->uq_key);
2989 if (error == ERESTART)
2995 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2997 struct abs_timeout timo;
3000 int32_t state, oldstate;
3001 int32_t blocked_writers;
3002 int32_t blocked_readers;
3006 error = fueword32(&rwlock->rw_flags, &flags);
3009 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3013 if (timeout != NULL)
3014 abs_timeout_init2(&timo, timeout);
3016 blocked_readers = 0;
3018 rv = fueword32(&rwlock->rw_state, &state);
3020 umtx_key_release(&uq->uq_key);
3023 while (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
3024 rv = casueword32(&rwlock->rw_state, state,
3025 &oldstate, state | URWLOCK_WRITE_OWNER);
3027 umtx_key_release(&uq->uq_key);
3030 if (oldstate == state) {
3031 umtx_key_release(&uq->uq_key);
3035 error = umtxq_check_susp(td);
3041 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) &&
3042 blocked_readers != 0) {
3043 umtxq_lock(&uq->uq_key);
3044 umtxq_busy(&uq->uq_key);
3045 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE);
3046 umtxq_unbusy(&uq->uq_key);
3047 umtxq_unlock(&uq->uq_key);
3053 /* grab monitor lock */
3054 umtxq_lock(&uq->uq_key);
3055 umtxq_busy(&uq->uq_key);
3056 umtxq_unlock(&uq->uq_key);
3059 * re-read the state, in case it changed between the try-lock above
3060 * and the check below
3062 rv = fueword32(&rwlock->rw_state, &state);
3066 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
3067 URWLOCK_READER_COUNT(state) != 0) &&
3068 (state & URWLOCK_WRITE_WAITERS) == 0) {
3069 rv = casueword32(&rwlock->rw_state, state,
3070 &oldstate, state | URWLOCK_WRITE_WAITERS);
3075 if (oldstate == state)
3078 error = umtxq_check_susp(td);
3083 umtxq_unbusy_unlocked(&uq->uq_key);
3087 if (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
3088 umtxq_unbusy_unlocked(&uq->uq_key);
3089 error = umtxq_check_susp(td);
3095 rv = fueword32(&rwlock->rw_blocked_writers,
3098 umtxq_unbusy_unlocked(&uq->uq_key);
3102 suword32(&rwlock->rw_blocked_writers, blocked_writers+1);
3104 while ((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) {
3105 umtxq_lock(&uq->uq_key);
3106 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3107 umtxq_unbusy(&uq->uq_key);
3109 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
3112 umtxq_busy(&uq->uq_key);
3113 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3114 umtxq_unlock(&uq->uq_key);
3117 rv = fueword32(&rwlock->rw_state, &state);
3124 rv = fueword32(&rwlock->rw_blocked_writers,
3127 umtxq_unbusy_unlocked(&uq->uq_key);
3131 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
3132 if (blocked_writers == 1) {
3133 rv = fueword32(&rwlock->rw_state, &state);
3135 umtxq_unbusy_unlocked(&uq->uq_key);
3140 rv = casueword32(&rwlock->rw_state, state,
3141 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3146 if (oldstate == state)
3149 error = umtxq_check_susp(td);
3151 * We are leaving the URWLOCK_WRITE_WAITERS
3152 * behind, but this should not harm the
3158 rv = fueword32(&rwlock->rw_blocked_readers,
3161 umtxq_unbusy_unlocked(&uq->uq_key);
3166 blocked_readers = 0;
3168 umtxq_unbusy_unlocked(&uq->uq_key);
3171 umtx_key_release(&uq->uq_key);
3172 if (error == ERESTART)
3178 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3182 int32_t state, oldstate;
3183 int error, rv, q, count;
3186 error = fueword32(&rwlock->rw_flags, &flags);
3189 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3193 error = fueword32(&rwlock->rw_state, &state);
3198 if (state & URWLOCK_WRITE_OWNER) {
3200 rv = casueword32(&rwlock->rw_state, state,
3201 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3206 if (oldstate != state) {
3208 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3212 error = umtxq_check_susp(td);
3218 } else if (URWLOCK_READER_COUNT(state) != 0) {
3220 rv = casueword32(&rwlock->rw_state, state,
3221 &oldstate, state - 1);
3226 if (oldstate != state) {
3228 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3232 error = umtxq_check_susp(td);
3245 if (!(flags & URWLOCK_PREFER_READER)) {
3246 if (state & URWLOCK_WRITE_WAITERS) {
3248 q = UMTX_EXCLUSIVE_QUEUE;
3249 } else if (state & URWLOCK_READ_WAITERS) {
3251 q = UMTX_SHARED_QUEUE;
3254 if (state & URWLOCK_READ_WAITERS) {
3256 q = UMTX_SHARED_QUEUE;
3257 } else if (state & URWLOCK_WRITE_WAITERS) {
3259 q = UMTX_EXCLUSIVE_QUEUE;
3264 umtxq_lock(&uq->uq_key);
3265 umtxq_busy(&uq->uq_key);
3266 umtxq_signal_queue(&uq->uq_key, count, q);
3267 umtxq_unbusy(&uq->uq_key);
3268 umtxq_unlock(&uq->uq_key);
3271 umtx_key_release(&uq->uq_key);
3276 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3278 struct abs_timeout timo;
3280 uint32_t flags, count, count1;
3284 error = fueword32(&sem->_flags, &flags);
3287 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3291 if (timeout != NULL)
3292 abs_timeout_init2(&timo, timeout);
3294 umtxq_lock(&uq->uq_key);
3295 umtxq_busy(&uq->uq_key);
3297 umtxq_unlock(&uq->uq_key);
3298 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3300 rv = fueword32(&sem->_count, &count);
3301 if (rv == -1 || count != 0) {
3302 umtxq_lock(&uq->uq_key);
3303 umtxq_unbusy(&uq->uq_key);
3305 umtxq_unlock(&uq->uq_key);
3306 umtx_key_release(&uq->uq_key);
3307 return (rv == -1 ? EFAULT : 0);
3309 umtxq_lock(&uq->uq_key);
3310 umtxq_unbusy(&uq->uq_key);
3312 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3314 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3318 /* A relative timeout cannot be restarted. */
3319 if (error == ERESTART && timeout != NULL &&
3320 (timeout->_flags & UMTX_ABSTIME) == 0)
3323 umtxq_unlock(&uq->uq_key);
3324 umtx_key_release(&uq->uq_key);
3329 * Signal a userland condition variable.
3332 do_sem_wake(struct thread *td, struct _usem *sem)
3334 struct umtx_key key;
3338 error = fueword32(&sem->_flags, &flags);
3341 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3345 cnt = umtxq_count(&key);
3347 umtxq_signal(&key, 1);
3349 * Check if count is greater than 0, this means the memory is
3350 * still being referenced by user code, so we can safely
3351 * update _has_waiters flag.
3355 error = suword32(&sem->_has_waiters, 0);
3363 umtx_key_release(&key);
3368 sys__umtx_lock(struct thread *td, struct _umtx_lock_args *uap)
3369 /* struct umtx *umtx */
3371 return do_lock_umtx(td, uap->umtx, td->td_tid, 0);
3375 sys__umtx_unlock(struct thread *td, struct _umtx_unlock_args *uap)
3376 /* struct umtx *umtx */
3378 return do_unlock_umtx(td, uap->umtx, td->td_tid);
3382 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3386 error = copyin(addr, tsp, sizeof(struct timespec));
3388 if (tsp->tv_sec < 0 ||
3389 tsp->tv_nsec >= 1000000000 ||
3397 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3401 if (size <= sizeof(struct timespec)) {
3402 tp->_clockid = CLOCK_REALTIME;
3404 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3406 error = copyin(addr, tp, sizeof(struct _umtx_time));
3409 if (tp->_timeout.tv_sec < 0 ||
3410 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3416 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap)
3418 struct timespec *ts, timeout;
3421 /* Allow a null timespec (wait forever). */
3422 if (uap->uaddr2 == NULL)
3425 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3430 return (do_lock_umtx(td, uap->obj, uap->val, ts));
3434 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap)
3436 return (do_unlock_umtx(td, uap->obj, uap->val));
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)
3496 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3499 #define BATCH_SIZE 128
3501 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3503 int count = uap->val;
3504 void *uaddrs[BATCH_SIZE];
3505 char **upp = (char **)uap->obj;
3512 if (tocopy > BATCH_SIZE)
3513 tocopy = BATCH_SIZE;
3514 error = copyin(upp+pos, uaddrs, tocopy * sizeof(char *));
3517 for (i = 0; i < tocopy; ++i)
3518 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3526 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3528 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3532 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3534 struct _umtx_time *tm_p, timeout;
3537 /* Allow a null timespec (wait forever). */
3538 if (uap->uaddr2 == NULL)
3541 error = umtx_copyin_umtx_time(
3542 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3547 return do_lock_umutex(td, uap->obj, tm_p, 0);
3551 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3553 return do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY);
3557 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3559 struct _umtx_time *tm_p, timeout;
3562 /* Allow a null timespec (wait forever). */
3563 if (uap->uaddr2 == NULL)
3566 error = umtx_copyin_umtx_time(
3567 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3572 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3576 __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)
3584 return do_unlock_umutex(td, uap->obj);
3588 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3590 return do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1);
3594 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3596 struct timespec *ts, timeout;
3599 /* Allow a null timespec (wait forever). */
3600 if (uap->uaddr2 == NULL)
3603 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3608 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3612 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3614 return do_cv_signal(td, uap->obj);
3618 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3620 return do_cv_broadcast(td, uap->obj);
3624 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3626 struct _umtx_time timeout;
3629 /* Allow a null timespec (wait forever). */
3630 if (uap->uaddr2 == NULL) {
3631 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3633 error = umtx_copyin_umtx_time(uap->uaddr2,
3634 (size_t)uap->uaddr1, &timeout);
3637 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3643 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3645 struct _umtx_time timeout;
3648 /* Allow a null timespec (wait forever). */
3649 if (uap->uaddr2 == NULL) {
3650 error = do_rw_wrlock(td, uap->obj, 0);
3652 error = umtx_copyin_umtx_time(uap->uaddr2,
3653 (size_t)uap->uaddr1, &timeout);
3657 error = do_rw_wrlock(td, uap->obj, &timeout);
3663 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3665 return do_rw_unlock(td, uap->obj);
3669 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3671 struct _umtx_time *tm_p, timeout;
3674 /* Allow a null timespec (wait forever). */
3675 if (uap->uaddr2 == NULL)
3678 error = umtx_copyin_umtx_time(
3679 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3684 return (do_sem_wait(td, uap->obj, tm_p));
3688 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3690 return do_sem_wake(td, uap->obj);
3694 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3696 return do_wake2_umutex(td, uap->obj, uap->val);
3699 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
3701 static _umtx_op_func op_table[] = {
3702 __umtx_op_lock_umtx, /* UMTX_OP_LOCK */
3703 __umtx_op_unlock_umtx, /* UMTX_OP_UNLOCK */
3704 __umtx_op_wait, /* UMTX_OP_WAIT */
3705 __umtx_op_wake, /* UMTX_OP_WAKE */
3706 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_TRYLOCK */
3707 __umtx_op_lock_umutex, /* UMTX_OP_MUTEX_LOCK */
3708 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3709 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3710 __umtx_op_cv_wait, /* UMTX_OP_CV_WAIT*/
3711 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3712 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3713 __umtx_op_wait_uint, /* UMTX_OP_WAIT_UINT */
3714 __umtx_op_rw_rdlock, /* UMTX_OP_RW_RDLOCK */
3715 __umtx_op_rw_wrlock, /* UMTX_OP_RW_WRLOCK */
3716 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3717 __umtx_op_wait_uint_private, /* UMTX_OP_WAIT_UINT_PRIVATE */
3718 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3719 __umtx_op_wait_umutex, /* UMTX_OP_UMUTEX_WAIT */
3720 __umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */
3721 __umtx_op_sem_wait, /* UMTX_OP_SEM_WAIT */
3722 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3723 __umtx_op_nwake_private, /* UMTX_OP_NWAKE_PRIVATE */
3724 __umtx_op_wake2_umutex /* UMTX_OP_UMUTEX_WAKE2 */
3728 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
3730 if ((unsigned)uap->op < UMTX_OP_MAX)
3731 return (*op_table[uap->op])(td, uap);
3735 #ifdef COMPAT_FREEBSD32
3737 freebsd32_umtx_lock(struct thread *td, struct freebsd32_umtx_lock_args *uap)
3738 /* struct umtx *umtx */
3740 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
3744 freebsd32_umtx_unlock(struct thread *td, struct freebsd32_umtx_unlock_args *uap)
3745 /* struct umtx *umtx */
3747 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
3755 struct umtx_time32 {
3756 struct timespec32 timeout;
3762 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
3764 struct timespec32 ts32;
3767 error = copyin(addr, &ts32, sizeof(struct timespec32));
3769 if (ts32.tv_sec < 0 ||
3770 ts32.tv_nsec >= 1000000000 ||
3774 tsp->tv_sec = ts32.tv_sec;
3775 tsp->tv_nsec = ts32.tv_nsec;
3782 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
3784 struct umtx_time32 t32;
3787 t32.clockid = CLOCK_REALTIME;
3789 if (size <= sizeof(struct timespec32))
3790 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
3792 error = copyin(addr, &t32, sizeof(struct umtx_time32));
3795 if (t32.timeout.tv_sec < 0 ||
3796 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
3798 tp->_timeout.tv_sec = t32.timeout.tv_sec;
3799 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
3800 tp->_flags = t32.flags;
3801 tp->_clockid = t32.clockid;
3806 __umtx_op_lock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
3808 struct timespec *ts, timeout;
3811 /* Allow a null timespec (wait forever). */
3812 if (uap->uaddr2 == NULL)
3815 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3820 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
3824 __umtx_op_unlock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
3826 return (do_unlock_umtx32(td, uap->obj, (uint32_t)uap->val));
3830 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3832 struct _umtx_time *tm_p, timeout;
3835 if (uap->uaddr2 == NULL)
3838 error = umtx_copyin_umtx_time32(uap->uaddr2,
3839 (size_t)uap->uaddr1, &timeout);
3844 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3848 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3850 struct _umtx_time *tm_p, timeout;
3853 /* Allow a null timespec (wait forever). */
3854 if (uap->uaddr2 == NULL)
3857 error = umtx_copyin_umtx_time(uap->uaddr2,
3858 (size_t)uap->uaddr1, &timeout);
3863 return do_lock_umutex(td, uap->obj, tm_p, 0);
3867 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3869 struct _umtx_time *tm_p, timeout;
3872 /* Allow a null timespec (wait forever). */
3873 if (uap->uaddr2 == NULL)
3876 error = umtx_copyin_umtx_time32(uap->uaddr2,
3877 (size_t)uap->uaddr1, &timeout);
3882 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3886 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3888 struct timespec *ts, timeout;
3891 /* Allow a null timespec (wait forever). */
3892 if (uap->uaddr2 == NULL)
3895 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3900 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3904 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3906 struct _umtx_time timeout;
3909 /* Allow a null timespec (wait forever). */
3910 if (uap->uaddr2 == NULL) {
3911 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3913 error = umtx_copyin_umtx_time32(uap->uaddr2,
3914 (size_t)uap->uaddr1, &timeout);
3917 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3923 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3925 struct _umtx_time timeout;
3928 /* Allow a null timespec (wait forever). */
3929 if (uap->uaddr2 == NULL) {
3930 error = do_rw_wrlock(td, uap->obj, 0);
3932 error = umtx_copyin_umtx_time32(uap->uaddr2,
3933 (size_t)uap->uaddr1, &timeout);
3936 error = do_rw_wrlock(td, uap->obj, &timeout);
3942 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3944 struct _umtx_time *tm_p, timeout;
3947 if (uap->uaddr2 == NULL)
3950 error = umtx_copyin_umtx_time32(
3951 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
3956 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3960 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3962 struct _umtx_time *tm_p, timeout;
3965 /* Allow a null timespec (wait forever). */
3966 if (uap->uaddr2 == NULL)
3969 error = umtx_copyin_umtx_time32(uap->uaddr2,
3970 (size_t)uap->uaddr1, &timeout);
3975 return (do_sem_wait(td, uap->obj, tm_p));
3979 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
3981 int count = uap->val;
3982 uint32_t uaddrs[BATCH_SIZE];
3983 uint32_t **upp = (uint32_t **)uap->obj;
3990 if (tocopy > BATCH_SIZE)
3991 tocopy = BATCH_SIZE;
3992 error = copyin(upp+pos, uaddrs, tocopy * sizeof(uint32_t));
3995 for (i = 0; i < tocopy; ++i)
3996 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
4004 static _umtx_op_func op_table_compat32[] = {
4005 __umtx_op_lock_umtx_compat32, /* UMTX_OP_LOCK */
4006 __umtx_op_unlock_umtx_compat32, /* UMTX_OP_UNLOCK */
4007 __umtx_op_wait_compat32, /* UMTX_OP_WAIT */
4008 __umtx_op_wake, /* UMTX_OP_WAKE */
4009 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_LOCK */
4010 __umtx_op_lock_umutex_compat32, /* UMTX_OP_MUTEX_TRYLOCK */
4011 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
4012 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
4013 __umtx_op_cv_wait_compat32, /* UMTX_OP_CV_WAIT*/
4014 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
4015 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
4016 __umtx_op_wait_compat32, /* UMTX_OP_WAIT_UINT */
4017 __umtx_op_rw_rdlock_compat32, /* UMTX_OP_RW_RDLOCK */
4018 __umtx_op_rw_wrlock_compat32, /* UMTX_OP_RW_WRLOCK */
4019 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
4020 __umtx_op_wait_uint_private_compat32, /* UMTX_OP_WAIT_UINT_PRIVATE */
4021 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
4022 __umtx_op_wait_umutex_compat32, /* UMTX_OP_UMUTEX_WAIT */
4023 __umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */
4024 __umtx_op_sem_wait_compat32, /* UMTX_OP_SEM_WAIT */
4025 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
4026 __umtx_op_nwake_private32, /* UMTX_OP_NWAKE_PRIVATE */
4027 __umtx_op_wake2_umutex /* UMTX_OP_UMUTEX_WAKE2 */
4031 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
4033 if ((unsigned)uap->op < UMTX_OP_MAX)
4034 return (*op_table_compat32[uap->op])(td,
4035 (struct _umtx_op_args *)uap);
4041 umtx_thread_init(struct thread *td)
4043 td->td_umtxq = umtxq_alloc();
4044 td->td_umtxq->uq_thread = td;
4048 umtx_thread_fini(struct thread *td)
4050 umtxq_free(td->td_umtxq);
4054 * It will be called when new thread is created, e.g fork().
4057 umtx_thread_alloc(struct thread *td)
4062 uq->uq_inherited_pri = PRI_MAX;
4064 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4065 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4066 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4067 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4074 umtx_exec_hook(void *arg __unused, struct proc *p __unused,
4075 struct image_params *imgp __unused)
4077 umtx_thread_cleanup(curthread);
4081 * thread_exit() hook.
4084 umtx_thread_exit(struct thread *td)
4086 umtx_thread_cleanup(td);
4090 * clean up umtx data.
4093 umtx_thread_cleanup(struct thread *td)
4098 if ((uq = td->td_umtxq) == NULL)
4101 mtx_lock(&umtx_lock);
4102 uq->uq_inherited_pri = PRI_MAX;
4103 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
4104 pi->pi_owner = NULL;
4105 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
4107 mtx_unlock(&umtx_lock);
4109 sched_lend_user_prio(td, PRI_MAX);