2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015, 2016 The FreeBSD Foundation
5 * Copyright (c) 2004, David Xu <davidxu@freebsd.org>
6 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
9 * Portions of this software were developed by Konstantin Belousov
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice unmodified, this list of conditions, and the following
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_umtx_profiling.h"
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
43 #include <sys/filedesc.h>
44 #include <sys/limits.h>
46 #include <sys/malloc.h>
48 #include <sys/mutex.h>
51 #include <sys/resource.h>
52 #include <sys/resourcevar.h>
53 #include <sys/rwlock.h>
55 #include <sys/sched.h>
57 #include <sys/sysctl.h>
58 #include <sys/sysent.h>
59 #include <sys/systm.h>
60 #include <sys/sysproto.h>
61 #include <sys/syscallsubr.h>
62 #include <sys/taskqueue.h>
64 #include <sys/eventhandler.h>
67 #include <security/mac/mac_framework.h>
70 #include <vm/vm_param.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_object.h>
75 #include <machine/atomic.h>
76 #include <machine/cpu.h>
78 #include <compat/freebsd32/freebsd32.h>
79 #ifdef COMPAT_FREEBSD32
80 #include <compat/freebsd32/freebsd32_proto.h>
84 #define _UMUTEX_WAIT 2
87 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
88 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
91 /* Priority inheritance mutex info. */
94 struct thread *pi_owner;
99 /* List entry to link umtx holding by thread */
100 TAILQ_ENTRY(umtx_pi) pi_link;
102 /* List entry in hash */
103 TAILQ_ENTRY(umtx_pi) pi_hashlink;
105 /* List for waiters */
106 TAILQ_HEAD(,umtx_q) pi_blocked;
108 /* Identify a userland lock object */
109 struct umtx_key pi_key;
112 /* A userland synchronous object user. */
114 /* Linked list for the hash. */
115 TAILQ_ENTRY(umtx_q) uq_link;
118 struct umtx_key uq_key;
122 #define UQF_UMTXQ 0x0001
124 /* The thread waits on. */
125 struct thread *uq_thread;
128 * Blocked on PI mutex. read can use chain lock
129 * or umtx_lock, write must have both chain lock and
130 * umtx_lock being hold.
132 struct umtx_pi *uq_pi_blocked;
134 /* On blocked list */
135 TAILQ_ENTRY(umtx_q) uq_lockq;
137 /* Thread contending with us */
138 TAILQ_HEAD(,umtx_pi) uq_pi_contested;
140 /* Inherited priority from PP mutex */
141 u_char uq_inherited_pri;
143 /* Spare queue ready to be reused */
144 struct umtxq_queue *uq_spare_queue;
146 /* The queue we on */
147 struct umtxq_queue *uq_cur_queue;
150 TAILQ_HEAD(umtxq_head, umtx_q);
152 /* Per-key wait-queue */
154 struct umtxq_head head;
156 LIST_ENTRY(umtxq_queue) link;
160 LIST_HEAD(umtxq_list, umtxq_queue);
162 /* Userland lock object's wait-queue chain */
164 /* Lock for this chain. */
167 /* List of sleep queues. */
168 struct umtxq_list uc_queue[2];
169 #define UMTX_SHARED_QUEUE 0
170 #define UMTX_EXCLUSIVE_QUEUE 1
172 LIST_HEAD(, umtxq_queue) uc_spare_queue;
177 /* Chain lock waiters */
180 /* All PI in the list */
181 TAILQ_HEAD(,umtx_pi) uc_pi_list;
183 #ifdef UMTX_PROFILING
189 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
192 * Don't propagate time-sharing priority, there is a security reason,
193 * a user can simply introduce PI-mutex, let thread A lock the mutex,
194 * and let another thread B block on the mutex, because B is
195 * sleeping, its priority will be boosted, this causes A's priority to
196 * be boosted via priority propagating too and will never be lowered even
197 * if it is using 100%CPU, this is unfair to other processes.
200 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
201 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
202 PRI_MAX_TIMESHARE : (td)->td_user_pri)
204 #define GOLDEN_RATIO_PRIME 2654404609U
206 #define UMTX_CHAINS 512
208 #define UMTX_SHIFTS (__WORD_BIT - 9)
210 #define GET_SHARE(flags) \
211 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
213 #define BUSY_SPINS 200
217 bool is_abs_real; /* TIMER_ABSTIME && CLOCK_REALTIME* */
222 struct umtx_copyops {
223 int (*copyin_timeout)(const void *uaddr, struct timespec *tsp);
224 int (*copyin_umtx_time)(const void *uaddr, size_t size,
225 struct _umtx_time *tp);
226 int (*copyin_robust_lists)(const void *uaddr, size_t size,
227 struct umtx_robust_lists_params *rbp);
228 int (*copyout_timeout)(void *uaddr, size_t size,
229 struct timespec *tsp);
230 const size_t timespec_sz;
231 const size_t umtx_time_sz;
235 _Static_assert(sizeof(struct umutex) == sizeof(struct umutex32), "umutex32");
236 _Static_assert(__offsetof(struct umutex, m_spare[0]) ==
237 __offsetof(struct umutex32, m_spare[0]), "m_spare32");
239 int umtx_shm_vnobj_persistent = 0;
240 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_vnode_persistent, CTLFLAG_RWTUN,
241 &umtx_shm_vnobj_persistent, 0,
242 "False forces destruction of umtx attached to file, on last close");
243 static int umtx_max_rb = 1000;
244 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_max_robust, CTLFLAG_RWTUN,
246 "Maximum number of robust mutexes allowed for each thread");
248 static uma_zone_t umtx_pi_zone;
249 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
250 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
251 static int umtx_pi_allocated;
253 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
255 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
256 &umtx_pi_allocated, 0, "Allocated umtx_pi");
257 static int umtx_verbose_rb = 1;
258 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
262 #ifdef UMTX_PROFILING
263 static long max_length;
264 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
265 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
269 static void abs_timeout_update(struct abs_timeout *timo);
271 static void umtx_shm_init(void);
272 static void umtxq_sysinit(void *);
273 static void umtxq_hash(struct umtx_key *key);
274 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
275 static void umtxq_unlock(struct umtx_key *key);
276 static void umtxq_busy(struct umtx_key *key);
277 static void umtxq_unbusy(struct umtx_key *key);
278 static void umtxq_insert_queue(struct umtx_q *uq, int q);
279 static void umtxq_remove_queue(struct umtx_q *uq, int q);
280 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *);
281 static int umtxq_count(struct umtx_key *key);
282 static struct umtx_pi *umtx_pi_alloc(int);
283 static void umtx_pi_free(struct umtx_pi *pi);
284 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
286 static void umtx_thread_cleanup(struct thread *td);
287 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
289 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
290 #define umtxq_insert(uq) umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
291 #define umtxq_remove(uq) umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
293 static struct mtx umtx_lock;
295 #ifdef UMTX_PROFILING
297 umtx_init_profiling(void)
299 struct sysctl_oid *chain_oid;
303 for (i = 0; i < UMTX_CHAINS; ++i) {
304 snprintf(chain_name, sizeof(chain_name), "%d", i);
305 chain_oid = SYSCTL_ADD_NODE(NULL,
306 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
307 chain_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
309 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
310 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
311 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
312 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
317 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
321 struct umtxq_chain *uc;
322 u_int fract, i, j, tot, whole;
323 u_int sf0, sf1, sf2, sf3, sf4;
324 u_int si0, si1, si2, si3, si4;
325 u_int sw0, sw1, sw2, sw3, sw4;
327 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
328 for (i = 0; i < 2; i++) {
330 for (j = 0; j < UMTX_CHAINS; ++j) {
331 uc = &umtxq_chains[i][j];
332 mtx_lock(&uc->uc_lock);
333 tot += uc->max_length;
334 mtx_unlock(&uc->uc_lock);
337 sbuf_printf(&sb, "%u) Empty ", i);
339 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
340 si0 = si1 = si2 = si3 = si4 = 0;
341 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
342 for (j = 0; j < UMTX_CHAINS; j++) {
343 uc = &umtxq_chains[i][j];
344 mtx_lock(&uc->uc_lock);
345 whole = uc->max_length * 100;
346 mtx_unlock(&uc->uc_lock);
347 fract = (whole % tot) * 100;
348 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
352 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
357 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
362 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
367 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
374 sbuf_printf(&sb, "queue %u:\n", i);
375 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
377 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
379 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
381 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
383 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
389 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
395 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
397 struct umtxq_chain *uc;
402 error = sysctl_handle_int(oidp, &clear, 0, req);
403 if (error != 0 || req->newptr == NULL)
407 for (i = 0; i < 2; ++i) {
408 for (j = 0; j < UMTX_CHAINS; ++j) {
409 uc = &umtxq_chains[i][j];
410 mtx_lock(&uc->uc_lock);
413 mtx_unlock(&uc->uc_lock);
420 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
421 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
422 sysctl_debug_umtx_chains_clear, "I",
423 "Clear umtx chains statistics");
424 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
425 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
426 sysctl_debug_umtx_chains_peaks, "A",
427 "Highest peaks in chains max length");
431 umtxq_sysinit(void *arg __unused)
435 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
436 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
437 for (i = 0; i < 2; ++i) {
438 for (j = 0; j < UMTX_CHAINS; ++j) {
439 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
440 MTX_DEF | MTX_DUPOK);
441 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
442 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
443 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
444 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
445 umtxq_chains[i][j].uc_busy = 0;
446 umtxq_chains[i][j].uc_waiters = 0;
447 #ifdef UMTX_PROFILING
448 umtxq_chains[i][j].length = 0;
449 umtxq_chains[i][j].max_length = 0;
453 #ifdef UMTX_PROFILING
454 umtx_init_profiling();
456 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
465 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
466 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
468 TAILQ_INIT(&uq->uq_spare_queue->head);
469 TAILQ_INIT(&uq->uq_pi_contested);
470 uq->uq_inherited_pri = PRI_MAX;
475 umtxq_free(struct umtx_q *uq)
478 MPASS(uq->uq_spare_queue != NULL);
479 free(uq->uq_spare_queue, M_UMTX);
484 umtxq_hash(struct umtx_key *key)
488 n = (uintptr_t)key->info.both.a + key->info.both.b;
489 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
492 static inline struct umtxq_chain *
493 umtxq_getchain(struct umtx_key *key)
496 if (key->type <= TYPE_SEM)
497 return (&umtxq_chains[1][key->hash]);
498 return (&umtxq_chains[0][key->hash]);
504 * The code is a macro so that file/line information is taken from the caller.
506 #define umtxq_lock(key) do { \
507 struct umtx_key *_key = (key); \
508 struct umtxq_chain *_uc; \
510 _uc = umtxq_getchain(_key); \
511 mtx_lock(&_uc->uc_lock); \
518 umtxq_unlock(struct umtx_key *key)
520 struct umtxq_chain *uc;
522 uc = umtxq_getchain(key);
523 mtx_unlock(&uc->uc_lock);
527 * Set chain to busy state when following operation
528 * may be blocked (kernel mutex can not be used).
531 umtxq_busy(struct umtx_key *key)
533 struct umtxq_chain *uc;
535 uc = umtxq_getchain(key);
536 mtx_assert(&uc->uc_lock, MA_OWNED);
540 int count = BUSY_SPINS;
543 while (uc->uc_busy && --count > 0)
549 while (uc->uc_busy) {
551 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
562 umtxq_unbusy(struct umtx_key *key)
564 struct umtxq_chain *uc;
566 uc = umtxq_getchain(key);
567 mtx_assert(&uc->uc_lock, MA_OWNED);
568 KASSERT(uc->uc_busy != 0, ("not busy"));
575 umtxq_unbusy_unlocked(struct umtx_key *key)
583 static struct umtxq_queue *
584 umtxq_queue_lookup(struct umtx_key *key, int q)
586 struct umtxq_queue *uh;
587 struct umtxq_chain *uc;
589 uc = umtxq_getchain(key);
590 UMTXQ_LOCKED_ASSERT(uc);
591 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
592 if (umtx_key_match(&uh->key, key))
600 umtxq_insert_queue(struct umtx_q *uq, int q)
602 struct umtxq_queue *uh;
603 struct umtxq_chain *uc;
605 uc = umtxq_getchain(&uq->uq_key);
606 UMTXQ_LOCKED_ASSERT(uc);
607 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
608 uh = umtxq_queue_lookup(&uq->uq_key, q);
610 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
612 uh = uq->uq_spare_queue;
613 uh->key = uq->uq_key;
614 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
615 #ifdef UMTX_PROFILING
617 if (uc->length > uc->max_length) {
618 uc->max_length = uc->length;
619 if (uc->max_length > max_length)
620 max_length = uc->max_length;
624 uq->uq_spare_queue = NULL;
626 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
628 uq->uq_flags |= UQF_UMTXQ;
629 uq->uq_cur_queue = uh;
634 umtxq_remove_queue(struct umtx_q *uq, int q)
636 struct umtxq_chain *uc;
637 struct umtxq_queue *uh;
639 uc = umtxq_getchain(&uq->uq_key);
640 UMTXQ_LOCKED_ASSERT(uc);
641 if (uq->uq_flags & UQF_UMTXQ) {
642 uh = uq->uq_cur_queue;
643 TAILQ_REMOVE(&uh->head, uq, uq_link);
645 uq->uq_flags &= ~UQF_UMTXQ;
646 if (TAILQ_EMPTY(&uh->head)) {
647 KASSERT(uh->length == 0,
648 ("inconsistent umtxq_queue length"));
649 #ifdef UMTX_PROFILING
652 LIST_REMOVE(uh, link);
654 uh = LIST_FIRST(&uc->uc_spare_queue);
655 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
656 LIST_REMOVE(uh, link);
658 uq->uq_spare_queue = uh;
659 uq->uq_cur_queue = NULL;
664 * Check if there are multiple waiters
667 umtxq_count(struct umtx_key *key)
669 struct umtxq_queue *uh;
671 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
672 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
679 * Check if there are multiple PI waiters and returns first
683 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
685 struct umtxq_queue *uh;
688 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
689 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
691 *first = TAILQ_FIRST(&uh->head);
698 * Wake up threads waiting on an userland object.
702 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
704 struct umtxq_queue *uh;
709 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
710 uh = umtxq_queue_lookup(key, q);
712 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
713 umtxq_remove_queue(uq, q);
723 * Wake up specified thread.
726 umtxq_signal_thread(struct umtx_q *uq)
729 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
735 tstohz(const struct timespec *tsp)
739 TIMESPEC_TO_TIMEVAL(&tv, tsp);
744 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
745 const struct timespec *timeout)
748 timo->clockid = clockid;
750 timo->is_abs_real = false;
751 abs_timeout_update(timo);
752 timespecadd(&timo->cur, timeout, &timo->end);
754 timo->end = *timeout;
755 timo->is_abs_real = clockid == CLOCK_REALTIME ||
756 clockid == CLOCK_REALTIME_FAST ||
757 clockid == CLOCK_REALTIME_PRECISE;
759 * If is_abs_real, umtxq_sleep will read the clock
760 * after setting td_rtcgen; otherwise, read it here.
762 if (!timo->is_abs_real) {
763 abs_timeout_update(timo);
769 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
772 abs_timeout_init(timo, umtxtime->_clockid,
773 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
777 abs_timeout_update(struct abs_timeout *timo)
780 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
784 abs_timeout_gethz(struct abs_timeout *timo)
788 if (timespeccmp(&timo->end, &timo->cur, <=))
790 timespecsub(&timo->end, &timo->cur, &tts);
791 return (tstohz(&tts));
795 umtx_unlock_val(uint32_t flags, bool rb)
799 return (UMUTEX_RB_OWNERDEAD);
800 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
801 return (UMUTEX_RB_NOTRECOV);
803 return (UMUTEX_UNOWNED);
808 * Put thread into sleep state, before sleeping, check if
809 * thread was removed from umtx queue.
812 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
814 struct umtxq_chain *uc;
817 if (abstime != NULL && abstime->is_abs_real) {
818 curthread->td_rtcgen = atomic_load_acq_int(&rtc_generation);
819 abs_timeout_update(abstime);
822 uc = umtxq_getchain(&uq->uq_key);
823 UMTXQ_LOCKED_ASSERT(uc);
825 if (!(uq->uq_flags & UQF_UMTXQ)) {
829 if (abstime != NULL) {
830 timo = abs_timeout_gethz(abstime);
837 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
838 if (error == EINTR || error == ERESTART) {
839 umtxq_lock(&uq->uq_key);
842 if (abstime != NULL) {
843 if (abstime->is_abs_real)
844 curthread->td_rtcgen =
845 atomic_load_acq_int(&rtc_generation);
846 abs_timeout_update(abstime);
848 umtxq_lock(&uq->uq_key);
851 curthread->td_rtcgen = 0;
856 * Convert userspace address into unique logical address.
859 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
861 struct thread *td = curthread;
863 vm_map_entry_t entry;
869 if (share == THREAD_SHARE) {
871 key->info.private.vs = td->td_proc->p_vmspace;
872 key->info.private.addr = (uintptr_t)addr;
874 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
875 map = &td->td_proc->p_vmspace->vm_map;
876 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
877 &entry, &key->info.shared.object, &pindex, &prot,
878 &wired) != KERN_SUCCESS) {
882 if ((share == PROCESS_SHARE) ||
883 (share == AUTO_SHARE &&
884 VM_INHERIT_SHARE == entry->inheritance)) {
886 key->info.shared.offset = (vm_offset_t)addr -
887 entry->start + entry->offset;
888 vm_object_reference(key->info.shared.object);
891 key->info.private.vs = td->td_proc->p_vmspace;
892 key->info.private.addr = (uintptr_t)addr;
894 vm_map_lookup_done(map, entry);
905 umtx_key_release(struct umtx_key *key)
908 vm_object_deallocate(key->info.shared.object);
911 #ifdef COMPAT_FREEBSD10
913 * Lock a umtx object.
916 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
917 const struct timespec *timeout)
919 struct abs_timeout timo;
927 abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
930 * Care must be exercised when dealing with umtx structure. It
931 * can fault on any access.
935 * Try the uncontested case. This should be done in userland.
937 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
939 /* The acquire succeeded. */
940 if (owner == UMTX_UNOWNED)
943 /* The address was invalid. */
947 /* If no one owns it but it is contested try to acquire it. */
948 if (owner == UMTX_CONTESTED) {
949 owner = casuword(&umtx->u_owner,
950 UMTX_CONTESTED, id | UMTX_CONTESTED);
952 if (owner == UMTX_CONTESTED)
955 /* The address was invalid. */
959 error = thread_check_susp(td, false);
963 /* If this failed the lock has changed, restart. */
968 * If we caught a signal, we have retried and now
974 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
975 AUTO_SHARE, &uq->uq_key)) != 0)
978 umtxq_lock(&uq->uq_key);
979 umtxq_busy(&uq->uq_key);
981 umtxq_unbusy(&uq->uq_key);
982 umtxq_unlock(&uq->uq_key);
985 * Set the contested bit so that a release in user space
986 * knows to use the system call for unlock. If this fails
987 * either some one else has acquired the lock or it has been
990 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
992 /* The address was invalid. */
994 umtxq_lock(&uq->uq_key);
996 umtxq_unlock(&uq->uq_key);
997 umtx_key_release(&uq->uq_key);
1002 * We set the contested bit, sleep. Otherwise the lock changed
1003 * and we need to retry or we lost a race to the thread
1004 * unlocking the umtx.
1006 umtxq_lock(&uq->uq_key);
1008 error = umtxq_sleep(uq, "umtx", timeout == NULL ? NULL :
1011 umtxq_unlock(&uq->uq_key);
1012 umtx_key_release(&uq->uq_key);
1015 error = thread_check_susp(td, false);
1018 if (timeout == NULL) {
1019 /* Mutex locking is restarted if it is interrupted. */
1023 /* Timed-locking is not restarted. */
1024 if (error == ERESTART)
1031 * Unlock a umtx object.
1034 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
1036 struct umtx_key key;
1043 * Make sure we own this mtx.
1045 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
1049 if ((owner & ~UMTX_CONTESTED) != id)
1052 /* This should be done in userland */
1053 if ((owner & UMTX_CONTESTED) == 0) {
1054 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
1062 /* We should only ever be in here for contested locks */
1063 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1069 count = umtxq_count(&key);
1073 * When unlocking the umtx, it must be marked as unowned if
1074 * there is zero or one thread only waiting for it.
1075 * Otherwise, it must be marked as contested.
1077 old = casuword(&umtx->u_owner, owner,
1078 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
1080 umtxq_signal(&key,1);
1083 umtx_key_release(&key);
1091 #ifdef COMPAT_FREEBSD32
1094 * Lock a umtx object.
1097 do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id,
1098 const struct timespec *timeout)
1100 struct abs_timeout timo;
1108 if (timeout != NULL)
1109 abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
1112 * Care must be exercised when dealing with umtx structure. It
1113 * can fault on any access.
1117 * Try the uncontested case. This should be done in userland.
1119 owner = casuword32(m, UMUTEX_UNOWNED, id);
1121 /* The acquire succeeded. */
1122 if (owner == UMUTEX_UNOWNED)
1125 /* The address was invalid. */
1129 /* If no one owns it but it is contested try to acquire it. */
1130 if (owner == UMUTEX_CONTESTED) {
1131 owner = casuword32(m,
1132 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1133 if (owner == UMUTEX_CONTESTED)
1136 /* The address was invalid. */
1140 error = thread_check_susp(td, false);
1144 /* If this failed the lock has changed, restart. */
1149 * If we caught a signal, we have retried and now
1155 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
1156 AUTO_SHARE, &uq->uq_key)) != 0)
1159 umtxq_lock(&uq->uq_key);
1160 umtxq_busy(&uq->uq_key);
1162 umtxq_unbusy(&uq->uq_key);
1163 umtxq_unlock(&uq->uq_key);
1166 * Set the contested bit so that a release in user space
1167 * knows to use the system call for unlock. If this fails
1168 * either some one else has acquired the lock or it has been
1171 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
1173 /* The address was invalid. */
1175 umtxq_lock(&uq->uq_key);
1177 umtxq_unlock(&uq->uq_key);
1178 umtx_key_release(&uq->uq_key);
1183 * We set the contested bit, sleep. Otherwise the lock changed
1184 * and we need to retry or we lost a race to the thread
1185 * unlocking the umtx.
1187 umtxq_lock(&uq->uq_key);
1189 error = umtxq_sleep(uq, "umtx", timeout == NULL ?
1192 umtxq_unlock(&uq->uq_key);
1193 umtx_key_release(&uq->uq_key);
1196 error = thread_check_susp(td, false);
1199 if (timeout == NULL) {
1200 /* Mutex locking is restarted if it is interrupted. */
1204 /* Timed-locking is not restarted. */
1205 if (error == ERESTART)
1212 * Unlock a umtx object.
1215 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
1217 struct umtx_key key;
1224 * Make sure we own this mtx.
1226 owner = fuword32(m);
1230 if ((owner & ~UMUTEX_CONTESTED) != id)
1233 /* This should be done in userland */
1234 if ((owner & UMUTEX_CONTESTED) == 0) {
1235 old = casuword32(m, owner, UMUTEX_UNOWNED);
1243 /* We should only ever be in here for contested locks */
1244 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1250 count = umtxq_count(&key);
1254 * When unlocking the umtx, it must be marked as unowned if
1255 * there is zero or one thread only waiting for it.
1256 * Otherwise, it must be marked as contested.
1258 old = casuword32(m, owner,
1259 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1261 umtxq_signal(&key,1);
1264 umtx_key_release(&key);
1271 #endif /* COMPAT_FREEBSD32 */
1272 #endif /* COMPAT_FREEBSD10 */
1275 * Fetch and compare value, sleep on the address if value is not changed.
1278 do_wait(struct thread *td, void *addr, u_long id,
1279 struct _umtx_time *timeout, int compat32, int is_private)
1281 struct abs_timeout timo;
1288 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
1289 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
1292 if (timeout != NULL)
1293 abs_timeout_init2(&timo, timeout);
1295 umtxq_lock(&uq->uq_key);
1297 umtxq_unlock(&uq->uq_key);
1298 if (compat32 == 0) {
1299 error = fueword(addr, &tmp);
1303 error = fueword32(addr, &tmp32);
1309 umtxq_lock(&uq->uq_key);
1312 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
1314 if ((uq->uq_flags & UQF_UMTXQ) == 0)
1318 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
1321 umtxq_unlock(&uq->uq_key);
1322 umtx_key_release(&uq->uq_key);
1323 if (error == ERESTART)
1329 * Wake up threads sleeping on the specified address.
1332 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1334 struct umtx_key key;
1337 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1338 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1341 umtxq_signal(&key, n_wake);
1343 umtx_key_release(&key);
1348 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1351 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1352 struct _umtx_time *timeout, int mode)
1354 struct abs_timeout timo;
1356 uint32_t owner, old, id;
1362 if (timeout != NULL)
1363 abs_timeout_init2(&timo, timeout);
1366 * Care must be exercised when dealing with umtx structure. It
1367 * can fault on any access.
1370 rv = fueword32(&m->m_owner, &owner);
1373 if (mode == _UMUTEX_WAIT) {
1374 if (owner == UMUTEX_UNOWNED ||
1375 owner == UMUTEX_CONTESTED ||
1376 owner == UMUTEX_RB_OWNERDEAD ||
1377 owner == UMUTEX_RB_NOTRECOV)
1381 * Robust mutex terminated. Kernel duty is to
1382 * return EOWNERDEAD to the userspace. The
1383 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1384 * by the common userspace code.
1386 if (owner == UMUTEX_RB_OWNERDEAD) {
1387 rv = casueword32(&m->m_owner,
1388 UMUTEX_RB_OWNERDEAD, &owner,
1389 id | UMUTEX_CONTESTED);
1393 MPASS(owner == UMUTEX_RB_OWNERDEAD);
1394 return (EOWNERDEAD); /* success */
1397 rv = thread_check_susp(td, false);
1402 if (owner == UMUTEX_RB_NOTRECOV)
1403 return (ENOTRECOVERABLE);
1406 * Try the uncontested case. This should be
1409 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1411 /* The address was invalid. */
1415 /* The acquire succeeded. */
1417 MPASS(owner == UMUTEX_UNOWNED);
1422 * If no one owns it but it is contested try
1426 if (owner == UMUTEX_CONTESTED) {
1427 rv = casueword32(&m->m_owner,
1428 UMUTEX_CONTESTED, &owner,
1429 id | UMUTEX_CONTESTED);
1430 /* The address was invalid. */
1434 MPASS(owner == UMUTEX_CONTESTED);
1438 rv = thread_check_susp(td, false);
1444 * If this failed the lock has
1450 /* rv == 1 but not contested, likely store failure */
1451 rv = thread_check_susp(td, false);
1456 if (mode == _UMUTEX_TRY)
1460 * If we caught a signal, we have retried and now
1466 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1467 GET_SHARE(flags), &uq->uq_key)) != 0)
1470 umtxq_lock(&uq->uq_key);
1471 umtxq_busy(&uq->uq_key);
1473 umtxq_unlock(&uq->uq_key);
1476 * Set the contested bit so that a release in user space
1477 * knows to use the system call for unlock. If this fails
1478 * either some one else has acquired the lock or it has been
1481 rv = casueword32(&m->m_owner, owner, &old,
1482 owner | UMUTEX_CONTESTED);
1484 /* The address was invalid or casueword failed to store. */
1485 if (rv == -1 || rv == 1) {
1486 umtxq_lock(&uq->uq_key);
1488 umtxq_unbusy(&uq->uq_key);
1489 umtxq_unlock(&uq->uq_key);
1490 umtx_key_release(&uq->uq_key);
1494 rv = thread_check_susp(td, false);
1502 * We set the contested bit, sleep. Otherwise the lock changed
1503 * and we need to retry or we lost a race to the thread
1504 * unlocking the umtx.
1506 umtxq_lock(&uq->uq_key);
1507 umtxq_unbusy(&uq->uq_key);
1508 MPASS(old == owner);
1509 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1512 umtxq_unlock(&uq->uq_key);
1513 umtx_key_release(&uq->uq_key);
1516 error = thread_check_susp(td, false);
1523 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1526 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1528 struct umtx_key key;
1529 uint32_t owner, old, id, newlock;
1536 * Make sure we own this mtx.
1538 error = fueword32(&m->m_owner, &owner);
1542 if ((owner & ~UMUTEX_CONTESTED) != id)
1545 newlock = umtx_unlock_val(flags, rb);
1546 if ((owner & UMUTEX_CONTESTED) == 0) {
1547 error = casueword32(&m->m_owner, owner, &old, newlock);
1551 error = thread_check_susp(td, false);
1556 MPASS(old == owner);
1560 /* We should only ever be in here for contested locks */
1561 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1567 count = umtxq_count(&key);
1571 * When unlocking the umtx, it must be marked as unowned if
1572 * there is zero or one thread only waiting for it.
1573 * Otherwise, it must be marked as contested.
1576 newlock |= UMUTEX_CONTESTED;
1577 error = casueword32(&m->m_owner, owner, &old, newlock);
1579 umtxq_signal(&key, 1);
1582 umtx_key_release(&key);
1588 error = thread_check_susp(td, false);
1597 * Check if the mutex is available and wake up a waiter,
1598 * only for simple mutex.
1601 do_wake_umutex(struct thread *td, struct umutex *m)
1603 struct umtx_key key;
1610 error = fueword32(&m->m_owner, &owner);
1614 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1615 owner != UMUTEX_RB_NOTRECOV)
1618 error = fueword32(&m->m_flags, &flags);
1622 /* We should only ever be in here for contested locks */
1623 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1629 count = umtxq_count(&key);
1632 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1633 owner != UMUTEX_RB_NOTRECOV) {
1634 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1638 } else if (error == 1) {
1642 umtx_key_release(&key);
1643 error = thread_check_susp(td, false);
1651 if (error == 0 && count != 0) {
1652 MPASS((owner & ~UMUTEX_CONTESTED) == 0 ||
1653 owner == UMUTEX_RB_OWNERDEAD ||
1654 owner == UMUTEX_RB_NOTRECOV);
1655 umtxq_signal(&key, 1);
1659 umtx_key_release(&key);
1664 * Check if the mutex has waiters and tries to fix contention bit.
1667 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1669 struct umtx_key key;
1670 uint32_t owner, old;
1675 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1679 type = TYPE_NORMAL_UMUTEX;
1681 case UMUTEX_PRIO_INHERIT:
1682 type = TYPE_PI_UMUTEX;
1684 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1685 type = TYPE_PI_ROBUST_UMUTEX;
1687 case UMUTEX_PRIO_PROTECT:
1688 type = TYPE_PP_UMUTEX;
1690 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1691 type = TYPE_PP_ROBUST_UMUTEX;
1696 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1702 count = umtxq_count(&key);
1705 error = fueword32(&m->m_owner, &owner);
1710 * Only repair contention bit if there is a waiter, this means
1711 * the mutex is still being referenced by userland code,
1712 * otherwise don't update any memory.
1714 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0 &&
1715 (count > 1 || (count == 1 && (owner & ~UMUTEX_CONTESTED) != 0))) {
1716 error = casueword32(&m->m_owner, owner, &old,
1717 owner | UMUTEX_CONTESTED);
1723 MPASS(old == owner);
1727 error = thread_check_susp(td, false);
1731 if (error == EFAULT) {
1732 umtxq_signal(&key, INT_MAX);
1733 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1734 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1735 umtxq_signal(&key, 1);
1738 umtx_key_release(&key);
1742 static inline struct umtx_pi *
1743 umtx_pi_alloc(int flags)
1747 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1748 TAILQ_INIT(&pi->pi_blocked);
1749 atomic_add_int(&umtx_pi_allocated, 1);
1754 umtx_pi_free(struct umtx_pi *pi)
1756 uma_zfree(umtx_pi_zone, pi);
1757 atomic_add_int(&umtx_pi_allocated, -1);
1761 * Adjust the thread's position on a pi_state after its priority has been
1765 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1767 struct umtx_q *uq, *uq1, *uq2;
1770 mtx_assert(&umtx_lock, MA_OWNED);
1777 * Check if the thread needs to be moved on the blocked chain.
1778 * It needs to be moved if either its priority is lower than
1779 * the previous thread or higher than the next thread.
1781 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1782 uq2 = TAILQ_NEXT(uq, uq_lockq);
1783 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1784 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1786 * Remove thread from blocked chain and determine where
1787 * it should be moved to.
1789 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1790 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1791 td1 = uq1->uq_thread;
1792 MPASS(td1->td_proc->p_magic == P_MAGIC);
1793 if (UPRI(td1) > UPRI(td))
1798 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1800 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1805 static struct umtx_pi *
1806 umtx_pi_next(struct umtx_pi *pi)
1808 struct umtx_q *uq_owner;
1810 if (pi->pi_owner == NULL)
1812 uq_owner = pi->pi_owner->td_umtxq;
1813 if (uq_owner == NULL)
1815 return (uq_owner->uq_pi_blocked);
1819 * Floyd's Cycle-Finding Algorithm.
1822 umtx_pi_check_loop(struct umtx_pi *pi)
1824 struct umtx_pi *pi1; /* fast iterator */
1826 mtx_assert(&umtx_lock, MA_OWNED);
1831 pi = umtx_pi_next(pi);
1834 pi1 = umtx_pi_next(pi1);
1837 pi1 = umtx_pi_next(pi1);
1847 * Propagate priority when a thread is blocked on POSIX
1851 umtx_propagate_priority(struct thread *td)
1857 mtx_assert(&umtx_lock, MA_OWNED);
1860 pi = uq->uq_pi_blocked;
1863 if (umtx_pi_check_loop(pi))
1868 if (td == NULL || td == curthread)
1871 MPASS(td->td_proc != NULL);
1872 MPASS(td->td_proc->p_magic == P_MAGIC);
1875 if (td->td_lend_user_pri > pri)
1876 sched_lend_user_prio(td, pri);
1884 * Pick up the lock that td is blocked on.
1887 pi = uq->uq_pi_blocked;
1890 /* Resort td on the list if needed. */
1891 umtx_pi_adjust_thread(pi, td);
1896 * Unpropagate priority for a PI mutex when a thread blocked on
1897 * it is interrupted by signal or resumed by others.
1900 umtx_repropagate_priority(struct umtx_pi *pi)
1902 struct umtx_q *uq, *uq_owner;
1903 struct umtx_pi *pi2;
1906 mtx_assert(&umtx_lock, MA_OWNED);
1908 if (umtx_pi_check_loop(pi))
1910 while (pi != NULL && pi->pi_owner != NULL) {
1912 uq_owner = pi->pi_owner->td_umtxq;
1914 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1915 uq = TAILQ_FIRST(&pi2->pi_blocked);
1917 if (pri > UPRI(uq->uq_thread))
1918 pri = UPRI(uq->uq_thread);
1922 if (pri > uq_owner->uq_inherited_pri)
1923 pri = uq_owner->uq_inherited_pri;
1924 thread_lock(pi->pi_owner);
1925 sched_lend_user_prio(pi->pi_owner, pri);
1926 thread_unlock(pi->pi_owner);
1927 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1928 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1933 * Insert a PI mutex into owned list.
1936 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1938 struct umtx_q *uq_owner;
1940 uq_owner = owner->td_umtxq;
1941 mtx_assert(&umtx_lock, MA_OWNED);
1942 MPASS(pi->pi_owner == NULL);
1943 pi->pi_owner = owner;
1944 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1948 * Disown a PI mutex, and remove it from the owned list.
1951 umtx_pi_disown(struct umtx_pi *pi)
1954 mtx_assert(&umtx_lock, MA_OWNED);
1955 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1956 pi->pi_owner = NULL;
1960 * Claim ownership of a PI mutex.
1963 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1968 mtx_lock(&umtx_lock);
1969 if (pi->pi_owner == owner) {
1970 mtx_unlock(&umtx_lock);
1974 if (pi->pi_owner != NULL) {
1976 * userland may have already messed the mutex, sigh.
1978 mtx_unlock(&umtx_lock);
1981 umtx_pi_setowner(pi, owner);
1982 uq = TAILQ_FIRST(&pi->pi_blocked);
1984 pri = UPRI(uq->uq_thread);
1986 if (pri < UPRI(owner))
1987 sched_lend_user_prio(owner, pri);
1988 thread_unlock(owner);
1990 mtx_unlock(&umtx_lock);
1995 * Adjust a thread's order position in its blocked PI mutex,
1996 * this may result new priority propagating process.
1999 umtx_pi_adjust(struct thread *td, u_char oldpri)
2005 mtx_lock(&umtx_lock);
2007 * Pick up the lock that td is blocked on.
2009 pi = uq->uq_pi_blocked;
2011 umtx_pi_adjust_thread(pi, td);
2012 umtx_repropagate_priority(pi);
2014 mtx_unlock(&umtx_lock);
2018 * Sleep on a PI mutex.
2021 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
2022 const char *wmesg, struct abs_timeout *timo, bool shared)
2024 struct thread *td, *td1;
2028 struct umtxq_chain *uc;
2030 uc = umtxq_getchain(&pi->pi_key);
2034 KASSERT(td == curthread, ("inconsistent uq_thread"));
2035 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
2036 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
2038 mtx_lock(&umtx_lock);
2039 if (pi->pi_owner == NULL) {
2040 mtx_unlock(&umtx_lock);
2041 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
2042 mtx_lock(&umtx_lock);
2044 if (pi->pi_owner == NULL)
2045 umtx_pi_setowner(pi, td1);
2046 PROC_UNLOCK(td1->td_proc);
2050 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
2051 pri = UPRI(uq1->uq_thread);
2057 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
2059 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
2061 uq->uq_pi_blocked = pi;
2063 td->td_flags |= TDF_UPIBLOCKED;
2065 umtx_propagate_priority(td);
2066 mtx_unlock(&umtx_lock);
2067 umtxq_unbusy(&uq->uq_key);
2069 error = umtxq_sleep(uq, wmesg, timo);
2072 mtx_lock(&umtx_lock);
2073 uq->uq_pi_blocked = NULL;
2075 td->td_flags &= ~TDF_UPIBLOCKED;
2077 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
2078 umtx_repropagate_priority(pi);
2079 mtx_unlock(&umtx_lock);
2080 umtxq_unlock(&uq->uq_key);
2086 * Add reference count for a PI mutex.
2089 umtx_pi_ref(struct umtx_pi *pi)
2092 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
2097 * Decrease reference count for a PI mutex, if the counter
2098 * is decreased to zero, its memory space is freed.
2101 umtx_pi_unref(struct umtx_pi *pi)
2103 struct umtxq_chain *uc;
2105 uc = umtxq_getchain(&pi->pi_key);
2106 UMTXQ_LOCKED_ASSERT(uc);
2107 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
2108 if (--pi->pi_refcount == 0) {
2109 mtx_lock(&umtx_lock);
2110 if (pi->pi_owner != NULL)
2112 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
2113 ("blocked queue not empty"));
2114 mtx_unlock(&umtx_lock);
2115 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
2121 * Find a PI mutex in hash table.
2123 static struct umtx_pi *
2124 umtx_pi_lookup(struct umtx_key *key)
2126 struct umtxq_chain *uc;
2129 uc = umtxq_getchain(key);
2130 UMTXQ_LOCKED_ASSERT(uc);
2132 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
2133 if (umtx_key_match(&pi->pi_key, key)) {
2141 * Insert a PI mutex into hash table.
2144 umtx_pi_insert(struct umtx_pi *pi)
2146 struct umtxq_chain *uc;
2148 uc = umtxq_getchain(&pi->pi_key);
2149 UMTXQ_LOCKED_ASSERT(uc);
2150 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
2157 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
2158 struct _umtx_time *timeout, int try)
2160 struct abs_timeout timo;
2162 struct umtx_pi *pi, *new_pi;
2163 uint32_t id, old_owner, owner, old;
2169 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2170 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2174 if (timeout != NULL)
2175 abs_timeout_init2(&timo, timeout);
2177 umtxq_lock(&uq->uq_key);
2178 pi = umtx_pi_lookup(&uq->uq_key);
2180 new_pi = umtx_pi_alloc(M_NOWAIT);
2181 if (new_pi == NULL) {
2182 umtxq_unlock(&uq->uq_key);
2183 new_pi = umtx_pi_alloc(M_WAITOK);
2184 umtxq_lock(&uq->uq_key);
2185 pi = umtx_pi_lookup(&uq->uq_key);
2187 umtx_pi_free(new_pi);
2191 if (new_pi != NULL) {
2192 new_pi->pi_key = uq->uq_key;
2193 umtx_pi_insert(new_pi);
2198 umtxq_unlock(&uq->uq_key);
2201 * Care must be exercised when dealing with umtx structure. It
2202 * can fault on any access.
2206 * Try the uncontested case. This should be done in userland.
2208 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
2209 /* The address was invalid. */
2214 /* The acquire succeeded. */
2216 MPASS(owner == UMUTEX_UNOWNED);
2221 if (owner == UMUTEX_RB_NOTRECOV) {
2222 error = ENOTRECOVERABLE;
2227 * Avoid overwriting a possible error from sleep due
2228 * to the pending signal with suspension check result.
2231 error = thread_check_susp(td, true);
2236 /* If no one owns it but it is contested try to acquire it. */
2237 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
2239 rv = casueword32(&m->m_owner, owner, &owner,
2240 id | UMUTEX_CONTESTED);
2241 /* The address was invalid. */
2248 error = thread_check_susp(td, true);
2254 * If this failed the lock could
2261 MPASS(owner == old_owner);
2262 umtxq_lock(&uq->uq_key);
2263 umtxq_busy(&uq->uq_key);
2264 error = umtx_pi_claim(pi, td);
2265 umtxq_unbusy(&uq->uq_key);
2266 umtxq_unlock(&uq->uq_key);
2269 * Since we're going to return an
2270 * error, restore the m_owner to its
2271 * previous, unowned state to avoid
2272 * compounding the problem.
2274 (void)casuword32(&m->m_owner,
2275 id | UMUTEX_CONTESTED, old_owner);
2277 if (error == 0 && old_owner == UMUTEX_RB_OWNERDEAD)
2282 if ((owner & ~UMUTEX_CONTESTED) == id) {
2293 * If we caught a signal, we have retried and now
2299 umtxq_lock(&uq->uq_key);
2300 umtxq_busy(&uq->uq_key);
2301 umtxq_unlock(&uq->uq_key);
2304 * Set the contested bit so that a release in user space
2305 * knows to use the system call for unlock. If this fails
2306 * either some one else has acquired the lock or it has been
2309 rv = casueword32(&m->m_owner, owner, &old, owner |
2312 /* The address was invalid. */
2314 umtxq_unbusy_unlocked(&uq->uq_key);
2319 umtxq_unbusy_unlocked(&uq->uq_key);
2320 error = thread_check_susp(td, true);
2325 * The lock changed and we need to retry or we
2326 * lost a race to the thread unlocking the
2327 * umtx. Note that the UMUTEX_RB_OWNERDEAD
2328 * value for owner is impossible there.
2333 umtxq_lock(&uq->uq_key);
2335 /* We set the contested bit, sleep. */
2336 MPASS(old == owner);
2337 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
2338 "umtxpi", timeout == NULL ? NULL : &timo,
2339 (flags & USYNC_PROCESS_SHARED) != 0);
2343 error = thread_check_susp(td, false);
2348 umtxq_lock(&uq->uq_key);
2350 umtxq_unlock(&uq->uq_key);
2352 umtx_key_release(&uq->uq_key);
2357 * Unlock a PI mutex.
2360 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2362 struct umtx_key key;
2363 struct umtx_q *uq_first, *uq_first2, *uq_me;
2364 struct umtx_pi *pi, *pi2;
2365 uint32_t id, new_owner, old, owner;
2366 int count, error, pri;
2372 * Make sure we own this mtx.
2374 error = fueword32(&m->m_owner, &owner);
2378 if ((owner & ~UMUTEX_CONTESTED) != id)
2381 new_owner = umtx_unlock_val(flags, rb);
2383 /* This should be done in userland */
2384 if ((owner & UMUTEX_CONTESTED) == 0) {
2385 error = casueword32(&m->m_owner, owner, &old, new_owner);
2389 error = thread_check_susp(td, true);
2399 /* We should only ever be in here for contested locks */
2400 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2401 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2407 count = umtxq_count_pi(&key, &uq_first);
2408 if (uq_first != NULL) {
2409 mtx_lock(&umtx_lock);
2410 pi = uq_first->uq_pi_blocked;
2411 KASSERT(pi != NULL, ("pi == NULL?"));
2412 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2413 mtx_unlock(&umtx_lock);
2416 umtx_key_release(&key);
2417 /* userland messed the mutex */
2420 uq_me = td->td_umtxq;
2421 if (pi->pi_owner == td)
2423 /* get highest priority thread which is still sleeping. */
2424 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2425 while (uq_first != NULL &&
2426 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2427 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2430 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2431 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2432 if (uq_first2 != NULL) {
2433 if (pri > UPRI(uq_first2->uq_thread))
2434 pri = UPRI(uq_first2->uq_thread);
2438 sched_lend_user_prio(td, pri);
2440 mtx_unlock(&umtx_lock);
2442 umtxq_signal_thread(uq_first);
2444 pi = umtx_pi_lookup(&key);
2446 * A umtx_pi can exist if a signal or timeout removed the
2447 * last waiter from the umtxq, but there is still
2448 * a thread in do_lock_pi() holding the umtx_pi.
2452 * The umtx_pi can be unowned, such as when a thread
2453 * has just entered do_lock_pi(), allocated the
2454 * umtx_pi, and unlocked the umtxq.
2455 * If the current thread owns it, it must disown it.
2457 mtx_lock(&umtx_lock);
2458 if (pi->pi_owner == td)
2460 mtx_unlock(&umtx_lock);
2466 * When unlocking the umtx, it must be marked as unowned if
2467 * there is zero or one thread only waiting for it.
2468 * Otherwise, it must be marked as contested.
2472 new_owner |= UMUTEX_CONTESTED;
2474 error = casueword32(&m->m_owner, owner, &old, new_owner);
2476 error = thread_check_susp(td, false);
2480 umtxq_unbusy_unlocked(&key);
2481 umtx_key_release(&key);
2484 if (error == 0 && old != owner)
2493 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2494 struct _umtx_time *timeout, int try)
2496 struct abs_timeout timo;
2497 struct umtx_q *uq, *uq2;
2501 int error, pri, old_inherited_pri, su, rv;
2505 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2506 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2510 if (timeout != NULL)
2511 abs_timeout_init2(&timo, timeout);
2513 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2515 old_inherited_pri = uq->uq_inherited_pri;
2516 umtxq_lock(&uq->uq_key);
2517 umtxq_busy(&uq->uq_key);
2518 umtxq_unlock(&uq->uq_key);
2520 rv = fueword32(&m->m_ceilings[0], &ceiling);
2525 ceiling = RTP_PRIO_MAX - ceiling;
2526 if (ceiling > RTP_PRIO_MAX) {
2531 mtx_lock(&umtx_lock);
2532 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2533 mtx_unlock(&umtx_lock);
2537 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2538 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2540 if (uq->uq_inherited_pri < UPRI(td))
2541 sched_lend_user_prio(td, uq->uq_inherited_pri);
2544 mtx_unlock(&umtx_lock);
2546 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2547 id | UMUTEX_CONTESTED);
2548 /* The address was invalid. */
2554 MPASS(owner == UMUTEX_CONTESTED);
2559 if (owner == UMUTEX_RB_OWNERDEAD) {
2560 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2561 &owner, id | UMUTEX_CONTESTED);
2567 MPASS(owner == UMUTEX_RB_OWNERDEAD);
2568 error = EOWNERDEAD; /* success */
2573 * rv == 1, only check for suspension if we
2574 * did not already catched a signal. If we
2575 * get an error from the check, the same
2576 * condition is checked by the umtxq_sleep()
2577 * call below, so we should obliterate the
2578 * error to not skip the last loop iteration.
2581 error = thread_check_susp(td, false);
2590 } else if (owner == UMUTEX_RB_NOTRECOV) {
2591 error = ENOTRECOVERABLE;
2598 * If we caught a signal, we have retried and now
2604 umtxq_lock(&uq->uq_key);
2606 umtxq_unbusy(&uq->uq_key);
2607 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2610 umtxq_unlock(&uq->uq_key);
2612 mtx_lock(&umtx_lock);
2613 uq->uq_inherited_pri = old_inherited_pri;
2615 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2616 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2618 if (pri > UPRI(uq2->uq_thread))
2619 pri = UPRI(uq2->uq_thread);
2622 if (pri > uq->uq_inherited_pri)
2623 pri = uq->uq_inherited_pri;
2625 sched_lend_user_prio(td, pri);
2627 mtx_unlock(&umtx_lock);
2630 if (error != 0 && error != EOWNERDEAD) {
2631 mtx_lock(&umtx_lock);
2632 uq->uq_inherited_pri = old_inherited_pri;
2634 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2635 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2637 if (pri > UPRI(uq2->uq_thread))
2638 pri = UPRI(uq2->uq_thread);
2641 if (pri > uq->uq_inherited_pri)
2642 pri = uq->uq_inherited_pri;
2644 sched_lend_user_prio(td, pri);
2646 mtx_unlock(&umtx_lock);
2650 umtxq_unbusy_unlocked(&uq->uq_key);
2651 umtx_key_release(&uq->uq_key);
2656 * Unlock a PP mutex.
2659 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2661 struct umtx_key key;
2662 struct umtx_q *uq, *uq2;
2664 uint32_t id, owner, rceiling;
2665 int error, pri, new_inherited_pri, su;
2669 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2672 * Make sure we own this mtx.
2674 error = fueword32(&m->m_owner, &owner);
2678 if ((owner & ~UMUTEX_CONTESTED) != id)
2681 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2686 new_inherited_pri = PRI_MAX;
2688 rceiling = RTP_PRIO_MAX - rceiling;
2689 if (rceiling > RTP_PRIO_MAX)
2691 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2694 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2695 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2702 * For priority protected mutex, always set unlocked state
2703 * to UMUTEX_CONTESTED, so that userland always enters kernel
2704 * to lock the mutex, it is necessary because thread priority
2705 * has to be adjusted for such mutex.
2707 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2712 umtxq_signal(&key, 1);
2719 mtx_lock(&umtx_lock);
2721 uq->uq_inherited_pri = new_inherited_pri;
2723 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2724 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2726 if (pri > UPRI(uq2->uq_thread))
2727 pri = UPRI(uq2->uq_thread);
2730 if (pri > uq->uq_inherited_pri)
2731 pri = uq->uq_inherited_pri;
2733 sched_lend_user_prio(td, pri);
2735 mtx_unlock(&umtx_lock);
2737 umtx_key_release(&key);
2742 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2743 uint32_t *old_ceiling)
2746 uint32_t flags, id, owner, save_ceiling;
2749 error = fueword32(&m->m_flags, &flags);
2752 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2754 if (ceiling > RTP_PRIO_MAX)
2758 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2759 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2763 umtxq_lock(&uq->uq_key);
2764 umtxq_busy(&uq->uq_key);
2765 umtxq_unlock(&uq->uq_key);
2767 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2773 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2774 id | UMUTEX_CONTESTED);
2781 MPASS(owner == UMUTEX_CONTESTED);
2782 rv = suword32(&m->m_ceilings[0], ceiling);
2783 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2784 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2788 if ((owner & ~UMUTEX_CONTESTED) == id) {
2789 rv = suword32(&m->m_ceilings[0], ceiling);
2790 error = rv == 0 ? 0 : EFAULT;
2794 if (owner == UMUTEX_RB_OWNERDEAD) {
2797 } else if (owner == UMUTEX_RB_NOTRECOV) {
2798 error = ENOTRECOVERABLE;
2803 * If we caught a signal, we have retried and now
2810 * We set the contested bit, sleep. Otherwise the lock changed
2811 * and we need to retry or we lost a race to the thread
2812 * unlocking the umtx.
2814 umtxq_lock(&uq->uq_key);
2816 umtxq_unbusy(&uq->uq_key);
2817 error = umtxq_sleep(uq, "umtxpp", NULL);
2819 umtxq_unlock(&uq->uq_key);
2821 umtxq_lock(&uq->uq_key);
2823 umtxq_signal(&uq->uq_key, INT_MAX);
2824 umtxq_unbusy(&uq->uq_key);
2825 umtxq_unlock(&uq->uq_key);
2826 umtx_key_release(&uq->uq_key);
2827 if (error == 0 && old_ceiling != NULL) {
2828 rv = suword32(old_ceiling, save_ceiling);
2829 error = rv == 0 ? 0 : EFAULT;
2835 * Lock a userland POSIX mutex.
2838 do_lock_umutex(struct thread *td, struct umutex *m,
2839 struct _umtx_time *timeout, int mode)
2844 error = fueword32(&m->m_flags, &flags);
2848 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2850 error = do_lock_normal(td, m, flags, timeout, mode);
2852 case UMUTEX_PRIO_INHERIT:
2853 error = do_lock_pi(td, m, flags, timeout, mode);
2855 case UMUTEX_PRIO_PROTECT:
2856 error = do_lock_pp(td, m, flags, timeout, mode);
2861 if (timeout == NULL) {
2862 if (error == EINTR && mode != _UMUTEX_WAIT)
2865 /* Timed-locking is not restarted. */
2866 if (error == ERESTART)
2873 * Unlock a userland POSIX mutex.
2876 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2881 error = fueword32(&m->m_flags, &flags);
2885 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2887 return (do_unlock_normal(td, m, flags, rb));
2888 case UMUTEX_PRIO_INHERIT:
2889 return (do_unlock_pi(td, m, flags, rb));
2890 case UMUTEX_PRIO_PROTECT:
2891 return (do_unlock_pp(td, m, flags, rb));
2898 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2899 struct timespec *timeout, u_long wflags)
2901 struct abs_timeout timo;
2903 uint32_t flags, clockid, hasw;
2907 error = fueword32(&cv->c_flags, &flags);
2910 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2914 if ((wflags & CVWAIT_CLOCKID) != 0) {
2915 error = fueword32(&cv->c_clockid, &clockid);
2917 umtx_key_release(&uq->uq_key);
2920 if (clockid < CLOCK_REALTIME ||
2921 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2922 /* hmm, only HW clock id will work. */
2923 umtx_key_release(&uq->uq_key);
2927 clockid = CLOCK_REALTIME;
2930 umtxq_lock(&uq->uq_key);
2931 umtxq_busy(&uq->uq_key);
2933 umtxq_unlock(&uq->uq_key);
2936 * Set c_has_waiters to 1 before releasing user mutex, also
2937 * don't modify cache line when unnecessary.
2939 error = fueword32(&cv->c_has_waiters, &hasw);
2940 if (error == 0 && hasw == 0)
2941 suword32(&cv->c_has_waiters, 1);
2943 umtxq_unbusy_unlocked(&uq->uq_key);
2945 error = do_unlock_umutex(td, m, false);
2947 if (timeout != NULL)
2948 abs_timeout_init(&timo, clockid, (wflags & CVWAIT_ABSTIME) != 0,
2951 umtxq_lock(&uq->uq_key);
2953 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2957 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2961 * This must be timeout,interrupted by signal or
2962 * surprious wakeup, clear c_has_waiter flag when
2965 umtxq_busy(&uq->uq_key);
2966 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2967 int oldlen = uq->uq_cur_queue->length;
2970 umtxq_unlock(&uq->uq_key);
2971 suword32(&cv->c_has_waiters, 0);
2972 umtxq_lock(&uq->uq_key);
2975 umtxq_unbusy(&uq->uq_key);
2976 if (error == ERESTART)
2980 umtxq_unlock(&uq->uq_key);
2981 umtx_key_release(&uq->uq_key);
2986 * Signal a userland condition variable.
2989 do_cv_signal(struct thread *td, struct ucond *cv)
2991 struct umtx_key key;
2992 int error, cnt, nwake;
2995 error = fueword32(&cv->c_flags, &flags);
2998 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
3002 cnt = umtxq_count(&key);
3003 nwake = umtxq_signal(&key, 1);
3006 error = suword32(&cv->c_has_waiters, 0);
3013 umtx_key_release(&key);
3018 do_cv_broadcast(struct thread *td, struct ucond *cv)
3020 struct umtx_key key;
3024 error = fueword32(&cv->c_flags, &flags);
3027 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
3032 umtxq_signal(&key, INT_MAX);
3035 error = suword32(&cv->c_has_waiters, 0);
3039 umtxq_unbusy_unlocked(&key);
3041 umtx_key_release(&key);
3046 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
3047 struct _umtx_time *timeout)
3049 struct abs_timeout timo;
3051 uint32_t flags, wrflags;
3052 int32_t state, oldstate;
3053 int32_t blocked_readers;
3054 int error, error1, rv;
3057 error = fueword32(&rwlock->rw_flags, &flags);
3060 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3064 if (timeout != NULL)
3065 abs_timeout_init2(&timo, timeout);
3067 wrflags = URWLOCK_WRITE_OWNER;
3068 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
3069 wrflags |= URWLOCK_WRITE_WAITERS;
3072 rv = fueword32(&rwlock->rw_state, &state);
3074 umtx_key_release(&uq->uq_key);
3078 /* try to lock it */
3079 while (!(state & wrflags)) {
3080 if (__predict_false(URWLOCK_READER_COUNT(state) ==
3081 URWLOCK_MAX_READERS)) {
3082 umtx_key_release(&uq->uq_key);
3085 rv = casueword32(&rwlock->rw_state, state,
3086 &oldstate, state + 1);
3088 umtx_key_release(&uq->uq_key);
3092 MPASS(oldstate == state);
3093 umtx_key_release(&uq->uq_key);
3096 error = thread_check_susp(td, true);
3105 /* grab monitor lock */
3106 umtxq_lock(&uq->uq_key);
3107 umtxq_busy(&uq->uq_key);
3108 umtxq_unlock(&uq->uq_key);
3111 * re-read the state, in case it changed between the try-lock above
3112 * and the check below
3114 rv = fueword32(&rwlock->rw_state, &state);
3118 /* set read contention bit */
3119 while (error == 0 && (state & wrflags) &&
3120 !(state & URWLOCK_READ_WAITERS)) {
3121 rv = casueword32(&rwlock->rw_state, state,
3122 &oldstate, state | URWLOCK_READ_WAITERS);
3128 MPASS(oldstate == state);
3132 error = thread_check_susp(td, false);
3137 umtxq_unbusy_unlocked(&uq->uq_key);
3141 /* state is changed while setting flags, restart */
3142 if (!(state & wrflags)) {
3143 umtxq_unbusy_unlocked(&uq->uq_key);
3144 error = thread_check_susp(td, true);
3152 * Contention bit is set, before sleeping, increase
3153 * read waiter count.
3155 rv = fueword32(&rwlock->rw_blocked_readers,
3158 umtxq_unbusy_unlocked(&uq->uq_key);
3162 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
3164 while (state & wrflags) {
3165 umtxq_lock(&uq->uq_key);
3167 umtxq_unbusy(&uq->uq_key);
3169 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
3172 umtxq_busy(&uq->uq_key);
3174 umtxq_unlock(&uq->uq_key);
3177 rv = fueword32(&rwlock->rw_state, &state);
3184 /* decrease read waiter count, and may clear read contention bit */
3185 rv = fueword32(&rwlock->rw_blocked_readers,
3188 umtxq_unbusy_unlocked(&uq->uq_key);
3192 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
3193 if (blocked_readers == 1) {
3194 rv = fueword32(&rwlock->rw_state, &state);
3196 umtxq_unbusy_unlocked(&uq->uq_key);
3201 rv = casueword32(&rwlock->rw_state, state,
3202 &oldstate, state & ~URWLOCK_READ_WAITERS);
3208 MPASS(oldstate == state);
3212 error1 = thread_check_susp(td, false);
3221 umtxq_unbusy_unlocked(&uq->uq_key);
3225 umtx_key_release(&uq->uq_key);
3226 if (error == ERESTART)
3232 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
3234 struct abs_timeout timo;
3237 int32_t state, oldstate;
3238 int32_t blocked_writers;
3239 int32_t blocked_readers;
3240 int error, error1, rv;
3243 error = fueword32(&rwlock->rw_flags, &flags);
3246 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3250 if (timeout != NULL)
3251 abs_timeout_init2(&timo, timeout);
3253 blocked_readers = 0;
3255 rv = fueword32(&rwlock->rw_state, &state);
3257 umtx_key_release(&uq->uq_key);
3260 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
3261 URWLOCK_READER_COUNT(state) == 0) {
3262 rv = casueword32(&rwlock->rw_state, state,
3263 &oldstate, state | URWLOCK_WRITE_OWNER);
3265 umtx_key_release(&uq->uq_key);
3269 MPASS(oldstate == state);
3270 umtx_key_release(&uq->uq_key);
3274 error = thread_check_susp(td, true);
3280 if ((state & (URWLOCK_WRITE_OWNER |
3281 URWLOCK_WRITE_WAITERS)) == 0 &&
3282 blocked_readers != 0) {
3283 umtxq_lock(&uq->uq_key);
3284 umtxq_busy(&uq->uq_key);
3285 umtxq_signal_queue(&uq->uq_key, INT_MAX,
3287 umtxq_unbusy(&uq->uq_key);
3288 umtxq_unlock(&uq->uq_key);
3294 /* grab monitor lock */
3295 umtxq_lock(&uq->uq_key);
3296 umtxq_busy(&uq->uq_key);
3297 umtxq_unlock(&uq->uq_key);
3300 * Re-read the state, in case it changed between the
3301 * try-lock above and the check below.
3303 rv = fueword32(&rwlock->rw_state, &state);
3307 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
3308 URWLOCK_READER_COUNT(state) != 0) &&
3309 (state & URWLOCK_WRITE_WAITERS) == 0) {
3310 rv = casueword32(&rwlock->rw_state, state,
3311 &oldstate, state | URWLOCK_WRITE_WAITERS);
3317 MPASS(oldstate == state);
3321 error = thread_check_susp(td, false);
3326 umtxq_unbusy_unlocked(&uq->uq_key);
3330 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
3331 URWLOCK_READER_COUNT(state) == 0) {
3332 umtxq_unbusy_unlocked(&uq->uq_key);
3333 error = thread_check_susp(td, false);
3339 rv = fueword32(&rwlock->rw_blocked_writers,
3342 umtxq_unbusy_unlocked(&uq->uq_key);
3346 suword32(&rwlock->rw_blocked_writers, blocked_writers + 1);
3348 while ((state & URWLOCK_WRITE_OWNER) ||
3349 URWLOCK_READER_COUNT(state) != 0) {
3350 umtxq_lock(&uq->uq_key);
3351 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3352 umtxq_unbusy(&uq->uq_key);
3354 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
3357 umtxq_busy(&uq->uq_key);
3358 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3359 umtxq_unlock(&uq->uq_key);
3362 rv = fueword32(&rwlock->rw_state, &state);
3369 rv = fueword32(&rwlock->rw_blocked_writers,
3372 umtxq_unbusy_unlocked(&uq->uq_key);
3376 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
3377 if (blocked_writers == 1) {
3378 rv = fueword32(&rwlock->rw_state, &state);
3380 umtxq_unbusy_unlocked(&uq->uq_key);
3385 rv = casueword32(&rwlock->rw_state, state,
3386 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3392 MPASS(oldstate == state);
3396 error1 = thread_check_susp(td, false);
3398 * We are leaving the URWLOCK_WRITE_WAITERS
3399 * behind, but this should not harm the
3408 rv = fueword32(&rwlock->rw_blocked_readers,
3411 umtxq_unbusy_unlocked(&uq->uq_key);
3416 blocked_readers = 0;
3418 umtxq_unbusy_unlocked(&uq->uq_key);
3421 umtx_key_release(&uq->uq_key);
3422 if (error == ERESTART)
3428 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3432 int32_t state, oldstate;
3433 int error, rv, q, count;
3436 error = fueword32(&rwlock->rw_flags, &flags);
3439 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3443 error = fueword32(&rwlock->rw_state, &state);
3448 if (state & URWLOCK_WRITE_OWNER) {
3450 rv = casueword32(&rwlock->rw_state, state,
3451 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3458 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3462 error = thread_check_susp(td, true);
3468 } else if (URWLOCK_READER_COUNT(state) != 0) {
3470 rv = casueword32(&rwlock->rw_state, state,
3471 &oldstate, state - 1);
3478 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3482 error = thread_check_susp(td, true);
3495 if (!(flags & URWLOCK_PREFER_READER)) {
3496 if (state & URWLOCK_WRITE_WAITERS) {
3498 q = UMTX_EXCLUSIVE_QUEUE;
3499 } else if (state & URWLOCK_READ_WAITERS) {
3501 q = UMTX_SHARED_QUEUE;
3504 if (state & URWLOCK_READ_WAITERS) {
3506 q = UMTX_SHARED_QUEUE;
3507 } else if (state & URWLOCK_WRITE_WAITERS) {
3509 q = UMTX_EXCLUSIVE_QUEUE;
3514 umtxq_lock(&uq->uq_key);
3515 umtxq_busy(&uq->uq_key);
3516 umtxq_signal_queue(&uq->uq_key, count, q);
3517 umtxq_unbusy(&uq->uq_key);
3518 umtxq_unlock(&uq->uq_key);
3521 umtx_key_release(&uq->uq_key);
3525 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3527 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3529 struct abs_timeout timo;
3531 uint32_t flags, count, count1;
3535 error = fueword32(&sem->_flags, &flags);
3538 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3542 if (timeout != NULL)
3543 abs_timeout_init2(&timo, timeout);
3546 umtxq_lock(&uq->uq_key);
3547 umtxq_busy(&uq->uq_key);
3549 umtxq_unlock(&uq->uq_key);
3550 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3552 rv1 = fueword32(&sem->_count, &count);
3553 if (rv == -1 || (rv == 0 && (rv1 == -1 || count != 0)) ||
3554 (rv == 1 && count1 == 0)) {
3555 umtxq_lock(&uq->uq_key);
3556 umtxq_unbusy(&uq->uq_key);
3558 umtxq_unlock(&uq->uq_key);
3560 rv = thread_check_susp(td, true);
3568 error = rv == -1 ? EFAULT : 0;
3571 umtxq_lock(&uq->uq_key);
3572 umtxq_unbusy(&uq->uq_key);
3574 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3576 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3580 /* A relative timeout cannot be restarted. */
3581 if (error == ERESTART && timeout != NULL &&
3582 (timeout->_flags & UMTX_ABSTIME) == 0)
3585 umtxq_unlock(&uq->uq_key);
3587 umtx_key_release(&uq->uq_key);
3592 * Signal a userland semaphore.
3595 do_sem_wake(struct thread *td, struct _usem *sem)
3597 struct umtx_key key;
3601 error = fueword32(&sem->_flags, &flags);
3604 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3608 cnt = umtxq_count(&key);
3611 * Check if count is greater than 0, this means the memory is
3612 * still being referenced by user code, so we can safely
3613 * update _has_waiters flag.
3617 error = suword32(&sem->_has_waiters, 0);
3622 umtxq_signal(&key, 1);
3626 umtx_key_release(&key);
3632 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3634 struct abs_timeout timo;
3636 uint32_t count, flags;
3640 flags = fuword32(&sem->_flags);
3641 if (timeout != NULL)
3642 abs_timeout_init2(&timo, timeout);
3645 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3648 umtxq_lock(&uq->uq_key);
3649 umtxq_busy(&uq->uq_key);
3651 umtxq_unlock(&uq->uq_key);
3652 rv = fueword32(&sem->_count, &count);
3654 umtxq_lock(&uq->uq_key);
3655 umtxq_unbusy(&uq->uq_key);
3657 umtxq_unlock(&uq->uq_key);
3658 umtx_key_release(&uq->uq_key);
3662 if (USEM_COUNT(count) != 0) {
3663 umtxq_lock(&uq->uq_key);
3664 umtxq_unbusy(&uq->uq_key);
3666 umtxq_unlock(&uq->uq_key);
3667 umtx_key_release(&uq->uq_key);
3670 if (count == USEM_HAS_WAITERS)
3672 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3675 umtxq_lock(&uq->uq_key);
3676 umtxq_unbusy(&uq->uq_key);
3678 umtxq_unlock(&uq->uq_key);
3679 umtx_key_release(&uq->uq_key);
3682 rv = thread_check_susp(td, true);
3687 umtxq_lock(&uq->uq_key);
3688 umtxq_unbusy(&uq->uq_key);
3690 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3692 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3696 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3697 /* A relative timeout cannot be restarted. */
3698 if (error == ERESTART)
3700 if (error == EINTR) {
3701 abs_timeout_update(&timo);
3702 timespecsub(&timo.end, &timo.cur,
3703 &timeout->_timeout);
3707 umtxq_unlock(&uq->uq_key);
3708 umtx_key_release(&uq->uq_key);
3713 * Signal a userland semaphore.
3716 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3718 struct umtx_key key;
3720 uint32_t count, flags;
3722 rv = fueword32(&sem->_flags, &flags);
3725 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3729 cnt = umtxq_count(&key);
3732 * If this was the last sleeping thread, clear the waiters
3737 rv = fueword32(&sem->_count, &count);
3738 while (rv != -1 && count & USEM_HAS_WAITERS) {
3739 rv = casueword32(&sem->_count, count, &count,
3740 count & ~USEM_HAS_WAITERS);
3742 rv = thread_check_susp(td, true);
3755 umtxq_signal(&key, 1);
3759 umtx_key_release(&key);
3763 #ifdef COMPAT_FREEBSD10
3765 freebsd10__umtx_lock(struct thread *td, struct freebsd10__umtx_lock_args *uap)
3767 return (do_lock_umtx(td, uap->umtx, td->td_tid, 0));
3771 freebsd10__umtx_unlock(struct thread *td,
3772 struct freebsd10__umtx_unlock_args *uap)
3774 return (do_unlock_umtx(td, uap->umtx, td->td_tid));
3779 umtx_copyin_timeout(const void *uaddr, struct timespec *tsp)
3783 error = copyin(uaddr, tsp, sizeof(*tsp));
3785 if (tsp->tv_sec < 0 ||
3786 tsp->tv_nsec >= 1000000000 ||
3794 umtx_copyin_umtx_time(const void *uaddr, size_t size, struct _umtx_time *tp)
3798 if (size <= sizeof(tp->_timeout)) {
3799 tp->_clockid = CLOCK_REALTIME;
3801 error = copyin(uaddr, &tp->_timeout, sizeof(tp->_timeout));
3803 error = copyin(uaddr, tp, sizeof(*tp));
3806 if (tp->_timeout.tv_sec < 0 ||
3807 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3813 umtx_copyin_robust_lists(const void *uaddr, size_t size,
3814 struct umtx_robust_lists_params *rb)
3817 if (size > sizeof(*rb))
3819 return (copyin(uaddr, rb, size));
3823 umtx_copyout_timeout(void *uaddr, size_t sz, struct timespec *tsp)
3827 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
3828 * and we're only called if sz >= sizeof(timespec) as supplied in the
3831 KASSERT(sz >= sizeof(*tsp),
3832 ("umtx_copyops specifies incorrect sizes"));
3834 return (copyout(tsp, uaddr, sizeof(*tsp)));
3837 #ifdef COMPAT_FREEBSD10
3839 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap,
3840 const struct umtx_copyops *ops)
3842 struct timespec *ts, timeout;
3845 /* Allow a null timespec (wait forever). */
3846 if (uap->uaddr2 == NULL)
3849 error = ops->copyin_timeout(uap->uaddr2, &timeout);
3854 #ifdef COMPAT_FREEBSD32
3856 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
3858 return (do_lock_umtx(td, uap->obj, uap->val, ts));
3862 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap,
3863 const struct umtx_copyops *ops)
3865 #ifdef COMPAT_FREEBSD32
3867 return (do_unlock_umtx32(td, uap->obj, uap->val));
3869 return (do_unlock_umtx(td, uap->obj, uap->val));
3871 #endif /* COMPAT_FREEBSD10 */
3873 #if !defined(COMPAT_FREEBSD10)
3875 __umtx_op_unimpl(struct thread *td __unused, struct _umtx_op_args *uap __unused,
3876 const struct umtx_copyops *ops __unused)
3878 return (EOPNOTSUPP);
3880 #endif /* COMPAT_FREEBSD10 */
3883 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap,
3884 const struct umtx_copyops *ops)
3886 struct _umtx_time timeout, *tm_p;
3889 if (uap->uaddr2 == NULL)
3892 error = ops->copyin_umtx_time(
3893 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3898 return (do_wait(td, uap->obj, uap->val, tm_p, ops->compat32, 0));
3902 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap,
3903 const struct umtx_copyops *ops)
3905 struct _umtx_time timeout, *tm_p;
3908 if (uap->uaddr2 == NULL)
3911 error = ops->copyin_umtx_time(
3912 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3917 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3921 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap,
3922 const struct umtx_copyops *ops)
3924 struct _umtx_time *tm_p, timeout;
3927 if (uap->uaddr2 == NULL)
3930 error = ops->copyin_umtx_time(
3931 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3936 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3940 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap,
3941 const struct umtx_copyops *ops __unused)
3944 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3947 #define BATCH_SIZE 128
3949 __umtx_op_nwake_private_native(struct thread *td, struct _umtx_op_args *uap)
3951 char *uaddrs[BATCH_SIZE], **upp;
3952 int count, error, i, pos, tocopy;
3954 upp = (char **)uap->obj;
3956 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3958 tocopy = MIN(count, BATCH_SIZE);
3959 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3962 for (i = 0; i < tocopy; ++i) {
3963 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3971 __umtx_op_nwake_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3973 uint32_t uaddrs[BATCH_SIZE], *upp;
3974 int count, error, i, pos, tocopy;
3976 upp = (uint32_t *)uap->obj;
3978 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3980 tocopy = MIN(count, BATCH_SIZE);
3981 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
3984 for (i = 0; i < tocopy; ++i) {
3985 kern_umtx_wake(td, (void *)(uintptr_t)uaddrs[i],
3994 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap,
3995 const struct umtx_copyops *ops)
3999 return (__umtx_op_nwake_private_compat32(td, uap));
4000 return (__umtx_op_nwake_private_native(td, uap));
4004 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap,
4005 const struct umtx_copyops *ops __unused)
4008 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
4012 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap,
4013 const struct umtx_copyops *ops)
4015 struct _umtx_time *tm_p, timeout;
4018 /* Allow a null timespec (wait forever). */
4019 if (uap->uaddr2 == NULL)
4022 error = ops->copyin_umtx_time(
4023 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4028 return (do_lock_umutex(td, uap->obj, tm_p, 0));
4032 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap,
4033 const struct umtx_copyops *ops __unused)
4036 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
4040 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap,
4041 const struct umtx_copyops *ops)
4043 struct _umtx_time *tm_p, timeout;
4046 /* Allow a null timespec (wait forever). */
4047 if (uap->uaddr2 == NULL)
4050 error = ops->copyin_umtx_time(
4051 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4056 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
4060 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap,
4061 const struct umtx_copyops *ops __unused)
4064 return (do_wake_umutex(td, uap->obj));
4068 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap,
4069 const struct umtx_copyops *ops __unused)
4072 return (do_unlock_umutex(td, uap->obj, false));
4076 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap,
4077 const struct umtx_copyops *ops __unused)
4080 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
4084 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap,
4085 const struct umtx_copyops *ops)
4087 struct timespec *ts, timeout;
4090 /* Allow a null timespec (wait forever). */
4091 if (uap->uaddr2 == NULL)
4094 error = ops->copyin_timeout(uap->uaddr2, &timeout);
4099 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
4103 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap,
4104 const struct umtx_copyops *ops __unused)
4107 return (do_cv_signal(td, uap->obj));
4111 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap,
4112 const struct umtx_copyops *ops __unused)
4115 return (do_cv_broadcast(td, uap->obj));
4119 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap,
4120 const struct umtx_copyops *ops)
4122 struct _umtx_time timeout;
4125 /* Allow a null timespec (wait forever). */
4126 if (uap->uaddr2 == NULL) {
4127 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4129 error = ops->copyin_umtx_time(uap->uaddr2,
4130 (size_t)uap->uaddr1, &timeout);
4133 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4139 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap,
4140 const struct umtx_copyops *ops)
4142 struct _umtx_time timeout;
4145 /* Allow a null timespec (wait forever). */
4146 if (uap->uaddr2 == NULL) {
4147 error = do_rw_wrlock(td, uap->obj, 0);
4149 error = ops->copyin_umtx_time(uap->uaddr2,
4150 (size_t)uap->uaddr1, &timeout);
4154 error = do_rw_wrlock(td, uap->obj, &timeout);
4160 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap,
4161 const struct umtx_copyops *ops __unused)
4164 return (do_rw_unlock(td, uap->obj));
4167 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4169 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap,
4170 const struct umtx_copyops *ops)
4172 struct _umtx_time *tm_p, timeout;
4175 /* Allow a null timespec (wait forever). */
4176 if (uap->uaddr2 == NULL)
4179 error = ops->copyin_umtx_time(
4180 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4185 return (do_sem_wait(td, uap->obj, tm_p));
4189 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap,
4190 const struct umtx_copyops *ops __unused)
4193 return (do_sem_wake(td, uap->obj));
4198 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap,
4199 const struct umtx_copyops *ops __unused)
4202 return (do_wake2_umutex(td, uap->obj, uap->val));
4206 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap,
4207 const struct umtx_copyops *ops)
4209 struct _umtx_time *tm_p, timeout;
4213 /* Allow a null timespec (wait forever). */
4214 if (uap->uaddr2 == NULL) {
4218 uasize = (size_t)uap->uaddr1;
4219 error = ops->copyin_umtx_time(uap->uaddr2, uasize, &timeout);
4224 error = do_sem2_wait(td, uap->obj, tm_p);
4225 if (error == EINTR && uap->uaddr2 != NULL &&
4226 (timeout._flags & UMTX_ABSTIME) == 0 &&
4227 uasize >= ops->umtx_time_sz + ops->timespec_sz) {
4228 error = ops->copyout_timeout(
4229 (void *)((uintptr_t)uap->uaddr2 + ops->umtx_time_sz),
4230 uasize - ops->umtx_time_sz, &timeout._timeout);
4240 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap,
4241 const struct umtx_copyops *ops __unused)
4244 return (do_sem2_wake(td, uap->obj));
4247 #define USHM_OBJ_UMTX(o) \
4248 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
4250 #define USHMF_REG_LINKED 0x0001
4251 #define USHMF_OBJ_LINKED 0x0002
4252 struct umtx_shm_reg {
4253 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
4254 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
4255 struct umtx_key ushm_key;
4256 struct ucred *ushm_cred;
4257 struct shmfd *ushm_obj;
4262 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
4263 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
4265 static uma_zone_t umtx_shm_reg_zone;
4266 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
4267 static struct mtx umtx_shm_lock;
4268 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
4269 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
4271 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
4274 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
4276 struct umtx_shm_reg_head d;
4277 struct umtx_shm_reg *reg, *reg1;
4280 mtx_lock(&umtx_shm_lock);
4281 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
4282 mtx_unlock(&umtx_shm_lock);
4283 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
4284 TAILQ_REMOVE(&d, reg, ushm_reg_link);
4285 umtx_shm_free_reg(reg);
4289 static struct task umtx_shm_reg_delfree_task =
4290 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
4292 static struct umtx_shm_reg *
4293 umtx_shm_find_reg_locked(const struct umtx_key *key)
4295 struct umtx_shm_reg *reg;
4296 struct umtx_shm_reg_head *reg_head;
4298 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
4299 mtx_assert(&umtx_shm_lock, MA_OWNED);
4300 reg_head = &umtx_shm_registry[key->hash];
4301 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
4302 KASSERT(reg->ushm_key.shared,
4303 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
4304 if (reg->ushm_key.info.shared.object ==
4305 key->info.shared.object &&
4306 reg->ushm_key.info.shared.offset ==
4307 key->info.shared.offset) {
4308 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
4309 KASSERT(reg->ushm_refcnt > 0,
4310 ("reg %p refcnt 0 onlist", reg));
4311 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
4312 ("reg %p not linked", reg));
4320 static struct umtx_shm_reg *
4321 umtx_shm_find_reg(const struct umtx_key *key)
4323 struct umtx_shm_reg *reg;
4325 mtx_lock(&umtx_shm_lock);
4326 reg = umtx_shm_find_reg_locked(key);
4327 mtx_unlock(&umtx_shm_lock);
4332 umtx_shm_free_reg(struct umtx_shm_reg *reg)
4335 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
4336 crfree(reg->ushm_cred);
4337 shm_drop(reg->ushm_obj);
4338 uma_zfree(umtx_shm_reg_zone, reg);
4342 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
4346 mtx_assert(&umtx_shm_lock, MA_OWNED);
4347 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
4349 res = reg->ushm_refcnt == 0;
4351 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
4352 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
4353 reg, ushm_reg_link);
4354 reg->ushm_flags &= ~USHMF_REG_LINKED;
4356 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
4357 LIST_REMOVE(reg, ushm_obj_link);
4358 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
4365 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
4371 object = reg->ushm_obj->shm_object;
4372 VM_OBJECT_WLOCK(object);
4373 object->flags |= OBJ_UMTXDEAD;
4374 VM_OBJECT_WUNLOCK(object);
4376 mtx_lock(&umtx_shm_lock);
4377 dofree = umtx_shm_unref_reg_locked(reg, force);
4378 mtx_unlock(&umtx_shm_lock);
4380 umtx_shm_free_reg(reg);
4384 umtx_shm_object_init(vm_object_t object)
4387 LIST_INIT(USHM_OBJ_UMTX(object));
4391 umtx_shm_object_terminated(vm_object_t object)
4393 struct umtx_shm_reg *reg, *reg1;
4396 if (LIST_EMPTY(USHM_OBJ_UMTX(object)))
4400 mtx_lock(&umtx_shm_lock);
4401 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
4402 if (umtx_shm_unref_reg_locked(reg, true)) {
4403 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
4408 mtx_unlock(&umtx_shm_lock);
4410 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
4414 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
4415 struct umtx_shm_reg **res)
4417 struct umtx_shm_reg *reg, *reg1;
4421 reg = umtx_shm_find_reg(key);
4426 cred = td->td_ucred;
4427 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
4429 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
4430 reg->ushm_refcnt = 1;
4431 bcopy(key, ®->ushm_key, sizeof(*key));
4432 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR, false);
4433 reg->ushm_cred = crhold(cred);
4434 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
4436 umtx_shm_free_reg(reg);
4439 mtx_lock(&umtx_shm_lock);
4440 reg1 = umtx_shm_find_reg_locked(key);
4442 mtx_unlock(&umtx_shm_lock);
4443 umtx_shm_free_reg(reg);
4448 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
4449 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
4451 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
4452 mtx_unlock(&umtx_shm_lock);
4458 umtx_shm_alive(struct thread *td, void *addr)
4461 vm_map_entry_t entry;
4468 map = &td->td_proc->p_vmspace->vm_map;
4469 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
4470 &object, &pindex, &prot, &wired);
4471 if (res != KERN_SUCCESS)
4476 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
4477 vm_map_lookup_done(map, entry);
4486 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
4487 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
4488 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
4489 for (i = 0; i < nitems(umtx_shm_registry); i++)
4490 TAILQ_INIT(&umtx_shm_registry[i]);
4494 umtx_shm(struct thread *td, void *addr, u_int flags)
4496 struct umtx_key key;
4497 struct umtx_shm_reg *reg;
4501 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
4502 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
4504 if ((flags & UMTX_SHM_ALIVE) != 0)
4505 return (umtx_shm_alive(td, addr));
4506 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
4509 KASSERT(key.shared == 1, ("non-shared key"));
4510 if ((flags & UMTX_SHM_CREAT) != 0) {
4511 error = umtx_shm_create_reg(td, &key, ®);
4513 reg = umtx_shm_find_reg(&key);
4517 umtx_key_release(&key);
4520 KASSERT(reg != NULL, ("no reg"));
4521 if ((flags & UMTX_SHM_DESTROY) != 0) {
4522 umtx_shm_unref_reg(reg, true);
4526 error = mac_posixshm_check_open(td->td_ucred,
4527 reg->ushm_obj, FFLAGS(O_RDWR));
4530 error = shm_access(reg->ushm_obj, td->td_ucred,
4534 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
4536 shm_hold(reg->ushm_obj);
4537 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
4539 td->td_retval[0] = fd;
4543 umtx_shm_unref_reg(reg, false);
4548 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap,
4549 const struct umtx_copyops *ops __unused)
4552 return (umtx_shm(td, uap->uaddr1, uap->val));
4556 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap,
4557 const struct umtx_copyops *ops)
4559 struct umtx_robust_lists_params rb;
4562 if (ops->compat32) {
4563 if ((td->td_pflags2 & TDP2_COMPAT32RB) == 0 &&
4564 (td->td_rb_list != 0 || td->td_rbp_list != 0 ||
4565 td->td_rb_inact != 0))
4567 } else if ((td->td_pflags2 & TDP2_COMPAT32RB) != 0) {
4571 bzero(&rb, sizeof(rb));
4572 error = ops->copyin_robust_lists(uap->uaddr1, uap->val, &rb);
4577 td->td_pflags2 |= TDP2_COMPAT32RB;
4579 td->td_rb_list = rb.robust_list_offset;
4580 td->td_rbp_list = rb.robust_priv_list_offset;
4581 td->td_rb_inact = rb.robust_inact_offset;
4585 #if defined(__i386__) || defined(__amd64__)
4587 * Provide the standard 32-bit definitions for x86, since native/compat32 use a
4588 * 32-bit time_t there. Other architectures just need the i386 definitions
4589 * along with their standard compat32.
4591 struct timespecx32 {
4596 struct umtx_timex32 {
4597 struct timespecx32 _timeout;
4603 #define timespeci386 timespec32
4604 #define umtx_timei386 umtx_time32
4606 #else /* !__i386__ && !__amd64__ */
4607 /* 32-bit architectures can emulate i386, so define these almost everywhere. */
4608 struct timespeci386 {
4613 struct umtx_timei386 {
4614 struct timespeci386 _timeout;
4619 #if defined(__LP64__)
4620 #define timespecx32 timespec32
4621 #define umtx_timex32 umtx_time32
4626 umtx_copyin_robust_lists32(const void *uaddr, size_t size,
4627 struct umtx_robust_lists_params *rbp)
4629 struct umtx_robust_lists_params_compat32 rb32;
4632 if (size > sizeof(rb32))
4634 bzero(&rb32, sizeof(rb32));
4635 error = copyin(uaddr, &rb32, size);
4638 CP(rb32, *rbp, robust_list_offset);
4639 CP(rb32, *rbp, robust_priv_list_offset);
4640 CP(rb32, *rbp, robust_inact_offset);
4646 umtx_copyin_timeouti386(const void *uaddr, struct timespec *tsp)
4648 struct timespeci386 ts32;
4651 error = copyin(uaddr, &ts32, sizeof(ts32));
4653 if (ts32.tv_sec < 0 ||
4654 ts32.tv_nsec >= 1000000000 ||
4658 CP(ts32, *tsp, tv_sec);
4659 CP(ts32, *tsp, tv_nsec);
4666 umtx_copyin_umtx_timei386(const void *uaddr, size_t size, struct _umtx_time *tp)
4668 struct umtx_timei386 t32;
4671 t32._clockid = CLOCK_REALTIME;
4673 if (size <= sizeof(t32._timeout))
4674 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4676 error = copyin(uaddr, &t32, sizeof(t32));
4679 if (t32._timeout.tv_sec < 0 ||
4680 t32._timeout.tv_nsec >= 1000000000 || t32._timeout.tv_nsec < 0)
4682 TS_CP(t32, *tp, _timeout);
4683 CP(t32, *tp, _flags);
4684 CP(t32, *tp, _clockid);
4689 umtx_copyout_timeouti386(void *uaddr, size_t sz, struct timespec *tsp)
4691 struct timespeci386 remain32 = {
4692 .tv_sec = tsp->tv_sec,
4693 .tv_nsec = tsp->tv_nsec,
4697 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4698 * and we're only called if sz >= sizeof(timespec) as supplied in the
4701 KASSERT(sz >= sizeof(remain32),
4702 ("umtx_copyops specifies incorrect sizes"));
4704 return (copyout(&remain32, uaddr, sizeof(remain32)));
4706 #endif /* !__i386__ */
4708 #if defined(__i386__) || defined(__LP64__)
4710 umtx_copyin_timeoutx32(const void *uaddr, struct timespec *tsp)
4712 struct timespecx32 ts32;
4715 error = copyin(uaddr, &ts32, sizeof(ts32));
4717 if (ts32.tv_sec < 0 ||
4718 ts32.tv_nsec >= 1000000000 ||
4722 CP(ts32, *tsp, tv_sec);
4723 CP(ts32, *tsp, tv_nsec);
4730 umtx_copyin_umtx_timex32(const void *uaddr, size_t size, struct _umtx_time *tp)
4732 struct umtx_timex32 t32;
4735 t32._clockid = CLOCK_REALTIME;
4737 if (size <= sizeof(t32._timeout))
4738 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4740 error = copyin(uaddr, &t32, sizeof(t32));
4743 if (t32._timeout.tv_sec < 0 ||
4744 t32._timeout.tv_nsec >= 1000000000 || t32._timeout.tv_nsec < 0)
4746 TS_CP(t32, *tp, _timeout);
4747 CP(t32, *tp, _flags);
4748 CP(t32, *tp, _clockid);
4753 umtx_copyout_timeoutx32(void *uaddr, size_t sz, struct timespec *tsp)
4755 struct timespecx32 remain32 = {
4756 .tv_sec = tsp->tv_sec,
4757 .tv_nsec = tsp->tv_nsec,
4761 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4762 * and we're only called if sz >= sizeof(timespec) as supplied in the
4765 KASSERT(sz >= sizeof(remain32),
4766 ("umtx_copyops specifies incorrect sizes"));
4768 return (copyout(&remain32, uaddr, sizeof(remain32)));
4770 #endif /* __i386__ || __LP64__ */
4772 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap,
4773 const struct umtx_copyops *umtx_ops);
4775 static const _umtx_op_func op_table[] = {
4776 #ifdef COMPAT_FREEBSD10
4777 [UMTX_OP_LOCK] = __umtx_op_lock_umtx,
4778 [UMTX_OP_UNLOCK] = __umtx_op_unlock_umtx,
4780 [UMTX_OP_LOCK] = __umtx_op_unimpl,
4781 [UMTX_OP_UNLOCK] = __umtx_op_unimpl,
4783 [UMTX_OP_WAIT] = __umtx_op_wait,
4784 [UMTX_OP_WAKE] = __umtx_op_wake,
4785 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4786 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
4787 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4788 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4789 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
4790 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4791 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4792 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
4793 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
4794 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
4795 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4796 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4797 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4798 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4799 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4800 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4801 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4802 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4804 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4805 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4807 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4808 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4809 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4810 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4811 [UMTX_OP_SHM] = __umtx_op_shm,
4812 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4815 static const struct umtx_copyops umtx_native_ops = {
4816 .copyin_timeout = umtx_copyin_timeout,
4817 .copyin_umtx_time = umtx_copyin_umtx_time,
4818 .copyin_robust_lists = umtx_copyin_robust_lists,
4819 .copyout_timeout = umtx_copyout_timeout,
4820 .timespec_sz = sizeof(struct timespec),
4821 .umtx_time_sz = sizeof(struct _umtx_time),
4825 static const struct umtx_copyops umtx_native_opsi386 = {
4826 .copyin_timeout = umtx_copyin_timeouti386,
4827 .copyin_umtx_time = umtx_copyin_umtx_timei386,
4828 .copyin_robust_lists = umtx_copyin_robust_lists32,
4829 .copyout_timeout = umtx_copyout_timeouti386,
4830 .timespec_sz = sizeof(struct timespeci386),
4831 .umtx_time_sz = sizeof(struct umtx_timei386),
4836 #if defined(__i386__) || defined(__LP64__)
4837 /* i386 can emulate other 32-bit archs, too! */
4838 static const struct umtx_copyops umtx_native_opsx32 = {
4839 .copyin_timeout = umtx_copyin_timeoutx32,
4840 .copyin_umtx_time = umtx_copyin_umtx_timex32,
4841 .copyin_robust_lists = umtx_copyin_robust_lists32,
4842 .copyout_timeout = umtx_copyout_timeoutx32,
4843 .timespec_sz = sizeof(struct timespecx32),
4844 .umtx_time_sz = sizeof(struct umtx_timex32),
4848 #ifdef COMPAT_FREEBSD32
4850 #define umtx_native_ops32 umtx_native_opsi386
4852 #define umtx_native_ops32 umtx_native_opsx32
4854 #endif /* COMPAT_FREEBSD32 */
4855 #endif /* __i386__ || __LP64__ */
4857 #define UMTX_OP__FLAGS (UMTX_OP__32BIT | UMTX_OP__I386)
4860 kern__umtx_op(struct thread *td, void *obj, int op, unsigned long val,
4861 void *uaddr1, void *uaddr2, const struct umtx_copyops *ops)
4863 struct _umtx_op_args uap = {
4865 .op = op & ~UMTX_OP__FLAGS,
4871 if ((uap.op >= nitems(op_table)))
4873 return ((*op_table[uap.op])(td, &uap, ops));
4877 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4879 static const struct umtx_copyops *umtx_ops;
4881 umtx_ops = &umtx_native_ops;
4883 if ((uap->op & (UMTX_OP__32BIT | UMTX_OP__I386)) != 0) {
4884 if ((uap->op & UMTX_OP__I386) != 0)
4885 umtx_ops = &umtx_native_opsi386;
4887 umtx_ops = &umtx_native_opsx32;
4889 #elif !defined(__i386__)
4890 /* We consider UMTX_OP__32BIT a nop on !i386 ILP32. */
4891 if ((uap->op & UMTX_OP__I386) != 0)
4892 umtx_ops = &umtx_native_opsi386;
4894 /* Likewise, UMTX_OP__I386 is a nop on i386. */
4895 if ((uap->op & UMTX_OP__32BIT) != 0)
4896 umtx_ops = &umtx_native_opsx32;
4898 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr1,
4899 uap->uaddr2, umtx_ops));
4902 #ifdef COMPAT_FREEBSD32
4903 #ifdef COMPAT_FREEBSD10
4905 freebsd10_freebsd32_umtx_lock(struct thread *td,
4906 struct freebsd10_freebsd32_umtx_lock_args *uap)
4908 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
4912 freebsd10_freebsd32_umtx_unlock(struct thread *td,
4913 struct freebsd10_freebsd32_umtx_unlock_args *uap)
4915 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
4917 #endif /* COMPAT_FREEBSD10 */
4920 freebsd32__umtx_op(struct thread *td, struct freebsd32__umtx_op_args *uap)
4923 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr,
4924 uap->uaddr2, &umtx_native_ops32));
4926 #endif /* COMPAT_FREEBSD32 */
4929 umtx_thread_init(struct thread *td)
4932 td->td_umtxq = umtxq_alloc();
4933 td->td_umtxq->uq_thread = td;
4937 umtx_thread_fini(struct thread *td)
4940 umtxq_free(td->td_umtxq);
4944 * It will be called when new thread is created, e.g fork().
4947 umtx_thread_alloc(struct thread *td)
4952 uq->uq_inherited_pri = PRI_MAX;
4954 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4955 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4956 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4957 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4963 * Clear robust lists for all process' threads, not delaying the
4964 * cleanup to thread exit, since the relevant address space is
4965 * destroyed right now.
4968 umtx_exec(struct proc *p)
4972 KASSERT(p == curproc, ("need curproc"));
4973 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4974 (p->p_flag & P_STOPPED_SINGLE) != 0,
4975 ("curproc must be single-threaded"));
4977 * There is no need to lock the list as only this thread can be
4980 FOREACH_THREAD_IN_PROC(p, td) {
4981 KASSERT(td == curthread ||
4982 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4983 ("running thread %p %p", p, td));
4984 umtx_thread_cleanup(td);
4985 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4993 umtx_thread_exit(struct thread *td)
4996 umtx_thread_cleanup(td);
5000 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res, bool compat32)
5007 error = fueword32((void *)ptr, &res32);
5011 error = fueword((void *)ptr, &res1);
5021 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list,
5024 struct umutex32 m32;
5027 memcpy(&m32, m, sizeof(m32));
5028 *rb_list = m32.m_rb_lnk;
5030 *rb_list = m->m_rb_lnk;
5035 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact,
5041 KASSERT(td->td_proc == curproc, ("need current vmspace"));
5042 error = copyin((void *)rbp, &m, sizeof(m));
5045 if (rb_list != NULL)
5046 umtx_read_rb_list(td, &m, rb_list, compat32);
5047 if ((m.m_flags & UMUTEX_ROBUST) == 0)
5049 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
5050 /* inact is cleared after unlock, allow the inconsistency */
5051 return (inact ? 0 : EINVAL);
5052 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
5056 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
5057 const char *name, bool compat32)
5065 error = umtx_read_uptr(td, rb_list, &rbp, compat32);
5066 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
5067 if (rbp == *rb_inact) {
5072 error = umtx_handle_rb(td, rbp, &rbp, inact, compat32);
5074 if (i == umtx_max_rb && umtx_verbose_rb) {
5075 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
5076 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
5078 if (error != 0 && umtx_verbose_rb) {
5079 uprintf("comm %s pid %d: handling %srb error %d\n",
5080 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
5085 * Clean up umtx data.
5088 umtx_thread_cleanup(struct thread *td)
5096 * Disown pi mutexes.
5100 if (uq->uq_inherited_pri != PRI_MAX ||
5101 !TAILQ_EMPTY(&uq->uq_pi_contested)) {
5102 mtx_lock(&umtx_lock);
5103 uq->uq_inherited_pri = PRI_MAX;
5104 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
5105 pi->pi_owner = NULL;
5106 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
5108 mtx_unlock(&umtx_lock);
5110 sched_lend_user_prio_cond(td, PRI_MAX);
5113 compat32 = (td->td_pflags2 & TDP2_COMPAT32RB) != 0;
5114 td->td_pflags2 &= ~TDP2_COMPAT32RB;
5116 if (td->td_rb_inact == 0 && td->td_rb_list == 0 && td->td_rbp_list == 0)
5120 * Handle terminated robust mutexes. Must be done after
5121 * robust pi disown, otherwise unlock could see unowned
5124 rb_inact = td->td_rb_inact;
5126 (void)umtx_read_uptr(td, rb_inact, &rb_inact, compat32);
5127 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "", compat32);
5128 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ", compat32);
5130 (void)umtx_handle_rb(td, rb_inact, NULL, true, compat32);