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