xref: /freebsd/sys/compat/linux/linux_futex.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*	$NetBSD: linux_futex.c,v 1.7 2006/07/24 19:01:49 manu Exp $ */
2 
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (c) 2009-2016 Dmitry Chagin
7  * Copyright (c) 2005 Emmanuel Dreyfus
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by Emmanuel Dreyfus
21  * 4. The name of the author may not be used to endorse or promote
22  *    products derived from this software without specific prior written
23  *    permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
26  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 #if 0
41 __KERNEL_RCSID(1, "$NetBSD: linux_futex.c,v 1.7 2006/07/24 19:01:49 manu Exp $");
42 #endif
43 
44 #include "opt_compat.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/imgact.h>
49 #include <sys/kernel.h>
50 #include <sys/ktr.h>
51 #include <sys/lock.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
54 #include <sys/priv.h>
55 #include <sys/proc.h>
56 #include <sys/queue.h>
57 #include <sys/sched.h>
58 #include <sys/sdt.h>
59 #include <sys/umtx.h>
60 
61 #include <vm/vm_extern.h>
62 
63 #ifdef COMPAT_LINUX32
64 #include <machine/../linux32/linux.h>
65 #include <machine/../linux32/linux32_proto.h>
66 #else
67 #include <machine/../linux/linux.h>
68 #include <machine/../linux/linux_proto.h>
69 #endif
70 #include <compat/linux/linux_dtrace.h>
71 #include <compat/linux/linux_emul.h>
72 #include <compat/linux/linux_futex.h>
73 #include <compat/linux/linux_timer.h>
74 #include <compat/linux/linux_util.h>
75 
76 /* DTrace init */
77 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
78 
79 /**
80  * Futex part for the special DTrace module "locks".
81  */
82 LIN_SDT_PROBE_DEFINE1(locks, futex_mtx, locked, "struct mtx *");
83 LIN_SDT_PROBE_DEFINE1(locks, futex_mtx, unlock, "struct mtx *");
84 
85 /**
86  * Per futex probes.
87  */
88 LIN_SDT_PROBE_DEFINE1(futex, futex, create, "struct sx *");
89 LIN_SDT_PROBE_DEFINE1(futex, futex, destroy, "struct sx *");
90 
91 /**
92  * DTrace probes in this module.
93  */
94 LIN_SDT_PROBE_DEFINE3(futex, futex_put, destroy, "uint32_t *", "uint32_t",
95     "int");
96 LIN_SDT_PROBE_DEFINE3(futex, futex_put, unlock, "uint32_t *", "uint32_t",
97     "int");
98 LIN_SDT_PROBE_DEFINE1(futex, futex_get0, umtx_key_get_error, "int");
99 LIN_SDT_PROBE_DEFINE3(futex, futex_get0, shared, "uint32_t *", "uint32_t",
100     "int");
101 LIN_SDT_PROBE_DEFINE1(futex, futex_get0, null, "uint32_t *");
102 LIN_SDT_PROBE_DEFINE3(futex, futex_get0, new, "uint32_t *", "uint32_t", "int");
103 LIN_SDT_PROBE_DEFINE0(futex, futex_get, error);
104 LIN_SDT_PROBE_DEFINE5(futex, futex_sleep, requeue_error, "int", "uint32_t *",
105     "struct waiting_proc *", "uint32_t *", "uint32_t");
106 LIN_SDT_PROBE_DEFINE3(futex, futex_sleep, sleep_error, "int", "uint32_t *",
107     "struct waiting_proc *");
108 LIN_SDT_PROBE_DEFINE3(futex, futex_wake, iterate, "uint32_t",
109     "struct waiting_proc *", "uint32_t");
110 LIN_SDT_PROBE_DEFINE1(futex, futex_wake, wakeup, "struct waiting_proc *");
111 LIN_SDT_PROBE_DEFINE1(futex, futex_requeue, wakeup, "struct waiting_proc *");
112 LIN_SDT_PROBE_DEFINE3(futex, futex_requeue, requeue, "uint32_t *",
113     "struct waiting_proc *", "uint32_t");
114 LIN_SDT_PROBE_DEFINE1(futex, futex_wait, sleep_error, "int");
115 LIN_SDT_PROBE_DEFINE4(futex, futex_atomic_op, decoded_op, "int", "int", "int",
116     "int");
117 LIN_SDT_PROBE_DEFINE0(futex, futex_atomic_op, missing_access_check);
118 LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, unimplemented_op, "int");
119 LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, unimplemented_cmp, "int");
120 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_clockswitch);
121 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, copyin_error, "int");
122 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, invalid_cmp_requeue_use);
123 LIN_SDT_PROBE_DEFINE3(futex, linux_sys_futex, debug_wait, "uint32_t *",
124     "uint32_t", "uint32_t");
125 LIN_SDT_PROBE_DEFINE4(futex, linux_sys_futex, debug_wait_value_neq,
126     "uint32_t *", "uint32_t", "int", "uint32_t");
127 LIN_SDT_PROBE_DEFINE3(futex, linux_sys_futex, debug_wake, "uint32_t *",
128     "uint32_t", "uint32_t");
129 LIN_SDT_PROBE_DEFINE5(futex, linux_sys_futex, debug_cmp_requeue, "uint32_t *",
130     "uint32_t", "uint32_t", "uint32_t *", "struct l_timespec *");
131 LIN_SDT_PROBE_DEFINE2(futex, linux_sys_futex, debug_cmp_requeue_value_neq,
132     "uint32_t", "int");
133 LIN_SDT_PROBE_DEFINE5(futex, linux_sys_futex, debug_wake_op, "uint32_t *",
134     "int", "uint32_t", "uint32_t *", "uint32_t");
135 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unhandled_efault);
136 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_lock_pi);
137 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_unlock_pi);
138 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_trylock_pi);
139 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, deprecated_requeue);
140 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_wait_requeue_pi);
141 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_cmp_requeue_pi);
142 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, unknown_operation, "int");
143 LIN_SDT_PROBE_DEFINE0(futex, linux_set_robust_list, size_error);
144 LIN_SDT_PROBE_DEFINE1(futex, linux_get_robust_list, copyout_error, "int");
145 LIN_SDT_PROBE_DEFINE1(futex, handle_futex_death, copyin_error, "int");
146 LIN_SDT_PROBE_DEFINE1(futex, fetch_robust_entry, copyin_error, "int");
147 LIN_SDT_PROBE_DEFINE1(futex, release_futexes, copyin_error, "int");
148 
149 struct futex;
150 
151 struct waiting_proc {
152 	uint32_t	wp_flags;
153 	struct futex	*wp_futex;
154 	TAILQ_ENTRY(waiting_proc) wp_list;
155 };
156 
157 struct futex {
158 	struct mtx	f_lck;
159 	uint32_t	*f_uaddr;	/* user-supplied value, for debug */
160 	struct umtx_key	f_key;
161 	uint32_t	f_refcount;
162 	uint32_t	f_bitset;
163 	LIST_ENTRY(futex) f_list;
164 	TAILQ_HEAD(lf_waiting_proc, waiting_proc) f_waiting_proc;
165 };
166 
167 #define FUTEX_LOCK(f)		mtx_lock(&(f)->f_lck)
168 #define FUTEX_LOCKED(f)		mtx_owned(&(f)->f_lck)
169 #define FUTEX_UNLOCK(f)		mtx_unlock(&(f)->f_lck)
170 #define FUTEX_INIT(f)		do { \
171 				    mtx_init(&(f)->f_lck, "ftlk", NULL, \
172 					MTX_DUPOK); \
173 				    LIN_SDT_PROBE1(futex, futex, create, \
174 					&(f)->f_lck); \
175 				} while (0)
176 #define FUTEX_DESTROY(f)	do { \
177 				    LIN_SDT_PROBE1(futex, futex, destroy, \
178 					&(f)->f_lck); \
179 				    mtx_destroy(&(f)->f_lck); \
180 				} while (0)
181 #define FUTEX_ASSERT_LOCKED(f)	mtx_assert(&(f)->f_lck, MA_OWNED)
182 #define FUTEX_ASSERT_UNLOCKED(f) mtx_assert(&(f)->f_lck, MA_NOTOWNED)
183 
184 #define FUTEXES_LOCK		do { \
185 				    mtx_lock(&futex_mtx); \
186 				    LIN_SDT_PROBE1(locks, futex_mtx, \
187 					locked, &futex_mtx); \
188 				} while (0)
189 #define FUTEXES_UNLOCK		do { \
190 				    LIN_SDT_PROBE1(locks, futex_mtx, \
191 					unlock, &futex_mtx); \
192 				    mtx_unlock(&futex_mtx); \
193 				} while (0)
194 
195 /* flags for futex_get() */
196 #define FUTEX_CREATE_WP		0x1	/* create waiting_proc */
197 #define FUTEX_DONTCREATE	0x2	/* don't create futex if not exists */
198 #define FUTEX_DONTEXISTS	0x4	/* return EINVAL if futex exists */
199 #define	FUTEX_SHARED		0x8	/* shared futex */
200 #define	FUTEX_DONTLOCK		0x10	/* don't lock futex */
201 
202 /* wp_flags */
203 #define FUTEX_WP_REQUEUED	0x1	/* wp requeued - wp moved from wp_list
204 					 * of futex where thread sleep to wp_list
205 					 * of another futex.
206 					 */
207 #define FUTEX_WP_REMOVED	0x2	/* wp is woken up and removed from futex
208 					 * wp_list to prevent double wakeup.
209 					 */
210 
211 static void futex_put(struct futex *, struct waiting_proc *);
212 static int futex_get0(uint32_t *, struct futex **f, uint32_t);
213 static int futex_get(uint32_t *, struct waiting_proc **, struct futex **,
214     uint32_t);
215 static int futex_sleep(struct futex *, struct waiting_proc *, struct timespec *);
216 static int futex_wake(struct futex *, int, uint32_t);
217 static int futex_requeue(struct futex *, int, struct futex *, int);
218 static int futex_copyin_timeout(int, struct l_timespec *, int,
219     struct timespec *);
220 static int futex_wait(struct futex *, struct waiting_proc *, struct timespec *,
221     uint32_t);
222 static void futex_lock(struct futex *);
223 static void futex_unlock(struct futex *);
224 static int futex_atomic_op(struct thread *, int, uint32_t *);
225 static int handle_futex_death(struct linux_emuldata *, uint32_t *,
226     unsigned int);
227 static int fetch_robust_entry(struct linux_robust_list **,
228     struct linux_robust_list **, unsigned int *);
229 
230 static int
231 futex_copyin_timeout(int op, struct l_timespec *luts, int clockrt,
232     struct timespec *ts)
233 {
234 	struct l_timespec lts;
235 	struct timespec kts;
236 	int error;
237 
238 	error = copyin(luts, &lts, sizeof(lts));
239 	if (error)
240 		return (error);
241 
242 	error = linux_to_native_timespec(ts, &lts);
243 	if (error)
244 		return (error);
245 	if (clockrt) {
246 		nanotime(&kts);
247 		timespecsub(ts, &kts, ts);
248 	} else if (op == LINUX_FUTEX_WAIT_BITSET) {
249 		nanouptime(&kts);
250 		timespecsub(ts, &kts, ts);
251 	}
252 	return (error);
253 }
254 
255 static void
256 futex_put(struct futex *f, struct waiting_proc *wp)
257 {
258 
259 	if (wp != NULL) {
260 		if ((wp->wp_flags & FUTEX_WP_REMOVED) == 0)
261 			TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
262 		free(wp, M_FUTEX_WP);
263 	}
264 
265 	FUTEXES_LOCK;
266 	if (--f->f_refcount == 0) {
267 		LIST_REMOVE(f, f_list);
268 		FUTEXES_UNLOCK;
269 		if (FUTEX_LOCKED(f))
270 			futex_unlock(f);
271 
272 		LIN_SDT_PROBE3(futex, futex_put, destroy, f->f_uaddr,
273 		    f->f_refcount, f->f_key.shared);
274 		LINUX_CTR3(sys_futex, "futex_put destroy uaddr %p ref %d "
275 		    "shared %d", f->f_uaddr, f->f_refcount, f->f_key.shared);
276 		umtx_key_release(&f->f_key);
277 		FUTEX_DESTROY(f);
278 		free(f, M_FUTEX);
279 		return;
280 	}
281 
282 	LIN_SDT_PROBE3(futex, futex_put, unlock, f->f_uaddr, f->f_refcount,
283 	    f->f_key.shared);
284 	LINUX_CTR3(sys_futex, "futex_put uaddr %p ref %d shared %d",
285 	    f->f_uaddr, f->f_refcount, f->f_key.shared);
286 	if (FUTEX_LOCKED(f))
287 		futex_unlock(f);
288 	FUTEXES_UNLOCK;
289 }
290 
291 static int
292 futex_get0(uint32_t *uaddr, struct futex **newf, uint32_t flags)
293 {
294 	struct futex *f, *tmpf;
295 	struct umtx_key key;
296 	int error;
297 
298 	*newf = tmpf = NULL;
299 
300 	error = umtx_key_get(uaddr, TYPE_FUTEX, (flags & FUTEX_SHARED) ?
301 	    AUTO_SHARE : THREAD_SHARE, &key);
302 	if (error) {
303 		LIN_SDT_PROBE1(futex, futex_get0, umtx_key_get_error, error);
304 		return (error);
305 	}
306 retry:
307 	FUTEXES_LOCK;
308 	LIST_FOREACH(f, &futex_list, f_list) {
309 		if (umtx_key_match(&f->f_key, &key)) {
310 			if (tmpf != NULL) {
311 				if (FUTEX_LOCKED(tmpf))
312 					futex_unlock(tmpf);
313 				FUTEX_DESTROY(tmpf);
314 				free(tmpf, M_FUTEX);
315 			}
316 			if (flags & FUTEX_DONTEXISTS) {
317 				FUTEXES_UNLOCK;
318 				umtx_key_release(&key);
319 
320 				return (EINVAL);
321 			}
322 
323 			/*
324 			 * Increment refcount of the found futex to
325 			 * prevent it from deallocation before FUTEX_LOCK()
326 			 */
327 			++f->f_refcount;
328 			FUTEXES_UNLOCK;
329 			umtx_key_release(&key);
330 
331 			if ((flags & FUTEX_DONTLOCK) == 0)
332 				futex_lock(f);
333 			*newf = f;
334 			LIN_SDT_PROBE3(futex, futex_get0, shared, uaddr,
335 			    f->f_refcount, f->f_key.shared);
336 			LINUX_CTR3(sys_futex, "futex_get uaddr %p ref %d shared %d",
337 			    uaddr, f->f_refcount, f->f_key.shared);
338 
339 			return (0);
340 		}
341 	}
342 
343 	if (flags & FUTEX_DONTCREATE) {
344 		FUTEXES_UNLOCK;
345 		umtx_key_release(&key);
346 		LIN_SDT_PROBE1(futex, futex_get0, null, uaddr);
347 		LINUX_CTR1(sys_futex, "futex_get uaddr %p null", uaddr);
348 
349 		return (0);
350 	}
351 
352 	if (tmpf == NULL) {
353 		FUTEXES_UNLOCK;
354 		tmpf = malloc(sizeof(*tmpf), M_FUTEX, M_WAITOK | M_ZERO);
355 		tmpf->f_uaddr = uaddr;
356 		tmpf->f_key = key;
357 		tmpf->f_refcount = 1;
358 		tmpf->f_bitset = FUTEX_BITSET_MATCH_ANY;
359 		FUTEX_INIT(tmpf);
360 		TAILQ_INIT(&tmpf->f_waiting_proc);
361 
362 		/*
363 		 * Lock the new futex before an insert into the futex_list
364 		 * to prevent futex usage by other.
365 		 */
366 		if ((flags & FUTEX_DONTLOCK) == 0)
367 			futex_lock(tmpf);
368 		goto retry;
369 	}
370 
371 	LIST_INSERT_HEAD(&futex_list, tmpf, f_list);
372 	FUTEXES_UNLOCK;
373 
374 	LIN_SDT_PROBE3(futex, futex_get0, new, uaddr, tmpf->f_refcount,
375 	    tmpf->f_key.shared);
376 	LINUX_CTR3(sys_futex, "futex_get uaddr %p ref %d shared %d new",
377 	    uaddr, tmpf->f_refcount, tmpf->f_key.shared);
378 	*newf = tmpf;
379 
380 	return (0);
381 }
382 
383 static int
384 futex_get(uint32_t *uaddr, struct waiting_proc **wp, struct futex **f,
385     uint32_t flags)
386 {
387 	int error;
388 
389 	if (flags & FUTEX_CREATE_WP) {
390 		*wp = malloc(sizeof(struct waiting_proc), M_FUTEX_WP, M_WAITOK);
391 		(*wp)->wp_flags = 0;
392 	}
393 	error = futex_get0(uaddr, f, flags);
394 	if (error) {
395 		LIN_SDT_PROBE0(futex, futex_get, error);
396 
397 		if (flags & FUTEX_CREATE_WP)
398 			free(*wp, M_FUTEX_WP);
399 
400 		return (error);
401 	}
402 	if (flags & FUTEX_CREATE_WP) {
403 		TAILQ_INSERT_HEAD(&(*f)->f_waiting_proc, *wp, wp_list);
404 		(*wp)->wp_futex = *f;
405 	}
406 
407 	return (error);
408 }
409 
410 static inline void
411 futex_lock(struct futex *f)
412 {
413 
414 	LINUX_CTR3(sys_futex, "futex_lock uaddr %p ref %d shared %d",
415 	    f->f_uaddr, f->f_refcount, f->f_key.shared);
416 	FUTEX_ASSERT_UNLOCKED(f);
417 	FUTEX_LOCK(f);
418 }
419 
420 static inline void
421 futex_unlock(struct futex *f)
422 {
423 
424 	LINUX_CTR3(sys_futex, "futex_unlock uaddr %p ref %d shared %d",
425 	    f->f_uaddr, f->f_refcount, f->f_key.shared);
426 	FUTEX_ASSERT_LOCKED(f);
427 	FUTEX_UNLOCK(f);
428 }
429 
430 static int
431 futex_sleep(struct futex *f, struct waiting_proc *wp, struct timespec *ts)
432 {
433 	struct timespec uts;
434 	sbintime_t sbt, prec, tmp;
435 	time_t over;
436 	int error;
437 
438 	FUTEX_ASSERT_LOCKED(f);
439 	if (ts != NULL) {
440 		uts = *ts;
441 		if (uts.tv_sec > INT32_MAX / 2) {
442 			over = uts.tv_sec - INT32_MAX / 2;
443 			uts.tv_sec -= over;
444 		}
445 		tmp = tstosbt(uts);
446 		if (TIMESEL(&sbt, tmp))
447 			sbt += tc_tick_sbt;
448 		sbt += tmp;
449 		prec = tmp;
450 		prec >>= tc_precexp;
451 	} else {
452 		sbt = 0;
453 		prec = 0;
454 	}
455 	LINUX_CTR4(sys_futex, "futex_sleep enter uaddr %p wp %p timo %ld ref %d",
456 	    f->f_uaddr, wp, sbt, f->f_refcount);
457 
458 	error = msleep_sbt(wp, &f->f_lck, PCATCH, "futex", sbt, prec, C_ABSOLUTE);
459 	if (wp->wp_flags & FUTEX_WP_REQUEUED) {
460 		KASSERT(f != wp->wp_futex, ("futex != wp_futex"));
461 
462 		if (error) {
463 			LIN_SDT_PROBE5(futex, futex_sleep, requeue_error, error,
464 			    f->f_uaddr, wp, wp->wp_futex->f_uaddr,
465 			    wp->wp_futex->f_refcount);
466 		}
467 
468 		LINUX_CTR5(sys_futex, "futex_sleep out error %d uaddr %p wp"
469 		    " %p requeued uaddr %p ref %d",
470 		    error, f->f_uaddr, wp, wp->wp_futex->f_uaddr,
471 		    wp->wp_futex->f_refcount);
472 		futex_put(f, NULL);
473 		f = wp->wp_futex;
474 		futex_lock(f);
475 	} else {
476 		if (error) {
477 			LIN_SDT_PROBE3(futex, futex_sleep, sleep_error, error,
478 			    f->f_uaddr, wp);
479 		}
480 		LINUX_CTR3(sys_futex, "futex_sleep out error %d uaddr %p wp %p",
481 		    error, f->f_uaddr, wp);
482 	}
483 
484 	futex_put(f, wp);
485 
486 	return (error);
487 }
488 
489 static int
490 futex_wake(struct futex *f, int n, uint32_t bitset)
491 {
492 	struct waiting_proc *wp, *wpt;
493 	int count = 0;
494 
495 	if (bitset == 0)
496 		return (EINVAL);
497 
498 	FUTEX_ASSERT_LOCKED(f);
499 	TAILQ_FOREACH_SAFE(wp, &f->f_waiting_proc, wp_list, wpt) {
500 		LIN_SDT_PROBE3(futex, futex_wake, iterate, f->f_uaddr, wp,
501 		    f->f_refcount);
502 		LINUX_CTR3(sys_futex, "futex_wake uaddr %p wp %p ref %d",
503 		    f->f_uaddr, wp, f->f_refcount);
504 		/*
505 		 * Unless we find a matching bit in
506 		 * the bitset, continue searching.
507 		 */
508 		if (!(wp->wp_futex->f_bitset & bitset))
509 			continue;
510 
511 		wp->wp_flags |= FUTEX_WP_REMOVED;
512 		TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
513 		LIN_SDT_PROBE1(futex, futex_wake, wakeup, wp);
514 		wakeup_one(wp);
515 		if (++count == n)
516 			break;
517 	}
518 
519 	return (count);
520 }
521 
522 static int
523 futex_requeue(struct futex *f, int n, struct futex *f2, int n2)
524 {
525 	struct waiting_proc *wp, *wpt;
526 	int count = 0;
527 
528 	FUTEX_ASSERT_LOCKED(f);
529 	FUTEX_ASSERT_LOCKED(f2);
530 
531 	TAILQ_FOREACH_SAFE(wp, &f->f_waiting_proc, wp_list, wpt) {
532 		if (++count <= n) {
533 			LINUX_CTR2(sys_futex, "futex_req_wake uaddr %p wp %p",
534 			    f->f_uaddr, wp);
535 			wp->wp_flags |= FUTEX_WP_REMOVED;
536 			TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
537 			LIN_SDT_PROBE1(futex, futex_requeue, wakeup, wp);
538 			wakeup_one(wp);
539 		} else {
540 			LIN_SDT_PROBE3(futex, futex_requeue, requeue,
541 			    f->f_uaddr, wp, f2->f_uaddr);
542 			LINUX_CTR3(sys_futex, "futex_requeue uaddr %p wp %p to %p",
543 			    f->f_uaddr, wp, f2->f_uaddr);
544 			wp->wp_flags |= FUTEX_WP_REQUEUED;
545 			/* Move wp to wp_list of f2 futex */
546 			TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
547 			TAILQ_INSERT_HEAD(&f2->f_waiting_proc, wp, wp_list);
548 
549 			/*
550 			 * Thread which sleeps on wp after waking should
551 			 * acquire f2 lock, so increment refcount of f2 to
552 			 * prevent it from premature deallocation.
553 			 */
554 			wp->wp_futex = f2;
555 			FUTEXES_LOCK;
556 			++f2->f_refcount;
557 			FUTEXES_UNLOCK;
558 			if (count - n >= n2)
559 				break;
560 		}
561 	}
562 
563 	return (count);
564 }
565 
566 static int
567 futex_wait(struct futex *f, struct waiting_proc *wp, struct timespec *ts,
568     uint32_t bitset)
569 {
570 	int error;
571 
572 	if (bitset == 0) {
573 		futex_put(f, wp);
574 		return (EINVAL);
575 	}
576 
577 	f->f_bitset = bitset;
578 	error = futex_sleep(f, wp, ts);
579 	if (error)
580 		LIN_SDT_PROBE1(futex, futex_wait, sleep_error, error);
581 	if (error == EWOULDBLOCK)
582 		error = ETIMEDOUT;
583 
584 	return (error);
585 }
586 
587 static int
588 futex_atomic_op(struct thread *td, int encoded_op, uint32_t *uaddr)
589 {
590 	int op = (encoded_op >> 28) & 7;
591 	int cmp = (encoded_op >> 24) & 15;
592 	int oparg = (encoded_op << 8) >> 20;
593 	int cmparg = (encoded_op << 20) >> 20;
594 	int oldval = 0, ret;
595 
596 	if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
597 		oparg = 1 << oparg;
598 
599 	LIN_SDT_PROBE4(futex, futex_atomic_op, decoded_op, op, cmp, oparg,
600 	    cmparg);
601 
602 	/* XXX: Linux verifies access here and returns EFAULT */
603 	LIN_SDT_PROBE0(futex, futex_atomic_op, missing_access_check);
604 
605 	switch (op) {
606 	case FUTEX_OP_SET:
607 		ret = futex_xchgl(oparg, uaddr, &oldval);
608 		break;
609 	case FUTEX_OP_ADD:
610 		ret = futex_addl(oparg, uaddr, &oldval);
611 		break;
612 	case FUTEX_OP_OR:
613 		ret = futex_orl(oparg, uaddr, &oldval);
614 		break;
615 	case FUTEX_OP_ANDN:
616 		ret = futex_andl(~oparg, uaddr, &oldval);
617 		break;
618 	case FUTEX_OP_XOR:
619 		ret = futex_xorl(oparg, uaddr, &oldval);
620 		break;
621 	default:
622 		LIN_SDT_PROBE1(futex, futex_atomic_op, unimplemented_op, op);
623 		ret = -ENOSYS;
624 		break;
625 	}
626 
627 	if (ret)
628 		return (ret);
629 
630 	switch (cmp) {
631 	case FUTEX_OP_CMP_EQ:
632 		ret = (oldval == cmparg);
633 		break;
634 	case FUTEX_OP_CMP_NE:
635 		ret = (oldval != cmparg);
636 		break;
637 	case FUTEX_OP_CMP_LT:
638 		ret = (oldval < cmparg);
639 		break;
640 	case FUTEX_OP_CMP_GE:
641 		ret = (oldval >= cmparg);
642 		break;
643 	case FUTEX_OP_CMP_LE:
644 		ret = (oldval <= cmparg);
645 		break;
646 	case FUTEX_OP_CMP_GT:
647 		ret = (oldval > cmparg);
648 		break;
649 	default:
650 		LIN_SDT_PROBE1(futex, futex_atomic_op, unimplemented_cmp, cmp);
651 		ret = -ENOSYS;
652 	}
653 
654 	return (ret);
655 }
656 
657 int
658 linux_sys_futex(struct thread *td, struct linux_sys_futex_args *args)
659 {
660 	int clockrt, nrwake, op_ret, ret;
661 	struct linux_pemuldata *pem;
662 	struct waiting_proc *wp;
663 	struct futex *f, *f2;
664 	struct timespec uts, *ts;
665 	int error, save;
666 	uint32_t flags, val;
667 
668 	if (args->op & LINUX_FUTEX_PRIVATE_FLAG) {
669 		flags = 0;
670 		args->op &= ~LINUX_FUTEX_PRIVATE_FLAG;
671 	} else
672 		flags = FUTEX_SHARED;
673 
674 	/*
675 	 * Currently support for switching between CLOCK_MONOTONIC and
676 	 * CLOCK_REALTIME is not present. However Linux forbids the use of
677 	 * FUTEX_CLOCK_REALTIME with any op except FUTEX_WAIT_BITSET and
678 	 * FUTEX_WAIT_REQUEUE_PI.
679 	 */
680 	clockrt = args->op & LINUX_FUTEX_CLOCK_REALTIME;
681 	args->op = args->op & ~LINUX_FUTEX_CLOCK_REALTIME;
682 	if (clockrt && args->op != LINUX_FUTEX_WAIT_BITSET &&
683 		args->op != LINUX_FUTEX_WAIT_REQUEUE_PI) {
684 		LIN_SDT_PROBE0(futex, linux_sys_futex,
685 		    unimplemented_clockswitch);
686 		return (ENOSYS);
687 	}
688 
689 	error = 0;
690 	f = f2 = NULL;
691 
692 	switch (args->op) {
693 	case LINUX_FUTEX_WAIT:
694 		args->val3 = FUTEX_BITSET_MATCH_ANY;
695 		/* FALLTHROUGH */
696 
697 	case LINUX_FUTEX_WAIT_BITSET:
698 		LIN_SDT_PROBE3(futex, linux_sys_futex, debug_wait, args->uaddr,
699 		    args->val, args->val3);
700 		LINUX_CTR3(sys_futex, "WAIT uaddr %p val 0x%x bitset 0x%x",
701 		    args->uaddr, args->val, args->val3);
702 
703 		if (args->timeout != NULL) {
704 			error = futex_copyin_timeout(args->op, args->timeout,
705 			    clockrt, &uts);
706 			if (error) {
707 				LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
708 				    error);
709 				return (error);
710 			}
711 			ts = &uts;
712 		} else
713 			ts = NULL;
714 
715 retry0:
716 		error = futex_get(args->uaddr, &wp, &f,
717 		    flags | FUTEX_CREATE_WP);
718 		if (error)
719 			return (error);
720 
721 		error = copyin_nofault(args->uaddr, &val, sizeof(val));
722 		if (error) {
723 			futex_put(f, wp);
724 			error = copyin(args->uaddr, &val, sizeof(val));
725 			if (error == 0)
726 				goto retry0;
727 			LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
728 			    error);
729 			LINUX_CTR1(sys_futex, "WAIT copyin failed %d",
730 			    error);
731 			return (error);
732 		}
733 		if (val != args->val) {
734 			LIN_SDT_PROBE4(futex, linux_sys_futex,
735 			    debug_wait_value_neq, args->uaddr, args->val, val,
736 			    args->val3);
737 			LINUX_CTR3(sys_futex,
738 			    "WAIT uaddr %p val 0x%x != uval 0x%x",
739 			    args->uaddr, args->val, val);
740 			futex_put(f, wp);
741 			return (EWOULDBLOCK);
742 		}
743 
744 		error = futex_wait(f, wp, ts, args->val3);
745 		break;
746 
747 	case LINUX_FUTEX_WAKE:
748 		args->val3 = FUTEX_BITSET_MATCH_ANY;
749 		/* FALLTHROUGH */
750 
751 	case LINUX_FUTEX_WAKE_BITSET:
752 		LIN_SDT_PROBE3(futex, linux_sys_futex, debug_wake, args->uaddr,
753 		    args->val, args->val3);
754 		LINUX_CTR3(sys_futex, "WAKE uaddr %p nrwake 0x%x bitset 0x%x",
755 		    args->uaddr, args->val, args->val3);
756 
757 		error = futex_get(args->uaddr, NULL, &f,
758 		    flags | FUTEX_DONTCREATE);
759 		if (error)
760 			return (error);
761 
762 		if (f == NULL) {
763 			td->td_retval[0] = 0;
764 			return (error);
765 		}
766 		td->td_retval[0] = futex_wake(f, args->val, args->val3);
767 		futex_put(f, NULL);
768 		break;
769 
770 	case LINUX_FUTEX_CMP_REQUEUE:
771 		LIN_SDT_PROBE5(futex, linux_sys_futex, debug_cmp_requeue,
772 		    args->uaddr, args->val, args->val3, args->uaddr2,
773 		    args->timeout);
774 		LINUX_CTR5(sys_futex, "CMP_REQUEUE uaddr %p "
775 		    "nrwake 0x%x uval 0x%x uaddr2 %p nrequeue 0x%x",
776 		    args->uaddr, args->val, args->val3, args->uaddr2,
777 		    args->timeout);
778 
779 		/*
780 		 * Linux allows this, we would not, it is an incorrect
781 		 * usage of declared ABI, so return EINVAL.
782 		 */
783 		if (args->uaddr == args->uaddr2) {
784 			LIN_SDT_PROBE0(futex, linux_sys_futex,
785 			    invalid_cmp_requeue_use);
786 			return (EINVAL);
787 		}
788 
789 retry1:
790 		error = futex_get(args->uaddr, NULL, &f, flags | FUTEX_DONTLOCK);
791 		if (error)
792 			return (error);
793 
794 		/*
795 		 * To avoid deadlocks return EINVAL if second futex
796 		 * exists at this time.
797 		 *
798 		 * Glibc fall back to FUTEX_WAKE in case of any error
799 		 * returned by FUTEX_CMP_REQUEUE.
800 		 */
801 		error = futex_get(args->uaddr2, NULL, &f2,
802 		    flags | FUTEX_DONTEXISTS | FUTEX_DONTLOCK);
803 		if (error) {
804 			futex_put(f, NULL);
805 			return (error);
806 		}
807 		futex_lock(f);
808 		futex_lock(f2);
809 		error = copyin_nofault(args->uaddr, &val, sizeof(val));
810 		if (error) {
811 			futex_put(f2, NULL);
812 			futex_put(f, NULL);
813 			error = copyin(args->uaddr, &val, sizeof(val));
814 			if (error == 0)
815 				goto retry1;
816 			LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
817 			    error);
818 			LINUX_CTR1(sys_futex, "CMP_REQUEUE copyin failed %d",
819 			    error);
820 			return (error);
821 		}
822 		if (val != args->val3) {
823 			LIN_SDT_PROBE2(futex, linux_sys_futex,
824 			    debug_cmp_requeue_value_neq, args->val, val);
825 			LINUX_CTR2(sys_futex, "CMP_REQUEUE val 0x%x != uval 0x%x",
826 			    args->val, val);
827 			futex_put(f2, NULL);
828 			futex_put(f, NULL);
829 			return (EAGAIN);
830 		}
831 
832 		nrwake = (int)(unsigned long)args->timeout;
833 		td->td_retval[0] = futex_requeue(f, args->val, f2, nrwake);
834 		futex_put(f2, NULL);
835 		futex_put(f, NULL);
836 		break;
837 
838 	case LINUX_FUTEX_WAKE_OP:
839 		LIN_SDT_PROBE5(futex, linux_sys_futex, debug_wake_op,
840 		    args->uaddr, args->op, args->val, args->uaddr2, args->val3);
841 		LINUX_CTR5(sys_futex, "WAKE_OP "
842 		    "uaddr %p nrwake 0x%x uaddr2 %p op 0x%x nrwake2 0x%x",
843 		    args->uaddr, args->val, args->uaddr2, args->val3,
844 		    args->timeout);
845 
846 		if (args->uaddr == args->uaddr2)
847 			return (EINVAL);
848 
849 retry2:
850 		error = futex_get(args->uaddr, NULL, &f, flags | FUTEX_DONTLOCK);
851 		if (error)
852 			return (error);
853 
854 		error = futex_get(args->uaddr2, NULL, &f2, flags | FUTEX_DONTLOCK);
855 		if (error) {
856 			futex_put(f, NULL);
857 			return (error);
858 		}
859 		futex_lock(f);
860 		futex_lock(f2);
861 
862 		/*
863 		 * This function returns positive number as results and
864 		 * negative as errors
865 		 */
866 		save = vm_fault_disable_pagefaults();
867 		op_ret = futex_atomic_op(td, args->val3, args->uaddr2);
868 		vm_fault_enable_pagefaults(save);
869 
870 		LINUX_CTR2(sys_futex, "WAKE_OP atomic_op uaddr %p ret 0x%x",
871 		    args->uaddr, op_ret);
872 
873 		if (op_ret < 0) {
874 			if (f2 != NULL)
875 				futex_put(f2, NULL);
876 			futex_put(f, NULL);
877 			error = copyin(args->uaddr2, &val, sizeof(val));
878 			if (error == 0)
879 				goto retry2;
880 			return (error);
881 		}
882 
883 		ret = futex_wake(f, args->val, args->val3);
884 
885 		if (op_ret > 0) {
886 			op_ret = 0;
887 			nrwake = (int)(unsigned long)args->timeout;
888 
889 			if (f2 != NULL)
890 				op_ret += futex_wake(f2, nrwake, args->val3);
891 			else
892 				op_ret += futex_wake(f, nrwake, args->val3);
893 			ret += op_ret;
894 		}
895 		if (f2 != NULL)
896 			futex_put(f2, NULL);
897 		futex_put(f, NULL);
898 		td->td_retval[0] = ret;
899 		break;
900 
901 	case LINUX_FUTEX_LOCK_PI:
902 		/* not yet implemented */
903 		pem = pem_find(td->td_proc);
904 		if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
905 			linux_msg(td, "unsupported FUTEX_LOCK_PI");
906 			pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
907 			LIN_SDT_PROBE0(futex, linux_sys_futex,
908 			    unimplemented_lock_pi);
909 		}
910 		return (ENOSYS);
911 
912 	case LINUX_FUTEX_UNLOCK_PI:
913 		/* not yet implemented */
914 		pem = pem_find(td->td_proc);
915 		if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
916 			linux_msg(td, "unsupported FUTEX_UNLOCK_PI");
917 			pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
918 			LIN_SDT_PROBE0(futex, linux_sys_futex,
919 			    unimplemented_unlock_pi);
920 		}
921 		return (ENOSYS);
922 
923 	case LINUX_FUTEX_TRYLOCK_PI:
924 		/* not yet implemented */
925 		pem = pem_find(td->td_proc);
926 		if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
927 			linux_msg(td, "unsupported FUTEX_TRYLOCK_PI");
928 			pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
929 			LIN_SDT_PROBE0(futex, linux_sys_futex,
930 			    unimplemented_trylock_pi);
931 		}
932 		return (ENOSYS);
933 
934 	case LINUX_FUTEX_REQUEUE:
935 		/*
936 		 * Glibc does not use this operation since version 2.3.3,
937 		 * as it is racy and replaced by FUTEX_CMP_REQUEUE operation.
938 		 * Glibc versions prior to 2.3.3 fall back to FUTEX_WAKE when
939 		 * FUTEX_REQUEUE returned EINVAL.
940 		 */
941 		pem = pem_find(td->td_proc);
942 		if ((pem->flags & LINUX_XDEPR_REQUEUEOP) == 0) {
943 			linux_msg(td, "unsupported FUTEX_REQUEUE");
944 			pem->flags |= LINUX_XDEPR_REQUEUEOP;
945 			LIN_SDT_PROBE0(futex, linux_sys_futex,
946 			    deprecated_requeue);
947 		}
948 		return (EINVAL);
949 
950 	case LINUX_FUTEX_WAIT_REQUEUE_PI:
951 		/* not yet implemented */
952 		pem = pem_find(td->td_proc);
953 		if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
954 			linux_msg(td, "unsupported FUTEX_WAIT_REQUEUE_PI");
955 			pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
956 			LIN_SDT_PROBE0(futex, linux_sys_futex,
957 			    unimplemented_wait_requeue_pi);
958 		}
959 		return (ENOSYS);
960 
961 	case LINUX_FUTEX_CMP_REQUEUE_PI:
962 		/* not yet implemented */
963 		pem = pem_find(td->td_proc);
964 		if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
965 			linux_msg(td, "unsupported FUTEX_CMP_REQUEUE_PI");
966 			pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
967 			LIN_SDT_PROBE0(futex, linux_sys_futex,
968 			    unimplemented_cmp_requeue_pi);
969 		}
970 		return (ENOSYS);
971 
972 	default:
973 		linux_msg(td, "unsupported futex op %d", args->op);
974 		LIN_SDT_PROBE1(futex, linux_sys_futex, unknown_operation,
975 		    args->op);
976 		return (ENOSYS);
977 	}
978 
979 	return (error);
980 }
981 
982 int
983 linux_set_robust_list(struct thread *td, struct linux_set_robust_list_args *args)
984 {
985 	struct linux_emuldata *em;
986 
987 	if (args->len != sizeof(struct linux_robust_list_head)) {
988 		LIN_SDT_PROBE0(futex, linux_set_robust_list, size_error);
989 		return (EINVAL);
990 	}
991 
992 	em = em_find(td);
993 	em->robust_futexes = args->head;
994 
995 	return (0);
996 }
997 
998 int
999 linux_get_robust_list(struct thread *td, struct linux_get_robust_list_args *args)
1000 {
1001 	struct linux_emuldata *em;
1002 	struct linux_robust_list_head *head;
1003 	l_size_t len = sizeof(struct linux_robust_list_head);
1004 	struct thread *td2;
1005 	int error = 0;
1006 
1007 	if (!args->pid) {
1008 		em = em_find(td);
1009 		KASSERT(em != NULL, ("get_robust_list: emuldata notfound.\n"));
1010 		head = em->robust_futexes;
1011 	} else {
1012 		td2 = tdfind(args->pid, -1);
1013 		if (td2 == NULL)
1014 			return (ESRCH);
1015 		if (SV_PROC_ABI(td2->td_proc) != SV_ABI_LINUX) {
1016 			PROC_UNLOCK(td2->td_proc);
1017 			return (EPERM);
1018 		}
1019 
1020 		em = em_find(td2);
1021 		KASSERT(em != NULL, ("get_robust_list: emuldata notfound.\n"));
1022 		/* XXX: ptrace? */
1023 		if (priv_check(td, PRIV_CRED_SETUID) ||
1024 		    priv_check(td, PRIV_CRED_SETEUID) ||
1025 		    p_candebug(td, td2->td_proc)) {
1026 			PROC_UNLOCK(td2->td_proc);
1027 			return (EPERM);
1028 		}
1029 		head = em->robust_futexes;
1030 
1031 		PROC_UNLOCK(td2->td_proc);
1032 	}
1033 
1034 	error = copyout(&len, args->len, sizeof(l_size_t));
1035 	if (error) {
1036 		LIN_SDT_PROBE1(futex, linux_get_robust_list, copyout_error,
1037 		    error);
1038 		return (EFAULT);
1039 	}
1040 
1041 	error = copyout(&head, args->head, sizeof(head));
1042 	if (error) {
1043 		LIN_SDT_PROBE1(futex, linux_get_robust_list, copyout_error,
1044 		    error);
1045 	}
1046 
1047 	return (error);
1048 }
1049 
1050 static int
1051 handle_futex_death(struct linux_emuldata *em, uint32_t *uaddr,
1052     unsigned int pi)
1053 {
1054 	uint32_t uval, nval, mval;
1055 	struct futex *f;
1056 	int error;
1057 
1058 retry:
1059 	error = copyin(uaddr, &uval, 4);
1060 	if (error) {
1061 		LIN_SDT_PROBE1(futex, handle_futex_death, copyin_error, error);
1062 		return (EFAULT);
1063 	}
1064 	if ((uval & FUTEX_TID_MASK) == em->em_tid) {
1065 		mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
1066 		nval = casuword32(uaddr, uval, mval);
1067 
1068 		if (nval == -1)
1069 			return (EFAULT);
1070 
1071 		if (nval != uval)
1072 			goto retry;
1073 
1074 		if (!pi && (uval & FUTEX_WAITERS)) {
1075 			error = futex_get(uaddr, NULL, &f,
1076 			    FUTEX_DONTCREATE | FUTEX_SHARED);
1077 			if (error)
1078 				return (error);
1079 			if (f != NULL) {
1080 				futex_wake(f, 1, FUTEX_BITSET_MATCH_ANY);
1081 				futex_put(f, NULL);
1082 			}
1083 		}
1084 	}
1085 
1086 	return (0);
1087 }
1088 
1089 static int
1090 fetch_robust_entry(struct linux_robust_list **entry,
1091     struct linux_robust_list **head, unsigned int *pi)
1092 {
1093 	l_ulong uentry;
1094 	int error;
1095 
1096 	error = copyin((const void *)head, &uentry, sizeof(l_ulong));
1097 	if (error) {
1098 		LIN_SDT_PROBE1(futex, fetch_robust_entry, copyin_error, error);
1099 		return (EFAULT);
1100 	}
1101 
1102 	*entry = (void *)(uentry & ~1UL);
1103 	*pi = uentry & 1;
1104 
1105 	return (0);
1106 }
1107 
1108 /* This walks the list of robust futexes releasing them. */
1109 void
1110 release_futexes(struct thread *td, struct linux_emuldata *em)
1111 {
1112 	struct linux_robust_list_head *head = NULL;
1113 	struct linux_robust_list *entry, *next_entry, *pending;
1114 	unsigned int limit = 2048, pi, next_pi, pip;
1115 	l_long futex_offset;
1116 	int rc, error;
1117 
1118 	head = em->robust_futexes;
1119 
1120 	if (head == NULL)
1121 		return;
1122 
1123 	if (fetch_robust_entry(&entry, PTRIN(&head->list.next), &pi))
1124 		return;
1125 
1126 	error = copyin(&head->futex_offset, &futex_offset,
1127 	    sizeof(futex_offset));
1128 	if (error) {
1129 		LIN_SDT_PROBE1(futex, release_futexes, copyin_error, error);
1130 		return;
1131 	}
1132 
1133 	if (fetch_robust_entry(&pending, PTRIN(&head->pending_list), &pip))
1134 		return;
1135 
1136 	while (entry != &head->list) {
1137 		rc = fetch_robust_entry(&next_entry, PTRIN(&entry->next), &next_pi);
1138 
1139 		if (entry != pending)
1140 			if (handle_futex_death(em,
1141 			    (uint32_t *)((caddr_t)entry + futex_offset), pi)) {
1142 				return;
1143 			}
1144 		if (rc)
1145 			return;
1146 
1147 		entry = next_entry;
1148 		pi = next_pi;
1149 
1150 		if (!--limit)
1151 			break;
1152 
1153 		sched_relinquish(curthread);
1154 	}
1155 
1156 	if (pending)
1157 		handle_futex_death(em, (uint32_t *)((caddr_t)pending + futex_offset), pip);
1158 }
1159