]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_umtx.c
In do_lock_pi(), do not return prematurely.
[FreeBSD/FreeBSD.git] / sys / kern / kern_umtx.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
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>
7  * All rights reserved.
8  *
9  * Portions of this software were developed by Konstantin Belousov
10  * under sponsorship from the FreeBSD Foundation.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice unmodified, this list of conditions, and the following
17  *    disclaimer.
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.
21  *
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.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "opt_umtx_profiling.h"
38
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
42 #include <sys/file.h>
43 #include <sys/filedesc.h>
44 #include <sys/limits.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mman.h>
48 #include <sys/mutex.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/resource.h>
52 #include <sys/resourcevar.h>
53 #include <sys/rwlock.h>
54 #include <sys/sbuf.h>
55 #include <sys/sched.h>
56 #include <sys/smp.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>
63 #include <sys/time.h>
64 #include <sys/eventhandler.h>
65 #include <sys/umtx.h>
66
67 #include <security/mac/mac_framework.h>
68
69 #include <vm/vm.h>
70 #include <vm/vm_param.h>
71 #include <vm/pmap.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_object.h>
74
75 #include <machine/atomic.h>
76 #include <machine/cpu.h>
77
78 #ifdef COMPAT_FREEBSD32
79 #include <compat/freebsd32/freebsd32_proto.h>
80 #endif
81
82 #define _UMUTEX_TRY             1
83 #define _UMUTEX_WAIT            2
84
85 #ifdef UMTX_PROFILING
86 #define UPROF_PERC_BIGGER(w, f, sw, sf)                                 \
87         (((w) > (sw)) || ((w) == (sw) && (f) > (sf)))
88 #endif
89
90 /* Priority inheritance mutex info. */
91 struct umtx_pi {
92         /* Owner thread */
93         struct thread           *pi_owner;
94
95         /* Reference count */
96         int                     pi_refcount;
97
98         /* List entry to link umtx holding by thread */
99         TAILQ_ENTRY(umtx_pi)    pi_link;
100
101         /* List entry in hash */
102         TAILQ_ENTRY(umtx_pi)    pi_hashlink;
103
104         /* List for waiters */
105         TAILQ_HEAD(,umtx_q)     pi_blocked;
106
107         /* Identify a userland lock object */
108         struct umtx_key         pi_key;
109 };
110
111 /* A userland synchronous object user. */
112 struct umtx_q {
113         /* Linked list for the hash. */
114         TAILQ_ENTRY(umtx_q)     uq_link;
115
116         /* Umtx key. */
117         struct umtx_key         uq_key;
118
119         /* Umtx flags. */
120         int                     uq_flags;
121 #define UQF_UMTXQ       0x0001
122
123         /* The thread waits on. */
124         struct thread           *uq_thread;
125
126         /*
127          * Blocked on PI mutex. read can use chain lock
128          * or umtx_lock, write must have both chain lock and
129          * umtx_lock being hold.
130          */
131         struct umtx_pi          *uq_pi_blocked;
132
133         /* On blocked list */
134         TAILQ_ENTRY(umtx_q)     uq_lockq;
135
136         /* Thread contending with us */
137         TAILQ_HEAD(,umtx_pi)    uq_pi_contested;
138
139         /* Inherited priority from PP mutex */
140         u_char                  uq_inherited_pri;
141         
142         /* Spare queue ready to be reused */
143         struct umtxq_queue      *uq_spare_queue;
144
145         /* The queue we on */
146         struct umtxq_queue      *uq_cur_queue;
147 };
148
149 TAILQ_HEAD(umtxq_head, umtx_q);
150
151 /* Per-key wait-queue */
152 struct umtxq_queue {
153         struct umtxq_head       head;
154         struct umtx_key         key;
155         LIST_ENTRY(umtxq_queue) link;
156         int                     length;
157 };
158
159 LIST_HEAD(umtxq_list, umtxq_queue);
160
161 /* Userland lock object's wait-queue chain */
162 struct umtxq_chain {
163         /* Lock for this chain. */
164         struct mtx              uc_lock;
165
166         /* List of sleep queues. */
167         struct umtxq_list       uc_queue[2];
168 #define UMTX_SHARED_QUEUE       0
169 #define UMTX_EXCLUSIVE_QUEUE    1
170
171         LIST_HEAD(, umtxq_queue) uc_spare_queue;
172
173         /* Busy flag */
174         char                    uc_busy;
175
176         /* Chain lock waiters */
177         int                     uc_waiters;
178
179         /* All PI in the list */
180         TAILQ_HEAD(,umtx_pi)    uc_pi_list;
181
182 #ifdef UMTX_PROFILING
183         u_int                   length;
184         u_int                   max_length;
185 #endif
186 };
187
188 #define UMTXQ_LOCKED_ASSERT(uc)         mtx_assert(&(uc)->uc_lock, MA_OWNED)
189
190 /*
191  * Don't propagate time-sharing priority, there is a security reason,
192  * a user can simply introduce PI-mutex, let thread A lock the mutex,
193  * and let another thread B block on the mutex, because B is
194  * sleeping, its priority will be boosted, this causes A's priority to
195  * be boosted via priority propagating too and will never be lowered even
196  * if it is using 100%CPU, this is unfair to other processes.
197  */
198
199 #define UPRI(td)        (((td)->td_user_pri >= PRI_MIN_TIMESHARE &&\
200                           (td)->td_user_pri <= PRI_MAX_TIMESHARE) ?\
201                          PRI_MAX_TIMESHARE : (td)->td_user_pri)
202
203 #define GOLDEN_RATIO_PRIME      2654404609U
204 #ifndef UMTX_CHAINS
205 #define UMTX_CHAINS             512
206 #endif
207 #define UMTX_SHIFTS             (__WORD_BIT - 9)
208
209 #define GET_SHARE(flags)        \
210     (((flags) & USYNC_PROCESS_SHARED) == 0 ? THREAD_SHARE : PROCESS_SHARE)
211
212 #define BUSY_SPINS              200
213
214 struct abs_timeout {
215         int clockid;
216         bool is_abs_real;       /* TIMER_ABSTIME && CLOCK_REALTIME* */
217         struct timespec cur;
218         struct timespec end;
219 };
220
221 #ifdef COMPAT_FREEBSD32
222 struct umutex32 {
223         volatile __lwpid_t      m_owner;        /* Owner of the mutex */
224         __uint32_t              m_flags;        /* Flags of the mutex */
225         __uint32_t              m_ceilings[2];  /* Priority protect ceiling */
226         __uint32_t              m_rb_lnk;       /* Robust linkage */
227         __uint32_t              m_pad;
228         __uint32_t              m_spare[2];
229 };
230
231 _Static_assert(sizeof(struct umutex) == sizeof(struct umutex32), "umutex32");
232 _Static_assert(__offsetof(struct umutex, m_spare[0]) ==
233     __offsetof(struct umutex32, m_spare[0]), "m_spare32");
234 #endif
235
236 int umtx_shm_vnobj_persistent = 0;
237 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_vnode_persistent, CTLFLAG_RWTUN,
238     &umtx_shm_vnobj_persistent, 0,
239     "False forces destruction of umtx attached to file, on last close");
240 static int umtx_max_rb = 1000;
241 SYSCTL_INT(_kern_ipc, OID_AUTO, umtx_max_robust, CTLFLAG_RWTUN,
242     &umtx_max_rb, 0,
243     "");
244
245 static uma_zone_t               umtx_pi_zone;
246 static struct umtxq_chain       umtxq_chains[2][UMTX_CHAINS];
247 static MALLOC_DEFINE(M_UMTX, "umtx", "UMTX queue memory");
248 static int                      umtx_pi_allocated;
249
250 static SYSCTL_NODE(_debug, OID_AUTO, umtx, CTLFLAG_RW, 0, "umtx debug");
251 SYSCTL_INT(_debug_umtx, OID_AUTO, umtx_pi_allocated, CTLFLAG_RD,
252     &umtx_pi_allocated, 0, "Allocated umtx_pi");
253 static int umtx_verbose_rb = 1;
254 SYSCTL_INT(_debug_umtx, OID_AUTO, robust_faults_verbose, CTLFLAG_RWTUN,
255     &umtx_verbose_rb, 0,
256     "");
257
258 #ifdef UMTX_PROFILING
259 static long max_length;
260 SYSCTL_LONG(_debug_umtx, OID_AUTO, max_length, CTLFLAG_RD, &max_length, 0, "max_length");
261 static SYSCTL_NODE(_debug_umtx, OID_AUTO, chains, CTLFLAG_RD, 0, "umtx chain stats");
262 #endif
263
264 static void abs_timeout_update(struct abs_timeout *timo);
265
266 static void umtx_shm_init(void);
267 static void umtxq_sysinit(void *);
268 static void umtxq_hash(struct umtx_key *key);
269 static struct umtxq_chain *umtxq_getchain(struct umtx_key *key);
270 static void umtxq_lock(struct umtx_key *key);
271 static void umtxq_unlock(struct umtx_key *key);
272 static void umtxq_busy(struct umtx_key *key);
273 static void umtxq_unbusy(struct umtx_key *key);
274 static void umtxq_insert_queue(struct umtx_q *uq, int q);
275 static void umtxq_remove_queue(struct umtx_q *uq, int q);
276 static int umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *);
277 static int umtxq_count(struct umtx_key *key);
278 static struct umtx_pi *umtx_pi_alloc(int);
279 static void umtx_pi_free(struct umtx_pi *pi);
280 static int do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags,
281     bool rb);
282 static void umtx_thread_cleanup(struct thread *td);
283 static void umtx_exec_hook(void *arg __unused, struct proc *p __unused,
284     struct image_params *imgp __unused);
285 SYSINIT(umtx, SI_SUB_EVENTHANDLER+1, SI_ORDER_MIDDLE, umtxq_sysinit, NULL);
286
287 #define umtxq_signal(key, nwake)        umtxq_signal_queue((key), (nwake), UMTX_SHARED_QUEUE)
288 #define umtxq_insert(uq)        umtxq_insert_queue((uq), UMTX_SHARED_QUEUE)
289 #define umtxq_remove(uq)        umtxq_remove_queue((uq), UMTX_SHARED_QUEUE)
290
291 static struct mtx umtx_lock;
292
293 #ifdef UMTX_PROFILING
294 static void
295 umtx_init_profiling(void) 
296 {
297         struct sysctl_oid *chain_oid;
298         char chain_name[10];
299         int i;
300
301         for (i = 0; i < UMTX_CHAINS; ++i) {
302                 snprintf(chain_name, sizeof(chain_name), "%d", i);
303                 chain_oid = SYSCTL_ADD_NODE(NULL, 
304                     SYSCTL_STATIC_CHILDREN(_debug_umtx_chains), OID_AUTO, 
305                     chain_name, CTLFLAG_RD, NULL, "umtx hash stats");
306                 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
307                     "max_length0", CTLFLAG_RD, &umtxq_chains[0][i].max_length, 0, NULL);
308                 SYSCTL_ADD_INT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
309                     "max_length1", CTLFLAG_RD, &umtxq_chains[1][i].max_length, 0, NULL);
310         }
311 }
312
313 static int
314 sysctl_debug_umtx_chains_peaks(SYSCTL_HANDLER_ARGS)
315 {
316         char buf[512];
317         struct sbuf sb;
318         struct umtxq_chain *uc;
319         u_int fract, i, j, tot, whole;
320         u_int sf0, sf1, sf2, sf3, sf4;
321         u_int si0, si1, si2, si3, si4;
322         u_int sw0, sw1, sw2, sw3, sw4;
323
324         sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
325         for (i = 0; i < 2; i++) {
326                 tot = 0;
327                 for (j = 0; j < UMTX_CHAINS; ++j) {
328                         uc = &umtxq_chains[i][j];
329                         mtx_lock(&uc->uc_lock);
330                         tot += uc->max_length;
331                         mtx_unlock(&uc->uc_lock);
332                 }
333                 if (tot == 0)
334                         sbuf_printf(&sb, "%u) Empty ", i);
335                 else {
336                         sf0 = sf1 = sf2 = sf3 = sf4 = 0;
337                         si0 = si1 = si2 = si3 = si4 = 0;
338                         sw0 = sw1 = sw2 = sw3 = sw4 = 0;
339                         for (j = 0; j < UMTX_CHAINS; j++) {
340                                 uc = &umtxq_chains[i][j];
341                                 mtx_lock(&uc->uc_lock);
342                                 whole = uc->max_length * 100;
343                                 mtx_unlock(&uc->uc_lock);
344                                 fract = (whole % tot) * 100;
345                                 if (UPROF_PERC_BIGGER(whole, fract, sw0, sf0)) {
346                                         sf0 = fract;
347                                         si0 = j;
348                                         sw0 = whole;
349                                 } else if (UPROF_PERC_BIGGER(whole, fract, sw1,
350                                     sf1)) {
351                                         sf1 = fract;
352                                         si1 = j;
353                                         sw1 = whole;
354                                 } else if (UPROF_PERC_BIGGER(whole, fract, sw2,
355                                     sf2)) {
356                                         sf2 = fract;
357                                         si2 = j;
358                                         sw2 = whole;
359                                 } else if (UPROF_PERC_BIGGER(whole, fract, sw3,
360                                     sf3)) {
361                                         sf3 = fract;
362                                         si3 = j;
363                                         sw3 = whole;
364                                 } else if (UPROF_PERC_BIGGER(whole, fract, sw4,
365                                     sf4)) {
366                                         sf4 = fract;
367                                         si4 = j;
368                                         sw4 = whole;
369                                 }
370                         }
371                         sbuf_printf(&sb, "queue %u:\n", i);
372                         sbuf_printf(&sb, "1st: %u.%u%% idx: %u\n", sw0 / tot,
373                             sf0 / tot, si0);
374                         sbuf_printf(&sb, "2nd: %u.%u%% idx: %u\n", sw1 / tot,
375                             sf1 / tot, si1);
376                         sbuf_printf(&sb, "3rd: %u.%u%% idx: %u\n", sw2 / tot,
377                             sf2 / tot, si2);
378                         sbuf_printf(&sb, "4th: %u.%u%% idx: %u\n", sw3 / tot,
379                             sf3 / tot, si3);
380                         sbuf_printf(&sb, "5th: %u.%u%% idx: %u\n", sw4 / tot,
381                             sf4 / tot, si4);
382                 }
383         }
384         sbuf_trim(&sb);
385         sbuf_finish(&sb);
386         sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
387         sbuf_delete(&sb);
388         return (0);
389 }
390
391 static int
392 sysctl_debug_umtx_chains_clear(SYSCTL_HANDLER_ARGS)
393 {
394         struct umtxq_chain *uc;
395         u_int i, j;
396         int clear, error;
397
398         clear = 0;
399         error = sysctl_handle_int(oidp, &clear, 0, req);
400         if (error != 0 || req->newptr == NULL)
401                 return (error);
402
403         if (clear != 0) {
404                 for (i = 0; i < 2; ++i) {
405                         for (j = 0; j < UMTX_CHAINS; ++j) {
406                                 uc = &umtxq_chains[i][j];
407                                 mtx_lock(&uc->uc_lock);
408                                 uc->length = 0;
409                                 uc->max_length = 0;     
410                                 mtx_unlock(&uc->uc_lock);
411                         }
412                 }
413         }
414         return (0);
415 }
416
417 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, clear,
418     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 0,
419     sysctl_debug_umtx_chains_clear, "I", "Clear umtx chains statistics");
420 SYSCTL_PROC(_debug_umtx_chains, OID_AUTO, peaks,
421     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 0,
422     sysctl_debug_umtx_chains_peaks, "A", "Highest peaks in chains max length");
423 #endif
424
425 static void
426 umtxq_sysinit(void *arg __unused)
427 {
428         int i, j;
429
430         umtx_pi_zone = uma_zcreate("umtx pi", sizeof(struct umtx_pi),
431                 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
432         for (i = 0; i < 2; ++i) {
433                 for (j = 0; j < UMTX_CHAINS; ++j) {
434                         mtx_init(&umtxq_chains[i][j].uc_lock, "umtxql", NULL,
435                                  MTX_DEF | MTX_DUPOK);
436                         LIST_INIT(&umtxq_chains[i][j].uc_queue[0]);
437                         LIST_INIT(&umtxq_chains[i][j].uc_queue[1]);
438                         LIST_INIT(&umtxq_chains[i][j].uc_spare_queue);
439                         TAILQ_INIT(&umtxq_chains[i][j].uc_pi_list);
440                         umtxq_chains[i][j].uc_busy = 0;
441                         umtxq_chains[i][j].uc_waiters = 0;
442 #ifdef UMTX_PROFILING
443                         umtxq_chains[i][j].length = 0;
444                         umtxq_chains[i][j].max_length = 0;      
445 #endif
446                 }
447         }
448 #ifdef UMTX_PROFILING
449         umtx_init_profiling();
450 #endif
451         mtx_init(&umtx_lock, "umtx lock", NULL, MTX_DEF);
452         EVENTHANDLER_REGISTER(process_exec, umtx_exec_hook, NULL,
453             EVENTHANDLER_PRI_ANY);
454         umtx_shm_init();
455 }
456
457 struct umtx_q *
458 umtxq_alloc(void)
459 {
460         struct umtx_q *uq;
461
462         uq = malloc(sizeof(struct umtx_q), M_UMTX, M_WAITOK | M_ZERO);
463         uq->uq_spare_queue = malloc(sizeof(struct umtxq_queue), M_UMTX,
464             M_WAITOK | M_ZERO);
465         TAILQ_INIT(&uq->uq_spare_queue->head);
466         TAILQ_INIT(&uq->uq_pi_contested);
467         uq->uq_inherited_pri = PRI_MAX;
468         return (uq);
469 }
470
471 void
472 umtxq_free(struct umtx_q *uq)
473 {
474
475         MPASS(uq->uq_spare_queue != NULL);
476         free(uq->uq_spare_queue, M_UMTX);
477         free(uq, M_UMTX);
478 }
479
480 static inline void
481 umtxq_hash(struct umtx_key *key)
482 {
483         unsigned n;
484
485         n = (uintptr_t)key->info.both.a + key->info.both.b;
486         key->hash = ((n * GOLDEN_RATIO_PRIME) >> UMTX_SHIFTS) % UMTX_CHAINS;
487 }
488
489 static inline struct umtxq_chain *
490 umtxq_getchain(struct umtx_key *key)
491 {
492
493         if (key->type <= TYPE_SEM)
494                 return (&umtxq_chains[1][key->hash]);
495         return (&umtxq_chains[0][key->hash]);
496 }
497
498 /*
499  * Lock a chain.
500  */
501 static inline void
502 umtxq_lock(struct umtx_key *key)
503 {
504         struct umtxq_chain *uc;
505
506         uc = umtxq_getchain(key);
507         mtx_lock(&uc->uc_lock);
508 }
509
510 /*
511  * Unlock a chain.
512  */
513 static inline void
514 umtxq_unlock(struct umtx_key *key)
515 {
516         struct umtxq_chain *uc;
517
518         uc = umtxq_getchain(key);
519         mtx_unlock(&uc->uc_lock);
520 }
521
522 /*
523  * Set chain to busy state when following operation
524  * may be blocked (kernel mutex can not be used).
525  */
526 static inline void
527 umtxq_busy(struct umtx_key *key)
528 {
529         struct umtxq_chain *uc;
530
531         uc = umtxq_getchain(key);
532         mtx_assert(&uc->uc_lock, MA_OWNED);
533         if (uc->uc_busy) {
534 #ifdef SMP
535                 if (smp_cpus > 1) {
536                         int count = BUSY_SPINS;
537                         if (count > 0) {
538                                 umtxq_unlock(key);
539                                 while (uc->uc_busy && --count > 0)
540                                         cpu_spinwait();
541                                 umtxq_lock(key);
542                         }
543                 }
544 #endif
545                 while (uc->uc_busy) {
546                         uc->uc_waiters++;
547                         msleep(uc, &uc->uc_lock, 0, "umtxqb", 0);
548                         uc->uc_waiters--;
549                 }
550         }
551         uc->uc_busy = 1;
552 }
553
554 /*
555  * Unbusy a chain.
556  */
557 static inline void
558 umtxq_unbusy(struct umtx_key *key)
559 {
560         struct umtxq_chain *uc;
561
562         uc = umtxq_getchain(key);
563         mtx_assert(&uc->uc_lock, MA_OWNED);
564         KASSERT(uc->uc_busy != 0, ("not busy"));
565         uc->uc_busy = 0;
566         if (uc->uc_waiters)
567                 wakeup_one(uc);
568 }
569
570 static inline void
571 umtxq_unbusy_unlocked(struct umtx_key *key)
572 {
573
574         umtxq_lock(key);
575         umtxq_unbusy(key);
576         umtxq_unlock(key);
577 }
578
579 static struct umtxq_queue *
580 umtxq_queue_lookup(struct umtx_key *key, int q)
581 {
582         struct umtxq_queue *uh;
583         struct umtxq_chain *uc;
584
585         uc = umtxq_getchain(key);
586         UMTXQ_LOCKED_ASSERT(uc);
587         LIST_FOREACH(uh, &uc->uc_queue[q], link) {
588                 if (umtx_key_match(&uh->key, key))
589                         return (uh);
590         }
591
592         return (NULL);
593 }
594
595 static inline void
596 umtxq_insert_queue(struct umtx_q *uq, int q)
597 {
598         struct umtxq_queue *uh;
599         struct umtxq_chain *uc;
600
601         uc = umtxq_getchain(&uq->uq_key);
602         UMTXQ_LOCKED_ASSERT(uc);
603         KASSERT((uq->uq_flags & UQF_UMTXQ) == 0, ("umtx_q is already on queue"));
604         uh = umtxq_queue_lookup(&uq->uq_key, q);
605         if (uh != NULL) {
606                 LIST_INSERT_HEAD(&uc->uc_spare_queue, uq->uq_spare_queue, link);
607         } else {
608                 uh = uq->uq_spare_queue;
609                 uh->key = uq->uq_key;
610                 LIST_INSERT_HEAD(&uc->uc_queue[q], uh, link);
611 #ifdef UMTX_PROFILING
612                 uc->length++;
613                 if (uc->length > uc->max_length) {
614                         uc->max_length = uc->length;
615                         if (uc->max_length > max_length)
616                                 max_length = uc->max_length;    
617                 }
618 #endif
619         }
620         uq->uq_spare_queue = NULL;
621
622         TAILQ_INSERT_TAIL(&uh->head, uq, uq_link);
623         uh->length++;
624         uq->uq_flags |= UQF_UMTXQ;
625         uq->uq_cur_queue = uh;
626         return;
627 }
628
629 static inline void
630 umtxq_remove_queue(struct umtx_q *uq, int q)
631 {
632         struct umtxq_chain *uc;
633         struct umtxq_queue *uh;
634
635         uc = umtxq_getchain(&uq->uq_key);
636         UMTXQ_LOCKED_ASSERT(uc);
637         if (uq->uq_flags & UQF_UMTXQ) {
638                 uh = uq->uq_cur_queue;
639                 TAILQ_REMOVE(&uh->head, uq, uq_link);
640                 uh->length--;
641                 uq->uq_flags &= ~UQF_UMTXQ;
642                 if (TAILQ_EMPTY(&uh->head)) {
643                         KASSERT(uh->length == 0,
644                             ("inconsistent umtxq_queue length"));
645 #ifdef UMTX_PROFILING
646                         uc->length--;
647 #endif
648                         LIST_REMOVE(uh, link);
649                 } else {
650                         uh = LIST_FIRST(&uc->uc_spare_queue);
651                         KASSERT(uh != NULL, ("uc_spare_queue is empty"));
652                         LIST_REMOVE(uh, link);
653                 }
654                 uq->uq_spare_queue = uh;
655                 uq->uq_cur_queue = NULL;
656         }
657 }
658
659 /*
660  * Check if there are multiple waiters
661  */
662 static int
663 umtxq_count(struct umtx_key *key)
664 {
665         struct umtxq_queue *uh;
666
667         UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
668         uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
669         if (uh != NULL)
670                 return (uh->length);
671         return (0);
672 }
673
674 /*
675  * Check if there are multiple PI waiters and returns first
676  * waiter.
677  */
678 static int
679 umtxq_count_pi(struct umtx_key *key, struct umtx_q **first)
680 {
681         struct umtxq_queue *uh;
682
683         *first = NULL;
684         UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
685         uh = umtxq_queue_lookup(key, UMTX_SHARED_QUEUE);
686         if (uh != NULL) {
687                 *first = TAILQ_FIRST(&uh->head);
688                 return (uh->length);
689         }
690         return (0);
691 }
692
693 /*
694  * Check for possible stops and suspensions while executing a umtx
695  * locking operation.
696  *
697  * The sleep argument controls whether the function can handle a stop
698  * request itself or it should return ERESTART and the request is
699  * proceed at the kernel/user boundary in ast.
700  *
701  * Typically, when retrying due to casueword(9) failure (rv == 1), we
702  * should handle the stop requests there, with exception of cases when
703  * the thread busied the umtx key, or when functions return
704  * immediately if umtxq_check_susp() returned non-zero.  On the other
705  * hand, retrying the whole lock operation, we better not stop there
706  * but delegate the handling to ast.
707  *
708  * If the request is for thread termination P_SINGLE_EXIT, we cannot
709  * handle it at all, and simply return EINTR.
710  */
711 static int
712 umtxq_check_susp(struct thread *td, bool sleep)
713 {
714         struct proc *p;
715         int error;
716
717         /*
718          * The check for TDF_NEEDSUSPCHK is racy, but it is enough to
719          * eventually break the lockstep loop.
720          */
721         if ((td->td_flags & TDF_NEEDSUSPCHK) == 0)
722                 return (0);
723         error = 0;
724         p = td->td_proc;
725         PROC_LOCK(p);
726         if (P_SHOULDSTOP(p) ||
727             ((p->p_flag & P_TRACED) && (td->td_dbgflags & TDB_SUSPEND))) {
728                 if (p->p_flag & P_SINGLE_EXIT)
729                         error = EINTR;
730                 else
731                         error = sleep ? thread_suspend_check(0) : ERESTART;
732         }
733         PROC_UNLOCK(p);
734         return (error);
735 }
736
737 /*
738  * Wake up threads waiting on an userland object.
739  */
740
741 static int
742 umtxq_signal_queue(struct umtx_key *key, int n_wake, int q)
743 {
744         struct umtxq_queue *uh;
745         struct umtx_q *uq;
746         int ret;
747
748         ret = 0;
749         UMTXQ_LOCKED_ASSERT(umtxq_getchain(key));
750         uh = umtxq_queue_lookup(key, q);
751         if (uh != NULL) {
752                 while ((uq = TAILQ_FIRST(&uh->head)) != NULL) {
753                         umtxq_remove_queue(uq, q);
754                         wakeup(uq);
755                         if (++ret >= n_wake)
756                                 return (ret);
757                 }
758         }
759         return (ret);
760 }
761
762
763 /*
764  * Wake up specified thread.
765  */
766 static inline void
767 umtxq_signal_thread(struct umtx_q *uq)
768 {
769
770         UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
771         umtxq_remove(uq);
772         wakeup(uq);
773 }
774
775 static inline int 
776 tstohz(const struct timespec *tsp)
777 {
778         struct timeval tv;
779
780         TIMESPEC_TO_TIMEVAL(&tv, tsp);
781         return tvtohz(&tv);
782 }
783
784 static void
785 abs_timeout_init(struct abs_timeout *timo, int clockid, int absolute,
786         const struct timespec *timeout)
787 {
788
789         timo->clockid = clockid;
790         if (!absolute) {
791                 timo->is_abs_real = false;
792                 abs_timeout_update(timo);
793                 timespecadd(&timo->cur, timeout, &timo->end);
794         } else {
795                 timo->end = *timeout;
796                 timo->is_abs_real = clockid == CLOCK_REALTIME ||
797                     clockid == CLOCK_REALTIME_FAST ||
798                     clockid == CLOCK_REALTIME_PRECISE;
799                 /*
800                  * If is_abs_real, umtxq_sleep will read the clock
801                  * after setting td_rtcgen; otherwise, read it here.
802                  */
803                 if (!timo->is_abs_real) {
804                         abs_timeout_update(timo);
805                 }
806         }
807 }
808
809 static void
810 abs_timeout_init2(struct abs_timeout *timo, const struct _umtx_time *umtxtime)
811 {
812
813         abs_timeout_init(timo, umtxtime->_clockid,
814             (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout);
815 }
816
817 static inline void
818 abs_timeout_update(struct abs_timeout *timo)
819 {
820
821         kern_clock_gettime(curthread, timo->clockid, &timo->cur);
822 }
823
824 static int
825 abs_timeout_gethz(struct abs_timeout *timo)
826 {
827         struct timespec tts;
828
829         if (timespeccmp(&timo->end, &timo->cur, <=))
830                 return (-1); 
831         timespecsub(&timo->end, &timo->cur, &tts);
832         return (tstohz(&tts));
833 }
834
835 static uint32_t
836 umtx_unlock_val(uint32_t flags, bool rb)
837 {
838
839         if (rb)
840                 return (UMUTEX_RB_OWNERDEAD);
841         else if ((flags & UMUTEX_NONCONSISTENT) != 0)
842                 return (UMUTEX_RB_NOTRECOV);
843         else
844                 return (UMUTEX_UNOWNED);
845
846 }
847
848 /*
849  * Put thread into sleep state, before sleeping, check if
850  * thread was removed from umtx queue.
851  */
852 static inline int
853 umtxq_sleep(struct umtx_q *uq, const char *wmesg, struct abs_timeout *abstime)
854 {
855         struct umtxq_chain *uc;
856         int error, timo;
857
858         if (abstime != NULL && abstime->is_abs_real) {
859                 curthread->td_rtcgen = atomic_load_acq_int(&rtc_generation);
860                 abs_timeout_update(abstime);
861         }
862
863         uc = umtxq_getchain(&uq->uq_key);
864         UMTXQ_LOCKED_ASSERT(uc);
865         for (;;) {
866                 if (!(uq->uq_flags & UQF_UMTXQ)) {
867                         error = 0;
868                         break;
869                 }
870                 if (abstime != NULL) {
871                         timo = abs_timeout_gethz(abstime);
872                         if (timo < 0) {
873                                 error = ETIMEDOUT;
874                                 break;
875                         }
876                 } else
877                         timo = 0;
878                 error = msleep(uq, &uc->uc_lock, PCATCH | PDROP, wmesg, timo);
879                 if (error == EINTR || error == ERESTART) {
880                         umtxq_lock(&uq->uq_key);
881                         break;
882                 }
883                 if (abstime != NULL) {
884                         if (abstime->is_abs_real)
885                                 curthread->td_rtcgen =
886                                     atomic_load_acq_int(&rtc_generation);
887                         abs_timeout_update(abstime);
888                 }
889                 umtxq_lock(&uq->uq_key);
890         }
891
892         curthread->td_rtcgen = 0;
893         return (error);
894 }
895
896 /*
897  * Convert userspace address into unique logical address.
898  */
899 int
900 umtx_key_get(const void *addr, int type, int share, struct umtx_key *key)
901 {
902         struct thread *td = curthread;
903         vm_map_t map;
904         vm_map_entry_t entry;
905         vm_pindex_t pindex;
906         vm_prot_t prot;
907         boolean_t wired;
908
909         key->type = type;
910         if (share == THREAD_SHARE) {
911                 key->shared = 0;
912                 key->info.private.vs = td->td_proc->p_vmspace;
913                 key->info.private.addr = (uintptr_t)addr;
914         } else {
915                 MPASS(share == PROCESS_SHARE || share == AUTO_SHARE);
916                 map = &td->td_proc->p_vmspace->vm_map;
917                 if (vm_map_lookup(&map, (vm_offset_t)addr, VM_PROT_WRITE,
918                     &entry, &key->info.shared.object, &pindex, &prot,
919                     &wired) != KERN_SUCCESS) {
920                         return (EFAULT);
921                 }
922
923                 if ((share == PROCESS_SHARE) ||
924                     (share == AUTO_SHARE &&
925                      VM_INHERIT_SHARE == entry->inheritance)) {
926                         key->shared = 1;
927                         key->info.shared.offset = (vm_offset_t)addr -
928                             entry->start + entry->offset;
929                         vm_object_reference(key->info.shared.object);
930                 } else {
931                         key->shared = 0;
932                         key->info.private.vs = td->td_proc->p_vmspace;
933                         key->info.private.addr = (uintptr_t)addr;
934                 }
935                 vm_map_lookup_done(map, entry);
936         }
937
938         umtxq_hash(key);
939         return (0);
940 }
941
942 /*
943  * Release key.
944  */
945 void
946 umtx_key_release(struct umtx_key *key)
947 {
948         if (key->shared)
949                 vm_object_deallocate(key->info.shared.object);
950 }
951
952 /*
953  * Fetch and compare value, sleep on the address if value is not changed.
954  */
955 static int
956 do_wait(struct thread *td, void *addr, u_long id,
957     struct _umtx_time *timeout, int compat32, int is_private)
958 {
959         struct abs_timeout timo;
960         struct umtx_q *uq;
961         u_long tmp;
962         uint32_t tmp32;
963         int error = 0;
964
965         uq = td->td_umtxq;
966         if ((error = umtx_key_get(addr, TYPE_SIMPLE_WAIT,
967                 is_private ? THREAD_SHARE : AUTO_SHARE, &uq->uq_key)) != 0)
968                 return (error);
969
970         if (timeout != NULL)
971                 abs_timeout_init2(&timo, timeout);
972
973         umtxq_lock(&uq->uq_key);
974         umtxq_insert(uq);
975         umtxq_unlock(&uq->uq_key);
976         if (compat32 == 0) {
977                 error = fueword(addr, &tmp);
978                 if (error != 0)
979                         error = EFAULT;
980         } else {
981                 error = fueword32(addr, &tmp32);
982                 if (error == 0)
983                         tmp = tmp32;
984                 else
985                         error = EFAULT;
986         }
987         umtxq_lock(&uq->uq_key);
988         if (error == 0) {
989                 if (tmp == id)
990                         error = umtxq_sleep(uq, "uwait", timeout == NULL ?
991                             NULL : &timo);
992                 if ((uq->uq_flags & UQF_UMTXQ) == 0)
993                         error = 0;
994                 else
995                         umtxq_remove(uq);
996         } else if ((uq->uq_flags & UQF_UMTXQ) != 0) {
997                 umtxq_remove(uq);
998         }
999         umtxq_unlock(&uq->uq_key);
1000         umtx_key_release(&uq->uq_key);
1001         if (error == ERESTART)
1002                 error = EINTR;
1003         return (error);
1004 }
1005
1006 /*
1007  * Wake up threads sleeping on the specified address.
1008  */
1009 int
1010 kern_umtx_wake(struct thread *td, void *uaddr, int n_wake, int is_private)
1011 {
1012         struct umtx_key key;
1013         int ret;
1014         
1015         if ((ret = umtx_key_get(uaddr, TYPE_SIMPLE_WAIT,
1016             is_private ? THREAD_SHARE : AUTO_SHARE, &key)) != 0)
1017                 return (ret);
1018         umtxq_lock(&key);
1019         umtxq_signal(&key, n_wake);
1020         umtxq_unlock(&key);
1021         umtx_key_release(&key);
1022         return (0);
1023 }
1024
1025 /*
1026  * Lock PTHREAD_PRIO_NONE protocol POSIX mutex.
1027  */
1028 static int
1029 do_lock_normal(struct thread *td, struct umutex *m, uint32_t flags,
1030     struct _umtx_time *timeout, int mode)
1031 {
1032         struct abs_timeout timo;
1033         struct umtx_q *uq;
1034         uint32_t owner, old, id;
1035         int error, rv;
1036
1037         id = td->td_tid;
1038         uq = td->td_umtxq;
1039         error = 0;
1040         if (timeout != NULL)
1041                 abs_timeout_init2(&timo, timeout);
1042
1043         /*
1044          * Care must be exercised when dealing with umtx structure. It
1045          * can fault on any access.
1046          */
1047         for (;;) {
1048                 rv = fueword32(&m->m_owner, &owner);
1049                 if (rv == -1)
1050                         return (EFAULT);
1051                 if (mode == _UMUTEX_WAIT) {
1052                         if (owner == UMUTEX_UNOWNED ||
1053                             owner == UMUTEX_CONTESTED ||
1054                             owner == UMUTEX_RB_OWNERDEAD ||
1055                             owner == UMUTEX_RB_NOTRECOV)
1056                                 return (0);
1057                 } else {
1058                         /*
1059                          * Robust mutex terminated.  Kernel duty is to
1060                          * return EOWNERDEAD to the userspace.  The
1061                          * umutex.m_flags UMUTEX_NONCONSISTENT is set
1062                          * by the common userspace code.
1063                          */
1064                         if (owner == UMUTEX_RB_OWNERDEAD) {
1065                                 rv = casueword32(&m->m_owner,
1066                                     UMUTEX_RB_OWNERDEAD, &owner,
1067                                     id | UMUTEX_CONTESTED);
1068                                 if (rv == -1)
1069                                         return (EFAULT);
1070                                 if (rv == 0) {
1071                                         MPASS(owner == UMUTEX_RB_OWNERDEAD);
1072                                         return (EOWNERDEAD); /* success */
1073                                 }
1074                                 MPASS(rv == 1);
1075                                 rv = umtxq_check_susp(td, false);
1076                                 if (rv != 0)
1077                                         return (rv);
1078                                 continue;
1079                         }
1080                         if (owner == UMUTEX_RB_NOTRECOV)
1081                                 return (ENOTRECOVERABLE);
1082
1083                         /*
1084                          * Try the uncontested case.  This should be
1085                          * done in userland.
1086                          */
1087                         rv = casueword32(&m->m_owner, UMUTEX_UNOWNED,
1088                             &owner, id);
1089                         /* The address was invalid. */
1090                         if (rv == -1)
1091                                 return (EFAULT);
1092
1093                         /* The acquire succeeded. */
1094                         if (rv == 0) {
1095                                 MPASS(owner == UMUTEX_UNOWNED);
1096                                 return (0);
1097                         }
1098
1099                         /*
1100                          * If no one owns it but it is contested try
1101                          * to acquire it.
1102                          */
1103                         MPASS(rv == 1);
1104                         if (owner == UMUTEX_CONTESTED) {
1105                                 rv = casueword32(&m->m_owner,
1106                                     UMUTEX_CONTESTED, &owner,
1107                                     id | UMUTEX_CONTESTED);
1108                                 /* The address was invalid. */
1109                                 if (rv == -1)
1110                                         return (EFAULT);
1111                                 if (rv == 0) {
1112                                         MPASS(owner == UMUTEX_CONTESTED);
1113                                         return (0);
1114                                 }
1115                                 if (rv == 1) {
1116                                         rv = umtxq_check_susp(td, false);
1117                                         if (rv != 0)
1118                                                 return (rv);
1119                                 }
1120
1121                                 /*
1122                                  * If this failed the lock has
1123                                  * changed, restart.
1124                                  */
1125                                 continue;
1126                         }
1127
1128                         /* rv == 1 but not contested, likely store failure */
1129                         rv = umtxq_check_susp(td, false);
1130                         if (rv != 0)
1131                                 return (rv);
1132                 }
1133
1134                 if (mode == _UMUTEX_TRY)
1135                         return (EBUSY);
1136
1137                 /*
1138                  * If we caught a signal, we have retried and now
1139                  * exit immediately.
1140                  */
1141                 if (error != 0)
1142                         return (error);
1143
1144                 if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX,
1145                     GET_SHARE(flags), &uq->uq_key)) != 0)
1146                         return (error);
1147
1148                 umtxq_lock(&uq->uq_key);
1149                 umtxq_busy(&uq->uq_key);
1150                 umtxq_insert(uq);
1151                 umtxq_unlock(&uq->uq_key);
1152
1153                 /*
1154                  * Set the contested bit so that a release in user space
1155                  * knows to use the system call for unlock.  If this fails
1156                  * either some one else has acquired the lock or it has been
1157                  * released.
1158                  */
1159                 rv = casueword32(&m->m_owner, owner, &old,
1160                     owner | UMUTEX_CONTESTED);
1161
1162                 /* The address was invalid or casueword failed to store. */
1163                 if (rv == -1 || rv == 1) {
1164                         umtxq_lock(&uq->uq_key);
1165                         umtxq_remove(uq);
1166                         umtxq_unbusy(&uq->uq_key);
1167                         umtxq_unlock(&uq->uq_key);
1168                         umtx_key_release(&uq->uq_key);
1169                         if (rv == -1)
1170                                 return (EFAULT);
1171                         if (rv == 1) {
1172                                 rv = umtxq_check_susp(td, false);
1173                                 if (rv != 0)
1174                                         return (rv);
1175                         }
1176                         continue;
1177                 }
1178
1179                 /*
1180                  * We set the contested bit, sleep. Otherwise the lock changed
1181                  * and we need to retry or we lost a race to the thread
1182                  * unlocking the umtx.
1183                  */
1184                 umtxq_lock(&uq->uq_key);
1185                 umtxq_unbusy(&uq->uq_key);
1186                 MPASS(old == owner);
1187                 error = umtxq_sleep(uq, "umtxn", timeout == NULL ?
1188                     NULL : &timo);
1189                 umtxq_remove(uq);
1190                 umtxq_unlock(&uq->uq_key);
1191                 umtx_key_release(&uq->uq_key);
1192
1193                 if (error == 0)
1194                         error = umtxq_check_susp(td, false);
1195         }
1196
1197         return (0);
1198 }
1199
1200 /*
1201  * Unlock PTHREAD_PRIO_NONE protocol POSIX mutex.
1202  */
1203 static int
1204 do_unlock_normal(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
1205 {
1206         struct umtx_key key;
1207         uint32_t owner, old, id, newlock;
1208         int error, count;
1209
1210         id = td->td_tid;
1211
1212 again:
1213         /*
1214          * Make sure we own this mtx.
1215          */
1216         error = fueword32(&m->m_owner, &owner);
1217         if (error == -1)
1218                 return (EFAULT);
1219
1220         if ((owner & ~UMUTEX_CONTESTED) != id)
1221                 return (EPERM);
1222
1223         newlock = umtx_unlock_val(flags, rb);
1224         if ((owner & UMUTEX_CONTESTED) == 0) {
1225                 error = casueword32(&m->m_owner, owner, &old, newlock);
1226                 if (error == -1)
1227                         return (EFAULT);
1228                 if (error == 1) {
1229                         error = umtxq_check_susp(td, false);
1230                         if (error != 0)
1231                                 return (error);
1232                         goto again;
1233                 }
1234                 MPASS(old == owner);
1235                 return (0);
1236         }
1237
1238         /* We should only ever be in here for contested locks */
1239         if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1240             &key)) != 0)
1241                 return (error);
1242
1243         umtxq_lock(&key);
1244         umtxq_busy(&key);
1245         count = umtxq_count(&key);
1246         umtxq_unlock(&key);
1247
1248         /*
1249          * When unlocking the umtx, it must be marked as unowned if
1250          * there is zero or one thread only waiting for it.
1251          * Otherwise, it must be marked as contested.
1252          */
1253         if (count > 1)
1254                 newlock |= UMUTEX_CONTESTED;
1255         error = casueword32(&m->m_owner, owner, &old, newlock);
1256         umtxq_lock(&key);
1257         umtxq_signal(&key, 1);
1258         umtxq_unbusy(&key);
1259         umtxq_unlock(&key);
1260         umtx_key_release(&key);
1261         if (error == -1)
1262                 return (EFAULT);
1263         if (error == 1) {
1264                 if (old != owner)
1265                         return (EINVAL);
1266                 error = umtxq_check_susp(td, false);
1267                 if (error != 0)
1268                         return (error);
1269                 goto again;
1270         }
1271         return (0);
1272 }
1273
1274 /*
1275  * Check if the mutex is available and wake up a waiter,
1276  * only for simple mutex.
1277  */
1278 static int
1279 do_wake_umutex(struct thread *td, struct umutex *m)
1280 {
1281         struct umtx_key key;
1282         uint32_t owner;
1283         uint32_t flags;
1284         int error;
1285         int count;
1286
1287 again:
1288         error = fueword32(&m->m_owner, &owner);
1289         if (error == -1)
1290                 return (EFAULT);
1291
1292         if ((owner & ~UMUTEX_CONTESTED) != 0 && owner != UMUTEX_RB_OWNERDEAD &&
1293             owner != UMUTEX_RB_NOTRECOV)
1294                 return (0);
1295
1296         error = fueword32(&m->m_flags, &flags);
1297         if (error == -1)
1298                 return (EFAULT);
1299
1300         /* We should only ever be in here for contested locks */
1301         if ((error = umtx_key_get(m, TYPE_NORMAL_UMUTEX, GET_SHARE(flags),
1302             &key)) != 0)
1303                 return (error);
1304
1305         umtxq_lock(&key);
1306         umtxq_busy(&key);
1307         count = umtxq_count(&key);
1308         umtxq_unlock(&key);
1309
1310         if (count <= 1 && owner != UMUTEX_RB_OWNERDEAD &&
1311             owner != UMUTEX_RB_NOTRECOV) {
1312                 error = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
1313                     UMUTEX_UNOWNED);
1314                 if (error == -1) {
1315                         error = EFAULT;
1316                 } else if (error == 1) {
1317                         umtxq_lock(&key);
1318                         umtxq_unbusy(&key);
1319                         umtxq_unlock(&key);
1320                         umtx_key_release(&key);
1321                         error = umtxq_check_susp(td, false);
1322                         if (error != 0)
1323                                 return (error);
1324                         goto again;
1325                 }
1326         }
1327
1328         umtxq_lock(&key);
1329         if (error == 0 && count != 0) {
1330                 MPASS((owner & ~UMUTEX_CONTESTED) == 0 ||
1331                     owner == UMUTEX_RB_OWNERDEAD ||
1332                     owner == UMUTEX_RB_NOTRECOV);
1333                 umtxq_signal(&key, 1);
1334         }
1335         umtxq_unbusy(&key);
1336         umtxq_unlock(&key);
1337         umtx_key_release(&key);
1338         return (error);
1339 }
1340
1341 /*
1342  * Check if the mutex has waiters and tries to fix contention bit.
1343  */
1344 static int
1345 do_wake2_umutex(struct thread *td, struct umutex *m, uint32_t flags)
1346 {
1347         struct umtx_key key;
1348         uint32_t owner, old;
1349         int type;
1350         int error;
1351         int count;
1352
1353         switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT |
1354             UMUTEX_ROBUST)) {
1355         case 0:
1356         case UMUTEX_ROBUST:
1357                 type = TYPE_NORMAL_UMUTEX;
1358                 break;
1359         case UMUTEX_PRIO_INHERIT:
1360                 type = TYPE_PI_UMUTEX;
1361                 break;
1362         case (UMUTEX_PRIO_INHERIT | UMUTEX_ROBUST):
1363                 type = TYPE_PI_ROBUST_UMUTEX;
1364                 break;
1365         case UMUTEX_PRIO_PROTECT:
1366                 type = TYPE_PP_UMUTEX;
1367                 break;
1368         case (UMUTEX_PRIO_PROTECT | UMUTEX_ROBUST):
1369                 type = TYPE_PP_ROBUST_UMUTEX;
1370                 break;
1371         default:
1372                 return (EINVAL);
1373         }
1374         if ((error = umtx_key_get(m, type, GET_SHARE(flags), &key)) != 0)
1375                 return (error);
1376
1377         owner = 0;
1378         umtxq_lock(&key);
1379         umtxq_busy(&key);
1380         count = umtxq_count(&key);
1381         umtxq_unlock(&key);
1382
1383         error = fueword32(&m->m_owner, &owner);
1384         if (error == -1)
1385                 error = EFAULT;
1386
1387         /*
1388          * Only repair contention bit if there is a waiter, this means
1389          * the mutex is still being referenced by userland code,
1390          * otherwise don't update any memory.
1391          */
1392         while (error == 0 && (owner & UMUTEX_CONTESTED) == 0 &&
1393             (count > 1 || (count == 1 && (owner & ~UMUTEX_CONTESTED) != 0))) {
1394                 error = casueword32(&m->m_owner, owner, &old,
1395                     owner | UMUTEX_CONTESTED);
1396                 if (error == -1) {
1397                         error = EFAULT;
1398                         break;
1399                 }
1400                 if (error == 0) {
1401                         MPASS(old == owner);
1402                         break;
1403                 }
1404                 owner = old;
1405                 error = umtxq_check_susp(td, false);
1406         }
1407
1408         umtxq_lock(&key);
1409         if (error == EFAULT) {
1410                 umtxq_signal(&key, INT_MAX);
1411         } else if (count != 0 && ((owner & ~UMUTEX_CONTESTED) == 0 ||
1412             owner == UMUTEX_RB_OWNERDEAD || owner == UMUTEX_RB_NOTRECOV))
1413                 umtxq_signal(&key, 1);
1414         umtxq_unbusy(&key);
1415         umtxq_unlock(&key);
1416         umtx_key_release(&key);
1417         return (error);
1418 }
1419
1420 static inline struct umtx_pi *
1421 umtx_pi_alloc(int flags)
1422 {
1423         struct umtx_pi *pi;
1424
1425         pi = uma_zalloc(umtx_pi_zone, M_ZERO | flags);
1426         TAILQ_INIT(&pi->pi_blocked);
1427         atomic_add_int(&umtx_pi_allocated, 1);
1428         return (pi);
1429 }
1430
1431 static inline void
1432 umtx_pi_free(struct umtx_pi *pi)
1433 {
1434         uma_zfree(umtx_pi_zone, pi);
1435         atomic_add_int(&umtx_pi_allocated, -1);
1436 }
1437
1438 /*
1439  * Adjust the thread's position on a pi_state after its priority has been
1440  * changed.
1441  */
1442 static int
1443 umtx_pi_adjust_thread(struct umtx_pi *pi, struct thread *td)
1444 {
1445         struct umtx_q *uq, *uq1, *uq2;
1446         struct thread *td1;
1447
1448         mtx_assert(&umtx_lock, MA_OWNED);
1449         if (pi == NULL)
1450                 return (0);
1451
1452         uq = td->td_umtxq;
1453
1454         /*
1455          * Check if the thread needs to be moved on the blocked chain.
1456          * It needs to be moved if either its priority is lower than
1457          * the previous thread or higher than the next thread.
1458          */
1459         uq1 = TAILQ_PREV(uq, umtxq_head, uq_lockq);
1460         uq2 = TAILQ_NEXT(uq, uq_lockq);
1461         if ((uq1 != NULL && UPRI(td) < UPRI(uq1->uq_thread)) ||
1462             (uq2 != NULL && UPRI(td) > UPRI(uq2->uq_thread))) {
1463                 /*
1464                  * Remove thread from blocked chain and determine where
1465                  * it should be moved to.
1466                  */
1467                 TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1468                 TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1469                         td1 = uq1->uq_thread;
1470                         MPASS(td1->td_proc->p_magic == P_MAGIC);
1471                         if (UPRI(td1) > UPRI(td))
1472                                 break;
1473                 }
1474
1475                 if (uq1 == NULL)
1476                         TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1477                 else
1478                         TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1479         }
1480         return (1);
1481 }
1482
1483 static struct umtx_pi *
1484 umtx_pi_next(struct umtx_pi *pi)
1485 {
1486         struct umtx_q *uq_owner;
1487
1488         if (pi->pi_owner == NULL)
1489                 return (NULL);
1490         uq_owner = pi->pi_owner->td_umtxq;
1491         if (uq_owner == NULL)
1492                 return (NULL);
1493         return (uq_owner->uq_pi_blocked);
1494 }
1495
1496 /*
1497  * Floyd's Cycle-Finding Algorithm.
1498  */
1499 static bool
1500 umtx_pi_check_loop(struct umtx_pi *pi)
1501 {
1502         struct umtx_pi *pi1;    /* fast iterator */
1503
1504         mtx_assert(&umtx_lock, MA_OWNED);
1505         if (pi == NULL)
1506                 return (false);
1507         pi1 = pi;
1508         for (;;) {
1509                 pi = umtx_pi_next(pi);
1510                 if (pi == NULL)
1511                         break;
1512                 pi1 = umtx_pi_next(pi1);
1513                 if (pi1 == NULL)
1514                         break;
1515                 pi1 = umtx_pi_next(pi1);
1516                 if (pi1 == NULL)
1517                         break;
1518                 if (pi == pi1)
1519                         return (true);
1520         }
1521         return (false);
1522 }
1523
1524 /*
1525  * Propagate priority when a thread is blocked on POSIX
1526  * PI mutex.
1527  */ 
1528 static void
1529 umtx_propagate_priority(struct thread *td)
1530 {
1531         struct umtx_q *uq;
1532         struct umtx_pi *pi;
1533         int pri;
1534
1535         mtx_assert(&umtx_lock, MA_OWNED);
1536         pri = UPRI(td);
1537         uq = td->td_umtxq;
1538         pi = uq->uq_pi_blocked;
1539         if (pi == NULL)
1540                 return;
1541         if (umtx_pi_check_loop(pi))
1542                 return;
1543
1544         for (;;) {
1545                 td = pi->pi_owner;
1546                 if (td == NULL || td == curthread)
1547                         return;
1548
1549                 MPASS(td->td_proc != NULL);
1550                 MPASS(td->td_proc->p_magic == P_MAGIC);
1551
1552                 thread_lock(td);
1553                 if (td->td_lend_user_pri > pri)
1554                         sched_lend_user_prio(td, pri);
1555                 else {
1556                         thread_unlock(td);
1557                         break;
1558                 }
1559                 thread_unlock(td);
1560
1561                 /*
1562                  * Pick up the lock that td is blocked on.
1563                  */
1564                 uq = td->td_umtxq;
1565                 pi = uq->uq_pi_blocked;
1566                 if (pi == NULL)
1567                         break;
1568                 /* Resort td on the list if needed. */
1569                 umtx_pi_adjust_thread(pi, td);
1570         }
1571 }
1572
1573 /*
1574  * Unpropagate priority for a PI mutex when a thread blocked on
1575  * it is interrupted by signal or resumed by others.
1576  */
1577 static void
1578 umtx_repropagate_priority(struct umtx_pi *pi)
1579 {
1580         struct umtx_q *uq, *uq_owner;
1581         struct umtx_pi *pi2;
1582         int pri;
1583
1584         mtx_assert(&umtx_lock, MA_OWNED);
1585
1586         if (umtx_pi_check_loop(pi))
1587                 return;
1588         while (pi != NULL && pi->pi_owner != NULL) {
1589                 pri = PRI_MAX;
1590                 uq_owner = pi->pi_owner->td_umtxq;
1591
1592                 TAILQ_FOREACH(pi2, &uq_owner->uq_pi_contested, pi_link) {
1593                         uq = TAILQ_FIRST(&pi2->pi_blocked);
1594                         if (uq != NULL) {
1595                                 if (pri > UPRI(uq->uq_thread))
1596                                         pri = UPRI(uq->uq_thread);
1597                         }
1598                 }
1599
1600                 if (pri > uq_owner->uq_inherited_pri)
1601                         pri = uq_owner->uq_inherited_pri;
1602                 thread_lock(pi->pi_owner);
1603                 sched_lend_user_prio(pi->pi_owner, pri);
1604                 thread_unlock(pi->pi_owner);
1605                 if ((pi = uq_owner->uq_pi_blocked) != NULL)
1606                         umtx_pi_adjust_thread(pi, uq_owner->uq_thread);
1607         }
1608 }
1609
1610 /*
1611  * Insert a PI mutex into owned list.
1612  */
1613 static void
1614 umtx_pi_setowner(struct umtx_pi *pi, struct thread *owner)
1615 {
1616         struct umtx_q *uq_owner;
1617
1618         uq_owner = owner->td_umtxq;
1619         mtx_assert(&umtx_lock, MA_OWNED);
1620         MPASS(pi->pi_owner == NULL);
1621         pi->pi_owner = owner;
1622         TAILQ_INSERT_TAIL(&uq_owner->uq_pi_contested, pi, pi_link);
1623 }
1624
1625
1626 /*
1627  * Disown a PI mutex, and remove it from the owned list.
1628  */
1629 static void
1630 umtx_pi_disown(struct umtx_pi *pi)
1631 {
1632
1633         mtx_assert(&umtx_lock, MA_OWNED);
1634         TAILQ_REMOVE(&pi->pi_owner->td_umtxq->uq_pi_contested, pi, pi_link);
1635         pi->pi_owner = NULL;
1636 }
1637
1638 /*
1639  * Claim ownership of a PI mutex.
1640  */
1641 static int
1642 umtx_pi_claim(struct umtx_pi *pi, struct thread *owner)
1643 {
1644         struct umtx_q *uq;
1645         int pri;
1646
1647         mtx_lock(&umtx_lock);
1648         if (pi->pi_owner == owner) {
1649                 mtx_unlock(&umtx_lock);
1650                 return (0);
1651         }
1652
1653         if (pi->pi_owner != NULL) {
1654                 /*
1655                  * userland may have already messed the mutex, sigh.
1656                  */
1657                 mtx_unlock(&umtx_lock);
1658                 return (EPERM);
1659         }
1660         umtx_pi_setowner(pi, owner);
1661         uq = TAILQ_FIRST(&pi->pi_blocked);
1662         if (uq != NULL) {
1663                 pri = UPRI(uq->uq_thread);
1664                 thread_lock(owner);
1665                 if (pri < UPRI(owner))
1666                         sched_lend_user_prio(owner, pri);
1667                 thread_unlock(owner);
1668         }
1669         mtx_unlock(&umtx_lock);
1670         return (0);
1671 }
1672
1673 /*
1674  * Adjust a thread's order position in its blocked PI mutex,
1675  * this may result new priority propagating process.
1676  */
1677 void
1678 umtx_pi_adjust(struct thread *td, u_char oldpri)
1679 {
1680         struct umtx_q *uq;
1681         struct umtx_pi *pi;
1682
1683         uq = td->td_umtxq;
1684         mtx_lock(&umtx_lock);
1685         /*
1686          * Pick up the lock that td is blocked on.
1687          */
1688         pi = uq->uq_pi_blocked;
1689         if (pi != NULL) {
1690                 umtx_pi_adjust_thread(pi, td);
1691                 umtx_repropagate_priority(pi);
1692         }
1693         mtx_unlock(&umtx_lock);
1694 }
1695
1696 /*
1697  * Sleep on a PI mutex.
1698  */
1699 static int
1700 umtxq_sleep_pi(struct umtx_q *uq, struct umtx_pi *pi, uint32_t owner,
1701     const char *wmesg, struct abs_timeout *timo, bool shared)
1702 {
1703         struct thread *td, *td1;
1704         struct umtx_q *uq1;
1705         int error, pri;
1706 #ifdef INVARIANTS
1707         struct umtxq_chain *uc;
1708
1709         uc = umtxq_getchain(&pi->pi_key);
1710 #endif
1711         error = 0;
1712         td = uq->uq_thread;
1713         KASSERT(td == curthread, ("inconsistent uq_thread"));
1714         UMTXQ_LOCKED_ASSERT(umtxq_getchain(&uq->uq_key));
1715         KASSERT(uc->uc_busy != 0, ("umtx chain is not busy"));
1716         umtxq_insert(uq);
1717         mtx_lock(&umtx_lock);
1718         if (pi->pi_owner == NULL) {
1719                 mtx_unlock(&umtx_lock);
1720                 td1 = tdfind(owner, shared ? -1 : td->td_proc->p_pid);
1721                 mtx_lock(&umtx_lock);
1722                 if (td1 != NULL) {
1723                         if (pi->pi_owner == NULL)
1724                                 umtx_pi_setowner(pi, td1);
1725                         PROC_UNLOCK(td1->td_proc);
1726                 }
1727         }
1728
1729         TAILQ_FOREACH(uq1, &pi->pi_blocked, uq_lockq) {
1730                 pri = UPRI(uq1->uq_thread);
1731                 if (pri > UPRI(td))
1732                         break;
1733         }
1734
1735         if (uq1 != NULL)
1736                 TAILQ_INSERT_BEFORE(uq1, uq, uq_lockq);
1737         else
1738                 TAILQ_INSERT_TAIL(&pi->pi_blocked, uq, uq_lockq);
1739
1740         uq->uq_pi_blocked = pi;
1741         thread_lock(td);
1742         td->td_flags |= TDF_UPIBLOCKED;
1743         thread_unlock(td);
1744         umtx_propagate_priority(td);
1745         mtx_unlock(&umtx_lock);
1746         umtxq_unbusy(&uq->uq_key);
1747
1748         error = umtxq_sleep(uq, wmesg, timo);
1749         umtxq_remove(uq);
1750
1751         mtx_lock(&umtx_lock);
1752         uq->uq_pi_blocked = NULL;
1753         thread_lock(td);
1754         td->td_flags &= ~TDF_UPIBLOCKED;
1755         thread_unlock(td);
1756         TAILQ_REMOVE(&pi->pi_blocked, uq, uq_lockq);
1757         umtx_repropagate_priority(pi);
1758         mtx_unlock(&umtx_lock);
1759         umtxq_unlock(&uq->uq_key);
1760
1761         return (error);
1762 }
1763
1764 /*
1765  * Add reference count for a PI mutex.
1766  */
1767 static void
1768 umtx_pi_ref(struct umtx_pi *pi)
1769 {
1770
1771         UMTXQ_LOCKED_ASSERT(umtxq_getchain(&pi->pi_key));
1772         pi->pi_refcount++;
1773 }
1774
1775 /*
1776  * Decrease reference count for a PI mutex, if the counter
1777  * is decreased to zero, its memory space is freed.
1778  */ 
1779 static void
1780 umtx_pi_unref(struct umtx_pi *pi)
1781 {
1782         struct umtxq_chain *uc;
1783
1784         uc = umtxq_getchain(&pi->pi_key);
1785         UMTXQ_LOCKED_ASSERT(uc);
1786         KASSERT(pi->pi_refcount > 0, ("invalid reference count"));
1787         if (--pi->pi_refcount == 0) {
1788                 mtx_lock(&umtx_lock);
1789                 if (pi->pi_owner != NULL)
1790                         umtx_pi_disown(pi);
1791                 KASSERT(TAILQ_EMPTY(&pi->pi_blocked),
1792                         ("blocked queue not empty"));
1793                 mtx_unlock(&umtx_lock);
1794                 TAILQ_REMOVE(&uc->uc_pi_list, pi, pi_hashlink);
1795                 umtx_pi_free(pi);
1796         }
1797 }
1798
1799 /*
1800  * Find a PI mutex in hash table.
1801  */
1802 static struct umtx_pi *
1803 umtx_pi_lookup(struct umtx_key *key)
1804 {
1805         struct umtxq_chain *uc;
1806         struct umtx_pi *pi;
1807
1808         uc = umtxq_getchain(key);
1809         UMTXQ_LOCKED_ASSERT(uc);
1810
1811         TAILQ_FOREACH(pi, &uc->uc_pi_list, pi_hashlink) {
1812                 if (umtx_key_match(&pi->pi_key, key)) {
1813                         return (pi);
1814                 }
1815         }
1816         return (NULL);
1817 }
1818
1819 /*
1820  * Insert a PI mutex into hash table.
1821  */
1822 static inline void
1823 umtx_pi_insert(struct umtx_pi *pi)
1824 {
1825         struct umtxq_chain *uc;
1826
1827         uc = umtxq_getchain(&pi->pi_key);
1828         UMTXQ_LOCKED_ASSERT(uc);
1829         TAILQ_INSERT_TAIL(&uc->uc_pi_list, pi, pi_hashlink);
1830 }
1831
1832 /*
1833  * Lock a PI mutex.
1834  */
1835 static int
1836 do_lock_pi(struct thread *td, struct umutex *m, uint32_t flags,
1837     struct _umtx_time *timeout, int try)
1838 {
1839         struct abs_timeout timo;
1840         struct umtx_q *uq;
1841         struct umtx_pi *pi, *new_pi;
1842         uint32_t id, old_owner, owner, old;
1843         int error, rv;
1844
1845         id = td->td_tid;
1846         uq = td->td_umtxq;
1847
1848         if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
1849             TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
1850             &uq->uq_key)) != 0)
1851                 return (error);
1852
1853         if (timeout != NULL)
1854                 abs_timeout_init2(&timo, timeout);
1855
1856         umtxq_lock(&uq->uq_key);
1857         pi = umtx_pi_lookup(&uq->uq_key);
1858         if (pi == NULL) {
1859                 new_pi = umtx_pi_alloc(M_NOWAIT);
1860                 if (new_pi == NULL) {
1861                         umtxq_unlock(&uq->uq_key);
1862                         new_pi = umtx_pi_alloc(M_WAITOK);
1863                         umtxq_lock(&uq->uq_key);
1864                         pi = umtx_pi_lookup(&uq->uq_key);
1865                         if (pi != NULL) {
1866                                 umtx_pi_free(new_pi);
1867                                 new_pi = NULL;
1868                         }
1869                 }
1870                 if (new_pi != NULL) {
1871                         new_pi->pi_key = uq->uq_key;
1872                         umtx_pi_insert(new_pi);
1873                         pi = new_pi;
1874                 }
1875         }
1876         umtx_pi_ref(pi);
1877         umtxq_unlock(&uq->uq_key);
1878
1879         /*
1880          * Care must be exercised when dealing with umtx structure.  It
1881          * can fault on any access.
1882          */
1883         for (;;) {
1884                 /*
1885                  * Try the uncontested case.  This should be done in userland.
1886                  */
1887                 rv = casueword32(&m->m_owner, UMUTEX_UNOWNED, &owner, id);
1888                 /* The address was invalid. */
1889                 if (rv == -1) {
1890                         error = EFAULT;
1891                         break;
1892                 }
1893                 /* The acquire succeeded. */
1894                 if (rv == 0) {
1895                         MPASS(owner == UMUTEX_UNOWNED);
1896                         error = 0;
1897                         break;
1898                 }
1899
1900                 if (owner == UMUTEX_RB_NOTRECOV) {
1901                         error = ENOTRECOVERABLE;
1902                         break;
1903                 }
1904
1905                 /*
1906                  * Avoid overwriting a possible error from sleep due
1907                  * to the pending signal with suspension check result.
1908                  */
1909                 if (error == 0) {
1910                         error = umtxq_check_susp(td, true);
1911                         if (error != 0)
1912                                 break;
1913                 }
1914
1915                 /* If no one owns it but it is contested try to acquire it. */
1916                 if (owner == UMUTEX_CONTESTED || owner == UMUTEX_RB_OWNERDEAD) {
1917                         old_owner = owner;
1918                         rv = casueword32(&m->m_owner, owner, &owner,
1919                             id | UMUTEX_CONTESTED);
1920                         /* The address was invalid. */
1921                         if (rv == -1) {
1922                                 error = EFAULT;
1923                                 break;
1924                         }
1925                         if (rv == 1) {
1926                                 if (error == 0) {
1927                                         error = umtxq_check_susp(td, true);
1928                                         if (error != 0)
1929                                                 break;
1930                                 }
1931
1932                                 /*
1933                                  * If this failed the lock could
1934                                  * changed, restart.
1935                                  */
1936                                 continue;
1937                         }
1938
1939                         MPASS(rv == 0);
1940                         MPASS(owner == old_owner);
1941                         umtxq_lock(&uq->uq_key);
1942                         umtxq_busy(&uq->uq_key);
1943                         error = umtx_pi_claim(pi, td);
1944                         umtxq_unbusy(&uq->uq_key);
1945                         umtxq_unlock(&uq->uq_key);
1946                         if (error != 0) {
1947                                 /*
1948                                  * Since we're going to return an
1949                                  * error, restore the m_owner to its
1950                                  * previous, unowned state to avoid
1951                                  * compounding the problem.
1952                                  */
1953                                 (void)casuword32(&m->m_owner,
1954                                     id | UMUTEX_CONTESTED, old_owner);
1955                         }
1956                         if (error == 0 && old_owner == UMUTEX_RB_OWNERDEAD)
1957                                 error = EOWNERDEAD;
1958                         break;
1959                 }
1960
1961                 if ((owner & ~UMUTEX_CONTESTED) == id) {
1962                         error = EDEADLK;
1963                         break;
1964                 }
1965
1966                 if (try != 0) {
1967                         error = EBUSY;
1968                         break;
1969                 }
1970
1971                 /*
1972                  * If we caught a signal, we have retried and now
1973                  * exit immediately.
1974                  */
1975                 if (error != 0)
1976                         break;
1977                         
1978                 umtxq_lock(&uq->uq_key);
1979                 umtxq_busy(&uq->uq_key);
1980                 umtxq_unlock(&uq->uq_key);
1981
1982                 /*
1983                  * Set the contested bit so that a release in user space
1984                  * knows to use the system call for unlock.  If this fails
1985                  * either some one else has acquired the lock or it has been
1986                  * released.
1987                  */
1988                 rv = casueword32(&m->m_owner, owner, &old, owner |
1989                     UMUTEX_CONTESTED);
1990
1991                 /* The address was invalid. */
1992                 if (rv == -1) {
1993                         umtxq_unbusy_unlocked(&uq->uq_key);
1994                         error = EFAULT;
1995                         break;
1996                 }
1997                 if (rv == 1) {
1998                         umtxq_unbusy_unlocked(&uq->uq_key);
1999                         error = umtxq_check_susp(td, true);
2000                         if (error != 0)
2001                                 break;
2002
2003                         /*
2004                          * The lock changed and we need to retry or we
2005                          * lost a race to the thread unlocking the
2006                          * umtx.  Note that the UMUTEX_RB_OWNERDEAD
2007                          * value for owner is impossible there.
2008                          */
2009                         continue;
2010                 }
2011
2012                 umtxq_lock(&uq->uq_key);
2013
2014                 /* We set the contested bit, sleep. */
2015                 MPASS(old == owner);
2016                 error = umtxq_sleep_pi(uq, pi, owner & ~UMUTEX_CONTESTED,
2017                     "umtxpi", timeout == NULL ? NULL : &timo,
2018                     (flags & USYNC_PROCESS_SHARED) != 0);
2019                 if (error != 0)
2020                         continue;
2021
2022                 error = umtxq_check_susp(td, false);
2023                 if (error != 0)
2024                         break;
2025         }
2026
2027         umtxq_lock(&uq->uq_key);
2028         umtx_pi_unref(pi);
2029         umtxq_unlock(&uq->uq_key);
2030
2031         umtx_key_release(&uq->uq_key);
2032         return (error);
2033 }
2034
2035 /*
2036  * Unlock a PI mutex.
2037  */
2038 static int
2039 do_unlock_pi(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2040 {
2041         struct umtx_key key;
2042         struct umtx_q *uq_first, *uq_first2, *uq_me;
2043         struct umtx_pi *pi, *pi2;
2044         uint32_t id, new_owner, old, owner;
2045         int count, error, pri;
2046
2047         id = td->td_tid;
2048
2049 usrloop:
2050         /*
2051          * Make sure we own this mtx.
2052          */
2053         error = fueword32(&m->m_owner, &owner);
2054         if (error == -1)
2055                 return (EFAULT);
2056
2057         if ((owner & ~UMUTEX_CONTESTED) != id)
2058                 return (EPERM);
2059
2060         new_owner = umtx_unlock_val(flags, rb);
2061
2062         /* This should be done in userland */
2063         if ((owner & UMUTEX_CONTESTED) == 0) {
2064                 error = casueword32(&m->m_owner, owner, &old, new_owner);
2065                 if (error == -1)
2066                         return (EFAULT);
2067                 if (error == 1) {
2068                         error = umtxq_check_susp(td, true);
2069                         if (error != 0)
2070                                 return (error);
2071                         goto usrloop;
2072                 }
2073                 if (old == owner)
2074                         return (0);
2075                 owner = old;
2076         }
2077
2078         /* We should only ever be in here for contested locks */
2079         if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2080             TYPE_PI_ROBUST_UMUTEX : TYPE_PI_UMUTEX, GET_SHARE(flags),
2081             &key)) != 0)
2082                 return (error);
2083
2084         umtxq_lock(&key);
2085         umtxq_busy(&key);
2086         count = umtxq_count_pi(&key, &uq_first);
2087         if (uq_first != NULL) {
2088                 mtx_lock(&umtx_lock);
2089                 pi = uq_first->uq_pi_blocked;
2090                 KASSERT(pi != NULL, ("pi == NULL?"));
2091                 if (pi->pi_owner != td && !(rb && pi->pi_owner == NULL)) {
2092                         mtx_unlock(&umtx_lock);
2093                         umtxq_unbusy(&key);
2094                         umtxq_unlock(&key);
2095                         umtx_key_release(&key);
2096                         /* userland messed the mutex */
2097                         return (EPERM);
2098                 }
2099                 uq_me = td->td_umtxq;
2100                 if (pi->pi_owner == td)
2101                         umtx_pi_disown(pi);
2102                 /* get highest priority thread which is still sleeping. */
2103                 uq_first = TAILQ_FIRST(&pi->pi_blocked);
2104                 while (uq_first != NULL && 
2105                     (uq_first->uq_flags & UQF_UMTXQ) == 0) {
2106                         uq_first = TAILQ_NEXT(uq_first, uq_lockq);
2107                 }
2108                 pri = PRI_MAX;
2109                 TAILQ_FOREACH(pi2, &uq_me->uq_pi_contested, pi_link) {
2110                         uq_first2 = TAILQ_FIRST(&pi2->pi_blocked);
2111                         if (uq_first2 != NULL) {
2112                                 if (pri > UPRI(uq_first2->uq_thread))
2113                                         pri = UPRI(uq_first2->uq_thread);
2114                         }
2115                 }
2116                 thread_lock(td);
2117                 sched_lend_user_prio(td, pri);
2118                 thread_unlock(td);
2119                 mtx_unlock(&umtx_lock);
2120                 if (uq_first)
2121                         umtxq_signal_thread(uq_first);
2122         } else {
2123                 pi = umtx_pi_lookup(&key);
2124                 /*
2125                  * A umtx_pi can exist if a signal or timeout removed the
2126                  * last waiter from the umtxq, but there is still
2127                  * a thread in do_lock_pi() holding the umtx_pi.
2128                  */
2129                 if (pi != NULL) {
2130                         /*
2131                          * The umtx_pi can be unowned, such as when a thread
2132                          * has just entered do_lock_pi(), allocated the
2133                          * umtx_pi, and unlocked the umtxq.
2134                          * If the current thread owns it, it must disown it.
2135                          */
2136                         mtx_lock(&umtx_lock);
2137                         if (pi->pi_owner == td)
2138                                 umtx_pi_disown(pi);
2139                         mtx_unlock(&umtx_lock);
2140                 }
2141         }
2142         umtxq_unlock(&key);
2143
2144         /*
2145          * When unlocking the umtx, it must be marked as unowned if
2146          * there is zero or one thread only waiting for it.
2147          * Otherwise, it must be marked as contested.
2148          */
2149
2150         if (count > 1)
2151                 new_owner |= UMUTEX_CONTESTED;
2152 again:
2153         error = casueword32(&m->m_owner, owner, &old, new_owner);
2154         if (error == 1) {
2155                 error = umtxq_check_susp(td, false);
2156                 if (error == 0)
2157                         goto again;
2158         }
2159         umtxq_unbusy_unlocked(&key);
2160         umtx_key_release(&key);
2161         if (error == -1)
2162                 return (EFAULT);
2163         if (error == 0 && old != owner)
2164                 return (EINVAL);
2165         return (error);
2166 }
2167
2168 /*
2169  * Lock a PP mutex.
2170  */
2171 static int
2172 do_lock_pp(struct thread *td, struct umutex *m, uint32_t flags,
2173     struct _umtx_time *timeout, int try)
2174 {
2175         struct abs_timeout timo;
2176         struct umtx_q *uq, *uq2;
2177         struct umtx_pi *pi;
2178         uint32_t ceiling;
2179         uint32_t owner, id;
2180         int error, pri, old_inherited_pri, su, rv;
2181
2182         id = td->td_tid;
2183         uq = td->td_umtxq;
2184         if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2185             TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2186             &uq->uq_key)) != 0)
2187                 return (error);
2188
2189         if (timeout != NULL)
2190                 abs_timeout_init2(&timo, timeout);
2191
2192         su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2193         for (;;) {
2194                 old_inherited_pri = uq->uq_inherited_pri;
2195                 umtxq_lock(&uq->uq_key);
2196                 umtxq_busy(&uq->uq_key);
2197                 umtxq_unlock(&uq->uq_key);
2198
2199                 rv = fueword32(&m->m_ceilings[0], &ceiling);
2200                 if (rv == -1) {
2201                         error = EFAULT;
2202                         goto out;
2203                 }
2204                 ceiling = RTP_PRIO_MAX - ceiling;
2205                 if (ceiling > RTP_PRIO_MAX) {
2206                         error = EINVAL;
2207                         goto out;
2208                 }
2209
2210                 mtx_lock(&umtx_lock);
2211                 if (UPRI(td) < PRI_MIN_REALTIME + ceiling) {
2212                         mtx_unlock(&umtx_lock);
2213                         error = EINVAL;
2214                         goto out;
2215                 }
2216                 if (su && PRI_MIN_REALTIME + ceiling < uq->uq_inherited_pri) {
2217                         uq->uq_inherited_pri = PRI_MIN_REALTIME + ceiling;
2218                         thread_lock(td);
2219                         if (uq->uq_inherited_pri < UPRI(td))
2220                                 sched_lend_user_prio(td, uq->uq_inherited_pri);
2221                         thread_unlock(td);
2222                 }
2223                 mtx_unlock(&umtx_lock);
2224
2225                 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2226                     id | UMUTEX_CONTESTED);
2227                 /* The address was invalid. */
2228                 if (rv == -1) {
2229                         error = EFAULT;
2230                         break;
2231                 }
2232                 if (rv == 0) {
2233                         MPASS(owner == UMUTEX_CONTESTED);
2234                         error = 0;
2235                         break;
2236                 }
2237                 /* rv == 1 */
2238                 if (owner == UMUTEX_RB_OWNERDEAD) {
2239                         rv = casueword32(&m->m_owner, UMUTEX_RB_OWNERDEAD,
2240                             &owner, id | UMUTEX_CONTESTED);
2241                         if (rv == -1) {
2242                                 error = EFAULT;
2243                                 break;
2244                         }
2245                         if (rv == 0) {
2246                                 MPASS(owner == UMUTEX_RB_OWNERDEAD);
2247                                 error = EOWNERDEAD; /* success */
2248                                 break;
2249                         }
2250
2251                         /*
2252                          *  rv == 1, only check for suspension if we
2253                          *  did not already catched a signal.  If we
2254                          *  get an error from the check, the same
2255                          *  condition is checked by the umtxq_sleep()
2256                          *  call below, so we should obliterate the
2257                          *  error to not skip the last loop iteration.
2258                          */
2259                         if (error == 0) {
2260                                 error = umtxq_check_susp(td, false);
2261                                 if (error == 0) {
2262                                         if (try != 0)
2263                                                 error = EBUSY;
2264                                         else
2265                                                 continue;
2266                                 }
2267                                 error = 0;
2268                         }
2269                 } else if (owner == UMUTEX_RB_NOTRECOV) {
2270                         error = ENOTRECOVERABLE;
2271                 }
2272
2273                 if (try != 0)
2274                         error = EBUSY;
2275
2276                 /*
2277                  * If we caught a signal, we have retried and now
2278                  * exit immediately.
2279                  */
2280                 if (error != 0)
2281                         break;
2282
2283                 umtxq_lock(&uq->uq_key);
2284                 umtxq_insert(uq);
2285                 umtxq_unbusy(&uq->uq_key);
2286                 error = umtxq_sleep(uq, "umtxpp", timeout == NULL ?
2287                     NULL : &timo);
2288                 umtxq_remove(uq);
2289                 umtxq_unlock(&uq->uq_key);
2290
2291                 mtx_lock(&umtx_lock);
2292                 uq->uq_inherited_pri = old_inherited_pri;
2293                 pri = PRI_MAX;
2294                 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2295                         uq2 = TAILQ_FIRST(&pi->pi_blocked);
2296                         if (uq2 != NULL) {
2297                                 if (pri > UPRI(uq2->uq_thread))
2298                                         pri = UPRI(uq2->uq_thread);
2299                         }
2300                 }
2301                 if (pri > uq->uq_inherited_pri)
2302                         pri = uq->uq_inherited_pri;
2303                 thread_lock(td);
2304                 sched_lend_user_prio(td, pri);
2305                 thread_unlock(td);
2306                 mtx_unlock(&umtx_lock);
2307         }
2308
2309         if (error != 0 && error != EOWNERDEAD) {
2310                 mtx_lock(&umtx_lock);
2311                 uq->uq_inherited_pri = old_inherited_pri;
2312                 pri = PRI_MAX;
2313                 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2314                         uq2 = TAILQ_FIRST(&pi->pi_blocked);
2315                         if (uq2 != NULL) {
2316                                 if (pri > UPRI(uq2->uq_thread))
2317                                         pri = UPRI(uq2->uq_thread);
2318                         }
2319                 }
2320                 if (pri > uq->uq_inherited_pri)
2321                         pri = uq->uq_inherited_pri;
2322                 thread_lock(td);
2323                 sched_lend_user_prio(td, pri);
2324                 thread_unlock(td);
2325                 mtx_unlock(&umtx_lock);
2326         }
2327
2328 out:
2329         umtxq_unbusy_unlocked(&uq->uq_key);
2330         umtx_key_release(&uq->uq_key);
2331         return (error);
2332 }
2333
2334 /*
2335  * Unlock a PP mutex.
2336  */
2337 static int
2338 do_unlock_pp(struct thread *td, struct umutex *m, uint32_t flags, bool rb)
2339 {
2340         struct umtx_key key;
2341         struct umtx_q *uq, *uq2;
2342         struct umtx_pi *pi;
2343         uint32_t id, owner, rceiling;
2344         int error, pri, new_inherited_pri, su;
2345
2346         id = td->td_tid;
2347         uq = td->td_umtxq;
2348         su = (priv_check(td, PRIV_SCHED_RTPRIO) == 0);
2349
2350         /*
2351          * Make sure we own this mtx.
2352          */
2353         error = fueword32(&m->m_owner, &owner);
2354         if (error == -1)
2355                 return (EFAULT);
2356
2357         if ((owner & ~UMUTEX_CONTESTED) != id)
2358                 return (EPERM);
2359
2360         error = copyin(&m->m_ceilings[1], &rceiling, sizeof(uint32_t));
2361         if (error != 0)
2362                 return (error);
2363
2364         if (rceiling == -1)
2365                 new_inherited_pri = PRI_MAX;
2366         else {
2367                 rceiling = RTP_PRIO_MAX - rceiling;
2368                 if (rceiling > RTP_PRIO_MAX)
2369                         return (EINVAL);
2370                 new_inherited_pri = PRI_MIN_REALTIME + rceiling;
2371         }
2372
2373         if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2374             TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2375             &key)) != 0)
2376                 return (error);
2377         umtxq_lock(&key);
2378         umtxq_busy(&key);
2379         umtxq_unlock(&key);
2380         /*
2381          * For priority protected mutex, always set unlocked state
2382          * to UMUTEX_CONTESTED, so that userland always enters kernel
2383          * to lock the mutex, it is necessary because thread priority
2384          * has to be adjusted for such mutex.
2385          */
2386         error = suword32(&m->m_owner, umtx_unlock_val(flags, rb) |
2387             UMUTEX_CONTESTED);
2388
2389         umtxq_lock(&key);
2390         if (error == 0)
2391                 umtxq_signal(&key, 1);
2392         umtxq_unbusy(&key);
2393         umtxq_unlock(&key);
2394
2395         if (error == -1)
2396                 error = EFAULT;
2397         else {
2398                 mtx_lock(&umtx_lock);
2399                 if (su != 0)
2400                         uq->uq_inherited_pri = new_inherited_pri;
2401                 pri = PRI_MAX;
2402                 TAILQ_FOREACH(pi, &uq->uq_pi_contested, pi_link) {
2403                         uq2 = TAILQ_FIRST(&pi->pi_blocked);
2404                         if (uq2 != NULL) {
2405                                 if (pri > UPRI(uq2->uq_thread))
2406                                         pri = UPRI(uq2->uq_thread);
2407                         }
2408                 }
2409                 if (pri > uq->uq_inherited_pri)
2410                         pri = uq->uq_inherited_pri;
2411                 thread_lock(td);
2412                 sched_lend_user_prio(td, pri);
2413                 thread_unlock(td);
2414                 mtx_unlock(&umtx_lock);
2415         }
2416         umtx_key_release(&key);
2417         return (error);
2418 }
2419
2420 static int
2421 do_set_ceiling(struct thread *td, struct umutex *m, uint32_t ceiling,
2422     uint32_t *old_ceiling)
2423 {
2424         struct umtx_q *uq;
2425         uint32_t flags, id, owner, save_ceiling;
2426         int error, rv, rv1;
2427
2428         error = fueword32(&m->m_flags, &flags);
2429         if (error == -1)
2430                 return (EFAULT);
2431         if ((flags & UMUTEX_PRIO_PROTECT) == 0)
2432                 return (EINVAL);
2433         if (ceiling > RTP_PRIO_MAX)
2434                 return (EINVAL);
2435         id = td->td_tid;
2436         uq = td->td_umtxq;
2437         if ((error = umtx_key_get(m, (flags & UMUTEX_ROBUST) != 0 ?
2438             TYPE_PP_ROBUST_UMUTEX : TYPE_PP_UMUTEX, GET_SHARE(flags),
2439             &uq->uq_key)) != 0)
2440                 return (error);
2441         for (;;) {
2442                 umtxq_lock(&uq->uq_key);
2443                 umtxq_busy(&uq->uq_key);
2444                 umtxq_unlock(&uq->uq_key);
2445
2446                 rv = fueword32(&m->m_ceilings[0], &save_ceiling);
2447                 if (rv == -1) {
2448                         error = EFAULT;
2449                         break;
2450                 }
2451
2452                 rv = casueword32(&m->m_owner, UMUTEX_CONTESTED, &owner,
2453                     id | UMUTEX_CONTESTED);
2454                 if (rv == -1) {
2455                         error = EFAULT;
2456                         break;
2457                 }
2458
2459                 if (rv == 0) {
2460                         MPASS(owner == UMUTEX_CONTESTED);
2461                         rv = suword32(&m->m_ceilings[0], ceiling);
2462                         rv1 = suword32(&m->m_owner, UMUTEX_CONTESTED);
2463                         error = (rv == 0 && rv1 == 0) ? 0: EFAULT;
2464                         break;
2465                 }
2466
2467                 if ((owner & ~UMUTEX_CONTESTED) == id) {
2468                         rv = suword32(&m->m_ceilings[0], ceiling);
2469                         error = rv == 0 ? 0 : EFAULT;
2470                         break;
2471                 }
2472
2473                 if (owner == UMUTEX_RB_OWNERDEAD) {
2474                         error = EOWNERDEAD;
2475                         break;
2476                 } else if (owner == UMUTEX_RB_NOTRECOV) {
2477                         error = ENOTRECOVERABLE;
2478                         break;
2479                 }
2480
2481                 /*
2482                  * If we caught a signal, we have retried and now
2483                  * exit immediately.
2484                  */
2485                 if (error != 0)
2486                         break;
2487
2488                 /*
2489                  * We set the contested bit, sleep. Otherwise the lock changed
2490                  * and we need to retry or we lost a race to the thread
2491                  * unlocking the umtx.
2492                  */
2493                 umtxq_lock(&uq->uq_key);
2494                 umtxq_insert(uq);
2495                 umtxq_unbusy(&uq->uq_key);
2496                 error = umtxq_sleep(uq, "umtxpp", NULL);
2497                 umtxq_remove(uq);
2498                 umtxq_unlock(&uq->uq_key);
2499         }
2500         umtxq_lock(&uq->uq_key);
2501         if (error == 0)
2502                 umtxq_signal(&uq->uq_key, INT_MAX);
2503         umtxq_unbusy(&uq->uq_key);
2504         umtxq_unlock(&uq->uq_key);
2505         umtx_key_release(&uq->uq_key);
2506         if (error == 0 && old_ceiling != NULL) {
2507                 rv = suword32(old_ceiling, save_ceiling);
2508                 error = rv == 0 ? 0 : EFAULT;
2509         }
2510         return (error);
2511 }
2512
2513 /*
2514  * Lock a userland POSIX mutex.
2515  */
2516 static int
2517 do_lock_umutex(struct thread *td, struct umutex *m,
2518     struct _umtx_time *timeout, int mode)
2519 {
2520         uint32_t flags;
2521         int error;
2522
2523         error = fueword32(&m->m_flags, &flags);
2524         if (error == -1)
2525                 return (EFAULT);
2526
2527         switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2528         case 0:
2529                 error = do_lock_normal(td, m, flags, timeout, mode);
2530                 break;
2531         case UMUTEX_PRIO_INHERIT:
2532                 error = do_lock_pi(td, m, flags, timeout, mode);
2533                 break;
2534         case UMUTEX_PRIO_PROTECT:
2535                 error = do_lock_pp(td, m, flags, timeout, mode);
2536                 break;
2537         default:
2538                 return (EINVAL);
2539         }
2540         if (timeout == NULL) {
2541                 if (error == EINTR && mode != _UMUTEX_WAIT)
2542                         error = ERESTART;
2543         } else {
2544                 /* Timed-locking is not restarted. */
2545                 if (error == ERESTART)
2546                         error = EINTR;
2547         }
2548         return (error);
2549 }
2550
2551 /*
2552  * Unlock a userland POSIX mutex.
2553  */
2554 static int
2555 do_unlock_umutex(struct thread *td, struct umutex *m, bool rb)
2556 {
2557         uint32_t flags;
2558         int error;
2559
2560         error = fueword32(&m->m_flags, &flags);
2561         if (error == -1)
2562                 return (EFAULT);
2563
2564         switch (flags & (UMUTEX_PRIO_INHERIT | UMUTEX_PRIO_PROTECT)) {
2565         case 0:
2566                 return (do_unlock_normal(td, m, flags, rb));
2567         case UMUTEX_PRIO_INHERIT:
2568                 return (do_unlock_pi(td, m, flags, rb));
2569         case UMUTEX_PRIO_PROTECT:
2570                 return (do_unlock_pp(td, m, flags, rb));
2571         }
2572
2573         return (EINVAL);
2574 }
2575
2576 static int
2577 do_cv_wait(struct thread *td, struct ucond *cv, struct umutex *m,
2578     struct timespec *timeout, u_long wflags)
2579 {
2580         struct abs_timeout timo;
2581         struct umtx_q *uq;
2582         uint32_t flags, clockid, hasw;
2583         int error;
2584
2585         uq = td->td_umtxq;
2586         error = fueword32(&cv->c_flags, &flags);
2587         if (error == -1)
2588                 return (EFAULT);
2589         error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &uq->uq_key);
2590         if (error != 0)
2591                 return (error);
2592
2593         if ((wflags & CVWAIT_CLOCKID) != 0) {
2594                 error = fueword32(&cv->c_clockid, &clockid);
2595                 if (error == -1) {
2596                         umtx_key_release(&uq->uq_key);
2597                         return (EFAULT);
2598                 }
2599                 if (clockid < CLOCK_REALTIME ||
2600                     clockid >= CLOCK_THREAD_CPUTIME_ID) {
2601                         /* hmm, only HW clock id will work. */
2602                         umtx_key_release(&uq->uq_key);
2603                         return (EINVAL);
2604                 }
2605         } else {
2606                 clockid = CLOCK_REALTIME;
2607         }
2608
2609         umtxq_lock(&uq->uq_key);
2610         umtxq_busy(&uq->uq_key);
2611         umtxq_insert(uq);
2612         umtxq_unlock(&uq->uq_key);
2613
2614         /*
2615          * Set c_has_waiters to 1 before releasing user mutex, also
2616          * don't modify cache line when unnecessary.
2617          */
2618         error = fueword32(&cv->c_has_waiters, &hasw);
2619         if (error == 0 && hasw == 0)
2620                 suword32(&cv->c_has_waiters, 1);
2621
2622         umtxq_unbusy_unlocked(&uq->uq_key);
2623
2624         error = do_unlock_umutex(td, m, false);
2625
2626         if (timeout != NULL)
2627                 abs_timeout_init(&timo, clockid, (wflags & CVWAIT_ABSTIME) != 0,
2628                     timeout);
2629         
2630         umtxq_lock(&uq->uq_key);
2631         if (error == 0) {
2632                 error = umtxq_sleep(uq, "ucond", timeout == NULL ?
2633                     NULL : &timo);
2634         }
2635
2636         if ((uq->uq_flags & UQF_UMTXQ) == 0)
2637                 error = 0;
2638         else {
2639                 /*
2640                  * This must be timeout,interrupted by signal or
2641                  * surprious wakeup, clear c_has_waiter flag when
2642                  * necessary.
2643                  */
2644                 umtxq_busy(&uq->uq_key);
2645                 if ((uq->uq_flags & UQF_UMTXQ) != 0) {
2646                         int oldlen = uq->uq_cur_queue->length;
2647                         umtxq_remove(uq);
2648                         if (oldlen == 1) {
2649                                 umtxq_unlock(&uq->uq_key);
2650                                 suword32(&cv->c_has_waiters, 0);
2651                                 umtxq_lock(&uq->uq_key);
2652                         }
2653                 }
2654                 umtxq_unbusy(&uq->uq_key);
2655                 if (error == ERESTART)
2656                         error = EINTR;
2657         }
2658
2659         umtxq_unlock(&uq->uq_key);
2660         umtx_key_release(&uq->uq_key);
2661         return (error);
2662 }
2663
2664 /*
2665  * Signal a userland condition variable.
2666  */
2667 static int
2668 do_cv_signal(struct thread *td, struct ucond *cv)
2669 {
2670         struct umtx_key key;
2671         int error, cnt, nwake;
2672         uint32_t flags;
2673
2674         error = fueword32(&cv->c_flags, &flags);
2675         if (error == -1)
2676                 return (EFAULT);
2677         if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2678                 return (error); 
2679         umtxq_lock(&key);
2680         umtxq_busy(&key);
2681         cnt = umtxq_count(&key);
2682         nwake = umtxq_signal(&key, 1);
2683         if (cnt <= nwake) {
2684                 umtxq_unlock(&key);
2685                 error = suword32(&cv->c_has_waiters, 0);
2686                 if (error == -1)
2687                         error = EFAULT;
2688                 umtxq_lock(&key);
2689         }
2690         umtxq_unbusy(&key);
2691         umtxq_unlock(&key);
2692         umtx_key_release(&key);
2693         return (error);
2694 }
2695
2696 static int
2697 do_cv_broadcast(struct thread *td, struct ucond *cv)
2698 {
2699         struct umtx_key key;
2700         int error;
2701         uint32_t flags;
2702
2703         error = fueword32(&cv->c_flags, &flags);
2704         if (error == -1)
2705                 return (EFAULT);
2706         if ((error = umtx_key_get(cv, TYPE_CV, GET_SHARE(flags), &key)) != 0)
2707                 return (error); 
2708
2709         umtxq_lock(&key);
2710         umtxq_busy(&key);
2711         umtxq_signal(&key, INT_MAX);
2712         umtxq_unlock(&key);
2713
2714         error = suword32(&cv->c_has_waiters, 0);
2715         if (error == -1)
2716                 error = EFAULT;
2717
2718         umtxq_unbusy_unlocked(&key);
2719
2720         umtx_key_release(&key);
2721         return (error);
2722 }
2723
2724 static int
2725 do_rw_rdlock(struct thread *td, struct urwlock *rwlock, long fflag,
2726     struct _umtx_time *timeout)
2727 {
2728         struct abs_timeout timo;
2729         struct umtx_q *uq;
2730         uint32_t flags, wrflags;
2731         int32_t state, oldstate;
2732         int32_t blocked_readers;
2733         int error, error1, rv;
2734
2735         uq = td->td_umtxq;
2736         error = fueword32(&rwlock->rw_flags, &flags);
2737         if (error == -1)
2738                 return (EFAULT);
2739         error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2740         if (error != 0)
2741                 return (error);
2742
2743         if (timeout != NULL)
2744                 abs_timeout_init2(&timo, timeout);
2745
2746         wrflags = URWLOCK_WRITE_OWNER;
2747         if (!(fflag & URWLOCK_PREFER_READER) && !(flags & URWLOCK_PREFER_READER))
2748                 wrflags |= URWLOCK_WRITE_WAITERS;
2749
2750         for (;;) {
2751                 rv = fueword32(&rwlock->rw_state, &state);
2752                 if (rv == -1) {
2753                         umtx_key_release(&uq->uq_key);
2754                         return (EFAULT);
2755                 }
2756
2757                 /* try to lock it */
2758                 while (!(state & wrflags)) {
2759                         if (__predict_false(URWLOCK_READER_COUNT(state) ==
2760                             URWLOCK_MAX_READERS)) {
2761                                 umtx_key_release(&uq->uq_key);
2762                                 return (EAGAIN);
2763                         }
2764                         rv = casueword32(&rwlock->rw_state, state,
2765                             &oldstate, state + 1);
2766                         if (rv == -1) {
2767                                 umtx_key_release(&uq->uq_key);
2768                                 return (EFAULT);
2769                         }
2770                         if (rv == 0) {
2771                                 MPASS(oldstate == state);
2772                                 umtx_key_release(&uq->uq_key);
2773                                 return (0);
2774                         }
2775                         error = umtxq_check_susp(td, true);
2776                         if (error != 0)
2777                                 break;
2778                         state = oldstate;
2779                 }
2780
2781                 if (error)
2782                         break;
2783
2784                 /* grab monitor lock */
2785                 umtxq_lock(&uq->uq_key);
2786                 umtxq_busy(&uq->uq_key);
2787                 umtxq_unlock(&uq->uq_key);
2788
2789                 /*
2790                  * re-read the state, in case it changed between the try-lock above
2791                  * and the check below
2792                  */
2793                 rv = fueword32(&rwlock->rw_state, &state);
2794                 if (rv == -1)
2795                         error = EFAULT;
2796
2797                 /* set read contention bit */
2798                 while (error == 0 && (state & wrflags) &&
2799                     !(state & URWLOCK_READ_WAITERS)) {
2800                         rv = casueword32(&rwlock->rw_state, state,
2801                             &oldstate, state | URWLOCK_READ_WAITERS);
2802                         if (rv == -1) {
2803                                 error = EFAULT;
2804                                 break;
2805                         }
2806                         if (rv == 0) {
2807                                 MPASS(oldstate == state);
2808                                 goto sleep;
2809                         }
2810                         state = oldstate;
2811                         error = umtxq_check_susp(td, false);
2812                         if (error != 0)
2813                                 break;
2814                 }
2815                 if (error != 0) {
2816                         umtxq_unbusy_unlocked(&uq->uq_key);
2817                         break;
2818                 }
2819
2820                 /* state is changed while setting flags, restart */
2821                 if (!(state & wrflags)) {
2822                         umtxq_unbusy_unlocked(&uq->uq_key);
2823                         error = umtxq_check_susp(td, true);
2824                         if (error != 0)
2825                                 break;
2826                         continue;
2827                 }
2828
2829 sleep:
2830                 /*
2831                  * Contention bit is set, before sleeping, increase
2832                  * read waiter count.
2833                  */
2834                 rv = fueword32(&rwlock->rw_blocked_readers,
2835                     &blocked_readers);
2836                 if (rv == -1) {
2837                         umtxq_unbusy_unlocked(&uq->uq_key);
2838                         error = EFAULT;
2839                         break;
2840                 }
2841                 suword32(&rwlock->rw_blocked_readers, blocked_readers+1);
2842
2843                 while (state & wrflags) {
2844                         umtxq_lock(&uq->uq_key);
2845                         umtxq_insert(uq);
2846                         umtxq_unbusy(&uq->uq_key);
2847
2848                         error = umtxq_sleep(uq, "urdlck", timeout == NULL ?
2849                             NULL : &timo);
2850
2851                         umtxq_busy(&uq->uq_key);
2852                         umtxq_remove(uq);
2853                         umtxq_unlock(&uq->uq_key);
2854                         if (error)
2855                                 break;
2856                         rv = fueword32(&rwlock->rw_state, &state);
2857                         if (rv == -1) {
2858                                 error = EFAULT;
2859                                 break;
2860                         }
2861                 }
2862
2863                 /* decrease read waiter count, and may clear read contention bit */
2864                 rv = fueword32(&rwlock->rw_blocked_readers,
2865                     &blocked_readers);
2866                 if (rv == -1) {
2867                         umtxq_unbusy_unlocked(&uq->uq_key);
2868                         error = EFAULT;
2869                         break;
2870                 }
2871                 suword32(&rwlock->rw_blocked_readers, blocked_readers-1);
2872                 if (blocked_readers == 1) {
2873                         rv = fueword32(&rwlock->rw_state, &state);
2874                         if (rv == -1) {
2875                                 umtxq_unbusy_unlocked(&uq->uq_key);
2876                                 error = EFAULT;
2877                                 break;
2878                         }
2879                         for (;;) {
2880                                 rv = casueword32(&rwlock->rw_state, state,
2881                                     &oldstate, state & ~URWLOCK_READ_WAITERS);
2882                                 if (rv == -1) {
2883                                         error = EFAULT;
2884                                         break;
2885                                 }
2886                                 if (rv == 0) {
2887                                         MPASS(oldstate == state);
2888                                         break;
2889                                 }
2890                                 state = oldstate;
2891                                 error1 = umtxq_check_susp(td, false);
2892                                 if (error1 != 0) {
2893                                         if (error == 0)
2894                                                 error = error1;
2895                                         break;
2896                                 }
2897                         }
2898                 }
2899
2900                 umtxq_unbusy_unlocked(&uq->uq_key);
2901                 if (error != 0)
2902                         break;
2903         }
2904         umtx_key_release(&uq->uq_key);
2905         if (error == ERESTART)
2906                 error = EINTR;
2907         return (error);
2908 }
2909
2910 static int
2911 do_rw_wrlock(struct thread *td, struct urwlock *rwlock, struct _umtx_time *timeout)
2912 {
2913         struct abs_timeout timo;
2914         struct umtx_q *uq;
2915         uint32_t flags;
2916         int32_t state, oldstate;
2917         int32_t blocked_writers;
2918         int32_t blocked_readers;
2919         int error, error1, rv;
2920
2921         uq = td->td_umtxq;
2922         error = fueword32(&rwlock->rw_flags, &flags);
2923         if (error == -1)
2924                 return (EFAULT);
2925         error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
2926         if (error != 0)
2927                 return (error);
2928
2929         if (timeout != NULL)
2930                 abs_timeout_init2(&timo, timeout);
2931
2932         blocked_readers = 0;
2933         for (;;) {
2934                 rv = fueword32(&rwlock->rw_state, &state);
2935                 if (rv == -1) {
2936                         umtx_key_release(&uq->uq_key);
2937                         return (EFAULT);
2938                 }
2939                 while ((state & URWLOCK_WRITE_OWNER) == 0 &&
2940                     URWLOCK_READER_COUNT(state) == 0) {
2941                         rv = casueword32(&rwlock->rw_state, state,
2942                             &oldstate, state | URWLOCK_WRITE_OWNER);
2943                         if (rv == -1) {
2944                                 umtx_key_release(&uq->uq_key);
2945                                 return (EFAULT);
2946                         }
2947                         if (rv == 0) {
2948                                 MPASS(oldstate == state);
2949                                 umtx_key_release(&uq->uq_key);
2950                                 return (0);
2951                         }
2952                         state = oldstate;
2953                         error = umtxq_check_susp(td, true);
2954                         if (error != 0)
2955                                 break;
2956                 }
2957
2958                 if (error) {
2959                         if ((state & (URWLOCK_WRITE_OWNER |
2960                             URWLOCK_WRITE_WAITERS)) == 0 &&
2961                             blocked_readers != 0) {
2962                                 umtxq_lock(&uq->uq_key);
2963                                 umtxq_busy(&uq->uq_key);
2964                                 umtxq_signal_queue(&uq->uq_key, INT_MAX,
2965                                     UMTX_SHARED_QUEUE);
2966                                 umtxq_unbusy(&uq->uq_key);
2967                                 umtxq_unlock(&uq->uq_key);
2968                         }
2969
2970                         break;
2971                 }
2972
2973                 /* grab monitor lock */
2974                 umtxq_lock(&uq->uq_key);
2975                 umtxq_busy(&uq->uq_key);
2976                 umtxq_unlock(&uq->uq_key);
2977
2978                 /*
2979                  * Re-read the state, in case it changed between the
2980                  * try-lock above and the check below.
2981                  */
2982                 rv = fueword32(&rwlock->rw_state, &state);
2983                 if (rv == -1)
2984                         error = EFAULT;
2985
2986                 while (error == 0 && ((state & URWLOCK_WRITE_OWNER) ||
2987                     URWLOCK_READER_COUNT(state) != 0) &&
2988                     (state & URWLOCK_WRITE_WAITERS) == 0) {
2989                         rv = casueword32(&rwlock->rw_state, state,
2990                             &oldstate, state | URWLOCK_WRITE_WAITERS);
2991                         if (rv == -1) {
2992                                 error = EFAULT;
2993                                 break;
2994                         }
2995                         if (rv == 0) {
2996                                 MPASS(oldstate == state);
2997                                 goto sleep;
2998                         }
2999                         state = oldstate;
3000                         error = umtxq_check_susp(td, false);
3001                         if (error != 0)
3002                                 break;
3003                 }
3004                 if (error != 0) {
3005                         umtxq_unbusy_unlocked(&uq->uq_key);
3006                         break;
3007                 }
3008
3009                 if ((state & URWLOCK_WRITE_OWNER) == 0 &&
3010                     URWLOCK_READER_COUNT(state) == 0) {
3011                         umtxq_unbusy_unlocked(&uq->uq_key);
3012                         error = umtxq_check_susp(td, false);
3013                         if (error != 0)
3014                                 break;
3015                         continue;
3016                 }
3017 sleep:
3018                 rv = fueword32(&rwlock->rw_blocked_writers,
3019                     &blocked_writers);
3020                 if (rv == -1) {
3021                         umtxq_unbusy_unlocked(&uq->uq_key);
3022                         error = EFAULT;
3023                         break;
3024                 }
3025                 suword32(&rwlock->rw_blocked_writers, blocked_writers + 1);
3026
3027                 while ((state & URWLOCK_WRITE_OWNER) ||
3028                     URWLOCK_READER_COUNT(state) != 0) {
3029                         umtxq_lock(&uq->uq_key);
3030                         umtxq_insert_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3031                         umtxq_unbusy(&uq->uq_key);
3032
3033                         error = umtxq_sleep(uq, "uwrlck", timeout == NULL ?
3034                             NULL : &timo);
3035
3036                         umtxq_busy(&uq->uq_key);
3037                         umtxq_remove_queue(uq, UMTX_EXCLUSIVE_QUEUE);
3038                         umtxq_unlock(&uq->uq_key);
3039                         if (error)
3040                                 break;
3041                         rv = fueword32(&rwlock->rw_state, &state);
3042                         if (rv == -1) {
3043                                 error = EFAULT;
3044                                 break;
3045                         }
3046                 }
3047
3048                 rv = fueword32(&rwlock->rw_blocked_writers,
3049                     &blocked_writers);
3050                 if (rv == -1) {
3051                         umtxq_unbusy_unlocked(&uq->uq_key);
3052                         error = EFAULT;
3053                         break;
3054                 }
3055                 suword32(&rwlock->rw_blocked_writers, blocked_writers-1);
3056                 if (blocked_writers == 1) {
3057                         rv = fueword32(&rwlock->rw_state, &state);
3058                         if (rv == -1) {
3059                                 umtxq_unbusy_unlocked(&uq->uq_key);
3060                                 error = EFAULT;
3061                                 break;
3062                         }
3063                         for (;;) {
3064                                 rv = casueword32(&rwlock->rw_state, state,
3065                                     &oldstate, state & ~URWLOCK_WRITE_WAITERS);
3066                                 if (rv == -1) {
3067                                         error = EFAULT;
3068                                         break;
3069                                 }
3070                                 if (rv == 0) {
3071                                         MPASS(oldstate == state);
3072                                         break;
3073                                 }
3074                                 state = oldstate;
3075                                 error1 = umtxq_check_susp(td, false);
3076                                 /*
3077                                  * We are leaving the URWLOCK_WRITE_WAITERS
3078                                  * behind, but this should not harm the
3079                                  * correctness.
3080                                  */
3081                                 if (error1 != 0) {
3082                                         if (error == 0)
3083                                                 error = error1;
3084                                         break;
3085                                 }
3086                         }
3087                         rv = fueword32(&rwlock->rw_blocked_readers,
3088                             &blocked_readers);
3089                         if (rv == -1) {
3090                                 umtxq_unbusy_unlocked(&uq->uq_key);
3091                                 error = EFAULT;
3092                                 break;
3093                         }
3094                 } else
3095                         blocked_readers = 0;
3096
3097                 umtxq_unbusy_unlocked(&uq->uq_key);
3098         }
3099
3100         umtx_key_release(&uq->uq_key);
3101         if (error == ERESTART)
3102                 error = EINTR;
3103         return (error);
3104 }
3105
3106 static int
3107 do_rw_unlock(struct thread *td, struct urwlock *rwlock)
3108 {
3109         struct umtx_q *uq;
3110         uint32_t flags;
3111         int32_t state, oldstate;
3112         int error, rv, q, count;
3113
3114         uq = td->td_umtxq;
3115         error = fueword32(&rwlock->rw_flags, &flags);
3116         if (error == -1)
3117                 return (EFAULT);
3118         error = umtx_key_get(rwlock, TYPE_RWLOCK, GET_SHARE(flags), &uq->uq_key);
3119         if (error != 0)
3120                 return (error);
3121
3122         error = fueword32(&rwlock->rw_state, &state);
3123         if (error == -1) {
3124                 error = EFAULT;
3125                 goto out;
3126         }
3127         if (state & URWLOCK_WRITE_OWNER) {
3128                 for (;;) {
3129                         rv = casueword32(&rwlock->rw_state, state, 
3130                             &oldstate, state & ~URWLOCK_WRITE_OWNER);
3131                         if (rv == -1) {
3132                                 error = EFAULT;
3133                                 goto out;
3134                         }
3135                         if (rv == 1) {
3136                                 state = oldstate;
3137                                 if (!(oldstate & URWLOCK_WRITE_OWNER)) {
3138                                         error = EPERM;
3139                                         goto out;
3140                                 }
3141                                 error = umtxq_check_susp(td, true);
3142                                 if (error != 0)
3143                                         goto out;
3144                         } else
3145                                 break;
3146                 }
3147         } else if (URWLOCK_READER_COUNT(state) != 0) {
3148                 for (;;) {
3149                         rv = casueword32(&rwlock->rw_state, state,
3150                             &oldstate, state - 1);
3151                         if (rv == -1) {
3152                                 error = EFAULT;
3153                                 goto out;
3154                         }
3155                         if (rv == 1) {
3156                                 state = oldstate;
3157                                 if (URWLOCK_READER_COUNT(oldstate) == 0) {
3158                                         error = EPERM;
3159                                         goto out;
3160                                 }
3161                                 error = umtxq_check_susp(td, true);
3162                                 if (error != 0)
3163                                         goto out;
3164                         } else
3165                                 break;
3166                 }
3167         } else {
3168                 error = EPERM;
3169                 goto out;
3170         }
3171
3172         count = 0;
3173
3174         if (!(flags & URWLOCK_PREFER_READER)) {
3175                 if (state & URWLOCK_WRITE_WAITERS) {
3176                         count = 1;
3177                         q = UMTX_EXCLUSIVE_QUEUE;
3178                 } else if (state & URWLOCK_READ_WAITERS) {
3179                         count = INT_MAX;
3180                         q = UMTX_SHARED_QUEUE;
3181                 }
3182         } else {
3183                 if (state & URWLOCK_READ_WAITERS) {
3184                         count = INT_MAX;
3185                         q = UMTX_SHARED_QUEUE;
3186                 } else if (state & URWLOCK_WRITE_WAITERS) {
3187                         count = 1;
3188                         q = UMTX_EXCLUSIVE_QUEUE;
3189                 }
3190         }
3191
3192         if (count) {
3193                 umtxq_lock(&uq->uq_key);
3194                 umtxq_busy(&uq->uq_key);
3195                 umtxq_signal_queue(&uq->uq_key, count, q);
3196                 umtxq_unbusy(&uq->uq_key);
3197                 umtxq_unlock(&uq->uq_key);
3198         }
3199 out:
3200         umtx_key_release(&uq->uq_key);
3201         return (error);
3202 }
3203
3204 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3205 static int
3206 do_sem_wait(struct thread *td, struct _usem *sem, struct _umtx_time *timeout)
3207 {
3208         struct abs_timeout timo;
3209         struct umtx_q *uq;
3210         uint32_t flags, count, count1;
3211         int error, rv, rv1;
3212
3213         uq = td->td_umtxq;
3214         error = fueword32(&sem->_flags, &flags);
3215         if (error == -1)
3216                 return (EFAULT);
3217         error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3218         if (error != 0)
3219                 return (error);
3220
3221         if (timeout != NULL)
3222                 abs_timeout_init2(&timo, timeout);
3223
3224 again:
3225         umtxq_lock(&uq->uq_key);
3226         umtxq_busy(&uq->uq_key);
3227         umtxq_insert(uq);
3228         umtxq_unlock(&uq->uq_key);
3229         rv = casueword32(&sem->_has_waiters, 0, &count1, 1);
3230         if (rv == 0)
3231                 rv1 = fueword32(&sem->_count, &count);
3232         if (rv == -1 || (rv == 0 && (rv1 == -1 || count != 0)) || rv == 1) {
3233                 umtxq_lock(&uq->uq_key);
3234                 umtxq_unbusy(&uq->uq_key);
3235                 umtxq_remove(uq);
3236                 umtxq_unlock(&uq->uq_key);
3237                 if (rv == 1) {
3238                         rv = umtxq_check_susp(td, true);
3239                         if (rv == 0)
3240                                 goto again;
3241                         error = rv;
3242                         goto out;
3243                 }
3244                 if (rv == 0)
3245                         rv = rv1;
3246                 error = rv == -1 ? EFAULT : 0;
3247                 goto out;
3248         }
3249         umtxq_lock(&uq->uq_key);
3250         umtxq_unbusy(&uq->uq_key);
3251
3252         error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3253
3254         if ((uq->uq_flags & UQF_UMTXQ) == 0)
3255                 error = 0;
3256         else {
3257                 umtxq_remove(uq);
3258                 /* A relative timeout cannot be restarted. */
3259                 if (error == ERESTART && timeout != NULL &&
3260                     (timeout->_flags & UMTX_ABSTIME) == 0)
3261                         error = EINTR;
3262         }
3263         umtxq_unlock(&uq->uq_key);
3264 out:
3265         umtx_key_release(&uq->uq_key);
3266         return (error);
3267 }
3268
3269 /*
3270  * Signal a userland semaphore.
3271  */
3272 static int
3273 do_sem_wake(struct thread *td, struct _usem *sem)
3274 {
3275         struct umtx_key key;
3276         int error, cnt;
3277         uint32_t flags;
3278
3279         error = fueword32(&sem->_flags, &flags);
3280         if (error == -1)
3281                 return (EFAULT);
3282         if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3283                 return (error); 
3284         umtxq_lock(&key);
3285         umtxq_busy(&key);
3286         cnt = umtxq_count(&key);
3287         if (cnt > 0) {
3288                 /*
3289                  * Check if count is greater than 0, this means the memory is
3290                  * still being referenced by user code, so we can safely
3291                  * update _has_waiters flag.
3292                  */
3293                 if (cnt == 1) {
3294                         umtxq_unlock(&key);
3295                         error = suword32(&sem->_has_waiters, 0);
3296                         umtxq_lock(&key);
3297                         if (error == -1)
3298                                 error = EFAULT;
3299                 }
3300                 umtxq_signal(&key, 1);
3301         }
3302         umtxq_unbusy(&key);
3303         umtxq_unlock(&key);
3304         umtx_key_release(&key);
3305         return (error);
3306 }
3307 #endif
3308
3309 static int
3310 do_sem2_wait(struct thread *td, struct _usem2 *sem, struct _umtx_time *timeout)
3311 {
3312         struct abs_timeout timo;
3313         struct umtx_q *uq;
3314         uint32_t count, flags;
3315         int error, rv;
3316
3317         uq = td->td_umtxq;
3318         flags = fuword32(&sem->_flags);
3319         error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &uq->uq_key);
3320         if (error != 0)
3321                 return (error);
3322
3323         if (timeout != NULL)
3324                 abs_timeout_init2(&timo, timeout);
3325
3326 again:
3327         umtxq_lock(&uq->uq_key);
3328         umtxq_busy(&uq->uq_key);
3329         umtxq_insert(uq);
3330         umtxq_unlock(&uq->uq_key);
3331         rv = fueword32(&sem->_count, &count);
3332         if (rv == -1) {
3333                 umtxq_lock(&uq->uq_key);
3334                 umtxq_unbusy(&uq->uq_key);
3335                 umtxq_remove(uq);
3336                 umtxq_unlock(&uq->uq_key);
3337                 umtx_key_release(&uq->uq_key);
3338                 return (EFAULT);
3339         }
3340         for (;;) {
3341                 if (USEM_COUNT(count) != 0) {
3342                         umtxq_lock(&uq->uq_key);
3343                         umtxq_unbusy(&uq->uq_key);
3344                         umtxq_remove(uq);
3345                         umtxq_unlock(&uq->uq_key);
3346                         umtx_key_release(&uq->uq_key);
3347                         return (0);
3348                 }
3349                 if (count == USEM_HAS_WAITERS)
3350                         break;
3351                 rv = casueword32(&sem->_count, 0, &count, USEM_HAS_WAITERS);
3352                 if (rv == 0)
3353                         break;
3354                 umtxq_lock(&uq->uq_key);
3355                 umtxq_unbusy(&uq->uq_key);
3356                 umtxq_remove(uq);
3357                 umtxq_unlock(&uq->uq_key);
3358                 umtx_key_release(&uq->uq_key);
3359                 if (rv == -1)
3360                         return (EFAULT);
3361                 rv = umtxq_check_susp(td, true);
3362                 if (rv != 0)
3363                         return (rv);
3364                 goto again;
3365         }
3366         umtxq_lock(&uq->uq_key);
3367         umtxq_unbusy(&uq->uq_key);
3368
3369         error = umtxq_sleep(uq, "usem", timeout == NULL ? NULL : &timo);
3370
3371         if ((uq->uq_flags & UQF_UMTXQ) == 0)
3372                 error = 0;
3373         else {
3374                 umtxq_remove(uq);
3375                 if (timeout != NULL && (timeout->_flags & UMTX_ABSTIME) == 0) {
3376                         /* A relative timeout cannot be restarted. */
3377                         if (error == ERESTART)
3378                                 error = EINTR;
3379                         if (error == EINTR) {
3380                                 abs_timeout_update(&timo);
3381                                 timespecsub(&timo.end, &timo.cur,
3382                                     &timeout->_timeout);
3383                         }
3384                 }
3385         }
3386         umtxq_unlock(&uq->uq_key);
3387         umtx_key_release(&uq->uq_key);
3388         return (error);
3389 }
3390
3391 /*
3392  * Signal a userland semaphore.
3393  */
3394 static int
3395 do_sem2_wake(struct thread *td, struct _usem2 *sem)
3396 {
3397         struct umtx_key key;
3398         int error, cnt, rv;
3399         uint32_t count, flags;
3400
3401         rv = fueword32(&sem->_flags, &flags);
3402         if (rv == -1)
3403                 return (EFAULT);
3404         if ((error = umtx_key_get(sem, TYPE_SEM, GET_SHARE(flags), &key)) != 0)
3405                 return (error); 
3406         umtxq_lock(&key);
3407         umtxq_busy(&key);
3408         cnt = umtxq_count(&key);
3409         if (cnt > 0) {
3410                 /*
3411                  * If this was the last sleeping thread, clear the waiters
3412                  * flag in _count.
3413                  */
3414                 if (cnt == 1) {
3415                         umtxq_unlock(&key);
3416                         rv = fueword32(&sem->_count, &count);
3417                         while (rv != -1 && count & USEM_HAS_WAITERS) {
3418                                 rv = casueword32(&sem->_count, count, &count,
3419                                     count & ~USEM_HAS_WAITERS);
3420                                 if (rv == 1) {
3421                                         rv = umtxq_check_susp(td, true);
3422                                         if (rv != 0)
3423                                                 break;
3424                                 }
3425                         }
3426                         if (rv == -1)
3427                                 error = EFAULT;
3428                         else if (rv > 0) {
3429                                 error = rv;
3430                         }
3431                         umtxq_lock(&key);
3432                 }
3433
3434                 umtxq_signal(&key, 1);
3435         }
3436         umtxq_unbusy(&key);
3437         umtxq_unlock(&key);
3438         umtx_key_release(&key);
3439         return (error);
3440 }
3441
3442 inline int
3443 umtx_copyin_timeout(const void *addr, struct timespec *tsp)
3444 {
3445         int error;
3446
3447         error = copyin(addr, tsp, sizeof(struct timespec));
3448         if (error == 0) {
3449                 if (tsp->tv_sec < 0 ||
3450                     tsp->tv_nsec >= 1000000000 ||
3451                     tsp->tv_nsec < 0)
3452                         error = EINVAL;
3453         }
3454         return (error);
3455 }
3456
3457 static inline int
3458 umtx_copyin_umtx_time(const void *addr, size_t size, struct _umtx_time *tp)
3459 {
3460         int error;
3461         
3462         if (size <= sizeof(struct timespec)) {
3463                 tp->_clockid = CLOCK_REALTIME;
3464                 tp->_flags = 0;
3465                 error = copyin(addr, &tp->_timeout, sizeof(struct timespec));
3466         } else 
3467                 error = copyin(addr, tp, sizeof(struct _umtx_time));
3468         if (error != 0)
3469                 return (error);
3470         if (tp->_timeout.tv_sec < 0 ||
3471             tp->_timeout.tv_nsec >= 1000000000 || tp->_timeout.tv_nsec < 0)
3472                 return (EINVAL);
3473         return (0);
3474 }
3475
3476 static int
3477 __umtx_op_unimpl(struct thread *td, struct _umtx_op_args *uap)
3478 {
3479
3480         return (EOPNOTSUPP);
3481 }
3482
3483 static int
3484 __umtx_op_wait(struct thread *td, struct _umtx_op_args *uap)
3485 {
3486         struct _umtx_time timeout, *tm_p;
3487         int error;
3488
3489         if (uap->uaddr2 == NULL)
3490                 tm_p = NULL;
3491         else {
3492                 error = umtx_copyin_umtx_time(
3493                     uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3494                 if (error != 0)
3495                         return (error);
3496                 tm_p = &timeout;
3497         }
3498         return (do_wait(td, uap->obj, uap->val, tm_p, 0, 0));
3499 }
3500
3501 static int
3502 __umtx_op_wait_uint(struct thread *td, struct _umtx_op_args *uap)
3503 {
3504         struct _umtx_time timeout, *tm_p;
3505         int error;
3506
3507         if (uap->uaddr2 == NULL)
3508                 tm_p = NULL;
3509         else {
3510                 error = umtx_copyin_umtx_time(
3511                     uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3512                 if (error != 0)
3513                         return (error);
3514                 tm_p = &timeout;
3515         }
3516         return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
3517 }
3518
3519 static int
3520 __umtx_op_wait_uint_private(struct thread *td, struct _umtx_op_args *uap)
3521 {
3522         struct _umtx_time *tm_p, timeout;
3523         int error;
3524
3525         if (uap->uaddr2 == NULL)
3526                 tm_p = NULL;
3527         else {
3528                 error = umtx_copyin_umtx_time(
3529                     uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3530                 if (error != 0)
3531                         return (error);
3532                 tm_p = &timeout;
3533         }
3534         return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
3535 }
3536
3537 static int
3538 __umtx_op_wake(struct thread *td, struct _umtx_op_args *uap)
3539 {
3540
3541         return (kern_umtx_wake(td, uap->obj, uap->val, 0));
3542 }
3543
3544 #define BATCH_SIZE      128
3545 static int
3546 __umtx_op_nwake_private(struct thread *td, struct _umtx_op_args *uap)
3547 {
3548         char *uaddrs[BATCH_SIZE], **upp;
3549         int count, error, i, pos, tocopy;
3550
3551         upp = (char **)uap->obj;
3552         error = 0;
3553         for (count = uap->val, pos = 0; count > 0; count -= tocopy,
3554             pos += tocopy) {
3555                 tocopy = MIN(count, BATCH_SIZE);
3556                 error = copyin(upp + pos, uaddrs, tocopy * sizeof(char *));
3557                 if (error != 0)
3558                         break;
3559                 for (i = 0; i < tocopy; ++i)
3560                         kern_umtx_wake(td, uaddrs[i], INT_MAX, 1);
3561                 maybe_yield();
3562         }
3563         return (error);
3564 }
3565
3566 static int
3567 __umtx_op_wake_private(struct thread *td, struct _umtx_op_args *uap)
3568 {
3569
3570         return (kern_umtx_wake(td, uap->obj, uap->val, 1));
3571 }
3572
3573 static int
3574 __umtx_op_lock_umutex(struct thread *td, struct _umtx_op_args *uap)
3575 {
3576         struct _umtx_time *tm_p, timeout;
3577         int error;
3578
3579         /* Allow a null timespec (wait forever). */
3580         if (uap->uaddr2 == NULL)
3581                 tm_p = NULL;
3582         else {
3583                 error = umtx_copyin_umtx_time(
3584                     uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3585                 if (error != 0)
3586                         return (error);
3587                 tm_p = &timeout;
3588         }
3589         return (do_lock_umutex(td, uap->obj, tm_p, 0));
3590 }
3591
3592 static int
3593 __umtx_op_trylock_umutex(struct thread *td, struct _umtx_op_args *uap)
3594 {
3595
3596         return (do_lock_umutex(td, uap->obj, NULL, _UMUTEX_TRY));
3597 }
3598
3599 static int
3600 __umtx_op_wait_umutex(struct thread *td, struct _umtx_op_args *uap)
3601 {
3602         struct _umtx_time *tm_p, timeout;
3603         int error;
3604
3605         /* Allow a null timespec (wait forever). */
3606         if (uap->uaddr2 == NULL)
3607                 tm_p = NULL;
3608         else {
3609                 error = umtx_copyin_umtx_time(
3610                     uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3611                 if (error != 0)
3612                         return (error);
3613                 tm_p = &timeout;
3614         }
3615         return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
3616 }
3617
3618 static int
3619 __umtx_op_wake_umutex(struct thread *td, struct _umtx_op_args *uap)
3620 {
3621
3622         return (do_wake_umutex(td, uap->obj));
3623 }
3624
3625 static int
3626 __umtx_op_unlock_umutex(struct thread *td, struct _umtx_op_args *uap)
3627 {
3628
3629         return (do_unlock_umutex(td, uap->obj, false));
3630 }
3631
3632 static int
3633 __umtx_op_set_ceiling(struct thread *td, struct _umtx_op_args *uap)
3634 {
3635
3636         return (do_set_ceiling(td, uap->obj, uap->val, uap->uaddr1));
3637 }
3638
3639 static int
3640 __umtx_op_cv_wait(struct thread *td, struct _umtx_op_args *uap)
3641 {
3642         struct timespec *ts, timeout;
3643         int error;
3644
3645         /* Allow a null timespec (wait forever). */
3646         if (uap->uaddr2 == NULL)
3647                 ts = NULL;
3648         else {
3649                 error = umtx_copyin_timeout(uap->uaddr2, &timeout);
3650                 if (error != 0)
3651                         return (error);
3652                 ts = &timeout;
3653         }
3654         return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
3655 }
3656
3657 static int
3658 __umtx_op_cv_signal(struct thread *td, struct _umtx_op_args *uap)
3659 {
3660
3661         return (do_cv_signal(td, uap->obj));
3662 }
3663
3664 static int
3665 __umtx_op_cv_broadcast(struct thread *td, struct _umtx_op_args *uap)
3666 {
3667
3668         return (do_cv_broadcast(td, uap->obj));
3669 }
3670
3671 static int
3672 __umtx_op_rw_rdlock(struct thread *td, struct _umtx_op_args *uap)
3673 {
3674         struct _umtx_time timeout;
3675         int error;
3676
3677         /* Allow a null timespec (wait forever). */
3678         if (uap->uaddr2 == NULL) {
3679                 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
3680         } else {
3681                 error = umtx_copyin_umtx_time(uap->uaddr2,
3682                    (size_t)uap->uaddr1, &timeout);
3683                 if (error != 0)
3684                         return (error);
3685                 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
3686         }
3687         return (error);
3688 }
3689
3690 static int
3691 __umtx_op_rw_wrlock(struct thread *td, struct _umtx_op_args *uap)
3692 {
3693         struct _umtx_time timeout;
3694         int error;
3695
3696         /* Allow a null timespec (wait forever). */
3697         if (uap->uaddr2 == NULL) {
3698                 error = do_rw_wrlock(td, uap->obj, 0);
3699         } else {
3700                 error = umtx_copyin_umtx_time(uap->uaddr2, 
3701                    (size_t)uap->uaddr1, &timeout);
3702                 if (error != 0)
3703                         return (error);
3704
3705                 error = do_rw_wrlock(td, uap->obj, &timeout);
3706         }
3707         return (error);
3708 }
3709
3710 static int
3711 __umtx_op_rw_unlock(struct thread *td, struct _umtx_op_args *uap)
3712 {
3713
3714         return (do_rw_unlock(td, uap->obj));
3715 }
3716
3717 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
3718 static int
3719 __umtx_op_sem_wait(struct thread *td, struct _umtx_op_args *uap)
3720 {
3721         struct _umtx_time *tm_p, timeout;
3722         int error;
3723
3724         /* Allow a null timespec (wait forever). */
3725         if (uap->uaddr2 == NULL)
3726                 tm_p = NULL;
3727         else {
3728                 error = umtx_copyin_umtx_time(
3729                     uap->uaddr2, (size_t)uap->uaddr1, &timeout);
3730                 if (error != 0)
3731                         return (error);
3732                 tm_p = &timeout;
3733         }
3734         return (do_sem_wait(td, uap->obj, tm_p));
3735 }
3736
3737 static int
3738 __umtx_op_sem_wake(struct thread *td, struct _umtx_op_args *uap)
3739 {
3740
3741         return (do_sem_wake(td, uap->obj));
3742 }
3743 #endif
3744
3745 static int
3746 __umtx_op_wake2_umutex(struct thread *td, struct _umtx_op_args *uap)
3747 {
3748
3749         return (do_wake2_umutex(td, uap->obj, uap->val));
3750 }
3751
3752 static int
3753 __umtx_op_sem2_wait(struct thread *td, struct _umtx_op_args *uap)
3754 {
3755         struct _umtx_time *tm_p, timeout;
3756         size_t uasize;
3757         int error;
3758
3759         /* Allow a null timespec (wait forever). */
3760         if (uap->uaddr2 == NULL) {
3761                 uasize = 0;
3762                 tm_p = NULL;
3763         } else {
3764                 uasize = (size_t)uap->uaddr1;
3765                 error = umtx_copyin_umtx_time(uap->uaddr2, uasize, &timeout);
3766                 if (error != 0)
3767                         return (error);
3768                 tm_p = &timeout;
3769         }
3770         error = do_sem2_wait(td, uap->obj, tm_p);
3771         if (error == EINTR && uap->uaddr2 != NULL &&
3772             (timeout._flags & UMTX_ABSTIME) == 0 &&
3773             uasize >= sizeof(struct _umtx_time) + sizeof(struct timespec)) {
3774                 error = copyout(&timeout._timeout,
3775                     (struct _umtx_time *)uap->uaddr2 + 1,
3776                     sizeof(struct timespec));
3777                 if (error == 0) {
3778                         error = EINTR;
3779                 }
3780         }
3781
3782         return (error);
3783 }
3784
3785 static int
3786 __umtx_op_sem2_wake(struct thread *td, struct _umtx_op_args *uap)
3787 {
3788
3789         return (do_sem2_wake(td, uap->obj));
3790 }
3791
3792 #define USHM_OBJ_UMTX(o)                                                \
3793     ((struct umtx_shm_obj_list *)(&(o)->umtx_data))
3794
3795 #define USHMF_REG_LINKED        0x0001
3796 #define USHMF_OBJ_LINKED        0x0002
3797 struct umtx_shm_reg {
3798         TAILQ_ENTRY(umtx_shm_reg) ushm_reg_link;
3799         LIST_ENTRY(umtx_shm_reg) ushm_obj_link;
3800         struct umtx_key         ushm_key;
3801         struct ucred            *ushm_cred;
3802         struct shmfd            *ushm_obj;
3803         u_int                   ushm_refcnt;
3804         u_int                   ushm_flags;
3805 };
3806
3807 LIST_HEAD(umtx_shm_obj_list, umtx_shm_reg);
3808 TAILQ_HEAD(umtx_shm_reg_head, umtx_shm_reg);
3809
3810 static uma_zone_t umtx_shm_reg_zone;
3811 static struct umtx_shm_reg_head umtx_shm_registry[UMTX_CHAINS];
3812 static struct mtx umtx_shm_lock;
3813 static struct umtx_shm_reg_head umtx_shm_reg_delfree =
3814     TAILQ_HEAD_INITIALIZER(umtx_shm_reg_delfree);
3815
3816 static void umtx_shm_free_reg(struct umtx_shm_reg *reg);
3817
3818 static void
3819 umtx_shm_reg_delfree_tq(void *context __unused, int pending __unused)
3820 {
3821         struct umtx_shm_reg_head d;
3822         struct umtx_shm_reg *reg, *reg1;
3823
3824         TAILQ_INIT(&d);
3825         mtx_lock(&umtx_shm_lock);
3826         TAILQ_CONCAT(&d, &umtx_shm_reg_delfree, ushm_reg_link);
3827         mtx_unlock(&umtx_shm_lock);
3828         TAILQ_FOREACH_SAFE(reg, &d, ushm_reg_link, reg1) {
3829                 TAILQ_REMOVE(&d, reg, ushm_reg_link);
3830                 umtx_shm_free_reg(reg);
3831         }
3832 }
3833
3834 static struct task umtx_shm_reg_delfree_task =
3835     TASK_INITIALIZER(0, umtx_shm_reg_delfree_tq, NULL);
3836
3837 static struct umtx_shm_reg *
3838 umtx_shm_find_reg_locked(const struct umtx_key *key)
3839 {
3840         struct umtx_shm_reg *reg;
3841         struct umtx_shm_reg_head *reg_head;
3842
3843         KASSERT(key->shared, ("umtx_p_find_rg: private key"));
3844         mtx_assert(&umtx_shm_lock, MA_OWNED);
3845         reg_head = &umtx_shm_registry[key->hash];
3846         TAILQ_FOREACH(reg, reg_head, ushm_reg_link) {
3847                 KASSERT(reg->ushm_key.shared,
3848                     ("non-shared key on reg %p %d", reg, reg->ushm_key.shared));
3849                 if (reg->ushm_key.info.shared.object ==
3850                     key->info.shared.object &&
3851                     reg->ushm_key.info.shared.offset ==
3852                     key->info.shared.offset) {
3853                         KASSERT(reg->ushm_key.type == TYPE_SHM, ("TYPE_USHM"));
3854                         KASSERT(reg->ushm_refcnt > 0,
3855                             ("reg %p refcnt 0 onlist", reg));
3856                         KASSERT((reg->ushm_flags & USHMF_REG_LINKED) != 0,
3857                             ("reg %p not linked", reg));
3858                         reg->ushm_refcnt++;
3859                         return (reg);
3860                 }
3861         }
3862         return (NULL);
3863 }
3864
3865 static struct umtx_shm_reg *
3866 umtx_shm_find_reg(const struct umtx_key *key)
3867 {
3868         struct umtx_shm_reg *reg;
3869
3870         mtx_lock(&umtx_shm_lock);
3871         reg = umtx_shm_find_reg_locked(key);
3872         mtx_unlock(&umtx_shm_lock);
3873         return (reg);
3874 }
3875
3876 static void
3877 umtx_shm_free_reg(struct umtx_shm_reg *reg)
3878 {
3879
3880         chgumtxcnt(reg->ushm_cred->cr_ruidinfo, -1, 0);
3881         crfree(reg->ushm_cred);
3882         shm_drop(reg->ushm_obj);
3883         uma_zfree(umtx_shm_reg_zone, reg);
3884 }
3885
3886 static bool
3887 umtx_shm_unref_reg_locked(struct umtx_shm_reg *reg, bool force)
3888 {
3889         bool res;
3890
3891         mtx_assert(&umtx_shm_lock, MA_OWNED);
3892         KASSERT(reg->ushm_refcnt > 0, ("ushm_reg %p refcnt 0", reg));
3893         reg->ushm_refcnt--;
3894         res = reg->ushm_refcnt == 0;
3895         if (res || force) {
3896                 if ((reg->ushm_flags & USHMF_REG_LINKED) != 0) {
3897                         TAILQ_REMOVE(&umtx_shm_registry[reg->ushm_key.hash],
3898                             reg, ushm_reg_link);
3899                         reg->ushm_flags &= ~USHMF_REG_LINKED;
3900                 }
3901                 if ((reg->ushm_flags & USHMF_OBJ_LINKED) != 0) {
3902                         LIST_REMOVE(reg, ushm_obj_link);
3903                         reg->ushm_flags &= ~USHMF_OBJ_LINKED;
3904                 }
3905         }
3906         return (res);
3907 }
3908
3909 static void
3910 umtx_shm_unref_reg(struct umtx_shm_reg *reg, bool force)
3911 {
3912         vm_object_t object;
3913         bool dofree;
3914
3915         if (force) {
3916                 object = reg->ushm_obj->shm_object;
3917                 VM_OBJECT_WLOCK(object);
3918                 object->flags |= OBJ_UMTXDEAD;
3919                 VM_OBJECT_WUNLOCK(object);
3920         }
3921         mtx_lock(&umtx_shm_lock);
3922         dofree = umtx_shm_unref_reg_locked(reg, force);
3923         mtx_unlock(&umtx_shm_lock);
3924         if (dofree)
3925                 umtx_shm_free_reg(reg);
3926 }
3927
3928 void
3929 umtx_shm_object_init(vm_object_t object)
3930 {
3931
3932         LIST_INIT(USHM_OBJ_UMTX(object));
3933 }
3934
3935 void
3936 umtx_shm_object_terminated(vm_object_t object)
3937 {
3938         struct umtx_shm_reg *reg, *reg1;
3939         bool dofree;
3940
3941         if (LIST_EMPTY(USHM_OBJ_UMTX(object)))
3942                 return;
3943
3944         dofree = false;
3945         mtx_lock(&umtx_shm_lock);
3946         LIST_FOREACH_SAFE(reg, USHM_OBJ_UMTX(object), ushm_obj_link, reg1) {
3947                 if (umtx_shm_unref_reg_locked(reg, true)) {
3948                         TAILQ_INSERT_TAIL(&umtx_shm_reg_delfree, reg,
3949                             ushm_reg_link);
3950                         dofree = true;
3951                 }
3952         }
3953         mtx_unlock(&umtx_shm_lock);
3954         if (dofree)
3955                 taskqueue_enqueue(taskqueue_thread, &umtx_shm_reg_delfree_task);
3956 }
3957
3958 static int
3959 umtx_shm_create_reg(struct thread *td, const struct umtx_key *key,
3960     struct umtx_shm_reg **res)
3961 {
3962         struct umtx_shm_reg *reg, *reg1;
3963         struct ucred *cred;
3964         int error;
3965
3966         reg = umtx_shm_find_reg(key);
3967         if (reg != NULL) {
3968                 *res = reg;
3969                 return (0);
3970         }
3971         cred = td->td_ucred;
3972         if (!chgumtxcnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_UMTXP)))
3973                 return (ENOMEM);
3974         reg = uma_zalloc(umtx_shm_reg_zone, M_WAITOK | M_ZERO);
3975         reg->ushm_refcnt = 1;
3976         bcopy(key, &reg->ushm_key, sizeof(*key));
3977         reg->ushm_obj = shm_alloc(td->td_ucred, O_RDWR);
3978         reg->ushm_cred = crhold(cred);
3979         error = shm_dotruncate(reg->ushm_obj, PAGE_SIZE);
3980         if (error != 0) {
3981                 umtx_shm_free_reg(reg);
3982                 return (error);
3983         }
3984         mtx_lock(&umtx_shm_lock);
3985         reg1 = umtx_shm_find_reg_locked(key);
3986         if (reg1 != NULL) {
3987                 mtx_unlock(&umtx_shm_lock);
3988                 umtx_shm_free_reg(reg);
3989                 *res = reg1;
3990                 return (0);
3991         }
3992         reg->ushm_refcnt++;
3993         TAILQ_INSERT_TAIL(&umtx_shm_registry[key->hash], reg, ushm_reg_link);
3994         LIST_INSERT_HEAD(USHM_OBJ_UMTX(key->info.shared.object), reg,
3995             ushm_obj_link);
3996         reg->ushm_flags = USHMF_REG_LINKED | USHMF_OBJ_LINKED;
3997         mtx_unlock(&umtx_shm_lock);
3998         *res = reg;
3999         return (0);
4000 }
4001
4002 static int
4003 umtx_shm_alive(struct thread *td, void *addr)
4004 {
4005         vm_map_t map;
4006         vm_map_entry_t entry;
4007         vm_object_t object;
4008         vm_pindex_t pindex;
4009         vm_prot_t prot;
4010         int res, ret;
4011         boolean_t wired;
4012
4013         map = &td->td_proc->p_vmspace->vm_map;
4014         res = vm_map_lookup(&map, (uintptr_t)addr, VM_PROT_READ, &entry,
4015             &object, &pindex, &prot, &wired);
4016         if (res != KERN_SUCCESS)
4017                 return (EFAULT);
4018         if (object == NULL)
4019                 ret = EINVAL;
4020         else
4021                 ret = (object->flags & OBJ_UMTXDEAD) != 0 ? ENOTTY : 0;
4022         vm_map_lookup_done(map, entry);
4023         return (ret);
4024 }
4025
4026 static void
4027 umtx_shm_init(void)
4028 {
4029         int i;
4030
4031         umtx_shm_reg_zone = uma_zcreate("umtx_shm", sizeof(struct umtx_shm_reg),
4032             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
4033         mtx_init(&umtx_shm_lock, "umtxshm", NULL, MTX_DEF);
4034         for (i = 0; i < nitems(umtx_shm_registry); i++)
4035                 TAILQ_INIT(&umtx_shm_registry[i]);
4036 }
4037
4038 static int
4039 umtx_shm(struct thread *td, void *addr, u_int flags)
4040 {
4041         struct umtx_key key;
4042         struct umtx_shm_reg *reg;
4043         struct file *fp;
4044         int error, fd;
4045
4046         if (__bitcount(flags & (UMTX_SHM_CREAT | UMTX_SHM_LOOKUP |
4047             UMTX_SHM_DESTROY| UMTX_SHM_ALIVE)) != 1)
4048                 return (EINVAL);
4049         if ((flags & UMTX_SHM_ALIVE) != 0)
4050                 return (umtx_shm_alive(td, addr));
4051         error = umtx_key_get(addr, TYPE_SHM, PROCESS_SHARE, &key);
4052         if (error != 0)
4053                 return (error);
4054         KASSERT(key.shared == 1, ("non-shared key"));
4055         if ((flags & UMTX_SHM_CREAT) != 0) {
4056                 error = umtx_shm_create_reg(td, &key, &reg);
4057         } else {
4058                 reg = umtx_shm_find_reg(&key);
4059                 if (reg == NULL)
4060                         error = ESRCH;
4061         }
4062         umtx_key_release(&key);
4063         if (error != 0)
4064                 return (error);
4065         KASSERT(reg != NULL, ("no reg"));
4066         if ((flags & UMTX_SHM_DESTROY) != 0) {
4067                 umtx_shm_unref_reg(reg, true);
4068         } else {
4069 #if 0
4070 #ifdef MAC
4071                 error = mac_posixshm_check_open(td->td_ucred,
4072                     reg->ushm_obj, FFLAGS(O_RDWR));
4073                 if (error == 0)
4074 #endif
4075                         error = shm_access(reg->ushm_obj, td->td_ucred,
4076                             FFLAGS(O_RDWR));
4077                 if (error == 0)
4078 #endif
4079                         error = falloc_caps(td, &fp, &fd, O_CLOEXEC, NULL);
4080                 if (error == 0) {
4081                         shm_hold(reg->ushm_obj);
4082                         finit(fp, FFLAGS(O_RDWR), DTYPE_SHM, reg->ushm_obj,
4083                             &shm_ops);
4084                         td->td_retval[0] = fd;
4085                         fdrop(fp, td);
4086                 }
4087         }
4088         umtx_shm_unref_reg(reg, false);
4089         return (error);
4090 }
4091
4092 static int
4093 __umtx_op_shm(struct thread *td, struct _umtx_op_args *uap)
4094 {
4095
4096         return (umtx_shm(td, uap->uaddr1, uap->val));
4097 }
4098
4099 static int
4100 umtx_robust_lists(struct thread *td, struct umtx_robust_lists_params *rbp)
4101 {
4102
4103         td->td_rb_list = rbp->robust_list_offset;
4104         td->td_rbp_list = rbp->robust_priv_list_offset;
4105         td->td_rb_inact = rbp->robust_inact_offset;
4106         return (0);
4107 }
4108
4109 static int
4110 __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap)
4111 {
4112         struct umtx_robust_lists_params rb;
4113         int error;
4114
4115         if (uap->val > sizeof(rb))
4116                 return (EINVAL);
4117         bzero(&rb, sizeof(rb));
4118         error = copyin(uap->uaddr1, &rb, uap->val);
4119         if (error != 0)
4120                 return (error);
4121         return (umtx_robust_lists(td, &rb));
4122 }
4123
4124 typedef int (*_umtx_op_func)(struct thread *td, struct _umtx_op_args *uap);
4125
4126 static const _umtx_op_func op_table[] = {
4127         [UMTX_OP_RESERVED0]     = __umtx_op_unimpl,
4128         [UMTX_OP_RESERVED1]     = __umtx_op_unimpl,
4129         [UMTX_OP_WAIT]          = __umtx_op_wait,
4130         [UMTX_OP_WAKE]          = __umtx_op_wake,
4131         [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4132         [UMTX_OP_MUTEX_LOCK]    = __umtx_op_lock_umutex,
4133         [UMTX_OP_MUTEX_UNLOCK]  = __umtx_op_unlock_umutex,
4134         [UMTX_OP_SET_CEILING]   = __umtx_op_set_ceiling,
4135         [UMTX_OP_CV_WAIT]       = __umtx_op_cv_wait,
4136         [UMTX_OP_CV_SIGNAL]     = __umtx_op_cv_signal,
4137         [UMTX_OP_CV_BROADCAST]  = __umtx_op_cv_broadcast,
4138         [UMTX_OP_WAIT_UINT]     = __umtx_op_wait_uint,
4139         [UMTX_OP_RW_RDLOCK]     = __umtx_op_rw_rdlock,
4140         [UMTX_OP_RW_WRLOCK]     = __umtx_op_rw_wrlock,
4141         [UMTX_OP_RW_UNLOCK]     = __umtx_op_rw_unlock,
4142         [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private,
4143         [UMTX_OP_WAKE_PRIVATE]  = __umtx_op_wake_private,
4144         [UMTX_OP_MUTEX_WAIT]    = __umtx_op_wait_umutex,
4145         [UMTX_OP_MUTEX_WAKE]    = __umtx_op_wake_umutex,
4146 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4147         [UMTX_OP_SEM_WAIT]      = __umtx_op_sem_wait,
4148         [UMTX_OP_SEM_WAKE]      = __umtx_op_sem_wake,
4149 #else
4150         [UMTX_OP_SEM_WAIT]      = __umtx_op_unimpl,
4151         [UMTX_OP_SEM_WAKE]      = __umtx_op_unimpl,
4152 #endif
4153         [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private,
4154         [UMTX_OP_MUTEX_WAKE2]   = __umtx_op_wake2_umutex,
4155         [UMTX_OP_SEM2_WAIT]     = __umtx_op_sem2_wait,
4156         [UMTX_OP_SEM2_WAKE]     = __umtx_op_sem2_wake,
4157         [UMTX_OP_SHM]           = __umtx_op_shm,
4158         [UMTX_OP_ROBUST_LISTS]  = __umtx_op_robust_lists,
4159 };
4160
4161 int
4162 sys__umtx_op(struct thread *td, struct _umtx_op_args *uap)
4163 {
4164
4165         if ((unsigned)uap->op < nitems(op_table))
4166                 return (*op_table[uap->op])(td, uap);
4167         return (EINVAL);
4168 }
4169
4170 #ifdef COMPAT_FREEBSD32
4171
4172 struct timespec32 {
4173         int32_t tv_sec;
4174         int32_t tv_nsec;
4175 };
4176
4177 struct umtx_time32 {
4178         struct  timespec32      timeout;
4179         uint32_t                flags;
4180         uint32_t                clockid;
4181 };
4182
4183 static inline int
4184 umtx_copyin_timeout32(void *addr, struct timespec *tsp)
4185 {
4186         struct timespec32 ts32;
4187         int error;
4188
4189         error = copyin(addr, &ts32, sizeof(struct timespec32));
4190         if (error == 0) {
4191                 if (ts32.tv_sec < 0 ||
4192                     ts32.tv_nsec >= 1000000000 ||
4193                     ts32.tv_nsec < 0)
4194                         error = EINVAL;
4195                 else {
4196                         tsp->tv_sec = ts32.tv_sec;
4197                         tsp->tv_nsec = ts32.tv_nsec;
4198                 }
4199         }
4200         return (error);
4201 }
4202
4203 static inline int
4204 umtx_copyin_umtx_time32(const void *addr, size_t size, struct _umtx_time *tp)
4205 {
4206         struct umtx_time32 t32;
4207         int error;
4208         
4209         t32.clockid = CLOCK_REALTIME;
4210         t32.flags   = 0;
4211         if (size <= sizeof(struct timespec32))
4212                 error = copyin(addr, &t32.timeout, sizeof(struct timespec32));
4213         else 
4214                 error = copyin(addr, &t32, sizeof(struct umtx_time32));
4215         if (error != 0)
4216                 return (error);
4217         if (t32.timeout.tv_sec < 0 ||
4218             t32.timeout.tv_nsec >= 1000000000 || t32.timeout.tv_nsec < 0)
4219                 return (EINVAL);
4220         tp->_timeout.tv_sec = t32.timeout.tv_sec;
4221         tp->_timeout.tv_nsec = t32.timeout.tv_nsec;
4222         tp->_flags = t32.flags;
4223         tp->_clockid = t32.clockid;
4224         return (0);
4225 }
4226
4227 static int
4228 __umtx_op_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4229 {
4230         struct _umtx_time *tm_p, timeout;
4231         int error;
4232
4233         if (uap->uaddr2 == NULL)
4234                 tm_p = NULL;
4235         else {
4236                 error = umtx_copyin_umtx_time32(uap->uaddr2,
4237                         (size_t)uap->uaddr1, &timeout);
4238                 if (error != 0)
4239                         return (error);
4240                 tm_p = &timeout;
4241         }
4242         return (do_wait(td, uap->obj, uap->val, tm_p, 1, 0));
4243 }
4244
4245 static int
4246 __umtx_op_lock_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4247 {
4248         struct _umtx_time *tm_p, timeout;
4249         int error;
4250
4251         /* Allow a null timespec (wait forever). */
4252         if (uap->uaddr2 == NULL)
4253                 tm_p = NULL;
4254         else {
4255                 error = umtx_copyin_umtx_time32(uap->uaddr2,
4256                             (size_t)uap->uaddr1, &timeout);
4257                 if (error != 0)
4258                         return (error);
4259                 tm_p = &timeout;
4260         }
4261         return (do_lock_umutex(td, uap->obj, tm_p, 0));
4262 }
4263
4264 static int
4265 __umtx_op_wait_umutex_compat32(struct thread *td, struct _umtx_op_args *uap)
4266 {
4267         struct _umtx_time *tm_p, timeout;
4268         int error;
4269
4270         /* Allow a null timespec (wait forever). */
4271         if (uap->uaddr2 == NULL)
4272                 tm_p = NULL;
4273         else {
4274                 error = umtx_copyin_umtx_time32(uap->uaddr2, 
4275                     (size_t)uap->uaddr1, &timeout);
4276                 if (error != 0)
4277                         return (error);
4278                 tm_p = &timeout;
4279         }
4280         return (do_lock_umutex(td, uap->obj, tm_p, _UMUTEX_WAIT));
4281 }
4282
4283 static int
4284 __umtx_op_cv_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4285 {
4286         struct timespec *ts, timeout;
4287         int error;
4288
4289         /* Allow a null timespec (wait forever). */
4290         if (uap->uaddr2 == NULL)
4291                 ts = NULL;
4292         else {
4293                 error = umtx_copyin_timeout32(uap->uaddr2, &timeout);
4294                 if (error != 0)
4295                         return (error);
4296                 ts = &timeout;
4297         }
4298         return (do_cv_wait(td, uap->obj, uap->uaddr1, ts, uap->val));
4299 }
4300
4301 static int
4302 __umtx_op_rw_rdlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4303 {
4304         struct _umtx_time timeout;
4305         int error;
4306
4307         /* Allow a null timespec (wait forever). */
4308         if (uap->uaddr2 == NULL) {
4309                 error = do_rw_rdlock(td, uap->obj, uap->val, 0);
4310         } else {
4311                 error = umtx_copyin_umtx_time32(uap->uaddr2,
4312                     (size_t)uap->uaddr1, &timeout);
4313                 if (error != 0)
4314                         return (error);
4315                 error = do_rw_rdlock(td, uap->obj, uap->val, &timeout);
4316         }
4317         return (error);
4318 }
4319
4320 static int
4321 __umtx_op_rw_wrlock_compat32(struct thread *td, struct _umtx_op_args *uap)
4322 {
4323         struct _umtx_time timeout;
4324         int error;
4325
4326         /* Allow a null timespec (wait forever). */
4327         if (uap->uaddr2 == NULL) {
4328                 error = do_rw_wrlock(td, uap->obj, 0);
4329         } else {
4330                 error = umtx_copyin_umtx_time32(uap->uaddr2,
4331                     (size_t)uap->uaddr1, &timeout);
4332                 if (error != 0)
4333                         return (error);
4334                 error = do_rw_wrlock(td, uap->obj, &timeout);
4335         }
4336         return (error);
4337 }
4338
4339 static int
4340 __umtx_op_wait_uint_private_compat32(struct thread *td, struct _umtx_op_args *uap)
4341 {
4342         struct _umtx_time *tm_p, timeout;
4343         int error;
4344
4345         if (uap->uaddr2 == NULL)
4346                 tm_p = NULL;
4347         else {
4348                 error = umtx_copyin_umtx_time32(
4349                     uap->uaddr2, (size_t)uap->uaddr1,&timeout);
4350                 if (error != 0)
4351                         return (error);
4352                 tm_p = &timeout;
4353         }
4354         return (do_wait(td, uap->obj, uap->val, tm_p, 1, 1));
4355 }
4356
4357 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4358 static int
4359 __umtx_op_sem_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4360 {
4361         struct _umtx_time *tm_p, timeout;
4362         int error;
4363
4364         /* Allow a null timespec (wait forever). */
4365         if (uap->uaddr2 == NULL)
4366                 tm_p = NULL;
4367         else {
4368                 error = umtx_copyin_umtx_time32(uap->uaddr2,
4369                     (size_t)uap->uaddr1, &timeout);
4370                 if (error != 0)
4371                         return (error);
4372                 tm_p = &timeout;
4373         }
4374         return (do_sem_wait(td, uap->obj, tm_p));
4375 }
4376 #endif
4377
4378 static int
4379 __umtx_op_sem2_wait_compat32(struct thread *td, struct _umtx_op_args *uap)
4380 {
4381         struct _umtx_time *tm_p, timeout;
4382         size_t uasize;
4383         int error;
4384
4385         /* Allow a null timespec (wait forever). */
4386         if (uap->uaddr2 == NULL) {
4387                 uasize = 0;
4388                 tm_p = NULL;
4389         } else {
4390                 uasize = (size_t)uap->uaddr1;
4391                 error = umtx_copyin_umtx_time32(uap->uaddr2, uasize, &timeout);
4392                 if (error != 0)
4393                         return (error);
4394                 tm_p = &timeout;
4395         }
4396         error = do_sem2_wait(td, uap->obj, tm_p);
4397         if (error == EINTR && uap->uaddr2 != NULL &&
4398             (timeout._flags & UMTX_ABSTIME) == 0 &&
4399             uasize >= sizeof(struct umtx_time32) + sizeof(struct timespec32)) {
4400                 struct timespec32 remain32 = {
4401                         .tv_sec = timeout._timeout.tv_sec,
4402                         .tv_nsec = timeout._timeout.tv_nsec
4403                 };
4404                 error = copyout(&remain32,
4405                     (struct umtx_time32 *)uap->uaddr2 + 1,
4406                     sizeof(struct timespec32));
4407                 if (error == 0) {
4408                         error = EINTR;
4409                 }
4410         }
4411
4412         return (error);
4413 }
4414
4415 static int
4416 __umtx_op_nwake_private32(struct thread *td, struct _umtx_op_args *uap)
4417 {
4418         uint32_t uaddrs[BATCH_SIZE], **upp;
4419         int count, error, i, pos, tocopy;
4420
4421         upp = (uint32_t **)uap->obj;
4422         error = 0;
4423         for (count = uap->val, pos = 0; count > 0; count -= tocopy,
4424             pos += tocopy) {
4425                 tocopy = MIN(count, BATCH_SIZE);
4426                 error = copyin(upp + pos, uaddrs, tocopy * sizeof(uint32_t));
4427                 if (error != 0)
4428                         break;
4429                 for (i = 0; i < tocopy; ++i)
4430                         kern_umtx_wake(td, (void *)(intptr_t)uaddrs[i],
4431                             INT_MAX, 1);
4432                 maybe_yield();
4433         }
4434         return (error);
4435 }
4436
4437 struct umtx_robust_lists_params_compat32 {
4438         uint32_t        robust_list_offset;
4439         uint32_t        robust_priv_list_offset;
4440         uint32_t        robust_inact_offset;
4441 };
4442
4443 static int
4444 __umtx_op_robust_lists_compat32(struct thread *td, struct _umtx_op_args *uap)
4445 {
4446         struct umtx_robust_lists_params rb;
4447         struct umtx_robust_lists_params_compat32 rb32;
4448         int error;
4449
4450         if (uap->val > sizeof(rb32))
4451                 return (EINVAL);
4452         bzero(&rb, sizeof(rb));
4453         bzero(&rb32, sizeof(rb32));
4454         error = copyin(uap->uaddr1, &rb32, uap->val);
4455         if (error != 0)
4456                 return (error);
4457         rb.robust_list_offset = rb32.robust_list_offset;
4458         rb.robust_priv_list_offset = rb32.robust_priv_list_offset;
4459         rb.robust_inact_offset = rb32.robust_inact_offset;
4460         return (umtx_robust_lists(td, &rb));
4461 }
4462
4463 static const _umtx_op_func op_table_compat32[] = {
4464         [UMTX_OP_RESERVED0]     = __umtx_op_unimpl,
4465         [UMTX_OP_RESERVED1]     = __umtx_op_unimpl,
4466         [UMTX_OP_WAIT]          = __umtx_op_wait_compat32,
4467         [UMTX_OP_WAKE]          = __umtx_op_wake,
4468         [UMTX_OP_MUTEX_TRYLOCK] = __umtx_op_trylock_umutex,
4469         [UMTX_OP_MUTEX_LOCK]    = __umtx_op_lock_umutex_compat32,
4470         [UMTX_OP_MUTEX_UNLOCK]  = __umtx_op_unlock_umutex,
4471         [UMTX_OP_SET_CEILING]   = __umtx_op_set_ceiling,
4472         [UMTX_OP_CV_WAIT]       = __umtx_op_cv_wait_compat32,
4473         [UMTX_OP_CV_SIGNAL]     = __umtx_op_cv_signal,
4474         [UMTX_OP_CV_BROADCAST]  = __umtx_op_cv_broadcast,
4475         [UMTX_OP_WAIT_UINT]     = __umtx_op_wait_compat32,
4476         [UMTX_OP_RW_RDLOCK]     = __umtx_op_rw_rdlock_compat32,
4477         [UMTX_OP_RW_WRLOCK]     = __umtx_op_rw_wrlock_compat32,
4478         [UMTX_OP_RW_UNLOCK]     = __umtx_op_rw_unlock,
4479         [UMTX_OP_WAIT_UINT_PRIVATE] = __umtx_op_wait_uint_private_compat32,
4480         [UMTX_OP_WAKE_PRIVATE]  = __umtx_op_wake_private,
4481         [UMTX_OP_MUTEX_WAIT]    = __umtx_op_wait_umutex_compat32,
4482         [UMTX_OP_MUTEX_WAKE]    = __umtx_op_wake_umutex,
4483 #if defined(COMPAT_FREEBSD9) || defined(COMPAT_FREEBSD10)
4484         [UMTX_OP_SEM_WAIT]      = __umtx_op_sem_wait_compat32,
4485         [UMTX_OP_SEM_WAKE]      = __umtx_op_sem_wake,
4486 #else
4487         [UMTX_OP_SEM_WAIT]      = __umtx_op_unimpl,
4488         [UMTX_OP_SEM_WAKE]      = __umtx_op_unimpl,
4489 #endif
4490         [UMTX_OP_NWAKE_PRIVATE] = __umtx_op_nwake_private32,
4491         [UMTX_OP_MUTEX_WAKE2]   = __umtx_op_wake2_umutex,
4492         [UMTX_OP_SEM2_WAIT]     = __umtx_op_sem2_wait_compat32,
4493         [UMTX_OP_SEM2_WAKE]     = __umtx_op_sem2_wake,
4494         [UMTX_OP_SHM]           = __umtx_op_shm,
4495         [UMTX_OP_ROBUST_LISTS]  = __umtx_op_robust_lists_compat32,
4496 };
4497
4498 int
4499 freebsd32__umtx_op(struct thread *td, struct freebsd32__umtx_op_args *uap)
4500 {
4501
4502         if ((unsigned)uap->op < nitems(op_table_compat32)) {
4503                 return (*op_table_compat32[uap->op])(td,
4504                     (struct _umtx_op_args *)uap);
4505         }
4506         return (EINVAL);
4507 }
4508 #endif
4509
4510 void
4511 umtx_thread_init(struct thread *td)
4512 {
4513
4514         td->td_umtxq = umtxq_alloc();
4515         td->td_umtxq->uq_thread = td;
4516 }
4517
4518 void
4519 umtx_thread_fini(struct thread *td)
4520 {
4521
4522         umtxq_free(td->td_umtxq);
4523 }
4524
4525 /*
4526  * It will be called when new thread is created, e.g fork().
4527  */
4528 void
4529 umtx_thread_alloc(struct thread *td)
4530 {
4531         struct umtx_q *uq;
4532
4533         uq = td->td_umtxq;
4534         uq->uq_inherited_pri = PRI_MAX;
4535
4536         KASSERT(uq->uq_flags == 0, ("uq_flags != 0"));
4537         KASSERT(uq->uq_thread == td, ("uq_thread != td"));
4538         KASSERT(uq->uq_pi_blocked == NULL, ("uq_pi_blocked != NULL"));
4539         KASSERT(TAILQ_EMPTY(&uq->uq_pi_contested), ("uq_pi_contested is not empty"));
4540 }
4541
4542 /*
4543  * exec() hook.
4544  *
4545  * Clear robust lists for all process' threads, not delaying the
4546  * cleanup to thread_exit hook, since the relevant address space is
4547  * destroyed right now.
4548  */
4549 static void
4550 umtx_exec_hook(void *arg __unused, struct proc *p,
4551     struct image_params *imgp __unused)
4552 {
4553         struct thread *td;
4554
4555         KASSERT(p == curproc, ("need curproc"));
4556         KASSERT((p->p_flag & P_HADTHREADS) == 0 ||
4557             (p->p_flag & P_STOPPED_SINGLE) != 0,
4558             ("curproc must be single-threaded"));
4559         /*
4560          * There is no need to lock the list as only this thread can be
4561          * running.
4562          */
4563         FOREACH_THREAD_IN_PROC(p, td) {
4564                 KASSERT(td == curthread ||
4565                     ((td->td_flags & TDF_BOUNDARY) != 0 && TD_IS_SUSPENDED(td)),
4566                     ("running thread %p %p", p, td));
4567                 umtx_thread_cleanup(td);
4568                 td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0;
4569         }
4570 }
4571
4572 /*
4573  * thread_exit() hook.
4574  */
4575 void
4576 umtx_thread_exit(struct thread *td)
4577 {
4578
4579         umtx_thread_cleanup(td);
4580 }
4581
4582 static int
4583 umtx_read_uptr(struct thread *td, uintptr_t ptr, uintptr_t *res)
4584 {
4585         u_long res1;
4586 #ifdef COMPAT_FREEBSD32
4587         uint32_t res32;
4588 #endif
4589         int error;
4590
4591 #ifdef COMPAT_FREEBSD32
4592         if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4593                 error = fueword32((void *)ptr, &res32);
4594                 if (error == 0)
4595                         res1 = res32;
4596         } else
4597 #endif
4598         {
4599                 error = fueword((void *)ptr, &res1);
4600         }
4601         if (error == 0)
4602                 *res = res1;
4603         else
4604                 error = EFAULT;
4605         return (error);
4606 }
4607
4608 static void
4609 umtx_read_rb_list(struct thread *td, struct umutex *m, uintptr_t *rb_list)
4610 {
4611 #ifdef COMPAT_FREEBSD32
4612         struct umutex32 m32;
4613
4614         if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
4615                 memcpy(&m32, m, sizeof(m32));
4616                 *rb_list = m32.m_rb_lnk;
4617         } else
4618 #endif
4619                 *rb_list = m->m_rb_lnk;
4620 }
4621
4622 static int
4623 umtx_handle_rb(struct thread *td, uintptr_t rbp, uintptr_t *rb_list, bool inact)
4624 {
4625         struct umutex m;
4626         int error;
4627
4628         KASSERT(td->td_proc == curproc, ("need current vmspace"));
4629         error = copyin((void *)rbp, &m, sizeof(m));
4630         if (error != 0)
4631                 return (error);
4632         if (rb_list != NULL)
4633                 umtx_read_rb_list(td, &m, rb_list);
4634         if ((m.m_flags & UMUTEX_ROBUST) == 0)
4635                 return (EINVAL);
4636         if ((m.m_owner & ~UMUTEX_CONTESTED) != td->td_tid)
4637                 /* inact is cleared after unlock, allow the inconsistency */
4638                 return (inact ? 0 : EINVAL);
4639         return (do_unlock_umutex(td, (struct umutex *)rbp, true));
4640 }
4641
4642 static void
4643 umtx_cleanup_rb_list(struct thread *td, uintptr_t rb_list, uintptr_t *rb_inact,
4644     const char *name)
4645 {
4646         int error, i;
4647         uintptr_t rbp;
4648         bool inact;
4649
4650         if (rb_list == 0)
4651                 return;
4652         error = umtx_read_uptr(td, rb_list, &rbp);
4653         for (i = 0; error == 0 && rbp != 0 && i < umtx_max_rb; i++) {
4654                 if (rbp == *rb_inact) {
4655                         inact = true;
4656                         *rb_inact = 0;
4657                 } else
4658                         inact = false;
4659                 error = umtx_handle_rb(td, rbp, &rbp, inact);
4660         }
4661         if (i == umtx_max_rb && umtx_verbose_rb) {
4662                 uprintf("comm %s pid %d: reached umtx %smax rb %d\n",
4663                     td->td_proc->p_comm, td->td_proc->p_pid, name, umtx_max_rb);
4664         }
4665         if (error != 0 && umtx_verbose_rb) {
4666                 uprintf("comm %s pid %d: handling %srb error %d\n",
4667                     td->td_proc->p_comm, td->td_proc->p_pid, name, error);
4668         }
4669 }
4670
4671 /*
4672  * Clean up umtx data.
4673  */
4674 static void
4675 umtx_thread_cleanup(struct thread *td)
4676 {
4677         struct umtx_q *uq;
4678         struct umtx_pi *pi;
4679         uintptr_t rb_inact;
4680
4681         /*
4682          * Disown pi mutexes.
4683          */
4684         uq = td->td_umtxq;
4685         if (uq != NULL) {
4686                 if (uq->uq_inherited_pri != PRI_MAX ||
4687                     !TAILQ_EMPTY(&uq->uq_pi_contested)) {
4688                         mtx_lock(&umtx_lock);
4689                         uq->uq_inherited_pri = PRI_MAX;
4690                         while ((pi = TAILQ_FIRST(&uq->uq_pi_contested)) != NULL) {
4691                                 pi->pi_owner = NULL;
4692                                 TAILQ_REMOVE(&uq->uq_pi_contested, pi, pi_link);
4693                         }
4694                         mtx_unlock(&umtx_lock);
4695                 }
4696                 sched_lend_user_prio_cond(td, PRI_MAX);
4697         }
4698
4699         if (td->td_rb_inact == 0 && td->td_rb_list == 0 && td->td_rbp_list == 0)
4700                 return;
4701
4702         /*
4703          * Handle terminated robust mutexes.  Must be done after
4704          * robust pi disown, otherwise unlock could see unowned
4705          * entries.
4706          */
4707         rb_inact = td->td_rb_inact;
4708         if (rb_inact != 0)
4709                 (void)umtx_read_uptr(td, rb_inact, &rb_inact);
4710         umtx_cleanup_rb_list(td, td->td_rb_list, &rb_inact, "");
4711         umtx_cleanup_rb_list(td, td->td_rbp_list, &rb_inact, "priv ");
4712         if (rb_inact != 0)
4713                 (void)umtx_handle_rb(td, rb_inact, NULL, true);
4714 }