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