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