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