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>
66 #include <sys/umtxvar.h>
68 #include <security/mac/mac_framework.h>
71 #include <vm/vm_param.h>
73 #include <vm/vm_map.h>
74 #include <vm/vm_object.h>
76 #include <machine/atomic.h>
77 #include <machine/cpu.h>
79 #include <compat/freebsd32/freebsd32.h>
80 #ifdef COMPAT_FREEBSD32
81 #include <compat/freebsd32/freebsd32_proto.h>
85 #define _UMUTEX_WAIT 2
88 #define UPROF_PERC_BIGGER(w, f, sw, sf) \
89 (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
92 #define UMTXQ_LOCKED_ASSERT(uc) mtx_assert(&(uc)->uc_lock, MA_OWNED)
94 #define UMTXQ_ASSERT_LOCKED_BUSY(key) do { \
95 struct umtxq_chain *uc; \
97 uc = umtxq_getchain(key); \
98 mtx_assert(&uc->uc_lock, MA_OWNED); \
99 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy")); \
102 #define UMTXQ_ASSERT_LOCKED_BUSY(key) do {} while (0)
106 * Don't propagate time-sharing priority, there is a security reason,
107 * a user can simply introduce PI-mutex, let thread A lock the mutex,
108 * and let another thread B block on the mutex, because B is
109 * sleeping, its priority will be boosted, this causes A's priority to
110 * be boosted via priority propagating too and will never be lowered even
111 * if it is using 100%CPU, this is unfair to other processes.
114 #define UPRI(td) (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
115 (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
116 PRI_MAX_TIMESHARE : (td)->td_user_pri)
118 #define GOLDEN_RATIO_PRIME 2654404609U
120 #define UMTX_CHAINS 512
122 #define UMTX_SHIFTS (__WORD_BIT - 9)
124 #define GET_SHARE(flags) \
125 (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
127 #define BUSY_SPINS 200
129 struct umtx_copyops {
130 int (*copyin_timeout)(const void *uaddr, struct timespec *tsp);
131 int (*copyin_umtx_time)(const void *uaddr, size_t size,
132 struct _umtx_time *tp);
133 int (*copyin_robust_lists)(const void *uaddr, size_t size,
134 struct umtx_robust_lists_params *rbp);
135 int (*copyout_timeout)(void *uaddr, size_t size,
136 struct timespec *tsp);
137 const size_t timespec_sz;
138 const size_t umtx_time_sz;
142 _Static_assert(sizeof(struct umutex) == sizeof(struct umutex32), "umutex32");
143 _Static_assert(__offsetof(struct umutex, m_spare[0]) ==
144 __offsetof(struct umutex32, m_spare[0]), "m_spare32");
146 int umtx_shm_vnobj_persistent = 0;
147 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_vnode_persistent, CTLFLAG_RWTUN,
148 &umtx_shm_vnobj_persistent, 0,
149 "False forces destruction of umtx attached to file, on last close");
150 static int umtx_max_rb = 1000;
151 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_max_robust, CTLFLAG_RWTUN,
153 "Maximum number of robust mutexes allowed for each thread");
155 static uma_zone_t umtx_pi_zone;
156 static struct umtxq_chain umtxq_chains[2][UMTX_CHAINS];
157 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
158 static int umtx_pi_allocated;
160 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
162 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
163 &umtx_pi_allocated, 0, "Allocated umtx_pi");
164 static int umtx_verbose_rb = 1;
165 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
169 #ifdef UMTX_PROFILING
170 static long max_length;
171 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
172 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
176 static inline void umtx_abs_timeout_init2(struct umtx_abs_timeout *timo,
177 const struct _umtx_time *umtxtime);
178 static int umtx_abs_timeout_gethz(struct umtx_abs_timeout *timo);
179 static inline void umtx_abs_timeout_update(struct umtx_abs_timeout *timo);
181 static void umtx_shm_init(void);
182 static void umtxq_sysinit(void *);
183 static void umtxq_hash(struct umtx_key *key);
184 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
186 static void umtx_thread_cleanup(struct thread *td);
187 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
189 #define umtxq_signal(key, nwake) umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
191 static struct mtx umtx_lock;
193 #ifdef UMTX_PROFILING
195 umtx_init_profiling(void)
197 struct sysctl_oid *chain_oid;
201 for (i = 0; i < UMTX_CHAINS; ++i) {
202 snprintf(chain_name, sizeof(chain_name), "%d", i);
203 chain_oid = SYSCTL_ADD_NODE(NULL,
204 SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO,
205 chain_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
207 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
208 "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
209 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
210 "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
215 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
219 struct umtxq_chain *uc;
220 u_int fract, i, j, tot, whole;
221 u_int sf0, sf1, sf2, sf3, sf4;
222 u_int si0, si1, si2, si3, si4;
223 u_int sw0, sw1, sw2, sw3, sw4;
225 sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
226 for (i = 0; i < 2; i++) {
228 for (j = 0; j < UMTX_CHAINS; ++j) {
229 uc = &umtxq_chains[i][j];
230 mtx_lock(&uc->uc_lock);
231 tot += uc->max_length;
232 mtx_unlock(&uc->uc_lock);
235 sbuf_printf(&sb, "%u) Empty ", i);
237 sf0 = sf1 = sf2 = sf3 = sf4 = 0;
238 si0 = si1 = si2 = si3 = si4 = 0;
239 sw0 = sw1 = sw2 = sw3 = sw4 = 0;
240 for (j = 0; j < UMTX_CHAINS; j++) {
241 uc = &umtxq_chains[i][j];
242 mtx_lock(&uc->uc_lock);
243 whole = uc->max_length * 100;
244 mtx_unlock(&uc->uc_lock);
245 fract = (whole % tot) * 100;
246 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
250 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
255 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
260 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
265 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
272 sbuf_printf(&sb, "queue %u:\n", i);
273 sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
275 sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
277 sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
279 sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
281 sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
287 sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
293 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
295 struct umtxq_chain *uc;
300 error = sysctl_handle_int(oidp, &clear, 0, req);
301 if (error != 0 || req->newptr == NULL)
305 for (i = 0; i < 2; ++i) {
306 for (j = 0; j < UMTX_CHAINS; ++j) {
307 uc = &umtxq_chains[i][j];
308 mtx_lock(&uc->uc_lock);
311 mtx_unlock(&uc->uc_lock);
318 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
319 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
320 sysctl_debug_umtx_chains_clear, "I",
321 "Clear umtx chains statistics");
322 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
323 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
324 sysctl_debug_umtx_chains_peaks, "A",
325 "Highest peaks in chains max length");
329 umtxq_sysinit(void *arg __unused)
333 umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
334 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
335 for (i = 0; i < 2; ++i) {
336 for (j = 0; j < UMTX_CHAINS; ++j) {
337 mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
338 MTX_DEF | MTX_DUPOK);
339 LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
340 LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
341 LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
342 TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
343 umtxq_chains[i][j].uc_busy = 0;
344 umtxq_chains[i][j].uc_waiters = 0;
345 #ifdef UMTX_PROFILING
346 umtxq_chains[i][j].length = 0;
347 umtxq_chains[i][j].max_length = 0;
351 #ifdef UMTX_PROFILING
352 umtx_init_profiling();
354 mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
363 uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
364 uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
366 TAILQ_INIT(&uq->uq_spare_queue->head);
367 TAILQ_INIT(&uq->uq_pi_contested);
368 uq->uq_inherited_pri = PRI_MAX;
373 umtxq_free(struct umtx_q *uq)
376 MPASS(uq->uq_spare_queue != NULL);
377 free(uq->uq_spare_queue, M_UMTX);
382 umtxq_hash(struct umtx_key *key)
386 n = (uintptr_t)key->info.both.a + key->info.both.b;
387 key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
391 umtxq_getchain(struct umtx_key *key)
394 if (key->type <= TYPE_SEM)
395 return (&umtxq_chains[1][key->hash]);
396 return (&umtxq_chains[0][key->hash]);
400 * Set chain to busy state when following operation
401 * may be blocked (kernel mutex can not be used).
404 umtxq_busy(struct umtx_key *key)
406 struct umtxq_chain *uc;
408 uc = umtxq_getchain(key);
409 mtx_assert(&uc->uc_lock, MA_OWNED);
413 int count = BUSY_SPINS;
416 while (uc->uc_busy && --count > 0)
422 while (uc->uc_busy) {
424 msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
435 umtxq_unbusy(struct umtx_key *key)
437 struct umtxq_chain *uc;
439 uc = umtxq_getchain(key);
440 mtx_assert(&uc->uc_lock, MA_OWNED);
441 KASSERT(uc->uc_busy != 0, ("not busy"));
448 umtxq_unbusy_unlocked(struct umtx_key *key)
456 static struct umtxq_queue *
457 umtxq_queue_lookup(struct umtx_key *key, int q)
459 struct umtxq_queue *uh;
460 struct umtxq_chain *uc;
462 uc = umtxq_getchain(key);
463 UMTXQ_LOCKED_ASSERT(uc);
464 LIST_FOREACH(uh, &uc->uc_queue[q], link) {
465 if (umtx_key_match(&uh->key, key))
473 umtxq_insert_queue(struct umtx_q *uq, int q)
475 struct umtxq_queue *uh;
476 struct umtxq_chain *uc;
478 uc = umtxq_getchain(&uq->uq_key);
479 UMTXQ_LOCKED_ASSERT(uc);
480 KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
481 uh = umtxq_queue_lookup(&uq->uq_key, q);
483 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
485 uh = uq->uq_spare_queue;
486 uh->key = uq->uq_key;
487 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
488 #ifdef UMTX_PROFILING
490 if (uc->length > uc->max_length) {
491 uc->max_length = uc->length;
492 if (uc->max_length > max_length)
493 max_length = uc->max_length;
497 uq->uq_spare_queue = NULL;
499 TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
501 uq->uq_flags |= UQF_UMTXQ;
502 uq->uq_cur_queue = uh;
507 umtxq_remove_queue(struct umtx_q *uq, int q)
509 struct umtxq_chain *uc;
510 struct umtxq_queue *uh;
512 uc = umtxq_getchain(&uq->uq_key);
513 UMTXQ_LOCKED_ASSERT(uc);
514 if (uq->uq_flags & UQF_UMTXQ) {
515 uh = uq->uq_cur_queue;
516 TAILQ_REMOVE(&uh->head, uq, uq_link);
518 uq->uq_flags &= ~UQF_UMTXQ;
519 if (TAILQ_EMPTY(&uh->head)) {
520 KASSERT(uh->length == 0,
521 ("inconsistent umtxq_queue length"));
522 #ifdef UMTX_PROFILING
525 LIST_REMOVE(uh, link);
527 uh = LIST_FIRST(&uc->uc_spare_queue);
528 KASSERT(uh != NULL, ("uc_spare_queue is empty"));
529 LIST_REMOVE(uh, link);
531 uq->uq_spare_queue = uh;
532 uq->uq_cur_queue = NULL;
537 * Check if there are multiple waiters
540 umtxq_count(struct umtx_key *key)
542 struct umtxq_queue *uh;
544 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
545 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
552 * Check if there are multiple PI waiters and returns first
556 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
558 struct umtxq_queue *uh;
561 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
562 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
564 *first = TAILQ_FIRST(&uh->head);
571 * Wake up threads waiting on an userland object by a bit mask.
574 umtxq_signal_mask(struct umtx_key *key, int n_wake, u_int bitset)
576 struct umtxq_queue *uh;
577 struct umtx_q *uq, *uq_temp;
581 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
582 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
585 TAILQ_FOREACH_SAFE(uq, &uh->head, uq_link, uq_temp) {
586 if ((uq->uq_bitset & bitset) == 0)
588 umtxq_remove_queue(uq, UMTX_SHARED_QUEUE);
597 * Wake up threads waiting on an userland object.
601 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
603 struct umtxq_queue *uh;
608 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
609 uh = umtxq_queue_lookup(key, q);
611 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
612 umtxq_remove_queue(uq, q);
622 * Wake up specified thread.
625 umtxq_signal_thread(struct umtx_q *uq)
628 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
634 * Wake up a maximum of n_wake threads that are waiting on an userland
635 * object identified by key. The remaining threads are removed from queue
636 * identified by key and added to the queue identified by key2 (requeued).
637 * The n_requeue specifies an upper limit on the number of threads that
638 * are requeued to the second queue.
641 umtxq_requeue(struct umtx_key *key, int n_wake, struct umtx_key *key2,
644 struct umtxq_queue *uh, *uh2;
645 struct umtx_q *uq, *uq_temp;
649 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
650 UMTXQ_LOCKED_ASSERT(umtxq_getchain(key2));
651 uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
652 uh2 = umtxq_queue_lookup(key2, UMTX_SHARED_QUEUE);
655 TAILQ_FOREACH_SAFE(uq, &uh->head, uq_link, uq_temp) {
656 if (++ret <= n_wake) {
663 if (ret - n_wake == n_requeue)
671 tstohz(const struct timespec *tsp)
675 TIMESPEC_TO_TIMEVAL(&tv, tsp);
680 umtx_abs_timeout_init(struct umtx_abs_timeout *timo, int clockid,
681 int absolute, const struct timespec *timeout)
684 timo->clockid = clockid;
686 timo->is_abs_real = false;
687 umtx_abs_timeout_update(timo);
688 timespecadd(&timo->cur, timeout, &timo->end);
690 timo->end = *timeout;
691 timo->is_abs_real = clockid == CLOCK_REALTIME ||
692 clockid == CLOCK_REALTIME_FAST ||
693 clockid == CLOCK_REALTIME_PRECISE;
695 * If is_abs_real, umtxq_sleep will read the clock
696 * after setting td_rtcgen; otherwise, read it here.
698 if (!timo->is_abs_real) {
699 umtx_abs_timeout_update(timo);
705 umtx_abs_timeout_init2(struct umtx_abs_timeout *timo,
706 const struct _umtx_time *umtxtime)
709 umtx_abs_timeout_init(timo, umtxtime->_clockid,
710 (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
714 umtx_abs_timeout_update(struct umtx_abs_timeout *timo)
717 kern_clock_gettime(curthread, timo->clockid, &timo->cur);
721 umtx_abs_timeout_gethz(struct umtx_abs_timeout *timo)
725 if (timespeccmp(&timo->end, &timo->cur, <=))
727 timespecsub(&timo->end, &timo->cur, &tts);
728 return (tstohz(&tts));
732 umtx_unlock_val(uint32_t flags, bool rb)
736 return (UMUTEX_RB_OWNERDEAD);
737 else if ((flags & UMUTEX_NONCONSISTENT) != 0)
738 return (UMUTEX_RB_NOTRECOV);
740 return (UMUTEX_UNOWNED);
745 * Put thread into sleep state, before sleeping, check if
746 * thread was removed from umtx queue.
749 umtxq_sleep(struct umtx_q *uq, const char *wmesg,
750 struct umtx_abs_timeout *abstime)
752 struct umtxq_chain *uc;
755 if (abstime != NULL && abstime->is_abs_real) {
756 curthread->td_rtcgen = atomic_load_acq_int(&rtc_generation);
757 umtx_abs_timeout_update(abstime);
760 uc = umtxq_getchain(&uq->uq_key);
761 UMTXQ_LOCKED_ASSERT(uc);
763 if (!(uq->uq_flags & UQF_UMTXQ)) {
767 if (abstime != NULL) {
768 timo = umtx_abs_timeout_gethz(abstime);
775 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
776 if (error == EINTR || error == ERESTART) {
777 umtxq_lock(&uq->uq_key);
780 if (abstime != NULL) {
781 if (abstime->is_abs_real)
782 curthread->td_rtcgen =
783 atomic_load_acq_int(&rtc_generation);
784 umtx_abs_timeout_update(abstime);
786 umtxq_lock(&uq->uq_key);
789 curthread->td_rtcgen = 0;
794 * Convert userspace address into unique logical address.
797 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
799 struct thread *td = curthread;
801 vm_map_entry_t entry;
807 if (share == THREAD_SHARE) {
809 key->info.private.vs = td->td_proc->p_vmspace;
810 key->info.private.addr = (uintptr_t)addr;
812 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
813 map = &td->td_proc->p_vmspace->vm_map;
814 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
815 &entry, &key->info.shared.object, &pindex, &prot,
816 &wired) != KERN_SUCCESS) {
820 if ((share == PROCESS_SHARE) ||
821 (share == AUTO_SHARE &&
822 VM_INHERIT_SHARE == entry->inheritance)) {
824 key->info.shared.offset = (vm_offset_t)addr -
825 entry->start + entry->offset;
826 vm_object_reference(key->info.shared.object);
829 key->info.private.vs = td->td_proc->p_vmspace;
830 key->info.private.addr = (uintptr_t)addr;
832 vm_map_lookup_done(map, entry);
843 umtx_key_release(struct umtx_key *key)
846 vm_object_deallocate(key->info.shared.object);
849 #ifdef COMPAT_FREEBSD10
851 * Lock a umtx object.
854 do_lock_umtx(struct thread *td, struct umtx *umtx, u_long id,
855 const struct timespec *timeout)
857 struct umtx_abs_timeout timo;
865 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
868 * Care must be exercised when dealing with umtx structure. It
869 * can fault on any access.
873 * Try the uncontested case. This should be done in userland.
875 owner = casuword(&umtx->u_owner, UMTX_UNOWNED, id);
877 /* The acquire succeeded. */
878 if (owner == UMTX_UNOWNED)
881 /* The address was invalid. */
885 /* If no one owns it but it is contested try to acquire it. */
886 if (owner == UMTX_CONTESTED) {
887 owner = casuword(&umtx->u_owner,
888 UMTX_CONTESTED, id | UMTX_CONTESTED);
890 if (owner == UMTX_CONTESTED)
893 /* The address was invalid. */
897 error = thread_check_susp(td, false);
901 /* If this failed the lock has changed, restart. */
906 * If we caught a signal, we have retried and now
912 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK,
913 AUTO_SHARE, &uq->uq_key)) != 0)
916 umtxq_lock(&uq->uq_key);
917 umtxq_busy(&uq->uq_key);
919 umtxq_unbusy(&uq->uq_key);
920 umtxq_unlock(&uq->uq_key);
923 * Set the contested bit so that a release in user space
924 * knows to use the system call for unlock. If this fails
925 * either some one else has acquired the lock or it has been
928 old = casuword(&umtx->u_owner, owner, owner | UMTX_CONTESTED);
930 /* The address was invalid. */
932 umtxq_lock(&uq->uq_key);
934 umtxq_unlock(&uq->uq_key);
935 umtx_key_release(&uq->uq_key);
940 * We set the contested bit, sleep. Otherwise the lock changed
941 * and we need to retry or we lost a race to the thread
942 * unlocking the umtx.
944 umtxq_lock(&uq->uq_key);
946 error = umtxq_sleep(uq, "umtx", timeout == NULL ? NULL :
949 umtxq_unlock(&uq->uq_key);
950 umtx_key_release(&uq->uq_key);
953 error = thread_check_susp(td, false);
956 if (timeout == NULL) {
957 /* Mutex locking is restarted if it is interrupted. */
961 /* Timed-locking is not restarted. */
962 if (error == ERESTART)
969 * Unlock a umtx object.
972 do_unlock_umtx(struct thread *td, struct umtx *umtx, u_long id)
981 * Make sure we own this mtx.
983 owner = fuword(__DEVOLATILE(u_long *, &umtx->u_owner));
987 if ((owner & ~UMTX_CONTESTED) != id)
990 /* This should be done in userland */
991 if ((owner & UMTX_CONTESTED) == 0) {
992 old = casuword(&umtx->u_owner, owner, UMTX_UNOWNED);
1000 /* We should only ever be in here for contested locks */
1001 if ((error = umtx_key_get(umtx, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1007 count = umtxq_count(&key);
1011 * When unlocking the umtx, it must be marked as unowned if
1012 * there is zero or one thread only waiting for it.
1013 * Otherwise, it must be marked as contested.
1015 old = casuword(&umtx->u_owner, owner,
1016 count <= 1 ? UMTX_UNOWNED : UMTX_CONTESTED);
1018 umtxq_signal(&key,1);
1021 umtx_key_release(&key);
1029 #ifdef COMPAT_FREEBSD32
1032 * Lock a umtx object.
1035 do_lock_umtx32(struct thread *td, uint32_t *m, uint32_t id,
1036 const struct timespec *timeout)
1038 struct umtx_abs_timeout timo;
1046 if (timeout != NULL)
1047 umtx_abs_timeout_init(&timo, CLOCK_REALTIME, 0, timeout);
1050 * Care must be exercised when dealing with umtx structure. It
1051 * can fault on any access.
1055 * Try the uncontested case. This should be done in userland.
1057 owner = casuword32(m, UMUTEX_UNOWNED, id);
1059 /* The acquire succeeded. */
1060 if (owner == UMUTEX_UNOWNED)
1063 /* The address was invalid. */
1067 /* If no one owns it but it is contested try to acquire it. */
1068 if (owner == UMUTEX_CONTESTED) {
1069 owner = casuword32(m,
1070 UMUTEX_CONTESTED, id | UMUTEX_CONTESTED);
1071 if (owner == UMUTEX_CONTESTED)
1074 /* The address was invalid. */
1078 error = thread_check_susp(td, false);
1082 /* If this failed the lock has changed, restart. */
1087 * If we caught a signal, we have retried and now
1093 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK,
1094 AUTO_SHARE, &uq->uq_key)) != 0)
1097 umtxq_lock(&uq->uq_key);
1098 umtxq_busy(&uq->uq_key);
1100 umtxq_unbusy(&uq->uq_key);
1101 umtxq_unlock(&uq->uq_key);
1104 * Set the contested bit so that a release in user space
1105 * knows to use the system call for unlock. If this fails
1106 * either some one else has acquired the lock or it has been
1109 old = casuword32(m, owner, owner | UMUTEX_CONTESTED);
1111 /* The address was invalid. */
1113 umtxq_lock(&uq->uq_key);
1115 umtxq_unlock(&uq->uq_key);
1116 umtx_key_release(&uq->uq_key);
1121 * We set the contested bit, sleep. Otherwise the lock changed
1122 * and we need to retry or we lost a race to the thread
1123 * unlocking the umtx.
1125 umtxq_lock(&uq->uq_key);
1127 error = umtxq_sleep(uq, "umtx", timeout == NULL ?
1130 umtxq_unlock(&uq->uq_key);
1131 umtx_key_release(&uq->uq_key);
1134 error = thread_check_susp(td, false);
1137 if (timeout == NULL) {
1138 /* Mutex locking is restarted if it is interrupted. */
1142 /* Timed-locking is not restarted. */
1143 if (error == ERESTART)
1150 * Unlock a umtx object.
1153 do_unlock_umtx32(struct thread *td, uint32_t *m, uint32_t id)
1155 struct umtx_key key;
1162 * Make sure we own this mtx.
1164 owner = fuword32(m);
1168 if ((owner & ~UMUTEX_CONTESTED) != id)
1171 /* This should be done in userland */
1172 if ((owner & UMUTEX_CONTESTED) == 0) {
1173 old = casuword32(m, owner, UMUTEX_UNOWNED);
1181 /* We should only ever be in here for contested locks */
1182 if ((error = umtx_key_get(m, TYPE_SIMPLE_LOCK, AUTO_SHARE,
1188 count = umtxq_count(&key);
1192 * When unlocking the umtx, it must be marked as unowned if
1193 * there is zero or one thread only waiting for it.
1194 * Otherwise, it must be marked as contested.
1196 old = casuword32(m, owner,
1197 count <= 1 ? UMUTEX_UNOWNED : UMUTEX_CONTESTED);
1199 umtxq_signal(&key,1);
1202 umtx_key_release(&key);
1209 #endif /* COMPAT_FREEBSD32 */
1210 #endif /* COMPAT_FREEBSD10 */
1213 * Fetch and compare value, sleep on the address if value is not changed.
1216 do_wait(struct thread *td, void *addr, u_long id,
1217 struct _umtx_time *timeout, int compat32, int is_private)
1219 struct umtx_abs_timeout timo;
1226 if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
1227 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
1230 if (timeout != NULL)
1231 umtx_abs_timeout_init2(&timo, timeout);
1233 umtxq_lock(&uq->uq_key);
1235 umtxq_unlock(&uq->uq_key);
1236 if (compat32 == 0) {
1237 error = fueword(addr, &tmp);
1241 error = fueword32(addr, &tmp32);
1247 umtxq_lock(&uq->uq_key);
1250 error = umtxq_sleep(uq, "uwait", timeout == NULL ?
1252 if ((uq->uq_flags & UQF_UMTXQ) == 0)
1256 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
1259 umtxq_unlock(&uq->uq_key);
1260 umtx_key_release(&uq->uq_key);
1261 if (error == ERESTART)
1267 * Wake up threads sleeping on the specified address.
1270 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1272 struct umtx_key key;
1275 if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1276 is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1279 umtxq_signal(&key, n_wake);
1281 umtx_key_release(&key);
1286 * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1289 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1290 struct _umtx_time *timeout, int mode)
1292 struct umtx_abs_timeout timo;
1294 uint32_t owner, old, id;
1300 if (timeout != NULL)
1301 umtx_abs_timeout_init2(&timo, timeout);
1304 * Care must be exercised when dealing with umtx structure. It
1305 * can fault on any access.
1308 rv = fueword32(&m->m_owner, &owner);
1311 if (mode == _UMUTEX_WAIT) {
1312 if (owner == UMUTEX_UNOWNED ||
1313 owner == UMUTEX_CONTESTED ||
1314 owner == UMUTEX_RB_OWNERDEAD ||
1315 owner == UMUTEX_RB_NOTRECOV)
1319 * Robust mutex terminated. Kernel duty is to
1320 * return EOWNERDEAD to the userspace. The
1321 * umutex.m_flags UMUTEX_NONCONSISTENT is set
1322 * by the common userspace code.
1324 if (owner == UMUTEX_RB_OWNERDEAD) {
1325 rv = casueword32(&m->m_owner,
1326 UMUTEX_RB_OWNERDEAD, &owner,
1327 id | UMUTEX_CONTESTED);
1331 MPASS(owner == UMUTEX_RB_OWNERDEAD);
1332 return (EOWNERDEAD); /* success */
1335 rv = thread_check_susp(td, false);
1340 if (owner == UMUTEX_RB_NOTRECOV)
1341 return (ENOTRECOVERABLE);
1344 * Try the uncontested case. This should be
1347 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1349 /* The address was invalid. */
1353 /* The acquire succeeded. */
1355 MPASS(owner == UMUTEX_UNOWNED);
1360 * If no one owns it but it is contested try
1364 if (owner == UMUTEX_CONTESTED) {
1365 rv = casueword32(&m->m_owner,
1366 UMUTEX_CONTESTED, &owner,
1367 id | UMUTEX_CONTESTED);
1368 /* The address was invalid. */
1372 MPASS(owner == UMUTEX_CONTESTED);
1376 rv = thread_check_susp(td, false);
1382 * If this failed the lock has
1388 /* rv == 1 but not contested, likely store failure */
1389 rv = thread_check_susp(td, false);
1394 if (mode == _UMUTEX_TRY)
1398 * If we caught a signal, we have retried and now
1404 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1405 GET_SHARE(flags), &uq->uq_key)) != 0)
1408 umtxq_lock(&uq->uq_key);
1409 umtxq_busy(&uq->uq_key);
1411 umtxq_unlock(&uq->uq_key);
1414 * Set the contested bit so that a release in user space
1415 * knows to use the system call for unlock. If this fails
1416 * either some one else has acquired the lock or it has been
1419 rv = casueword32(&m->m_owner, owner, &old,
1420 owner | UMUTEX_CONTESTED);
1422 /* The address was invalid or casueword failed to store. */
1423 if (rv == -1 || rv == 1) {
1424 umtxq_lock(&uq->uq_key);
1426 umtxq_unbusy(&uq->uq_key);
1427 umtxq_unlock(&uq->uq_key);
1428 umtx_key_release(&uq->uq_key);
1432 rv = thread_check_susp(td, false);
1440 * We set the contested bit, sleep. Otherwise the lock changed
1441 * and we need to retry or we lost a race to the thread
1442 * unlocking the umtx.
1444 umtxq_lock(&uq->uq_key);
1445 umtxq_unbusy(&uq->uq_key);
1446 MPASS(old == owner);
1447 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1450 umtxq_unlock(&uq->uq_key);
1451 umtx_key_release(&uq->uq_key);
1454 error = thread_check_susp(td, false);
1461 * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1464 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1466 struct umtx_key key;
1467 uint32_t owner, old, id, newlock;
1474 * Make sure we own this mtx.
1476 error = fueword32(&m->m_owner, &owner);
1480 if ((owner & ~UMUTEX_CONTESTED) != id)
1483 newlock = umtx_unlock_val(flags, rb);
1484 if ((owner & UMUTEX_CONTESTED) == 0) {
1485 error = casueword32(&m->m_owner, owner, &old, newlock);
1489 error = thread_check_susp(td, false);
1494 MPASS(old == owner);
1498 /* We should only ever be in here for contested locks */
1499 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1505 count = umtxq_count(&key);
1509 * When unlocking the umtx, it must be marked as unowned if
1510 * there is zero or one thread only waiting for it.
1511 * Otherwise, it must be marked as contested.
1514 newlock |= UMUTEX_CONTESTED;
1515 error = casueword32(&m->m_owner, owner, &old, newlock);
1517 umtxq_signal(&key, 1);
1520 umtx_key_release(&key);
1526 error = thread_check_susp(td, false);
1535 * Check if the mutex is available and wake up a waiter,
1536 * only for simple mutex.
1539 do_wake_umutex(struct thread *td, struct umutex *m)
1541 struct umtx_key key;
1548 error = fueword32(&m->m_owner, &owner);
1552 if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1553 owner != UMUTEX_RB_NOTRECOV)
1556 error = fueword32(&m->m_flags, &flags);
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);
1570 if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1571 owner != UMUTEX_RB_NOTRECOV) {
1572 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1576 } else if (error == 1) {
1580 umtx_key_release(&key);
1581 error = thread_check_susp(td, false);
1589 if (error == 0 && count != 0) {
1590 MPASS((owner & ~UMUTEX_CONTESTED) == 0 ||
1591 owner == UMUTEX_RB_OWNERDEAD ||
1592 owner == UMUTEX_RB_NOTRECOV);
1593 umtxq_signal(&key, 1);
1597 umtx_key_release(&key);
1602 * Check if the mutex has waiters and tries to fix contention bit.
1605 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1607 struct umtx_key key;
1608 uint32_t owner, old;
1613 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1617 type = TYPE_NORMAL_UMUTEX;
1619 case UMUTEX_PRIO_INHERIT:
1620 type = TYPE_PI_UMUTEX;
1622 case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1623 type = TYPE_PI_ROBUST_UMUTEX;
1625 case UMUTEX_PRIO_PROTECT:
1626 type = TYPE_PP_UMUTEX;
1628 case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1629 type = TYPE_PP_ROBUST_UMUTEX;
1634 if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1640 count = umtxq_count(&key);
1643 error = fueword32(&m->m_owner, &owner);
1648 * Only repair contention bit if there is a waiter, this means
1649 * the mutex is still being referenced by userland code,
1650 * otherwise don't update any memory.
1652 while (error == 0 && (owner & UMUTEX_CONTESTED) == 0 &&
1653 (count > 1 || (count == 1 && (owner & ~UMUTEX_CONTESTED) != 0))) {
1654 error = casueword32(&m->m_owner, owner, &old,
1655 owner | UMUTEX_CONTESTED);
1661 MPASS(old == owner);
1665 error = thread_check_susp(td, false);
1669 if (error == EFAULT) {
1670 umtxq_signal(&key, INT_MAX);
1671 } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1672 owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1673 umtxq_signal(&key, 1);
1676 umtx_key_release(&key);
1681 umtx_pi_alloc(int flags)
1685 pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1686 TAILQ_INIT(&pi->pi_blocked);
1687 atomic_add_int(&umtx_pi_allocated, 1);
1692 umtx_pi_free(struct umtx_pi *pi)
1694 uma_zfree(umtx_pi_zone, pi);
1695 atomic_add_int(&umtx_pi_allocated, -1);
1699 * Adjust the thread's position on a pi_state after its priority has been
1703 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1705 struct umtx_q *uq, *uq1, *uq2;
1708 mtx_assert(&umtx_lock, MA_OWNED);
1715 * Check if the thread needs to be moved on the blocked chain.
1716 * It needs to be moved if either its priority is lower than
1717 * the previous thread or higher than the next thread.
1719 uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1720 uq2 = TAILQ_NEXT(uq, uq_lockq);
1721 if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1722 (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1724 * Remove thread from blocked chain and determine where
1725 * it should be moved to.
1727 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1728 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1729 td1 = uq1->uq_thread;
1730 MPASS(td1->td_proc->p_magic == P_MAGIC);
1731 if (UPRI(td1) > UPRI(td))
1736 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1738 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1743 static struct umtx_pi *
1744 umtx_pi_next(struct umtx_pi *pi)
1746 struct umtx_q *uq_owner;
1748 if (pi->pi_owner == NULL)
1750 uq_owner = pi->pi_owner->td_umtxq;
1751 if (uq_owner == NULL)
1753 return (uq_owner->uq_pi_blocked);
1757 * Floyd's Cycle-Finding Algorithm.
1760 umtx_pi_check_loop(struct umtx_pi *pi)
1762 struct umtx_pi *pi1; /* fast iterator */
1764 mtx_assert(&umtx_lock, MA_OWNED);
1769 pi = umtx_pi_next(pi);
1772 pi1 = umtx_pi_next(pi1);
1775 pi1 = umtx_pi_next(pi1);
1785 * Propagate priority when a thread is blocked on POSIX
1789 umtx_propagate_priority(struct thread *td)
1795 mtx_assert(&umtx_lock, MA_OWNED);
1798 pi = uq->uq_pi_blocked;
1801 if (umtx_pi_check_loop(pi))
1806 if (td == NULL || td == curthread)
1809 MPASS(td->td_proc != NULL);
1810 MPASS(td->td_proc->p_magic == P_MAGIC);
1813 if (td->td_lend_user_pri > pri)
1814 sched_lend_user_prio(td, pri);
1822 * Pick up the lock that td is blocked on.
1825 pi = uq->uq_pi_blocked;
1828 /* Resort td on the list if needed. */
1829 umtx_pi_adjust_thread(pi, td);
1834 * Unpropagate priority for a PI mutex when a thread blocked on
1835 * it is interrupted by signal or resumed by others.
1838 umtx_repropagate_priority(struct umtx_pi *pi)
1840 struct umtx_q *uq, *uq_owner;
1841 struct umtx_pi *pi2;
1844 mtx_assert(&umtx_lock, MA_OWNED);
1846 if (umtx_pi_check_loop(pi))
1848 while (pi != NULL && pi->pi_owner != NULL) {
1850 uq_owner = pi->pi_owner->td_umtxq;
1852 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1853 uq = TAILQ_FIRST(&pi2->pi_blocked);
1855 if (pri > UPRI(uq->uq_thread))
1856 pri = UPRI(uq->uq_thread);
1860 if (pri > uq_owner->uq_inherited_pri)
1861 pri = uq_owner->uq_inherited_pri;
1862 thread_lock(pi->pi_owner);
1863 sched_lend_user_prio(pi->pi_owner, pri);
1864 thread_unlock(pi->pi_owner);
1865 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1866 umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1871 * Insert a PI mutex into owned list.
1874 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1876 struct umtx_q *uq_owner;
1878 uq_owner = owner->td_umtxq;
1879 mtx_assert(&umtx_lock, MA_OWNED);
1880 MPASS(pi->pi_owner == NULL);
1881 pi->pi_owner = owner;
1882 TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1886 * Disown a PI mutex, and remove it from the owned list.
1889 umtx_pi_disown(struct umtx_pi *pi)
1892 mtx_assert(&umtx_lock, MA_OWNED);
1893 TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1894 pi->pi_owner = NULL;
1898 * Claim ownership of a PI mutex.
1901 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1906 mtx_lock(&umtx_lock);
1907 if (pi->pi_owner == owner) {
1908 mtx_unlock(&umtx_lock);
1912 if (pi->pi_owner != NULL) {
1914 * userland may have already messed the mutex, sigh.
1916 mtx_unlock(&umtx_lock);
1919 umtx_pi_setowner(pi, owner);
1920 uq = TAILQ_FIRST(&pi->pi_blocked);
1922 pri = UPRI(uq->uq_thread);
1924 if (pri < UPRI(owner))
1925 sched_lend_user_prio(owner, pri);
1926 thread_unlock(owner);
1928 mtx_unlock(&umtx_lock);
1933 * Adjust a thread's order position in its blocked PI mutex,
1934 * this may result new priority propagating process.
1937 umtx_pi_adjust(struct thread *td, u_char oldpri)
1943 mtx_lock(&umtx_lock);
1945 * Pick up the lock that td is blocked on.
1947 pi = uq->uq_pi_blocked;
1949 umtx_pi_adjust_thread(pi, td);
1950 umtx_repropagate_priority(pi);
1952 mtx_unlock(&umtx_lock);
1956 * Sleep on a PI mutex.
1959 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
1960 const char *wmesg, struct umtx_abs_timeout *timo, bool shared)
1962 struct thread *td, *td1;
1966 struct umtxq_chain *uc;
1968 uc = umtxq_getchain(&pi->pi_key);
1972 KASSERT(td == curthread, ("inconsistent uq_thread"));
1973 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
1974 KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1976 mtx_lock(&umtx_lock);
1977 if (pi->pi_owner == NULL) {
1978 mtx_unlock(&umtx_lock);
1979 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
1980 mtx_lock(&umtx_lock);
1982 if (pi->pi_owner == NULL)
1983 umtx_pi_setowner(pi, td1);
1984 PROC_UNLOCK(td1->td_proc);
1988 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1989 pri = UPRI(uq1->uq_thread);
1995 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1997 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1999 uq->uq_pi_blocked = pi;
2001 td->td_flags |= TDF_UPIBLOCKED;
2003 umtx_propagate_priority(td);
2004 mtx_unlock(&umtx_lock);
2005 umtxq_unbusy(&uq->uq_key);
2007 error = umtxq_sleep(uq, wmesg, timo);
2010 mtx_lock(&umtx_lock);
2011 uq->uq_pi_blocked = NULL;
2013 td->td_flags &= ~TDF_UPIBLOCKED;
2015 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
2016 umtx_repropagate_priority(pi);
2017 mtx_unlock(&umtx_lock);
2018 umtxq_unlock(&uq->uq_key);
2024 * Add reference count for a PI mutex.
2027 umtx_pi_ref(struct umtx_pi *pi)
2030 UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
2035 * Decrease reference count for a PI mutex, if the counter
2036 * is decreased to zero, its memory space is freed.
2039 umtx_pi_unref(struct umtx_pi *pi)
2041 struct umtxq_chain *uc;
2043 uc = umtxq_getchain(&pi->pi_key);
2044 UMTXQ_LOCKED_ASSERT(uc);
2045 KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
2046 if (--pi->pi_refcount == 0) {
2047 mtx_lock(&umtx_lock);
2048 if (pi->pi_owner != NULL)
2050 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
2051 ("blocked queue not empty"));
2052 mtx_unlock(&umtx_lock);
2053 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
2059 * Find a PI mutex in hash table.
2062 umtx_pi_lookup(struct umtx_key *key)
2064 struct umtxq_chain *uc;
2067 uc = umtxq_getchain(key);
2068 UMTXQ_LOCKED_ASSERT(uc);
2070 TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
2071 if (umtx_key_match(&pi->pi_key, key)) {
2079 * Insert a PI mutex into hash table.
2082 umtx_pi_insert(struct umtx_pi *pi)
2084 struct umtxq_chain *uc;
2086 uc = umtxq_getchain(&pi->pi_key);
2087 UMTXQ_LOCKED_ASSERT(uc);
2088 TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
2092 * Drop a PI mutex and wakeup a top waiter.
2095 umtx_pi_drop(struct thread *td, struct umtx_key *key, bool rb, int *count)
2097 struct umtx_q *uq_first, *uq_first2, *uq_me;
2098 struct umtx_pi *pi, *pi2;
2101 UMTXQ_ASSERT_LOCKED_BUSY(key);
2102 *count = umtxq_count_pi(key, &uq_first);
2103 if (uq_first != NULL) {
2104 mtx_lock(&umtx_lock);
2105 pi = uq_first->uq_pi_blocked;
2106 KASSERT(pi != NULL, ("pi == NULL?"));
2107 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2108 mtx_unlock(&umtx_lock);
2109 /* userland messed the mutex */
2112 uq_me = td->td_umtxq;
2113 if (pi->pi_owner == td)
2115 /* get highest priority thread which is still sleeping. */
2116 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2117 while (uq_first != NULL &&
2118 (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2119 uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2122 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2123 uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2124 if (uq_first2 != NULL) {
2125 if (pri > UPRI(uq_first2->uq_thread))
2126 pri = UPRI(uq_first2->uq_thread);
2130 sched_lend_user_prio(td, pri);
2132 mtx_unlock(&umtx_lock);
2134 umtxq_signal_thread(uq_first);
2136 pi = umtx_pi_lookup(key);
2138 * A umtx_pi can exist if a signal or timeout removed the
2139 * last waiter from the umtxq, but there is still
2140 * a thread in do_lock_pi() holding the umtx_pi.
2144 * The umtx_pi can be unowned, such as when a thread
2145 * has just entered do_lock_pi(), allocated the
2146 * umtx_pi, and unlocked the umtxq.
2147 * If the current thread owns it, it must disown it.
2149 mtx_lock(&umtx_lock);
2150 if (pi->pi_owner == td)
2152 mtx_unlock(&umtx_lock);
2162 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
2163 struct _umtx_time *timeout, int try)
2165 struct umtx_abs_timeout timo;
2167 struct umtx_pi *pi, *new_pi;
2168 uint32_t id, old_owner, owner, old;
2174 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2175 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2179 if (timeout != NULL)
2180 umtx_abs_timeout_init2(&timo, timeout);
2182 umtxq_lock(&uq->uq_key);
2183 pi = umtx_pi_lookup(&uq->uq_key);
2185 new_pi = umtx_pi_alloc(M_NOWAIT);
2186 if (new_pi == NULL) {
2187 umtxq_unlock(&uq->uq_key);
2188 new_pi = umtx_pi_alloc(M_WAITOK);
2189 umtxq_lock(&uq->uq_key);
2190 pi = umtx_pi_lookup(&uq->uq_key);
2192 umtx_pi_free(new_pi);
2196 if (new_pi != NULL) {
2197 new_pi->pi_key = uq->uq_key;
2198 umtx_pi_insert(new_pi);
2203 umtxq_unlock(&uq->uq_key);
2206 * Care must be exercised when dealing with umtx structure. It
2207 * can fault on any access.
2211 * Try the uncontested case. This should be done in userland.
2213 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
2214 /* The address was invalid. */
2219 /* The acquire succeeded. */
2221 MPASS(owner == UMUTEX_UNOWNED);
2226 if (owner == UMUTEX_RB_NOTRECOV) {
2227 error = ENOTRECOVERABLE;
2232 * Avoid overwriting a possible error from sleep due
2233 * to the pending signal with suspension check result.
2236 error = thread_check_susp(td, true);
2241 /* If no one owns it but it is contested try to acquire it. */
2242 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
2244 rv = casueword32(&m->m_owner, owner, &owner,
2245 id | UMUTEX_CONTESTED);
2246 /* The address was invalid. */
2253 error = thread_check_susp(td, true);
2259 * If this failed the lock could
2266 MPASS(owner == old_owner);
2267 umtxq_lock(&uq->uq_key);
2268 umtxq_busy(&uq->uq_key);
2269 error = umtx_pi_claim(pi, td);
2270 umtxq_unbusy(&uq->uq_key);
2271 umtxq_unlock(&uq->uq_key);
2274 * Since we're going to return an
2275 * error, restore the m_owner to its
2276 * previous, unowned state to avoid
2277 * compounding the problem.
2279 (void)casuword32(&m->m_owner,
2280 id | UMUTEX_CONTESTED, old_owner);
2282 if (error == 0 && old_owner == UMUTEX_RB_OWNERDEAD)
2287 if ((owner & ~UMUTEX_CONTESTED) == id) {
2298 * If we caught a signal, we have retried and now
2304 umtxq_lock(&uq->uq_key);
2305 umtxq_busy(&uq->uq_key);
2306 umtxq_unlock(&uq->uq_key);
2309 * Set the contested bit so that a release in user space
2310 * knows to use the system call for unlock. If this fails
2311 * either some one else has acquired the lock or it has been
2314 rv = casueword32(&m->m_owner, owner, &old, owner |
2317 /* The address was invalid. */
2319 umtxq_unbusy_unlocked(&uq->uq_key);
2324 umtxq_unbusy_unlocked(&uq->uq_key);
2325 error = thread_check_susp(td, true);
2330 * The lock changed and we need to retry or we
2331 * lost a race to the thread unlocking the
2332 * umtx. Note that the UMUTEX_RB_OWNERDEAD
2333 * value for owner is impossible there.
2338 umtxq_lock(&uq->uq_key);
2340 /* We set the contested bit, sleep. */
2341 MPASS(old == owner);
2342 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
2343 "umtxpi", timeout == NULL ? NULL : &timo,
2344 (flags & USYNC_PROCESS_SHARED) != 0);
2348 error = thread_check_susp(td, false);
2353 umtxq_lock(&uq->uq_key);
2355 umtxq_unlock(&uq->uq_key);
2357 umtx_key_release(&uq->uq_key);
2362 * Unlock a PI mutex.
2365 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2367 struct umtx_key key;
2368 uint32_t id, new_owner, old, owner;
2375 * Make sure we own this mtx.
2377 error = fueword32(&m->m_owner, &owner);
2381 if ((owner & ~UMUTEX_CONTESTED) != id)
2384 new_owner = umtx_unlock_val(flags, rb);
2386 /* This should be done in userland */
2387 if ((owner & UMUTEX_CONTESTED) == 0) {
2388 error = casueword32(&m->m_owner, owner, &old, new_owner);
2392 error = thread_check_susp(td, true);
2402 /* We should only ever be in here for contested locks */
2403 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2404 TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2410 error = umtx_pi_drop(td, &key, rb, &count);
2414 umtx_key_release(&key);
2415 /* userland messed the mutex */
2421 * When unlocking the umtx, it must be marked as unowned if
2422 * there is zero or one thread only waiting for it.
2423 * Otherwise, it must be marked as contested.
2427 new_owner |= UMUTEX_CONTESTED;
2429 error = casueword32(&m->m_owner, owner, &old, new_owner);
2431 error = thread_check_susp(td, false);
2435 umtxq_unbusy_unlocked(&key);
2436 umtx_key_release(&key);
2439 if (error == 0 && old != owner)
2448 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2449 struct _umtx_time *timeout, int try)
2451 struct umtx_abs_timeout timo;
2452 struct umtx_q *uq, *uq2;
2456 int error, pri, old_inherited_pri, su, rv;
2460 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2461 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2465 if (timeout != NULL)
2466 umtx_abs_timeout_init2(&timo, timeout);
2468 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2470 old_inherited_pri = uq->uq_inherited_pri;
2471 umtxq_lock(&uq->uq_key);
2472 umtxq_busy(&uq->uq_key);
2473 umtxq_unlock(&uq->uq_key);
2475 rv = fueword32(&m->m_ceilings[0], &ceiling);
2480 ceiling = RTP_PRIO_MAX - ceiling;
2481 if (ceiling > RTP_PRIO_MAX) {
2486 mtx_lock(&umtx_lock);
2487 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2488 mtx_unlock(&umtx_lock);
2492 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2493 uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2495 if (uq->uq_inherited_pri < UPRI(td))
2496 sched_lend_user_prio(td, uq->uq_inherited_pri);
2499 mtx_unlock(&umtx_lock);
2501 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2502 id | UMUTEX_CONTESTED);
2503 /* The address was invalid. */
2509 MPASS(owner == UMUTEX_CONTESTED);
2514 if (owner == UMUTEX_RB_OWNERDEAD) {
2515 rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2516 &owner, id | UMUTEX_CONTESTED);
2522 MPASS(owner == UMUTEX_RB_OWNERDEAD);
2523 error = EOWNERDEAD; /* success */
2528 * rv == 1, only check for suspension if we
2529 * did not already catched a signal. If we
2530 * get an error from the check, the same
2531 * condition is checked by the umtxq_sleep()
2532 * call below, so we should obliterate the
2533 * error to not skip the last loop iteration.
2536 error = thread_check_susp(td, false);
2545 } else if (owner == UMUTEX_RB_NOTRECOV) {
2546 error = ENOTRECOVERABLE;
2553 * If we caught a signal, we have retried and now
2559 umtxq_lock(&uq->uq_key);
2561 umtxq_unbusy(&uq->uq_key);
2562 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2565 umtxq_unlock(&uq->uq_key);
2567 mtx_lock(&umtx_lock);
2568 uq->uq_inherited_pri = old_inherited_pri;
2570 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2571 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2573 if (pri > UPRI(uq2->uq_thread))
2574 pri = UPRI(uq2->uq_thread);
2577 if (pri > uq->uq_inherited_pri)
2578 pri = uq->uq_inherited_pri;
2580 sched_lend_user_prio(td, pri);
2582 mtx_unlock(&umtx_lock);
2585 if (error != 0 && error != EOWNERDEAD) {
2586 mtx_lock(&umtx_lock);
2587 uq->uq_inherited_pri = old_inherited_pri;
2589 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2590 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2592 if (pri > UPRI(uq2->uq_thread))
2593 pri = UPRI(uq2->uq_thread);
2596 if (pri > uq->uq_inherited_pri)
2597 pri = uq->uq_inherited_pri;
2599 sched_lend_user_prio(td, pri);
2601 mtx_unlock(&umtx_lock);
2605 umtxq_unbusy_unlocked(&uq->uq_key);
2606 umtx_key_release(&uq->uq_key);
2611 * Unlock a PP mutex.
2614 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2616 struct umtx_key key;
2617 struct umtx_q *uq, *uq2;
2619 uint32_t id, owner, rceiling;
2620 int error, pri, new_inherited_pri, su;
2624 su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2627 * Make sure we own this mtx.
2629 error = fueword32(&m->m_owner, &owner);
2633 if ((owner & ~UMUTEX_CONTESTED) != id)
2636 error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2641 new_inherited_pri = PRI_MAX;
2643 rceiling = RTP_PRIO_MAX - rceiling;
2644 if (rceiling > RTP_PRIO_MAX)
2646 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2649 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2650 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2657 * For priority protected mutex, always set unlocked state
2658 * to UMUTEX_CONTESTED, so that userland always enters kernel
2659 * to lock the mutex, it is necessary because thread priority
2660 * has to be adjusted for such mutex.
2662 error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2667 umtxq_signal(&key, 1);
2674 mtx_lock(&umtx_lock);
2676 uq->uq_inherited_pri = new_inherited_pri;
2678 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2679 uq2 = TAILQ_FIRST(&pi->pi_blocked);
2681 if (pri > UPRI(uq2->uq_thread))
2682 pri = UPRI(uq2->uq_thread);
2685 if (pri > uq->uq_inherited_pri)
2686 pri = uq->uq_inherited_pri;
2688 sched_lend_user_prio(td, pri);
2690 mtx_unlock(&umtx_lock);
2692 umtx_key_release(&key);
2697 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2698 uint32_t *old_ceiling)
2701 uint32_t flags, id, owner, save_ceiling;
2704 error = fueword32(&m->m_flags, &flags);
2707 if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2709 if (ceiling > RTP_PRIO_MAX)
2713 if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2714 TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2718 umtxq_lock(&uq->uq_key);
2719 umtxq_busy(&uq->uq_key);
2720 umtxq_unlock(&uq->uq_key);
2722 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2728 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2729 id | UMUTEX_CONTESTED);
2736 MPASS(owner == UMUTEX_CONTESTED);
2737 rv = suword32(&m->m_ceilings[0], ceiling);
2738 rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2739 error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2743 if ((owner & ~UMUTEX_CONTESTED) == id) {
2744 rv = suword32(&m->m_ceilings[0], ceiling);
2745 error = rv == 0 ? 0 : EFAULT;
2749 if (owner == UMUTEX_RB_OWNERDEAD) {
2752 } else if (owner == UMUTEX_RB_NOTRECOV) {
2753 error = ENOTRECOVERABLE;
2758 * If we caught a signal, we have retried and now
2765 * We set the contested bit, sleep. Otherwise the lock changed
2766 * and we need to retry or we lost a race to the thread
2767 * unlocking the umtx.
2769 umtxq_lock(&uq->uq_key);
2771 umtxq_unbusy(&uq->uq_key);
2772 error = umtxq_sleep(uq, "umtxpp", NULL);
2774 umtxq_unlock(&uq->uq_key);
2776 umtxq_lock(&uq->uq_key);
2778 umtxq_signal(&uq->uq_key, INT_MAX);
2779 umtxq_unbusy(&uq->uq_key);
2780 umtxq_unlock(&uq->uq_key);
2781 umtx_key_release(&uq->uq_key);
2782 if (error == 0 && old_ceiling != NULL) {
2783 rv = suword32(old_ceiling, save_ceiling);
2784 error = rv == 0 ? 0 : EFAULT;
2790 * Lock a userland POSIX mutex.
2793 do_lock_umutex(struct thread *td, struct umutex *m,
2794 struct _umtx_time *timeout, int mode)
2799 error = fueword32(&m->m_flags, &flags);
2803 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2805 error = do_lock_normal(td, m, flags, timeout, mode);
2807 case UMUTEX_PRIO_INHERIT:
2808 error = do_lock_pi(td, m, flags, timeout, mode);
2810 case UMUTEX_PRIO_PROTECT:
2811 error = do_lock_pp(td, m, flags, timeout, mode);
2816 if (timeout == NULL) {
2817 if (error == EINTR && mode != _UMUTEX_WAIT)
2820 /* Timed-locking is not restarted. */
2821 if (error == ERESTART)
2828 * Unlock a userland POSIX mutex.
2831 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2836 error = fueword32(&m->m_flags, &flags);
2840 switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2842 return (do_unlock_normal(td, m, flags, rb));
2843 case UMUTEX_PRIO_INHERIT:
2844 return (do_unlock_pi(td, m, flags, rb));
2845 case UMUTEX_PRIO_PROTECT:
2846 return (do_unlock_pp(td, m, flags, rb));
2853 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2854 struct timespec *timeout, u_long wflags)
2856 struct umtx_abs_timeout timo;
2858 uint32_t flags, clockid, hasw;
2862 error = fueword32(&cv->c_flags, &flags);
2865 error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2869 if ((wflags & CVWAIT_CLOCKID) != 0) {
2870 error = fueword32(&cv->c_clockid, &clockid);
2872 umtx_key_release(&uq->uq_key);
2875 if (clockid < CLOCK_REALTIME ||
2876 clockid >= CLOCK_THREAD_CPUTIME_ID) {
2877 /* hmm, only HW clock id will work. */
2878 umtx_key_release(&uq->uq_key);
2882 clockid = CLOCK_REALTIME;
2885 umtxq_lock(&uq->uq_key);
2886 umtxq_busy(&uq->uq_key);
2888 umtxq_unlock(&uq->uq_key);
2891 * Set c_has_waiters to 1 before releasing user mutex, also
2892 * don't modify cache line when unnecessary.
2894 error = fueword32(&cv->c_has_waiters, &hasw);
2895 if (error == 0 && hasw == 0)
2896 suword32(&cv->c_has_waiters, 1);
2898 umtxq_unbusy_unlocked(&uq->uq_key);
2900 error = do_unlock_umutex(td, m, false);
2902 if (timeout != NULL)
2903 umtx_abs_timeout_init(&timo, clockid,
2904 (wflags & CVWAIT_ABSTIME) != 0, timeout);
2906 umtxq_lock(&uq->uq_key);
2908 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2912 if ((uq->uq_flags & UQF_UMTXQ) == 0)
2916 * This must be timeout,interrupted by signal or
2917 * surprious wakeup, clear c_has_waiter flag when
2920 umtxq_busy(&uq->uq_key);
2921 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2922 int oldlen = uq->uq_cur_queue->length;
2925 umtxq_unlock(&uq->uq_key);
2926 suword32(&cv->c_has_waiters, 0);
2927 umtxq_lock(&uq->uq_key);
2930 umtxq_unbusy(&uq->uq_key);
2931 if (error == ERESTART)
2935 umtxq_unlock(&uq->uq_key);
2936 umtx_key_release(&uq->uq_key);
2941 * Signal a userland condition variable.
2944 do_cv_signal(struct thread *td, struct ucond *cv)
2946 struct umtx_key key;
2947 int error, cnt, nwake;
2950 error = fueword32(&cv->c_flags, &flags);
2953 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2957 cnt = umtxq_count(&key);
2958 nwake = umtxq_signal(&key, 1);
2961 error = suword32(&cv->c_has_waiters, 0);
2968 umtx_key_release(&key);
2973 do_cv_broadcast(struct thread *td, struct ucond *cv)
2975 struct umtx_key key;
2979 error = fueword32(&cv->c_flags, &flags);
2982 if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2987 umtxq_signal(&key, INT_MAX);
2990 error = suword32(&cv->c_has_waiters, 0);
2994 umtxq_unbusy_unlocked(&key);
2996 umtx_key_release(&key);
3001 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
3002 struct _umtx_time *timeout)
3004 struct umtx_abs_timeout timo;
3006 uint32_t flags, wrflags;
3007 int32_t state, oldstate;
3008 int32_t blocked_readers;
3009 int error, error1, rv;
3012 error = fueword32(&rwlock->rw_flags, &flags);
3015 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3019 if (timeout != NULL)
3020 umtx_abs_timeout_init2(&timo, timeout);
3022 wrflags = URWLOCK_WRITE_OWNER;
3023 if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
3024 wrflags |= URWLOCK_WRITE_WAITERS;
3027 rv = fueword32(&rwlock->rw_state, &state);
3029 umtx_key_release(&uq->uq_key);
3033 /* try to lock it */
3034 while (!(state & wrflags)) {
3035 if (__predict_false(URWLOCK_READER_COUNT(state) ==
3036 URWLOCK_MAX_READERS)) {
3037 umtx_key_release(&uq->uq_key);
3040 rv = casueword32(&rwlock->rw_state, state,
3041 &oldstate, state + 1);
3043 umtx_key_release(&uq->uq_key);
3047 MPASS(oldstate == state);
3048 umtx_key_release(&uq->uq_key);
3051 error = thread_check_susp(td, true);
3060 /* grab monitor lock */
3061 umtxq_lock(&uq->uq_key);
3062 umtxq_busy(&uq->uq_key);
3063 umtxq_unlock(&uq->uq_key);
3066 * re-read the state, in case it changed between the try-lock above
3067 * and the check below
3069 rv = fueword32(&rwlock->rw_state, &state);
3073 /* set read contention bit */
3074 while (error == 0 && (state & wrflags) &&
3075 !(state & URWLOCK_READ_WAITERS)) {
3076 rv = casueword32(&rwlock->rw_state, state,
3077 &oldstate, state | URWLOCK_READ_WAITERS);
3083 MPASS(oldstate == state);
3087 error = thread_check_susp(td, false);
3092 umtxq_unbusy_unlocked(&uq->uq_key);
3096 /* state is changed while setting flags, restart */
3097 if (!(state & wrflags)) {
3098 umtxq_unbusy_unlocked(&uq->uq_key);
3099 error = thread_check_susp(td, true);
3107 * Contention bit is set, before sleeping, increase
3108 * read waiter count.
3110 rv = fueword32(&rwlock->rw_blocked_readers,
3113 umtxq_unbusy_unlocked(&uq->uq_key);
3117 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
3119 while (state & wrflags) {
3120 umtxq_lock(&uq->uq_key);
3122 umtxq_unbusy(&uq->uq_key);
3124 error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
3127 umtxq_busy(&uq->uq_key);
3129 umtxq_unlock(&uq->uq_key);
3132 rv = fueword32(&rwlock->rw_state, &state);
3139 /* decrease read waiter count, and may clear read contention bit */
3140 rv = fueword32(&rwlock->rw_blocked_readers,
3143 umtxq_unbusy_unlocked(&uq->uq_key);
3147 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
3148 if (blocked_readers == 1) {
3149 rv = fueword32(&rwlock->rw_state, &state);
3151 umtxq_unbusy_unlocked(&uq->uq_key);
3156 rv = casueword32(&rwlock->rw_state, state,
3157 &oldstate, state & ~URWLOCK_READ_WAITERS);
3163 MPASS(oldstate == state);
3167 error1 = thread_check_susp(td, false);
3176 umtxq_unbusy_unlocked(&uq->uq_key);
3180 umtx_key_release(&uq->uq_key);
3181 if (error == ERESTART)
3187 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
3189 struct umtx_abs_timeout timo;
3192 int32_t state, oldstate;
3193 int32_t blocked_writers;
3194 int32_t blocked_readers;
3195 int error, error1, rv;
3198 error = fueword32(&rwlock->rw_flags, &flags);
3201 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3205 if (timeout != NULL)
3206 umtx_abs_timeout_init2(&timo, timeout);
3208 blocked_readers = 0;
3210 rv = fueword32(&rwlock->rw_state, &state);
3212 umtx_key_release(&uq->uq_key);
3215 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
3216 URWLOCK_READER_COUNT(state) == 0) {
3217 rv = casueword32(&rwlock->rw_state, state,
3218 &oldstate, state | URWLOCK_WRITE_OWNER);
3220 umtx_key_release(&uq->uq_key);
3224 MPASS(oldstate == state);
3225 umtx_key_release(&uq->uq_key);
3229 error = thread_check_susp(td, true);
3235 if ((state & (URWLOCK_WRITE_OWNER |
3236 URWLOCK_WRITE_WAITERS)) == 0 &&
3237 blocked_readers != 0) {
3238 umtxq_lock(&uq->uq_key);
3239 umtxq_busy(&uq->uq_key);
3240 umtxq_signal_queue(&uq->uq_key, INT_MAX,
3242 umtxq_unbusy(&uq->uq_key);
3243 umtxq_unlock(&uq->uq_key);
3249 /* grab monitor lock */
3250 umtxq_lock(&uq->uq_key);
3251 umtxq_busy(&uq->uq_key);
3252 umtxq_unlock(&uq->uq_key);
3255 * Re-read the state, in case it changed between the
3256 * try-lock above and the check below.
3258 rv = fueword32(&rwlock->rw_state, &state);
3262 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
3263 URWLOCK_READER_COUNT(state) != 0) &&
3264 (state & URWLOCK_WRITE_WAITERS) == 0) {
3265 rv = casueword32(&rwlock->rw_state, state,
3266 &oldstate, state | URWLOCK_WRITE_WAITERS);
3272 MPASS(oldstate == state);
3276 error = thread_check_susp(td, false);
3281 umtxq_unbusy_unlocked(&uq->uq_key);
3285 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
3286 URWLOCK_READER_COUNT(state) == 0) {
3287 umtxq_unbusy_unlocked(&uq->uq_key);
3288 error = thread_check_susp(td, false);
3294 rv = fueword32(&rwlock->rw_blocked_writers,
3297 umtxq_unbusy_unlocked(&uq->uq_key);
3301 suword32(&rwlock->rw_blocked_writers, blocked_writers + 1);
3303 while ((state & URWLOCK_WRITE_OWNER) ||
3304 URWLOCK_READER_COUNT(state) != 0) {
3305 umtxq_lock(&uq->uq_key);
3306 umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3307 umtxq_unbusy(&uq->uq_key);
3309 error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
3312 umtxq_busy(&uq->uq_key);
3313 umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3314 umtxq_unlock(&uq->uq_key);
3317 rv = fueword32(&rwlock->rw_state, &state);
3324 rv = fueword32(&rwlock->rw_blocked_writers,
3327 umtxq_unbusy_unlocked(&uq->uq_key);
3331 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
3332 if (blocked_writers == 1) {
3333 rv = fueword32(&rwlock->rw_state, &state);
3335 umtxq_unbusy_unlocked(&uq->uq_key);
3340 rv = casueword32(&rwlock->rw_state, state,
3341 &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3347 MPASS(oldstate == state);
3351 error1 = thread_check_susp(td, false);
3353 * We are leaving the URWLOCK_WRITE_WAITERS
3354 * behind, but this should not harm the
3363 rv = fueword32(&rwlock->rw_blocked_readers,
3366 umtxq_unbusy_unlocked(&uq->uq_key);
3371 blocked_readers = 0;
3373 umtxq_unbusy_unlocked(&uq->uq_key);
3376 umtx_key_release(&uq->uq_key);
3377 if (error == ERESTART)
3383 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3387 int32_t state, oldstate;
3388 int error, rv, q, count;
3391 error = fueword32(&rwlock->rw_flags, &flags);
3394 error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3398 error = fueword32(&rwlock->rw_state, &state);
3403 if (state & URWLOCK_WRITE_OWNER) {
3405 rv = casueword32(&rwlock->rw_state, state,
3406 &oldstate, state & ~URWLOCK_WRITE_OWNER);
3413 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3417 error = thread_check_susp(td, true);
3423 } else if (URWLOCK_READER_COUNT(state) != 0) {
3425 rv = casueword32(&rwlock->rw_state, state,
3426 &oldstate, state - 1);
3433 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3437 error = thread_check_susp(td, true);
3450 if (!(flags & URWLOCK_PREFER_READER)) {
3451 if (state & URWLOCK_WRITE_WAITERS) {
3453 q = UMTX_EXCLUSIVE_QUEUE;
3454 } else if (state & URWLOCK_READ_WAITERS) {
3456 q = UMTX_SHARED_QUEUE;
3459 if (state & URWLOCK_READ_WAITERS) {
3461 q = UMTX_SHARED_QUEUE;
3462 } else if (state & URWLOCK_WRITE_WAITERS) {
3464 q = UMTX_EXCLUSIVE_QUEUE;
3469 umtxq_lock(&uq->uq_key);
3470 umtxq_busy(&uq->uq_key);
3471 umtxq_signal_queue(&uq->uq_key, count, q);
3472 umtxq_unbusy(&uq->uq_key);
3473 umtxq_unlock(&uq->uq_key);
3476 umtx_key_release(&uq->uq_key);
3480 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3482 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3484 struct umtx_abs_timeout timo;
3486 uint32_t flags, count, count1;
3490 error = fueword32(&sem->_flags, &flags);
3493 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3497 if (timeout != NULL)
3498 umtx_abs_timeout_init2(&timo, timeout);
3501 umtxq_lock(&uq->uq_key);
3502 umtxq_busy(&uq->uq_key);
3504 umtxq_unlock(&uq->uq_key);
3505 rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3507 rv1 = fueword32(&sem->_count, &count);
3508 if (rv == -1 || (rv == 0 && (rv1 == -1 || count != 0)) ||
3509 (rv == 1 && count1 == 0)) {
3510 umtxq_lock(&uq->uq_key);
3511 umtxq_unbusy(&uq->uq_key);
3513 umtxq_unlock(&uq->uq_key);
3515 rv = thread_check_susp(td, true);
3523 error = rv == -1 ? EFAULT : 0;
3526 umtxq_lock(&uq->uq_key);
3527 umtxq_unbusy(&uq->uq_key);
3529 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3531 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3535 /* A relative timeout cannot be restarted. */
3536 if (error == ERESTART && timeout != NULL &&
3537 (timeout->_flags & UMTX_ABSTIME) == 0)
3540 umtxq_unlock(&uq->uq_key);
3542 umtx_key_release(&uq->uq_key);
3547 * Signal a userland semaphore.
3550 do_sem_wake(struct thread *td, struct _usem *sem)
3552 struct umtx_key key;
3556 error = fueword32(&sem->_flags, &flags);
3559 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3563 cnt = umtxq_count(&key);
3566 * Check if count is greater than 0, this means the memory is
3567 * still being referenced by user code, so we can safely
3568 * update _has_waiters flag.
3572 error = suword32(&sem->_has_waiters, 0);
3577 umtxq_signal(&key, 1);
3581 umtx_key_release(&key);
3587 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3589 struct umtx_abs_timeout timo;
3591 uint32_t count, flags;
3595 flags = fuword32(&sem->_flags);
3596 if (timeout != NULL)
3597 umtx_abs_timeout_init2(&timo, timeout);
3600 error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3603 umtxq_lock(&uq->uq_key);
3604 umtxq_busy(&uq->uq_key);
3606 umtxq_unlock(&uq->uq_key);
3607 rv = fueword32(&sem->_count, &count);
3609 umtxq_lock(&uq->uq_key);
3610 umtxq_unbusy(&uq->uq_key);
3612 umtxq_unlock(&uq->uq_key);
3613 umtx_key_release(&uq->uq_key);
3617 if (USEM_COUNT(count) != 0) {
3618 umtxq_lock(&uq->uq_key);
3619 umtxq_unbusy(&uq->uq_key);
3621 umtxq_unlock(&uq->uq_key);
3622 umtx_key_release(&uq->uq_key);
3625 if (count == USEM_HAS_WAITERS)
3627 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3630 umtxq_lock(&uq->uq_key);
3631 umtxq_unbusy(&uq->uq_key);
3633 umtxq_unlock(&uq->uq_key);
3634 umtx_key_release(&uq->uq_key);
3637 rv = thread_check_susp(td, true);
3642 umtxq_lock(&uq->uq_key);
3643 umtxq_unbusy(&uq->uq_key);
3645 error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3647 if ((uq->uq_flags & UQF_UMTXQ) == 0)
3651 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3652 /* A relative timeout cannot be restarted. */
3653 if (error == ERESTART)
3655 if (error == EINTR) {
3656 umtx_abs_timeout_update(&timo);
3657 timespecsub(&timo.end, &timo.cur,
3658 &timeout->_timeout);
3662 umtxq_unlock(&uq->uq_key);
3663 umtx_key_release(&uq->uq_key);
3668 * Signal a userland semaphore.
3671 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3673 struct umtx_key key;
3675 uint32_t count, flags;
3677 rv = fueword32(&sem->_flags, &flags);
3680 if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3684 cnt = umtxq_count(&key);
3687 * If this was the last sleeping thread, clear the waiters
3692 rv = fueword32(&sem->_count, &count);
3693 while (rv != -1 && count & USEM_HAS_WAITERS) {
3694 rv = casueword32(&sem->_count, count, &count,
3695 count & ~USEM_HAS_WAITERS);
3697 rv = thread_check_susp(td, true);
3710 umtxq_signal(&key, 1);
3714 umtx_key_release(&key);
3718 #ifdef COMPAT_FREEBSD10
3720 freebsd10__umtx_lock(struct thread *td, struct freebsd10__umtx_lock_args *uap)
3722 return (do_lock_umtx(td, uap->umtx, td->td_tid, 0));
3726 freebsd10__umtx_unlock(struct thread *td,
3727 struct freebsd10__umtx_unlock_args *uap)
3729 return (do_unlock_umtx(td, uap->umtx, td->td_tid));
3734 umtx_copyin_timeout(const void *uaddr, struct timespec *tsp)
3738 error = copyin(uaddr, tsp, sizeof(*tsp));
3740 if (tsp->tv_sec < 0 ||
3741 tsp->tv_nsec >= 1000000000 ||
3749 umtx_copyin_umtx_time(const void *uaddr, size_t size, struct _umtx_time *tp)
3753 if (size <= sizeof(tp->_timeout)) {
3754 tp->_clockid = CLOCK_REALTIME;
3756 error = copyin(uaddr, &tp->_timeout, sizeof(tp->_timeout));
3758 error = copyin(uaddr, tp, sizeof(*tp));
3761 if (tp->_timeout.tv_sec < 0 ||
3762 tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3768 umtx_copyin_robust_lists(const void *uaddr, size_t size,
3769 struct umtx_robust_lists_params *rb)
3772 if (size > sizeof(*rb))
3774 return (copyin(uaddr, rb, size));
3778 umtx_copyout_timeout(void *uaddr, size_t sz, struct timespec *tsp)
3782 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
3783 * and we're only called if sz >= sizeof(timespec) as supplied in the
3786 KASSERT(sz >= sizeof(*tsp),
3787 ("umtx_copyops specifies incorrect sizes"));
3789 return (copyout(tsp, uaddr, sizeof(*tsp)));
3792 #ifdef COMPAT_FREEBSD10
3794 __umtx_op_lock_umtx(struct thread *td, struct _umtx_op_args *uap,
3795 const struct umtx_copyops *ops)
3797 struct timespec *ts, timeout;
3800 /* Allow a null timespec (wait forever). */
3801 if (uap->uaddr2 == NULL)
3804 error = ops->copyin_timeout(uap->uaddr2, &timeout);
3809 #ifdef COMPAT_FREEBSD32
3811 return (do_lock_umtx32(td, uap->obj, uap->val, ts));
3813 return (do_lock_umtx(td, uap->obj, uap->val, ts));
3817 __umtx_op_unlock_umtx(struct thread *td, struct _umtx_op_args *uap,
3818 const struct umtx_copyops *ops)
3820 #ifdef COMPAT_FREEBSD32
3822 return (do_unlock_umtx32(td, uap->obj, uap->val));
3824 return (do_unlock_umtx(td, uap->obj, uap->val));
3826 #endif /* COMPAT_FREEBSD10 */
3828 #if !defined(COMPAT_FREEBSD10)
3830 __umtx_op_unimpl(struct thread *td __unused, struct _umtx_op_args *uap __unused,
3831 const struct umtx_copyops *ops __unused)
3833 return (EOPNOTSUPP);
3835 #endif /* COMPAT_FREEBSD10 */
3838 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap,
3839 const struct umtx_copyops *ops)
3841 struct _umtx_time timeout, *tm_p;
3844 if (uap->uaddr2 == NULL)
3847 error = ops->copyin_umtx_time(
3848 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3853 return (do_wait(td, uap->obj, uap->val, tm_p, ops->compat32, 0));
3857 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap,
3858 const struct umtx_copyops *ops)
3860 struct _umtx_time timeout, *tm_p;
3863 if (uap->uaddr2 == NULL)
3866 error = ops->copyin_umtx_time(
3867 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3872 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3876 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap,
3877 const struct umtx_copyops *ops)
3879 struct _umtx_time *tm_p, timeout;
3882 if (uap->uaddr2 == NULL)
3885 error = ops->copyin_umtx_time(
3886 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3891 return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3895 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap,
3896 const struct umtx_copyops *ops __unused)
3899 return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3902 #define BATCH_SIZE 128
3904 __umtx_op_nwake_private_native(struct thread *td, struct _umtx_op_args *uap)
3906 char *uaddrs[BATCH_SIZE], **upp;
3907 int count, error, i, pos, tocopy;
3909 upp = (char **)uap->obj;
3911 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3913 tocopy = MIN(count, BATCH_SIZE);
3914 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3917 for (i = 0; i < tocopy; ++i) {
3918 kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3926 __umtx_op_nwake_private_compat32(struct thread *td, struct _umtx_op_args *uap)
3928 uint32_t uaddrs[BATCH_SIZE], *upp;
3929 int count, error, i, pos, tocopy;
3931 upp = (uint32_t *)uap->obj;
3933 for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3935 tocopy = MIN(count, BATCH_SIZE);
3936 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
3939 for (i = 0; i < tocopy; ++i) {
3940 kern_umtx_wake(td, (void *)(uintptr_t)uaddrs[i],
3949 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap,
3950 const struct umtx_copyops *ops)
3954 return (__umtx_op_nwake_private_compat32(td, uap));
3955 return (__umtx_op_nwake_private_native(td, uap));
3959 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap,
3960 const struct umtx_copyops *ops __unused)
3963 return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3967 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap,
3968 const struct umtx_copyops *ops)
3970 struct _umtx_time *tm_p, timeout;
3973 /* Allow a null timespec (wait forever). */
3974 if (uap->uaddr2 == NULL)
3977 error = ops->copyin_umtx_time(
3978 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3983 return (do_lock_umutex(td, uap->obj, tm_p, 0));
3987 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap,
3988 const struct umtx_copyops *ops __unused)
3991 return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
3995 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap,
3996 const struct umtx_copyops *ops)
3998 struct _umtx_time *tm_p, timeout;
4001 /* Allow a null timespec (wait forever). */
4002 if (uap->uaddr2 == NULL)
4005 error = ops->copyin_umtx_time(
4006 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4011 return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
4015 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap,
4016 const struct umtx_copyops *ops __unused)
4019 return (do_wake_umutex(td, uap->obj));
4023 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap,
4024 const struct umtx_copyops *ops __unused)
4027 return (do_unlock_umutex(td, uap->obj, false));
4031 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap,
4032 const struct umtx_copyops *ops __unused)
4035 return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
4039 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap,
4040 const struct umtx_copyops *ops)
4042 struct timespec *ts, timeout;
4045 /* Allow a null timespec (wait forever). */
4046 if (uap->uaddr2 == NULL)
4049 error = ops->copyin_timeout(uap->uaddr2, &timeout);
4054 return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
4058 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap,
4059 const struct umtx_copyops *ops __unused)
4062 return (do_cv_signal(td, uap->obj));
4066 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap,
4067 const struct umtx_copyops *ops __unused)
4070 return (do_cv_broadcast(td, uap->obj));
4074 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap,
4075 const struct umtx_copyops *ops)
4077 struct _umtx_time timeout;
4080 /* Allow a null timespec (wait forever). */
4081 if (uap->uaddr2 == NULL) {
4082 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4084 error = ops->copyin_umtx_time(uap->uaddr2,
4085 (size_t)uap->uaddr1, &timeout);
4088 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4094 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap,
4095 const struct umtx_copyops *ops)
4097 struct _umtx_time timeout;
4100 /* Allow a null timespec (wait forever). */
4101 if (uap->uaddr2 == NULL) {
4102 error = do_rw_wrlock(td, uap->obj, 0);
4104 error = ops->copyin_umtx_time(uap->uaddr2,
4105 (size_t)uap->uaddr1, &timeout);
4109 error = do_rw_wrlock(td, uap->obj, &timeout);
4115 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap,
4116 const struct umtx_copyops *ops __unused)
4119 return (do_rw_unlock(td, uap->obj));
4122 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4124 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap,
4125 const struct umtx_copyops *ops)
4127 struct _umtx_time *tm_p, timeout;
4130 /* Allow a null timespec (wait forever). */
4131 if (uap->uaddr2 == NULL)
4134 error = ops->copyin_umtx_time(
4135 uap->uaddr2, (size_t)uap->uaddr1, &timeout);
4140 return (do_sem_wait(td, uap->obj, tm_p));
4144 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap,
4145 const struct umtx_copyops *ops __unused)
4148 return (do_sem_wake(td, uap->obj));
4153 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap,
4154 const struct umtx_copyops *ops __unused)
4157 return (do_wake2_umutex(td, uap->obj, uap->val));
4161 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap,
4162 const struct umtx_copyops *ops)
4164 struct _umtx_time *tm_p, timeout;
4168 /* Allow a null timespec (wait forever). */
4169 if (uap->uaddr2 == NULL) {
4173 uasize = (size_t)uap->uaddr1;
4174 error = ops->copyin_umtx_time(uap->uaddr2, uasize, &timeout);
4179 error = do_sem2_wait(td, uap->obj, tm_p);
4180 if (error == EINTR && uap->uaddr2 != NULL &&
4181 (timeout._flags & UMTX_ABSTIME) == 0 &&
4182 uasize >= ops->umtx_time_sz + ops->timespec_sz) {
4183 error = ops->copyout_timeout(
4184 (void *)((uintptr_t)uap->uaddr2 + ops->umtx_time_sz),
4185 uasize - ops->umtx_time_sz, &timeout._timeout);
4195 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap,
4196 const struct umtx_copyops *ops __unused)
4199 return (do_sem2_wake(td, uap->obj));
4202 #define USHM_OBJ_UMTX(o) \
4203 ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
4205 #define USHMF_REG_LINKED 0x0001
4206 #define USHMF_OBJ_LINKED 0x0002
4207 struct umtx_shm_reg {
4208 TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
4209 LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
4210 struct umtx_key ushm_key;
4211 struct ucred *ushm_cred;
4212 struct shmfd *ushm_obj;
4217 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
4218 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
4220 static uma_zone_t umtx_shm_reg_zone;
4221 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
4222 static struct mtx umtx_shm_lock;
4223 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
4224 TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
4226 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
4229 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
4231 struct umtx_shm_reg_head d;
4232 struct umtx_shm_reg *reg, *reg1;
4235 mtx_lock(&umtx_shm_lock);
4236 TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
4237 mtx_unlock(&umtx_shm_lock);
4238 TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
4239 TAILQ_REMOVE(&d, reg, ushm_reg_link);
4240 umtx_shm_free_reg(reg);
4244 static struct task umtx_shm_reg_delfree_task =
4245 TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
4247 static struct umtx_shm_reg *
4248 umtx_shm_find_reg_locked(const struct umtx_key *key)
4250 struct umtx_shm_reg *reg;
4251 struct umtx_shm_reg_head *reg_head;
4253 KASSERT(key->shared, ("umtx_p_find_rg: private key"));
4254 mtx_assert(&umtx_shm_lock, MA_OWNED);
4255 reg_head = &umtx_shm_registry[key->hash];
4256 TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
4257 KASSERT(reg->ushm_key.shared,
4258 ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
4259 if (reg->ushm_key.info.shared.object ==
4260 key->info.shared.object &&
4261 reg->ushm_key.info.shared.offset ==
4262 key->info.shared.offset) {
4263 KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
4264 KASSERT(reg->ushm_refcnt > 0,
4265 ("reg %p refcnt 0 onlist", reg));
4266 KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
4267 ("reg %p not linked", reg));
4275 static struct umtx_shm_reg *
4276 umtx_shm_find_reg(const struct umtx_key *key)
4278 struct umtx_shm_reg *reg;
4280 mtx_lock(&umtx_shm_lock);
4281 reg = umtx_shm_find_reg_locked(key);
4282 mtx_unlock(&umtx_shm_lock);
4287 umtx_shm_free_reg(struct umtx_shm_reg *reg)
4290 chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
4291 crfree(reg->ushm_cred);
4292 shm_drop(reg->ushm_obj);
4293 uma_zfree(umtx_shm_reg_zone, reg);
4297 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
4301 mtx_assert(&umtx_shm_lock, MA_OWNED);
4302 KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
4304 res = reg->ushm_refcnt == 0;
4306 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
4307 TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
4308 reg, ushm_reg_link);
4309 reg->ushm_flags &= ~USHMF_REG_LINKED;
4311 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
4312 LIST_REMOVE(reg, ushm_obj_link);
4313 reg->ushm_flags &= ~USHMF_OBJ_LINKED;
4320 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
4326 object = reg->ushm_obj->shm_object;
4327 VM_OBJECT_WLOCK(object);
4328 object->flags |= OBJ_UMTXDEAD;
4329 VM_OBJECT_WUNLOCK(object);
4331 mtx_lock(&umtx_shm_lock);
4332 dofree = umtx_shm_unref_reg_locked(reg, force);
4333 mtx_unlock(&umtx_shm_lock);
4335 umtx_shm_free_reg(reg);
4339 umtx_shm_object_init(vm_object_t object)
4342 LIST_INIT(USHM_OBJ_UMTX(object));
4346 umtx_shm_object_terminated(vm_object_t object)
4348 struct umtx_shm_reg *reg, *reg1;
4351 if (LIST_EMPTY(USHM_OBJ_UMTX(object)))
4355 mtx_lock(&umtx_shm_lock);
4356 LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
4357 if (umtx_shm_unref_reg_locked(reg, true)) {
4358 TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
4363 mtx_unlock(&umtx_shm_lock);
4365 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
4369 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
4370 struct umtx_shm_reg **res)
4372 struct umtx_shm_reg *reg, *reg1;
4376 reg = umtx_shm_find_reg(key);
4381 cred = td->td_ucred;
4382 if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
4384 reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
4385 reg->ushm_refcnt = 1;
4386 bcopy(key, ®->ushm_key, sizeof(*key));
4387 reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR, false);
4388 reg->ushm_cred = crhold(cred);
4389 error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
4391 umtx_shm_free_reg(reg);
4394 mtx_lock(&umtx_shm_lock);
4395 reg1 = umtx_shm_find_reg_locked(key);
4397 mtx_unlock(&umtx_shm_lock);
4398 umtx_shm_free_reg(reg);
4403 TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
4404 LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
4406 reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
4407 mtx_unlock(&umtx_shm_lock);
4413 umtx_shm_alive(struct thread *td, void *addr)
4416 vm_map_entry_t entry;
4423 map = &td->td_proc->p_vmspace->vm_map;
4424 res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
4425 &object, &pindex, &prot, &wired);
4426 if (res != KERN_SUCCESS)
4431 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
4432 vm_map_lookup_done(map, entry);
4441 umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
4442 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
4443 mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
4444 for (i = 0; i < nitems(umtx_shm_registry); i++)
4445 TAILQ_INIT(&umtx_shm_registry[i]);
4449 umtx_shm(struct thread *td, void *addr, u_int flags)
4451 struct umtx_key key;
4452 struct umtx_shm_reg *reg;
4456 if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
4457 UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
4459 if ((flags & UMTX_SHM_ALIVE) != 0)
4460 return (umtx_shm_alive(td, addr));
4461 error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
4464 KASSERT(key.shared == 1, ("non-shared key"));
4465 if ((flags & UMTX_SHM_CREAT) != 0) {
4466 error = umtx_shm_create_reg(td, &key, ®);
4468 reg = umtx_shm_find_reg(&key);
4472 umtx_key_release(&key);
4475 KASSERT(reg != NULL, ("no reg"));
4476 if ((flags & UMTX_SHM_DESTROY) != 0) {
4477 umtx_shm_unref_reg(reg, true);
4481 error = mac_posixshm_check_open(td->td_ucred,
4482 reg->ushm_obj, FFLAGS(O_RDWR));
4485 error = shm_access(reg->ushm_obj, td->td_ucred,
4489 error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
4491 shm_hold(reg->ushm_obj);
4492 finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
4494 td->td_retval[0] = fd;
4498 umtx_shm_unref_reg(reg, false);
4503 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap,
4504 const struct umtx_copyops *ops __unused)
4507 return (umtx_shm(td, uap->uaddr1, uap->val));
4511 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap,
4512 const struct umtx_copyops *ops)
4514 struct umtx_robust_lists_params rb;
4517 if (ops->compat32) {
4518 if ((td->td_pflags2 & TDP2_COMPAT32RB) == 0 &&
4519 (td->td_rb_list != 0 || td->td_rbp_list != 0 ||
4520 td->td_rb_inact != 0))
4522 } else if ((td->td_pflags2 & TDP2_COMPAT32RB) != 0) {
4526 bzero(&rb, sizeof(rb));
4527 error = ops->copyin_robust_lists(uap->uaddr1, uap->val, &rb);
4532 td->td_pflags2 |= TDP2_COMPAT32RB;
4534 td->td_rb_list = rb.robust_list_offset;
4535 td->td_rbp_list = rb.robust_priv_list_offset;
4536 td->td_rb_inact = rb.robust_inact_offset;
4540 #if defined(__i386__) || defined(__amd64__)
4542 * Provide the standard 32-bit definitions for x86, since native/compat32 use a
4543 * 32-bit time_t there. Other architectures just need the i386 definitions
4544 * along with their standard compat32.
4546 struct timespecx32 {
4551 struct umtx_timex32 {
4552 struct timespecx32 _timeout;
4558 #define timespeci386 timespec32
4559 #define umtx_timei386 umtx_time32
4561 #else /* !__i386__ && !__amd64__ */
4562 /* 32-bit architectures can emulate i386, so define these almost everywhere. */
4563 struct timespeci386 {
4568 struct umtx_timei386 {
4569 struct timespeci386 _timeout;
4574 #if defined(__LP64__)
4575 #define timespecx32 timespec32
4576 #define umtx_timex32 umtx_time32
4581 umtx_copyin_robust_lists32(const void *uaddr, size_t size,
4582 struct umtx_robust_lists_params *rbp)
4584 struct umtx_robust_lists_params_compat32 rb32;
4587 if (size > sizeof(rb32))
4589 bzero(&rb32, sizeof(rb32));
4590 error = copyin(uaddr, &rb32, size);
4593 CP(rb32, *rbp, robust_list_offset);
4594 CP(rb32, *rbp, robust_priv_list_offset);
4595 CP(rb32, *rbp, robust_inact_offset);
4601 umtx_copyin_timeouti386(const void *uaddr, struct timespec *tsp)
4603 struct timespeci386 ts32;
4606 error = copyin(uaddr, &ts32, sizeof(ts32));
4608 if (ts32.tv_sec < 0 ||
4609 ts32.tv_nsec >= 1000000000 ||
4613 CP(ts32, *tsp, tv_sec);
4614 CP(ts32, *tsp, tv_nsec);
4621 umtx_copyin_umtx_timei386(const void *uaddr, size_t size, struct _umtx_time *tp)
4623 struct umtx_timei386 t32;
4626 t32._clockid = CLOCK_REALTIME;
4628 if (size <= sizeof(t32._timeout))
4629 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4631 error = copyin(uaddr, &t32, sizeof(t32));
4634 if (t32._timeout.tv_sec < 0 ||
4635 t32._timeout.tv_nsec >= 1000000000 || t32._timeout.tv_nsec < 0)
4637 TS_CP(t32, *tp, _timeout);
4638 CP(t32, *tp, _flags);
4639 CP(t32, *tp, _clockid);
4644 umtx_copyout_timeouti386(void *uaddr, size_t sz, struct timespec *tsp)
4646 struct timespeci386 remain32 = {
4647 .tv_sec = tsp->tv_sec,
4648 .tv_nsec = tsp->tv_nsec,
4652 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4653 * and we're only called if sz >= sizeof(timespec) as supplied in the
4656 KASSERT(sz >= sizeof(remain32),
4657 ("umtx_copyops specifies incorrect sizes"));
4659 return (copyout(&remain32, uaddr, sizeof(remain32)));
4661 #endif /* !__i386__ */
4663 #if defined(__i386__) || defined(__LP64__)
4665 umtx_copyin_timeoutx32(const void *uaddr, struct timespec *tsp)
4667 struct timespecx32 ts32;
4670 error = copyin(uaddr, &ts32, sizeof(ts32));
4672 if (ts32.tv_sec < 0 ||
4673 ts32.tv_nsec >= 1000000000 ||
4677 CP(ts32, *tsp, tv_sec);
4678 CP(ts32, *tsp, tv_nsec);
4685 umtx_copyin_umtx_timex32(const void *uaddr, size_t size, struct _umtx_time *tp)
4687 struct umtx_timex32 t32;
4690 t32._clockid = CLOCK_REALTIME;
4692 if (size <= sizeof(t32._timeout))
4693 error = copyin(uaddr, &t32._timeout, sizeof(t32._timeout));
4695 error = copyin(uaddr, &t32, sizeof(t32));
4698 if (t32._timeout.tv_sec < 0 ||
4699 t32._timeout.tv_nsec >= 1000000000 || t32._timeout.tv_nsec < 0)
4701 TS_CP(t32, *tp, _timeout);
4702 CP(t32, *tp, _flags);
4703 CP(t32, *tp, _clockid);
4708 umtx_copyout_timeoutx32(void *uaddr, size_t sz, struct timespec *tsp)
4710 struct timespecx32 remain32 = {
4711 .tv_sec = tsp->tv_sec,
4712 .tv_nsec = tsp->tv_nsec,
4716 * Should be guaranteed by the caller, sz == uaddr1 - sizeof(_umtx_time)
4717 * and we're only called if sz >= sizeof(timespec) as supplied in the
4720 KASSERT(sz >= sizeof(remain32),
4721 ("umtx_copyops specifies incorrect sizes"));
4723 return (copyout(&remain32, uaddr, sizeof(remain32)));
4725 #endif /* __i386__ || __LP64__ */
4727 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap,
4728 const struct umtx_copyops *umtx_ops);
4730 static const _umtx_op_func op_table[] = {
4731 #ifdef COMPAT_FREEBSD10
4732 [UMTX_OP_LOCK] = __umtx_op_lock_umtx,
4733 [UMTX_OP_UNLOCK] = __umtx_op_unlock_umtx,
4735 [UMTX_OP_LOCK] = __umtx_op_unimpl,
4736 [UMTX_OP_UNLOCK] = __umtx_op_unimpl,
4738 [UMTX_OP_WAIT] = __umtx_op_wait,
4739 [UMTX_OP_WAKE] = __umtx_op_wake,
4740 [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4741 [UMTX_OP_MUTEX_LOCK] = __umtx_op_lock_umutex,
4742 [UMTX_OP_MUTEX_UNLOCK] = __umtx_op_unlock_umutex,
4743 [UMTX_OP_SET_CEILING] = __umtx_op_set_ceiling,
4744 [UMTX_OP_CV_WAIT] = __umtx_op_cv_wait,
4745 [UMTX_OP_CV_SIGNAL] = __umtx_op_cv_signal,
4746 [UMTX_OP_CV_BROADCAST] = __umtx_op_cv_broadcast,
4747 [UMTX_OP_WAIT_UINT] = __umtx_op_wait_uint,
4748 [UMTX_OP_RW_RDLOCK] = __umtx_op_rw_rdlock,
4749 [UMTX_OP_RW_WRLOCK] = __umtx_op_rw_wrlock,
4750 [UMTX_OP_RW_UNLOCK] = __umtx_op_rw_unlock,
4751 [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4752 [UMTX_OP_WAKE_PRIVATE] = __umtx_op_wake_private,
4753 [UMTX_OP_MUTEX_WAIT] = __umtx_op_wait_umutex,
4754 [UMTX_OP_MUTEX_WAKE] = __umtx_op_wake_umutex,
4755 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4756 [UMTX_OP_SEM_WAIT] = __umtx_op_sem_wait,
4757 [UMTX_OP_SEM_WAKE] = __umtx_op_sem_wake,
4759 [UMTX_OP_SEM_WAIT] = __umtx_op_unimpl,
4760 [UMTX_OP_SEM_WAKE] = __umtx_op_unimpl,
4762 [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4763 [UMTX_OP_MUTEX_WAKE2] = __umtx_op_wake2_umutex,
4764 [UMTX_OP_SEM2_WAIT] = __umtx_op_sem2_wait,
4765 [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake,
4766 [UMTX_OP_SHM] = __umtx_op_shm,
4767 [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists,
4770 static const struct umtx_copyops umtx_native_ops = {
4771 .copyin_timeout = umtx_copyin_timeout,
4772 .copyin_umtx_time = umtx_copyin_umtx_time,
4773 .copyin_robust_lists = umtx_copyin_robust_lists,
4774 .copyout_timeout = umtx_copyout_timeout,
4775 .timespec_sz = sizeof(struct timespec),
4776 .umtx_time_sz = sizeof(struct _umtx_time),
4780 static const struct umtx_copyops umtx_native_opsi386 = {
4781 .copyin_timeout = umtx_copyin_timeouti386,
4782 .copyin_umtx_time = umtx_copyin_umtx_timei386,
4783 .copyin_robust_lists = umtx_copyin_robust_lists32,
4784 .copyout_timeout = umtx_copyout_timeouti386,
4785 .timespec_sz = sizeof(struct timespeci386),
4786 .umtx_time_sz = sizeof(struct umtx_timei386),
4791 #if defined(__i386__) || defined(__LP64__)
4792 /* i386 can emulate other 32-bit archs, too! */
4793 static const struct umtx_copyops umtx_native_opsx32 = {
4794 .copyin_timeout = umtx_copyin_timeoutx32,
4795 .copyin_umtx_time = umtx_copyin_umtx_timex32,
4796 .copyin_robust_lists = umtx_copyin_robust_lists32,
4797 .copyout_timeout = umtx_copyout_timeoutx32,
4798 .timespec_sz = sizeof(struct timespecx32),
4799 .umtx_time_sz = sizeof(struct umtx_timex32),
4803 #ifdef COMPAT_FREEBSD32
4805 #define umtx_native_ops32 umtx_native_opsi386
4807 #define umtx_native_ops32 umtx_native_opsx32
4809 #endif /* COMPAT_FREEBSD32 */
4810 #endif /* __i386__ || __LP64__ */
4812 #define UMTX_OP__FLAGS (UMTX_OP__32BIT | UMTX_OP__I386)
4815 kern__umtx_op(struct thread *td, void *obj, int op, unsigned long val,
4816 void *uaddr1, void *uaddr2, const struct umtx_copyops *ops)
4818 struct _umtx_op_args uap = {
4820 .op = op & ~UMTX_OP__FLAGS,
4826 if ((uap.op >= nitems(op_table)))
4828 return ((*op_table[uap.op])(td, &uap, ops));
4832 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4834 static const struct umtx_copyops *umtx_ops;
4836 umtx_ops = &umtx_native_ops;
4838 if ((uap->op & (UMTX_OP__32BIT | UMTX_OP__I386)) != 0) {
4839 if ((uap->op & UMTX_OP__I386) != 0)
4840 umtx_ops = &umtx_native_opsi386;
4842 umtx_ops = &umtx_native_opsx32;
4844 #elif !defined(__i386__)
4845 /* We consider UMTX_OP__32BIT a nop on !i386 ILP32. */
4846 if ((uap->op & UMTX_OP__I386) != 0)
4847 umtx_ops = &umtx_native_opsi386;
4849 /* Likewise, UMTX_OP__I386 is a nop on i386. */
4850 if ((uap->op & UMTX_OP__32BIT) != 0)
4851 umtx_ops = &umtx_native_opsx32;
4853 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr1,
4854 uap->uaddr2, umtx_ops));
4857 #ifdef COMPAT_FREEBSD32
4858 #ifdef COMPAT_FREEBSD10
4860 freebsd10_freebsd32_umtx_lock(struct thread *td,
4861 struct freebsd10_freebsd32_umtx_lock_args *uap)
4863 return (do_lock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid, NULL));
4867 freebsd10_freebsd32_umtx_unlock(struct thread *td,
4868 struct freebsd10_freebsd32_umtx_unlock_args *uap)
4870 return (do_unlock_umtx32(td, (uint32_t *)uap->umtx, td->td_tid));
4872 #endif /* COMPAT_FREEBSD10 */
4875 freebsd32__umtx_op(struct thread *td, struct freebsd32__umtx_op_args *uap)
4878 return (kern__umtx_op(td, uap->obj, uap->op, uap->val, uap->uaddr,
4879 uap->uaddr2, &umtx_native_ops32));
4881 #endif /* COMPAT_FREEBSD32 */
4884 umtx_thread_init(struct thread *td)
4887 td->td_umtxq = umtxq_alloc();
4888 td->td_umtxq->uq_thread = td;
4892 umtx_thread_fini(struct thread *td)
4895 umtxq_free(td->td_umtxq);
4899 * It will be called when new thread is created, e.g fork().
4902 umtx_thread_alloc(struct thread *td)
4907 uq->uq_inherited_pri = PRI_MAX;
4909 KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4910 KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4911 KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4912 KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4918 * Clear robust lists for all process' threads, not delaying the
4919 * cleanup to thread exit, since the relevant address space is
4920 * destroyed right now.
4923 umtx_exec(struct proc *p)
4927 KASSERT(p == curproc, ("need curproc"));
4928 KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4929 (p->p_flag & P_STOPPED_SINGLE) != 0,
4930 ("curproc must be single-threaded"));
4932 * There is no need to lock the list as only this thread can be
4935 FOREACH_THREAD_IN_PROC(p, td) {
4936 KASSERT(td == curthread ||
4937 ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4938 ("running thread %p %p", p, td));
4939 umtx_thread_cleanup(td);
4940 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4948 umtx_thread_exit(struct thread *td)
4951 umtx_thread_cleanup(td);
4955 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res, bool compat32)
4962 error = fueword32((void *)ptr, &res32);
4966 error = fueword((void *)ptr, &res1);
4976 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list,
4979 struct umutex32 m32;
4982 memcpy(&m32, m, sizeof(m32));
4983 *rb_list = m32.m_rb_lnk;
4985 *rb_list = m->m_rb_lnk;
4990 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact,
4996 KASSERT(td->td_proc == curproc, ("need current vmspace"));
4997 error = copyin((void *)rbp, &m, sizeof(m));
5000 if (rb_list != NULL)
5001 umtx_read_rb_list(td, &m, rb_list, compat32);
5002 if ((m.m_flags & UMUTEX_ROBUST) == 0)
5004 if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
5005 /* inact is cleared after unlock, allow the inconsistency */
5006 return (inact ? 0 : EINVAL);
5007 return (do_unlock_umutex(td, (struct umutex *)rbp, true));
5011 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
5012 const char *name, bool compat32)
5020 error = umtx_read_uptr(td, rb_list, &rbp, compat32);
5021 for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
5022 if (rbp == *rb_inact) {
5027 error = umtx_handle_rb(td, rbp, &rbp, inact, compat32);
5029 if (i == umtx_max_rb && umtx_verbose_rb) {
5030 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
5031 td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
5033 if (error != 0 && umtx_verbose_rb) {
5034 uprintf("comm %s pid %d: handling %srb error %d\n",
5035 td->td_proc->p_comm, td->td_proc->p_pid, name, error);
5040 * Clean up umtx data.
5043 umtx_thread_cleanup(struct thread *td)
5051 * Disown pi mutexes.
5055 if (uq->uq_inherited_pri != PRI_MAX ||
5056 !TAILQ_EMPTY(&uq->uq_pi_contested)) {
5057 mtx_lock(&umtx_lock);
5058 uq->uq_inherited_pri = PRI_MAX;
5059 while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
5060 pi->pi_owner = NULL;
5061 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
5063 mtx_unlock(&umtx_lock);
5065 sched_lend_user_prio_cond(td, PRI_MAX);
5068 compat32 = (td->td_pflags2 & TDP2_COMPAT32RB) != 0;
5069 td->td_pflags2 &= ~TDP2_COMPAT32RB;
5071 if (td->td_rb_inact == 0 && td->td_rb_list == 0 && td->td_rbp_list == 0)
5075 * Handle terminated robust mutexes. Must be done after
5076 * robust pi disown, otherwise unlock could see unowned
5079 rb_inact = td->td_rb_inact;
5081 (void)umtx_read_uptr(td, rb_inact, &rb_inact, compat32);
5082 umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "", compat32);
5083 umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ", compat32);
5085 (void)umtx_handle_rb(td, rb_inact, NULL, true, compat32);