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: stable/12/sys/compat/linux/linux_futex.c 370058 2021-06-29 20:01:48Z markj $");
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_DEFINE2(futex, futex_put, entry, "struct futex *",
95     "struct waiting_proc *");
96 LIN_SDT_PROBE_DEFINE3(futex, futex_put, destroy, "uint32_t *", "uint32_t",
97     "int");
98 LIN_SDT_PROBE_DEFINE3(futex, futex_put, unlock, "uint32_t *", "uint32_t",
99     "int");
100 LIN_SDT_PROBE_DEFINE0(futex, futex_put, return);
101 LIN_SDT_PROBE_DEFINE3(futex, futex_get0, entry, "uint32_t *", "struct futex **",
102     "uint32_t");
103 LIN_SDT_PROBE_DEFINE1(futex, futex_get0, umtx_key_get_error, "int");
104 LIN_SDT_PROBE_DEFINE3(futex, futex_get0, shared, "uint32_t *", "uint32_t",
105     "int");
106 LIN_SDT_PROBE_DEFINE1(futex, futex_get0, null, "uint32_t *");
107 LIN_SDT_PROBE_DEFINE3(futex, futex_get0, new, "uint32_t *", "uint32_t", "int");
108 LIN_SDT_PROBE_DEFINE1(futex, futex_get0, return, "int");
109 LIN_SDT_PROBE_DEFINE3(futex, futex_get, entry, "uint32_t *",
110     "struct waiting_proc **", "struct futex **");
111 LIN_SDT_PROBE_DEFINE0(futex, futex_get, error);
112 LIN_SDT_PROBE_DEFINE1(futex, futex_get, return, "int");
113 LIN_SDT_PROBE_DEFINE3(futex, futex_sleep, entry, "struct futex *",
114     "struct waiting_proc **", "struct timespec *");
115 LIN_SDT_PROBE_DEFINE5(futex, futex_sleep, requeue_error, "int", "uint32_t *",
116     "struct waiting_proc *", "uint32_t *", "uint32_t");
117 LIN_SDT_PROBE_DEFINE3(futex, futex_sleep, sleep_error, "int", "uint32_t *",
118     "struct waiting_proc *");
119 LIN_SDT_PROBE_DEFINE1(futex, futex_sleep, return, "int");
120 LIN_SDT_PROBE_DEFINE3(futex, futex_wake, entry, "struct futex *", "int",
121     "uint32_t");
122 LIN_SDT_PROBE_DEFINE3(futex, futex_wake, iterate, "uint32_t",
123     "struct waiting_proc *", "uint32_t");
124 LIN_SDT_PROBE_DEFINE1(futex, futex_wake, wakeup, "struct waiting_proc *");
125 LIN_SDT_PROBE_DEFINE1(futex, futex_wake, return, "int");
126 LIN_SDT_PROBE_DEFINE4(futex, futex_requeue, entry, "struct futex *", "int",
127     "struct futex *", "int");
128 LIN_SDT_PROBE_DEFINE1(futex, futex_requeue, wakeup, "struct waiting_proc *");
129 LIN_SDT_PROBE_DEFINE3(futex, futex_requeue, requeue, "uint32_t *",
130     "struct waiting_proc *", "uint32_t");
131 LIN_SDT_PROBE_DEFINE1(futex, futex_requeue, return, "int");
132 LIN_SDT_PROBE_DEFINE4(futex, futex_wait, entry, "struct futex *",
133     "struct waiting_proc **", "struct timespec *", "uint32_t");
134 LIN_SDT_PROBE_DEFINE1(futex, futex_wait, sleep_error, "int");
135 LIN_SDT_PROBE_DEFINE1(futex, futex_wait, return, "int");
136 LIN_SDT_PROBE_DEFINE3(futex, futex_atomic_op, entry, "struct thread *",
137     "int", "uint32_t");
138 LIN_SDT_PROBE_DEFINE4(futex, futex_atomic_op, decoded_op, "int", "int", "int",
139     "int");
140 LIN_SDT_PROBE_DEFINE0(futex, futex_atomic_op, missing_access_check);
141 LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, unimplemented_op, "int");
142 LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, unimplemented_cmp, "int");
143 LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, return, "int");
144 LIN_SDT_PROBE_DEFINE2(futex, linux_sys_futex, entry, "struct thread *",
145     "struct linux_sys_futex_args *");
146 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_clockswitch);
147 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, copyin_error, "int");
148 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, invalid_cmp_requeue_use);
149 LIN_SDT_PROBE_DEFINE3(futex, linux_sys_futex, debug_wait, "uint32_t *",
150     "uint32_t", "uint32_t");
151 LIN_SDT_PROBE_DEFINE4(futex, linux_sys_futex, debug_wait_value_neq,
152     "uint32_t *", "uint32_t", "int", "uint32_t");
153 LIN_SDT_PROBE_DEFINE3(futex, linux_sys_futex, debug_wake, "uint32_t *",
154     "uint32_t", "uint32_t");
155 LIN_SDT_PROBE_DEFINE5(futex, linux_sys_futex, debug_cmp_requeue, "uint32_t *",
156     "uint32_t", "uint32_t", "uint32_t *", "struct l_timespec *");
157 LIN_SDT_PROBE_DEFINE2(futex, linux_sys_futex, debug_cmp_requeue_value_neq,
158     "uint32_t", "int");
159 LIN_SDT_PROBE_DEFINE5(futex, linux_sys_futex, debug_wake_op, "uint32_t *",
160     "int", "uint32_t", "uint32_t *", "uint32_t");
161 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unhandled_efault);
162 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_lock_pi);
163 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_unlock_pi);
164 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_trylock_pi);
165 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, deprecated_requeue);
166 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_wait_requeue_pi);
167 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_cmp_requeue_pi);
168 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, unknown_operation, "int");
169 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, return, "int");
170 LIN_SDT_PROBE_DEFINE2(futex, linux_set_robust_list, entry, "struct thread *",
171     "struct linux_set_robust_list_args *");
172 LIN_SDT_PROBE_DEFINE0(futex, linux_set_robust_list, size_error);
173 LIN_SDT_PROBE_DEFINE1(futex, linux_set_robust_list, return, "int");
174 LIN_SDT_PROBE_DEFINE2(futex, linux_get_robust_list, entry, "struct thread *",
175     "struct linux_get_robust_list_args *");
176 LIN_SDT_PROBE_DEFINE1(futex, linux_get_robust_list, copyout_error, "int");
177 LIN_SDT_PROBE_DEFINE1(futex, linux_get_robust_list, return, "int");
178 LIN_SDT_PROBE_DEFINE3(futex, handle_futex_death, entry,
179     "struct linux_emuldata *", "uint32_t *", "unsigned int");
180 LIN_SDT_PROBE_DEFINE1(futex, handle_futex_death, copyin_error, "int");
181 LIN_SDT_PROBE_DEFINE1(futex, handle_futex_death, return, "int");
182 LIN_SDT_PROBE_DEFINE3(futex, fetch_robust_entry, entry,
183     "struct linux_robust_list **", "struct linux_robust_list **",
184     "unsigned int *");
185 LIN_SDT_PROBE_DEFINE1(futex, fetch_robust_entry, copyin_error, "int");
186 LIN_SDT_PROBE_DEFINE1(futex, fetch_robust_entry, return, "int");
187 LIN_SDT_PROBE_DEFINE2(futex, release_futexes, entry, "struct thread *",
188     "struct linux_emuldata *");
189 LIN_SDT_PROBE_DEFINE1(futex, release_futexes, copyin_error, "int");
190 LIN_SDT_PROBE_DEFINE0(futex, release_futexes, return);
191 
192 struct futex;
193 
194 struct waiting_proc {
195 	uint32_t	wp_flags;
196 	struct futex	*wp_futex;
197 	TAILQ_ENTRY(waiting_proc) wp_list;
198 };
199 
200 struct futex {
201 	struct mtx	f_lck;
202 	uint32_t	*f_uaddr;	/* user-supplied value, for debug */
203 	struct umtx_key	f_key;
204 	uint32_t	f_refcount;
205 	uint32_t	f_bitset;
206 	LIST_ENTRY(futex) f_list;
207 	TAILQ_HEAD(lf_waiting_proc, waiting_proc) f_waiting_proc;
208 };
209 
210 #define FUTEX_LOCK(f)		mtx_lock(&(f)->f_lck)
211 #define FUTEX_LOCKED(f)		mtx_owned(&(f)->f_lck)
212 #define FUTEX_UNLOCK(f)		mtx_unlock(&(f)->f_lck)
213 #define FUTEX_INIT(f)		do { \
214 				    mtx_init(&(f)->f_lck, "ftlk", NULL, \
215 					MTX_DUPOK); \
216 				    LIN_SDT_PROBE1(futex, futex, create, \
217 					&(f)->f_lck); \
218 				} while (0)
219 #define FUTEX_DESTROY(f)	do { \
220 				    LIN_SDT_PROBE1(futex, futex, destroy, \
221 					&(f)->f_lck); \
222 				    mtx_destroy(&(f)->f_lck); \
223 				} while (0)
224 #define FUTEX_ASSERT_LOCKED(f)	mtx_assert(&(f)->f_lck, MA_OWNED)
225 #define FUTEX_ASSERT_UNLOCKED(f) mtx_assert(&(f)->f_lck, MA_NOTOWNED)
226 
227 #define FUTEXES_LOCK		do { \
228 				    mtx_lock(&futex_mtx); \
229 				    LIN_SDT_PROBE1(locks, futex_mtx, \
230 					locked, &futex_mtx); \
231 				} while (0)
232 #define FUTEXES_UNLOCK		do { \
233 				    LIN_SDT_PROBE1(locks, futex_mtx, \
234 					unlock, &futex_mtx); \
235 				    mtx_unlock(&futex_mtx); \
236 				} while (0)
237 
238 /* flags for futex_get() */
239 #define FUTEX_CREATE_WP		0x1	/* create waiting_proc */
240 #define FUTEX_DONTCREATE	0x2	/* don't create futex if not exists */
241 #define FUTEX_DONTEXISTS	0x4	/* return EINVAL if futex exists */
242 #define	FUTEX_SHARED		0x8	/* shared futex */
243 #define	FUTEX_DONTLOCK		0x10	/* don't lock futex */
244 
245 /* wp_flags */
246 #define FUTEX_WP_REQUEUED	0x1	/* wp requeued - wp moved from wp_list
247 					 * of futex where thread sleep to wp_list
248 					 * of another futex.
249 					 */
250 #define FUTEX_WP_REMOVED	0x2	/* wp is woken up and removed from futex
251 					 * wp_list to prevent double wakeup.
252 					 */
253 
254 static void futex_put(struct futex *, struct waiting_proc *);
255 static int futex_get0(uint32_t *, struct futex **f, uint32_t);
256 static int futex_get(uint32_t *, struct waiting_proc **, struct futex **,
257     uint32_t);
258 static int futex_sleep(struct futex *, struct waiting_proc *, struct timespec *);
259 static int futex_wake(struct futex *, int, uint32_t);
260 static int futex_requeue(struct futex *, int, struct futex *, int);
261 static int futex_copyin_timeout(int, struct l_timespec *, int,
262     struct timespec *);
263 static int futex_wait(struct futex *, struct waiting_proc *, struct timespec *,
264     uint32_t);
265 static void futex_lock(struct futex *);
266 static void futex_unlock(struct futex *);
267 static int futex_atomic_op(struct thread *, int, uint32_t *);
268 static int handle_futex_death(struct linux_emuldata *, uint32_t *,
269     unsigned int);
270 static int fetch_robust_entry(struct linux_robust_list **,
271     struct linux_robust_list **, unsigned int *);
272 
273 static int
futex_copyin_timeout(int op,struct l_timespec * luts,int clockrt,struct timespec * ts)274 futex_copyin_timeout(int op, struct l_timespec *luts, int clockrt,
275     struct timespec *ts)
276 {
277 	struct l_timespec lts;
278 	struct timespec kts;
279 	int error;
280 
281 	error = copyin(luts, &lts, sizeof(lts));
282 	if (error)
283 		return (error);
284 
285 	error = linux_to_native_timespec(ts, &lts);
286 	if (error)
287 		return (error);
288 	if (clockrt) {
289 		nanotime(&kts);
290 		timespecsub(ts, &kts, ts);
291 	} else if (op == LINUX_FUTEX_WAIT_BITSET) {
292 		nanouptime(&kts);
293 		timespecsub(ts, &kts, ts);
294 	}
295 	return (error);
296 }
297 
298 static void
futex_put(struct futex * f,struct waiting_proc * wp)299 futex_put(struct futex *f, struct waiting_proc *wp)
300 {
301 	LIN_SDT_PROBE2(futex, futex_put, entry, f, wp);
302 
303 	if (wp != NULL) {
304 		if ((wp->wp_flags & FUTEX_WP_REMOVED) == 0)
305 			TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
306 		free(wp, M_FUTEX_WP);
307 	}
308 
309 	FUTEXES_LOCK;
310 	if (--f->f_refcount == 0) {
311 		LIST_REMOVE(f, f_list);
312 		FUTEXES_UNLOCK;
313 		if (FUTEX_LOCKED(f))
314 			futex_unlock(f);
315 
316 		LIN_SDT_PROBE3(futex, futex_put, destroy, f->f_uaddr,
317 		    f->f_refcount, f->f_key.shared);
318 		LINUX_CTR3(sys_futex, "futex_put destroy uaddr %p ref %d "
319 		    "shared %d", f->f_uaddr, f->f_refcount, f->f_key.shared);
320 		umtx_key_release(&f->f_key);
321 		FUTEX_DESTROY(f);
322 		free(f, M_FUTEX);
323 
324 		LIN_SDT_PROBE0(futex, futex_put, return);
325 		return;
326 	}
327 
328 	LIN_SDT_PROBE3(futex, futex_put, unlock, f->f_uaddr, f->f_refcount,
329 	    f->f_key.shared);
330 	LINUX_CTR3(sys_futex, "futex_put uaddr %p ref %d shared %d",
331 	    f->f_uaddr, f->f_refcount, f->f_key.shared);
332 	if (FUTEX_LOCKED(f))
333 		futex_unlock(f);
334 	FUTEXES_UNLOCK;
335 
336 	LIN_SDT_PROBE0(futex, futex_put, return);
337 }
338 
339 static int
futex_get0(uint32_t * uaddr,struct futex ** newf,uint32_t flags)340 futex_get0(uint32_t *uaddr, struct futex **newf, uint32_t flags)
341 {
342 	struct futex *f, *tmpf;
343 	struct umtx_key key;
344 	int error;
345 
346 	LIN_SDT_PROBE3(futex, futex_get0, entry, uaddr, newf, flags);
347 
348 	*newf = tmpf = NULL;
349 
350 	error = umtx_key_get(uaddr, TYPE_FUTEX, (flags & FUTEX_SHARED) ?
351 	    AUTO_SHARE : THREAD_SHARE, &key);
352 	if (error) {
353 		LIN_SDT_PROBE1(futex, futex_get0, umtx_key_get_error, error);
354 		LIN_SDT_PROBE1(futex, futex_get0, return, error);
355 		return (error);
356 	}
357 retry:
358 	FUTEXES_LOCK;
359 	LIST_FOREACH(f, &futex_list, f_list) {
360 		if (umtx_key_match(&f->f_key, &key)) {
361 			if (tmpf != NULL) {
362 				if (FUTEX_LOCKED(tmpf))
363 					futex_unlock(tmpf);
364 				FUTEX_DESTROY(tmpf);
365 				free(tmpf, M_FUTEX);
366 			}
367 			if (flags & FUTEX_DONTEXISTS) {
368 				FUTEXES_UNLOCK;
369 				umtx_key_release(&key);
370 
371 				LIN_SDT_PROBE1(futex, futex_get0, return,
372 				    EINVAL);
373 				return (EINVAL);
374 			}
375 
376 			/*
377 			 * Increment refcount of the found futex to
378 			 * prevent it from deallocation before FUTEX_LOCK()
379 			 */
380 			++f->f_refcount;
381 			FUTEXES_UNLOCK;
382 			umtx_key_release(&key);
383 
384 			if ((flags & FUTEX_DONTLOCK) == 0)
385 				futex_lock(f);
386 			*newf = f;
387 			LIN_SDT_PROBE3(futex, futex_get0, shared, uaddr,
388 			    f->f_refcount, f->f_key.shared);
389 			LINUX_CTR3(sys_futex, "futex_get uaddr %p ref %d shared %d",
390 			    uaddr, f->f_refcount, f->f_key.shared);
391 
392 			LIN_SDT_PROBE1(futex, futex_get0, return, 0);
393 			return (0);
394 		}
395 	}
396 
397 	if (flags & FUTEX_DONTCREATE) {
398 		FUTEXES_UNLOCK;
399 		umtx_key_release(&key);
400 		LIN_SDT_PROBE1(futex, futex_get0, null, uaddr);
401 		LINUX_CTR1(sys_futex, "futex_get uaddr %p null", uaddr);
402 
403 		LIN_SDT_PROBE1(futex, futex_get0, return, 0);
404 		return (0);
405 	}
406 
407 	if (tmpf == NULL) {
408 		FUTEXES_UNLOCK;
409 		tmpf = malloc(sizeof(*tmpf), M_FUTEX, M_WAITOK | M_ZERO);
410 		tmpf->f_uaddr = uaddr;
411 		tmpf->f_key = key;
412 		tmpf->f_refcount = 1;
413 		tmpf->f_bitset = FUTEX_BITSET_MATCH_ANY;
414 		FUTEX_INIT(tmpf);
415 		TAILQ_INIT(&tmpf->f_waiting_proc);
416 
417 		/*
418 		 * Lock the new futex before an insert into the futex_list
419 		 * to prevent futex usage by other.
420 		 */
421 		if ((flags & FUTEX_DONTLOCK) == 0)
422 			futex_lock(tmpf);
423 		goto retry;
424 	}
425 
426 	LIST_INSERT_HEAD(&futex_list, tmpf, f_list);
427 	FUTEXES_UNLOCK;
428 
429 	LIN_SDT_PROBE3(futex, futex_get0, new, uaddr, tmpf->f_refcount,
430 	    tmpf->f_key.shared);
431 	LINUX_CTR3(sys_futex, "futex_get uaddr %p ref %d shared %d new",
432 	    uaddr, tmpf->f_refcount, tmpf->f_key.shared);
433 	*newf = tmpf;
434 
435 	LIN_SDT_PROBE1(futex, futex_get0, return, 0);
436 	return (0);
437 }
438 
439 static int
futex_get(uint32_t * uaddr,struct waiting_proc ** wp,struct futex ** f,uint32_t flags)440 futex_get(uint32_t *uaddr, struct waiting_proc **wp, struct futex **f,
441     uint32_t flags)
442 {
443 	int error;
444 
445 	LIN_SDT_PROBE3(futex, futex_get, entry, uaddr, wp, f);
446 
447 	if (flags & FUTEX_CREATE_WP) {
448 		*wp = malloc(sizeof(struct waiting_proc), M_FUTEX_WP, M_WAITOK);
449 		(*wp)->wp_flags = 0;
450 	}
451 	error = futex_get0(uaddr, f, flags);
452 	if (error) {
453 		LIN_SDT_PROBE0(futex, futex_get, error);
454 
455 		if (flags & FUTEX_CREATE_WP)
456 			free(*wp, M_FUTEX_WP);
457 
458 		LIN_SDT_PROBE1(futex, futex_get, return, error);
459 		return (error);
460 	}
461 	if (flags & FUTEX_CREATE_WP) {
462 		TAILQ_INSERT_HEAD(&(*f)->f_waiting_proc, *wp, wp_list);
463 		(*wp)->wp_futex = *f;
464 	}
465 
466 	LIN_SDT_PROBE1(futex, futex_get, return, error);
467 	return (error);
468 }
469 
470 static inline void
futex_lock(struct futex * f)471 futex_lock(struct futex *f)
472 {
473 
474 	LINUX_CTR3(sys_futex, "futex_lock uaddr %p ref %d shared %d",
475 	    f->f_uaddr, f->f_refcount, f->f_key.shared);
476 	FUTEX_ASSERT_UNLOCKED(f);
477 	FUTEX_LOCK(f);
478 }
479 
480 static inline void
futex_unlock(struct futex * f)481 futex_unlock(struct futex *f)
482 {
483 
484 	LINUX_CTR3(sys_futex, "futex_unlock uaddr %p ref %d shared %d",
485 	    f->f_uaddr, f->f_refcount, f->f_key.shared);
486 	FUTEX_ASSERT_LOCKED(f);
487 	FUTEX_UNLOCK(f);
488 }
489 
490 static int
futex_sleep(struct futex * f,struct waiting_proc * wp,struct timespec * ts)491 futex_sleep(struct futex *f, struct waiting_proc *wp, struct timespec *ts)
492 {
493 	struct timespec uts;
494 	sbintime_t sbt, prec, tmp;
495 	time_t over;
496 	int error;
497 
498 	FUTEX_ASSERT_LOCKED(f);
499 	if (ts != NULL) {
500 		uts = *ts;
501 		if (uts.tv_sec > INT32_MAX / 2) {
502 			over = uts.tv_sec - INT32_MAX / 2;
503 			uts.tv_sec -= over;
504 		}
505 		tmp = tstosbt(uts);
506 		if (TIMESEL(&sbt, tmp))
507 			sbt += tc_tick_sbt;
508 		sbt += tmp;
509 		prec = tmp;
510 		prec >>= tc_precexp;
511 	} else {
512 		sbt = 0;
513 		prec = 0;
514 	}
515 	LIN_SDT_PROBE3(futex, futex_sleep, entry, f, wp, sbt);
516 	LINUX_CTR4(sys_futex, "futex_sleep enter uaddr %p wp %p timo %ld ref %d",
517 	    f->f_uaddr, wp, sbt, f->f_refcount);
518 
519 	error = msleep_sbt(wp, &f->f_lck, PCATCH, "futex", sbt, prec, C_ABSOLUTE);
520 	if (wp->wp_flags & FUTEX_WP_REQUEUED) {
521 		KASSERT(f != wp->wp_futex, ("futex != wp_futex"));
522 
523 		if (error) {
524 			LIN_SDT_PROBE5(futex, futex_sleep, requeue_error, error,
525 			    f->f_uaddr, wp, wp->wp_futex->f_uaddr,
526 			    wp->wp_futex->f_refcount);
527 		}
528 
529 		LINUX_CTR5(sys_futex, "futex_sleep out error %d uaddr %p wp"
530 		    " %p requeued uaddr %p ref %d",
531 		    error, f->f_uaddr, wp, wp->wp_futex->f_uaddr,
532 		    wp->wp_futex->f_refcount);
533 		futex_put(f, NULL);
534 		f = wp->wp_futex;
535 		futex_lock(f);
536 	} else {
537 		if (error) {
538 			LIN_SDT_PROBE3(futex, futex_sleep, sleep_error, error,
539 			    f->f_uaddr, wp);
540 		}
541 		LINUX_CTR3(sys_futex, "futex_sleep out error %d uaddr %p wp %p",
542 		    error, f->f_uaddr, wp);
543 	}
544 
545 	futex_put(f, wp);
546 
547 	LIN_SDT_PROBE1(futex, futex_sleep, return, error);
548 	return (error);
549 }
550 
551 static int
futex_wake(struct futex * f,int n,uint32_t bitset)552 futex_wake(struct futex *f, int n, uint32_t bitset)
553 {
554 	struct waiting_proc *wp, *wpt;
555 	int count = 0;
556 
557 	LIN_SDT_PROBE3(futex, futex_wake, entry, f, n, bitset);
558 
559 	if (bitset == 0) {
560 		LIN_SDT_PROBE1(futex, futex_wake, return, EINVAL);
561 		return (EINVAL);
562 	}
563 
564 	FUTEX_ASSERT_LOCKED(f);
565 	TAILQ_FOREACH_SAFE(wp, &f->f_waiting_proc, wp_list, wpt) {
566 		LIN_SDT_PROBE3(futex, futex_wake, iterate, f->f_uaddr, wp,
567 		    f->f_refcount);
568 		LINUX_CTR3(sys_futex, "futex_wake uaddr %p wp %p ref %d",
569 		    f->f_uaddr, wp, f->f_refcount);
570 		/*
571 		 * Unless we find a matching bit in
572 		 * the bitset, continue searching.
573 		 */
574 		if (!(wp->wp_futex->f_bitset & bitset))
575 			continue;
576 
577 		wp->wp_flags |= FUTEX_WP_REMOVED;
578 		TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
579 		LIN_SDT_PROBE1(futex, futex_wake, wakeup, wp);
580 		wakeup_one(wp);
581 		if (++count == n)
582 			break;
583 	}
584 
585 	LIN_SDT_PROBE1(futex, futex_wake, return, count);
586 	return (count);
587 }
588 
589 static int
futex_requeue(struct futex * f,int nrwake,struct futex * f2,int nrrequeue)590 futex_requeue(struct futex *f, int nrwake, struct futex *f2,
591     int nrrequeue)
592 {
593 	struct waiting_proc *wp, *wpt;
594 	int count = 0;
595 
596 	LIN_SDT_PROBE4(futex, futex_requeue, entry, f, nrwake, f2, nrrequeue);
597 
598 	FUTEX_ASSERT_LOCKED(f);
599 	FUTEX_ASSERT_LOCKED(f2);
600 
601 	TAILQ_FOREACH_SAFE(wp, &f->f_waiting_proc, wp_list, wpt) {
602 		if (++count <= nrwake) {
603 			LINUX_CTR2(sys_futex, "futex_req_wake uaddr %p wp %p",
604 			    f->f_uaddr, wp);
605 			wp->wp_flags |= FUTEX_WP_REMOVED;
606 			TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
607 			LIN_SDT_PROBE1(futex, futex_requeue, wakeup, wp);
608 			wakeup_one(wp);
609 		} else {
610 			LIN_SDT_PROBE3(futex, futex_requeue, requeue,
611 			    f->f_uaddr, wp, f2->f_uaddr);
612 			LINUX_CTR3(sys_futex, "futex_requeue uaddr %p wp %p to %p",
613 			    f->f_uaddr, wp, f2->f_uaddr);
614 			wp->wp_flags |= FUTEX_WP_REQUEUED;
615 			/* Move wp to wp_list of f2 futex */
616 			TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
617 			TAILQ_INSERT_HEAD(&f2->f_waiting_proc, wp, wp_list);
618 
619 			/*
620 			 * Thread which sleeps on wp after waking should
621 			 * acquire f2 lock, so increment refcount of f2 to
622 			 * prevent it from premature deallocation.
623 			 */
624 			wp->wp_futex = f2;
625 			FUTEXES_LOCK;
626 			++f2->f_refcount;
627 			FUTEXES_UNLOCK;
628 			if (count - nrwake >= nrrequeue)
629 				break;
630 		}
631 	}
632 
633 	LIN_SDT_PROBE1(futex, futex_requeue, return, count);
634 	return (count);
635 }
636 
637 static int
futex_wait(struct futex * f,struct waiting_proc * wp,struct timespec * ts,uint32_t bitset)638 futex_wait(struct futex *f, struct waiting_proc *wp, struct timespec *ts,
639     uint32_t bitset)
640 {
641 	int error;
642 
643 	LIN_SDT_PROBE4(futex, futex_wait, entry, f, wp, ts, bitset);
644 
645 	if (bitset == 0) {
646 		LIN_SDT_PROBE1(futex, futex_wait, return, EINVAL);
647 		futex_put(f, wp);
648 		return (EINVAL);
649 	}
650 
651 	f->f_bitset = bitset;
652 	error = futex_sleep(f, wp, ts);
653 	if (error)
654 		LIN_SDT_PROBE1(futex, futex_wait, sleep_error, error);
655 	if (error == EWOULDBLOCK)
656 		error = ETIMEDOUT;
657 
658 	LIN_SDT_PROBE1(futex, futex_wait, return, error);
659 	return (error);
660 }
661 
662 static int
futex_atomic_op(struct thread * td,int encoded_op,uint32_t * uaddr)663 futex_atomic_op(struct thread *td, int encoded_op, uint32_t *uaddr)
664 {
665 	int op = (encoded_op >> 28) & 7;
666 	int cmp = (encoded_op >> 24) & 15;
667 	int oparg = (encoded_op << 8) >> 20;
668 	int cmparg = (encoded_op << 20) >> 20;
669 	int oldval = 0, ret;
670 
671 	LIN_SDT_PROBE3(futex, futex_atomic_op, entry, td, encoded_op, uaddr);
672 
673 	if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
674 		oparg = 1 << oparg;
675 
676 	LIN_SDT_PROBE4(futex, futex_atomic_op, decoded_op, op, cmp, oparg,
677 	    cmparg);
678 
679 	/* XXX: Linux verifies access here and returns EFAULT */
680 	LIN_SDT_PROBE0(futex, futex_atomic_op, missing_access_check);
681 
682 	switch (op) {
683 	case FUTEX_OP_SET:
684 		ret = futex_xchgl(oparg, uaddr, &oldval);
685 		break;
686 	case FUTEX_OP_ADD:
687 		ret = futex_addl(oparg, uaddr, &oldval);
688 		break;
689 	case FUTEX_OP_OR:
690 		ret = futex_orl(oparg, uaddr, &oldval);
691 		break;
692 	case FUTEX_OP_ANDN:
693 		ret = futex_andl(~oparg, uaddr, &oldval);
694 		break;
695 	case FUTEX_OP_XOR:
696 		ret = futex_xorl(oparg, uaddr, &oldval);
697 		break;
698 	default:
699 		LIN_SDT_PROBE1(futex, futex_atomic_op, unimplemented_op, op);
700 		ret = -ENOSYS;
701 		break;
702 	}
703 
704 	if (ret) {
705 		LIN_SDT_PROBE1(futex, futex_atomic_op, return, ret);
706 		return (ret);
707 	}
708 
709 	switch (cmp) {
710 	case FUTEX_OP_CMP_EQ:
711 		ret = (oldval == cmparg);
712 		break;
713 	case FUTEX_OP_CMP_NE:
714 		ret = (oldval != cmparg);
715 		break;
716 	case FUTEX_OP_CMP_LT:
717 		ret = (oldval < cmparg);
718 		break;
719 	case FUTEX_OP_CMP_GE:
720 		ret = (oldval >= cmparg);
721 		break;
722 	case FUTEX_OP_CMP_LE:
723 		ret = (oldval <= cmparg);
724 		break;
725 	case FUTEX_OP_CMP_GT:
726 		ret = (oldval > cmparg);
727 		break;
728 	default:
729 		LIN_SDT_PROBE1(futex, futex_atomic_op, unimplemented_cmp, cmp);
730 		ret = -ENOSYS;
731 	}
732 
733 	LIN_SDT_PROBE1(futex, futex_atomic_op, return, ret);
734 	return (ret);
735 }
736 
737 int
linux_sys_futex(struct thread * td,struct linux_sys_futex_args * args)738 linux_sys_futex(struct thread *td, struct linux_sys_futex_args *args)
739 {
740 	int clockrt, nrwake, nrrequeue, op_ret, ret;
741 	struct linux_pemuldata *pem;
742 	struct waiting_proc *wp;
743 	struct futex *f, *f2;
744 	struct timespec uts, *ts;
745 	int error, save;
746 	uint32_t flags, val;
747 
748 	LIN_SDT_PROBE2(futex, linux_sys_futex, entry, td, args);
749 
750 	if (args->op & LINUX_FUTEX_PRIVATE_FLAG) {
751 		flags = 0;
752 		args->op &= ~LINUX_FUTEX_PRIVATE_FLAG;
753 	} else
754 		flags = FUTEX_SHARED;
755 
756 	/*
757 	 * Currently support for switching between CLOCK_MONOTONIC and
758 	 * CLOCK_REALTIME is not present. However Linux forbids the use of
759 	 * FUTEX_CLOCK_REALTIME with any op except FUTEX_WAIT_BITSET and
760 	 * FUTEX_WAIT_REQUEUE_PI.
761 	 */
762 	clockrt = args->op & LINUX_FUTEX_CLOCK_REALTIME;
763 	args->op = args->op & ~LINUX_FUTEX_CLOCK_REALTIME;
764 	if (clockrt && args->op != LINUX_FUTEX_WAIT_BITSET &&
765 		args->op != LINUX_FUTEX_WAIT_REQUEUE_PI) {
766 		LIN_SDT_PROBE0(futex, linux_sys_futex,
767 		    unimplemented_clockswitch);
768 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
769 		return (ENOSYS);
770 	}
771 
772 	error = 0;
773 	f = f2 = NULL;
774 
775 	switch (args->op) {
776 	case LINUX_FUTEX_WAIT:
777 		args->val3 = FUTEX_BITSET_MATCH_ANY;
778 		/* FALLTHROUGH */
779 
780 	case LINUX_FUTEX_WAIT_BITSET:
781 		LIN_SDT_PROBE3(futex, linux_sys_futex, debug_wait, args->uaddr,
782 		    args->val, args->val3);
783 		LINUX_CTR3(sys_futex, "WAIT uaddr %p val 0x%x bitset 0x%x",
784 		    args->uaddr, args->val, args->val3);
785 
786 		if (args->timeout != NULL) {
787 			error = futex_copyin_timeout(args->op, args->timeout,
788 			    clockrt, &uts);
789 			if (error) {
790 				LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
791 				    error);
792 				LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
793 				return (error);
794 			}
795 			ts = &uts;
796 		} else
797 			ts = NULL;
798 
799 retry0:
800 		error = futex_get(args->uaddr, &wp, &f,
801 		    flags | FUTEX_CREATE_WP);
802 		if (error) {
803 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
804 			return (error);
805 		}
806 
807 		error = copyin_nofault(args->uaddr, &val, sizeof(val));
808 		if (error) {
809 			futex_put(f, wp);
810 			error = copyin(args->uaddr, &val, sizeof(val));
811 			if (error == 0)
812 				goto retry0;
813 			LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
814 			    error);
815 			LINUX_CTR1(sys_futex, "WAIT copyin failed %d",
816 			    error);
817 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
818 			return (error);
819 		}
820 		if (val != args->val) {
821 			LIN_SDT_PROBE4(futex, linux_sys_futex,
822 			    debug_wait_value_neq, args->uaddr, args->val, val,
823 			    args->val3);
824 			LINUX_CTR3(sys_futex,
825 			    "WAIT uaddr %p val 0x%x != uval 0x%x",
826 			    args->uaddr, args->val, val);
827 			futex_put(f, wp);
828 
829 			LIN_SDT_PROBE1(futex, linux_sys_futex, return,
830 			    EWOULDBLOCK);
831 			return (EWOULDBLOCK);
832 		}
833 
834 		error = futex_wait(f, wp, ts, args->val3);
835 		break;
836 
837 	case LINUX_FUTEX_WAKE:
838 		args->val3 = FUTEX_BITSET_MATCH_ANY;
839 		/* FALLTHROUGH */
840 
841 	case LINUX_FUTEX_WAKE_BITSET:
842 		LIN_SDT_PROBE3(futex, linux_sys_futex, debug_wake, args->uaddr,
843 		    args->val, args->val3);
844 		LINUX_CTR3(sys_futex, "WAKE uaddr %p nrwake 0x%x bitset 0x%x",
845 		    args->uaddr, args->val, args->val3);
846 
847 		error = futex_get(args->uaddr, NULL, &f,
848 		    flags | FUTEX_DONTCREATE);
849 		if (error) {
850 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
851 			return (error);
852 		}
853 
854 		if (f == NULL) {
855 			td->td_retval[0] = 0;
856 
857 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
858 			return (error);
859 		}
860 		td->td_retval[0] = futex_wake(f, args->val, args->val3);
861 		futex_put(f, NULL);
862 		break;
863 
864 	case LINUX_FUTEX_CMP_REQUEUE:
865 		LIN_SDT_PROBE5(futex, linux_sys_futex, debug_cmp_requeue,
866 		    args->uaddr, args->val, args->val3, args->uaddr2,
867 		    args->timeout);
868 		LINUX_CTR5(sys_futex, "CMP_REQUEUE uaddr %p "
869 		    "nrwake 0x%x uval 0x%x uaddr2 %p nrequeue 0x%x",
870 		    args->uaddr, args->val, args->val3, args->uaddr2,
871 		    args->timeout);
872 
873 		/*
874 		 * Linux allows this, we would not, it is an incorrect
875 		 * usage of declared ABI, so return EINVAL.
876 		 */
877 		if (args->uaddr == args->uaddr2) {
878 			LIN_SDT_PROBE0(futex, linux_sys_futex,
879 			    invalid_cmp_requeue_use);
880 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, EINVAL);
881 			return (EINVAL);
882 		}
883 
884 		nrrequeue = (int)(unsigned long)args->timeout;
885 		nrwake = args->val;
886 		/*
887 		 * Sanity check to prevent signed integer overflow,
888 		 * see Linux CVE-2018-6927
889 		 */
890 		if (nrwake < 0 || nrrequeue < 0)
891 			return (EINVAL);
892 
893 retry1:
894 		error = futex_get(args->uaddr, NULL, &f, flags | FUTEX_DONTLOCK);
895 		if (error) {
896 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
897 			return (error);
898 		}
899 
900 		/*
901 		 * To avoid deadlocks return EINVAL if second futex
902 		 * exists at this time.
903 		 *
904 		 * Glibc fall back to FUTEX_WAKE in case of any error
905 		 * returned by FUTEX_CMP_REQUEUE.
906 		 */
907 		error = futex_get(args->uaddr2, NULL, &f2,
908 		    flags | FUTEX_DONTEXISTS | FUTEX_DONTLOCK);
909 		if (error) {
910 			futex_put(f, NULL);
911 
912 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
913 			return (error);
914 		}
915 		futex_lock(f);
916 		futex_lock(f2);
917 		error = copyin_nofault(args->uaddr, &val, sizeof(val));
918 		if (error) {
919 			futex_put(f2, NULL);
920 			futex_put(f, NULL);
921 			error = copyin(args->uaddr, &val, sizeof(val));
922 			if (error == 0)
923 				goto retry1;
924 			LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
925 			    error);
926 			LINUX_CTR1(sys_futex, "CMP_REQUEUE copyin failed %d",
927 			    error);
928 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
929 			return (error);
930 		}
931 		if (val != args->val3) {
932 			LIN_SDT_PROBE2(futex, linux_sys_futex,
933 			    debug_cmp_requeue_value_neq, args->val, val);
934 			LINUX_CTR2(sys_futex, "CMP_REQUEUE val 0x%x != uval 0x%x",
935 			    args->val, val);
936 			futex_put(f2, NULL);
937 			futex_put(f, NULL);
938 
939 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, EAGAIN);
940 			return (EAGAIN);
941 		}
942 
943 		td->td_retval[0] = futex_requeue(f, nrwake, f2, nrrequeue);
944 		futex_put(f2, NULL);
945 		futex_put(f, NULL);
946 		break;
947 
948 	case LINUX_FUTEX_WAKE_OP:
949 		LIN_SDT_PROBE5(futex, linux_sys_futex, debug_wake_op,
950 		    args->uaddr, args->op, args->val, args->uaddr2, args->val3);
951 		LINUX_CTR5(sys_futex, "WAKE_OP "
952 		    "uaddr %p nrwake 0x%x uaddr2 %p op 0x%x nrwake2 0x%x",
953 		    args->uaddr, args->val, args->uaddr2, args->val3,
954 		    args->timeout);
955 
956 		if (args->uaddr == args->uaddr2) {
957 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, EINVAL);
958 			return (EINVAL);
959 		}
960 
961 retry2:
962 		error = futex_get(args->uaddr, NULL, &f, flags | FUTEX_DONTLOCK);
963 		if (error) {
964 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
965 			return (error);
966 		}
967 
968 		error = futex_get(args->uaddr2, NULL, &f2, flags | FUTEX_DONTLOCK);
969 		if (error) {
970 			futex_put(f, NULL);
971 
972 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
973 			return (error);
974 		}
975 		futex_lock(f);
976 		futex_lock(f2);
977 
978 		/*
979 		 * This function returns positive number as results and
980 		 * negative as errors
981 		 */
982 		save = vm_fault_disable_pagefaults();
983 		op_ret = futex_atomic_op(td, args->val3, args->uaddr2);
984 		vm_fault_enable_pagefaults(save);
985 
986 		LINUX_CTR2(sys_futex, "WAKE_OP atomic_op uaddr %p ret 0x%x",
987 		    args->uaddr, op_ret);
988 
989 		if (op_ret < 0) {
990 			if (f2 != NULL)
991 				futex_put(f2, NULL);
992 			futex_put(f, NULL);
993 			error = copyin(args->uaddr2, &val, sizeof(val));
994 			if (error == 0)
995 				goto retry2;
996 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
997 			return (error);
998 		}
999 
1000 		ret = futex_wake(f, args->val, args->val3);
1001 
1002 		if (op_ret > 0) {
1003 			op_ret = 0;
1004 			nrwake = (int)(unsigned long)args->timeout;
1005 
1006 			if (f2 != NULL)
1007 				op_ret += futex_wake(f2, nrwake, args->val3);
1008 			else
1009 				op_ret += futex_wake(f, nrwake, args->val3);
1010 			ret += op_ret;
1011 
1012 		}
1013 		if (f2 != NULL)
1014 			futex_put(f2, NULL);
1015 		futex_put(f, NULL);
1016 		td->td_retval[0] = ret;
1017 		break;
1018 
1019 	case LINUX_FUTEX_LOCK_PI:
1020 		/* not yet implemented */
1021 		pem = pem_find(td->td_proc);
1022 		if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
1023 			linux_msg(td, "unsupported FUTEX_LOCK_PI");
1024 			pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
1025 			LIN_SDT_PROBE0(futex, linux_sys_futex,
1026 			    unimplemented_lock_pi);
1027 		}
1028 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1029 		return (ENOSYS);
1030 
1031 	case LINUX_FUTEX_UNLOCK_PI:
1032 		/* not yet implemented */
1033 		pem = pem_find(td->td_proc);
1034 		if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
1035 			linux_msg(td, "unsupported FUTEX_UNLOCK_PI");
1036 			pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
1037 			LIN_SDT_PROBE0(futex, linux_sys_futex,
1038 			    unimplemented_unlock_pi);
1039 		}
1040 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1041 		return (ENOSYS);
1042 
1043 	case LINUX_FUTEX_TRYLOCK_PI:
1044 		/* not yet implemented */
1045 		pem = pem_find(td->td_proc);
1046 		if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
1047 			linux_msg(td, "unsupported FUTEX_TRYLOCK_PI");
1048 			pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
1049 			LIN_SDT_PROBE0(futex, linux_sys_futex,
1050 			    unimplemented_trylock_pi);
1051 		}
1052 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1053 		return (ENOSYS);
1054 
1055 	case LINUX_FUTEX_REQUEUE:
1056 		/*
1057 		 * Glibc does not use this operation since version 2.3.3,
1058 		 * as it is racy and replaced by FUTEX_CMP_REQUEUE operation.
1059 		 * Glibc versions prior to 2.3.3 fall back to FUTEX_WAKE when
1060 		 * FUTEX_REQUEUE returned EINVAL.
1061 		 */
1062 		pem = pem_find(td->td_proc);
1063 		if ((pem->flags & LINUX_XDEPR_REQUEUEOP) == 0) {
1064 			linux_msg(td, "unsupported FUTEX_REQUEUE");
1065 			pem->flags |= LINUX_XDEPR_REQUEUEOP;
1066 			LIN_SDT_PROBE0(futex, linux_sys_futex,
1067 			    deprecated_requeue);
1068 		}
1069 
1070 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, EINVAL);
1071 		return (EINVAL);
1072 
1073 	case LINUX_FUTEX_WAIT_REQUEUE_PI:
1074 		/* not yet implemented */
1075 		pem = pem_find(td->td_proc);
1076 		if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
1077 			linux_msg(td, "unsupported FUTEX_WAIT_REQUEUE_PI");
1078 			pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
1079 			LIN_SDT_PROBE0(futex, linux_sys_futex,
1080 			    unimplemented_wait_requeue_pi);
1081 		}
1082 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1083 		return (ENOSYS);
1084 
1085 	case LINUX_FUTEX_CMP_REQUEUE_PI:
1086 		/* not yet implemented */
1087 		pem = pem_find(td->td_proc);
1088 		if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
1089 			linux_msg(td, "unsupported FUTEX_CMP_REQUEUE_PI");
1090 			pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
1091 			LIN_SDT_PROBE0(futex, linux_sys_futex,
1092 			    unimplemented_cmp_requeue_pi);
1093 		}
1094 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1095 		return (ENOSYS);
1096 
1097 	default:
1098 		linux_msg(td, "unsupported futex op %d", args->op);
1099 		LIN_SDT_PROBE1(futex, linux_sys_futex, unknown_operation,
1100 		    args->op);
1101 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1102 		return (ENOSYS);
1103 	}
1104 
1105 	LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
1106 	return (error);
1107 }
1108 
1109 int
linux_set_robust_list(struct thread * td,struct linux_set_robust_list_args * args)1110 linux_set_robust_list(struct thread *td, struct linux_set_robust_list_args *args)
1111 {
1112 	struct linux_emuldata *em;
1113 
1114 	LIN_SDT_PROBE2(futex, linux_set_robust_list, entry, td, args);
1115 
1116 	if (args->len != sizeof(struct linux_robust_list_head)) {
1117 		LIN_SDT_PROBE0(futex, linux_set_robust_list, size_error);
1118 		LIN_SDT_PROBE1(futex, linux_set_robust_list, return, EINVAL);
1119 		return (EINVAL);
1120 	}
1121 
1122 	em = em_find(td);
1123 	em->robust_futexes = args->head;
1124 
1125 	LIN_SDT_PROBE1(futex, linux_set_robust_list, return, 0);
1126 	return (0);
1127 }
1128 
1129 int
linux_get_robust_list(struct thread * td,struct linux_get_robust_list_args * args)1130 linux_get_robust_list(struct thread *td, struct linux_get_robust_list_args *args)
1131 {
1132 	struct linux_emuldata *em;
1133 	struct linux_robust_list_head *head;
1134 	l_size_t len = sizeof(struct linux_robust_list_head);
1135 	struct thread *td2;
1136 	int error = 0;
1137 
1138 	LIN_SDT_PROBE2(futex, linux_get_robust_list, entry, td, args);
1139 
1140 	if (!args->pid) {
1141 		em = em_find(td);
1142 		KASSERT(em != NULL, ("get_robust_list: emuldata notfound.\n"));
1143 		head = em->robust_futexes;
1144 	} else {
1145 		td2 = tdfind(args->pid, -1);
1146 		if (td2 == NULL) {
1147 			LIN_SDT_PROBE1(futex, linux_get_robust_list, return,
1148 			    ESRCH);
1149 			return (ESRCH);
1150 		}
1151 		if (SV_PROC_ABI(td2->td_proc) != SV_ABI_LINUX) {
1152 			LIN_SDT_PROBE1(futex, linux_get_robust_list, return,
1153 			    EPERM);
1154 			PROC_UNLOCK(td2->td_proc);
1155 			return (EPERM);
1156 		}
1157 
1158 		em = em_find(td2);
1159 		KASSERT(em != NULL, ("get_robust_list: emuldata notfound.\n"));
1160 		/* XXX: ptrace? */
1161 		if (priv_check(td, PRIV_CRED_SETUID) ||
1162 		    priv_check(td, PRIV_CRED_SETEUID) ||
1163 		    p_candebug(td, td2->td_proc)) {
1164 			PROC_UNLOCK(td2->td_proc);
1165 
1166 			LIN_SDT_PROBE1(futex, linux_get_robust_list, return,
1167 			    EPERM);
1168 			return (EPERM);
1169 		}
1170 		head = em->robust_futexes;
1171 
1172 		PROC_UNLOCK(td2->td_proc);
1173 	}
1174 
1175 	error = copyout(&len, args->len, sizeof(l_size_t));
1176 	if (error) {
1177 		LIN_SDT_PROBE1(futex, linux_get_robust_list, copyout_error,
1178 		    error);
1179 		LIN_SDT_PROBE1(futex, linux_get_robust_list, return, EFAULT);
1180 		return (EFAULT);
1181 	}
1182 
1183 	error = copyout(&head, args->head, sizeof(head));
1184 	if (error) {
1185 		LIN_SDT_PROBE1(futex, linux_get_robust_list, copyout_error,
1186 		    error);
1187 	}
1188 
1189 	LIN_SDT_PROBE1(futex, linux_get_robust_list, return, error);
1190 	return (error);
1191 }
1192 
1193 static int
handle_futex_death(struct linux_emuldata * em,uint32_t * uaddr,unsigned int pi)1194 handle_futex_death(struct linux_emuldata *em, uint32_t *uaddr,
1195     unsigned int pi)
1196 {
1197 	uint32_t uval, nval, mval;
1198 	struct futex *f;
1199 	int error;
1200 
1201 	LIN_SDT_PROBE3(futex, handle_futex_death, entry, em, uaddr, pi);
1202 
1203 retry:
1204 	error = copyin(uaddr, &uval, 4);
1205 	if (error) {
1206 		LIN_SDT_PROBE1(futex, handle_futex_death, copyin_error, error);
1207 		LIN_SDT_PROBE1(futex, handle_futex_death, return, EFAULT);
1208 		return (EFAULT);
1209 	}
1210 	if ((uval & FUTEX_TID_MASK) == em->em_tid) {
1211 		mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
1212 		nval = casuword32(uaddr, uval, mval);
1213 
1214 		if (nval == -1) {
1215 			LIN_SDT_PROBE1(futex, handle_futex_death, return,
1216 			    EFAULT);
1217 			return (EFAULT);
1218 		}
1219 
1220 		if (nval != uval)
1221 			goto retry;
1222 
1223 		if (!pi && (uval & FUTEX_WAITERS)) {
1224 			error = futex_get(uaddr, NULL, &f,
1225 			    FUTEX_DONTCREATE | FUTEX_SHARED);
1226 			if (error) {
1227 				LIN_SDT_PROBE1(futex, handle_futex_death,
1228 				    return, error);
1229 				return (error);
1230 			}
1231 			if (f != NULL) {
1232 				futex_wake(f, 1, FUTEX_BITSET_MATCH_ANY);
1233 				futex_put(f, NULL);
1234 			}
1235 		}
1236 	}
1237 
1238 	LIN_SDT_PROBE1(futex, handle_futex_death, return, 0);
1239 	return (0);
1240 }
1241 
1242 static int
fetch_robust_entry(struct linux_robust_list ** entry,struct linux_robust_list ** head,unsigned int * pi)1243 fetch_robust_entry(struct linux_robust_list **entry,
1244     struct linux_robust_list **head, unsigned int *pi)
1245 {
1246 	l_ulong uentry;
1247 	int error;
1248 
1249 	LIN_SDT_PROBE3(futex, fetch_robust_entry, entry, entry, head, pi);
1250 
1251 	error = copyin((const void *)head, &uentry, sizeof(l_ulong));
1252 	if (error) {
1253 		LIN_SDT_PROBE1(futex, fetch_robust_entry, copyin_error, error);
1254 		LIN_SDT_PROBE1(futex, fetch_robust_entry, return, EFAULT);
1255 		return (EFAULT);
1256 	}
1257 
1258 	*entry = (void *)(uentry & ~1UL);
1259 	*pi = uentry & 1;
1260 
1261 	LIN_SDT_PROBE1(futex, fetch_robust_entry, return, 0);
1262 	return (0);
1263 }
1264 
1265 /* This walks the list of robust futexes releasing them. */
1266 void
release_futexes(struct thread * td,struct linux_emuldata * em)1267 release_futexes(struct thread *td, struct linux_emuldata *em)
1268 {
1269 	struct linux_robust_list_head *head = NULL;
1270 	struct linux_robust_list *entry, *next_entry, *pending;
1271 	unsigned int limit = 2048, pi, next_pi, pip;
1272 	l_long futex_offset;
1273 	int rc, error;
1274 
1275 	LIN_SDT_PROBE2(futex, release_futexes, entry, td, em);
1276 
1277 	head = em->robust_futexes;
1278 
1279 	if (head == NULL) {
1280 		LIN_SDT_PROBE0(futex, release_futexes, return);
1281 		return;
1282 	}
1283 
1284 	if (fetch_robust_entry(&entry, PTRIN(&head->list.next), &pi)) {
1285 		LIN_SDT_PROBE0(futex, release_futexes, return);
1286 		return;
1287 	}
1288 
1289 	error = copyin(&head->futex_offset, &futex_offset,
1290 	    sizeof(futex_offset));
1291 	if (error) {
1292 		LIN_SDT_PROBE1(futex, release_futexes, copyin_error, error);
1293 		LIN_SDT_PROBE0(futex, release_futexes, return);
1294 		return;
1295 	}
1296 
1297 	if (fetch_robust_entry(&pending, PTRIN(&head->pending_list), &pip)) {
1298 		LIN_SDT_PROBE0(futex, release_futexes, return);
1299 		return;
1300 	}
1301 
1302 	while (entry != &head->list) {
1303 		rc = fetch_robust_entry(&next_entry, PTRIN(&entry->next), &next_pi);
1304 
1305 		if (entry != pending)
1306 			if (handle_futex_death(em,
1307 			    (uint32_t *)((caddr_t)entry + futex_offset), pi)) {
1308 				LIN_SDT_PROBE0(futex, release_futexes, return);
1309 				return;
1310 			}
1311 		if (rc) {
1312 			LIN_SDT_PROBE0(futex, release_futexes, return);
1313 			return;
1314 		}
1315 
1316 		entry = next_entry;
1317 		pi = next_pi;
1318 
1319 		if (!--limit)
1320 			break;
1321 
1322 		sched_relinquish(curthread);
1323 	}
1324 
1325 	if (pending)
1326 		handle_futex_death(em, (uint32_t *)((caddr_t)pending + futex_offset), pip);
1327 
1328 	LIN_SDT_PROBE0(futex, release_futexes, return);
1329 }
1330