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