2 * Copyright (c) 2015 The FreeBSD Foundation
3 * Copyright (c) 2004, David Xu <davidxu@freebsd.org>
4 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
7 * Portions of this software were developed by Konstantin Belousov
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice unmodified, this list of conditions, and the following
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
36 #include "opt_umtx_profiling.h"
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/fcntl.h>
42 #include <sys/filedesc.h>
43 #include <sys/limits.h>
45 #include <sys/malloc.h>
47 #include <sys/mutex.h>
50 #include <sys/resource.h>
51 #include <sys/resourcevar.h>
52 #include <sys/rwlock.h>
54 #include <sys/sched.h>
56 #include <sys/sysctl.h>
57 #include <sys/sysent.h>
58 #include <sys/systm.h>
59 #include <sys/sysproto.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/taskqueue.h>
62 #include <sys/eventhandler.h>
65 #include <security/mac/mac_framework.h>
68 #include <vm/vm_param.h>
70 #include <vm/vm_map.h>
71 #include <vm/vm_object.h>
73 #include <machine/cpu.h>
75 #ifdef COMPAT_FREEBSD32
76 #include <compat/freebsd32/freebsd32_proto.h>
80 #define _UMUTEX_WAIT 2
83 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
84 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
87 /* Priority inheritance mutex info. */
90 struct thread *pi_owner;
95 /* List entry to link umtx holding by thread */
96 TAILQ_ENTRY(umtx_pi) pi_link;
98 /* List entry in hash */
99 TAILQ_ENTRY(umtx_pi) pi_hashlink;
101 /* List for waiters */
102 TAILQ_HEAD(,umtx_q) pi_blocked;
104 /* Identify a userland lock object */
105 struct umtx_key pi_key;
108 /* A userland synchronous object user. */
110 /* Linked list for the hash. */
111 TAILQ_ENTRY(umtx_q) uq_link;
114 struct umtx_key uq_key;
118 #define UQF_UMTXQ 0x0001
120 /* The thread waits on. */
121 struct thread *uq_thread;
124 * Blocked on PI mutex. read can use chain lock
125 * or umtx_lock, write must have both chain lock and
126 * umtx_lock being hold.
128 struct umtx_pi *uq_pi_blocked;
130 /* On blocked list */
131 TAILQ_ENTRY(umtx_q) uq_lockq;
133 /* Thread contending with us */
134 TAILQ_HEAD(,umtx_pi) uq_pi_contested;
136 /* Inherited priority from PP mutex */
137 u_char uq_inherited_pri;
139 /* Spare queue ready to be reused */
140 struct umtxq_queue *uq_spare_queue;
142 /* The queue we on */
143 struct umtxq_queue *uq_cur_queue;
146 TAILQ_HEAD(umtxq_head, umtx_q);
148 /* Per-key wait-queue */
150 struct umtxq_head head;
152 LIST_ENTRY(umtxq_queue) link;
156 LIST_HEAD(umtxq_list, umtxq_queue);
158 /* Userland lock object's wait-queue chain */
160 /* Lock for this chain. */
163 /* List of sleep queues. */
164 struct umtxq_list uc_queue[2];
165 #define UMTX_SHARED_QUEUE 0
166 #define UMTX_EXCLUSIVE_QUEUE 1
168 LIST_HEAD(, umtxq_queue) uc_spare_queue;
173 /* Chain lock waiters */
176 /* All PI in the list */
177 TAILQ_HEAD(,umtx_pi) uc_pi_list;
179 #ifdef UMTX_PROFILING
185 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
188 * Don't propagate time-sharing priority, there is a security reason,
189 * a user can simply introduce PI-mutex, let thread A lock the mutex,
190 * and let another thread B block on the mutex, because B is
191 * sleeping, its priority will be boosted, this causes A's priority to
192 * be boosted via priority propagating too and will never be lowered even
193 * if it is using 100%CPU, this is unfair to other processes.
196 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
197 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
198 PRI_MAX_TIMESHARE : (td)->td_user_pri)
200 #define GOLDEN_RATIO_PRIME 2654404609U
201 #define UMTX_CHAINS 512
202 #define UMTX_SHIFTS (__WORD_BIT - 9)
204 #define GET_SHARE(flags) \
205 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
207 #define BUSY_SPINS 200
215 static uma_zone_t umtx_pi_zone;
216 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
217 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
218 static int umtx_pi_allocated;
220 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW, 0, "umtx debug");
221 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
222 &umtx_pi_allocated, 0, "Allocated umtx_pi");
224 #ifdef UMTX_PROFILING
225 static long max_length;
226 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
227 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD, 0, "umtx chain stats");
230 static void umtx_shm_init(void);
231 static void umtxq_sysinit(void *);
232 static void umtxq_hash(struct umtx_key *key);
233 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
234 static void umtxq_lock(struct umtx_key *key);
235 static void umtxq_unlock(struct umtx_key *key);
236 static void umtxq_busy(struct umtx_key *key);
237 static void umtxq_unbusy(struct umtx_key *key);
238 static void umtxq_insert_queue(struct umtx_q *uq, int q);
239 static void umtxq_remove_queue(struct umtx_q *uq, int q);
240 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *);
241 static int umtxq_count(struct umtx_key *key);
242 static struct umtx_pi *umtx_pi_alloc(int);
243 static void umtx_pi_free(struct umtx_pi *pi);
244 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags);
245 static void umtx_thread_cleanup(struct thread *td);
246 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
247 struct image_params *imgp __unused);
248 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
250 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
251 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
252 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
254 static struct mtx umtx_lock;
256 #ifdef UMTX_PROFILING
258 umtx_init_profiling(void)
260 struct sysctl_oid *chain_oid;
264 for (i = 0; i < UMTX_CHAINS; ++i) {
265 snprintf(chain_name, sizeof(chain_name), "%d", i);
266 chain_oid = SYSCTL_ADD_NODE(NULL,
267 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
268 chain_name, CTLFLAG_RD, NULL, "umtx hash stats");
269 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
270 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
271 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
272 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
277 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
281 struct umtxq_chain *uc;
282 u_int fract, i, j, tot, whole;
283 u_int sf0, sf1, sf2, sf3, sf4;
284 u_int si0, si1, si2, si3, si4;
285 u_int sw0, sw1, sw2, sw3, sw4;
287 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
288 for (i = 0; i < 2; i++) {
290 for (j = 0; j < UMTX_CHAINS; ++j) {
291 uc = &umtxq_chains[i][j];
292 mtx_lock(&uc->uc_lock);
293 tot += uc->max_length;
294 mtx_unlock(&uc->uc_lock);
297 sbuf_printf(&sb, "%u) Empty ", i);
299 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
300 si0 = si1 = si2 = si3 = si4 = 0;
301 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
302 for (j = 0; j < UMTX_CHAINS; j++) {
303 uc = &umtxq_chains[i][j];
304 mtx_lock(&uc->uc_lock);
305 whole = uc->max_length * 100;
306 mtx_unlock(&uc->uc_lock);
307 fract = (whole % tot) * 100;
308 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
312 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
317 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
322 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
327 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
334 sbuf_printf(&sb, "queue %u:\n", i);
335 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
337 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
339 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
341 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
343 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
349 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
355 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
357 struct umtxq_chain *uc;
362 error = sysctl_handle_int(oidp, &clear, 0, req);
363 if (error != 0 || req->newptr == NULL)
367 for (i = 0; i < 2; ++i) {
368 for (j = 0; j < UMTX_CHAINS; ++j) {
369 uc = &umtxq_chains[i][j];
370 mtx_lock(&uc->uc_lock);
373 mtx_unlock(&uc->uc_lock);
380 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
381 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
382 sysctl_debug_umtx_chains_clear, "I", "Clear umtx chains statistics");
383 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
384 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
385 sysctl_debug_umtx_chains_peaks, "A", "Highest peaks in chains max length");
389 umtxq_sysinit(void *arg __unused)
393 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
394 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
395 for (i = 0; i < 2; ++i) {
396 for (j = 0; j < UMTX_CHAINS; ++j) {
397 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
398 MTX_DEF | MTX_DUPOK);
399 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
400 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
401 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
402 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
403 umtxq_chains[i][j].uc_busy = 0;
404 umtxq_chains[i][j].uc_waiters = 0;
405 #ifdef UMTX_PROFILING
406 umtxq_chains[i][j].length = 0;
407 umtxq_chains[i][j].max_length = 0;
411 #ifdef UMTX_PROFILING
412 umtx_init_profiling();
414 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
415 EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
416 EVENTHANDLER_PRI_ANY);
425 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
426 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX, M_WAITOK | M_ZERO);
427 TAILQ_INIT(&uq->uq_spare_queue->head);
428 TAILQ_INIT(&uq->uq_pi_contested);
429 uq->uq_inherited_pri = PRI_MAX;
434 umtxq_free(struct umtx_q *uq)
436 MPASS(uq->uq_spare_queue != NULL);
437 free(uq->uq_spare_queue, M_UMTX);
442 umtxq_hash(struct umtx_key *key)
444 unsigned n = (uintptr_t)key->info.both.a + key->info.both.b;
445 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
448 static inline struct umtxq_chain *
449 umtxq_getchain(struct umtx_key *key)
451 if (key->type <= TYPE_SEM)
452 return (&umtxq_chains[1][key->hash]);
453 return (&umtxq_chains[0][key->hash]);
460 umtxq_lock(struct umtx_key *key)
462 struct umtxq_chain *uc;
464 uc = umtxq_getchain(key);
465 mtx_lock(&uc->uc_lock);
472 umtxq_unlock(struct umtx_key *key)
474 struct umtxq_chain *uc;
476 uc = umtxq_getchain(key);
477 mtx_unlock(&uc->uc_lock);
481 * Set chain to busy state when following operation
482 * may be blocked (kernel mutex can not be used).
485 umtxq_busy(struct umtx_key *key)
487 struct umtxq_chain *uc;
489 uc = umtxq_getchain(key);
490 mtx_assert(&uc->uc_lock, MA_OWNED);
494 int count = BUSY_SPINS;
497 while (uc->uc_busy && --count > 0)
503 while (uc->uc_busy) {
505 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
516 umtxq_unbusy(struct umtx_key *key)
518 struct umtxq_chain *uc;
520 uc = umtxq_getchain(key);
521 mtx_assert(&uc->uc_lock, MA_OWNED);
522 KASSERT(uc->uc_busy != 0, ("not busy"));
529 umtxq_unbusy_unlocked(struct umtx_key *key)
537 static struct umtxq_queue *
538 umtxq_queue_lookup(struct umtx_key *key, int q)
540 struct umtxq_queue *uh;
541 struct umtxq_chain *uc;
543 uc = umtxq_getchain(key);
544 UMTXQ_LOCKED_ASSERT(uc);
545 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
546 if (umtx_key_match(&uh->key, key))
554 umtxq_insert_queue(struct umtx_q *uq, int q)
556 struct umtxq_queue *uh;
557 struct umtxq_chain *uc;
559 uc = umtxq_getchain(&uq->uq_key);
560 UMTXQ_LOCKED_ASSERT(uc);
561 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
562 uh = umtxq_queue_lookup(&uq->uq_key, q);
564 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
566 uh = uq->uq_spare_queue;
567 uh->key = uq->uq_key;
568 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
569 #ifdef UMTX_PROFILING
571 if (uc->length > uc->max_length) {
572 uc->max_length = uc->length;
573 if (uc->max_length > max_length)
574 max_length = uc->max_length;
578 uq->uq_spare_queue = NULL;
580 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
582 uq->uq_flags |= UQF_UMTXQ;
583 uq->uq_cur_queue = uh;
588 umtxq_remove_queue(struct umtx_q *uq, int q)
590 struct umtxq_chain *uc;
591 struct umtxq_queue *uh;
593 uc = umtxq_getchain(&uq->uq_key);
594 UMTXQ_LOCKED_ASSERT(uc);
595 if (uq->uq_flags & UQF_UMTXQ) {
596 uh = uq->uq_cur_queue;
597 TAILQ_REMOVE(&uh->head, uq, uq_link);
599 uq->uq_flags &= ~UQF_UMTXQ;
600 if (TAILQ_EMPTY(&uh->head)) {
601 KASSERT(uh->length == 0,
602 ("inconsistent umtxq_queue length"));
603 #ifdef UMTX_PROFILING
606 LIST_REMOVE(uh, link);
608 uh = LIST_FIRST(&uc->uc_spare_queue);
609 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
610 LIST_REMOVE(uh, link);
612 uq->uq_spare_queue = uh;
613 uq->uq_cur_queue = NULL;
618 * Check if there are multiple waiters
621 umtxq_count(struct umtx_key *key)
623 struct umtxq_chain *uc;
624 struct umtxq_queue *uh;
626 uc = umtxq_getchain(key);
627 UMTXQ_LOCKED_ASSERT(uc);
628 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
635 * Check if there are multiple PI waiters and returns first
639 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
641 struct umtxq_chain *uc;
642 struct umtxq_queue *uh;
645 uc = umtxq_getchain(key);
646 UMTXQ_LOCKED_ASSERT(uc);
647 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
649 *first = TAILQ_FIRST(&uh->head);
656 umtxq_check_susp(struct thread *td)
662 * The check for TDF_NEEDSUSPCHK is racy, but it is enough to
663 * eventually break the lockstep loop.
665 if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
670 if (P_SHOULDSTOP(p) ||
671 ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) {
672 if (p->p_flag & P_SINGLE_EXIT)
682 * Wake up threads waiting on an userland object.
686 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
688 struct umtxq_chain *uc;
689 struct umtxq_queue *uh;
694 uc = umtxq_getchain(key);
695 UMTXQ_LOCKED_ASSERT(uc);
696 uh = umtxq_queue_lookup(key, q);
698 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
699 umtxq_remove_queue(uq, q);
710 * Wake up specified thread.
713 umtxq_signal_thread(struct umtx_q *uq)
715 struct umtxq_chain *uc;
717 uc = umtxq_getchain(&uq->uq_key);
718 UMTXQ_LOCKED_ASSERT(uc);
724 tstohz(const struct timespec *tsp)
728 TIMESPEC_TO_TIMEVAL(&tv, tsp);
733 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
734 const struct timespec *timeout)
737 timo->clockid = clockid;
739 kern_clock_gettime(curthread, clockid, &timo->end);
740 timo->cur = timo->end;
741 timespecadd(&timo->end, timeout);
743 timo->end = *timeout;
744 kern_clock_gettime(curthread, clockid, &timo->cur);
749 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
752 abs_timeout_init(timo, umtxtime->_clockid,
753 (umtxtime->_flags & UMTX_ABSTIME) != 0,
754 &umtxtime->_timeout);
758 abs_timeout_update(struct abs_timeout *timo)
760 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
764 abs_timeout_gethz(struct abs_timeout *timo)
768 if (timespeccmp(&timo->end, &timo->cur, <=))
771 timespecsub(&tts, &timo->cur);
772 return (tstohz(&tts));
776 * Put thread into sleep state, before sleeping, check if
777 * thread was removed from umtx queue.
780 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
782 struct umtxq_chain *uc;
785 uc = umtxq_getchain(&uq->uq_key);
786 UMTXQ_LOCKED_ASSERT(uc);
788 if (!(uq->uq_flags & UQF_UMTXQ))
790 if (abstime != NULL) {
791 timo = abs_timeout_gethz(abstime);
796 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
797 if (error != EWOULDBLOCK) {
798 umtxq_lock(&uq->uq_key);
802 abs_timeout_update(abstime);
803 umtxq_lock(&uq->uq_key);
809 * Convert userspace address into unique logical address.
812 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
814 struct thread *td = curthread;
816 vm_map_entry_t entry;
822 if (share == THREAD_SHARE) {
824 key->info.private.vs = td->td_proc->p_vmspace;
825 key->info.private.addr = (uintptr_t)addr;
827 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
828 map = &td->td_proc->p_vmspace->vm_map;
829 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
830 &entry, &key->info.shared.object, &pindex, &prot,
831 &wired) != KERN_SUCCESS) {
835 if ((share == PROCESS_SHARE) ||
836 (share == AUTO_SHARE &&
837 VM_INHERIT_SHARE == entry->inheritance)) {
839 key->info.shared.offset = (vm_offset_t)addr -
840 entry->start + entry->offset;
841 vm_object_reference(key->info.shared.object);
844 key->info.private.vs = td->td_proc->p_vmspace;
845 key->info.private.addr = (uintptr_t)addr;
847 vm_map_lookup_done(map, entry);
858 umtx_key_release(struct umtx_key *key)
861 vm_object_deallocate(key->info.shared.object);
865 * Fetch and compare value, sleep on the address if value is not changed.
868 do_wait(struct thread *td, void *addr, u_long id,
869 struct _umtx_time *timeout, int compat32, int is_private)
871 struct abs_timeout timo;
878 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
879 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
883 abs_timeout_init2(&timo, timeout);
885 umtxq_lock(&uq->uq_key);
887 umtxq_unlock(&uq->uq_key);
889 error = fueword(addr, &tmp);
893 error = fueword32(addr, &tmp32);
899 umtxq_lock(&uq->uq_key);
902 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
904 if ((uq->uq_flags & UQF_UMTXQ) == 0)
908 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
911 umtxq_unlock(&uq->uq_key);
912 umtx_key_release(&uq->uq_key);
913 if (error == ERESTART)
919 * Wake up threads sleeping on the specified address.
922 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
927 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
928 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
931 umtxq_signal(&key, n_wake);
933 umtx_key_release(&key);
938 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
941 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
942 struct _umtx_time *timeout, int mode)
944 struct abs_timeout timo;
946 uint32_t owner, old, id;
953 abs_timeout_init2(&timo, timeout);
956 * Care must be exercised when dealing with umtx structure. It
957 * can fault on any access.
960 rv = fueword32(&m->m_owner, &owner);
963 if (mode == _UMUTEX_WAIT) {
964 if (owner == UMUTEX_UNOWNED || owner == UMUTEX_CONTESTED)
968 * Try the uncontested case. This should be done in userland.
970 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
972 /* The address was invalid. */
976 /* The acquire succeeded. */
977 if (owner == UMUTEX_UNOWNED)
980 /* If no one owns it but it is contested try to acquire it. */
981 if (owner == UMUTEX_CONTESTED) {
982 rv = casueword32(&m->m_owner,
983 UMUTEX_CONTESTED, &owner,
984 id | UMUTEX_CONTESTED);
985 /* The address was invalid. */
989 if (owner == UMUTEX_CONTESTED)
992 rv = umtxq_check_susp(td);
996 /* If this failed the lock has changed, restart. */
1001 if (mode == _UMUTEX_TRY)
1005 * If we caught a signal, we have retried and now
1011 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1012 GET_SHARE(flags), &uq->uq_key)) != 0)
1015 umtxq_lock(&uq->uq_key);
1016 umtxq_busy(&uq->uq_key);
1018 umtxq_unlock(&uq->uq_key);
1021 * Set the contested bit so that a release in user space
1022 * knows to use the system call for unlock. If this fails
1023 * either some one else has acquired the lock or it has been
1026 rv = casueword32(&m->m_owner, owner, &old,
1027 owner | UMUTEX_CONTESTED);
1029 /* The address was invalid. */
1031 umtxq_lock(&uq->uq_key);
1033 umtxq_unbusy(&uq->uq_key);
1034 umtxq_unlock(&uq->uq_key);
1035 umtx_key_release(&uq->uq_key);
1040 * We set the contested bit, sleep. Otherwise the lock changed
1041 * and we need to retry or we lost a race to the thread
1042 * unlocking the umtx.
1044 umtxq_lock(&uq->uq_key);
1045 umtxq_unbusy(&uq->uq_key);
1047 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1050 umtxq_unlock(&uq->uq_key);
1051 umtx_key_release(&uq->uq_key);
1054 error = umtxq_check_susp(td);
1061 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1064 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags)
1066 struct umtx_key key;
1067 uint32_t owner, old, id;
1073 * Make sure we own this mtx.
1075 error = fueword32(&m->m_owner, &owner);
1079 if ((owner & ~UMUTEX_CONTESTED) != id)
1082 if ((owner & UMUTEX_CONTESTED) == 0) {
1083 error = casueword32(&m->m_owner, owner, &old, UMUTEX_UNOWNED);
1091 /* We should only ever be in here for contested locks */
1092 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1098 count = umtxq_count(&key);
1102 * When unlocking the umtx, it must be marked as unowned if
1103 * there is zero or one thread only waiting for it.
1104 * Otherwise, it must be marked as contested.
1106 error = casueword32(&m->m_owner, owner, &old,
1107 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1109 umtxq_signal(&key,1);
1112 umtx_key_release(&key);
1121 * Check if the mutex is available and wake up a waiter,
1122 * only for simple mutex.
1125 do_wake_umutex(struct thread *td, struct umutex *m)
1127 struct umtx_key key;
1133 error = fueword32(&m->m_owner, &owner);
1137 if ((owner & ~UMUTEX_CONTESTED) != 0)
1140 error = fueword32(&m->m_flags, &flags);
1144 /* We should only ever be in here for contested locks */
1145 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1151 count = umtxq_count(&key);
1155 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1162 if (error == 0 && count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1163 umtxq_signal(&key, 1);
1166 umtx_key_release(&key);
1171 * Check if the mutex has waiters and tries to fix contention bit.
1174 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1176 struct umtx_key key;
1177 uint32_t owner, old;
1182 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
1184 type = TYPE_NORMAL_UMUTEX;
1186 case UMUTEX_PRIO_INHERIT:
1187 type = TYPE_PI_UMUTEX;
1189 case UMUTEX_PRIO_PROTECT:
1190 type = TYPE_PP_UMUTEX;
1195 if ((error = umtx_key_get(m, type, GET_SHARE(flags),
1202 count = umtxq_count(&key);
1205 * Only repair contention bit if there is a waiter, this means the mutex
1206 * is still being referenced by userland code, otherwise don't update
1210 error = fueword32(&m->m_owner, &owner);
1213 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0) {
1214 error = casueword32(&m->m_owner, owner, &old,
1215 owner | UMUTEX_CONTESTED);
1223 error = umtxq_check_susp(td);
1227 } else if (count == 1) {
1228 error = fueword32(&m->m_owner, &owner);
1231 while (error == 0 && (owner & ~UMUTEX_CONTESTED) != 0 &&
1232 (owner & UMUTEX_CONTESTED) == 0) {
1233 error = casueword32(&m->m_owner, owner, &old,
1234 owner | UMUTEX_CONTESTED);
1242 error = umtxq_check_susp(td);
1248 if (error == EFAULT) {
1249 umtxq_signal(&key, INT_MAX);
1250 } else if (count != 0 && (owner & ~UMUTEX_CONTESTED) == 0)
1251 umtxq_signal(&key, 1);
1254 umtx_key_release(&key);
1258 static inline struct umtx_pi *
1259 umtx_pi_alloc(int flags)
1263 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1264 TAILQ_INIT(&pi->pi_blocked);
1265 atomic_add_int(&umtx_pi_allocated, 1);
1270 umtx_pi_free(struct umtx_pi *pi)
1272 uma_zfree(umtx_pi_zone, pi);
1273 atomic_add_int(&umtx_pi_allocated, -1);
1277 * Adjust the thread's position on a pi_state after its priority has been
1281 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1283 struct umtx_q *uq, *uq1, *uq2;
1286 mtx_assert(&umtx_lock, MA_OWNED);
1293 * Check if the thread needs to be moved on the blocked chain.
1294 * It needs to be moved if either its priority is lower than
1295 * the previous thread or higher than the next thread.
1297 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1298 uq2 = TAILQ_NEXT(uq, uq_lockq);
1299 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1300 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1302 * Remove thread from blocked chain and determine where
1303 * it should be moved to.
1305 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1306 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1307 td1 = uq1->uq_thread;
1308 MPASS(td1->td_proc->p_magic == P_MAGIC);
1309 if (UPRI(td1) > UPRI(td))
1314 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1316 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1321 static struct umtx_pi *
1322 umtx_pi_next(struct umtx_pi *pi)
1324 struct umtx_q *uq_owner;
1326 if (pi->pi_owner == NULL)
1328 uq_owner = pi->pi_owner->td_umtxq;
1329 if (uq_owner == NULL)
1331 return (uq_owner->uq_pi_blocked);
1335 * Floyd's Cycle-Finding Algorithm.
1338 umtx_pi_check_loop(struct umtx_pi *pi)
1340 struct umtx_pi *pi1; /* fast iterator */
1342 mtx_assert(&umtx_lock, MA_OWNED);
1347 pi = umtx_pi_next(pi);
1350 pi1 = umtx_pi_next(pi1);
1353 pi1 = umtx_pi_next(pi1);
1363 * Propagate priority when a thread is blocked on POSIX
1367 umtx_propagate_priority(struct thread *td)
1373 mtx_assert(&umtx_lock, MA_OWNED);
1376 pi = uq->uq_pi_blocked;
1379 if (umtx_pi_check_loop(pi))
1384 if (td == NULL || td == curthread)
1387 MPASS(td->td_proc != NULL);
1388 MPASS(td->td_proc->p_magic == P_MAGIC);
1391 if (td->td_lend_user_pri > pri)
1392 sched_lend_user_prio(td, pri);
1400 * Pick up the lock that td is blocked on.
1403 pi = uq->uq_pi_blocked;
1406 /* Resort td on the list if needed. */
1407 umtx_pi_adjust_thread(pi, td);
1412 * Unpropagate priority for a PI mutex when a thread blocked on
1413 * it is interrupted by signal or resumed by others.
1416 umtx_repropagate_priority(struct umtx_pi *pi)
1418 struct umtx_q *uq, *uq_owner;
1419 struct umtx_pi *pi2;
1422 mtx_assert(&umtx_lock, MA_OWNED);
1424 if (umtx_pi_check_loop(pi))
1426 while (pi != NULL && pi->pi_owner != NULL) {
1428 uq_owner = pi->pi_owner->td_umtxq;
1430 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1431 uq = TAILQ_FIRST(&pi2->pi_blocked);
1433 if (pri > UPRI(uq->uq_thread))
1434 pri = UPRI(uq->uq_thread);
1438 if (pri > uq_owner->uq_inherited_pri)
1439 pri = uq_owner->uq_inherited_pri;
1440 thread_lock(pi->pi_owner);
1441 sched_lend_user_prio(pi->pi_owner, pri);
1442 thread_unlock(pi->pi_owner);
1443 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1444 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1449 * Insert a PI mutex into owned list.
1452 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1454 struct umtx_q *uq_owner;
1456 uq_owner = owner->td_umtxq;
1457 mtx_assert(&umtx_lock, MA_OWNED);
1458 if (pi->pi_owner != NULL)
1459 panic("pi_owner != NULL");
1460 pi->pi_owner = owner;
1461 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1466 * Disown a PI mutex, and remove it from the owned list.
1469 umtx_pi_disown(struct umtx_pi *pi)
1472 mtx_assert(&umtx_lock, MA_OWNED);
1473 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1474 pi->pi_owner = NULL;
1478 * Claim ownership of a PI mutex.
1481 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1485 mtx_lock(&umtx_lock);
1486 if (pi->pi_owner == owner) {
1487 mtx_unlock(&umtx_lock);
1491 if (pi->pi_owner != NULL) {
1493 * userland may have already messed the mutex, sigh.
1495 mtx_unlock(&umtx_lock);
1498 umtx_pi_setowner(pi, owner);
1499 uq = TAILQ_FIRST(&pi->pi_blocked);
1503 pri = UPRI(uq->uq_thread);
1505 if (pri < UPRI(owner))
1506 sched_lend_user_prio(owner, pri);
1507 thread_unlock(owner);
1509 mtx_unlock(&umtx_lock);
1514 * Adjust a thread's order position in its blocked PI mutex,
1515 * this may result new priority propagating process.
1518 umtx_pi_adjust(struct thread *td, u_char oldpri)
1524 mtx_lock(&umtx_lock);
1526 * Pick up the lock that td is blocked on.
1528 pi = uq->uq_pi_blocked;
1530 umtx_pi_adjust_thread(pi, td);
1531 umtx_repropagate_priority(pi);
1533 mtx_unlock(&umtx_lock);
1537 * Sleep on a PI mutex.
1540 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi,
1541 uint32_t owner, const char *wmesg, struct abs_timeout *timo)
1543 struct umtxq_chain *uc;
1544 struct thread *td, *td1;
1550 KASSERT(td == curthread, ("inconsistent uq_thread"));
1551 uc = umtxq_getchain(&uq->uq_key);
1552 UMTXQ_LOCKED_ASSERT(uc);
1553 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1555 mtx_lock(&umtx_lock);
1556 if (pi->pi_owner == NULL) {
1557 mtx_unlock(&umtx_lock);
1558 /* XXX Only look up thread in current process. */
1559 td1 = tdfind(owner, curproc->p_pid);
1560 mtx_lock(&umtx_lock);
1562 if (pi->pi_owner == NULL)
1563 umtx_pi_setowner(pi, td1);
1564 PROC_UNLOCK(td1->td_proc);
1568 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1569 pri = UPRI(uq1->uq_thread);
1575 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1577 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1579 uq->uq_pi_blocked = pi;
1581 td->td_flags |= TDF_UPIBLOCKED;
1583 umtx_propagate_priority(td);
1584 mtx_unlock(&umtx_lock);
1585 umtxq_unbusy(&uq->uq_key);
1587 error = umtxq_sleep(uq, wmesg, timo);
1590 mtx_lock(&umtx_lock);
1591 uq->uq_pi_blocked = NULL;
1593 td->td_flags &= ~TDF_UPIBLOCKED;
1595 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1596 umtx_repropagate_priority(pi);
1597 mtx_unlock(&umtx_lock);
1598 umtxq_unlock(&uq->uq_key);
1604 * Add reference count for a PI mutex.
1607 umtx_pi_ref(struct umtx_pi *pi)
1609 struct umtxq_chain *uc;
1611 uc = umtxq_getchain(&pi->pi_key);
1612 UMTXQ_LOCKED_ASSERT(uc);
1617 * Decrease reference count for a PI mutex, if the counter
1618 * is decreased to zero, its memory space is freed.
1621 umtx_pi_unref(struct umtx_pi *pi)
1623 struct umtxq_chain *uc;
1625 uc = umtxq_getchain(&pi->pi_key);
1626 UMTXQ_LOCKED_ASSERT(uc);
1627 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1628 if (--pi->pi_refcount == 0) {
1629 mtx_lock(&umtx_lock);
1630 if (pi->pi_owner != NULL)
1632 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1633 ("blocked queue not empty"));
1634 mtx_unlock(&umtx_lock);
1635 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1641 * Find a PI mutex in hash table.
1643 static struct umtx_pi *
1644 umtx_pi_lookup(struct umtx_key *key)
1646 struct umtxq_chain *uc;
1649 uc = umtxq_getchain(key);
1650 UMTXQ_LOCKED_ASSERT(uc);
1652 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1653 if (umtx_key_match(&pi->pi_key, key)) {
1661 * Insert a PI mutex into hash table.
1664 umtx_pi_insert(struct umtx_pi *pi)
1666 struct umtxq_chain *uc;
1668 uc = umtxq_getchain(&pi->pi_key);
1669 UMTXQ_LOCKED_ASSERT(uc);
1670 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1677 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1678 struct _umtx_time *timeout, int try)
1680 struct abs_timeout timo;
1682 struct umtx_pi *pi, *new_pi;
1683 uint32_t id, owner, old;
1689 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1693 if (timeout != NULL)
1694 abs_timeout_init2(&timo, timeout);
1696 umtxq_lock(&uq->uq_key);
1697 pi = umtx_pi_lookup(&uq->uq_key);
1699 new_pi = umtx_pi_alloc(M_NOWAIT);
1700 if (new_pi == NULL) {
1701 umtxq_unlock(&uq->uq_key);
1702 new_pi = umtx_pi_alloc(M_WAITOK);
1703 umtxq_lock(&uq->uq_key);
1704 pi = umtx_pi_lookup(&uq->uq_key);
1706 umtx_pi_free(new_pi);
1710 if (new_pi != NULL) {
1711 new_pi->pi_key = uq->uq_key;
1712 umtx_pi_insert(new_pi);
1717 umtxq_unlock(&uq->uq_key);
1720 * Care must be exercised when dealing with umtx structure. It
1721 * can fault on any access.
1725 * Try the uncontested case. This should be done in userland.
1727 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
1728 /* The address was invalid. */
1734 /* The acquire succeeded. */
1735 if (owner == UMUTEX_UNOWNED) {
1740 /* If no one owns it but it is contested try to acquire it. */
1741 if (owner == UMUTEX_CONTESTED) {
1742 rv = casueword32(&m->m_owner,
1743 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
1744 /* The address was invalid. */
1750 if (owner == UMUTEX_CONTESTED) {
1751 umtxq_lock(&uq->uq_key);
1752 umtxq_busy(&uq->uq_key);
1753 error = umtx_pi_claim(pi, td);
1754 umtxq_unbusy(&uq->uq_key);
1755 umtxq_unlock(&uq->uq_key);
1758 * Since we're going to return an
1759 * error, restore the m_owner to its
1760 * previous, unowned state to avoid
1761 * compounding the problem.
1763 (void)casuword32(&m->m_owner,
1764 id | UMUTEX_CONTESTED,
1770 error = umtxq_check_susp(td);
1774 /* If this failed the lock has changed, restart. */
1778 if ((owner & ~UMUTEX_CONTESTED) == id) {
1789 * If we caught a signal, we have retried and now
1795 umtxq_lock(&uq->uq_key);
1796 umtxq_busy(&uq->uq_key);
1797 umtxq_unlock(&uq->uq_key);
1800 * Set the contested bit so that a release in user space
1801 * knows to use the system call for unlock. If this fails
1802 * either some one else has acquired the lock or it has been
1805 rv = casueword32(&m->m_owner, owner, &old,
1806 owner | UMUTEX_CONTESTED);
1808 /* The address was invalid. */
1810 umtxq_unbusy_unlocked(&uq->uq_key);
1815 umtxq_lock(&uq->uq_key);
1817 * We set the contested bit, sleep. Otherwise the lock changed
1818 * and we need to retry or we lost a race to the thread
1819 * unlocking the umtx.
1822 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
1823 "umtxpi", timeout == NULL ? NULL : &timo);
1827 umtxq_unbusy(&uq->uq_key);
1828 umtxq_unlock(&uq->uq_key);
1831 error = umtxq_check_susp(td);
1836 umtxq_lock(&uq->uq_key);
1838 umtxq_unlock(&uq->uq_key);
1840 umtx_key_release(&uq->uq_key);
1845 * Unlock a PI mutex.
1848 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags)
1850 struct umtx_key key;
1851 struct umtx_q *uq_first, *uq_first2, *uq_me;
1852 struct umtx_pi *pi, *pi2;
1853 uint32_t owner, old, id;
1860 * Make sure we own this mtx.
1862 error = fueword32(&m->m_owner, &owner);
1866 if ((owner & ~UMUTEX_CONTESTED) != id)
1869 /* This should be done in userland */
1870 if ((owner & UMUTEX_CONTESTED) == 0) {
1871 error = casueword32(&m->m_owner, owner, &old, UMUTEX_UNOWNED);
1879 /* We should only ever be in here for contested locks */
1880 if ((error = umtx_key_get(m, TYPE_PI_UMUTEX, GET_SHARE(flags),
1886 count = umtxq_count_pi(&key, &uq_first);
1887 if (uq_first != NULL) {
1888 mtx_lock(&umtx_lock);
1889 pi = uq_first->uq_pi_blocked;
1890 KASSERT(pi != NULL, ("pi == NULL?"));
1891 if (pi->pi_owner != td) {
1892 mtx_unlock(&umtx_lock);
1895 umtx_key_release(&key);
1896 /* userland messed the mutex */
1899 uq_me = td->td_umtxq;
1901 /* get highest priority thread which is still sleeping. */
1902 uq_first = TAILQ_FIRST(&pi->pi_blocked);
1903 while (uq_first != NULL &&
1904 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
1905 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
1908 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
1909 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
1910 if (uq_first2 != NULL) {
1911 if (pri > UPRI(uq_first2->uq_thread))
1912 pri = UPRI(uq_first2->uq_thread);
1916 sched_lend_user_prio(td, pri);
1918 mtx_unlock(&umtx_lock);
1920 umtxq_signal_thread(uq_first);
1922 pi = umtx_pi_lookup(&key);
1924 * A umtx_pi can exist if a signal or timeout removed the
1925 * last waiter from the umtxq, but there is still
1926 * a thread in do_lock_pi() holding the umtx_pi.
1930 * The umtx_pi can be unowned, such as when a thread
1931 * has just entered do_lock_pi(), allocated the
1932 * umtx_pi, and unlocked the umtxq.
1933 * If the current thread owns it, it must disown it.
1935 mtx_lock(&umtx_lock);
1936 if (pi->pi_owner == td)
1938 mtx_unlock(&umtx_lock);
1944 * When unlocking the umtx, it must be marked as unowned if
1945 * there is zero or one thread only waiting for it.
1946 * Otherwise, it must be marked as contested.
1948 error = casueword32(&m->m_owner, owner, &old,
1949 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1951 umtxq_unbusy_unlocked(&key);
1952 umtx_key_release(&key);
1964 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
1965 struct _umtx_time *timeout, int try)
1967 struct abs_timeout timo;
1968 struct umtx_q *uq, *uq2;
1972 int error, pri, old_inherited_pri, su, rv;
1976 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
1980 if (timeout != NULL)
1981 abs_timeout_init2(&timo, timeout);
1983 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
1985 old_inherited_pri = uq->uq_inherited_pri;
1986 umtxq_lock(&uq->uq_key);
1987 umtxq_busy(&uq->uq_key);
1988 umtxq_unlock(&uq->uq_key);
1990 rv = fueword32(&m->m_ceilings[0], &ceiling);
1995 ceiling = RTP_PRIO_MAX - ceiling;
1996 if (ceiling > RTP_PRIO_MAX) {
2001 mtx_lock(&umtx_lock);
2002 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2003 mtx_unlock(&umtx_lock);
2007 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2008 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2010 if (uq->uq_inherited_pri < UPRI(td))
2011 sched_lend_user_prio(td, uq->uq_inherited_pri);
2014 mtx_unlock(&umtx_lock);
2016 rv = casueword32(&m->m_owner,
2017 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
2018 /* The address was invalid. */
2024 if (owner == UMUTEX_CONTESTED) {
2035 * If we caught a signal, we have retried and now
2041 umtxq_lock(&uq->uq_key);
2043 umtxq_unbusy(&uq->uq_key);
2044 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2047 umtxq_unlock(&uq->uq_key);
2049 mtx_lock(&umtx_lock);
2050 uq->uq_inherited_pri = old_inherited_pri;
2052 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2053 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2055 if (pri > UPRI(uq2->uq_thread))
2056 pri = UPRI(uq2->uq_thread);
2059 if (pri > uq->uq_inherited_pri)
2060 pri = uq->uq_inherited_pri;
2062 sched_lend_user_prio(td, pri);
2064 mtx_unlock(&umtx_lock);
2068 mtx_lock(&umtx_lock);
2069 uq->uq_inherited_pri = old_inherited_pri;
2071 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2072 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2074 if (pri > UPRI(uq2->uq_thread))
2075 pri = UPRI(uq2->uq_thread);
2078 if (pri > uq->uq_inherited_pri)
2079 pri = uq->uq_inherited_pri;
2081 sched_lend_user_prio(td, pri);
2083 mtx_unlock(&umtx_lock);
2087 umtxq_unbusy_unlocked(&uq->uq_key);
2088 umtx_key_release(&uq->uq_key);
2093 * Unlock a PP mutex.
2096 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags)
2098 struct umtx_key key;
2099 struct umtx_q *uq, *uq2;
2103 int error, pri, new_inherited_pri, su;
2107 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2110 * Make sure we own this mtx.
2112 error = fueword32(&m->m_owner, &owner);
2116 if ((owner & ~UMUTEX_CONTESTED) != id)
2119 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2124 new_inherited_pri = PRI_MAX;
2126 rceiling = RTP_PRIO_MAX - rceiling;
2127 if (rceiling > RTP_PRIO_MAX)
2129 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2132 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2139 * For priority protected mutex, always set unlocked state
2140 * to UMUTEX_CONTESTED, so that userland always enters kernel
2141 * to lock the mutex, it is necessary because thread priority
2142 * has to be adjusted for such mutex.
2144 error = suword32(&m->m_owner, UMUTEX_CONTESTED);
2148 umtxq_signal(&key, 1);
2155 mtx_lock(&umtx_lock);
2157 uq->uq_inherited_pri = new_inherited_pri;
2159 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2160 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2162 if (pri > UPRI(uq2->uq_thread))
2163 pri = UPRI(uq2->uq_thread);
2166 if (pri > uq->uq_inherited_pri)
2167 pri = uq->uq_inherited_pri;
2169 sched_lend_user_prio(td, pri);
2171 mtx_unlock(&umtx_lock);
2173 umtx_key_release(&key);
2178 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2179 uint32_t *old_ceiling)
2182 uint32_t save_ceiling;
2187 error = fueword32(&m->m_flags, &flags);
2190 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2192 if (ceiling > RTP_PRIO_MAX)
2196 if ((error = umtx_key_get(m, TYPE_PP_UMUTEX, GET_SHARE(flags),
2200 umtxq_lock(&uq->uq_key);
2201 umtxq_busy(&uq->uq_key);
2202 umtxq_unlock(&uq->uq_key);
2204 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2210 rv = casueword32(&m->m_owner,
2211 UMUTEX_CONTESTED, &owner, id | UMUTEX_CONTESTED);
2217 if (owner == UMUTEX_CONTESTED) {
2218 suword32(&m->m_ceilings[0], ceiling);
2219 suword32(&m->m_owner, UMUTEX_CONTESTED);
2224 if ((owner & ~UMUTEX_CONTESTED) == id) {
2225 suword32(&m->m_ceilings[0], ceiling);
2231 * If we caught a signal, we have retried and now
2238 * We set the contested bit, sleep. Otherwise the lock changed
2239 * and we need to retry or we lost a race to the thread
2240 * unlocking the umtx.
2242 umtxq_lock(&uq->uq_key);
2244 umtxq_unbusy(&uq->uq_key);
2245 error = umtxq_sleep(uq, "umtxpp", NULL);
2247 umtxq_unlock(&uq->uq_key);
2249 umtxq_lock(&uq->uq_key);
2251 umtxq_signal(&uq->uq_key, INT_MAX);
2252 umtxq_unbusy(&uq->uq_key);
2253 umtxq_unlock(&uq->uq_key);
2254 umtx_key_release(&uq->uq_key);
2255 if (error == 0 && old_ceiling != NULL)
2256 suword32(old_ceiling, save_ceiling);
2261 * Lock a userland POSIX mutex.
2264 do_lock_umutex(struct thread *td, struct umutex *m,
2265 struct _umtx_time *timeout, int mode)
2270 error = fueword32(&m->m_flags, &flags);
2274 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2276 error = do_lock_normal(td, m, flags, timeout, mode);
2278 case UMUTEX_PRIO_INHERIT:
2279 error = do_lock_pi(td, m, flags, timeout, mode);
2281 case UMUTEX_PRIO_PROTECT:
2282 error = do_lock_pp(td, m, flags, timeout, mode);
2287 if (timeout == NULL) {
2288 if (error == EINTR && mode != _UMUTEX_WAIT)
2291 /* Timed-locking is not restarted. */
2292 if (error == ERESTART)
2299 * Unlock a userland POSIX mutex.
2302 do_unlock_umutex(struct thread *td, struct umutex *m)
2307 error = fueword32(&m->m_flags, &flags);
2311 switch(flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2313 return (do_unlock_normal(td, m, flags));
2314 case UMUTEX_PRIO_INHERIT:
2315 return (do_unlock_pi(td, m, flags));
2316 case UMUTEX_PRIO_PROTECT:
2317 return (do_unlock_pp(td, m, flags));
2324 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2325 struct timespec *timeout, u_long wflags)
2327 struct abs_timeout timo;
2329 uint32_t flags, clockid, hasw;
2333 error = fueword32(&cv->c_flags, &flags);
2336 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2340 if ((wflags & CVWAIT_CLOCKID) != 0) {
2341 error = fueword32(&cv->c_clockid, &clockid);
2343 umtx_key_release(&uq->uq_key);
2346 if (clockid < CLOCK_REALTIME ||
2347 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2348 /* hmm, only HW clock id will work. */
2349 umtx_key_release(&uq->uq_key);
2353 clockid = CLOCK_REALTIME;
2356 umtxq_lock(&uq->uq_key);
2357 umtxq_busy(&uq->uq_key);
2359 umtxq_unlock(&uq->uq_key);
2362 * Set c_has_waiters to 1 before releasing user mutex, also
2363 * don't modify cache line when unnecessary.
2365 error = fueword32(&cv->c_has_waiters, &hasw);
2366 if (error == 0 && hasw == 0)
2367 suword32(&cv->c_has_waiters, 1);
2369 umtxq_unbusy_unlocked(&uq->uq_key);
2371 error = do_unlock_umutex(td, m);
2373 if (timeout != NULL)
2374 abs_timeout_init(&timo, clockid, ((wflags & CVWAIT_ABSTIME) != 0),
2377 umtxq_lock(&uq->uq_key);
2379 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2383 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2387 * This must be timeout,interrupted by signal or
2388 * surprious wakeup, clear c_has_waiter flag when
2391 umtxq_busy(&uq->uq_key);
2392 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2393 int oldlen = uq->uq_cur_queue->length;
2396 umtxq_unlock(&uq->uq_key);
2397 suword32(&cv->c_has_waiters, 0);
2398 umtxq_lock(&uq->uq_key);
2401 umtxq_unbusy(&uq->uq_key);
2402 if (error == ERESTART)
2406 umtxq_unlock(&uq->uq_key);
2407 umtx_key_release(&uq->uq_key);
2412 * Signal a userland condition variable.
2415 do_cv_signal(struct thread *td, struct ucond *cv)
2417 struct umtx_key key;
2418 int error, cnt, nwake;
2421 error = fueword32(&cv->c_flags, &flags);
2424 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2428 cnt = umtxq_count(&key);
2429 nwake = umtxq_signal(&key, 1);
2432 error = suword32(&cv->c_has_waiters, 0);
2439 umtx_key_release(&key);
2444 do_cv_broadcast(struct thread *td, struct ucond *cv)
2446 struct umtx_key key;
2450 error = fueword32(&cv->c_flags, &flags);
2453 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2458 umtxq_signal(&key, INT_MAX);
2461 error = suword32(&cv->c_has_waiters, 0);
2465 umtxq_unbusy_unlocked(&key);
2467 umtx_key_release(&key);
2472 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag, struct _umtx_time *timeout)
2474 struct abs_timeout timo;
2476 uint32_t flags, wrflags;
2477 int32_t state, oldstate;
2478 int32_t blocked_readers;
2482 error = fueword32(&rwlock->rw_flags, &flags);
2485 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2489 if (timeout != NULL)
2490 abs_timeout_init2(&timo, timeout);
2492 wrflags = URWLOCK_WRITE_OWNER;
2493 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2494 wrflags |= URWLOCK_WRITE_WAITERS;
2497 rv = fueword32(&rwlock->rw_state, &state);
2499 umtx_key_release(&uq->uq_key);
2503 /* try to lock it */
2504 while (!(state & wrflags)) {
2505 if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) {
2506 umtx_key_release(&uq->uq_key);
2509 rv = casueword32(&rwlock->rw_state, state,
2510 &oldstate, state + 1);
2512 umtx_key_release(&uq->uq_key);
2515 if (oldstate == state) {
2516 umtx_key_release(&uq->uq_key);
2519 error = umtxq_check_susp(td);
2528 /* grab monitor lock */
2529 umtxq_lock(&uq->uq_key);
2530 umtxq_busy(&uq->uq_key);
2531 umtxq_unlock(&uq->uq_key);
2534 * re-read the state, in case it changed between the try-lock above
2535 * and the check below
2537 rv = fueword32(&rwlock->rw_state, &state);
2541 /* set read contention bit */
2542 while (error == 0 && (state & wrflags) &&
2543 !(state & URWLOCK_READ_WAITERS)) {
2544 rv = casueword32(&rwlock->rw_state, state,
2545 &oldstate, state | URWLOCK_READ_WAITERS);
2550 if (oldstate == state)
2553 error = umtxq_check_susp(td);
2558 umtxq_unbusy_unlocked(&uq->uq_key);
2562 /* state is changed while setting flags, restart */
2563 if (!(state & wrflags)) {
2564 umtxq_unbusy_unlocked(&uq->uq_key);
2565 error = umtxq_check_susp(td);
2572 /* contention bit is set, before sleeping, increase read waiter count */
2573 rv = fueword32(&rwlock->rw_blocked_readers,
2576 umtxq_unbusy_unlocked(&uq->uq_key);
2580 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2582 while (state & wrflags) {
2583 umtxq_lock(&uq->uq_key);
2585 umtxq_unbusy(&uq->uq_key);
2587 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2590 umtxq_busy(&uq->uq_key);
2592 umtxq_unlock(&uq->uq_key);
2595 rv = fueword32(&rwlock->rw_state, &state);
2602 /* decrease read waiter count, and may clear read contention bit */
2603 rv = fueword32(&rwlock->rw_blocked_readers,
2606 umtxq_unbusy_unlocked(&uq->uq_key);
2610 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2611 if (blocked_readers == 1) {
2612 rv = fueword32(&rwlock->rw_state, &state);
2615 while (error == 0) {
2616 rv = casueword32(&rwlock->rw_state, state,
2617 &oldstate, state & ~URWLOCK_READ_WAITERS);
2622 if (oldstate == state)
2625 error = umtxq_check_susp(td);
2629 umtxq_unbusy_unlocked(&uq->uq_key);
2633 umtx_key_release(&uq->uq_key);
2634 if (error == ERESTART)
2640 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2642 struct abs_timeout timo;
2645 int32_t state, oldstate;
2646 int32_t blocked_writers;
2647 int32_t blocked_readers;
2651 error = fueword32(&rwlock->rw_flags, &flags);
2654 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2658 if (timeout != NULL)
2659 abs_timeout_init2(&timo, timeout);
2661 blocked_readers = 0;
2663 rv = fueword32(&rwlock->rw_state, &state);
2665 umtx_key_release(&uq->uq_key);
2668 while (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2669 rv = casueword32(&rwlock->rw_state, state,
2670 &oldstate, state | URWLOCK_WRITE_OWNER);
2672 umtx_key_release(&uq->uq_key);
2675 if (oldstate == state) {
2676 umtx_key_release(&uq->uq_key);
2680 error = umtxq_check_susp(td);
2686 if (!(state & (URWLOCK_WRITE_OWNER|URWLOCK_WRITE_WAITERS)) &&
2687 blocked_readers != 0) {
2688 umtxq_lock(&uq->uq_key);
2689 umtxq_busy(&uq->uq_key);
2690 umtxq_signal_queue(&uq->uq_key, INT_MAX, UMTX_SHARED_QUEUE);
2691 umtxq_unbusy(&uq->uq_key);
2692 umtxq_unlock(&uq->uq_key);
2698 /* grab monitor lock */
2699 umtxq_lock(&uq->uq_key);
2700 umtxq_busy(&uq->uq_key);
2701 umtxq_unlock(&uq->uq_key);
2704 * re-read the state, in case it changed between the try-lock above
2705 * and the check below
2707 rv = fueword32(&rwlock->rw_state, &state);
2711 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
2712 URWLOCK_READER_COUNT(state) != 0) &&
2713 (state & URWLOCK_WRITE_WAITERS) == 0) {
2714 rv = casueword32(&rwlock->rw_state, state,
2715 &oldstate, state | URWLOCK_WRITE_WAITERS);
2720 if (oldstate == state)
2723 error = umtxq_check_susp(td);
2728 umtxq_unbusy_unlocked(&uq->uq_key);
2732 if (!(state & URWLOCK_WRITE_OWNER) && URWLOCK_READER_COUNT(state) == 0) {
2733 umtxq_unbusy_unlocked(&uq->uq_key);
2734 error = umtxq_check_susp(td);
2740 rv = fueword32(&rwlock->rw_blocked_writers,
2743 umtxq_unbusy_unlocked(&uq->uq_key);
2747 suword32(&rwlock->rw_blocked_writers, blocked_writers+1);
2749 while ((state & URWLOCK_WRITE_OWNER) || URWLOCK_READER_COUNT(state) != 0) {
2750 umtxq_lock(&uq->uq_key);
2751 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2752 umtxq_unbusy(&uq->uq_key);
2754 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
2757 umtxq_busy(&uq->uq_key);
2758 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
2759 umtxq_unlock(&uq->uq_key);
2762 rv = fueword32(&rwlock->rw_state, &state);
2769 rv = fueword32(&rwlock->rw_blocked_writers,
2772 umtxq_unbusy_unlocked(&uq->uq_key);
2776 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
2777 if (blocked_writers == 1) {
2778 rv = fueword32(&rwlock->rw_state, &state);
2780 umtxq_unbusy_unlocked(&uq->uq_key);
2785 rv = casueword32(&rwlock->rw_state, state,
2786 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
2791 if (oldstate == state)
2794 error = umtxq_check_susp(td);
2796 * We are leaving the URWLOCK_WRITE_WAITERS
2797 * behind, but this should not harm the
2803 rv = fueword32(&rwlock->rw_blocked_readers,
2806 umtxq_unbusy_unlocked(&uq->uq_key);
2811 blocked_readers = 0;
2813 umtxq_unbusy_unlocked(&uq->uq_key);
2816 umtx_key_release(&uq->uq_key);
2817 if (error == ERESTART)
2823 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
2827 int32_t state, oldstate;
2828 int error, rv, q, count;
2831 error = fueword32(&rwlock->rw_flags, &flags);
2834 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2838 error = fueword32(&rwlock->rw_state, &state);
2843 if (state & URWLOCK_WRITE_OWNER) {
2845 rv = casueword32(&rwlock->rw_state, state,
2846 &oldstate, state & ~URWLOCK_WRITE_OWNER);
2851 if (oldstate != state) {
2853 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
2857 error = umtxq_check_susp(td);
2863 } else if (URWLOCK_READER_COUNT(state) != 0) {
2865 rv = casueword32(&rwlock->rw_state, state,
2866 &oldstate, state - 1);
2871 if (oldstate != state) {
2873 if (URWLOCK_READER_COUNT(oldstate) == 0) {
2877 error = umtxq_check_susp(td);
2890 if (!(flags & URWLOCK_PREFER_READER)) {
2891 if (state & URWLOCK_WRITE_WAITERS) {
2893 q = UMTX_EXCLUSIVE_QUEUE;
2894 } else if (state & URWLOCK_READ_WAITERS) {
2896 q = UMTX_SHARED_QUEUE;
2899 if (state & URWLOCK_READ_WAITERS) {
2901 q = UMTX_SHARED_QUEUE;
2902 } else if (state & URWLOCK_WRITE_WAITERS) {
2904 q = UMTX_EXCLUSIVE_QUEUE;
2909 umtxq_lock(&uq->uq_key);
2910 umtxq_busy(&uq->uq_key);
2911 umtxq_signal_queue(&uq->uq_key, count, q);
2912 umtxq_unbusy(&uq->uq_key);
2913 umtxq_unlock(&uq->uq_key);
2916 umtx_key_release(&uq->uq_key);
2920 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
2922 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
2924 struct abs_timeout timo;
2926 uint32_t flags, count, count1;
2930 error = fueword32(&sem->_flags, &flags);
2933 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
2937 if (timeout != NULL)
2938 abs_timeout_init2(&timo, timeout);
2940 umtxq_lock(&uq->uq_key);
2941 umtxq_busy(&uq->uq_key);
2943 umtxq_unlock(&uq->uq_key);
2944 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
2946 rv = fueword32(&sem->_count, &count);
2947 if (rv == -1 || count != 0) {
2948 umtxq_lock(&uq->uq_key);
2949 umtxq_unbusy(&uq->uq_key);
2951 umtxq_unlock(&uq->uq_key);
2952 umtx_key_release(&uq->uq_key);
2953 return (rv == -1 ? EFAULT : 0);
2955 umtxq_lock(&uq->uq_key);
2956 umtxq_unbusy(&uq->uq_key);
2958 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
2960 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2964 /* A relative timeout cannot be restarted. */
2965 if (error == ERESTART && timeout != NULL &&
2966 (timeout->_flags & UMTX_ABSTIME) == 0)
2969 umtxq_unlock(&uq->uq_key);
2970 umtx_key_release(&uq->uq_key);
2975 * Signal a userland semaphore.
2978 do_sem_wake(struct thread *td, struct _usem *sem)
2980 struct umtx_key key;
2984 error = fueword32(&sem->_flags, &flags);
2987 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
2991 cnt = umtxq_count(&key);
2993 umtxq_signal(&key, 1);
2995 * Check if count is greater than 0, this means the memory is
2996 * still being referenced by user code, so we can safely
2997 * update _has_waiters flag.
3001 error = suword32(&sem->_has_waiters, 0);
3009 umtx_key_release(&key);
3015 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3017 struct abs_timeout timo;
3019 uint32_t count, flags;
3023 flags = fuword32(&sem->_flags);
3024 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3028 if (timeout != NULL)
3029 abs_timeout_init2(&timo, timeout);
3031 umtxq_lock(&uq->uq_key);
3032 umtxq_busy(&uq->uq_key);
3034 umtxq_unlock(&uq->uq_key);
3035 rv = fueword32(&sem->_count, &count);
3037 umtxq_lock(&uq->uq_key);
3038 umtxq_unbusy(&uq->uq_key);
3040 umtxq_unlock(&uq->uq_key);
3041 umtx_key_release(&uq->uq_key);
3045 if (USEM_COUNT(count) != 0) {
3046 umtxq_lock(&uq->uq_key);
3047 umtxq_unbusy(&uq->uq_key);
3049 umtxq_unlock(&uq->uq_key);
3050 umtx_key_release(&uq->uq_key);
3053 if (count == USEM_HAS_WAITERS)
3055 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3057 umtxq_lock(&uq->uq_key);
3058 umtxq_unbusy(&uq->uq_key);
3060 umtxq_unlock(&uq->uq_key);
3061 umtx_key_release(&uq->uq_key);
3067 umtxq_lock(&uq->uq_key);
3068 umtxq_unbusy(&uq->uq_key);
3070 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3072 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3076 /* A relative timeout cannot be restarted. */
3077 if (error == ERESTART && timeout != NULL &&
3078 (timeout->_flags & UMTX_ABSTIME) == 0)
3081 umtxq_unlock(&uq->uq_key);
3082 umtx_key_release(&uq->uq_key);
3087 * Signal a userland semaphore.
3090 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3092 struct umtx_key key;
3094 uint32_t count, flags;
3096 rv = fueword32(&sem->_flags, &flags);
3099 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3103 cnt = umtxq_count(&key);
3105 umtxq_signal(&key, 1);
3108 * If this was the last sleeping thread, clear the waiters
3113 rv = fueword32(&sem->_count, &count);
3114 while (rv != -1 && count & USEM_HAS_WAITERS)
3115 rv = casueword32(&sem->_count, count, &count,
3116 count & ~USEM_HAS_WAITERS);
3124 umtx_key_release(&key);
3129 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3133 error = copyin(addr, tsp, sizeof(struct timespec));
3135 if (tsp->tv_sec < 0 ||
3136 tsp->tv_nsec >= 1000000000 ||
3144 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3148 if (size <= sizeof(struct timespec)) {
3149 tp->_clockid = CLOCK_REALTIME;
3151 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3153 error = copyin(addr, tp, sizeof(struct _umtx_time));
3156 if (tp->_timeout.tv_sec < 0 ||
3157 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3163 __umtx_op_unimpl(struct thread *td, struct _umtx_op_args *uap)
3166 return (EOPNOTSUPP);
3170 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3172 struct _umtx_time timeout, *tm_p;
3175 if (uap->uaddr2 == NULL)
3178 error = umtx_copyin_umtx_time(
3179 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3184 return do_wait(td, uap->obj, uap->val, tm_p, 0, 0);
3188 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3190 struct _umtx_time timeout, *tm_p;
3193 if (uap->uaddr2 == NULL)
3196 error = umtx_copyin_umtx_time(
3197 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3202 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3206 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3208 struct _umtx_time *tm_p, timeout;
3211 if (uap->uaddr2 == NULL)
3214 error = umtx_copyin_umtx_time(
3215 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3220 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3224 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3226 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3229 #define BATCH_SIZE 128
3231 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3233 int count = uap->val;
3234 void *uaddrs[BATCH_SIZE];
3235 char **upp = (char **)uap->obj;
3242 if (tocopy > BATCH_SIZE)
3243 tocopy = BATCH_SIZE;
3244 error = copyin(upp+pos, uaddrs, tocopy * sizeof(char *));
3247 for (i = 0; i < tocopy; ++i)
3248 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3256 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3258 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3262 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3264 struct _umtx_time *tm_p, timeout;
3267 /* Allow a null timespec (wait forever). */
3268 if (uap->uaddr2 == NULL)
3271 error = umtx_copyin_umtx_time(
3272 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3277 return do_lock_umutex(td, uap->obj, tm_p, 0);
3281 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3283 return do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY);
3287 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3289 struct _umtx_time *tm_p, timeout;
3292 /* Allow a null timespec (wait forever). */
3293 if (uap->uaddr2 == NULL)
3296 error = umtx_copyin_umtx_time(
3297 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3302 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3306 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3308 return do_wake_umutex(td, uap->obj);
3312 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3314 return do_unlock_umutex(td, uap->obj);
3318 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3320 return do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1);
3324 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3326 struct timespec *ts, timeout;
3329 /* Allow a null timespec (wait forever). */
3330 if (uap->uaddr2 == NULL)
3333 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3338 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3342 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3344 return do_cv_signal(td, uap->obj);
3348 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3350 return do_cv_broadcast(td, uap->obj);
3354 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3356 struct _umtx_time timeout;
3359 /* Allow a null timespec (wait forever). */
3360 if (uap->uaddr2 == NULL) {
3361 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3363 error = umtx_copyin_umtx_time(uap->uaddr2,
3364 (size_t)uap->uaddr1, &timeout);
3367 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3373 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3375 struct _umtx_time timeout;
3378 /* Allow a null timespec (wait forever). */
3379 if (uap->uaddr2 == NULL) {
3380 error = do_rw_wrlock(td, uap->obj, 0);
3382 error = umtx_copyin_umtx_time(uap->uaddr2,
3383 (size_t)uap->uaddr1, &timeout);
3387 error = do_rw_wrlock(td, uap->obj, &timeout);
3393 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3395 return do_rw_unlock(td, uap->obj);
3398 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3400 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3402 struct _umtx_time *tm_p, timeout;
3405 /* Allow a null timespec (wait forever). */
3406 if (uap->uaddr2 == NULL)
3409 error = umtx_copyin_umtx_time(
3410 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3415 return (do_sem_wait(td, uap->obj, tm_p));
3419 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3422 return (do_sem_wake(td, uap->obj));
3427 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3430 return (do_wake2_umutex(td, uap->obj, uap->val));
3434 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap)
3436 struct _umtx_time *tm_p, timeout;
3439 /* Allow a null timespec (wait forever). */
3440 if (uap->uaddr2 == NULL)
3443 error = umtx_copyin_umtx_time(
3444 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3449 return (do_sem2_wait(td, uap->obj, tm_p));
3453 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap)
3456 return (do_sem2_wake(td, uap->obj));
3459 #define USHM_OBJ_UMTX(o) \
3460 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
3462 #define USHMF_REG_LINKED 0x0001
3463 #define USHMF_OBJ_LINKED 0x0002
3464 struct umtx_shm_reg {
3465 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
3466 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
3467 struct umtx_key ushm_key;
3468 struct ucred *ushm_cred;
3469 struct shmfd *ushm_obj;
3474 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
3475 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
3477 static uma_zone_t umtx_shm_reg_zone;
3478 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
3479 static struct mtx umtx_shm_lock;
3480 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
3481 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
3483 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
3486 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
3488 struct umtx_shm_reg_head d;
3489 struct umtx_shm_reg *reg, *reg1;
3492 mtx_lock(&umtx_shm_lock);
3493 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
3494 mtx_unlock(&umtx_shm_lock);
3495 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
3496 TAILQ_REMOVE(&d, reg, ushm_reg_link);
3497 umtx_shm_free_reg(reg);
3501 static struct task umtx_shm_reg_delfree_task =
3502 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
3504 static struct umtx_shm_reg *
3505 umtx_shm_find_reg_locked(const struct umtx_key *key)
3507 struct umtx_shm_reg *reg;
3508 struct umtx_shm_reg_head *reg_head;
3510 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
3511 mtx_assert(&umtx_shm_lock, MA_OWNED);
3512 reg_head = &umtx_shm_registry[key->hash];
3513 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
3514 KASSERT(reg->ushm_key.shared,
3515 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
3516 if (reg->ushm_key.info.shared.object ==
3517 key->info.shared.object &&
3518 reg->ushm_key.info.shared.offset ==
3519 key->info.shared.offset) {
3520 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
3521 KASSERT(reg->ushm_refcnt > 0,
3522 ("reg %p refcnt 0 onlist", reg));
3523 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
3524 ("reg %p not linked", reg));
3532 static struct umtx_shm_reg *
3533 umtx_shm_find_reg(const struct umtx_key *key)
3535 struct umtx_shm_reg *reg;
3537 mtx_lock(&umtx_shm_lock);
3538 reg = umtx_shm_find_reg_locked(key);
3539 mtx_unlock(&umtx_shm_lock);
3544 umtx_shm_free_reg(struct umtx_shm_reg *reg)
3547 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
3548 crfree(reg->ushm_cred);
3549 shm_drop(reg->ushm_obj);
3550 uma_zfree(umtx_shm_reg_zone, reg);
3554 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
3558 mtx_assert(&umtx_shm_lock, MA_OWNED);
3559 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
3561 res = reg->ushm_refcnt == 0;
3563 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
3564 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
3565 reg, ushm_reg_link);
3566 reg->ushm_flags &= ~USHMF_REG_LINKED;
3568 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
3569 LIST_REMOVE(reg, ushm_obj_link);
3570 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
3577 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
3583 object = reg->ushm_obj->shm_object;
3584 VM_OBJECT_WLOCK(object);
3585 object->flags |= OBJ_UMTXDEAD;
3586 VM_OBJECT_WUNLOCK(object);
3588 mtx_lock(&umtx_shm_lock);
3589 dofree = umtx_shm_unref_reg_locked(reg, force);
3590 mtx_unlock(&umtx_shm_lock);
3592 umtx_shm_free_reg(reg);
3596 umtx_shm_object_init(vm_object_t object)
3599 LIST_INIT(USHM_OBJ_UMTX(object));
3603 umtx_shm_object_terminated(vm_object_t object)
3605 struct umtx_shm_reg *reg, *reg1;
3609 mtx_lock(&umtx_shm_lock);
3610 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
3611 if (umtx_shm_unref_reg_locked(reg, true)) {
3612 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
3617 mtx_unlock(&umtx_shm_lock);
3619 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
3623 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
3624 struct umtx_shm_reg **res)
3626 struct umtx_shm_reg *reg, *reg1;
3630 reg = umtx_shm_find_reg(key);
3635 cred = td->td_ucred;
3636 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
3638 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
3639 reg->ushm_refcnt = 1;
3640 bcopy(key, ®->ushm_key, sizeof(*key));
3641 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR);
3642 reg->ushm_cred = crhold(cred);
3643 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
3645 umtx_shm_free_reg(reg);
3648 mtx_lock(&umtx_shm_lock);
3649 reg1 = umtx_shm_find_reg_locked(key);
3651 mtx_unlock(&umtx_shm_lock);
3652 umtx_shm_free_reg(reg);
3657 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
3658 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
3660 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
3661 mtx_unlock(&umtx_shm_lock);
3667 umtx_shm_alive(struct thread *td, void *addr)
3670 vm_map_entry_t entry;
3677 map = &td->td_proc->p_vmspace->vm_map;
3678 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
3679 &object, &pindex, &prot, &wired);
3680 if (res != KERN_SUCCESS)
3685 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
3686 vm_map_lookup_done(map, entry);
3695 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
3696 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
3697 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
3698 for (i = 0; i < nitems(umtx_shm_registry); i++)
3699 TAILQ_INIT(&umtx_shm_registry[i]);
3703 umtx_shm(struct thread *td, void *addr, u_int flags)
3705 struct umtx_key key;
3706 struct umtx_shm_reg *reg;
3710 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
3711 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
3713 if ((flags & UMTX_SHM_ALIVE) != 0)
3714 return (umtx_shm_alive(td, addr));
3715 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
3718 KASSERT(key.shared == 1, ("non-shared key"));
3719 if ((flags & UMTX_SHM_CREAT) != 0) {
3720 error = umtx_shm_create_reg(td, &key, ®);
3722 reg = umtx_shm_find_reg(&key);
3726 umtx_key_release(&key);
3729 KASSERT(reg != NULL, ("no reg"));
3730 if ((flags & UMTX_SHM_DESTROY) != 0) {
3731 umtx_shm_unref_reg(reg, true);
3735 error = mac_posixshm_check_open(td->td_ucred,
3736 reg->ushm_obj, FFLAGS(O_RDWR));
3739 error = shm_access(reg->ushm_obj, td->td_ucred,
3743 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
3745 shm_hold(reg->ushm_obj);
3746 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
3748 td->td_retval[0] = fd;
3752 umtx_shm_unref_reg(reg, false);
3757 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap)
3760 return (umtx_shm(td, uap->uaddr1, uap->val));
3763 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
3765 static const _umtx_op_func op_table[] = {
3766 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
3767 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
3768 [UMTX_OP_WAIT] = __umtx_op_wait,
3769 [UMTX_OP_WAKE] = __umtx_op_wake,
3770 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
3771 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
3772 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
3773 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
3774 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
3775 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
3776 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
3777 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
3778 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
3779 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
3780 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
3781 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
3782 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
3783 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
3784 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
3785 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3786 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
3787 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
3789 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
3790 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
3792 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
3793 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
3794 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
3795 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
3796 [UMTX_OP_SHM] = __umtx_op_shm,
3800 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
3803 if ((unsigned)uap->op < nitems(op_table))
3804 return (*op_table[uap->op])(td, uap);
3808 #ifdef COMPAT_FREEBSD32
3815 struct umtx_time32 {
3816 struct timespec32 timeout;
3822 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
3824 struct timespec32 ts32;
3827 error = copyin(addr, &ts32, sizeof(struct timespec32));
3829 if (ts32.tv_sec < 0 ||
3830 ts32.tv_nsec >= 1000000000 ||
3834 tsp->tv_sec = ts32.tv_sec;
3835 tsp->tv_nsec = ts32.tv_nsec;
3842 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
3844 struct umtx_time32 t32;
3847 t32.clockid = CLOCK_REALTIME;
3849 if (size <= sizeof(struct timespec32))
3850 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
3852 error = copyin(addr, &t32, sizeof(struct umtx_time32));
3855 if (t32.timeout.tv_sec < 0 ||
3856 t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
3858 tp->_timeout.tv_sec = t32.timeout.tv_sec;
3859 tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
3860 tp->_flags = t32.flags;
3861 tp->_clockid = t32.clockid;
3866 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3868 struct _umtx_time *tm_p, timeout;
3871 if (uap->uaddr2 == NULL)
3874 error = umtx_copyin_umtx_time32(uap->uaddr2,
3875 (size_t)uap->uaddr1, &timeout);
3880 return do_wait(td, uap->obj, uap->val, tm_p, 1, 0);
3884 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3886 struct _umtx_time *tm_p, timeout;
3889 /* Allow a null timespec (wait forever). */
3890 if (uap->uaddr2 == NULL)
3893 error = umtx_copyin_umtx_time(uap->uaddr2,
3894 (size_t)uap->uaddr1, &timeout);
3899 return do_lock_umutex(td, uap->obj, tm_p, 0);
3903 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
3905 struct _umtx_time *tm_p, timeout;
3908 /* Allow a null timespec (wait forever). */
3909 if (uap->uaddr2 == NULL)
3912 error = umtx_copyin_umtx_time32(uap->uaddr2,
3913 (size_t)uap->uaddr1, &timeout);
3918 return do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT);
3922 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3924 struct timespec *ts, timeout;
3927 /* Allow a null timespec (wait forever). */
3928 if (uap->uaddr2 == NULL)
3931 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
3936 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3940 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3942 struct _umtx_time timeout;
3945 /* Allow a null timespec (wait forever). */
3946 if (uap->uaddr2 == NULL) {
3947 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3949 error = umtx_copyin_umtx_time32(uap->uaddr2,
3950 (size_t)uap->uaddr1, &timeout);
3953 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3959 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
3961 struct _umtx_time timeout;
3964 /* Allow a null timespec (wait forever). */
3965 if (uap->uaddr2 == NULL) {
3966 error = do_rw_wrlock(td, uap->obj, 0);
3968 error = umtx_copyin_umtx_time32(uap->uaddr2,
3969 (size_t)uap->uaddr1, &timeout);
3972 error = do_rw_wrlock(td, uap->obj, &timeout);
3978 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3980 struct _umtx_time *tm_p, timeout;
3983 if (uap->uaddr2 == NULL)
3986 error = umtx_copyin_umtx_time32(
3987 uap->uaddr2, (size_t)uap->uaddr1,&timeout);
3992 return do_wait(td, uap->obj, uap->val, tm_p, 1, 1);
3995 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3997 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
3999 struct _umtx_time *tm_p, timeout;
4002 /* Allow a null timespec (wait forever). */
4003 if (uap->uaddr2 == NULL)
4006 error = umtx_copyin_umtx_time32(uap->uaddr2,
4007 (size_t)uap->uaddr1, &timeout);
4012 return (do_sem_wait(td, uap->obj, tm_p));
4017 __umtx_op_sem2_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4019 struct _umtx_time *tm_p, timeout;
4022 /* Allow a null timespec (wait forever). */
4023 if (uap->uaddr2 == NULL)
4026 error = umtx_copyin_umtx_time32(uap->uaddr2,
4027 (size_t)uap->uaddr1, &timeout);
4032 return (do_sem2_wait(td, uap->obj, tm_p));
4036 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
4038 int count = uap->val;
4039 uint32_t uaddrs[BATCH_SIZE];
4040 uint32_t **upp = (uint32_t **)uap->obj;
4047 if (tocopy > BATCH_SIZE)
4048 tocopy = BATCH_SIZE;
4049 error = copyin(upp+pos, uaddrs, tocopy * sizeof(uint32_t));
4052 for (i = 0; i < tocopy; ++i)
4053 kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
4061 static const _umtx_op_func op_table_compat32[] = {
4062 [UMTX_OP_RESERVED0] = __umtx_op_unimpl,
4063 [UMTX_OP_RESERVED1] = __umtx_op_unimpl,
4064 [UMTX_OP_WAIT] = __umtx_op_wait_compat32,
4065 [UMTX_OP_WAKE] = __umtx_op_wake,
4066 [UMTX_OP_MUTEX_LOCK] = __umtx_op_trylock_umutex,
4067 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_lock_umutex_compat32,
4068 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4069 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4070 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait_compat32,
4071 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4072 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4073 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_compat32,
4074 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock_compat32,
4075 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock_compat32,
4076 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4077 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private_compat32,
4078 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4079 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex_compat32,
4080 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4081 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4082 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait_compat32,
4083 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4085 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4086 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4088 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private32,
4089 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4090 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait_compat32,
4091 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4092 [UMTX_OP_SHM] = __umtx_op_shm,
4096 freebsd32_umtx_op(struct thread *td, struct freebsd32_umtx_op_args *uap)
4099 if ((unsigned)uap->op < nitems(op_table_compat32)) {
4100 return (*op_table_compat32[uap->op])(td,
4101 (struct _umtx_op_args *)uap);
4108 umtx_thread_init(struct thread *td)
4110 td->td_umtxq = umtxq_alloc();
4111 td->td_umtxq->uq_thread = td;
4115 umtx_thread_fini(struct thread *td)
4117 umtxq_free(td->td_umtxq);
4121 * It will be called when new thread is created, e.g fork().
4124 umtx_thread_alloc(struct thread *td)
4129 uq->uq_inherited_pri = PRI_MAX;
4131 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4132 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4133 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4134 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4141 umtx_exec_hook(void *arg __unused, struct proc *p __unused,
4142 struct image_params *imgp __unused)
4144 umtx_thread_cleanup(curthread);
4148 * thread_exit() hook.
4151 umtx_thread_exit(struct thread *td)
4153 umtx_thread_cleanup(td);
4157 * clean up umtx data.
4160 umtx_thread_cleanup(struct thread *td)
4165 if ((uq = td->td_umtxq) == NULL)
4168 mtx_lock(&umtx_lock);
4169 uq->uq_inherited_pri = PRI_MAX;
4170 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
4171 pi->pi_owner = NULL;
4172 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
4174 mtx_unlock(&umtx_lock);
4176 sched_lend_user_prio(td, PRI_MAX);