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