2 * Copyright (c) 2004, David Xu <davidxu@freebsd.org>
3 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_compat.h"
32 #include "opt_umtx_profiling.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
42 #include <sys/sched.h>
44 #include <sys/sysctl.h>
45 #include <sys/sysent.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/syscallsubr.h>
49 #include <sys/eventhandler.h>
53 #include <vm/vm_param.h>
55 #include <vm/vm_map.h>
56 #include <vm/vm_object.h>
58 #include <machine/cpu.h>
60 #ifdef COMPAT_FREEBSD32
61 #include <compat/freebsd32/freebsd32_proto.h>
65 #define _UMUTEX_WAIT 2
67 /* Priority inheritance mutex info. */
70 struct thread *pi_owner;
75 /* List entry to link umtx holding by thread */
76 TAILQ_ENTRY(umtx_pi) pi_link;
78 /* List entry in hash */
79 TAILQ_ENTRY(umtx_pi) pi_hashlink;
81 /* List for waiters */
82 TAILQ_HEAD(,umtx_q) pi_blocked;
84 /* Identify a userland lock object */
85 struct umtx_key pi_key;
88 /* A userland synchronous object user. */
90 /* Linked list for the hash. */
91 TAILQ_ENTRY(umtx_q) uq_link;
94 struct umtx_key uq_key;
98 #define UQF_UMTXQ 0x0001
100 /* The thread waits on. */
101 struct thread *uq_thread;
104 * Blocked on PI mutex. read can use chain lock
105 * or umtx_lock, write must have both chain lock and
106 * umtx_lock being hold.
108 struct umtx_pi *uq_pi_blocked;
110 /* On blocked list */
111 TAILQ_ENTRY(umtx_q) uq_lockq;
113 /* Thread contending with us */
114 TAILQ_HEAD(,umtx_pi) uq_pi_contested;
116 /* Inherited priority from PP mutex */
117 u_char uq_inherited_pri;
119 /* Spare queue ready to be reused */
120 struct umtxq_queue *uq_spare_queue;
122 /* The queue we on */
123 struct umtxq_queue *uq_cur_queue;
126 TAILQ_HEAD(umtxq_head, umtx_q);
128 /* Per-key wait-queue */
130 struct umtxq_head head;
132 LIST_ENTRY(umtxq_queue) link;
136 LIST_HEAD(umtxq_list, umtxq_queue);
138 /* Userland lock object's wait-queue chain */
140 /* Lock for this chain. */
143 /* List of sleep queues. */
144 struct umtxq_list uc_queue[2];
145 #define UMTX_SHARED_QUEUE 0
146 #define UMTX_EXCLUSIVE_QUEUE 1
148 LIST_HEAD(, umtxq_queue) uc_spare_queue;
153 /* Chain lock waiters */
156 /* All PI in the list */
157 TAILQ_HEAD(,umtx_pi) uc_pi_list;
159 #ifdef UMTX_PROFILING
165 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
166 #define UMTXQ_BUSY_ASSERT(uc) KASSERT(&(uc)->uc_busy, ("umtx chain is not busy"))
169 * Don't propagate time-sharing priority, there is a security reason,
170 * a user can simply introduce PI-mutex, let thread A lock the mutex,
171 * and let another thread B block on the mutex, because B is
172 * sleeping, its priority will be boosted, this causes A's priority to
173 * be boosted via priority propagating too and will never be lowered even
174 * if it is using 100%CPU, this is unfair to other processes.
177 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
178 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
179 PRI_MAX_TIMESHARE : (td)->td_user_pri)
181 #define GOLDEN_RATIO_PRIME 2654404609U
182 #define UMTX_CHAINS 512
183 #define UMTX_SHIFTS (__WORD_BIT - 9)
185 #define GET_SHARE(flags) \
186 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
188 #define BUSY_SPINS 200
190 static uma_zone_t umtx_pi_zone;
191 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
192 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
193 static int umtx_pi_allocated;
195 SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW, 0, "umtx debug");
196 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
197 &umtx_pi_allocated, 0, "Allocated umtx_pi");
199 #ifdef UMTX_PROFILING
200 static long max_length;
201 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
202 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD, 0, "umtx chain stats");
205 static void umtxq_sysinit(void *);
206 static void umtxq_hash(struct umtx_key *key);
207 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
208 static void umtxq_lock(struct umtx_key *key);
209 static void umtxq_unlock(struct umtx_key *key);
210 static void umtxq_busy(struct umtx_key *key);
211 static void umtxq_unbusy(struct umtx_key *key);
212 static void umtxq_insert_queue(struct umtx_q *uq, int q);
213 static void umtxq_remove_queue(struct umtx_q *uq, int q);
214 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, int timo);
215 static int umtxq_count(struct umtx_key *key);
216 static struct umtx_pi *umtx_pi_alloc(int);
217 static void umtx_pi_free(struct umtx_pi *pi);
218 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags);
219 static void umtx_thread_cleanup(struct thread *td);
220 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
221 struct image_params *imgp __unused);
222 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
224 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
225 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
226 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
228 static struct mtx umtx_lock;
230 #ifdef UMTX_PROFILING
232 umtx_init_profiling(void)
234 struct sysctl_oid *chain_oid;
238 for (i = 0; i < UMTX_CHAINS; ++i) {
239 snprintf(chain_name, sizeof(chain_name), "%d", i);
240 chain_oid = SYSCTL_ADD_NODE(NULL,
241 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
242 chain_name, CTLFLAG_RD, NULL, "umtx hash stats");
243 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
244 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
245 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
246 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
252 umtxq_sysinit(void *arg __unused)
256 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
257 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
258 for (i = 0; i < 2; ++i) {
259 for (j = 0; j < UMTX_CHAINS; ++j) {
260 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
261 MTX_DEF | MTX_DUPOK);
262 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
263 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
264 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
265 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
266 umtxq_chains[i][j].uc_busy = 0;
267 umtxq_chains[i][j].uc_waiters = 0;
268 #ifdef UMTX_PROFILING
269 umtxq_chains[i][j].length = 0;
270 umtxq_chains[i][j].max_length = 0;
274 #ifdef UMTX_PROFILING
275 umtx_init_profiling();
277 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_SPIN);
278 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
279 EVENTHANDLER_PRI_ANY);
287 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
288 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX, M_WAITOK | M_ZERO);
289 TAILQ_INIT(&uq->uq_spare_queue->head);
290 TAILQ_INIT(&uq->uq_pi_contested);
291 uq->uq_inherited_pri = PRI_MAX;
296 umtxq_free(struct umtx_q *uq)
298 MPASS(uq->uq_spare_queue != NULL);
299 free(uq->uq_spare_queue, M_UMTX);
304 umtxq_hash(struct umtx_key *key)
306 unsigned n = (uintptr_t)key->info.both.a + key->info.both.b;
307 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
310 static inline struct umtxq_chain *
311 umtxq_getchain(struct umtx_key *key)
313 if (key->type <= TYPE_SEM)
314 return (&umtxq_chains[1][key->hash]);
315 return (&umtxq_chains[0][key->hash]);
322 umtxq_lock(struct umtx_key *key)
324 struct umtxq_chain *uc;
326 uc = umtxq_getchain(key);
327 mtx_lock(&uc->uc_lock);
334 umtxq_unlock(struct umtx_key *key)
336 struct umtxq_chain *uc;
338 uc = umtxq_getchain(key);
339 mtx_unlock(&uc->uc_lock);
343 * Set chain to busy state when following operation
344 * may be blocked (kernel mutex can not be used).
347 umtxq_busy(struct umtx_key *key)
349 struct umtxq_chain *uc;
351 uc = umtxq_getchain(key);
352 mtx_assert(&uc->uc_lock, MA_OWNED);
356 int count = BUSY_SPINS;
359 while (uc->uc_busy && --count > 0)
365 while (uc->uc_busy) {
367 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
378 umtxq_unbusy(struct umtx_key *key)
380 struct umtxq_chain *uc;
382 uc = umtxq_getchain(key);
383 mtx_assert(&uc->uc_lock, MA_OWNED);
384 KASSERT(uc->uc_busy != 0, ("not busy"));
390 static struct umtxq_queue *
391 umtxq_queue_lookup(struct umtx_key *key, int q)
393 struct umtxq_queue *uh;
394 struct umtxq_chain *uc;
396 uc = umtxq_getchain(key);
397 UMTXQ_LOCKED_ASSERT(uc);
398 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
399 if (umtx_key_match(&uh->key, key))
407 umtxq_insert_queue(struct umtx_q *uq, int q)
409 struct umtxq_queue *uh;
410 struct umtxq_chain *uc;
412 uc = umtxq_getchain(&uq->uq_key);
413 UMTXQ_LOCKED_ASSERT(uc);
414 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
415 uh = umtxq_queue_lookup(&uq->uq_key, q);
417 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
419 uh = uq->uq_spare_queue;
420 uh->key = uq->uq_key;
421 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
423 uq->uq_spare_queue = NULL;
425 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
427 #ifdef UMTX_PROFILING
429 if (uc->length > uc->max_length) {
430 uc->max_length = uc->length;
431 if (uc->max_length > max_length)
432 max_length = uc->max_length;
435 uq->uq_flags |= UQF_UMTXQ;
436 uq->uq_cur_queue = uh;
441 umtxq_remove_queue(struct umtx_q *uq, int q)
443 struct umtxq_chain *uc;
444 struct umtxq_queue *uh;
446 uc = umtxq_getchain(&uq->uq_key);
447 UMTXQ_LOCKED_ASSERT(uc);
448 if (uq->uq_flags & UQF_UMTXQ) {
449 uh = uq->uq_cur_queue;
450 TAILQ_REMOVE(&uh->head, uq, uq_link);
452 #ifdef UMTX_PROFILING
455 uq->uq_flags &= ~UQF_UMTXQ;
456 if (TAILQ_EMPTY(&uh->head)) {
457 KASSERT(uh->length == 0,
458 ("inconsistent umtxq_queue length"));
459 LIST_REMOVE(uh, link);
461 uh = LIST_FIRST(&uc->uc_spare_queue);
462 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
463 LIST_REMOVE(uh, link);
465 uq->uq_spare_queue = uh;
466 uq->uq_cur_queue = NULL;
471 * Check if there are multiple waiters
474 umtxq_count(struct umtx_key *key)
476 struct umtxq_chain *uc;
477 struct umtxq_queue *uh;
479 uc = umtxq_getchain(key);
480 UMTXQ_LOCKED_ASSERT(uc);
481 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
488 * Check if there are multiple PI waiters and returns first
492 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
494 struct umtxq_chain *uc;
495 struct umtxq_queue *uh;
498 uc = umtxq_getchain(key);
499 UMTXQ_LOCKED_ASSERT(uc);
500 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
502 *first = TAILQ_FIRST(&uh->head);
509 * Wake up threads waiting on an userland object.
513 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
515 struct umtxq_chain *uc;
516 struct umtxq_queue *uh;
521 uc = umtxq_getchain(key);
522 UMTXQ_LOCKED_ASSERT(uc);
523 uh = umtxq_queue_lookup(key, q);
525 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
526 umtxq_remove_queue(uq, q);
537 * Wake up specified thread.
540 umtxq_signal_thread(struct umtx_q *uq)
542 struct umtxq_chain *uc;
544 uc = umtxq_getchain(&uq->uq_key);
545 UMTXQ_LOCKED_ASSERT(uc);
551 * Put thread into sleep state, before sleeping, check if
552 * thread was removed from umtx queue.
555 umtxq_sleep(struct umtx_q *uq, const char *wmesg, int timo)
557 struct umtxq_chain *uc;
560 uc = umtxq_getchain(&uq->uq_key);
561 UMTXQ_LOCKED_ASSERT(uc);
562 if (!(uq->uq_flags & UQF_UMTXQ))
564 error = msleep(uq, &uc->uc_lock, PCATCH, wmesg, timo);
565 if (error == EWOULDBLOCK)
571 * Convert userspace address into unique logical address.
574 umtx_key_get(void *addr, int type, int share, struct umtx_key *key)
576 struct thread *td = curthread;
578 vm_map_entry_t entry;
584 if (share == THREAD_SHARE) {
586 key->info.private.vs = td->td_proc->p_vmspace;
587 key->info.private.addr = (uintptr_t)addr;
589 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
590 map = &td->td_proc->p_vmspace->vm_map;
591 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
592 &entry, &key->info.shared.object, &pindex, &prot,
593 &wired) != KERN_SUCCESS) {
597 if ((share == PROCESS_SHARE) ||
598 (share == AUTO_SHARE &&
599 VM_INHERIT_SHARE == entry->inheritance)) {
601 key->info.shared.offset = entry->offset + entry->start -
603 vm_object_reference(key->info.shared.object);
606 key->info.private.vs = td->td_proc->p_vmspace;
607 key->info.private.addr = (uintptr_t)addr;
609 vm_map_lookup_done(map, entry);
620 umtx_key_release(struct umtx_key *key)
623 vm_object_deallocate(key->info.shared.object);
627 * Lock a umtx object.
630 _do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id, int timo)
640 * Care must be exercised when dealing with umtx structure. It
641 * can fault on any access.
645 * Try the uncontested case. This should be done in userland.
647 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
649 /* The acquire succeeded. */
650 if (owner == UMTX_UNOWNED)
653 /* The address was invalid. */
657 /* If no one owns it but it is contested try to acquire it. */
658 if (owner == UMTX_CONTESTED) {
659 owner = casuword(&umtx->u_owner,
660 UMTX_CONTESTED, id | UMTX_CONTESTED);
662 if (owner == UMTX_CONTESTED)
665 /* The address was invalid. */
669 /* If this failed the lock has changed, restart. */
674 * If we caught a signal, we have retried and now
680 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
681 AUTO_SHARE, &uq->uq_key)) != 0)
684 umtxq_lock(&uq->uq_key);
685 umtxq_busy(&uq->uq_key);
687 umtxq_unbusy(&uq->uq_key);
688 umtxq_unlock(&uq->uq_key);
691 * Set the contested bit so that a release in user space
692 * knows to use the system call for unlock. If this fails
693 * either some one else has acquired the lock or it has been
696 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
698 /* The address was invalid. */
700 umtxq_lock(&uq->uq_key);
702 umtxq_unlock(&uq->uq_key);
703 umtx_key_release(&uq->uq_key);
708 * We set the contested bit, sleep. Otherwise the lock changed
709 * and we need to retry or we lost a race to the thread
710 * unlocking the umtx.
712 umtxq_lock(&uq->uq_key);
714 error = umtxq_sleep(uq, "umtx", timo);
716 umtxq_unlock(&uq->uq_key);
717 umtx_key_release(&uq->uq_key);
724 * Lock a umtx object.
727 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
728 struct timespec *timeout)
730 struct timespec ts, ts2, ts3;
734 if (timeout == NULL) {
735 error = _do_lock_umtx(td, umtx, id, 0);
736 /* Mutex locking is restarted if it is interrupted. */
741 timespecadd(&ts, timeout);
742 TIMESPEC_TO_TIMEVAL(&tv, timeout);
744 error = _do_lock_umtx(td, umtx, id, tvtohz(&tv));
745 if (error != ETIMEDOUT)
748 if (timespeccmp(&ts2, &ts, >=)) {
753 timespecsub(&ts3, &ts2);
754 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
756 /* Timed-locking is not restarted. */
757 if (error == ERESTART)
764 * Unlock a umtx object.
767 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
776 * Make sure we own this mtx.
778 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
782 if ((owner & ~UMTX_CONTESTED) != id)
785 /* This should be done in userland */
786 if ((owner & UMTX_CONTESTED) == 0) {
787 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
795 /* We should only ever be in here for contested locks */
796 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
802 count = umtxq_count(&key);
806 * When unlocking the umtx, it must be marked as unowned if
807 * there is zero or one thread only waiting for it.
808 * Otherwise, it must be marked as contested.
810 old = casuword(&umtx->u_owner, owner,
811 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
813 umtxq_signal(&key,1);
816 umtx_key_release(&key);
824 #ifdef COMPAT_FREEBSD32
827 * Lock a umtx object.
830 _do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id, int timo)
840 * Care must be exercised when dealing with umtx structure. It
841 * can fault on any access.
845 * Try the uncontested case. This should be done in userland.
847 owner = casuword32(m, UMUTEX_UNOWNED, id);
849 /* The acquire succeeded. */
850 if (owner == UMUTEX_UNOWNED)
853 /* The address was invalid. */
857 /* If no one owns it but it is contested try to acquire it. */
858 if (owner == UMUTEX_CONTESTED) {
859 owner = casuword32(m,
860 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
861 if (owner == UMUTEX_CONTESTED)
864 /* The address was invalid. */
868 /* If this failed the lock has changed, restart. */
873 * If we caught a signal, we have retried and now
879 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
880 AUTO_SHARE, &uq->uq_key)) != 0)
883 umtxq_lock(&uq->uq_key);
884 umtxq_busy(&uq->uq_key);
886 umtxq_unbusy(&uq->uq_key);
887 umtxq_unlock(&uq->uq_key);
890 * Set the contested bit so that a release in user space
891 * knows to use the system call for unlock. If this fails
892 * either some one else has acquired the lock or it has been
895 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
897 /* The address was invalid. */
899 umtxq_lock(&uq->uq_key);
901 umtxq_unlock(&uq->uq_key);
902 umtx_key_release(&uq->uq_key);
907 * We set the contested bit, sleep. Otherwise the lock changed
908 * and we need to retry or we lost a race to the thread
909 * unlocking the umtx.
911 umtxq_lock(&uq->uq_key);
913 error = umtxq_sleep(uq, "umtx", timo);
915 umtxq_unlock(&uq->uq_key);
916 umtx_key_release(&uq->uq_key);
923 * Lock a umtx object.
926 do_lock_umtx32(struct thread *td, void *m, uint32_t id,
927 struct timespec *timeout)
929 struct timespec ts, ts2, ts3;
933 if (timeout == NULL) {
934 error = _do_lock_umtx32(td, m, id, 0);
935 /* Mutex locking is restarted if it is interrupted. */
940 timespecadd(&ts, timeout);
941 TIMESPEC_TO_TIMEVAL(&tv, timeout);
943 error = _do_lock_umtx32(td, m, id, tvtohz(&tv));
944 if (error != ETIMEDOUT)
947 if (timespeccmp(&ts2, &ts, >=)) {
952 timespecsub(&ts3, &ts2);
953 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
955 /* Timed-locking is not restarted. */
956 if (error == ERESTART)
963 * Unlock a umtx object.
966 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
975 * Make sure we own this mtx.
981 if ((owner & ~UMUTEX_CONTESTED) != id)
984 /* This should be done in userland */
985 if ((owner & UMUTEX_CONTESTED) == 0) {
986 old = casuword32(m, owner, UMUTEX_UNOWNED);
994 /* We should only ever be in here for contested locks */
995 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1001 count = umtxq_count(&key);
1005 * When unlocking the umtx, it must be marked as unowned if
1006 * there is zero or one thread only waiting for it.
1007 * Otherwise, it must be marked as contested.
1009 old = casuword32(m, owner,
1010 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1012 umtxq_signal(&key,1);
1015 umtx_key_release(&key);
1025 * Fetch and compare value, sleep on the address if value is not changed.
1028 do_wait(struct thread *td, void *addr, u_long id,
1029 struct timespec *timeout, int compat32, int is_private)
1032 struct timespec ts, ts2, ts3;
1038 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
1039 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
1042 umtxq_lock(&uq->uq_key);
1044 umtxq_unlock(&uq->uq_key);
1048 tmp = (unsigned int)fuword32(addr);
1050 umtxq_lock(&uq->uq_key);
1052 umtxq_unlock(&uq->uq_key);
1053 } else if (timeout == NULL) {
1054 umtxq_lock(&uq->uq_key);
1055 error = umtxq_sleep(uq, "uwait", 0);
1057 umtxq_unlock(&uq->uq_key);
1060 timespecadd(&ts, timeout);
1061 TIMESPEC_TO_TIMEVAL(&tv, timeout);
1062 umtxq_lock(&uq->uq_key);
1064 error = umtxq_sleep(uq, "uwait", tvtohz(&tv));
1065 if (!(uq->uq_flags & UQF_UMTXQ)) {
1069 if (error != ETIMEDOUT)
1071 umtxq_unlock(&uq->uq_key);
1072 getnanouptime(&ts2);
1073 if (timespeccmp(&ts2, &ts, >=)) {
1075 umtxq_lock(&uq->uq_key);
1079 timespecsub(&ts3, &ts2);
1080 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
1081 umtxq_lock(&uq->uq_key);
1084 umtxq_unlock(&uq->uq_key);
1086 umtx_key_release(&uq->uq_key);
1087 if (error == ERESTART)
1093 * Wake up threads sleeping on the specified address.
1096 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1098 struct umtx_key key;
1101 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1102 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1105 ret = umtxq_signal(&key, n_wake);
1107 umtx_key_release(&key);
1112 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1115 _do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags, int timo,
1119 uint32_t owner, old, id;
1126 * Care must be exercised when dealing with umtx structure. It
1127 * can fault on any access.
1130 owner = fuword32(__DEVOLATILE(void *, &m->m_owner));
1131 if (mode == _UMUTEX_WAIT) {
1132 if (owner == UMUTEX_UNOWNED || owner == UMUTEX_CONTESTED)
1136 * Try the uncontested case. This should be done in userland.
1138 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id);
1140 /* The acquire succeeded. */
1141 if (owner == UMUTEX_UNOWNED)
1144 /* The address was invalid. */
1148 /* If no one owns it but it is contested try to acquire it. */
1149 if (owner == UMUTEX_CONTESTED) {
1150 owner = casuword32(&m->m_owner,
1151 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1153 if (owner == UMUTEX_CONTESTED)
1156 /* The address was invalid. */
1160 /* If this failed the lock has changed, restart. */
1165 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1166 (owner & ~UMUTEX_CONTESTED) == id)
1169 if (mode == _UMUTEX_TRY)
1173 * If we caught a signal, we have retried and now
1179 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1180 GET_SHARE(flags), &uq->uq_key)) != 0)
1183 umtxq_lock(&uq->uq_key);
1184 umtxq_busy(&uq->uq_key);
1186 umtxq_unlock(&uq->uq_key);
1189 * Set the contested bit so that a release in user space
1190 * knows to use the system call for unlock. If this fails
1191 * either some one else has acquired the lock or it has been
1194 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED);
1196 /* The address was invalid. */
1198 umtxq_lock(&uq->uq_key);
1200 umtxq_unbusy(&uq->uq_key);
1201 umtxq_unlock(&uq->uq_key);
1202 umtx_key_release(&uq->uq_key);
1207 * We set the contested bit, sleep. Otherwise the lock changed
1208 * and we need to retry or we lost a race to the thread
1209 * unlocking the umtx.
1211 umtxq_lock(&uq->uq_key);
1212 umtxq_unbusy(&uq->uq_key);
1214 error = umtxq_sleep(uq, "umtxn", timo);
1216 umtxq_unlock(&uq->uq_key);
1217 umtx_key_release(&uq->uq_key);
1224 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1227 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1230 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags)
1232 struct umtx_key key;
1233 uint32_t owner, old, id;
1239 * Make sure we own this mtx.
1241 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1245 if ((owner & ~UMUTEX_CONTESTED) != id)
1248 if ((owner & UMUTEX_CONTESTED) == 0) {
1249 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED);
1257 /* We should only ever be in here for contested locks */
1258 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1264 count = umtxq_count(&key);
1268 * When unlocking the umtx, it must be marked as unowned if
1269 * there is zero or one thread only waiting for it.
1270 * Otherwise, it must be marked as contested.
1272 old = casuword32(&m->m_owner, owner,
1273 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1275 umtxq_signal(&key,1);
1278 umtx_key_release(&key);
1287 * Check if the mutex is available and wake up a waiter,
1288 * only for simple mutex.
1291 do_wake_umutex(struct thread *td, struct umutex *m)
1293 struct umtx_key key;
1299 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1303 if ((owner & ~UMUTEX_CONTESTED) != 0)
1306 flags = fuword32(&m->m_flags);
1308 /* We should only ever be in here for contested locks */
1309 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1315 count = umtxq_count(&key);
1319 owner = casuword32(&m->m_owner, UMUTEX_CONTESTED, UMUTEX_UNOWNED);
1322 if (count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1323 umtxq_signal(&key, 1);
1326 umtx_key_release(&key);
1331 * Check if the mutex has waiters and tries to fix contention bit.
1334 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1336 struct umtx_key key;
1337 uint32_t owner, old;
1342 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
1344 type = TYPE_NORMAL_UMUTEX;
1346 case UMUTEX_PRIO_INHERIT:
1347 type = TYPE_PI_UMUTEX;
1349 case UMUTEX_PRIO_PROTECT:
1350 type = TYPE_PP_UMUTEX;
1355 if ((error = umtx_key_get(m, type, GET_SHARE(flags),
1362 count = umtxq_count(&key);
1365 * Only repair contention bit if there is a waiter, this means the mutex
1366 * is still being referenced by userland code, otherwise don't update
1370 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1371 while ((owner & UMUTEX_CONTESTED) ==0) {
1372 old = casuword32(&m->m_owner, owner,
1373 owner|UMUTEX_CONTESTED);
1378 } else if (count == 1) {
1379 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1380 while ((owner & ~UMUTEX_CONTESTED) != 0 &&
1381 (owner & UMUTEX_CONTESTED) == 0) {
1382 old = casuword32(&m->m_owner, owner,
1383 owner|UMUTEX_CONTESTED);
1392 umtxq_signal(&key, INT_MAX);
1394 else if (count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1395 umtxq_signal(&key, 1);
1398 umtx_key_release(&key);
1402 static inline struct umtx_pi *
1403 umtx_pi_alloc(int flags)
1407 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1408 TAILQ_INIT(&pi->pi_blocked);
1409 atomic_add_int(&umtx_pi_allocated, 1);
1414 umtx_pi_free(struct umtx_pi *pi)
1416 uma_zfree(umtx_pi_zone, pi);
1417 atomic_add_int(&umtx_pi_allocated, -1);
1421 * Adjust the thread's position on a pi_state after its priority has been
1425 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1427 struct umtx_q *uq, *uq1, *uq2;
1430 mtx_assert(&umtx_lock, MA_OWNED);
1437 * Check if the thread needs to be moved on the blocked chain.
1438 * It needs to be moved if either its priority is lower than
1439 * the previous thread or higher than the next thread.
1441 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1442 uq2 = TAILQ_NEXT(uq, uq_lockq);
1443 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1444 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1446 * Remove thread from blocked chain and determine where
1447 * it should be moved to.
1449 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1450 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1451 td1 = uq1->uq_thread;
1452 MPASS(td1->td_proc->p_magic == P_MAGIC);
1453 if (UPRI(td1) > UPRI(td))
1458 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1460 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1466 * Propagate priority when a thread is blocked on POSIX
1470 umtx_propagate_priority(struct thread *td)
1476 mtx_assert(&umtx_lock, MA_OWNED);
1479 pi = uq->uq_pi_blocked;
1485 if (td == NULL || td == curthread)
1488 MPASS(td->td_proc != NULL);
1489 MPASS(td->td_proc->p_magic == P_MAGIC);
1492 if (td->td_lend_user_pri > pri)
1493 sched_lend_user_prio(td, pri);
1501 * Pick up the lock that td is blocked on.
1504 pi = uq->uq_pi_blocked;
1507 /* Resort td on the list if needed. */
1508 umtx_pi_adjust_thread(pi, td);
1513 * Unpropagate priority for a PI mutex when a thread blocked on
1514 * it is interrupted by signal or resumed by others.
1517 umtx_repropagate_priority(struct umtx_pi *pi)
1519 struct umtx_q *uq, *uq_owner;
1520 struct umtx_pi *pi2;
1523 mtx_assert(&umtx_lock, MA_OWNED);
1525 while (pi != NULL && pi->pi_owner != NULL) {
1527 uq_owner = pi->pi_owner->td_umtxq;
1529 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1530 uq = TAILQ_FIRST(&pi2->pi_blocked);
1532 if (pri > UPRI(uq->uq_thread))
1533 pri = UPRI(uq->uq_thread);
1537 if (pri > uq_owner->uq_inherited_pri)
1538 pri = uq_owner->uq_inherited_pri;
1539 thread_lock(pi->pi_owner);
1540 sched_lend_user_prio(pi->pi_owner, pri);
1541 thread_unlock(pi->pi_owner);
1542 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1543 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1548 * Insert a PI mutex into owned list.
1551 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1553 struct umtx_q *uq_owner;
1555 uq_owner = owner->td_umtxq;
1556 mtx_assert(&umtx_lock, MA_OWNED);
1557 if (pi->pi_owner != NULL)
1558 panic("pi_ower != NULL");
1559 pi->pi_owner = owner;
1560 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1564 * Claim ownership of a PI mutex.
1567 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1569 struct umtx_q *uq, *uq_owner;
1571 uq_owner = owner->td_umtxq;
1572 mtx_lock_spin(&umtx_lock);
1573 if (pi->pi_owner == owner) {
1574 mtx_unlock_spin(&umtx_lock);
1578 if (pi->pi_owner != NULL) {
1580 * userland may have already messed the mutex, sigh.
1582 mtx_unlock_spin(&umtx_lock);
1585 umtx_pi_setowner(pi, owner);
1586 uq = TAILQ_FIRST(&pi->pi_blocked);
1590 pri = UPRI(uq->uq_thread);
1592 if (pri < UPRI(owner))
1593 sched_lend_user_prio(owner, pri);
1594 thread_unlock(owner);
1596 mtx_unlock_spin(&umtx_lock);
1601 * Adjust a thread's order position in its blocked PI mutex,
1602 * this may result new priority propagating process.
1605 umtx_pi_adjust(struct thread *td, u_char oldpri)
1611 mtx_lock_spin(&umtx_lock);
1613 * Pick up the lock that td is blocked on.
1615 pi = uq->uq_pi_blocked;
1617 umtx_pi_adjust_thread(pi, td);
1618 umtx_repropagate_priority(pi);
1620 mtx_unlock_spin(&umtx_lock);
1624 * Sleep on a PI mutex.
1627 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi,
1628 uint32_t owner, const char *wmesg, int timo)
1630 struct umtxq_chain *uc;
1631 struct thread *td, *td1;
1637 KASSERT(td == curthread, ("inconsistent uq_thread"));
1638 uc = umtxq_getchain(&uq->uq_key);
1639 UMTXQ_LOCKED_ASSERT(uc);
1640 UMTXQ_BUSY_ASSERT(uc);
1642 mtx_lock_spin(&umtx_lock);
1643 if (pi->pi_owner == NULL) {
1644 mtx_unlock_spin(&umtx_lock);
1645 /* XXX Only look up thread in current process. */
1646 td1 = tdfind(owner, curproc->p_pid);
1647 mtx_lock_spin(&umtx_lock);
1649 if (pi->pi_owner == NULL)
1650 umtx_pi_setowner(pi, td1);
1651 PROC_UNLOCK(td1->td_proc);
1655 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1656 pri = UPRI(uq1->uq_thread);
1662 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1664 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1666 uq->uq_pi_blocked = pi;
1668 td->td_flags |= TDF_UPIBLOCKED;
1670 umtx_propagate_priority(td);
1671 mtx_unlock_spin(&umtx_lock);
1672 umtxq_unbusy(&uq->uq_key);
1674 if (uq->uq_flags & UQF_UMTXQ) {
1675 error = msleep(uq, &uc->uc_lock, PCATCH, wmesg, timo);
1676 if (error == EWOULDBLOCK)
1678 if (uq->uq_flags & UQF_UMTXQ) {
1682 mtx_lock_spin(&umtx_lock);
1683 uq->uq_pi_blocked = NULL;
1685 td->td_flags &= ~TDF_UPIBLOCKED;
1687 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1688 umtx_repropagate_priority(pi);
1689 mtx_unlock_spin(&umtx_lock);
1690 umtxq_unlock(&uq->uq_key);
1696 * Add reference count for a PI mutex.
1699 umtx_pi_ref(struct umtx_pi *pi)
1701 struct umtxq_chain *uc;
1703 uc = umtxq_getchain(&pi->pi_key);
1704 UMTXQ_LOCKED_ASSERT(uc);
1709 * Decrease reference count for a PI mutex, if the counter
1710 * is decreased to zero, its memory space is freed.
1713 umtx_pi_unref(struct umtx_pi *pi)
1715 struct umtxq_chain *uc;
1717 uc = umtxq_getchain(&pi->pi_key);
1718 UMTXQ_LOCKED_ASSERT(uc);
1719 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1720 if (--pi->pi_refcount == 0) {
1721 mtx_lock_spin(&umtx_lock);
1722 if (pi->pi_owner != NULL) {
1723 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested,
1725 pi->pi_owner = NULL;
1727 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1728 ("blocked queue not empty"));
1729 mtx_unlock_spin(&umtx_lock);
1730 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1736 * Find a PI mutex in hash table.
1738 static struct umtx_pi *
1739 umtx_pi_lookup(struct umtx_key *key)
1741 struct umtxq_chain *uc;
1744 uc = umtxq_getchain(key);
1745 UMTXQ_LOCKED_ASSERT(uc);
1747 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1748 if (umtx_key_match(&pi->pi_key, key)) {
1756 * Insert a PI mutex into hash table.
1759 umtx_pi_insert(struct umtx_pi *pi)
1761 struct umtxq_chain *uc;
1763 uc = umtxq_getchain(&pi->pi_key);
1764 UMTXQ_LOCKED_ASSERT(uc);
1765 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1772 _do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags, int timo,
1776 struct umtx_pi *pi, *new_pi;
1777 uint32_t id, owner, old;
1783 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1786 umtxq_lock(&uq->uq_key);
1787 pi = umtx_pi_lookup(&uq->uq_key);
1789 new_pi = umtx_pi_alloc(M_NOWAIT);
1790 if (new_pi == NULL) {
1791 umtxq_unlock(&uq->uq_key);
1792 new_pi = umtx_pi_alloc(M_WAITOK);
1793 umtxq_lock(&uq->uq_key);
1794 pi = umtx_pi_lookup(&uq->uq_key);
1796 umtx_pi_free(new_pi);
1800 if (new_pi != NULL) {
1801 new_pi->pi_key = uq->uq_key;
1802 umtx_pi_insert(new_pi);
1807 umtxq_unlock(&uq->uq_key);
1810 * Care must be exercised when dealing with umtx structure. It
1811 * can fault on any access.
1815 * Try the uncontested case. This should be done in userland.
1817 owner = casuword32(&m->m_owner, UMUTEX_UNOWNED, id);
1819 /* The acquire succeeded. */
1820 if (owner == UMUTEX_UNOWNED) {
1825 /* The address was invalid. */
1831 /* If no one owns it but it is contested try to acquire it. */
1832 if (owner == UMUTEX_CONTESTED) {
1833 owner = casuword32(&m->m_owner,
1834 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1836 if (owner == UMUTEX_CONTESTED) {
1837 umtxq_lock(&uq->uq_key);
1838 umtxq_busy(&uq->uq_key);
1839 error = umtx_pi_claim(pi, td);
1840 umtxq_unbusy(&uq->uq_key);
1841 umtxq_unlock(&uq->uq_key);
1845 /* The address was invalid. */
1851 /* If this failed the lock has changed, restart. */
1855 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
1856 (owner & ~UMUTEX_CONTESTED) == id) {
1867 * If we caught a signal, we have retried and now
1873 umtxq_lock(&uq->uq_key);
1874 umtxq_busy(&uq->uq_key);
1875 umtxq_unlock(&uq->uq_key);
1878 * Set the contested bit so that a release in user space
1879 * knows to use the system call for unlock. If this fails
1880 * either some one else has acquired the lock or it has been
1883 old = casuword32(&m->m_owner, owner, owner | UMUTEX_CONTESTED);
1885 /* The address was invalid. */
1887 umtxq_lock(&uq->uq_key);
1888 umtxq_unbusy(&uq->uq_key);
1889 umtxq_unlock(&uq->uq_key);
1894 umtxq_lock(&uq->uq_key);
1896 * We set the contested bit, sleep. Otherwise the lock changed
1897 * and we need to retry or we lost a race to the thread
1898 * unlocking the umtx.
1901 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
1904 umtxq_unbusy(&uq->uq_key);
1905 umtxq_unlock(&uq->uq_key);
1909 umtxq_lock(&uq->uq_key);
1911 umtxq_unlock(&uq->uq_key);
1913 umtx_key_release(&uq->uq_key);
1918 * Unlock a PI mutex.
1921 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags)
1923 struct umtx_key key;
1924 struct umtx_q *uq_first, *uq_first2, *uq_me;
1925 struct umtx_pi *pi, *pi2;
1926 uint32_t owner, old, id;
1933 * Make sure we own this mtx.
1935 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
1939 if ((owner & ~UMUTEX_CONTESTED) != id)
1942 /* This should be done in userland */
1943 if ((owner & UMUTEX_CONTESTED) == 0) {
1944 old = casuword32(&m->m_owner, owner, UMUTEX_UNOWNED);
1952 /* We should only ever be in here for contested locks */
1953 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1959 count = umtxq_count_pi(&key, &uq_first);
1960 if (uq_first != NULL) {
1961 mtx_lock_spin(&umtx_lock);
1962 pi = uq_first->uq_pi_blocked;
1963 KASSERT(pi != NULL, ("pi == NULL?"));
1964 if (pi->pi_owner != curthread) {
1965 mtx_unlock_spin(&umtx_lock);
1968 umtx_key_release(&key);
1969 /* userland messed the mutex */
1972 uq_me = curthread->td_umtxq;
1973 pi->pi_owner = NULL;
1974 TAILQ_REMOVE(&uq_me->uq_pi_contested, pi, pi_link);
1975 /* get highest priority thread which is still sleeping. */
1976 uq_first = TAILQ_FIRST(&pi->pi_blocked);
1977 while (uq_first != NULL &&
1978 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
1979 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
1982 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
1983 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
1984 if (uq_first2 != NULL) {
1985 if (pri > UPRI(uq_first2->uq_thread))
1986 pri = UPRI(uq_first2->uq_thread);
1989 thread_lock(curthread);
1990 sched_lend_user_prio(curthread, pri);
1991 thread_unlock(curthread);
1992 mtx_unlock_spin(&umtx_lock);
1994 umtxq_signal_thread(uq_first);
1999 * When unlocking the umtx, it must be marked as unowned if
2000 * there is zero or one thread only waiting for it.
2001 * Otherwise, it must be marked as contested.
2003 old = casuword32(&m->m_owner, owner,
2004 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
2009 umtx_key_release(&key);
2021 _do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags, int timo,
2024 struct umtx_q *uq, *uq2;
2028 int error, pri, old_inherited_pri, su;
2032 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2035 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2037 old_inherited_pri = uq->uq_inherited_pri;
2038 umtxq_lock(&uq->uq_key);
2039 umtxq_busy(&uq->uq_key);
2040 umtxq_unlock(&uq->uq_key);
2042 ceiling = RTP_PRIO_MAX - fuword32(&m->m_ceilings[0]);
2043 if (ceiling > RTP_PRIO_MAX) {
2048 mtx_lock_spin(&umtx_lock);
2049 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2050 mtx_unlock_spin(&umtx_lock);
2054 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2055 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2057 if (uq->uq_inherited_pri < UPRI(td))
2058 sched_lend_user_prio(td, uq->uq_inherited_pri);
2061 mtx_unlock_spin(&umtx_lock);
2063 owner = casuword32(&m->m_owner,
2064 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
2066 if (owner == UMUTEX_CONTESTED) {
2071 /* The address was invalid. */
2077 if ((flags & UMUTEX_ERROR_CHECK) != 0 &&
2078 (owner & ~UMUTEX_CONTESTED) == id) {
2089 * If we caught a signal, we have retried and now
2095 umtxq_lock(&uq->uq_key);
2097 umtxq_unbusy(&uq->uq_key);
2098 error = umtxq_sleep(uq, "umtxpp", timo);
2100 umtxq_unlock(&uq->uq_key);
2102 mtx_lock_spin(&umtx_lock);
2103 uq->uq_inherited_pri = old_inherited_pri;
2105 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2106 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2108 if (pri > UPRI(uq2->uq_thread))
2109 pri = UPRI(uq2->uq_thread);
2112 if (pri > uq->uq_inherited_pri)
2113 pri = uq->uq_inherited_pri;
2115 sched_lend_user_prio(td, pri);
2117 mtx_unlock_spin(&umtx_lock);
2121 mtx_lock_spin(&umtx_lock);
2122 uq->uq_inherited_pri = old_inherited_pri;
2124 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2125 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2127 if (pri > UPRI(uq2->uq_thread))
2128 pri = UPRI(uq2->uq_thread);
2131 if (pri > uq->uq_inherited_pri)
2132 pri = uq->uq_inherited_pri;
2134 sched_lend_user_prio(td, pri);
2136 mtx_unlock_spin(&umtx_lock);
2140 umtxq_lock(&uq->uq_key);
2141 umtxq_unbusy(&uq->uq_key);
2142 umtxq_unlock(&uq->uq_key);
2143 umtx_key_release(&uq->uq_key);
2148 * Unlock a PP mutex.
2151 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags)
2153 struct umtx_key key;
2154 struct umtx_q *uq, *uq2;
2158 int error, pri, new_inherited_pri, su;
2162 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2165 * Make sure we own this mtx.
2167 owner = fuword32(__DEVOLATILE(uint32_t *, &m->m_owner));
2171 if ((owner & ~UMUTEX_CONTESTED) != id)
2174 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2179 new_inherited_pri = PRI_MAX;
2181 rceiling = RTP_PRIO_MAX - rceiling;
2182 if (rceiling > RTP_PRIO_MAX)
2184 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2187 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2194 * For priority protected mutex, always set unlocked state
2195 * to UMUTEX_CONTESTED, so that userland always enters kernel
2196 * to lock the mutex, it is necessary because thread priority
2197 * has to be adjusted for such mutex.
2199 error = suword32(__DEVOLATILE(uint32_t *, &m->m_owner),
2204 umtxq_signal(&key, 1);
2211 mtx_lock_spin(&umtx_lock);
2213 uq->uq_inherited_pri = new_inherited_pri;
2215 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2216 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2218 if (pri > UPRI(uq2->uq_thread))
2219 pri = UPRI(uq2->uq_thread);
2222 if (pri > uq->uq_inherited_pri)
2223 pri = uq->uq_inherited_pri;
2225 sched_lend_user_prio(td, pri);
2227 mtx_unlock_spin(&umtx_lock);
2229 umtx_key_release(&key);
2234 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2235 uint32_t *old_ceiling)
2238 uint32_t save_ceiling;
2243 flags = fuword32(&m->m_flags);
2244 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2246 if (ceiling > RTP_PRIO_MAX)
2250 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2254 umtxq_lock(&uq->uq_key);
2255 umtxq_busy(&uq->uq_key);
2256 umtxq_unlock(&uq->uq_key);
2258 save_ceiling = fuword32(&m->m_ceilings[0]);
2260 owner = casuword32(&m->m_owner,
2261 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
2263 if (owner == UMUTEX_CONTESTED) {
2264 suword32(&m->m_ceilings[0], ceiling);
2265 suword32(__DEVOLATILE(uint32_t *, &m->m_owner),
2271 /* The address was invalid. */
2277 if ((owner & ~UMUTEX_CONTESTED) == id) {
2278 suword32(&m->m_ceilings[0], ceiling);
2284 * If we caught a signal, we have retried and now
2291 * We set the contested bit, sleep. Otherwise the lock changed
2292 * and we need to retry or we lost a race to the thread
2293 * unlocking the umtx.
2295 umtxq_lock(&uq->uq_key);
2297 umtxq_unbusy(&uq->uq_key);
2298 error = umtxq_sleep(uq, "umtxpp", 0);
2300 umtxq_unlock(&uq->uq_key);
2302 umtxq_lock(&uq->uq_key);
2304 umtxq_signal(&uq->uq_key, INT_MAX);
2305 umtxq_unbusy(&uq->uq_key);
2306 umtxq_unlock(&uq->uq_key);
2307 umtx_key_release(&uq->uq_key);
2308 if (error == 0 && old_ceiling != NULL)
2309 suword32(old_ceiling, save_ceiling);
2314 _do_lock_umutex(struct thread *td, struct umutex *m, int flags, int timo,
2317 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2319 return (_do_lock_normal(td, m, flags, timo, mode));
2320 case UMUTEX_PRIO_INHERIT:
2321 return (_do_lock_pi(td, m, flags, timo, mode));
2322 case UMUTEX_PRIO_PROTECT:
2323 return (_do_lock_pp(td, m, flags, timo, mode));
2329 * Lock a userland POSIX mutex.
2332 do_lock_umutex(struct thread *td, struct umutex *m,
2333 struct timespec *timeout, int mode)
2335 struct timespec ts, ts2, ts3;
2340 flags = fuword32(&m->m_flags);
2344 if (timeout == NULL) {
2345 error = _do_lock_umutex(td, m, flags, 0, mode);
2346 /* Mutex locking is restarted if it is interrupted. */
2347 if (error == EINTR && mode != _UMUTEX_WAIT)
2351 timespecadd(&ts, timeout);
2352 TIMESPEC_TO_TIMEVAL(&tv, timeout);
2354 error = _do_lock_umutex(td, m, flags, tvtohz(&tv), mode);
2355 if (error != ETIMEDOUT)
2357 getnanouptime(&ts2);
2358 if (timespeccmp(&ts2, &ts, >=)) {
2363 timespecsub(&ts3, &ts2);
2364 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
2366 /* Timed-locking is not restarted. */
2367 if (error == ERESTART)
2374 * Unlock a userland POSIX mutex.
2377 do_unlock_umutex(struct thread *td, struct umutex *m)
2381 flags = fuword32(&m->m_flags);
2385 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2387 return (do_unlock_normal(td, m, flags));
2388 case UMUTEX_PRIO_INHERIT:
2389 return (do_unlock_pi(td, m, flags));
2390 case UMUTEX_PRIO_PROTECT:
2391 return (do_unlock_pp(td, m, flags));
2398 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2399 struct timespec *timeout, u_long wflags)
2403 struct timespec cts, ets, tts;
2409 flags = fuword32(&cv->c_flags);
2410 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2414 if ((wflags & CVWAIT_CLOCKID) != 0) {
2415 clockid = fuword32(&cv->c_clockid);
2416 if (clockid < CLOCK_REALTIME ||
2417 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2418 /* hmm, only HW clock id will work. */
2422 clockid = CLOCK_REALTIME;
2425 umtxq_lock(&uq->uq_key);
2426 umtxq_busy(&uq->uq_key);
2428 umtxq_unlock(&uq->uq_key);
2431 * Set c_has_waiters to 1 before releasing user mutex, also
2432 * don't modify cache line when unnecessary.
2434 if (fuword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters)) == 0)
2435 suword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters), 1);
2437 umtxq_lock(&uq->uq_key);
2438 umtxq_unbusy(&uq->uq_key);
2439 umtxq_unlock(&uq->uq_key);
2441 error = do_unlock_umutex(td, m);
2443 umtxq_lock(&uq->uq_key);
2445 if (timeout == NULL) {
2446 error = umtxq_sleep(uq, "ucond", 0);
2448 if ((wflags & CVWAIT_ABSTIME) == 0) {
2449 kern_clock_gettime(td, clockid, &ets);
2450 timespecadd(&ets, timeout);
2452 } else { /* absolute time */
2455 kern_clock_gettime(td, clockid, &cts);
2456 timespecsub(&tts, &cts);
2458 TIMESPEC_TO_TIMEVAL(&tv, &tts);
2460 error = umtxq_sleep(uq, "ucond", tvtohz(&tv));
2461 if (error != ETIMEDOUT)
2463 kern_clock_gettime(td, clockid, &cts);
2464 if (timespeccmp(&cts, &ets, >=)) {
2469 timespecsub(&tts, &cts);
2470 TIMESPEC_TO_TIMEVAL(&tv, &tts);
2475 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2479 * This must be timeout,interrupted by signal or
2480 * surprious wakeup, clear c_has_waiter flag when
2483 umtxq_busy(&uq->uq_key);
2484 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2485 int oldlen = uq->uq_cur_queue->length;
2488 umtxq_unlock(&uq->uq_key);
2490 __DEVOLATILE(uint32_t *,
2491 &cv->c_has_waiters), 0);
2492 umtxq_lock(&uq->uq_key);
2495 umtxq_unbusy(&uq->uq_key);
2496 if (error == ERESTART)
2500 umtxq_unlock(&uq->uq_key);
2501 umtx_key_release(&uq->uq_key);
2506 * Signal a userland condition variable.
2509 do_cv_signal(struct thread *td, struct ucond *cv)
2511 struct umtx_key key;
2512 int error, cnt, nwake;
2515 flags = fuword32(&cv->c_flags);
2516 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2520 cnt = umtxq_count(&key);
2521 nwake = umtxq_signal(&key, 1);
2525 __DEVOLATILE(uint32_t *, &cv->c_has_waiters), 0);
2530 umtx_key_release(&key);
2535 do_cv_broadcast(struct thread *td, struct ucond *cv)
2537 struct umtx_key key;
2541 flags = fuword32(&cv->c_flags);
2542 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2547 umtxq_signal(&key, INT_MAX);
2550 error = suword32(__DEVOLATILE(uint32_t *, &cv->c_has_waiters), 0);
2556 umtx_key_release(&key);
2561 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, int timo)
2564 uint32_t flags, wrflags;
2565 int32_t state, oldstate;
2566 int32_t blocked_readers;
2570 flags = fuword32(&rwlock->rw_flags);
2571 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2575 wrflags = URWLOCK_WRITE_OWNER;
2576 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2577 wrflags |= URWLOCK_WRITE_WAITERS;
2580 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2581 /* try to lock it */
2582 while (!(state & wrflags)) {
2583 if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) {
2584 umtx_key_release(&uq->uq_key);
2587 oldstate = casuword32(&rwlock->rw_state, state, state + 1);
2588 if (oldstate == state) {
2589 umtx_key_release(&uq->uq_key);
2598 /* grab monitor lock */
2599 umtxq_lock(&uq->uq_key);
2600 umtxq_busy(&uq->uq_key);
2601 umtxq_unlock(&uq->uq_key);
2604 * re-read the state, in case it changed between the try-lock above
2605 * and the check below
2607 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2609 /* set read contention bit */
2610 while ((state & wrflags) && !(state & URWLOCK_READ_WAITERS)) {
2611 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_READ_WAITERS);
2612 if (oldstate == state)
2617 /* state is changed while setting flags, restart */
2618 if (!(state & wrflags)) {
2619 umtxq_lock(&uq->uq_key);
2620 umtxq_unbusy(&uq->uq_key);
2621 umtxq_unlock(&uq->uq_key);
2626 /* contention bit is set, before sleeping, increase read waiter count */
2627 blocked_readers = fuword32(&rwlock->rw_blocked_readers);
2628 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2630 while (state & wrflags) {
2631 umtxq_lock(&uq->uq_key);
2633 umtxq_unbusy(&uq->uq_key);
2635 error = umtxq_sleep(uq, "urdlck", timo);
2637 umtxq_busy(&uq->uq_key);
2639 umtxq_unlock(&uq->uq_key);
2642 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2645 /* decrease read waiter count, and may clear read contention bit */
2646 blocked_readers = fuword32(&rwlock->rw_blocked_readers);
2647 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2648 if (blocked_readers == 1) {
2649 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2651 oldstate = casuword32(&rwlock->rw_state, state,
2652 state & ~URWLOCK_READ_WAITERS);
2653 if (oldstate == state)
2659 umtxq_lock(&uq->uq_key);
2660 umtxq_unbusy(&uq->uq_key);
2661 umtxq_unlock(&uq->uq_key);
2663 umtx_key_release(&uq->uq_key);
2668 do_rw_rdlock2(struct thread *td, void *obj, long val, struct timespec *timeout)
2670 struct timespec ts, ts2, ts3;
2675 timespecadd(&ts, timeout);
2676 TIMESPEC_TO_TIMEVAL(&tv, timeout);
2678 error = do_rw_rdlock(td, obj, val, tvtohz(&tv));
2679 if (error != ETIMEDOUT)
2681 getnanouptime(&ts2);
2682 if (timespeccmp(&ts2, &ts, >=)) {
2687 timespecsub(&ts3, &ts2);
2688 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
2690 if (error == ERESTART)
2696 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, int timo)
2700 int32_t state, oldstate;
2701 int32_t blocked_writers;
2702 int32_t blocked_readers;
2706 flags = fuword32(&rwlock->rw_flags);
2707 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2711 blocked_readers = 0;
2713 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2714 while (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2715 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_WRITE_OWNER);
2716 if (oldstate == state) {
2717 umtx_key_release(&uq->uq_key);
2724 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) &&
2725 blocked_readers != 0) {
2726 umtxq_lock(&uq->uq_key);
2727 umtxq_busy(&uq->uq_key);
2728 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE);
2729 umtxq_unbusy(&uq->uq_key);
2730 umtxq_unlock(&uq->uq_key);
2736 /* grab monitor lock */
2737 umtxq_lock(&uq->uq_key);
2738 umtxq_busy(&uq->uq_key);
2739 umtxq_unlock(&uq->uq_key);
2742 * re-read the state, in case it changed between the try-lock above
2743 * and the check below
2745 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2747 while (((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) &&
2748 (state & URWLOCK_WRITE_WAITERS) == 0) {
2749 oldstate = casuword32(&rwlock->rw_state, state, state | URWLOCK_WRITE_WAITERS);
2750 if (oldstate == state)
2755 if (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2756 umtxq_lock(&uq->uq_key);
2757 umtxq_unbusy(&uq->uq_key);
2758 umtxq_unlock(&uq->uq_key);
2762 blocked_writers = fuword32(&rwlock->rw_blocked_writers);
2763 suword32(&rwlock->rw_blocked_writers, blocked_writers+1);
2765 while ((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) {
2766 umtxq_lock(&uq->uq_key);
2767 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2768 umtxq_unbusy(&uq->uq_key);
2770 error = umtxq_sleep(uq, "uwrlck", timo);
2772 umtxq_busy(&uq->uq_key);
2773 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2774 umtxq_unlock(&uq->uq_key);
2777 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2780 blocked_writers = fuword32(&rwlock->rw_blocked_writers);
2781 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
2782 if (blocked_writers == 1) {
2783 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2785 oldstate = casuword32(&rwlock->rw_state, state,
2786 state & ~URWLOCK_WRITE_WAITERS);
2787 if (oldstate == state)
2791 blocked_readers = fuword32(&rwlock->rw_blocked_readers);
2793 blocked_readers = 0;
2795 umtxq_lock(&uq->uq_key);
2796 umtxq_unbusy(&uq->uq_key);
2797 umtxq_unlock(&uq->uq_key);
2800 umtx_key_release(&uq->uq_key);
2805 do_rw_wrlock2(struct thread *td, void *obj, struct timespec *timeout)
2807 struct timespec ts, ts2, ts3;
2812 timespecadd(&ts, timeout);
2813 TIMESPEC_TO_TIMEVAL(&tv, timeout);
2815 error = do_rw_wrlock(td, obj, tvtohz(&tv));
2816 if (error != ETIMEDOUT)
2818 getnanouptime(&ts2);
2819 if (timespeccmp(&ts2, &ts, >=)) {
2824 timespecsub(&ts3, &ts2);
2825 TIMESPEC_TO_TIMEVAL(&tv, &ts3);
2827 if (error == ERESTART)
2833 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
2837 int32_t state, oldstate;
2838 int error, q, count;
2841 flags = fuword32(&rwlock->rw_flags);
2842 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2846 state = fuword32(__DEVOLATILE(int32_t *, &rwlock->rw_state));
2847 if (state & URWLOCK_WRITE_OWNER) {
2849 oldstate = casuword32(&rwlock->rw_state, state,
2850 state & ~URWLOCK_WRITE_OWNER);
2851 if (oldstate != state) {
2853 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
2860 } else if (URWLOCK_READER_COUNT(state) != 0) {
2862 oldstate = casuword32(&rwlock->rw_state, state,
2864 if (oldstate != state) {
2866 if (URWLOCK_READER_COUNT(oldstate) == 0) {
2881 if (!(flags & URWLOCK_PREFER_READER)) {
2882 if (state & URWLOCK_WRITE_WAITERS) {
2884 q = UMTX_EXCLUSIVE_QUEUE;
2885 } else if (state & URWLOCK_READ_WAITERS) {
2887 q = UMTX_SHARED_QUEUE;
2890 if (state & URWLOCK_READ_WAITERS) {
2892 q = UMTX_SHARED_QUEUE;
2893 } else if (state & URWLOCK_WRITE_WAITERS) {
2895 q = UMTX_EXCLUSIVE_QUEUE;
2900 umtxq_lock(&uq->uq_key);
2901 umtxq_busy(&uq->uq_key);
2902 umtxq_signal_queue(&uq->uq_key, count, q);
2903 umtxq_unbusy(&uq->uq_key);
2904 umtxq_unlock(&uq->uq_key);
2907 umtx_key_release(&uq->uq_key);
2912 do_sem_wait(struct thread *td, struct _usem *sem, struct timespec *timeout)
2916 struct timespec cts, ets, tts;
2917 uint32_t flags, count;
2921 flags = fuword32(&sem->_flags);
2922 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
2925 umtxq_lock(&uq->uq_key);
2926 umtxq_busy(&uq->uq_key);
2928 umtxq_unlock(&uq->uq_key);
2930 if (fuword32(__DEVOLATILE(uint32_t *, &sem->_has_waiters)) == 0)
2931 casuword32(__DEVOLATILE(uint32_t *, &sem->_has_waiters), 0, 1);
2933 count = fuword32(__DEVOLATILE(uint32_t *, &sem->_count));
2935 umtxq_lock(&uq->uq_key);
2936 umtxq_unbusy(&uq->uq_key);
2938 umtxq_unlock(&uq->uq_key);
2939 umtx_key_release(&uq->uq_key);
2943 umtxq_lock(&uq->uq_key);
2944 umtxq_unbusy(&uq->uq_key);
2945 umtxq_unlock(&uq->uq_key);
2947 umtxq_lock(&uq->uq_key);
2948 if (timeout == NULL) {
2949 error = umtxq_sleep(uq, "usem", 0);
2951 getnanouptime(&ets);
2952 timespecadd(&ets, timeout);
2953 TIMESPEC_TO_TIMEVAL(&tv, timeout);
2955 error = umtxq_sleep(uq, "usem", tvtohz(&tv));
2956 if (error != ETIMEDOUT)
2958 getnanouptime(&cts);
2959 if (timespeccmp(&cts, &ets, >=)) {
2964 timespecsub(&tts, &cts);
2965 TIMESPEC_TO_TIMEVAL(&tv, &tts);
2969 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2973 if (error == ERESTART)
2976 umtxq_unlock(&uq->uq_key);
2977 umtx_key_release(&uq->uq_key);
2982 * Signal a userland condition variable.
2985 do_sem_wake(struct thread *td, struct _usem *sem)
2987 struct umtx_key key;
2988 int error, cnt, nwake;
2991 flags = fuword32(&sem->_flags);
2992 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
2996 cnt = umtxq_count(&key);
2997 nwake = umtxq_signal(&key, 1);
3001 __DEVOLATILE(uint32_t *, &sem->_has_waiters), 0);
3006 umtx_key_release(&key);
3011 sys__umtx_lock(struct thread *td, struct _umtx_lock_args *uap)
3012 /* struct umtx *umtx */
3014 return _do_lock_umtx(td, uap->umtx, td->td_tid, 0);
3018 sys__umtx_unlock(struct thread *td, struct _umtx_unlock_args *uap)
3019 /* struct umtx *umtx */
3021 return do_unlock_umtx(td, uap->umtx, td->td_tid);
3025 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3029 error = copyin(addr, tsp, sizeof(struct timespec));
3031 if (tsp->tv_sec < 0 ||
3032 tsp->tv_nsec >= 1000000000 ||
3040 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap)
3042 struct timespec *ts, timeout;
3045 /* Allow a null timespec (wait forever). */
3046 if (uap->uaddr2 == NULL)
3049 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3054 return (do_lock_umtx(td, uap->obj, uap->val, ts));
3058 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap)
3060 return (do_unlock_umtx(td, uap->obj, uap->val));
3064 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3066 struct timespec *ts, timeout;
3069 if (uap->uaddr2 == NULL)
3072 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3077 return do_wait(td, uap->obj, uap->val, ts, 0, 0);
3081 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3083 struct timespec *ts, timeout;
3086 if (uap->uaddr2 == NULL)
3089 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3094 return do_wait(td, uap->obj, uap->val, ts, 1, 0);
3098 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3100 struct timespec *ts, timeout;
3103 if (uap->uaddr2 == NULL)
3106 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3111 return do_wait(td, uap->obj, uap->val, ts, 1, 1);
3115 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3117 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3120 #define BATCH_SIZE 128
3122 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3124 int count = uap->val;
3125 void *uaddrs[BATCH_SIZE];
3126 char **upp = (char **)uap->obj;
3133 if (tocopy > BATCH_SIZE)
3134 tocopy = BATCH_SIZE;
3135 error = copyin(upp+pos, uaddrs, tocopy * sizeof(char *));
3138 for (i = 0; i < tocopy; ++i)
3139 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3147 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3149 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3153 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3155 struct timespec *ts, timeout;
3158 /* Allow a null timespec (wait forever). */
3159 if (uap->uaddr2 == NULL)
3162 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3167 return do_lock_umutex(td, uap->obj, ts, 0);
3171 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3173 return do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY);
3177 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3179 struct timespec *ts, timeout;
3182 /* Allow a null timespec (wait forever). */
3183 if (uap->uaddr2 == NULL)
3186 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3191 return do_lock_umutex(td, uap->obj, ts, _UMUTEX_WAIT);
3195 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3197 return do_wake_umutex(td, uap->obj);
3201 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3203 return do_unlock_umutex(td, uap->obj);
3207 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3209 return do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1);
3213 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3215 struct timespec *ts, timeout;
3218 /* Allow a null timespec (wait forever). */
3219 if (uap->uaddr2 == NULL)
3222 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3227 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3231 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3233 return do_cv_signal(td, uap->obj);
3237 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3239 return do_cv_broadcast(td, uap->obj);
3243 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3245 struct timespec timeout;
3248 /* Allow a null timespec (wait forever). */
3249 if (uap->uaddr2 == NULL) {
3250 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3252 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3255 error = do_rw_rdlock2(td, uap->obj, uap->val, &timeout);
3261 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3263 struct timespec timeout;
3266 /* Allow a null timespec (wait forever). */
3267 if (uap->uaddr2 == NULL) {
3268 error = do_rw_wrlock(td, uap->obj, 0);
3270 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3274 error = do_rw_wrlock2(td, uap->obj, &timeout);
3280 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3282 return do_rw_unlock(td, uap->obj);
3286 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3288 struct timespec *ts, timeout;
3291 /* Allow a null timespec (wait forever). */
3292 if (uap->uaddr2 == NULL)
3295 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3300 return (do_sem_wait(td, uap->obj, ts));
3304 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3306 return do_sem_wake(td, uap->obj);
3310 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3312 return do_wake2_umutex(td, uap->obj, uap->val);
3315 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
3317 static _umtx_op_func op_table[] = {
3318 __umtx_op_lock_umtx, /* UMTX_OP_LOCK */
3319 __umtx_op_unlock_umtx, /* UMTX_OP_UNLOCK */
3320 __umtx_op_wait, /* UMTX_OP_WAIT */
3321 __umtx_op_wake, /* UMTX_OP_WAKE */
3322 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_TRYLOCK */
3323 __umtx_op_lock_umutex, /* UMTX_OP_MUTEX_LOCK */
3324 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3325 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3326 __umtx_op_cv_wait, /* UMTX_OP_CV_WAIT*/
3327 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3328 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3329 __umtx_op_wait_uint, /* UMTX_OP_WAIT_UINT */
3330 __umtx_op_rw_rdlock, /* UMTX_OP_RW_RDLOCK */
3331 __umtx_op_rw_wrlock, /* UMTX_OP_RW_WRLOCK */
3332 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3333 __umtx_op_wait_uint_private, /* UMTX_OP_WAIT_UINT_PRIVATE */
3334 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3335 __umtx_op_wait_umutex, /* UMTX_OP_UMUTEX_WAIT */
3336 __umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */
3337 __umtx_op_sem_wait, /* UMTX_OP_SEM_WAIT */
3338 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3339 __umtx_op_nwake_private, /* UMTX_OP_NWAKE_PRIVATE */
3340 __umtx_op_wake2_umutex /* UMTX_OP_UMUTEX_WAKE2 */
3344 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
3346 if ((unsigned)uap->op < UMTX_OP_MAX)
3347 return (*op_table[uap->op])(td, uap);
3351 #ifdef COMPAT_FREEBSD32
3353 freebsd32_umtx_lock(struct thread *td, struct freebsd32_umtx_lock_args *uap)
3354 /* struct umtx *umtx */
3356 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
3360 freebsd32_umtx_unlock(struct thread *td, struct freebsd32_umtx_unlock_args *uap)
3361 /* struct umtx *umtx */
3363 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
3372 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
3374 struct timespec32 ts32;
3377 error = copyin(addr, &ts32, sizeof(struct timespec32));
3379 if (ts32.tv_sec < 0 ||
3380 ts32.tv_nsec >= 1000000000 ||
3384 tsp->tv_sec = ts32.tv_sec;
3385 tsp->tv_nsec = ts32.tv_nsec;
3392 __umtx_op_lock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
3394 struct timespec *ts, timeout;
3397 /* Allow a null timespec (wait forever). */
3398 if (uap->uaddr2 == NULL)
3401 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3406 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
3410 __umtx_op_unlock_umtx_compat32(struct thread *td, struct _umtx_op_args *uap)
3412 return (do_unlock_umtx32(td, uap->obj, (uint32_t)uap->val));
3416 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3418 struct timespec *ts, timeout;
3421 if (uap->uaddr2 == NULL)
3424 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3429 return do_wait(td, uap->obj, uap->val, ts, 1, 0);
3433 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3435 struct timespec *ts, timeout;
3438 /* Allow a null timespec (wait forever). */
3439 if (uap->uaddr2 == NULL)
3442 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3447 return do_lock_umutex(td, uap->obj, ts, 0);
3451 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3453 struct timespec *ts, timeout;
3456 /* Allow a null timespec (wait forever). */
3457 if (uap->uaddr2 == NULL)
3460 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3465 return do_lock_umutex(td, uap->obj, ts, _UMUTEX_WAIT);
3469 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3471 struct timespec *ts, timeout;
3474 /* Allow a null timespec (wait forever). */
3475 if (uap->uaddr2 == NULL)
3478 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3483 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3487 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3489 struct timespec timeout;
3492 /* Allow a null timespec (wait forever). */
3493 if (uap->uaddr2 == NULL) {
3494 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3496 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3499 error = do_rw_rdlock2(td, uap->obj, uap->val, &timeout);
3505 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3507 struct timespec timeout;
3510 /* Allow a null timespec (wait forever). */
3511 if (uap->uaddr2 == NULL) {
3512 error = do_rw_wrlock(td, uap->obj, 0);
3514 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3518 error = do_rw_wrlock2(td, uap->obj, &timeout);
3524 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3526 struct timespec *ts, timeout;
3529 if (uap->uaddr2 == NULL)
3532 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3537 return do_wait(td, uap->obj, uap->val, ts, 1, 1);
3541 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3543 struct timespec *ts, timeout;
3546 /* Allow a null timespec (wait forever). */
3547 if (uap->uaddr2 == NULL)
3550 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3555 return (do_sem_wait(td, uap->obj, ts));
3559 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
3561 int count = uap->val;
3562 uint32_t uaddrs[BATCH_SIZE];
3563 uint32_t **upp = (uint32_t **)uap->obj;
3570 if (tocopy > BATCH_SIZE)
3571 tocopy = BATCH_SIZE;
3572 error = copyin(upp+pos, uaddrs, tocopy * sizeof(uint32_t));
3575 for (i = 0; i < tocopy; ++i)
3576 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
3584 static _umtx_op_func op_table_compat32[] = {
3585 __umtx_op_lock_umtx_compat32, /* UMTX_OP_LOCK */
3586 __umtx_op_unlock_umtx_compat32, /* UMTX_OP_UNLOCK */
3587 __umtx_op_wait_compat32, /* UMTX_OP_WAIT */
3588 __umtx_op_wake, /* UMTX_OP_WAKE */
3589 __umtx_op_trylock_umutex, /* UMTX_OP_MUTEX_LOCK */
3590 __umtx_op_lock_umutex_compat32, /* UMTX_OP_MUTEX_TRYLOCK */
3591 __umtx_op_unlock_umutex, /* UMTX_OP_MUTEX_UNLOCK */
3592 __umtx_op_set_ceiling, /* UMTX_OP_SET_CEILING */
3593 __umtx_op_cv_wait_compat32, /* UMTX_OP_CV_WAIT*/
3594 __umtx_op_cv_signal, /* UMTX_OP_CV_SIGNAL */
3595 __umtx_op_cv_broadcast, /* UMTX_OP_CV_BROADCAST */
3596 __umtx_op_wait_compat32, /* UMTX_OP_WAIT_UINT */
3597 __umtx_op_rw_rdlock_compat32, /* UMTX_OP_RW_RDLOCK */
3598 __umtx_op_rw_wrlock_compat32, /* UMTX_OP_RW_WRLOCK */
3599 __umtx_op_rw_unlock, /* UMTX_OP_RW_UNLOCK */
3600 __umtx_op_wait_uint_private_compat32, /* UMTX_OP_WAIT_UINT_PRIVATE */
3601 __umtx_op_wake_private, /* UMTX_OP_WAKE_PRIVATE */
3602 __umtx_op_wait_umutex_compat32, /* UMTX_OP_UMUTEX_WAIT */
3603 __umtx_op_wake_umutex, /* UMTX_OP_UMUTEX_WAKE */
3604 __umtx_op_sem_wait_compat32, /* UMTX_OP_SEM_WAIT */
3605 __umtx_op_sem_wake, /* UMTX_OP_SEM_WAKE */
3606 __umtx_op_nwake_private32, /* UMTX_OP_NWAKE_PRIVATE */
3607 __umtx_op_wake2_umutex /* UMTX_OP_UMUTEX_WAKE2 */
3611 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
3613 if ((unsigned)uap->op < UMTX_OP_MAX)
3614 return (*op_table_compat32[uap->op])(td,
3615 (struct _umtx_op_args *)uap);
3621 umtx_thread_init(struct thread *td)
3623 td->td_umtxq = umtxq_alloc();
3624 td->td_umtxq->uq_thread = td;
3628 umtx_thread_fini(struct thread *td)
3630 umtxq_free(td->td_umtxq);
3634 * It will be called when new thread is created, e.g fork().
3637 umtx_thread_alloc(struct thread *td)
3642 uq->uq_inherited_pri = PRI_MAX;
3644 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
3645 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
3646 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
3647 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
3654 umtx_exec_hook(void *arg __unused, struct proc *p __unused,
3655 struct image_params *imgp __unused)
3657 umtx_thread_cleanup(curthread);
3661 * thread_exit() hook.
3664 umtx_thread_exit(struct thread *td)
3666 umtx_thread_cleanup(td);
3670 * clean up umtx data.
3673 umtx_thread_cleanup(struct thread *td)
3678 if ((uq = td->td_umtxq) == NULL)
3681 mtx_lock_spin(&umtx_lock);
3682 uq->uq_inherited_pri = PRI_MAX;
3683 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
3684 pi->pi_owner = NULL;
3685 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
3687 mtx_unlock_spin(&umtx_lock);
3689 sched_lend_user_prio(td, PRI_MAX);