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/eventhandler.h>
50 #include <vm/vm_param.h>
52 #include <vm/vm_map.h>
53 #include <vm/vm_object.h>
55 #include <machine/cpu.h>
58 #include <compat/freebsd32/freebsd32_proto.h>
61 #define TYPE_SIMPLE_LOCK 0
62 #define TYPE_SIMPLE_WAIT 1
63 #define TYPE_NORMAL_UMUTEX 2
64 #define TYPE_PI_UMUTEX 3
65 #define TYPE_PP_UMUTEX 4
68 /* Key to represent a unique userland synchronous object */
89 /* Priority inheritance mutex info. */
92 struct thread *pi_owner;
97 /* List entry to link umtx holding by thread */
98 TAILQ_ENTRY(umtx_pi) pi_link;
100 /* List entry in hash */
101 TAILQ_ENTRY(umtx_pi) pi_hashlink;
103 /* List for waiters */
104 TAILQ_HEAD(,umtx_q) pi_blocked;
106 /* Identify a userland lock object */
107 struct umtx_key pi_key;
110 /* A userland synchronous object user. */
112 /* Linked list for the hash. */
113 TAILQ_ENTRY(umtx_q) uq_link;
116 struct umtx_key uq_key;
120 #define UQF_UMTXQ 0x0001
122 /* The thread waits on. */
123 struct thread *uq_thread;
126 * Blocked on PI mutex. read can use chain lock
127 * or umtx_lock, write must have both chain lock and
128 * umtx_lock being hold.
130 struct umtx_pi *uq_pi_blocked;
132 /* On blocked list */
133 TAILQ_ENTRY(umtx_q) uq_lockq;
135 /* Thread contending with us */
136 TAILQ_HEAD(,umtx_pi) uq_pi_contested;
138 /* Inherited priority from PP mutex */
139 u_char uq_inherited_pri;
142 TAILQ_HEAD(umtxq_head, umtx_q);
144 /* Userland lock object's wait-queue chain */
146 /* Lock for this chain. */
149 /* List of sleep queues. */
150 struct umtxq_head uc_queue;
155 /* Chain lock waiters */
158 /* All PI in the list */
159 TAILQ_HEAD(,umtx_pi) uc_pi_list;
162 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
165 * Don't propagate time-sharing priority, there is a security reason,
166 * a user can simply introduce PI-mutex, let thread A lock the mutex,
167 * and let another thread B block on the mutex, because B is
168 * sleeping, its priority will be boosted, this causes A's priority to
169 * be boosted via priority propagating too and will never be lowered even
170 * if it is using 100%CPU, this is unfair to other processes.
173 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
174 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
175 PRI_MAX_TIMESHARE : (td)->td_user_pri)
177 #define GOLDEN_RATIO_PRIME 2654404609U
178 #define UMTX_CHAINS 128
179 #define UMTX_SHIFTS (__WORD_BIT - 7)
181 #define THREAD_SHARE 0
182 #define PROCESS_SHARE 1
185 #define GET_SHARE(flags) \
186 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
188 static uma_zone_t umtx_pi_zone;
189 static struct umtxq_chain umtxq_chains[UMTX_CHAINS];
190 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
191 static int umtx_pi_allocated;
193 SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW, 0, "umtx debug");
194 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
195 &umtx_pi_allocated, 0, "Allocated umtx_pi");
197 static void umtxq_sysinit(void *);
198 static void umtxq_hash(struct umtx_key *key);
199 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
200 static void umtxq_lock(struct umtx_key *key);
201 static void umtxq_unlock(struct umtx_key *key);
202 static void umtxq_busy(struct umtx_key *key);
203 static void umtxq_unbusy(struct umtx_key *key);
204 static void umtxq_insert(struct umtx_q *uq);
205 static void umtxq_remove(struct umtx_q *uq);
206 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, int timo);
207 static int umtxq_count(struct umtx_key *key);
208 static int umtxq_signal(struct umtx_key *key, int nr_wakeup);
209 static int umtx_key_match(const struct umtx_key *k1, const struct umtx_key *k2);
210 static int umtx_key_get(void *addr, int type, int share,
211 struct umtx_key *key);
212 static void umtx_key_release(struct umtx_key *key);
213 static struct umtx_pi *umtx_pi_alloc(int);
214 static void umtx_pi_free(struct umtx_pi *pi);
215 static void umtx_pi_adjust_locked(struct thread *td, u_char oldpri);
216 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags);
217 static void umtx_thread_cleanup(struct thread *td);
218 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
219 struct image_params *imgp __unused);
220 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
222 static struct mtx umtx_lock;
225 umtxq_sysinit(void *arg __unused)
229 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
230 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
231 for (i = 0; i < UMTX_CHAINS; ++i) {
232 mtx_init(&umtxq_chains[i].uc_lock, "umtxql", NULL,
233 MTX_DEF | MTX_DUPOK);
234 TAILQ_INIT(&umtxq_chains[i].uc_queue);
235 TAILQ_INIT(&umtxq_chains[i].uc_pi_list);
236 umtxq_chains[i].uc_busy = 0;
237 umtxq_chains[i].uc_waiters = 0;
239 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_SPIN);
240 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
241 EVENTHANDLER_PRI_ANY);
249 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
250 TAILQ_INIT(&uq->uq_pi_contested);
251 uq->uq_inherited_pri = PRI_MAX;
256 umtxq_free(struct umtx_q *uq)
262 umtxq_hash(struct umtx_key *key)
264 unsigned n = (uintptr_t)key->info.both.a + key->info.both.b;
265 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
269 umtx_key_match(const struct umtx_key *k1, const struct umtx_key *k2)
271 return (k1->type == k2->type &&
272 k1->info.both.a == k2->info.both.a &&
273 k1->info.both.b == k2->info.both.b);
276 static inline struct umtxq_chain *
277 umtxq_getchain(struct umtx_key *key)
279 return (&umtxq_chains[key->hash]);
283 * Set chain to busy state when following operation
284 * may be blocked (kernel mutex can not be used).
287 umtxq_busy(struct umtx_key *key)
289 struct umtxq_chain *uc;
291 uc = umtxq_getchain(key);
292 mtx_assert(&uc->uc_lock, MA_OWNED);
293 while (uc->uc_busy != 0) {
295 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
305 umtxq_unbusy(struct umtx_key *key)
307 struct umtxq_chain *uc;
309 uc = umtxq_getchain(key);
310 mtx_assert(&uc->uc_lock, MA_OWNED);
311 KASSERT(uc->uc_busy != 0, ("not busy"));
321 umtxq_lock(struct umtx_key *key)
323 struct umtxq_chain *uc;
325 uc = umtxq_getchain(key);
326 mtx_lock(&uc->uc_lock);
333 umtxq_unlock(struct umtx_key *key)
335 struct umtxq_chain *uc;
337 uc = umtxq_getchain(key);
338 mtx_unlock(&uc->uc_lock);
342 * Insert a thread onto the umtx queue.
345 umtxq_insert(struct umtx_q *uq)
347 struct umtxq_chain *uc;
349 uc = umtxq_getchain(&uq->uq_key);
350 UMTXQ_LOCKED_ASSERT(uc);
351 TAILQ_INSERT_TAIL(&uc->uc_queue, uq, uq_link);
352 uq->uq_flags |= UQF_UMTXQ;
356 * Remove thread from the umtx queue.
359 umtxq_remove(struct umtx_q *uq)
361 struct umtxq_chain *uc;
363 uc = umtxq_getchain(&uq->uq_key);
364 UMTXQ_LOCKED_ASSERT(uc);
365 if (uq->uq_flags & UQF_UMTXQ) {
366 TAILQ_REMOVE(&uc->uc_queue, uq, uq_link);
367 uq->uq_flags &= ~UQF_UMTXQ;
372 * Check if there are multiple waiters
375 umtxq_count(struct umtx_key *key)
377 struct umtxq_chain *uc;
381 uc = umtxq_getchain(key);
382 UMTXQ_LOCKED_ASSERT(uc);
383 TAILQ_FOREACH(uq, &uc->uc_queue, uq_link) {
384 if (umtx_key_match(&uq->uq_key, key)) {
393 * Check if there are multiple PI waiters and returns first
397 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
399 struct umtxq_chain *uc;
404 uc = umtxq_getchain(key);
405 UMTXQ_LOCKED_ASSERT(uc);
406 TAILQ_FOREACH(uq, &uc->uc_queue, uq_link) {
407 if (umtx_key_match(&uq->uq_key, key)) {
417 * Wake up threads waiting on an userland object.
420 umtxq_signal(struct umtx_key *key, int n_wake)
422 struct umtxq_chain *uc;
423 struct umtx_q *uq, *next;
427 uc = umtxq_getchain(key);
428 UMTXQ_LOCKED_ASSERT(uc);
429 TAILQ_FOREACH_SAFE(uq, &uc->uc_queue, uq_link, next) {
430 if (umtx_key_match(&uq->uq_key, key)) {
441 * Wake up specified thread.
444 umtxq_signal_thread(struct umtx_q *uq)
446 struct umtxq_chain *uc;
448 uc = umtxq_getchain(&uq->uq_key);
449 UMTXQ_LOCKED_ASSERT(uc);
455 * Put thread into sleep state, before sleeping, check if
456 * thread was removed from umtx queue.
459 umtxq_sleep(struct umtx_q *uq, const char *wmesg, int timo)
461 struct umtxq_chain *uc;
464 uc = umtxq_getchain(&uq->uq_key);
465 UMTXQ_LOCKED_ASSERT(uc);
466 if (!(uq->uq_flags & UQF_UMTXQ))
468 error = msleep(uq, &uc->uc_lock, PCATCH, wmesg, timo);
469 if (error == EWOULDBLOCK)
475 * Convert userspace address into unique logical address.
478 umtx_key_get(void *addr, int type, int share, struct umtx_key *key)
480 struct thread *td = curthread;
482 vm_map_entry_t entry;
488 if (share == THREAD_SHARE) {
490 key->info.private.vs = td->td_proc->p_vmspace;
491 key->info.private.addr = (uintptr_t)addr;
493 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
494 map = &td->td_proc->p_vmspace->vm_map;
495 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
496 &entry, &key->info.shared.object, &pindex, &prot,
497 &wired) != KERN_SUCCESS) {
501 if ((share == PROCESS_SHARE) ||
502 (share == AUTO_SHARE &&
503 VM_INHERIT_SHARE == entry->inheritance)) {
505 key->info.shared.offset = entry->offset + entry->start -
507 vm_object_reference(key->info.shared.object);
510 key->info.private.vs = td->td_proc->p_vmspace;
511 key->info.private.addr = (uintptr_t)addr;
513 vm_map_lookup_done(map, entry);
524 umtx_key_release(struct umtx_key *key)
527 vm_object_deallocate(key->info.shared.object);
531 * Lock a umtx object.
534 _do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id, int timo)
544 * Care must be exercised when dealing with umtx structure. It
545 * can fault on any access.
549 * Try the uncontested case. This should be done in userland.
551 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
553 /* The acquire succeeded. */
554 if (owner == UMTX_UNOWNED)
557 /* The address was invalid. */
561 /* If no one owns it but it is contested try to acquire it. */
562 if (owner == UMTX_CONTESTED) {
563 owner = casuword(&umtx->u_owner,
564 UMTX_CONTESTED, id | UMTX_CONTESTED);
566 if (owner == UMTX_CONTESTED)
569 /* The address was invalid. */
573 /* If this failed the lock has changed, restart. */
578 * If we caught a signal, we have retried and now
584 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
585 AUTO_SHARE, &uq->uq_key)) != 0)
588 umtxq_lock(&uq->uq_key);
589 umtxq_busy(&uq->uq_key);
591 umtxq_unbusy(&uq->uq_key);
592 umtxq_unlock(&uq->uq_key);
595 * Set the contested bit so that a release in user space
596 * knows to use the system call for unlock. If this fails
597 * either some one else has acquired the lock or it has been
600 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
602 /* The address was invalid. */
604 umtxq_lock(&uq->uq_key);
606 umtxq_unlock(&uq->uq_key);
607 umtx_key_release(&uq->uq_key);
612 * We set the contested bit, sleep. Otherwise the lock changed
613 * and we need to retry or we lost a race to the thread
614 * unlocking the umtx.
616 umtxq_lock(&uq->uq_key);
618 error = umtxq_sleep(uq, "umtx", timo);
620 umtxq_unlock(&uq->uq_key);
621 umtx_key_release(&uq->uq_key);
628 * Lock a umtx object.
631 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
632 struct timespec *timeout)
634 struct timespec ts, ts2, ts3;
638 if (timeout == NULL) {
639 error = _do_lock_umtx(td, umtx, id, 0);
640 /* Mutex locking is restarted if it is interrupted. */
645 timespecadd(&ts, timeout);
646 TIMESPEC_TO_TIMEVAL(&tv, timeout);
648 error = _do_lock_umtx(td, umtx, id, tvtohz(&tv));
649 if (error != ETIMEDOUT)
652 if (timespeccmp(&ts2, &ts, >=)) {
657 timespecsub(&ts3, &ts2);
658 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
660 /* Timed-locking is not restarted. */
661 if (error == ERESTART)
668 * Unlock a umtx object.
671 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
680 * Make sure we own this mtx.
682 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
686 if ((owner & ~UMTX_CONTESTED) != id)
689 /* This should be done in userland */
690 if ((owner & UMTX_CONTESTED) == 0) {
691 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
699 /* We should only ever be in here for contested locks */
700 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
706 count = umtxq_count(&key);
710 * When unlocking the umtx, it must be marked as unowned if
711 * there is zero or one thread only waiting for it.
712 * Otherwise, it must be marked as contested.
714 old = casuword(&umtx->u_owner, owner,
715 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
717 umtxq_signal(&key,1);
720 umtx_key_release(&key);
731 * Lock a umtx object.
734 _do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id, int timo)
744 * Care must be exercised when dealing with umtx structure. It
745 * can fault on any access.
749 * Try the uncontested case. This should be done in userland.
751 owner = casuword32(m, UMUTEX_UNOWNED, id);
753 /* The acquire succeeded. */
754 if (owner == UMUTEX_UNOWNED)
757 /* The address was invalid. */
761 /* If no one owns it but it is contested try to acquire it. */
762 if (owner == UMUTEX_CONTESTED) {
763 owner = casuword32(m,
764 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
765 if (owner == UMUTEX_CONTESTED)
768 /* The address was invalid. */
772 /* If this failed the lock has changed, restart. */
777 * If we caught a signal, we have retried and now
783 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
784 AUTO_SHARE, &uq->uq_key)) != 0)
787 umtxq_lock(&uq->uq_key);
788 umtxq_busy(&uq->uq_key);
790 umtxq_unbusy(&uq->uq_key);
791 umtxq_unlock(&uq->uq_key);
794 * Set the contested bit so that a release in user space
795 * knows to use the system call for unlock. If this fails
796 * either some one else has acquired the lock or it has been
799 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
801 /* The address was invalid. */
803 umtxq_lock(&uq->uq_key);
805 umtxq_unlock(&uq->uq_key);
806 umtx_key_release(&uq->uq_key);
811 * We set the contested bit, sleep. Otherwise the lock changed
812 * and we need to retry or we lost a race to the thread
813 * unlocking the umtx.
815 umtxq_lock(&uq->uq_key);
817 error = umtxq_sleep(uq, "umtx", timo);
819 umtxq_unlock(&uq->uq_key);
820 umtx_key_release(&uq->uq_key);
827 * Lock a umtx object.
830 do_lock_umtx32(struct thread *td, void *m, uint32_t id,
831 struct timespec *timeout)
833 struct timespec ts, ts2, ts3;
837 if (timeout == NULL) {
838 error = _do_lock_umtx32(td, m, id, 0);
839 /* Mutex locking is restarted if it is interrupted. */
844 timespecadd(&ts, timeout);
845 TIMESPEC_TO_TIMEVAL(&tv, timeout);
847 error = _do_lock_umtx32(td, m, id, tvtohz(&tv));
848 if (error != ETIMEDOUT)
851 if (timespeccmp(&ts2, &ts, >=)) {
856 timespecsub(&ts3, &ts2);
857 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
859 /* Timed-locking is not restarted. */
860 if (error == ERESTART)
867 * Unlock a umtx object.
870 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
879 * Make sure we own this mtx.
885 if ((owner & ~UMUTEX_CONTESTED) != id)
888 /* This should be done in userland */
889 if ((owner & UMUTEX_CONTESTED) == 0) {
890 old = casuword32(m, owner, UMUTEX_UNOWNED);
898 /* We should only ever be in here for contested locks */
899 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
905 count = umtxq_count(&key);
909 * When unlocking the umtx, it must be marked as unowned if
910 * there is zero or one thread only waiting for it.
911 * Otherwise, it must be marked as contested.
913 old = casuword32(m, owner,
914 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
916 umtxq_signal(&key,1);
919 umtx_key_release(&key);
929 * Fetch and compare value, sleep on the address if value is not changed.
932 do_wait(struct thread *td, void *addr, u_long id,
933 struct timespec *timeout, int compat32)
936 struct timespec ts, ts2, ts3;
942 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT, AUTO_SHARE,
946 umtxq_lock(&uq->uq_key);
948 umtxq_unlock(&uq->uq_key);
952 tmp = fuword32(addr);
954 umtxq_lock(&uq->uq_key);
956 umtxq_unlock(&uq->uq_key);
957 } else if (timeout == NULL) {
958 umtxq_lock(&uq->uq_key);
959 error = umtxq_sleep(uq, "uwait", 0);
961 umtxq_unlock(&uq->uq_key);
964 timespecadd(&ts, timeout);
965 TIMESPEC_TO_TIMEVAL(&tv, timeout);
966 umtxq_lock(&uq->uq_key);
968 error = umtxq_sleep(uq, "uwait", tvtohz(&tv));
969 if (!(uq->uq_flags & UQF_UMTXQ))
971 if (error != ETIMEDOUT)
973 umtxq_unlock(&uq->uq_key);
975 if (timespeccmp(&ts2, &ts, >=)) {
977 umtxq_lock(&uq->uq_key);
981 timespecsub(&ts3, &ts2);
982 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
983 umtxq_lock(&uq->uq_key);
986 umtxq_unlock(&uq->uq_key);
988 umtx_key_release(&uq->uq_key);
989 if (error == ERESTART)
995 * Wake up threads sleeping on the specified address.
998 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake)
1000 struct umtx_key key;
1003 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT, AUTO_SHARE,
1007 ret = umtxq_signal(&key, n_wake);
1009 umtx_key_release(&key);
1014 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1017 _do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags, int timo,
1021 uint32_t owner, old, id;
1028 * Care must be exercised when dealing with umtx structure. It
1029 * can fault on any access.
1033 * Try the uncontested case. This should be done in userland.
1035 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id);
1037 /* The acquire succeeded. */
1038 if (owner == UMUTEX_UNOWNED)
1041 /* The address was invalid. */
1045 /* If no one owns it but it is contested try to acquire it. */
1046 if (owner == UMUTEX_CONTESTED) {
1047 owner = casuword32(&m->m_owner,
1048 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1050 if (owner == UMUTEX_CONTESTED)
1053 /* The address was invalid. */
1057 /* If this failed the lock has changed, restart. */
1061 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1062 (owner & ~UMUTEX_CONTESTED) == id)
1069 * If we caught a signal, we have retried and now
1075 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1076 GET_SHARE(flags), &uq->uq_key)) != 0)
1079 umtxq_lock(&uq->uq_key);
1080 umtxq_busy(&uq->uq_key);
1082 umtxq_unbusy(&uq->uq_key);
1083 umtxq_unlock(&uq->uq_key);
1086 * Set the contested bit so that a release in user space
1087 * knows to use the system call for unlock. If this fails
1088 * either some one else has acquired the lock or it has been
1091 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED);
1093 /* The address was invalid. */
1095 umtxq_lock(&uq->uq_key);
1097 umtxq_unlock(&uq->uq_key);
1098 umtx_key_release(&uq->uq_key);
1103 * We set the contested bit, sleep. Otherwise the lock changed
1104 * and we need to retry or we lost a race to the thread
1105 * unlocking the umtx.
1107 umtxq_lock(&uq->uq_key);
1109 error = umtxq_sleep(uq, "umtxn", timo);
1111 umtxq_unlock(&uq->uq_key);
1112 umtx_key_release(&uq->uq_key);
1119 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1122 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1125 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags)
1127 struct umtx_key key;
1128 uint32_t owner, old, id;
1134 * Make sure we own this mtx.
1136 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1140 if ((owner & ~UMUTEX_CONTESTED) != id)
1143 /* This should be done in userland */
1144 if ((owner & UMUTEX_CONTESTED) == 0) {
1145 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED);
1153 /* We should only ever be in here for contested locks */
1154 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1160 count = umtxq_count(&key);
1164 * When unlocking the umtx, it must be marked as unowned if
1165 * there is zero or one thread only waiting for it.
1166 * Otherwise, it must be marked as contested.
1168 old = casuword32(&m->m_owner, owner,
1169 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1171 umtxq_signal(&key,1);
1174 umtx_key_release(&key);
1182 static inline struct umtx_pi *
1183 umtx_pi_alloc(int flags)
1187 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1188 TAILQ_INIT(&pi->pi_blocked);
1189 atomic_add_int(&umtx_pi_allocated, 1);
1194 umtx_pi_free(struct umtx_pi *pi)
1196 uma_zfree(umtx_pi_zone, pi);
1197 atomic_add_int(&umtx_pi_allocated, -1);
1201 * Adjust the thread's position on a pi_state after its priority has been
1205 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1207 struct umtx_q *uq, *uq1, *uq2;
1210 mtx_assert(&umtx_lock, MA_OWNED);
1217 * Check if the thread needs to be moved on the blocked chain.
1218 * It needs to be moved if either its priority is lower than
1219 * the previous thread or higher than the next thread.
1221 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1222 uq2 = TAILQ_NEXT(uq, uq_lockq);
1223 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1224 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1226 * Remove thread from blocked chain and determine where
1227 * it should be moved to.
1229 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1230 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1231 td1 = uq1->uq_thread;
1232 MPASS(td1->td_proc->p_magic == P_MAGIC);
1233 if (UPRI(td1) > UPRI(td))
1238 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1240 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1246 * Propagate priority when a thread is blocked on POSIX
1250 umtx_propagate_priority(struct thread *td)
1256 mtx_assert(&umtx_lock, MA_OWNED);
1259 pi = uq->uq_pi_blocked;
1268 MPASS(td->td_proc != NULL);
1269 MPASS(td->td_proc->p_magic == P_MAGIC);
1271 if (UPRI(td) <= pri)
1275 sched_lend_user_prio(td, pri);
1279 * Pick up the lock that td is blocked on.
1282 pi = uq->uq_pi_blocked;
1283 /* Resort td on the list if needed. */
1284 if (!umtx_pi_adjust_thread(pi, td))
1290 * Unpropagate priority for a PI mutex when a thread blocked on
1291 * it is interrupted by signal or resumed by others.
1294 umtx_unpropagate_priority(struct umtx_pi *pi)
1296 struct umtx_q *uq, *uq_owner;
1297 struct umtx_pi *pi2;
1300 mtx_assert(&umtx_lock, MA_OWNED);
1302 while (pi != NULL && pi->pi_owner != NULL) {
1304 uq_owner = pi->pi_owner->td_umtxq;
1306 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1307 uq = TAILQ_FIRST(&pi2->pi_blocked);
1309 if (pri > UPRI(uq->uq_thread))
1310 pri = UPRI(uq->uq_thread);
1314 if (pri > uq_owner->uq_inherited_pri)
1315 pri = uq_owner->uq_inherited_pri;
1316 thread_lock(pi->pi_owner);
1317 oldpri = pi->pi_owner->td_user_pri;
1318 sched_unlend_user_prio(pi->pi_owner, pri);
1319 thread_unlock(pi->pi_owner);
1320 umtx_pi_adjust_locked(pi->pi_owner, oldpri);
1321 pi = uq_owner->uq_pi_blocked;
1326 * Insert a PI mutex into owned list.
1329 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1331 struct umtx_q *uq_owner;
1333 uq_owner = owner->td_umtxq;
1334 mtx_assert(&umtx_lock, MA_OWNED);
1335 if (pi->pi_owner != NULL)
1336 panic("pi_ower != NULL");
1337 pi->pi_owner = owner;
1338 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1342 * Claim ownership of a PI mutex.
1345 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1347 struct umtx_q *uq, *uq_owner;
1349 uq_owner = owner->td_umtxq;
1350 mtx_lock_spin(&umtx_lock);
1351 if (pi->pi_owner == owner) {
1352 mtx_unlock_spin(&umtx_lock);
1356 if (pi->pi_owner != NULL) {
1358 * userland may have already messed the mutex, sigh.
1360 mtx_unlock_spin(&umtx_lock);
1363 umtx_pi_setowner(pi, owner);
1364 uq = TAILQ_FIRST(&pi->pi_blocked);
1368 pri = UPRI(uq->uq_thread);
1370 if (pri < UPRI(owner))
1371 sched_lend_user_prio(owner, pri);
1372 thread_unlock(owner);
1374 mtx_unlock_spin(&umtx_lock);
1379 umtx_pi_adjust_locked(struct thread *td, u_char oldpri)
1386 * Pick up the lock that td is blocked on.
1388 pi = uq->uq_pi_blocked;
1391 /* Resort the turnstile on the list. */
1392 if (!umtx_pi_adjust_thread(pi, td))
1396 * If our priority was lowered and we are at the head of the
1397 * turnstile, then propagate our new priority up the chain.
1399 if (uq == TAILQ_FIRST(&pi->pi_blocked) && UPRI(td) < oldpri)
1400 umtx_propagate_priority(td);
1404 * Adjust a thread's order position in its blocked PI mutex,
1405 * this may result new priority propagating process.
1408 umtx_pi_adjust(struct thread *td, u_char oldpri)
1414 mtx_lock_spin(&umtx_lock);
1416 * Pick up the lock that td is blocked on.
1418 pi = uq->uq_pi_blocked;
1420 umtx_pi_adjust_locked(td, oldpri);
1421 mtx_unlock_spin(&umtx_lock);
1425 * Sleep on a PI mutex.
1428 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi,
1429 uint32_t owner, const char *wmesg, int timo)
1431 struct umtxq_chain *uc;
1432 struct thread *td, *td1;
1438 KASSERT(td == curthread, ("inconsistent uq_thread"));
1439 uc = umtxq_getchain(&uq->uq_key);
1440 UMTXQ_LOCKED_ASSERT(uc);
1442 if (pi->pi_owner == NULL) {
1444 * Current, We only support process private PI-mutex,
1445 * non-contended PI-mutexes are locked in userland.
1446 * Process shared PI-mutex should always be initialized
1447 * by kernel and be registered in kernel, locking should
1448 * always be done by kernel to avoid security problems.
1449 * For process private PI-mutex, we can find owner
1450 * thread and boost its priority safely.
1453 td1 = thread_find(curproc, owner);
1454 mtx_lock_spin(&umtx_lock);
1455 if (td1 != NULL && pi->pi_owner == NULL) {
1456 uq1 = td1->td_umtxq;
1457 umtx_pi_setowner(pi, td1);
1459 PROC_UNLOCK(curproc);
1461 mtx_lock_spin(&umtx_lock);
1464 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1465 pri = UPRI(uq1->uq_thread);
1471 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1473 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1475 uq->uq_pi_blocked = pi;
1477 td->td_flags |= TDF_UPIBLOCKED;
1479 mtx_unlock_spin(&umtx_lock);
1480 umtxq_unlock(&uq->uq_key);
1482 mtx_lock_spin(&umtx_lock);
1483 umtx_propagate_priority(td);
1484 mtx_unlock_spin(&umtx_lock);
1486 umtxq_lock(&uq->uq_key);
1487 if (uq->uq_flags & UQF_UMTXQ) {
1488 error = msleep(uq, &uc->uc_lock, PCATCH, wmesg, timo);
1489 if (error == EWOULDBLOCK)
1491 if (uq->uq_flags & UQF_UMTXQ) {
1492 umtxq_busy(&uq->uq_key);
1494 umtxq_unbusy(&uq->uq_key);
1497 umtxq_unlock(&uq->uq_key);
1499 mtx_lock_spin(&umtx_lock);
1500 uq->uq_pi_blocked = NULL;
1502 td->td_flags &= ~TDF_UPIBLOCKED;
1504 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1505 umtx_unpropagate_priority(pi);
1506 mtx_unlock_spin(&umtx_lock);
1508 umtxq_lock(&uq->uq_key);
1514 * Add reference count for a PI mutex.
1517 umtx_pi_ref(struct umtx_pi *pi)
1519 struct umtxq_chain *uc;
1521 uc = umtxq_getchain(&pi->pi_key);
1522 UMTXQ_LOCKED_ASSERT(uc);
1527 * Decrease reference count for a PI mutex, if the counter
1528 * is decreased to zero, its memory space is freed.
1531 umtx_pi_unref(struct umtx_pi *pi)
1533 struct umtxq_chain *uc;
1536 uc = umtxq_getchain(&pi->pi_key);
1537 UMTXQ_LOCKED_ASSERT(uc);
1538 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1539 if (--pi->pi_refcount == 0) {
1540 mtx_lock_spin(&umtx_lock);
1541 if (pi->pi_owner != NULL) {
1542 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested,
1544 pi->pi_owner = NULL;
1546 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1547 ("blocked queue not empty"));
1548 mtx_unlock_spin(&umtx_lock);
1549 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1557 * Find a PI mutex in hash table.
1559 static struct umtx_pi *
1560 umtx_pi_lookup(struct umtx_key *key)
1562 struct umtxq_chain *uc;
1565 uc = umtxq_getchain(key);
1566 UMTXQ_LOCKED_ASSERT(uc);
1568 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1569 if (umtx_key_match(&pi->pi_key, key)) {
1577 * Insert a PI mutex into hash table.
1580 umtx_pi_insert(struct umtx_pi *pi)
1582 struct umtxq_chain *uc;
1584 uc = umtxq_getchain(&pi->pi_key);
1585 UMTXQ_LOCKED_ASSERT(uc);
1586 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1593 _do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags, int timo,
1597 struct umtx_pi *pi, *new_pi;
1598 uint32_t id, owner, old;
1604 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1607 umtxq_lock(&uq->uq_key);
1608 pi = umtx_pi_lookup(&uq->uq_key);
1610 new_pi = umtx_pi_alloc(M_NOWAIT);
1611 if (new_pi == NULL) {
1612 umtxq_unlock(&uq->uq_key);
1613 new_pi = umtx_pi_alloc(M_WAITOK);
1614 new_pi->pi_key = uq->uq_key;
1615 umtxq_lock(&uq->uq_key);
1616 pi = umtx_pi_lookup(&uq->uq_key);
1618 umtx_pi_free(new_pi);
1622 if (new_pi != NULL) {
1623 new_pi->pi_key = uq->uq_key;
1624 umtx_pi_insert(new_pi);
1629 umtxq_unlock(&uq->uq_key);
1632 * Care must be exercised when dealing with umtx structure. It
1633 * can fault on any access.
1637 * Try the uncontested case. This should be done in userland.
1639 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id);
1641 /* The acquire succeeded. */
1642 if (owner == UMUTEX_UNOWNED) {
1647 /* The address was invalid. */
1653 /* If no one owns it but it is contested try to acquire it. */
1654 if (owner == UMUTEX_CONTESTED) {
1655 owner = casuword32(&m->m_owner,
1656 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1658 if (owner == UMUTEX_CONTESTED) {
1659 umtxq_lock(&uq->uq_key);
1660 error = umtx_pi_claim(pi, td);
1661 umtxq_unlock(&uq->uq_key);
1665 /* The address was invalid. */
1671 /* If this failed the lock has changed, restart. */
1675 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1676 (owner & ~UMUTEX_CONTESTED) == id) {
1687 * If we caught a signal, we have retried and now
1693 umtxq_lock(&uq->uq_key);
1694 umtxq_busy(&uq->uq_key);
1695 umtxq_unlock(&uq->uq_key);
1698 * Set the contested bit so that a release in user space
1699 * knows to use the system call for unlock. If this fails
1700 * either some one else has acquired the lock or it has been
1703 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED);
1705 /* The address was invalid. */
1707 umtxq_lock(&uq->uq_key);
1708 umtxq_unbusy(&uq->uq_key);
1709 umtxq_unlock(&uq->uq_key);
1714 umtxq_lock(&uq->uq_key);
1715 umtxq_unbusy(&uq->uq_key);
1717 * We set the contested bit, sleep. Otherwise the lock changed
1718 * and we need to retry or we lost a race to the thread
1719 * unlocking the umtx.
1722 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
1724 umtxq_unlock(&uq->uq_key);
1727 umtxq_lock(&uq->uq_key);
1729 umtxq_unlock(&uq->uq_key);
1731 umtx_key_release(&uq->uq_key);
1736 * Unlock a PI mutex.
1739 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags)
1741 struct umtx_key key;
1742 struct umtx_q *uq_first, *uq_first2, *uq_me;
1743 struct umtx_pi *pi, *pi2;
1744 uint32_t owner, old, id;
1751 * Make sure we own this mtx.
1753 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1757 if ((owner & ~UMUTEX_CONTESTED) != id)
1760 /* This should be done in userland */
1761 if ((owner & UMUTEX_CONTESTED) == 0) {
1762 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED);
1770 /* We should only ever be in here for contested locks */
1771 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1777 count = umtxq_count_pi(&key, &uq_first);
1778 if (uq_first != NULL) {
1779 pi = uq_first->uq_pi_blocked;
1780 if (pi->pi_owner != curthread) {
1783 /* userland messed the mutex */
1786 uq_me = curthread->td_umtxq;
1787 mtx_lock_spin(&umtx_lock);
1788 pi->pi_owner = NULL;
1789 TAILQ_REMOVE(&uq_me->uq_pi_contested, pi, pi_link);
1790 uq_first = TAILQ_FIRST(&pi->pi_blocked);
1792 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
1793 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
1794 if (uq_first2 != NULL) {
1795 if (pri > UPRI(uq_first2->uq_thread))
1796 pri = UPRI(uq_first2->uq_thread);
1799 thread_lock(curthread);
1800 sched_unlend_user_prio(curthread, pri);
1801 thread_unlock(curthread);
1802 mtx_unlock_spin(&umtx_lock);
1807 * When unlocking the umtx, it must be marked as unowned if
1808 * there is zero or one thread only waiting for it.
1809 * Otherwise, it must be marked as contested.
1811 old = casuword32(&m->m_owner, owner,
1812 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1815 if (uq_first != NULL)
1816 umtxq_signal_thread(uq_first);
1819 umtx_key_release(&key);
1831 _do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags, int timo,
1834 struct umtx_q *uq, *uq2;
1838 int error, pri, old_inherited_pri, su;
1842 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
1845 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
1847 old_inherited_pri = uq->uq_inherited_pri;
1848 umtxq_lock(&uq->uq_key);
1849 umtxq_busy(&uq->uq_key);
1850 umtxq_unlock(&uq->uq_key);
1852 ceiling = RTP_PRIO_MAX - fuword32(&m->m_ceilings[0]);
1853 if (ceiling > RTP_PRIO_MAX) {
1858 mtx_lock_spin(&umtx_lock);
1859 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
1860 mtx_unlock_spin(&umtx_lock);
1864 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
1865 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
1867 if (uq->uq_inherited_pri < UPRI(td))
1868 sched_lend_user_prio(td, uq->uq_inherited_pri);
1871 mtx_unlock_spin(&umtx_lock);
1873 owner = casuword32(&m->m_owner,
1874 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1876 if (owner == UMUTEX_CONTESTED) {
1881 /* The address was invalid. */
1887 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1888 (owner & ~UMUTEX_CONTESTED) == id) {
1899 * If we caught a signal, we have retried and now
1905 umtxq_lock(&uq->uq_key);
1907 umtxq_unbusy(&uq->uq_key);
1908 error = umtxq_sleep(uq, "umtxpp", timo);
1910 umtxq_unlock(&uq->uq_key);
1912 mtx_lock_spin(&umtx_lock);
1913 uq->uq_inherited_pri = old_inherited_pri;
1915 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
1916 uq2 = TAILQ_FIRST(&pi->pi_blocked);
1918 if (pri > UPRI(uq2->uq_thread))
1919 pri = UPRI(uq2->uq_thread);
1922 if (pri > uq->uq_inherited_pri)
1923 pri = uq->uq_inherited_pri;
1925 sched_unlend_user_prio(td, pri);
1927 mtx_unlock_spin(&umtx_lock);
1931 mtx_lock_spin(&umtx_lock);
1932 uq->uq_inherited_pri = old_inherited_pri;
1934 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
1935 uq2 = TAILQ_FIRST(&pi->pi_blocked);
1937 if (pri > UPRI(uq2->uq_thread))
1938 pri = UPRI(uq2->uq_thread);
1941 if (pri > uq->uq_inherited_pri)
1942 pri = uq->uq_inherited_pri;
1944 sched_unlend_user_prio(td, pri);
1946 mtx_unlock_spin(&umtx_lock);
1950 umtxq_lock(&uq->uq_key);
1951 umtxq_unbusy(&uq->uq_key);
1952 umtxq_unlock(&uq->uq_key);
1953 umtx_key_release(&uq->uq_key);
1958 * Unlock a PP mutex.
1961 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags)
1963 struct umtx_key key;
1964 struct umtx_q *uq, *uq2;
1968 int error, pri, new_inherited_pri, su;
1972 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
1975 * Make sure we own this mtx.
1977 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1981 if ((owner & ~UMUTEX_CONTESTED) != id)
1984 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
1989 new_inherited_pri = PRI_MAX;
1991 rceiling = RTP_PRIO_MAX - rceiling;
1992 if (rceiling > RTP_PRIO_MAX)
1994 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
1997 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2004 * For priority protected mutex, always set unlocked state
2005 * to UMUTEX_CONTESTED, so that userland always enters kernel
2006 * to lock the mutex, it is necessary because thread priority
2007 * has to be adjusted for such mutex.
2009 error = suword32(__DEVOLATILE(uint32_t *, &m->m_owner),
2014 umtxq_signal(&key, 1);
2021 mtx_lock_spin(&umtx_lock);
2023 uq->uq_inherited_pri = new_inherited_pri;
2025 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2026 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2028 if (pri > UPRI(uq2->uq_thread))
2029 pri = UPRI(uq2->uq_thread);
2032 if (pri > uq->uq_inherited_pri)
2033 pri = uq->uq_inherited_pri;
2035 sched_unlend_user_prio(td, pri);
2037 mtx_unlock_spin(&umtx_lock);
2039 umtx_key_release(&key);
2044 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2045 uint32_t *old_ceiling)
2048 uint32_t save_ceiling;
2053 flags = fuword32(&m->m_flags);
2054 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2056 if (ceiling > RTP_PRIO_MAX)
2060 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2064 umtxq_lock(&uq->uq_key);
2065 umtxq_busy(&uq->uq_key);
2066 umtxq_unlock(&uq->uq_key);
2068 save_ceiling = fuword32(&m->m_ceilings[0]);
2070 owner = casuword32(&m->m_owner,
2071 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
2073 if (owner == UMUTEX_CONTESTED) {
2074 suword32(&m->m_ceilings[0], ceiling);
2075 suword32(__DEVOLATILE(uint32_t *, &m->m_owner),
2081 /* The address was invalid. */
2087 if ((owner & ~UMUTEX_CONTESTED) == id) {
2088 suword32(&m->m_ceilings[0], ceiling);
2094 * If we caught a signal, we have retried and now
2101 * We set the contested bit, sleep. Otherwise the lock changed
2102 * and we need to retry or we lost a race to the thread
2103 * unlocking the umtx.
2105 umtxq_lock(&uq->uq_key);
2107 umtxq_unbusy(&uq->uq_key);
2108 error = umtxq_sleep(uq, "umtxpp", 0);
2110 umtxq_unlock(&uq->uq_key);
2112 umtxq_lock(&uq->uq_key);
2114 umtxq_signal(&uq->uq_key, INT_MAX);
2115 umtxq_unbusy(&uq->uq_key);
2116 umtxq_unlock(&uq->uq_key);
2117 umtx_key_release(&uq->uq_key);
2118 if (error == 0 && old_ceiling != NULL)
2119 suword32(old_ceiling, save_ceiling);
2124 _do_lock_umutex(struct thread *td, struct umutex *m, int flags, int timo,
2127 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2129 return (_do_lock_normal(td, m, flags, timo, try));
2130 case UMUTEX_PRIO_INHERIT:
2131 return (_do_lock_pi(td, m, flags, timo, try));
2132 case UMUTEX_PRIO_PROTECT:
2133 return (_do_lock_pp(td, m, flags, timo, try));
2139 * Lock a userland POSIX mutex.
2142 do_lock_umutex(struct thread *td, struct umutex *m,
2143 struct timespec *timeout, int try)
2145 struct timespec ts, ts2, ts3;
2150 flags = fuword32(&m->m_flags);
2154 if (timeout == NULL) {
2155 error = _do_lock_umutex(td, m, flags, 0, try);
2156 /* Mutex locking is restarted if it is interrupted. */
2161 timespecadd(&ts, timeout);
2162 TIMESPEC_TO_TIMEVAL(&tv, timeout);
2164 error = _do_lock_umutex(td, m, flags, tvtohz(&tv), try);
2165 if (error != ETIMEDOUT)
2167 getnanouptime(&ts2);
2168 if (timespeccmp(&ts2, &ts, >=)) {
2173 timespecsub(&ts3, &ts2);
2174 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
2176 /* Timed-locking is not restarted. */
2177 if (error == ERESTART)
2184 * Unlock a userland POSIX mutex.
2187 do_unlock_umutex(struct thread *td, struct umutex *m)
2191 flags = fuword32(&m->m_flags);
2195 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2197 return (do_unlock_normal(td, m, flags));
2198 case UMUTEX_PRIO_INHERIT:
2199 return (do_unlock_pi(td, m, flags));
2200 case UMUTEX_PRIO_PROTECT:
2201 return (do_unlock_pp(td, m, flags));
2208 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2209 struct timespec *timeout, u_long wflags)
2213 struct timespec cts, ets, tts;
2218 flags = fuword32(&cv->c_flags);
2219 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2222 umtxq_lock(&uq->uq_key);
2223 umtxq_busy(&uq->uq_key);
2225 umtxq_unlock(&uq->uq_key);
2228 * The magic thing is we should set c_has_waiters to 1 before
2229 * releasing user mutex.
2231 suword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters), 1);
2233 umtxq_lock(&uq->uq_key);
2234 umtxq_unbusy(&uq->uq_key);
2235 umtxq_unlock(&uq->uq_key);
2237 error = do_unlock_umutex(td, m);
2239 umtxq_lock(&uq->uq_key);
2241 if ((wflags & UMTX_CHECK_UNPARKING) &&
2242 (td->td_pflags & TDP_WAKEUP)) {
2243 td->td_pflags &= ~TDP_WAKEUP;
2245 } else if (timeout == NULL) {
2246 error = umtxq_sleep(uq, "ucond", 0);
2248 getnanouptime(&ets);
2249 timespecadd(&ets, timeout);
2250 TIMESPEC_TO_TIMEVAL(&tv, timeout);
2252 error = umtxq_sleep(uq, "ucond", tvtohz(&tv));
2253 if (error != ETIMEDOUT)
2255 getnanouptime(&cts);
2256 if (timespeccmp(&cts, &ets, >=)) {
2261 timespecsub(&tts, &cts);
2262 TIMESPEC_TO_TIMEVAL(&tv, &tts);
2268 if ((uq->uq_flags & UQF_UMTXQ) == 0) {
2270 * If we concurrently got do_cv_signal()d
2271 * and we got an error or UNIX signals or a timeout,
2272 * then, perform another umtxq_signal to avoid
2273 * consuming the wakeup. This may cause supurious
2274 * wakeup for another thread which was just queued,
2275 * but SUSV3 explicitly allows supurious wakeup to
2276 * occur, and indeed a kernel based implementation
2279 if (!umtxq_signal(&uq->uq_key, 1))
2282 if (error == ERESTART)
2286 umtxq_unlock(&uq->uq_key);
2287 umtx_key_release(&uq->uq_key);
2292 * Signal a userland condition variable.
2295 do_cv_signal(struct thread *td, struct ucond *cv)
2297 struct umtx_key key;
2298 int error, cnt, nwake;
2301 flags = fuword32(&cv->c_flags);
2302 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2306 cnt = umtxq_count(&key);
2307 nwake = umtxq_signal(&key, 1);
2311 __DEVOLATILE(uint32_t *, &cv->c_has_waiters), 0);
2316 umtx_key_release(&key);
2321 do_cv_broadcast(struct thread *td, struct ucond *cv)
2323 struct umtx_key key;
2327 flags = fuword32(&cv->c_flags);
2328 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2333 umtxq_signal(&key, INT_MAX);
2336 error = suword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters), 0);
2342 umtx_key_release(&key);
2347 _umtx_lock(struct thread *td, struct _umtx_lock_args *uap)
2348 /* struct umtx *umtx */
2350 return _do_lock_umtx(td, uap->umtx, td->td_tid, 0);
2354 _umtx_unlock(struct thread *td, struct _umtx_unlock_args *uap)
2355 /* struct umtx *umtx */
2357 return do_unlock_umtx(td, uap->umtx, td->td_tid);
2361 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap)
2363 struct timespec *ts, timeout;
2366 /* Allow a null timespec (wait forever). */
2367 if (uap->uaddr2 == NULL)
2370 error = copyin(uap->uaddr2, &timeout, sizeof(timeout));
2373 if (timeout.tv_nsec >= 1000000000 ||
2374 timeout.tv_nsec < 0) {
2379 return (do_lock_umtx(td, uap->obj, uap->val, ts));
2383 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap)
2385 return (do_unlock_umtx(td, uap->obj, uap->val));
2389 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
2391 struct timespec *ts, timeout;
2394 if (uap->uaddr2 == NULL)
2397 error = copyin(uap->uaddr2, &timeout, sizeof(timeout));
2400 if (timeout.tv_nsec >= 1000000000 ||
2401 timeout.tv_nsec < 0)
2405 return do_wait(td, uap->obj, uap->val, ts, 0);
2409 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
2411 struct timespec *ts, timeout;
2414 if (uap->uaddr2 == NULL)
2417 error = copyin(uap->uaddr2, &timeout, sizeof(timeout));
2420 if (timeout.tv_nsec >= 1000000000 ||
2421 timeout.tv_nsec < 0)
2425 return do_wait(td, uap->obj, uap->val, ts, 1);
2429 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
2431 return (kern_umtx_wake(td, uap->obj, uap->val));
2435 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
2437 struct timespec *ts, timeout;
2440 /* Allow a null timespec (wait forever). */
2441 if (uap->uaddr2 == NULL)
2444 error = copyin(uap->uaddr2, &timeout,
2448 if (timeout.tv_nsec >= 1000000000 ||
2449 timeout.tv_nsec < 0) {
2454 return do_lock_umutex(td, uap->obj, ts, 0);
2458 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
2460 return do_lock_umutex(td, uap->obj, NULL, 1);
2464 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
2466 return do_unlock_umutex(td, uap->obj);
2470 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
2472 return do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1);
2476 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
2478 struct timespec *ts, timeout;
2481 /* Allow a null timespec (wait forever). */
2482 if (uap->uaddr2 == NULL)
2485 error = copyin(uap->uaddr2, &timeout,
2489 if (timeout.tv_nsec >= 1000000000 ||
2490 timeout.tv_nsec < 0) {
2495 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
2499 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
2501 return do_cv_signal(td, uap->obj);
2505 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
2507 return do_cv_broadcast(td, uap->obj);
2510 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
2512 static _umtx_op_func op_table[] = {
2513 __umtx_op_lock_umtx, /* UMTX_OP_LOCK */
2514 __umtx_op_unlock_umtx, /* UMTX_OP_UNLOCK */
2515 __umtx_op_wait, /* UMTX_OP_WAIT */
2516 __umtx_op_wake, /* UMTX_OP_WAKE */
2517 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_TRYLOCK */
2518 __umtx_op_lock_umutex, /* UMTX_OP_MUTEX_LOCK */
2519 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
2520 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
2521 __umtx_op_cv_wait, /* UMTX_OP_CV_WAIT*/
2522 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
2523 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
2524 __umtx_op_wait_uint /* UMTX_OP_WAIT_UINT */
2528 _umtx_op(struct thread *td, struct _umtx_op_args *uap)
2530 if ((unsigned)uap->op < UMTX_OP_MAX)
2531 return (*op_table[uap->op])(td, uap);
2537 freebsd32_umtx_lock(struct thread *td, struct freebsd32_umtx_lock_args *uap)
2538 /* struct umtx *umtx */
2540 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
2544 freebsd32_umtx_unlock(struct thread *td, struct freebsd32_umtx_unlock_args *uap)
2545 /* struct umtx *umtx */
2547 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
2556 copyin_timeout32(void *addr, struct timespec *tsp)
2558 struct timespec32 ts32;
2561 error = copyin(addr, &ts32, sizeof(struct timespec32));
2563 tsp->tv_sec = ts32.tv_sec;
2564 tsp->tv_nsec = ts32.tv_nsec;
2570 __umtx_op_lock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
2572 struct timespec *ts, timeout;
2575 /* Allow a null timespec (wait forever). */
2576 if (uap->uaddr2 == NULL)
2579 error = copyin_timeout32(uap->uaddr2, &timeout);
2582 if (timeout.tv_nsec >= 1000000000 ||
2583 timeout.tv_nsec < 0) {
2588 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
2592 __umtx_op_unlock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
2594 return (do_unlock_umtx32(td, uap->obj, (uint32_t)uap->val));
2598 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
2600 struct timespec *ts, timeout;
2603 if (uap->uaddr2 == NULL)
2606 error = copyin_timeout32(uap->uaddr2, &timeout);
2609 if (timeout.tv_nsec >= 1000000000 ||
2610 timeout.tv_nsec < 0)
2614 return do_wait(td, uap->obj, uap->val, ts, 1);
2618 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
2620 struct timespec *ts, timeout;
2623 /* Allow a null timespec (wait forever). */
2624 if (uap->uaddr2 == NULL)
2627 error = copyin_timeout32(uap->uaddr2, &timeout);
2630 if (timeout.tv_nsec >= 1000000000 ||
2631 timeout.tv_nsec < 0)
2635 return do_lock_umutex(td, uap->obj, ts, 0);
2639 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
2641 struct timespec *ts, timeout;
2644 /* Allow a null timespec (wait forever). */
2645 if (uap->uaddr2 == NULL)
2648 error = copyin_timeout32(uap->uaddr2, &timeout);
2651 if (timeout.tv_nsec >= 1000000000 ||
2652 timeout.tv_nsec < 0)
2656 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
2659 static _umtx_op_func op_table_compat32[] = {
2660 __umtx_op_lock_umtx_compat32, /* UMTX_OP_LOCK */
2661 __umtx_op_unlock_umtx_compat32, /* UMTX_OP_UNLOCK */
2662 __umtx_op_wait_compat32, /* UMTX_OP_WAIT */
2663 __umtx_op_wake, /* UMTX_OP_WAKE */
2664 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_LOCK */
2665 __umtx_op_lock_umutex_compat32, /* UMTX_OP_MUTEX_TRYLOCK */
2666 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
2667 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
2668 __umtx_op_cv_wait_compat32, /* UMTX_OP_CV_WAIT*/
2669 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
2670 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
2671 __umtx_op_wait_compat32 /* UMTX_OP_WAIT_UINT */
2675 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
2677 if ((unsigned)uap->op < UMTX_OP_MAX)
2678 return (*op_table_compat32[uap->op])(td,
2679 (struct _umtx_op_args *)uap);
2685 umtx_thread_init(struct thread *td)
2687 td->td_umtxq = umtxq_alloc();
2688 td->td_umtxq->uq_thread = td;
2692 umtx_thread_fini(struct thread *td)
2694 umtxq_free(td->td_umtxq);
2698 * It will be called when new thread is created, e.g fork().
2701 umtx_thread_alloc(struct thread *td)
2706 uq->uq_inherited_pri = PRI_MAX;
2708 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
2709 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
2710 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
2711 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
2718 umtx_exec_hook(void *arg __unused, struct proc *p __unused,
2719 struct image_params *imgp __unused)
2721 umtx_thread_cleanup(curthread);
2725 * thread_exit() hook.
2728 umtx_thread_exit(struct thread *td)
2730 umtx_thread_cleanup(td);
2734 * clean up umtx data.
2737 umtx_thread_cleanup(struct thread *td)
2742 if ((uq = td->td_umtxq) == NULL)
2745 mtx_lock_spin(&umtx_lock);
2746 uq->uq_inherited_pri = PRI_MAX;
2747 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
2748 pi->pi_owner = NULL;
2749 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
2752 td->td_flags &= ~TDF_UBORROWING;
2754 mtx_unlock_spin(&umtx_lock);