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>
39 #include <sys/sched.h>
40 #include <sys/sysctl.h>
41 #include <sys/sysent.h>
42 #include <sys/systm.h>
43 #include <sys/sysproto.h>
44 #include <sys/eventhandler.h>
48 #include <vm/vm_param.h>
50 #include <vm/vm_map.h>
51 #include <vm/vm_object.h>
54 #include <compat/freebsd32/freebsd32_proto.h>
57 #define TYPE_SIMPLE_LOCK 0
58 #define TYPE_SIMPLE_WAIT 1
59 #define TYPE_NORMAL_UMUTEX 2
60 #define TYPE_PI_UMUTEX 3
61 #define TYPE_PP_UMUTEX 4
64 /* Key to represent a unique userland synchronous object */
85 /* Priority inheritance mutex info. */
88 struct thread *pi_owner;
93 /* List entry to link umtx holding by thread */
94 TAILQ_ENTRY(umtx_pi) pi_link;
96 /* List entry in hash */
97 TAILQ_ENTRY(umtx_pi) pi_hashlink;
99 /* List for waiters */
100 TAILQ_HEAD(,umtx_q) pi_blocked;
102 /* Identify a userland lock object */
103 struct umtx_key pi_key;
106 /* A userland synchronous object user. */
108 /* Linked list for the hash. */
109 TAILQ_ENTRY(umtx_q) uq_link;
112 struct umtx_key uq_key;
116 #define UQF_UMTXQ 0x0001
118 /* The thread waits on. */
119 struct thread *uq_thread;
122 * Blocked on PI mutex. read can use chain lock
123 * or sched_lock, write must have both chain lock and
124 * sched_lock being hold.
126 struct umtx_pi *uq_pi_blocked;
128 /* On blocked list */
129 TAILQ_ENTRY(umtx_q) uq_lockq;
131 /* Thread contending with us */
132 TAILQ_HEAD(,umtx_pi) uq_pi_contested;
134 /* Inherited priority from PP mutex */
135 u_char uq_inherited_pri;
138 TAILQ_HEAD(umtxq_head, umtx_q);
140 /* Userland lock object's wait-queue chain */
142 /* Lock for this chain. */
145 /* List of sleep queues. */
146 struct umtxq_head uc_queue;
151 /* Chain lock waiters */
154 /* All PI in the list */
155 TAILQ_HEAD(,umtx_pi) uc_pi_list;
158 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
161 * Don't propagate time-sharing priority, there is a security reason,
162 * a user can simply introduce PI-mutex, let thread A lock the mutex,
163 * and let another thread B block on the mutex, because B is
164 * sleeping, its priority will be boosted, this causes A's priority to
165 * be boosted via priority propagating too and will never be lowered even
166 * if it is using 100%CPU, this is unfair to other processes.
169 #define UPRI(td) (((td)->td_ksegrp->kg_user_pri >= PRI_MIN_TIMESHARE &&\
170 (td)->td_ksegrp->kg_user_pri <= PRI_MAX_TIMESHARE) ?\
171 PRI_MAX_TIMESHARE : (td)->td_ksegrp->kg_user_pri)
173 #define GOLDEN_RATIO_PRIME 2654404609U
174 #define UMTX_CHAINS 128
175 #define UMTX_SHIFTS (__WORD_BIT - 7)
177 #define THREAD_SHARE 0
178 #define PROCESS_SHARE 1
181 #define GET_SHARE(flags) \
182 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
184 static uma_zone_t umtx_pi_zone;
185 static struct umtxq_chain umtxq_chains[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(struct umtx_q *uq);
201 static void umtxq_remove(struct umtx_q *uq);
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 int umtxq_signal(struct umtx_key *key, int nr_wakeup);
205 static int umtx_key_match(const struct umtx_key *k1, const struct umtx_key *k2);
206 static int umtx_key_get(void *addr, int type, int share,
207 struct umtx_key *key);
208 static void umtx_key_release(struct umtx_key *key);
209 static struct umtx_pi *umtx_pi_alloc(void);
210 static void umtx_pi_free(struct umtx_pi *pi);
211 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags);
212 static void umtx_thread_cleanup(struct thread *td);
213 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
214 struct image_params *imgp __unused);
215 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
218 umtxq_sysinit(void *arg __unused)
222 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
223 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
224 for (i = 0; i < UMTX_CHAINS; ++i) {
225 mtx_init(&umtxq_chains[i].uc_lock, "umtxql", NULL,
226 MTX_DEF | MTX_DUPOK);
227 TAILQ_INIT(&umtxq_chains[i].uc_queue);
228 TAILQ_INIT(&umtxq_chains[i].uc_pi_list);
229 umtxq_chains[i].uc_busy = 0;
230 umtxq_chains[i].uc_waiters = 0;
232 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
233 EVENTHANDLER_PRI_ANY);
241 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
242 TAILQ_INIT(&uq->uq_pi_contested);
243 uq->uq_inherited_pri = PRI_MAX;
248 umtxq_free(struct umtx_q *uq)
254 umtxq_hash(struct umtx_key *key)
256 unsigned n = (uintptr_t)key->info.both.a + key->info.both.b;
257 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
261 umtx_key_match(const struct umtx_key *k1, const struct umtx_key *k2)
263 return (k1->type == k2->type &&
264 k1->info.both.a == k2->info.both.a &&
265 k1->info.both.b == k2->info.both.b);
268 static inline struct umtxq_chain *
269 umtxq_getchain(struct umtx_key *key)
271 return (&umtxq_chains[key->hash]);
275 * Set chain to busy state when following operation
276 * may be blocked (kernel mutex can not be used).
279 umtxq_busy(struct umtx_key *key)
281 struct umtxq_chain *uc;
283 uc = umtxq_getchain(key);
284 mtx_assert(&uc->uc_lock, MA_OWNED);
285 while (uc->uc_busy != 0) {
287 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
297 umtxq_unbusy(struct umtx_key *key)
299 struct umtxq_chain *uc;
301 uc = umtxq_getchain(key);
302 mtx_assert(&uc->uc_lock, MA_OWNED);
303 KASSERT(uc->uc_busy != 0, ("not busy"));
313 umtxq_lock(struct umtx_key *key)
315 struct umtxq_chain *uc;
317 uc = umtxq_getchain(key);
318 mtx_lock(&uc->uc_lock);
325 umtxq_unlock(struct umtx_key *key)
327 struct umtxq_chain *uc;
329 uc = umtxq_getchain(key);
330 mtx_unlock(&uc->uc_lock);
334 * Insert a thread onto the umtx queue.
337 umtxq_insert(struct umtx_q *uq)
339 struct umtxq_chain *uc;
341 uc = umtxq_getchain(&uq->uq_key);
342 UMTXQ_LOCKED_ASSERT(uc);
343 TAILQ_INSERT_TAIL(&uc->uc_queue, uq, uq_link);
344 uq->uq_flags |= UQF_UMTXQ;
348 * Remove thread from the umtx queue.
351 umtxq_remove(struct umtx_q *uq)
353 struct umtxq_chain *uc;
355 uc = umtxq_getchain(&uq->uq_key);
356 UMTXQ_LOCKED_ASSERT(uc);
357 if (uq->uq_flags & UQF_UMTXQ) {
358 TAILQ_REMOVE(&uc->uc_queue, uq, uq_link);
359 uq->uq_flags &= ~UQF_UMTXQ;
364 * Check if there are multiple waiters
367 umtxq_count(struct umtx_key *key)
369 struct umtxq_chain *uc;
373 uc = umtxq_getchain(key);
374 UMTXQ_LOCKED_ASSERT(uc);
375 TAILQ_FOREACH(uq, &uc->uc_queue, uq_link) {
376 if (umtx_key_match(&uq->uq_key, key)) {
385 * Check if there are multiple PI waiters and returns first
389 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
391 struct umtxq_chain *uc;
396 uc = umtxq_getchain(key);
397 UMTXQ_LOCKED_ASSERT(uc);
398 TAILQ_FOREACH(uq, &uc->uc_queue, uq_link) {
399 if (umtx_key_match(&uq->uq_key, key)) {
409 * Wake up threads waiting on an userland object.
412 umtxq_signal(struct umtx_key *key, int n_wake)
414 struct umtxq_chain *uc;
415 struct umtx_q *uq, *next;
419 uc = umtxq_getchain(key);
420 UMTXQ_LOCKED_ASSERT(uc);
421 TAILQ_FOREACH_SAFE(uq, &uc->uc_queue, uq_link, next) {
422 if (umtx_key_match(&uq->uq_key, key)) {
433 * Wake up specified thread.
436 umtxq_signal_thread(struct umtx_q *uq)
438 struct umtxq_chain *uc;
440 uc = umtxq_getchain(&uq->uq_key);
441 UMTXQ_LOCKED_ASSERT(uc);
447 * Put thread into sleep state, before sleeping, check if
448 * thread was removed from umtx queue.
451 umtxq_sleep(struct umtx_q *uq, const char *wmesg, int timo)
453 struct umtxq_chain *uc;
456 uc = umtxq_getchain(&uq->uq_key);
457 UMTXQ_LOCKED_ASSERT(uc);
458 if (!(uq->uq_flags & UQF_UMTXQ))
460 error = msleep(uq, &uc->uc_lock, PCATCH, wmesg, timo);
461 if (error == EWOULDBLOCK)
467 * Convert userspace address into unique logical address.
470 umtx_key_get(void *addr, int type, int share, struct umtx_key *key)
472 struct thread *td = curthread;
474 vm_map_entry_t entry;
480 if (share == THREAD_SHARE) {
482 key->info.private.vs = td->td_proc->p_vmspace;
483 key->info.private.addr = (uintptr_t)addr;
484 } else if (share == PROCESS_SHARE || share == AUTO_SHARE) {
485 map = &td->td_proc->p_vmspace->vm_map;
486 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
487 &entry, &key->info.shared.object, &pindex, &prot,
488 &wired) != KERN_SUCCESS) {
492 if ((share == PROCESS_SHARE) ||
493 (share == AUTO_SHARE &&
494 VM_INHERIT_SHARE == entry->inheritance)) {
496 key->info.shared.offset = entry->offset + entry->start -
498 vm_object_reference(key->info.shared.object);
501 key->info.private.vs = td->td_proc->p_vmspace;
502 key->info.private.addr = (uintptr_t)addr;
504 vm_map_lookup_done(map, entry);
515 umtx_key_release(struct umtx_key *key)
518 vm_object_deallocate(key->info.shared.object);
522 * Lock a umtx object.
525 _do_lock_umtx(struct thread *td, struct umtx *umtx, uintptr_t id, int timo)
535 * Care must be exercised when dealing with umtx structure. It
536 * can fault on any access.
540 * Try the uncontested case. This should be done in userland.
542 owner = casuptr((intptr_t *)&umtx->u_owner, UMTX_UNOWNED, id);
544 /* The acquire succeeded. */
545 if (owner == UMTX_UNOWNED)
548 /* The address was invalid. */
552 /* If no one owns it but it is contested try to acquire it. */
553 if (owner == UMTX_CONTESTED) {
554 owner = casuptr((intptr_t *)&umtx->u_owner,
555 UMTX_CONTESTED, id | UMTX_CONTESTED);
557 if (owner == UMTX_CONTESTED)
560 /* The address was invalid. */
564 /* If this failed the lock has changed, restart. */
569 * If we caught a signal, we have retried and now
575 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
576 AUTO_SHARE, &uq->uq_key)) != 0)
579 umtxq_lock(&uq->uq_key);
580 umtxq_busy(&uq->uq_key);
582 umtxq_unbusy(&uq->uq_key);
583 umtxq_unlock(&uq->uq_key);
586 * Set the contested bit so that a release in user space
587 * knows to use the system call for unlock. If this fails
588 * either some one else has acquired the lock or it has been
591 old = casuptr((intptr_t *)&umtx->u_owner, owner,
592 owner | UMTX_CONTESTED);
594 /* The address was invalid. */
596 umtxq_lock(&uq->uq_key);
598 umtxq_unlock(&uq->uq_key);
599 umtx_key_release(&uq->uq_key);
604 * We set the contested bit, sleep. Otherwise the lock changed
605 * and we need to retry or we lost a race to the thread
606 * unlocking the umtx.
608 umtxq_lock(&uq->uq_key);
610 error = umtxq_sleep(uq, "umtx", timo);
612 umtxq_unlock(&uq->uq_key);
613 umtx_key_release(&uq->uq_key);
620 * Lock a umtx object.
623 do_lock_umtx(struct thread *td, struct umtx *umtx, uintptr_t id,
624 struct timespec *timeout)
626 struct timespec ts, ts2, ts3;
630 if (timeout == NULL) {
631 error = _do_lock_umtx(td, umtx, id, 0);
632 /* Mutex locking is restarted if it is interrupted. */
637 timespecadd(&ts, timeout);
638 TIMESPEC_TO_TIMEVAL(&tv, timeout);
640 error = _do_lock_umtx(td, umtx, id, tvtohz(&tv));
641 if (error != ETIMEDOUT)
644 if (timespeccmp(&ts2, &ts, >=)) {
649 timespecsub(&ts3, &ts2);
650 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
652 /* Timed-locking is not restarted. */
653 if (error == ERESTART)
660 * Unlock a umtx object.
663 do_unlock_umtx(struct thread *td, struct umtx *umtx, uintptr_t id)
672 * Make sure we own this mtx.
674 * XXX Need a {fu,su}ptr this is not correct on arch where
675 * sizeof(intptr_t) != sizeof(long).
677 owner = fuword(&umtx->u_owner);
681 if ((owner & ~UMTX_CONTESTED) != id)
684 /* This should be done in userland */
685 if ((owner & UMTX_CONTESTED) == 0) {
686 old = casuptr((intptr_t *)&umtx->u_owner, owner,
695 /* We should only ever be in here for contested locks */
696 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
702 count = umtxq_count(&key);
706 * When unlocking the umtx, it must be marked as unowned if
707 * there is zero or one thread only waiting for it.
708 * Otherwise, it must be marked as contested.
710 old = casuptr((intptr_t *)&umtx->u_owner, owner,
711 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
713 umtxq_signal(&key,1);
716 umtx_key_release(&key);
727 * Lock a umtx object.
730 _do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id, int timo)
740 * Care must be exercised when dealing with umtx structure. It
741 * can fault on any access.
745 * Try the uncontested case. This should be done in userland.
747 owner = casuword32(m, UMUTEX_UNOWNED, id);
749 /* The acquire succeeded. */
750 if (owner == UMUTEX_UNOWNED)
753 /* The address was invalid. */
757 /* If no one owns it but it is contested try to acquire it. */
758 if (owner == UMUTEX_CONTESTED) {
759 owner = casuword32(m,
760 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
761 if (owner == UMUTEX_CONTESTED)
764 /* The address was invalid. */
768 /* If this failed the lock has changed, restart. */
773 * If we caught a signal, we have retried and now
779 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
780 AUTO_SHARE, &uq->uq_key)) != 0)
783 umtxq_lock(&uq->uq_key);
784 umtxq_busy(&uq->uq_key);
786 umtxq_unbusy(&uq->uq_key);
787 umtxq_unlock(&uq->uq_key);
790 * Set the contested bit so that a release in user space
791 * knows to use the system call for unlock. If this fails
792 * either some one else has acquired the lock or it has been
795 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
797 /* The address was invalid. */
799 umtxq_lock(&uq->uq_key);
801 umtxq_unlock(&uq->uq_key);
802 umtx_key_release(&uq->uq_key);
807 * We set the contested bit, sleep. Otherwise the lock changed
808 * and we need to retry or we lost a race to the thread
809 * unlocking the umtx.
811 umtxq_lock(&uq->uq_key);
813 error = umtxq_sleep(uq, "umtx", timo);
815 umtxq_unlock(&uq->uq_key);
816 umtx_key_release(&uq->uq_key);
823 * Lock a umtx object.
826 do_lock_umtx32(struct thread *td, void *m, uint32_t id,
827 struct timespec *timeout)
829 struct timespec ts, ts2, ts3;
833 if (timeout == NULL) {
834 error = _do_lock_umtx32(td, m, id, 0);
835 /* Mutex locking is restarted if it is interrupted. */
840 timespecadd(&ts, timeout);
841 TIMESPEC_TO_TIMEVAL(&tv, timeout);
843 error = _do_lock_umtx32(td, m, id, tvtohz(&tv));
844 if (error != ETIMEDOUT)
847 if (timespeccmp(&ts2, &ts, >=)) {
852 timespecsub(&ts3, &ts2);
853 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
855 /* Timed-locking is not restarted. */
856 if (error == ERESTART)
863 * Unlock a umtx object.
866 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
875 * Make sure we own this mtx.
877 * XXX Need a {fu,su}ptr this is not correct on arch where
878 * sizeof(intptr_t) != sizeof(long).
884 if ((owner & ~UMUTEX_CONTESTED) != id)
887 /* This should be done in userland */
888 if ((owner & UMUTEX_CONTESTED) == 0) {
889 old = casuword32(m, owner, UMUTEX_UNOWNED);
897 /* We should only ever be in here for contested locks */
898 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
904 count = umtxq_count(&key);
908 * When unlocking the umtx, it must be marked as unowned if
909 * there is zero or one thread only waiting for it.
910 * Otherwise, it must be marked as contested.
912 old = casuword32(m, owner,
913 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
915 umtxq_signal(&key,1);
918 umtx_key_release(&key);
928 * Fetch and compare value, sleep on the address if value is not changed.
931 do_wait(struct thread *td, void *addr, uintptr_t id,
932 struct timespec *timeout, int compat32)
935 struct timespec ts, ts2, ts3;
941 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT, AUTO_SHARE,
945 umtxq_lock(&uq->uq_key);
947 umtxq_unlock(&uq->uq_key);
951 tmp = fuword32(addr);
953 umtxq_lock(&uq->uq_key);
955 umtxq_unlock(&uq->uq_key);
956 } else if (timeout == NULL) {
957 umtxq_lock(&uq->uq_key);
958 error = umtxq_sleep(uq, "ucond", 0);
960 umtxq_unlock(&uq->uq_key);
963 timespecadd(&ts, timeout);
964 TIMESPEC_TO_TIMEVAL(&tv, timeout);
965 umtxq_lock(&uq->uq_key);
967 error = umtxq_sleep(uq, "ucond", tvtohz(&tv));
968 if (!(uq->uq_flags & UQF_UMTXQ))
970 if (error != ETIMEDOUT)
972 umtxq_unlock(&uq->uq_key);
974 if (timespeccmp(&ts2, &ts, >=)) {
976 umtxq_lock(&uq->uq_key);
980 timespecsub(&ts3, &ts2);
981 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
982 umtxq_lock(&uq->uq_key);
985 umtxq_unlock(&uq->uq_key);
987 umtx_key_release(&uq->uq_key);
988 if (error == ERESTART)
994 * Wake up threads sleeping on the specified address.
997 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake)
1002 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT, AUTO_SHARE,
1006 ret = umtxq_signal(&key, n_wake);
1008 umtx_key_release(&key);
1013 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1016 _do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags, int timo,
1020 uint32_t owner, old, id;
1027 * Care must be exercised when dealing with umtx structure. It
1028 * can fault on any access.
1032 * Try the uncontested case. This should be done in userland.
1034 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id);
1036 /* The acquire succeeded. */
1037 if (owner == UMUTEX_UNOWNED)
1040 /* The address was invalid. */
1044 /* If no one owns it but it is contested try to acquire it. */
1045 if (owner == UMUTEX_CONTESTED) {
1046 owner = casuword32(&m->m_owner,
1047 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1049 if (owner == UMUTEX_CONTESTED)
1052 /* The address was invalid. */
1056 /* If this failed the lock has changed, restart. */
1060 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1061 (owner & ~UMUTEX_CONTESTED) == id)
1068 * If we caught a signal, we have retried and now
1074 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1075 GET_SHARE(flags), &uq->uq_key)) != 0)
1078 umtxq_lock(&uq->uq_key);
1079 umtxq_busy(&uq->uq_key);
1081 umtxq_unbusy(&uq->uq_key);
1082 umtxq_unlock(&uq->uq_key);
1085 * Set the contested bit so that a release in user space
1086 * knows to use the system call for unlock. If this fails
1087 * either some one else has acquired the lock or it has been
1090 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED);
1092 /* The address was invalid. */
1094 umtxq_lock(&uq->uq_key);
1096 umtxq_unlock(&uq->uq_key);
1097 umtx_key_release(&uq->uq_key);
1102 * We set the contested bit, sleep. Otherwise the lock changed
1103 * and we need to retry or we lost a race to the thread
1104 * unlocking the umtx.
1106 umtxq_lock(&uq->uq_key);
1108 error = umtxq_sleep(uq, "umtxn", timo);
1110 umtxq_unlock(&uq->uq_key);
1111 umtx_key_release(&uq->uq_key);
1118 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1121 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1124 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags)
1126 struct umtx_key key;
1127 uint32_t owner, old, id;
1133 * Make sure we own this mtx.
1135 owner = fuword32(&m->m_owner);
1139 if ((owner & ~UMUTEX_CONTESTED) != id)
1142 /* This should be done in userland */
1143 if ((owner & UMUTEX_CONTESTED) == 0) {
1144 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED);
1152 /* We should only ever be in here for contested locks */
1153 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1159 count = umtxq_count(&key);
1163 * When unlocking the umtx, it must be marked as unowned if
1164 * there is zero or one thread only waiting for it.
1165 * Otherwise, it must be marked as contested.
1167 old = casuword32(&m->m_owner, owner,
1168 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1170 umtxq_signal(&key,1);
1173 umtx_key_release(&key);
1181 static inline struct umtx_pi *
1186 pi = uma_zalloc(umtx_pi_zone, M_ZERO | M_WAITOK);
1187 TAILQ_INIT(&pi->pi_blocked);
1188 atomic_add_int(&umtx_pi_allocated, 1);
1193 umtx_pi_free(struct umtx_pi *pi)
1195 uma_zfree(umtx_pi_zone, pi);
1196 atomic_add_int(&umtx_pi_allocated, -1);
1200 * Adjust the thread's position on a pi_state after its priority has been
1204 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1206 struct umtx_q *uq, *uq1, *uq2;
1209 mtx_assert(&sched_lock, MA_OWNED);
1216 * Check if the thread needs to be moved on the blocked chain.
1217 * It needs to be moved if either its priority is lower than
1218 * the previous thread or higher than the next thread.
1220 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1221 uq2 = TAILQ_NEXT(uq, uq_lockq);
1222 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1223 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1225 * Remove thread from blocked chain and determine where
1226 * it should be moved to.
1228 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1229 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1230 td1 = uq1->uq_thread;
1231 MPASS(td1->td_proc->p_magic == P_MAGIC);
1232 if (UPRI(td1) > UPRI(td))
1237 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1239 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1245 * Propagate priority when a thread is blocked on POSIX
1249 umtx_propagate_priority(struct thread *td)
1255 mtx_assert(&sched_lock, MA_OWNED);
1258 pi = uq->uq_pi_blocked;
1267 MPASS(td->td_proc != NULL);
1268 MPASS(td->td_proc->p_magic == P_MAGIC);
1270 if (UPRI(td) <= pri)
1273 sched_lend_user_prio(td, pri);
1276 * Pick up the lock that td is blocked on.
1279 pi = uq->uq_pi_blocked;
1280 /* Resort td on the list if needed. */
1281 if (!umtx_pi_adjust_thread(pi, td))
1287 * Unpropagate priority for a PI mutex when a thread blocked on
1288 * it is interrupted by signal or resumed by others.
1291 umtx_unpropagate_priority(struct umtx_pi *pi)
1293 struct umtx_q *uq, *uq_owner;
1294 struct umtx_pi *pi2;
1297 mtx_assert(&sched_lock, MA_OWNED);
1299 while (pi != NULL && pi->pi_owner != NULL) {
1301 uq_owner = pi->pi_owner->td_umtxq;
1303 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1304 uq = TAILQ_FIRST(&pi2->pi_blocked);
1306 if (pri > UPRI(uq->uq_thread))
1307 pri = UPRI(uq->uq_thread);
1311 if (pri > uq_owner->uq_inherited_pri)
1312 pri = uq_owner->uq_inherited_pri;
1313 sched_unlend_user_prio(pi->pi_owner, pri);
1314 pi = uq_owner->uq_pi_blocked;
1319 * Insert a PI mutex into owned list.
1322 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1324 struct umtx_q *uq_owner;
1326 uq_owner = owner->td_umtxq;
1327 mtx_assert(&sched_lock, MA_OWNED);
1328 if (pi->pi_owner != NULL)
1329 panic("pi_ower != NULL");
1330 pi->pi_owner = owner;
1331 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1335 * Claim ownership of a PI mutex.
1338 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1340 struct umtx_q *uq, *uq_owner;
1342 uq_owner = owner->td_umtxq;
1343 mtx_lock_spin(&sched_lock);
1344 if (pi->pi_owner == owner) {
1345 mtx_unlock_spin(&sched_lock);
1349 if (pi->pi_owner != NULL) {
1351 * userland may have already messed the mutex, sigh.
1353 mtx_unlock_spin(&sched_lock);
1356 umtx_pi_setowner(pi, owner);
1357 uq = TAILQ_FIRST(&pi->pi_blocked);
1361 pri = UPRI(uq->uq_thread);
1362 if (pri < UPRI(owner))
1363 sched_lend_user_prio(owner, pri);
1365 mtx_unlock_spin(&sched_lock);
1370 * Adjust a thread's order position in its blocked PI mutex,
1371 * this may result new priority propagating process.
1374 umtx_pi_adjust(struct thread *td, u_char oldpri)
1381 mtx_assert(&sched_lock, MA_OWNED);
1382 MPASS(TD_ON_UPILOCK(td));
1385 * Pick up the lock that td is blocked on.
1387 pi = uq->uq_pi_blocked;
1390 /* Resort the turnstile on the list. */
1391 if (!umtx_pi_adjust_thread(pi, td))
1395 * If our priority was lowered and we are at the head of the
1396 * turnstile, then propagate our new priority up the chain.
1398 if (uq == TAILQ_FIRST(&pi->pi_blocked) && UPRI(td) < oldpri)
1399 umtx_propagate_priority(td);
1403 * Sleep on a PI mutex.
1406 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi,
1407 uint32_t owner, const char *wmesg, int timo)
1409 struct umtxq_chain *uc;
1410 struct thread *td, *td1;
1416 KASSERT(td == curthread, ("inconsistent uq_thread"));
1417 uc = umtxq_getchain(&uq->uq_key);
1418 UMTXQ_LOCKED_ASSERT(uc);
1420 if (pi->pi_owner == NULL) {
1422 * Current, We only support process private PI-mutex,
1423 * non-contended PI-mutexes are locked in userland.
1424 * Process shared PI-mutex should always be initialized
1425 * by kernel and be registered in kernel, locking should
1426 * always be done by kernel to avoid security problems.
1427 * For process private PI-mutex, we can find owner
1428 * thread and boost its priority safely.
1431 td1 = thread_find(curproc, owner);
1432 mtx_lock_spin(&sched_lock);
1433 if (td1 != NULL && pi->pi_owner == NULL) {
1434 uq1 = td1->td_umtxq;
1435 umtx_pi_setowner(pi, td1);
1437 PROC_UNLOCK(curproc);
1439 mtx_lock_spin(&sched_lock);
1442 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1443 pri = UPRI(uq1->uq_thread);
1449 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1451 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1453 uq->uq_pi_blocked = pi;
1454 td->td_flags |= TDF_UPIBLOCKED;
1455 mtx_unlock_spin(&sched_lock);
1456 umtxq_unlock(&uq->uq_key);
1458 mtx_lock_spin(&sched_lock);
1459 umtx_propagate_priority(td);
1460 mtx_unlock_spin(&sched_lock);
1462 umtxq_lock(&uq->uq_key);
1463 if (uq->uq_flags & UQF_UMTXQ) {
1464 error = msleep(uq, &uc->uc_lock, PCATCH, wmesg, timo);
1465 if (error == EWOULDBLOCK)
1467 if (uq->uq_flags & UQF_UMTXQ) {
1468 umtxq_busy(&uq->uq_key);
1470 umtxq_unbusy(&uq->uq_key);
1473 umtxq_unlock(&uq->uq_key);
1475 mtx_lock_spin(&sched_lock);
1476 uq->uq_pi_blocked = NULL;
1477 td->td_flags &= ~TDF_UPIBLOCKED;
1478 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1479 umtx_unpropagate_priority(pi);
1480 mtx_unlock_spin(&sched_lock);
1482 umtxq_lock(&uq->uq_key);
1488 * Add reference count for a PI mutex.
1491 umtx_pi_ref(struct umtx_pi *pi)
1493 struct umtxq_chain *uc;
1495 uc = umtxq_getchain(&pi->pi_key);
1496 UMTXQ_LOCKED_ASSERT(uc);
1501 * Decrease reference count for a PI mutex, if the counter
1502 * is decreased to zero, its memory space is freed.
1505 umtx_pi_unref(struct umtx_pi *pi)
1507 struct umtxq_chain *uc;
1510 uc = umtxq_getchain(&pi->pi_key);
1511 UMTXQ_LOCKED_ASSERT(uc);
1512 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1513 if (--pi->pi_refcount == 0) {
1514 mtx_lock_spin(&sched_lock);
1515 if (pi->pi_owner != NULL) {
1516 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested,
1518 pi->pi_owner = NULL;
1520 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1521 ("blocked queue not empty"));
1522 mtx_unlock_spin(&sched_lock);
1523 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1531 * Find a PI mutex in hash table.
1533 static struct umtx_pi *
1534 umtx_pi_lookup(struct umtx_key *key)
1536 struct umtxq_chain *uc;
1539 uc = umtxq_getchain(key);
1540 UMTXQ_LOCKED_ASSERT(uc);
1542 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1543 if (umtx_key_match(&pi->pi_key, key)) {
1551 * Insert a PI mutex into hash table.
1554 umtx_pi_insert(struct umtx_pi *pi)
1556 struct umtxq_chain *uc;
1558 uc = umtxq_getchain(&pi->pi_key);
1559 UMTXQ_LOCKED_ASSERT(uc);
1560 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1567 _do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags, int timo,
1571 struct umtx_pi *pi, *new_pi;
1572 uint32_t id, owner, old;
1578 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1583 umtxq_lock(&uq->uq_key);
1584 pi = umtx_pi_lookup(&uq->uq_key);
1586 umtxq_unlock(&uq->uq_key);
1587 new_pi = umtx_pi_alloc();
1588 new_pi->pi_key = uq->uq_key;
1589 umtxq_lock(&uq->uq_key);
1590 pi = umtx_pi_lookup(&uq->uq_key);
1592 umtx_pi_free(new_pi);
1594 umtx_pi_insert(new_pi);
1600 umtxq_unlock(&uq->uq_key);
1603 * Care must be exercised when dealing with umtx structure. It
1604 * can fault on any access.
1608 * Try the uncontested case. This should be done in userland.
1610 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id);
1612 /* The acquire succeeded. */
1613 if (owner == UMUTEX_UNOWNED) {
1618 /* The address was invalid. */
1624 /* If no one owns it but it is contested try to acquire it. */
1625 if (owner == UMUTEX_CONTESTED) {
1626 owner = casuword32(&m->m_owner,
1627 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1629 if (owner == UMUTEX_CONTESTED) {
1630 umtxq_lock(&uq->uq_key);
1631 error = umtx_pi_claim(pi, td);
1632 umtxq_unlock(&uq->uq_key);
1636 /* The address was invalid. */
1642 /* If this failed the lock has changed, restart. */
1643 umtxq_lock(&uq->uq_key);
1645 umtxq_unlock(&uq->uq_key);
1650 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1651 (owner & ~UMUTEX_CONTESTED) == id) {
1662 * If we caught a signal, we have retried and now
1668 umtxq_lock(&uq->uq_key);
1669 umtxq_busy(&uq->uq_key);
1670 umtxq_unlock(&uq->uq_key);
1673 * Set the contested bit so that a release in user space
1674 * knows to use the system call for unlock. If this fails
1675 * either some one else has acquired the lock or it has been
1678 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED);
1680 /* The address was invalid. */
1682 umtxq_lock(&uq->uq_key);
1683 umtxq_unbusy(&uq->uq_key);
1684 umtxq_unlock(&uq->uq_key);
1689 umtxq_lock(&uq->uq_key);
1690 umtxq_unbusy(&uq->uq_key);
1692 * We set the contested bit, sleep. Otherwise the lock changed
1693 * and we need to retry or we lost a race to the thread
1694 * unlocking the umtx.
1697 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
1700 umtxq_unlock(&uq->uq_key);
1705 umtxq_lock(&uq->uq_key);
1707 umtxq_unlock(&uq->uq_key);
1710 umtx_key_release(&uq->uq_key);
1715 * Unlock a PI mutex.
1718 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags)
1720 struct umtx_key key;
1721 struct umtx_q *uq_first, *uq_first2, *uq_me;
1722 struct umtx_pi *pi, *pi2;
1723 uint32_t owner, old, id;
1730 * Make sure we own this mtx.
1732 owner = fuword32(&m->m_owner);
1736 if ((owner & ~UMUTEX_CONTESTED) != id)
1739 /* This should be done in userland */
1740 if ((owner & UMUTEX_CONTESTED) == 0) {
1741 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED);
1749 /* We should only ever be in here for contested locks */
1750 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1756 count = umtxq_count_pi(&key, &uq_first);
1757 if (uq_first != NULL) {
1758 pi = uq_first->uq_pi_blocked;
1759 if (pi->pi_owner != curthread) {
1762 /* userland messed the mutex */
1765 uq_me = curthread->td_umtxq;
1766 mtx_lock_spin(&sched_lock);
1767 pi->pi_owner = NULL;
1768 TAILQ_REMOVE(&uq_me->uq_pi_contested, pi, pi_link);
1769 uq_first = TAILQ_FIRST(&pi->pi_blocked);
1771 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
1772 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
1773 if (uq_first2 != NULL) {
1774 if (pri > UPRI(uq_first2->uq_thread))
1775 pri = UPRI(uq_first2->uq_thread);
1778 sched_unlend_user_prio(curthread, pri);
1779 mtx_unlock_spin(&sched_lock);
1784 * When unlocking the umtx, it must be marked as unowned if
1785 * there is zero or one thread only waiting for it.
1786 * Otherwise, it must be marked as contested.
1788 old = casuword32(&m->m_owner, owner,
1789 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1792 if (uq_first != NULL)
1793 umtxq_signal_thread(uq_first);
1796 umtx_key_release(&key);
1808 _do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags, int timo,
1811 struct umtx_q *uq, *uq2;
1815 int error, pri, old_inherited_pri, su;
1819 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
1822 su = (suser(td) == 0);
1824 old_inherited_pri = uq->uq_inherited_pri;
1825 umtxq_lock(&uq->uq_key);
1826 umtxq_busy(&uq->uq_key);
1827 umtxq_unlock(&uq->uq_key);
1829 ceiling = RTP_PRIO_MAX - fuword32(&m->m_ceilings[0]);
1830 if (ceiling > RTP_PRIO_MAX) {
1835 mtx_lock_spin(&sched_lock);
1836 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
1837 mtx_unlock_spin(&sched_lock);
1841 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
1842 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
1843 if (uq->uq_inherited_pri < UPRI(td))
1844 sched_lend_user_prio(td, uq->uq_inherited_pri);
1846 mtx_unlock_spin(&sched_lock);
1848 owner = casuword32(&m->m_owner,
1849 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1851 if (owner == UMUTEX_CONTESTED) {
1856 /* The address was invalid. */
1862 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1863 (owner & ~UMUTEX_CONTESTED) == id) {
1874 * If we caught a signal, we have retried and now
1880 umtxq_lock(&uq->uq_key);
1882 umtxq_unbusy(&uq->uq_key);
1883 error = umtxq_sleep(uq, "umtxpp", timo);
1885 umtxq_unlock(&uq->uq_key);
1887 mtx_lock_spin(&sched_lock);
1888 uq->uq_inherited_pri = old_inherited_pri;
1890 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
1891 uq2 = TAILQ_FIRST(&pi->pi_blocked);
1893 if (pri > UPRI(uq2->uq_thread))
1894 pri = UPRI(uq2->uq_thread);
1897 if (pri > uq->uq_inherited_pri)
1898 pri = uq->uq_inherited_pri;
1899 sched_unlend_user_prio(td, pri);
1900 mtx_unlock_spin(&sched_lock);
1904 mtx_lock_spin(&sched_lock);
1905 uq->uq_inherited_pri = old_inherited_pri;
1907 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
1908 uq2 = TAILQ_FIRST(&pi->pi_blocked);
1910 if (pri > UPRI(uq2->uq_thread))
1911 pri = UPRI(uq2->uq_thread);
1914 if (pri > uq->uq_inherited_pri)
1915 pri = uq->uq_inherited_pri;
1916 sched_unlend_user_prio(td, pri);
1917 mtx_unlock_spin(&sched_lock);
1921 umtxq_lock(&uq->uq_key);
1922 umtxq_unbusy(&uq->uq_key);
1923 umtxq_unlock(&uq->uq_key);
1924 umtx_key_release(&uq->uq_key);
1929 * Unlock a PP mutex.
1932 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags)
1934 struct umtx_key key;
1935 struct umtx_q *uq, *uq2;
1939 int error, pri, new_inherited_pri, su;
1943 su = (suser(td) == 0);
1946 * Make sure we own this mtx.
1948 owner = fuword32(&m->m_owner);
1952 if ((owner & ~UMUTEX_CONTESTED) != id)
1955 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
1960 new_inherited_pri = PRI_MAX;
1962 rceiling = RTP_PRIO_MAX - rceiling;
1963 if (rceiling > RTP_PRIO_MAX)
1965 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
1968 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
1975 * For priority protected mutex, always set unlocked state
1976 * to UMUTEX_CONTESTED, so that userland always enters kernel
1977 * to lock the mutex, it is necessary because thread priority
1978 * has to be adjusted for such mutex.
1980 error = suword32(&m->m_owner, UMUTEX_CONTESTED);
1984 umtxq_signal(&key, 1);
1991 mtx_lock_spin(&sched_lock);
1993 uq->uq_inherited_pri = new_inherited_pri;
1995 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
1996 uq2 = TAILQ_FIRST(&pi->pi_blocked);
1998 if (pri > UPRI(uq2->uq_thread))
1999 pri = UPRI(uq2->uq_thread);
2002 if (pri > uq->uq_inherited_pri)
2003 pri = uq->uq_inherited_pri;
2004 sched_unlend_user_prio(td, pri);
2005 mtx_unlock_spin(&sched_lock);
2007 umtx_key_release(&key);
2012 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2013 uint32_t *old_ceiling)
2016 uint32_t save_ceiling;
2021 flags = fuword32(&m->m_flags);
2022 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2024 if (ceiling > RTP_PRIO_MAX)
2028 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2032 umtxq_lock(&uq->uq_key);
2033 umtxq_busy(&uq->uq_key);
2034 umtxq_unlock(&uq->uq_key);
2036 save_ceiling = fuword32(&m->m_ceilings[0]);
2038 owner = casuword32(&m->m_owner,
2039 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
2041 if (owner == UMUTEX_CONTESTED) {
2042 suword32(&m->m_ceilings[0], ceiling);
2043 suword32(&m->m_owner, UMUTEX_CONTESTED);
2048 /* The address was invalid. */
2054 if ((owner & ~UMUTEX_CONTESTED) == id) {
2055 suword32(&m->m_ceilings[0], ceiling);
2061 * If we caught a signal, we have retried and now
2068 * We set the contested bit, sleep. Otherwise the lock changed
2069 * and we need to retry or we lost a race to the thread
2070 * unlocking the umtx.
2072 umtxq_lock(&uq->uq_key);
2074 umtxq_unbusy(&uq->uq_key);
2075 error = umtxq_sleep(uq, "umtxpp", 0);
2077 umtxq_unlock(&uq->uq_key);
2079 umtxq_lock(&uq->uq_key);
2081 umtxq_signal(&uq->uq_key, INT_MAX);
2082 umtxq_unbusy(&uq->uq_key);
2083 umtxq_unlock(&uq->uq_key);
2084 umtx_key_release(&uq->uq_key);
2085 if (error == 0 && old_ceiling != NULL)
2086 suword32(old_ceiling, save_ceiling);
2091 _do_lock_umutex(struct thread *td, struct umutex *m, int flags, int timo,
2094 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2096 return (_do_lock_normal(td, m, flags, timo, try));
2097 case UMUTEX_PRIO_INHERIT:
2098 return (_do_lock_pi(td, m, flags, timo, try));
2099 case UMUTEX_PRIO_PROTECT:
2100 return (_do_lock_pp(td, m, flags, timo, try));
2106 * Lock a userland POSIX mutex.
2109 do_lock_umutex(struct thread *td, struct umutex *m,
2110 struct timespec *timeout, int try)
2112 struct timespec ts, ts2, ts3;
2117 flags = fuword32(&m->m_flags);
2121 if (timeout == NULL) {
2122 error = _do_lock_umutex(td, m, flags, 0, try);
2123 /* Mutex locking is restarted if it is interrupted. */
2128 timespecadd(&ts, timeout);
2129 TIMESPEC_TO_TIMEVAL(&tv, timeout);
2131 error = _do_lock_umutex(td, m, flags, tvtohz(&tv), try);
2132 if (error != ETIMEDOUT)
2134 getnanouptime(&ts2);
2135 if (timespeccmp(&ts2, &ts, >=)) {
2140 timespecsub(&ts3, &ts2);
2141 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
2143 /* Timed-locking is not restarted. */
2144 if (error == ERESTART)
2151 * Unlock a userland POSIX mutex.
2154 do_unlock_umutex(struct thread *td, struct umutex *m)
2158 flags = fuword32(&m->m_flags);
2162 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2164 return (do_unlock_normal(td, m, flags));
2165 case UMUTEX_PRIO_INHERIT:
2166 return (do_unlock_pi(td, m, flags));
2167 case UMUTEX_PRIO_PROTECT:
2168 return (do_unlock_pp(td, m, flags));
2175 _umtx_lock(struct thread *td, struct _umtx_lock_args *uap)
2176 /* struct umtx *umtx */
2178 return _do_lock_umtx(td, uap->umtx, td->td_tid, 0);
2182 _umtx_unlock(struct thread *td, struct _umtx_unlock_args *uap)
2183 /* struct umtx *umtx */
2185 return do_unlock_umtx(td, uap->umtx, td->td_tid);
2189 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap)
2191 struct timespec *ts, timeout;
2194 /* Allow a null timespec (wait forever). */
2195 if (uap->uaddr2 == NULL)
2198 error = copyin(uap->uaddr2, &timeout, sizeof(timeout));
2201 if (timeout.tv_nsec >= 1000000000 ||
2202 timeout.tv_nsec < 0) {
2207 return (do_lock_umtx(td, uap->obj, uap->val, ts));
2211 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap)
2213 return (do_unlock_umtx(td, uap->obj, uap->val));
2217 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
2219 struct timespec *ts, timeout;
2222 if (uap->uaddr2 == NULL)
2225 error = copyin(uap->uaddr2, &timeout, sizeof(timeout));
2228 if (timeout.tv_nsec >= 1000000000 ||
2229 timeout.tv_nsec < 0)
2233 return do_wait(td, uap->obj, uap->val, ts, 0);
2237 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
2239 return (kern_umtx_wake(td, uap->obj, uap->val));
2243 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
2245 struct timespec *ts, timeout;
2248 /* Allow a null timespec (wait forever). */
2249 if (uap->uaddr2 == NULL)
2252 error = copyin(uap->uaddr2, &timeout,
2256 if (timeout.tv_nsec >= 1000000000 ||
2257 timeout.tv_nsec < 0) {
2262 return do_lock_umutex(td, uap->obj, ts, 0);
2266 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
2268 return do_lock_umutex(td, uap->obj, NULL, 1);
2272 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
2274 return do_unlock_umutex(td, uap->obj);
2278 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
2280 return do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1);
2283 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
2285 static _umtx_op_func op_table[] = {
2286 __umtx_op_lock_umtx, /* UMTX_OP_LOCK */
2287 __umtx_op_unlock_umtx, /* UMTX_OP_UNLOCK */
2288 __umtx_op_wait, /* UMTX_OP_WAIT */
2289 __umtx_op_wake, /* UMTX_OP_WAKE */
2290 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_TRYLOCK */
2291 __umtx_op_lock_umutex, /* UMTX_OP_MUTEX_LOCK */
2292 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
2293 __umtx_op_set_ceiling /* UMTX_OP_SET_CEILING */
2297 _umtx_op(struct thread *td, struct _umtx_op_args *uap)
2299 if (uap->op >= 0 && uap->op < UMTX_OP_MAX)
2300 return (*op_table[uap->op])(td, uap);
2312 copyin_timeout32(void *addr, struct timespec *tsp)
2314 struct timespec32 ts32;
2317 error = copyin(addr, &ts32, sizeof(struct timespec32));
2319 tsp->tv_sec = ts32.tv_sec;
2320 tsp->tv_nsec = ts32.tv_nsec;
2326 __umtx_op_lock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
2328 struct timespec *ts, timeout;
2331 /* Allow a null timespec (wait forever). */
2332 if (uap->uaddr2 == NULL)
2335 error = copyin_timeout32(uap->uaddr2, &timeout);
2338 if (timeout.tv_nsec >= 1000000000 ||
2339 timeout.tv_nsec < 0) {
2344 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
2348 __umtx_op_unlock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
2350 return (do_unlock_umtx32(td, uap->obj, (uint32_t)uap->val));
2354 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
2356 struct timespec *ts, timeout;
2359 if (uap->uaddr2 == NULL)
2362 error = copyin_timeout32(uap->uaddr2, &timeout);
2365 if (timeout.tv_nsec >= 1000000000 ||
2366 timeout.tv_nsec < 0)
2370 return do_wait(td, uap->obj, uap->val, ts, 1);
2374 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
2376 struct timespec *ts, timeout;
2379 /* Allow a null timespec (wait forever). */
2380 if (uap->uaddr2 == NULL)
2383 error = copyin_timeout32(uap->uaddr2, &timeout);
2386 if (timeout.tv_nsec >= 1000000000 ||
2387 timeout.tv_nsec < 0)
2391 return do_lock_umutex(td, uap->obj, ts, 0);
2394 static _umtx_op_func op_table_compat32[] = {
2395 __umtx_op_lock_umtx_compat32, /* UMTX_OP_LOCK */
2396 __umtx_op_unlock_umtx_compat32, /* UMTX_OP_UNLOCK */
2397 __umtx_op_wait_compat32, /* UMTX_OP_WAIT */
2398 __umtx_op_wake, /* UMTX_OP_WAKE */
2399 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_LOCK */
2400 __umtx_op_lock_umutex_compat32, /* UMTX_OP_MUTEX_TRYLOCK */
2401 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
2402 __umtx_op_set_ceiling /* UMTX_OP_SET_CEILING */
2406 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
2408 if (uap->op >= 0 && uap->op < UMTX_OP_MAX)
2409 return (*op_table_compat32[uap->op])(td,
2410 (struct _umtx_op_args *)uap);
2416 umtx_thread_init(struct thread *td)
2418 td->td_umtxq = umtxq_alloc();
2419 td->td_umtxq->uq_thread = td;
2423 umtx_thread_fini(struct thread *td)
2425 umtxq_free(td->td_umtxq);
2429 * It will be called when new thread is created, e.g fork().
2432 umtx_thread_alloc(struct thread *td)
2437 uq->uq_inherited_pri = PRI_MAX;
2439 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
2440 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
2441 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
2442 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
2449 umtx_exec_hook(void *arg __unused, struct proc *p __unused,
2450 struct image_params *imgp __unused)
2452 umtx_thread_cleanup(curthread);
2456 * thread_exit() hook.
2459 umtx_thread_exit(struct thread *td)
2461 umtx_thread_cleanup(td);
2465 * clean up umtx data.
2468 umtx_thread_cleanup(struct thread *td)
2473 if ((uq = td->td_umtxq) == NULL)
2476 mtx_lock_spin(&sched_lock);
2477 uq->uq_inherited_pri = PRI_MAX;
2478 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
2479 pi->pi_owner = NULL;
2480 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
2482 td->td_flags &= ~TDF_UBORROWING;
2483 mtx_unlock_spin(&sched_lock);