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 <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/limits.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
40 #include <sys/sched.h>
42 #include <sys/sysctl.h>
43 #include <sys/sysent.h>
44 #include <sys/systm.h>
45 #include <sys/sysproto.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/eventhandler.h>
51 #include <vm/vm_param.h>
53 #include <vm/vm_map.h>
54 #include <vm/vm_object.h>
56 #include <machine/cpu.h>
58 #ifdef COMPAT_FREEBSD32
59 #include <compat/freebsd32/freebsd32_proto.h>
63 #define _UMUTEX_WAIT 2
65 /* Priority inheritance mutex info. */
68 struct thread *pi_owner;
73 /* List entry to link umtx holding by thread */
74 TAILQ_ENTRY(umtx_pi) pi_link;
76 /* List entry in hash */
77 TAILQ_ENTRY(umtx_pi) pi_hashlink;
79 /* List for waiters */
80 TAILQ_HEAD(,umtx_q) pi_blocked;
82 /* Identify a userland lock object */
83 struct umtx_key pi_key;
86 /* A userland synchronous object user. */
88 /* Linked list for the hash. */
89 TAILQ_ENTRY(umtx_q) uq_link;
92 struct umtx_key uq_key;
96 #define UQF_UMTXQ 0x0001
98 /* The thread waits on. */
99 struct thread *uq_thread;
102 * Blocked on PI mutex. read can use chain lock
103 * or umtx_lock, write must have both chain lock and
104 * umtx_lock being hold.
106 struct umtx_pi *uq_pi_blocked;
108 /* On blocked list */
109 TAILQ_ENTRY(umtx_q) uq_lockq;
111 /* Thread contending with us */
112 TAILQ_HEAD(,umtx_pi) uq_pi_contested;
114 /* Inherited priority from PP mutex */
115 u_char uq_inherited_pri;
117 /* Spare queue ready to be reused */
118 struct umtxq_queue *uq_spare_queue;
120 /* The queue we on */
121 struct umtxq_queue *uq_cur_queue;
124 TAILQ_HEAD(umtxq_head, umtx_q);
126 /* Per-key wait-queue */
128 struct umtxq_head head;
130 LIST_ENTRY(umtxq_queue) link;
134 LIST_HEAD(umtxq_list, umtxq_queue);
136 /* Userland lock object's wait-queue chain */
138 /* Lock for this chain. */
141 /* List of sleep queues. */
142 struct umtxq_list uc_queue[2];
143 #define UMTX_SHARED_QUEUE 0
144 #define UMTX_EXCLUSIVE_QUEUE 1
146 LIST_HEAD(, umtxq_queue) uc_spare_queue;
151 /* Chain lock waiters */
154 /* All PI in the list */
155 TAILQ_HEAD(,umtx_pi) uc_pi_list;
159 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
160 #define UMTXQ_BUSY_ASSERT(uc) KASSERT(&(uc)->uc_busy, ("umtx chain is not busy"))
163 * Don't propagate time-sharing priority, there is a security reason,
164 * a user can simply introduce PI-mutex, let thread A lock the mutex,
165 * and let another thread B block on the mutex, because B is
166 * sleeping, its priority will be boosted, this causes A's priority to
167 * be boosted via priority propagating too and will never be lowered even
168 * if it is using 100%CPU, this is unfair to other processes.
171 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
172 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
173 PRI_MAX_TIMESHARE : (td)->td_user_pri)
175 #define GOLDEN_RATIO_PRIME 2654404609U
176 #define UMTX_CHAINS 512
177 #define UMTX_SHIFTS (__WORD_BIT - 9)
179 #define GET_SHARE(flags) \
180 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
182 #define BUSY_SPINS 200
184 static uma_zone_t umtx_pi_zone;
185 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
186 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
187 static int umtx_pi_allocated;
189 SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW, 0, "umtx debug");
190 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
191 &umtx_pi_allocated, 0, "Allocated umtx_pi");
193 static void umtxq_sysinit(void *);
194 static void umtxq_hash(struct umtx_key *key);
195 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
196 static void umtxq_lock(struct umtx_key *key);
197 static void umtxq_unlock(struct umtx_key *key);
198 static void umtxq_busy(struct umtx_key *key);
199 static void umtxq_unbusy(struct umtx_key *key);
200 static void umtxq_insert_queue(struct umtx_q *uq, int q);
201 static void umtxq_remove_queue(struct umtx_q *uq, int q);
202 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, int timo);
203 static int umtxq_count(struct umtx_key *key);
204 static struct umtx_pi *umtx_pi_alloc(int);
205 static void umtx_pi_free(struct umtx_pi *pi);
206 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags);
207 static void umtx_thread_cleanup(struct thread *td);
208 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
209 struct image_params *imgp __unused);
210 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
212 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
213 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
214 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
216 static struct mtx umtx_lock;
219 umtxq_sysinit(void *arg __unused)
223 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
224 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
225 for (i = 0; i < 2; ++i) {
226 for (j = 0; j < UMTX_CHAINS; ++j) {
227 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
228 MTX_DEF | MTX_DUPOK);
229 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
230 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
231 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
232 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
233 umtxq_chains[i][j].uc_busy = 0;
234 umtxq_chains[i][j].uc_waiters = 0;
237 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_SPIN);
238 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
239 EVENTHANDLER_PRI_ANY);
247 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
248 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX, M_WAITOK | M_ZERO);
249 TAILQ_INIT(&uq->uq_spare_queue->head);
250 TAILQ_INIT(&uq->uq_pi_contested);
251 uq->uq_inherited_pri = PRI_MAX;
256 umtxq_free(struct umtx_q *uq)
258 MPASS(uq->uq_spare_queue != NULL);
259 free(uq->uq_spare_queue, M_UMTX);
264 umtxq_hash(struct umtx_key *key)
266 unsigned n = (uintptr_t)key->info.both.a + key->info.both.b;
267 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
270 static inline struct umtxq_chain *
271 umtxq_getchain(struct umtx_key *key)
273 if (key->type <= TYPE_SEM)
274 return (&umtxq_chains[1][key->hash]);
275 return (&umtxq_chains[0][key->hash]);
282 umtxq_lock(struct umtx_key *key)
284 struct umtxq_chain *uc;
286 uc = umtxq_getchain(key);
287 mtx_lock(&uc->uc_lock);
294 umtxq_unlock(struct umtx_key *key)
296 struct umtxq_chain *uc;
298 uc = umtxq_getchain(key);
299 mtx_unlock(&uc->uc_lock);
303 * Set chain to busy state when following operation
304 * may be blocked (kernel mutex can not be used).
307 umtxq_busy(struct umtx_key *key)
309 struct umtxq_chain *uc;
311 uc = umtxq_getchain(key);
312 mtx_assert(&uc->uc_lock, MA_OWNED);
316 int count = BUSY_SPINS;
319 while (uc->uc_busy && --count > 0)
325 while (uc->uc_busy) {
327 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
338 umtxq_unbusy(struct umtx_key *key)
340 struct umtxq_chain *uc;
342 uc = umtxq_getchain(key);
343 mtx_assert(&uc->uc_lock, MA_OWNED);
344 KASSERT(uc->uc_busy != 0, ("not busy"));
350 static struct umtxq_queue *
351 umtxq_queue_lookup(struct umtx_key *key, int q)
353 struct umtxq_queue *uh;
354 struct umtxq_chain *uc;
356 uc = umtxq_getchain(key);
357 UMTXQ_LOCKED_ASSERT(uc);
358 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
359 if (umtx_key_match(&uh->key, key))
367 umtxq_insert_queue(struct umtx_q *uq, int q)
369 struct umtxq_queue *uh;
370 struct umtxq_chain *uc;
372 uc = umtxq_getchain(&uq->uq_key);
373 UMTXQ_LOCKED_ASSERT(uc);
374 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
375 uh = umtxq_queue_lookup(&uq->uq_key, q);
377 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
379 uh = uq->uq_spare_queue;
380 uh->key = uq->uq_key;
381 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
383 uq->uq_spare_queue = NULL;
385 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
387 uq->uq_flags |= UQF_UMTXQ;
388 uq->uq_cur_queue = uh;
393 umtxq_remove_queue(struct umtx_q *uq, int q)
395 struct umtxq_chain *uc;
396 struct umtxq_queue *uh;
398 uc = umtxq_getchain(&uq->uq_key);
399 UMTXQ_LOCKED_ASSERT(uc);
400 if (uq->uq_flags & UQF_UMTXQ) {
401 uh = uq->uq_cur_queue;
402 TAILQ_REMOVE(&uh->head, uq, uq_link);
404 uq->uq_flags &= ~UQF_UMTXQ;
405 if (TAILQ_EMPTY(&uh->head)) {
406 KASSERT(uh->length == 0,
407 ("inconsistent umtxq_queue length"));
408 LIST_REMOVE(uh, link);
410 uh = LIST_FIRST(&uc->uc_spare_queue);
411 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
412 LIST_REMOVE(uh, link);
414 uq->uq_spare_queue = uh;
415 uq->uq_cur_queue = NULL;
420 * Check if there are multiple waiters
423 umtxq_count(struct umtx_key *key)
425 struct umtxq_chain *uc;
426 struct umtxq_queue *uh;
428 uc = umtxq_getchain(key);
429 UMTXQ_LOCKED_ASSERT(uc);
430 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
437 * Check if there are multiple PI waiters and returns first
441 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
443 struct umtxq_chain *uc;
444 struct umtxq_queue *uh;
447 uc = umtxq_getchain(key);
448 UMTXQ_LOCKED_ASSERT(uc);
449 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
451 *first = TAILQ_FIRST(&uh->head);
458 * Wake up threads waiting on an userland object.
462 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
464 struct umtxq_chain *uc;
465 struct umtxq_queue *uh;
470 uc = umtxq_getchain(key);
471 UMTXQ_LOCKED_ASSERT(uc);
472 uh = umtxq_queue_lookup(key, q);
474 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
475 umtxq_remove_queue(uq, q);
486 * Wake up specified thread.
489 umtxq_signal_thread(struct umtx_q *uq)
491 struct umtxq_chain *uc;
493 uc = umtxq_getchain(&uq->uq_key);
494 UMTXQ_LOCKED_ASSERT(uc);
500 * Put thread into sleep state, before sleeping, check if
501 * thread was removed from umtx queue.
504 umtxq_sleep(struct umtx_q *uq, const char *wmesg, int timo)
506 struct umtxq_chain *uc;
509 uc = umtxq_getchain(&uq->uq_key);
510 UMTXQ_LOCKED_ASSERT(uc);
511 if (!(uq->uq_flags & UQF_UMTXQ))
513 error = msleep(uq, &uc->uc_lock, PCATCH, wmesg, timo);
514 if (error == EWOULDBLOCK)
520 * Convert userspace address into unique logical address.
523 umtx_key_get(void *addr, int type, int share, struct umtx_key *key)
525 struct thread *td = curthread;
527 vm_map_entry_t entry;
533 if (share == THREAD_SHARE) {
535 key->info.private.vs = td->td_proc->p_vmspace;
536 key->info.private.addr = (uintptr_t)addr;
538 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
539 map = &td->td_proc->p_vmspace->vm_map;
540 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
541 &entry, &key->info.shared.object, &pindex, &prot,
542 &wired) != KERN_SUCCESS) {
546 if ((share == PROCESS_SHARE) ||
547 (share == AUTO_SHARE &&
548 VM_INHERIT_SHARE == entry->inheritance)) {
550 key->info.shared.offset = entry->offset + entry->start -
552 vm_object_reference(key->info.shared.object);
555 key->info.private.vs = td->td_proc->p_vmspace;
556 key->info.private.addr = (uintptr_t)addr;
558 vm_map_lookup_done(map, entry);
569 umtx_key_release(struct umtx_key *key)
572 vm_object_deallocate(key->info.shared.object);
576 * Lock a umtx object.
579 _do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id, int timo)
589 * Care must be exercised when dealing with umtx structure. It
590 * can fault on any access.
594 * Try the uncontested case. This should be done in userland.
596 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
598 /* The acquire succeeded. */
599 if (owner == UMTX_UNOWNED)
602 /* The address was invalid. */
606 /* If no one owns it but it is contested try to acquire it. */
607 if (owner == UMTX_CONTESTED) {
608 owner = casuword(&umtx->u_owner,
609 UMTX_CONTESTED, id | UMTX_CONTESTED);
611 if (owner == UMTX_CONTESTED)
614 /* The address was invalid. */
618 /* If this failed the lock has changed, restart. */
623 * If we caught a signal, we have retried and now
629 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
630 AUTO_SHARE, &uq->uq_key)) != 0)
633 umtxq_lock(&uq->uq_key);
634 umtxq_busy(&uq->uq_key);
636 umtxq_unbusy(&uq->uq_key);
637 umtxq_unlock(&uq->uq_key);
640 * Set the contested bit so that a release in user space
641 * knows to use the system call for unlock. If this fails
642 * either some one else has acquired the lock or it has been
645 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
647 /* The address was invalid. */
649 umtxq_lock(&uq->uq_key);
651 umtxq_unlock(&uq->uq_key);
652 umtx_key_release(&uq->uq_key);
657 * We set the contested bit, sleep. Otherwise the lock changed
658 * and we need to retry or we lost a race to the thread
659 * unlocking the umtx.
661 umtxq_lock(&uq->uq_key);
663 error = umtxq_sleep(uq, "umtx", timo);
665 umtxq_unlock(&uq->uq_key);
666 umtx_key_release(&uq->uq_key);
673 * Lock a umtx object.
676 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
677 struct timespec *timeout)
679 struct timespec ts, ts2, ts3;
683 if (timeout == NULL) {
684 error = _do_lock_umtx(td, umtx, id, 0);
685 /* Mutex locking is restarted if it is interrupted. */
690 timespecadd(&ts, timeout);
691 TIMESPEC_TO_TIMEVAL(&tv, timeout);
693 error = _do_lock_umtx(td, umtx, id, tvtohz(&tv));
694 if (error != ETIMEDOUT)
697 if (timespeccmp(&ts2, &ts, >=)) {
702 timespecsub(&ts3, &ts2);
703 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
705 /* Timed-locking is not restarted. */
706 if (error == ERESTART)
713 * Unlock a umtx object.
716 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
725 * Make sure we own this mtx.
727 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
731 if ((owner & ~UMTX_CONTESTED) != id)
734 /* This should be done in userland */
735 if ((owner & UMTX_CONTESTED) == 0) {
736 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
744 /* We should only ever be in here for contested locks */
745 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
751 count = umtxq_count(&key);
755 * When unlocking the umtx, it must be marked as unowned if
756 * there is zero or one thread only waiting for it.
757 * Otherwise, it must be marked as contested.
759 old = casuword(&umtx->u_owner, owner,
760 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
762 umtxq_signal(&key,1);
765 umtx_key_release(&key);
773 #ifdef COMPAT_FREEBSD32
776 * Lock a umtx object.
779 _do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id, int timo)
789 * Care must be exercised when dealing with umtx structure. It
790 * can fault on any access.
794 * Try the uncontested case. This should be done in userland.
796 owner = casuword32(m, UMUTEX_UNOWNED, id);
798 /* The acquire succeeded. */
799 if (owner == UMUTEX_UNOWNED)
802 /* The address was invalid. */
806 /* If no one owns it but it is contested try to acquire it. */
807 if (owner == UMUTEX_CONTESTED) {
808 owner = casuword32(m,
809 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
810 if (owner == UMUTEX_CONTESTED)
813 /* The address was invalid. */
817 /* If this failed the lock has changed, restart. */
822 * If we caught a signal, we have retried and now
828 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
829 AUTO_SHARE, &uq->uq_key)) != 0)
832 umtxq_lock(&uq->uq_key);
833 umtxq_busy(&uq->uq_key);
835 umtxq_unbusy(&uq->uq_key);
836 umtxq_unlock(&uq->uq_key);
839 * Set the contested bit so that a release in user space
840 * knows to use the system call for unlock. If this fails
841 * either some one else has acquired the lock or it has been
844 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
846 /* The address was invalid. */
848 umtxq_lock(&uq->uq_key);
850 umtxq_unlock(&uq->uq_key);
851 umtx_key_release(&uq->uq_key);
856 * We set the contested bit, sleep. Otherwise the lock changed
857 * and we need to retry or we lost a race to the thread
858 * unlocking the umtx.
860 umtxq_lock(&uq->uq_key);
862 error = umtxq_sleep(uq, "umtx", timo);
864 umtxq_unlock(&uq->uq_key);
865 umtx_key_release(&uq->uq_key);
872 * Lock a umtx object.
875 do_lock_umtx32(struct thread *td, void *m, uint32_t id,
876 struct timespec *timeout)
878 struct timespec ts, ts2, ts3;
882 if (timeout == NULL) {
883 error = _do_lock_umtx32(td, m, id, 0);
884 /* Mutex locking is restarted if it is interrupted. */
889 timespecadd(&ts, timeout);
890 TIMESPEC_TO_TIMEVAL(&tv, timeout);
892 error = _do_lock_umtx32(td, m, id, tvtohz(&tv));
893 if (error != ETIMEDOUT)
896 if (timespeccmp(&ts2, &ts, >=)) {
901 timespecsub(&ts3, &ts2);
902 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
904 /* Timed-locking is not restarted. */
905 if (error == ERESTART)
912 * Unlock a umtx object.
915 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
924 * Make sure we own this mtx.
930 if ((owner & ~UMUTEX_CONTESTED) != id)
933 /* This should be done in userland */
934 if ((owner & UMUTEX_CONTESTED) == 0) {
935 old = casuword32(m, owner, UMUTEX_UNOWNED);
943 /* We should only ever be in here for contested locks */
944 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
950 count = umtxq_count(&key);
954 * When unlocking the umtx, it must be marked as unowned if
955 * there is zero or one thread only waiting for it.
956 * Otherwise, it must be marked as contested.
958 old = casuword32(m, owner,
959 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
961 umtxq_signal(&key,1);
964 umtx_key_release(&key);
974 * Fetch and compare value, sleep on the address if value is not changed.
977 do_wait(struct thread *td, void *addr, u_long id,
978 struct timespec *timeout, int compat32, int is_private)
981 struct timespec ts, ts2, ts3;
987 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
988 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
991 umtxq_lock(&uq->uq_key);
993 umtxq_unlock(&uq->uq_key);
997 tmp = (unsigned int)fuword32(addr);
999 umtxq_lock(&uq->uq_key);
1001 umtxq_unlock(&uq->uq_key);
1002 } else if (timeout == NULL) {
1003 umtxq_lock(&uq->uq_key);
1004 error = umtxq_sleep(uq, "uwait", 0);
1006 umtxq_unlock(&uq->uq_key);
1009 timespecadd(&ts, timeout);
1010 TIMESPEC_TO_TIMEVAL(&tv, timeout);
1011 umtxq_lock(&uq->uq_key);
1013 error = umtxq_sleep(uq, "uwait", tvtohz(&tv));
1014 if (!(uq->uq_flags & UQF_UMTXQ)) {
1018 if (error != ETIMEDOUT)
1020 umtxq_unlock(&uq->uq_key);
1021 getnanouptime(&ts2);
1022 if (timespeccmp(&ts2, &ts, >=)) {
1024 umtxq_lock(&uq->uq_key);
1028 timespecsub(&ts3, &ts2);
1029 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
1030 umtxq_lock(&uq->uq_key);
1033 umtxq_unlock(&uq->uq_key);
1035 umtx_key_release(&uq->uq_key);
1036 if (error == ERESTART)
1042 * Wake up threads sleeping on the specified address.
1045 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1047 struct umtx_key key;
1050 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1051 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1054 ret = umtxq_signal(&key, n_wake);
1056 umtx_key_release(&key);
1061 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1064 _do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags, int timo,
1068 uint32_t owner, old, id;
1075 * Care must be exercised when dealing with umtx structure. It
1076 * can fault on any access.
1079 owner = fuword32(__DEVOLATILE(void *, &m->m_owner));
1080 if (mode == _UMUTEX_WAIT) {
1081 if (owner == UMUTEX_UNOWNED || owner == UMUTEX_CONTESTED)
1085 * Try the uncontested case. This should be done in userland.
1087 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id);
1089 /* The acquire succeeded. */
1090 if (owner == UMUTEX_UNOWNED)
1093 /* The address was invalid. */
1097 /* If no one owns it but it is contested try to acquire it. */
1098 if (owner == UMUTEX_CONTESTED) {
1099 owner = casuword32(&m->m_owner,
1100 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1102 if (owner == UMUTEX_CONTESTED)
1105 /* The address was invalid. */
1109 /* If this failed the lock has changed, restart. */
1114 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1115 (owner & ~UMUTEX_CONTESTED) == id)
1118 if (mode == _UMUTEX_TRY)
1122 * If we caught a signal, we have retried and now
1128 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1129 GET_SHARE(flags), &uq->uq_key)) != 0)
1132 umtxq_lock(&uq->uq_key);
1133 umtxq_busy(&uq->uq_key);
1135 umtxq_unlock(&uq->uq_key);
1138 * Set the contested bit so that a release in user space
1139 * knows to use the system call for unlock. If this fails
1140 * either some one else has acquired the lock or it has been
1143 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED);
1145 /* The address was invalid. */
1147 umtxq_lock(&uq->uq_key);
1149 umtxq_unbusy(&uq->uq_key);
1150 umtxq_unlock(&uq->uq_key);
1151 umtx_key_release(&uq->uq_key);
1156 * We set the contested bit, sleep. Otherwise the lock changed
1157 * and we need to retry or we lost a race to the thread
1158 * unlocking the umtx.
1160 umtxq_lock(&uq->uq_key);
1161 umtxq_unbusy(&uq->uq_key);
1163 error = umtxq_sleep(uq, "umtxn", timo);
1165 umtxq_unlock(&uq->uq_key);
1166 umtx_key_release(&uq->uq_key);
1173 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1176 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1179 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags)
1181 struct umtx_key key;
1182 uint32_t owner, old, id;
1188 * Make sure we own this mtx.
1190 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1194 if ((owner & ~UMUTEX_CONTESTED) != id)
1197 if ((owner & UMUTEX_CONTESTED) == 0) {
1198 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED);
1206 /* We should only ever be in here for contested locks */
1207 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1213 count = umtxq_count(&key);
1217 * When unlocking the umtx, it must be marked as unowned if
1218 * there is zero or one thread only waiting for it.
1219 * Otherwise, it must be marked as contested.
1221 old = casuword32(&m->m_owner, owner,
1222 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1224 umtxq_signal(&key,1);
1227 umtx_key_release(&key);
1236 * Check if the mutex is available and wake up a waiter,
1237 * only for simple mutex.
1240 do_wake_umutex(struct thread *td, struct umutex *m)
1242 struct umtx_key key;
1248 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1252 if ((owner & ~UMUTEX_CONTESTED) != 0)
1255 flags = fuword32(&m->m_flags);
1257 /* We should only ever be in here for contested locks */
1258 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1264 count = umtxq_count(&key);
1268 owner = casuword32(&m->m_owner, UMUTEX_CONTESTED, UMUTEX_UNOWNED);
1271 if (count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1272 umtxq_signal(&key, 1);
1275 umtx_key_release(&key);
1279 static inline struct umtx_pi *
1280 umtx_pi_alloc(int flags)
1284 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1285 TAILQ_INIT(&pi->pi_blocked);
1286 atomic_add_int(&umtx_pi_allocated, 1);
1291 umtx_pi_free(struct umtx_pi *pi)
1293 uma_zfree(umtx_pi_zone, pi);
1294 atomic_add_int(&umtx_pi_allocated, -1);
1298 * Adjust the thread's position on a pi_state after its priority has been
1302 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1304 struct umtx_q *uq, *uq1, *uq2;
1307 mtx_assert(&umtx_lock, MA_OWNED);
1314 * Check if the thread needs to be moved on the blocked chain.
1315 * It needs to be moved if either its priority is lower than
1316 * the previous thread or higher than the next thread.
1318 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1319 uq2 = TAILQ_NEXT(uq, uq_lockq);
1320 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1321 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1323 * Remove thread from blocked chain and determine where
1324 * it should be moved to.
1326 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1327 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1328 td1 = uq1->uq_thread;
1329 MPASS(td1->td_proc->p_magic == P_MAGIC);
1330 if (UPRI(td1) > UPRI(td))
1335 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1337 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1343 * Propagate priority when a thread is blocked on POSIX
1347 umtx_propagate_priority(struct thread *td)
1353 mtx_assert(&umtx_lock, MA_OWNED);
1356 pi = uq->uq_pi_blocked;
1362 if (td == NULL || td == curthread)
1365 MPASS(td->td_proc != NULL);
1366 MPASS(td->td_proc->p_magic == P_MAGIC);
1369 if (td->td_lend_user_pri > pri)
1370 sched_lend_user_prio(td, pri);
1378 * Pick up the lock that td is blocked on.
1381 pi = uq->uq_pi_blocked;
1384 /* Resort td on the list if needed. */
1385 umtx_pi_adjust_thread(pi, td);
1390 * Unpropagate priority for a PI mutex when a thread blocked on
1391 * it is interrupted by signal or resumed by others.
1394 umtx_repropagate_priority(struct umtx_pi *pi)
1396 struct umtx_q *uq, *uq_owner;
1397 struct umtx_pi *pi2;
1400 mtx_assert(&umtx_lock, MA_OWNED);
1402 while (pi != NULL && pi->pi_owner != NULL) {
1404 uq_owner = pi->pi_owner->td_umtxq;
1406 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1407 uq = TAILQ_FIRST(&pi2->pi_blocked);
1409 if (pri > UPRI(uq->uq_thread))
1410 pri = UPRI(uq->uq_thread);
1414 if (pri > uq_owner->uq_inherited_pri)
1415 pri = uq_owner->uq_inherited_pri;
1416 thread_lock(pi->pi_owner);
1417 sched_lend_user_prio(pi->pi_owner, pri);
1418 thread_unlock(pi->pi_owner);
1419 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1420 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1425 * Insert a PI mutex into owned list.
1428 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1430 struct umtx_q *uq_owner;
1432 uq_owner = owner->td_umtxq;
1433 mtx_assert(&umtx_lock, MA_OWNED);
1434 if (pi->pi_owner != NULL)
1435 panic("pi_ower != NULL");
1436 pi->pi_owner = owner;
1437 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1441 * Claim ownership of a PI mutex.
1444 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1446 struct umtx_q *uq, *uq_owner;
1448 uq_owner = owner->td_umtxq;
1449 mtx_lock_spin(&umtx_lock);
1450 if (pi->pi_owner == owner) {
1451 mtx_unlock_spin(&umtx_lock);
1455 if (pi->pi_owner != NULL) {
1457 * userland may have already messed the mutex, sigh.
1459 mtx_unlock_spin(&umtx_lock);
1462 umtx_pi_setowner(pi, owner);
1463 uq = TAILQ_FIRST(&pi->pi_blocked);
1467 pri = UPRI(uq->uq_thread);
1469 if (pri < UPRI(owner))
1470 sched_lend_user_prio(owner, pri);
1471 thread_unlock(owner);
1473 mtx_unlock_spin(&umtx_lock);
1478 * Adjust a thread's order position in its blocked PI mutex,
1479 * this may result new priority propagating process.
1482 umtx_pi_adjust(struct thread *td, u_char oldpri)
1488 mtx_lock_spin(&umtx_lock);
1490 * Pick up the lock that td is blocked on.
1492 pi = uq->uq_pi_blocked;
1494 umtx_pi_adjust_thread(pi, td);
1495 umtx_repropagate_priority(pi);
1497 mtx_unlock_spin(&umtx_lock);
1501 * Sleep on a PI mutex.
1504 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi,
1505 uint32_t owner, const char *wmesg, int timo)
1507 struct umtxq_chain *uc;
1508 struct thread *td, *td1;
1514 KASSERT(td == curthread, ("inconsistent uq_thread"));
1515 uc = umtxq_getchain(&uq->uq_key);
1516 UMTXQ_LOCKED_ASSERT(uc);
1517 UMTXQ_BUSY_ASSERT(uc);
1519 mtx_lock_spin(&umtx_lock);
1520 if (pi->pi_owner == NULL) {
1521 mtx_unlock_spin(&umtx_lock);
1522 /* XXX Only look up thread in current process. */
1523 td1 = tdfind(owner, curproc->p_pid);
1524 mtx_lock_spin(&umtx_lock);
1526 if (pi->pi_owner == NULL)
1527 umtx_pi_setowner(pi, td1);
1528 PROC_UNLOCK(td1->td_proc);
1532 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1533 pri = UPRI(uq1->uq_thread);
1539 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1541 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1543 uq->uq_pi_blocked = pi;
1545 td->td_flags |= TDF_UPIBLOCKED;
1547 umtx_propagate_priority(td);
1548 mtx_unlock_spin(&umtx_lock);
1549 umtxq_unbusy(&uq->uq_key);
1551 if (uq->uq_flags & UQF_UMTXQ) {
1552 error = msleep(uq, &uc->uc_lock, PCATCH, wmesg, timo);
1553 if (error == EWOULDBLOCK)
1555 if (uq->uq_flags & UQF_UMTXQ) {
1559 mtx_lock_spin(&umtx_lock);
1560 uq->uq_pi_blocked = NULL;
1562 td->td_flags &= ~TDF_UPIBLOCKED;
1564 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1565 umtx_repropagate_priority(pi);
1566 mtx_unlock_spin(&umtx_lock);
1567 umtxq_unlock(&uq->uq_key);
1573 * Add reference count for a PI mutex.
1576 umtx_pi_ref(struct umtx_pi *pi)
1578 struct umtxq_chain *uc;
1580 uc = umtxq_getchain(&pi->pi_key);
1581 UMTXQ_LOCKED_ASSERT(uc);
1586 * Decrease reference count for a PI mutex, if the counter
1587 * is decreased to zero, its memory space is freed.
1590 umtx_pi_unref(struct umtx_pi *pi)
1592 struct umtxq_chain *uc;
1594 uc = umtxq_getchain(&pi->pi_key);
1595 UMTXQ_LOCKED_ASSERT(uc);
1596 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1597 if (--pi->pi_refcount == 0) {
1598 mtx_lock_spin(&umtx_lock);
1599 if (pi->pi_owner != NULL) {
1600 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested,
1602 pi->pi_owner = NULL;
1604 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1605 ("blocked queue not empty"));
1606 mtx_unlock_spin(&umtx_lock);
1607 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1613 * Find a PI mutex in hash table.
1615 static struct umtx_pi *
1616 umtx_pi_lookup(struct umtx_key *key)
1618 struct umtxq_chain *uc;
1621 uc = umtxq_getchain(key);
1622 UMTXQ_LOCKED_ASSERT(uc);
1624 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1625 if (umtx_key_match(&pi->pi_key, key)) {
1633 * Insert a PI mutex into hash table.
1636 umtx_pi_insert(struct umtx_pi *pi)
1638 struct umtxq_chain *uc;
1640 uc = umtxq_getchain(&pi->pi_key);
1641 UMTXQ_LOCKED_ASSERT(uc);
1642 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1649 _do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags, int timo,
1653 struct umtx_pi *pi, *new_pi;
1654 uint32_t id, owner, old;
1660 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1663 umtxq_lock(&uq->uq_key);
1664 pi = umtx_pi_lookup(&uq->uq_key);
1666 new_pi = umtx_pi_alloc(M_NOWAIT);
1667 if (new_pi == NULL) {
1668 umtxq_unlock(&uq->uq_key);
1669 new_pi = umtx_pi_alloc(M_WAITOK);
1670 umtxq_lock(&uq->uq_key);
1671 pi = umtx_pi_lookup(&uq->uq_key);
1673 umtx_pi_free(new_pi);
1677 if (new_pi != NULL) {
1678 new_pi->pi_key = uq->uq_key;
1679 umtx_pi_insert(new_pi);
1684 umtxq_unlock(&uq->uq_key);
1687 * Care must be exercised when dealing with umtx structure. It
1688 * can fault on any access.
1692 * Try the uncontested case. This should be done in userland.
1694 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id);
1696 /* The acquire succeeded. */
1697 if (owner == UMUTEX_UNOWNED) {
1702 /* The address was invalid. */
1708 /* If no one owns it but it is contested try to acquire it. */
1709 if (owner == UMUTEX_CONTESTED) {
1710 owner = casuword32(&m->m_owner,
1711 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1713 if (owner == UMUTEX_CONTESTED) {
1714 umtxq_lock(&uq->uq_key);
1715 umtxq_busy(&uq->uq_key);
1716 error = umtx_pi_claim(pi, td);
1717 umtxq_unbusy(&uq->uq_key);
1718 umtxq_unlock(&uq->uq_key);
1722 /* The address was invalid. */
1728 /* If this failed the lock has changed, restart. */
1732 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1733 (owner & ~UMUTEX_CONTESTED) == id) {
1744 * If we caught a signal, we have retried and now
1750 umtxq_lock(&uq->uq_key);
1751 umtxq_busy(&uq->uq_key);
1752 umtxq_unlock(&uq->uq_key);
1755 * Set the contested bit so that a release in user space
1756 * knows to use the system call for unlock. If this fails
1757 * either some one else has acquired the lock or it has been
1760 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED);
1762 /* The address was invalid. */
1764 umtxq_lock(&uq->uq_key);
1765 umtxq_unbusy(&uq->uq_key);
1766 umtxq_unlock(&uq->uq_key);
1771 umtxq_lock(&uq->uq_key);
1773 * We set the contested bit, sleep. Otherwise the lock changed
1774 * and we need to retry or we lost a race to the thread
1775 * unlocking the umtx.
1778 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
1781 umtxq_unbusy(&uq->uq_key);
1782 umtxq_unlock(&uq->uq_key);
1786 umtxq_lock(&uq->uq_key);
1788 umtxq_unlock(&uq->uq_key);
1790 umtx_key_release(&uq->uq_key);
1795 * Unlock a PI mutex.
1798 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags)
1800 struct umtx_key key;
1801 struct umtx_q *uq_first, *uq_first2, *uq_me;
1802 struct umtx_pi *pi, *pi2;
1803 uint32_t owner, old, id;
1810 * Make sure we own this mtx.
1812 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1816 if ((owner & ~UMUTEX_CONTESTED) != id)
1819 /* This should be done in userland */
1820 if ((owner & UMUTEX_CONTESTED) == 0) {
1821 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED);
1829 /* We should only ever be in here for contested locks */
1830 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1836 count = umtxq_count_pi(&key, &uq_first);
1837 if (uq_first != NULL) {
1838 mtx_lock_spin(&umtx_lock);
1839 pi = uq_first->uq_pi_blocked;
1840 KASSERT(pi != NULL, ("pi == NULL?"));
1841 if (pi->pi_owner != curthread) {
1842 mtx_unlock_spin(&umtx_lock);
1845 umtx_key_release(&key);
1846 /* userland messed the mutex */
1849 uq_me = curthread->td_umtxq;
1850 pi->pi_owner = NULL;
1851 TAILQ_REMOVE(&uq_me->uq_pi_contested, pi, pi_link);
1852 /* get highest priority thread which is still sleeping. */
1853 uq_first = TAILQ_FIRST(&pi->pi_blocked);
1854 while (uq_first != NULL &&
1855 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
1856 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
1859 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
1860 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
1861 if (uq_first2 != NULL) {
1862 if (pri > UPRI(uq_first2->uq_thread))
1863 pri = UPRI(uq_first2->uq_thread);
1866 thread_lock(curthread);
1867 sched_lend_user_prio(curthread, pri);
1868 thread_unlock(curthread);
1869 mtx_unlock_spin(&umtx_lock);
1871 umtxq_signal_thread(uq_first);
1876 * When unlocking the umtx, it must be marked as unowned if
1877 * there is zero or one thread only waiting for it.
1878 * Otherwise, it must be marked as contested.
1880 old = casuword32(&m->m_owner, owner,
1881 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1886 umtx_key_release(&key);
1898 _do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags, int timo,
1901 struct umtx_q *uq, *uq2;
1905 int error, pri, old_inherited_pri, su;
1909 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
1912 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
1914 old_inherited_pri = uq->uq_inherited_pri;
1915 umtxq_lock(&uq->uq_key);
1916 umtxq_busy(&uq->uq_key);
1917 umtxq_unlock(&uq->uq_key);
1919 ceiling = RTP_PRIO_MAX - fuword32(&m->m_ceilings[0]);
1920 if (ceiling > RTP_PRIO_MAX) {
1925 mtx_lock_spin(&umtx_lock);
1926 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
1927 mtx_unlock_spin(&umtx_lock);
1931 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
1932 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
1934 if (uq->uq_inherited_pri < UPRI(td))
1935 sched_lend_user_prio(td, uq->uq_inherited_pri);
1938 mtx_unlock_spin(&umtx_lock);
1940 owner = casuword32(&m->m_owner,
1941 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1943 if (owner == UMUTEX_CONTESTED) {
1948 /* The address was invalid. */
1954 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1955 (owner & ~UMUTEX_CONTESTED) == id) {
1966 * If we caught a signal, we have retried and now
1972 umtxq_lock(&uq->uq_key);
1974 umtxq_unbusy(&uq->uq_key);
1975 error = umtxq_sleep(uq, "umtxpp", timo);
1977 umtxq_unlock(&uq->uq_key);
1979 mtx_lock_spin(&umtx_lock);
1980 uq->uq_inherited_pri = old_inherited_pri;
1982 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
1983 uq2 = TAILQ_FIRST(&pi->pi_blocked);
1985 if (pri > UPRI(uq2->uq_thread))
1986 pri = UPRI(uq2->uq_thread);
1989 if (pri > uq->uq_inherited_pri)
1990 pri = uq->uq_inherited_pri;
1992 sched_lend_user_prio(td, pri);
1994 mtx_unlock_spin(&umtx_lock);
1998 mtx_lock_spin(&umtx_lock);
1999 uq->uq_inherited_pri = old_inherited_pri;
2001 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2002 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2004 if (pri > UPRI(uq2->uq_thread))
2005 pri = UPRI(uq2->uq_thread);
2008 if (pri > uq->uq_inherited_pri)
2009 pri = uq->uq_inherited_pri;
2011 sched_lend_user_prio(td, pri);
2013 mtx_unlock_spin(&umtx_lock);
2017 umtxq_lock(&uq->uq_key);
2018 umtxq_unbusy(&uq->uq_key);
2019 umtxq_unlock(&uq->uq_key);
2020 umtx_key_release(&uq->uq_key);
2025 * Unlock a PP mutex.
2028 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags)
2030 struct umtx_key key;
2031 struct umtx_q *uq, *uq2;
2035 int error, pri, new_inherited_pri, su;
2039 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2042 * Make sure we own this mtx.
2044 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
2048 if ((owner & ~UMUTEX_CONTESTED) != id)
2051 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2056 new_inherited_pri = PRI_MAX;
2058 rceiling = RTP_PRIO_MAX - rceiling;
2059 if (rceiling > RTP_PRIO_MAX)
2061 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2064 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2071 * For priority protected mutex, always set unlocked state
2072 * to UMUTEX_CONTESTED, so that userland always enters kernel
2073 * to lock the mutex, it is necessary because thread priority
2074 * has to be adjusted for such mutex.
2076 error = suword32(__DEVOLATILE(uint32_t *, &m->m_owner),
2081 umtxq_signal(&key, 1);
2088 mtx_lock_spin(&umtx_lock);
2090 uq->uq_inherited_pri = new_inherited_pri;
2092 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2093 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2095 if (pri > UPRI(uq2->uq_thread))
2096 pri = UPRI(uq2->uq_thread);
2099 if (pri > uq->uq_inherited_pri)
2100 pri = uq->uq_inherited_pri;
2102 sched_lend_user_prio(td, pri);
2104 mtx_unlock_spin(&umtx_lock);
2106 umtx_key_release(&key);
2111 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2112 uint32_t *old_ceiling)
2115 uint32_t save_ceiling;
2120 flags = fuword32(&m->m_flags);
2121 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2123 if (ceiling > RTP_PRIO_MAX)
2127 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2131 umtxq_lock(&uq->uq_key);
2132 umtxq_busy(&uq->uq_key);
2133 umtxq_unlock(&uq->uq_key);
2135 save_ceiling = fuword32(&m->m_ceilings[0]);
2137 owner = casuword32(&m->m_owner,
2138 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
2140 if (owner == UMUTEX_CONTESTED) {
2141 suword32(&m->m_ceilings[0], ceiling);
2142 suword32(__DEVOLATILE(uint32_t *, &m->m_owner),
2148 /* The address was invalid. */
2154 if ((owner & ~UMUTEX_CONTESTED) == id) {
2155 suword32(&m->m_ceilings[0], ceiling);
2161 * If we caught a signal, we have retried and now
2168 * We set the contested bit, sleep. Otherwise the lock changed
2169 * and we need to retry or we lost a race to the thread
2170 * unlocking the umtx.
2172 umtxq_lock(&uq->uq_key);
2174 umtxq_unbusy(&uq->uq_key);
2175 error = umtxq_sleep(uq, "umtxpp", 0);
2177 umtxq_unlock(&uq->uq_key);
2179 umtxq_lock(&uq->uq_key);
2181 umtxq_signal(&uq->uq_key, INT_MAX);
2182 umtxq_unbusy(&uq->uq_key);
2183 umtxq_unlock(&uq->uq_key);
2184 umtx_key_release(&uq->uq_key);
2185 if (error == 0 && old_ceiling != NULL)
2186 suword32(old_ceiling, save_ceiling);
2191 _do_lock_umutex(struct thread *td, struct umutex *m, int flags, int timo,
2194 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2196 return (_do_lock_normal(td, m, flags, timo, mode));
2197 case UMUTEX_PRIO_INHERIT:
2198 return (_do_lock_pi(td, m, flags, timo, mode));
2199 case UMUTEX_PRIO_PROTECT:
2200 return (_do_lock_pp(td, m, flags, timo, mode));
2206 * Lock a userland POSIX mutex.
2209 do_lock_umutex(struct thread *td, struct umutex *m,
2210 struct timespec *timeout, int mode)
2212 struct timespec ts, ts2, ts3;
2217 flags = fuword32(&m->m_flags);
2221 if (timeout == NULL) {
2222 error = _do_lock_umutex(td, m, flags, 0, mode);
2223 /* Mutex locking is restarted if it is interrupted. */
2224 if (error == EINTR && mode != _UMUTEX_WAIT)
2228 timespecadd(&ts, timeout);
2229 TIMESPEC_TO_TIMEVAL(&tv, timeout);
2231 error = _do_lock_umutex(td, m, flags, tvtohz(&tv), mode);
2232 if (error != ETIMEDOUT)
2234 getnanouptime(&ts2);
2235 if (timespeccmp(&ts2, &ts, >=)) {
2240 timespecsub(&ts3, &ts2);
2241 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
2243 /* Timed-locking is not restarted. */
2244 if (error == ERESTART)
2251 * Unlock a userland POSIX mutex.
2254 do_unlock_umutex(struct thread *td, struct umutex *m)
2258 flags = fuword32(&m->m_flags);
2262 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2264 return (do_unlock_normal(td, m, flags));
2265 case UMUTEX_PRIO_INHERIT:
2266 return (do_unlock_pi(td, m, flags));
2267 case UMUTEX_PRIO_PROTECT:
2268 return (do_unlock_pp(td, m, flags));
2275 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2276 struct timespec *timeout, u_long wflags)
2280 struct timespec cts, ets, tts;
2286 flags = fuword32(&cv->c_flags);
2287 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2291 if ((wflags & CVWAIT_CLOCKID) != 0) {
2292 clockid = fuword32(&cv->c_clockid);
2293 if (clockid < CLOCK_REALTIME ||
2294 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2295 /* hmm, only HW clock id will work. */
2299 clockid = CLOCK_REALTIME;
2302 umtxq_lock(&uq->uq_key);
2303 umtxq_busy(&uq->uq_key);
2305 umtxq_unlock(&uq->uq_key);
2308 * Set c_has_waiters to 1 before releasing user mutex, also
2309 * don't modify cache line when unnecessary.
2311 if (fuword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters)) == 0)
2312 suword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters), 1);
2314 umtxq_lock(&uq->uq_key);
2315 umtxq_unbusy(&uq->uq_key);
2316 umtxq_unlock(&uq->uq_key);
2318 error = do_unlock_umutex(td, m);
2320 umtxq_lock(&uq->uq_key);
2322 if (timeout == NULL) {
2323 error = umtxq_sleep(uq, "ucond", 0);
2325 if ((wflags & CVWAIT_ABSTIME) == 0) {
2326 kern_clock_gettime(td, clockid, &ets);
2327 timespecadd(&ets, timeout);
2329 } else { /* absolute time */
2332 kern_clock_gettime(td, clockid, &cts);
2333 timespecsub(&tts, &cts);
2335 TIMESPEC_TO_TIMEVAL(&tv, &tts);
2337 error = umtxq_sleep(uq, "ucond", tvtohz(&tv));
2338 if (error != ETIMEDOUT)
2340 kern_clock_gettime(td, clockid, &cts);
2341 if (timespeccmp(&cts, &ets, >=)) {
2346 timespecsub(&tts, &cts);
2347 TIMESPEC_TO_TIMEVAL(&tv, &tts);
2352 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2356 * This must be timeout,interrupted by signal or
2357 * surprious wakeup, clear c_has_waiter flag when
2360 umtxq_busy(&uq->uq_key);
2361 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2362 int oldlen = uq->uq_cur_queue->length;
2365 umtxq_unlock(&uq->uq_key);
2367 __DEVOLATILE(uint32_t *,
2368 &cv->c_has_waiters), 0);
2369 umtxq_lock(&uq->uq_key);
2372 umtxq_unbusy(&uq->uq_key);
2373 if (error == ERESTART)
2377 umtxq_unlock(&uq->uq_key);
2378 umtx_key_release(&uq->uq_key);
2383 * Signal a userland condition variable.
2386 do_cv_signal(struct thread *td, struct ucond *cv)
2388 struct umtx_key key;
2389 int error, cnt, nwake;
2392 flags = fuword32(&cv->c_flags);
2393 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2397 cnt = umtxq_count(&key);
2398 nwake = umtxq_signal(&key, 1);
2402 __DEVOLATILE(uint32_t *, &cv->c_has_waiters), 0);
2407 umtx_key_release(&key);
2412 do_cv_broadcast(struct thread *td, struct ucond *cv)
2414 struct umtx_key key;
2418 flags = fuword32(&cv->c_flags);
2419 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2424 umtxq_signal(&key, INT_MAX);
2427 error = suword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters), 0);
2433 umtx_key_release(&key);
2438 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, int timo)
2441 uint32_t flags, wrflags;
2442 int32_t state, oldstate;
2443 int32_t blocked_readers;
2447 flags = fuword32(&rwlock->rw_flags);
2448 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2452 wrflags = URWLOCK_WRITE_OWNER;
2453 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2454 wrflags |= URWLOCK_WRITE_WAITERS;
2457 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2458 /* try to lock it */
2459 while (!(state & wrflags)) {
2460 if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) {
2461 umtx_key_release(&uq->uq_key);
2464 oldstate = casuword32(&rwlock->rw_state, state, state + 1);
2465 if (oldstate == state) {
2466 umtx_key_release(&uq->uq_key);
2475 /* grab monitor lock */
2476 umtxq_lock(&uq->uq_key);
2477 umtxq_busy(&uq->uq_key);
2478 umtxq_unlock(&uq->uq_key);
2481 * re-read the state, in case it changed between the try-lock above
2482 * and the check below
2484 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2486 /* set read contention bit */
2487 while ((state & wrflags) && !(state & URWLOCK_READ_WAITERS)) {
2488 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_READ_WAITERS);
2489 if (oldstate == state)
2494 /* state is changed while setting flags, restart */
2495 if (!(state & wrflags)) {
2496 umtxq_lock(&uq->uq_key);
2497 umtxq_unbusy(&uq->uq_key);
2498 umtxq_unlock(&uq->uq_key);
2503 /* contention bit is set, before sleeping, increase read waiter count */
2504 blocked_readers = fuword32(&rwlock->rw_blocked_readers);
2505 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2507 while (state & wrflags) {
2508 umtxq_lock(&uq->uq_key);
2510 umtxq_unbusy(&uq->uq_key);
2512 error = umtxq_sleep(uq, "urdlck", timo);
2514 umtxq_busy(&uq->uq_key);
2516 umtxq_unlock(&uq->uq_key);
2519 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2522 /* decrease read waiter count, and may clear read contention bit */
2523 blocked_readers = fuword32(&rwlock->rw_blocked_readers);
2524 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2525 if (blocked_readers == 1) {
2526 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2528 oldstate = casuword32(&rwlock->rw_state, state,
2529 state & ~URWLOCK_READ_WAITERS);
2530 if (oldstate == state)
2536 umtxq_lock(&uq->uq_key);
2537 umtxq_unbusy(&uq->uq_key);
2538 umtxq_unlock(&uq->uq_key);
2540 umtx_key_release(&uq->uq_key);
2545 do_rw_rdlock2(struct thread *td, void *obj, long val, struct timespec *timeout)
2547 struct timespec ts, ts2, ts3;
2552 timespecadd(&ts, timeout);
2553 TIMESPEC_TO_TIMEVAL(&tv, timeout);
2555 error = do_rw_rdlock(td, obj, val, tvtohz(&tv));
2556 if (error != ETIMEDOUT)
2558 getnanouptime(&ts2);
2559 if (timespeccmp(&ts2, &ts, >=)) {
2564 timespecsub(&ts3, &ts2);
2565 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
2567 if (error == ERESTART)
2573 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, int timo)
2577 int32_t state, oldstate;
2578 int32_t blocked_writers;
2579 int32_t blocked_readers;
2583 flags = fuword32(&rwlock->rw_flags);
2584 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2588 blocked_readers = 0;
2590 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2591 while (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2592 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_WRITE_OWNER);
2593 if (oldstate == state) {
2594 umtx_key_release(&uq->uq_key);
2601 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) &&
2602 blocked_readers != 0) {
2603 umtxq_lock(&uq->uq_key);
2604 umtxq_busy(&uq->uq_key);
2605 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE);
2606 umtxq_unbusy(&uq->uq_key);
2607 umtxq_unlock(&uq->uq_key);
2613 /* grab monitor lock */
2614 umtxq_lock(&uq->uq_key);
2615 umtxq_busy(&uq->uq_key);
2616 umtxq_unlock(&uq->uq_key);
2619 * re-read the state, in case it changed between the try-lock above
2620 * and the check below
2622 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2624 while (((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) &&
2625 (state & URWLOCK_WRITE_WAITERS) == 0) {
2626 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_WRITE_WAITERS);
2627 if (oldstate == state)
2632 if (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2633 umtxq_lock(&uq->uq_key);
2634 umtxq_unbusy(&uq->uq_key);
2635 umtxq_unlock(&uq->uq_key);
2639 blocked_writers = fuword32(&rwlock->rw_blocked_writers);
2640 suword32(&rwlock->rw_blocked_writers, blocked_writers+1);
2642 while ((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) {
2643 umtxq_lock(&uq->uq_key);
2644 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2645 umtxq_unbusy(&uq->uq_key);
2647 error = umtxq_sleep(uq, "uwrlck", timo);
2649 umtxq_busy(&uq->uq_key);
2650 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2651 umtxq_unlock(&uq->uq_key);
2654 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2657 blocked_writers = fuword32(&rwlock->rw_blocked_writers);
2658 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
2659 if (blocked_writers == 1) {
2660 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2662 oldstate = casuword32(&rwlock->rw_state, state,
2663 state & ~URWLOCK_WRITE_WAITERS);
2664 if (oldstate == state)
2668 blocked_readers = fuword32(&rwlock->rw_blocked_readers);
2670 blocked_readers = 0;
2672 umtxq_lock(&uq->uq_key);
2673 umtxq_unbusy(&uq->uq_key);
2674 umtxq_unlock(&uq->uq_key);
2677 umtx_key_release(&uq->uq_key);
2682 do_rw_wrlock2(struct thread *td, void *obj, struct timespec *timeout)
2684 struct timespec ts, ts2, ts3;
2689 timespecadd(&ts, timeout);
2690 TIMESPEC_TO_TIMEVAL(&tv, timeout);
2692 error = do_rw_wrlock(td, obj, tvtohz(&tv));
2693 if (error != ETIMEDOUT)
2695 getnanouptime(&ts2);
2696 if (timespeccmp(&ts2, &ts, >=)) {
2701 timespecsub(&ts3, &ts2);
2702 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
2704 if (error == ERESTART)
2710 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
2714 int32_t state, oldstate;
2715 int error, q, count;
2718 flags = fuword32(&rwlock->rw_flags);
2719 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2723 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2724 if (state & URWLOCK_WRITE_OWNER) {
2726 oldstate = casuword32(&rwlock->rw_state, state,
2727 state & ~URWLOCK_WRITE_OWNER);
2728 if (oldstate != state) {
2730 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
2737 } else if (URWLOCK_READER_COUNT(state) != 0) {
2739 oldstate = casuword32(&rwlock->rw_state, state,
2741 if (oldstate != state) {
2743 if (URWLOCK_READER_COUNT(oldstate) == 0) {
2758 if (!(flags & URWLOCK_PREFER_READER)) {
2759 if (state & URWLOCK_WRITE_WAITERS) {
2761 q = UMTX_EXCLUSIVE_QUEUE;
2762 } else if (state & URWLOCK_READ_WAITERS) {
2764 q = UMTX_SHARED_QUEUE;
2767 if (state & URWLOCK_READ_WAITERS) {
2769 q = UMTX_SHARED_QUEUE;
2770 } else if (state & URWLOCK_WRITE_WAITERS) {
2772 q = UMTX_EXCLUSIVE_QUEUE;
2777 umtxq_lock(&uq->uq_key);
2778 umtxq_busy(&uq->uq_key);
2779 umtxq_signal_queue(&uq->uq_key, count, q);
2780 umtxq_unbusy(&uq->uq_key);
2781 umtxq_unlock(&uq->uq_key);
2784 umtx_key_release(&uq->uq_key);
2789 do_sem_wait(struct thread *td, struct _usem *sem, struct timespec *timeout)
2793 struct timespec cts, ets, tts;
2794 uint32_t flags, count;
2798 flags = fuword32(&sem->_flags);
2799 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
2802 umtxq_lock(&uq->uq_key);
2803 umtxq_busy(&uq->uq_key);
2805 umtxq_unlock(&uq->uq_key);
2807 if (fuword32(__DEVOLATILE(uint32_t *, &sem->_has_waiters)) == 0)
2808 casuword32(__DEVOLATILE(uint32_t *, &sem->_has_waiters), 0, 1);
2810 count = fuword32(__DEVOLATILE(uint32_t *, &sem->_count));
2812 umtxq_lock(&uq->uq_key);
2813 umtxq_unbusy(&uq->uq_key);
2815 umtxq_unlock(&uq->uq_key);
2816 umtx_key_release(&uq->uq_key);
2820 umtxq_lock(&uq->uq_key);
2821 umtxq_unbusy(&uq->uq_key);
2822 umtxq_unlock(&uq->uq_key);
2824 umtxq_lock(&uq->uq_key);
2825 if (timeout == NULL) {
2826 error = umtxq_sleep(uq, "usem", 0);
2828 getnanouptime(&ets);
2829 timespecadd(&ets, timeout);
2830 TIMESPEC_TO_TIMEVAL(&tv, timeout);
2832 error = umtxq_sleep(uq, "usem", tvtohz(&tv));
2833 if (error != ETIMEDOUT)
2835 getnanouptime(&cts);
2836 if (timespeccmp(&cts, &ets, >=)) {
2841 timespecsub(&tts, &cts);
2842 TIMESPEC_TO_TIMEVAL(&tv, &tts);
2846 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2850 if (error == ERESTART)
2853 umtxq_unlock(&uq->uq_key);
2854 umtx_key_release(&uq->uq_key);
2859 * Signal a userland condition variable.
2862 do_sem_wake(struct thread *td, struct _usem *sem)
2864 struct umtx_key key;
2865 int error, cnt, nwake;
2868 flags = fuword32(&sem->_flags);
2869 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
2873 cnt = umtxq_count(&key);
2874 nwake = umtxq_signal(&key, 1);
2878 __DEVOLATILE(uint32_t *, &sem->_has_waiters), 0);
2883 umtx_key_release(&key);
2888 sys__umtx_lock(struct thread *td, struct _umtx_lock_args *uap)
2889 /* struct umtx *umtx */
2891 return _do_lock_umtx(td, uap->umtx, td->td_tid, 0);
2895 sys__umtx_unlock(struct thread *td, struct _umtx_unlock_args *uap)
2896 /* struct umtx *umtx */
2898 return do_unlock_umtx(td, uap->umtx, td->td_tid);
2902 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap)
2904 struct timespec *ts, timeout;
2907 /* Allow a null timespec (wait forever). */
2908 if (uap->uaddr2 == NULL)
2911 error = copyin(uap->uaddr2, &timeout, sizeof(timeout));
2914 if (timeout.tv_nsec >= 1000000000 ||
2915 timeout.tv_nsec < 0) {
2920 return (do_lock_umtx(td, uap->obj, uap->val, ts));
2924 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap)
2926 return (do_unlock_umtx(td, uap->obj, uap->val));
2930 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
2932 struct timespec *ts, timeout;
2935 if (uap->uaddr2 == NULL)
2938 error = copyin(uap->uaddr2, &timeout, sizeof(timeout));
2941 if (timeout.tv_nsec >= 1000000000 ||
2942 timeout.tv_nsec < 0)
2946 return do_wait(td, uap->obj, uap->val, ts, 0, 0);
2950 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
2952 struct timespec *ts, timeout;
2955 if (uap->uaddr2 == NULL)
2958 error = copyin(uap->uaddr2, &timeout, sizeof(timeout));
2961 if (timeout.tv_nsec >= 1000000000 ||
2962 timeout.tv_nsec < 0)
2966 return do_wait(td, uap->obj, uap->val, ts, 1, 0);
2970 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
2972 struct timespec *ts, timeout;
2975 if (uap->uaddr2 == NULL)
2978 error = copyin(uap->uaddr2, &timeout, sizeof(timeout));
2981 if (timeout.tv_nsec >= 1000000000 ||
2982 timeout.tv_nsec < 0)
2986 return do_wait(td, uap->obj, uap->val, ts, 1, 1);
2990 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
2992 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
2995 #define BATCH_SIZE 128
2997 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
2999 int count = uap->val;
3000 void *uaddrs[BATCH_SIZE];
3001 char **upp = (char **)uap->obj;
3008 if (tocopy > BATCH_SIZE)
3009 tocopy = BATCH_SIZE;
3010 error = copyin(upp+pos, uaddrs, tocopy * sizeof(char *));
3013 for (i = 0; i < tocopy; ++i)
3014 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3022 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3024 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3028 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3030 struct timespec *ts, timeout;
3033 /* Allow a null timespec (wait forever). */
3034 if (uap->uaddr2 == NULL)
3037 error = copyin(uap->uaddr2, &timeout,
3041 if (timeout.tv_nsec >= 1000000000 ||
3042 timeout.tv_nsec < 0) {
3047 return do_lock_umutex(td, uap->obj, ts, 0);
3051 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3053 return do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY);
3057 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3059 struct timespec *ts, timeout;
3062 /* Allow a null timespec (wait forever). */
3063 if (uap->uaddr2 == NULL)
3066 error = copyin(uap->uaddr2, &timeout,
3070 if (timeout.tv_nsec >= 1000000000 ||
3071 timeout.tv_nsec < 0) {
3076 return do_lock_umutex(td, uap->obj, ts, _UMUTEX_WAIT);
3080 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3082 return do_wake_umutex(td, uap->obj);
3086 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3088 return do_unlock_umutex(td, uap->obj);
3092 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3094 return do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1);
3098 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3100 struct timespec *ts, timeout;
3103 /* Allow a null timespec (wait forever). */
3104 if (uap->uaddr2 == NULL)
3107 error = copyin(uap->uaddr2, &timeout,
3111 if (timeout.tv_nsec >= 1000000000 ||
3112 timeout.tv_nsec < 0) {
3117 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3121 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3123 return do_cv_signal(td, uap->obj);
3127 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3129 return do_cv_broadcast(td, uap->obj);
3133 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3135 struct timespec timeout;
3138 /* Allow a null timespec (wait forever). */
3139 if (uap->uaddr2 == NULL) {
3140 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3142 error = copyin(uap->uaddr2, &timeout,
3146 if (timeout.tv_nsec >= 1000000000 ||
3147 timeout.tv_nsec < 0) {
3150 error = do_rw_rdlock2(td, uap->obj, uap->val, &timeout);
3156 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3158 struct timespec timeout;
3161 /* Allow a null timespec (wait forever). */
3162 if (uap->uaddr2 == NULL) {
3163 error = do_rw_wrlock(td, uap->obj, 0);
3165 error = copyin(uap->uaddr2, &timeout,
3169 if (timeout.tv_nsec >= 1000000000 ||
3170 timeout.tv_nsec < 0) {
3174 error = do_rw_wrlock2(td, uap->obj, &timeout);
3180 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3182 return do_rw_unlock(td, uap->obj);
3186 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3188 struct timespec *ts, timeout;
3191 /* Allow a null timespec (wait forever). */
3192 if (uap->uaddr2 == NULL)
3195 error = copyin(uap->uaddr2, &timeout,
3199 if (timeout.tv_nsec >= 1000000000 ||
3200 timeout.tv_nsec < 0) {
3205 return (do_sem_wait(td, uap->obj, ts));
3209 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3211 return do_sem_wake(td, uap->obj);
3214 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
3216 static _umtx_op_func op_table[] = {
3217 __umtx_op_lock_umtx, /* UMTX_OP_LOCK */
3218 __umtx_op_unlock_umtx, /* UMTX_OP_UNLOCK */
3219 __umtx_op_wait, /* UMTX_OP_WAIT */
3220 __umtx_op_wake, /* UMTX_OP_WAKE */
3221 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_TRYLOCK */
3222 __umtx_op_lock_umutex, /* UMTX_OP_MUTEX_LOCK */
3223 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3224 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3225 __umtx_op_cv_wait, /* UMTX_OP_CV_WAIT*/
3226 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3227 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3228 __umtx_op_wait_uint, /* UMTX_OP_WAIT_UINT */
3229 __umtx_op_rw_rdlock, /* UMTX_OP_RW_RDLOCK */
3230 __umtx_op_rw_wrlock, /* UMTX_OP_RW_WRLOCK */
3231 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3232 __umtx_op_wait_uint_private, /* UMTX_OP_WAIT_UINT_PRIVATE */
3233 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3234 __umtx_op_wait_umutex, /* UMTX_OP_UMUTEX_WAIT */
3235 __umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */
3236 __umtx_op_sem_wait, /* UMTX_OP_SEM_WAIT */
3237 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3238 __umtx_op_nwake_private /* UMTX_OP_NWAKE_PRIVATE */
3242 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
3244 if ((unsigned)uap->op < UMTX_OP_MAX)
3245 return (*op_table[uap->op])(td, uap);
3249 #ifdef COMPAT_FREEBSD32
3251 freebsd32_umtx_lock(struct thread *td, struct freebsd32_umtx_lock_args *uap)
3252 /* struct umtx *umtx */
3254 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
3258 freebsd32_umtx_unlock(struct thread *td, struct freebsd32_umtx_unlock_args *uap)
3259 /* struct umtx *umtx */
3261 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
3270 copyin_timeout32(void *addr, struct timespec *tsp)
3272 struct timespec32 ts32;
3275 error = copyin(addr, &ts32, sizeof(struct timespec32));
3277 tsp->tv_sec = ts32.tv_sec;
3278 tsp->tv_nsec = ts32.tv_nsec;
3284 __umtx_op_lock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
3286 struct timespec *ts, timeout;
3289 /* Allow a null timespec (wait forever). */
3290 if (uap->uaddr2 == NULL)
3293 error = copyin_timeout32(uap->uaddr2, &timeout);
3296 if (timeout.tv_nsec >= 1000000000 ||
3297 timeout.tv_nsec < 0) {
3302 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
3306 __umtx_op_unlock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
3308 return (do_unlock_umtx32(td, uap->obj, (uint32_t)uap->val));
3312 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3314 struct timespec *ts, timeout;
3317 if (uap->uaddr2 == NULL)
3320 error = copyin_timeout32(uap->uaddr2, &timeout);
3323 if (timeout.tv_nsec >= 1000000000 ||
3324 timeout.tv_nsec < 0)
3328 return do_wait(td, uap->obj, uap->val, ts, 1, 0);
3332 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3334 struct timespec *ts, timeout;
3337 /* Allow a null timespec (wait forever). */
3338 if (uap->uaddr2 == NULL)
3341 error = copyin_timeout32(uap->uaddr2, &timeout);
3344 if (timeout.tv_nsec >= 1000000000 ||
3345 timeout.tv_nsec < 0)
3349 return do_lock_umutex(td, uap->obj, ts, 0);
3353 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3355 struct timespec *ts, timeout;
3358 /* Allow a null timespec (wait forever). */
3359 if (uap->uaddr2 == NULL)
3362 error = copyin_timeout32(uap->uaddr2, &timeout);
3365 if (timeout.tv_nsec >= 1000000000 ||
3366 timeout.tv_nsec < 0)
3370 return do_lock_umutex(td, uap->obj, ts, _UMUTEX_WAIT);
3374 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3376 struct timespec *ts, timeout;
3379 /* Allow a null timespec (wait forever). */
3380 if (uap->uaddr2 == NULL)
3383 error = copyin_timeout32(uap->uaddr2, &timeout);
3386 if (timeout.tv_nsec >= 1000000000 ||
3387 timeout.tv_nsec < 0)
3391 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3395 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3397 struct timespec timeout;
3400 /* Allow a null timespec (wait forever). */
3401 if (uap->uaddr2 == NULL) {
3402 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3404 error = copyin_timeout32(uap->uaddr2, &timeout);
3407 if (timeout.tv_nsec >= 1000000000 ||
3408 timeout.tv_nsec < 0) {
3411 error = do_rw_rdlock2(td, uap->obj, uap->val, &timeout);
3417 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3419 struct timespec timeout;
3422 /* Allow a null timespec (wait forever). */
3423 if (uap->uaddr2 == NULL) {
3424 error = do_rw_wrlock(td, uap->obj, 0);
3426 error = copyin_timeout32(uap->uaddr2, &timeout);
3429 if (timeout.tv_nsec >= 1000000000 ||
3430 timeout.tv_nsec < 0) {
3434 error = do_rw_wrlock2(td, uap->obj, &timeout);
3440 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3442 struct timespec *ts, timeout;
3445 if (uap->uaddr2 == NULL)
3448 error = copyin_timeout32(uap->uaddr2, &timeout);
3451 if (timeout.tv_nsec >= 1000000000 ||
3452 timeout.tv_nsec < 0)
3456 return do_wait(td, uap->obj, uap->val, ts, 1, 1);
3460 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3462 struct timespec *ts, timeout;
3465 /* Allow a null timespec (wait forever). */
3466 if (uap->uaddr2 == NULL)
3469 error = copyin_timeout32(uap->uaddr2, &timeout);
3472 if (timeout.tv_nsec >= 1000000000 ||
3473 timeout.tv_nsec < 0)
3477 return (do_sem_wait(td, uap->obj, ts));
3481 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
3483 int count = uap->val;
3484 uint32_t uaddrs[BATCH_SIZE];
3485 uint32_t **upp = (uint32_t **)uap->obj;
3492 if (tocopy > BATCH_SIZE)
3493 tocopy = BATCH_SIZE;
3494 error = copyin(upp+pos, uaddrs, tocopy * sizeof(uint32_t));
3497 for (i = 0; i < tocopy; ++i)
3498 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
3506 static _umtx_op_func op_table_compat32[] = {
3507 __umtx_op_lock_umtx_compat32, /* UMTX_OP_LOCK */
3508 __umtx_op_unlock_umtx_compat32, /* UMTX_OP_UNLOCK */
3509 __umtx_op_wait_compat32, /* UMTX_OP_WAIT */
3510 __umtx_op_wake, /* UMTX_OP_WAKE */
3511 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_LOCK */
3512 __umtx_op_lock_umutex_compat32, /* UMTX_OP_MUTEX_TRYLOCK */
3513 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3514 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3515 __umtx_op_cv_wait_compat32, /* UMTX_OP_CV_WAIT*/
3516 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3517 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3518 __umtx_op_wait_compat32, /* UMTX_OP_WAIT_UINT */
3519 __umtx_op_rw_rdlock_compat32, /* UMTX_OP_RW_RDLOCK */
3520 __umtx_op_rw_wrlock_compat32, /* UMTX_OP_RW_WRLOCK */
3521 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3522 __umtx_op_wait_uint_private_compat32, /* UMTX_OP_WAIT_UINT_PRIVATE */
3523 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3524 __umtx_op_wait_umutex_compat32, /* UMTX_OP_UMUTEX_WAIT */
3525 __umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */
3526 __umtx_op_sem_wait_compat32, /* UMTX_OP_SEM_WAIT */
3527 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3528 __umtx_op_nwake_private32 /* UMTX_OP_NWAKE_PRIVATE */
3532 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
3534 if ((unsigned)uap->op < UMTX_OP_MAX)
3535 return (*op_table_compat32[uap->op])(td,
3536 (struct _umtx_op_args *)uap);
3542 umtx_thread_init(struct thread *td)
3544 td->td_umtxq = umtxq_alloc();
3545 td->td_umtxq->uq_thread = td;
3549 umtx_thread_fini(struct thread *td)
3551 umtxq_free(td->td_umtxq);
3555 * It will be called when new thread is created, e.g fork().
3558 umtx_thread_alloc(struct thread *td)
3563 uq->uq_inherited_pri = PRI_MAX;
3565 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
3566 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
3567 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
3568 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
3575 umtx_exec_hook(void *arg __unused, struct proc *p __unused,
3576 struct image_params *imgp __unused)
3578 umtx_thread_cleanup(curthread);
3582 * thread_exit() hook.
3585 umtx_thread_exit(struct thread *td)
3587 umtx_thread_cleanup(td);
3591 * clean up umtx data.
3594 umtx_thread_cleanup(struct thread *td)
3599 if ((uq = td->td_umtxq) == NULL)
3602 mtx_lock_spin(&umtx_lock);
3603 uq->uq_inherited_pri = PRI_MAX;
3604 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
3605 pi->pi_owner = NULL;
3606 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
3608 mtx_unlock_spin(&umtx_lock);
3610 sched_lend_user_prio(td, PRI_MAX);