1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2009-2021 Dmitry Chagin <dchagin@FreeBSD.org> 5 * Copyright (c) 2008 Roman Divacky 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/imgact.h> 34 #include <sys/imgact_elf.h> 35 #include <sys/ktr.h> 36 #include <sys/lock.h> 37 #include <sys/mutex.h> 38 #include <sys/priv.h> 39 #include <sys/proc.h> 40 #include <sys/sched.h> 41 #include <sys/sysent.h> 42 #include <sys/vnode.h> 43 #include <sys/umtxvar.h> 44 45 #ifdef COMPAT_LINUX32 46 #include <machine/../linux32/linux.h> 47 #include <machine/../linux32/linux32_proto.h> 48 #else 49 #include <machine/../linux/linux.h> 50 #include <machine/../linux/linux_proto.h> 51 #endif 52 #include <compat/linux/linux_emul.h> 53 #include <compat/linux/linux_futex.h> 54 #include <compat/linux/linux_misc.h> 55 #include <compat/linux/linux_time.h> 56 #include <compat/linux/linux_util.h> 57 58 #define FUTEX_SHARED 0x8 /* shared futex */ 59 #define FUTEX_UNOWNED 0 60 61 #define GET_SHARED(a) (a->flags & FUTEX_SHARED) ? AUTO_SHARE : THREAD_SHARE 62 63 static int futex_atomic_op(struct thread *, int, uint32_t *, int *); 64 static int handle_futex_death(struct thread *td, struct linux_emuldata *, 65 uint32_t *, unsigned int, bool); 66 static int fetch_robust_entry(struct linux_robust_list **, 67 struct linux_robust_list **, unsigned int *); 68 69 struct linux_futex_args { 70 uint32_t *uaddr; 71 int32_t op; 72 uint32_t flags; 73 bool clockrt; 74 uint32_t val; 75 struct timespec *ts; 76 uint32_t *uaddr2; 77 uint32_t val3; 78 bool val3_compare; 79 struct timespec kts; 80 }; 81 82 static inline int futex_key_get(const void *, int, int, struct umtx_key *); 83 static void linux_umtx_abs_timeout_init(struct umtx_abs_timeout *, 84 struct linux_futex_args *); 85 static int linux_futex(struct thread *, struct linux_futex_args *); 86 static int linux_futex_wait(struct thread *, struct linux_futex_args *); 87 static int linux_futex_wake(struct thread *, struct linux_futex_args *); 88 static int linux_futex_requeue(struct thread *, struct linux_futex_args *); 89 static int linux_futex_wakeop(struct thread *, struct linux_futex_args *); 90 static int linux_futex_lock_pi(struct thread *, bool, struct linux_futex_args *); 91 static int linux_futex_unlock_pi(struct thread *, bool, 92 struct linux_futex_args *); 93 static int futex_wake_pi(struct thread *, uint32_t *, bool); 94 95 static int 96 futex_key_get(const void *uaddr, int type, int share, struct umtx_key *key) 97 { 98 99 /* Check that futex address is a 32bit aligned. */ 100 if (!__is_aligned(uaddr, sizeof(uint32_t))) 101 return (EINVAL); 102 return (umtx_key_get(uaddr, type, share, key)); 103 } 104 105 int 106 futex_wake(struct thread *td, uint32_t *uaddr, int val, bool shared) 107 { 108 struct linux_futex_args args; 109 110 bzero(&args, sizeof(args)); 111 args.op = LINUX_FUTEX_WAKE; 112 args.uaddr = uaddr; 113 args.flags = shared == true ? FUTEX_SHARED : 0; 114 args.val = val; 115 args.val3 = FUTEX_BITSET_MATCH_ANY; 116 117 return (linux_futex_wake(td, &args)); 118 } 119 120 static int 121 futex_wake_pi(struct thread *td, uint32_t *uaddr, bool shared) 122 { 123 struct linux_futex_args args; 124 125 bzero(&args, sizeof(args)); 126 args.op = LINUX_FUTEX_UNLOCK_PI; 127 args.uaddr = uaddr; 128 args.flags = shared == true ? FUTEX_SHARED : 0; 129 130 return (linux_futex_unlock_pi(td, true, &args)); 131 } 132 133 static int 134 futex_atomic_op(struct thread *td, int encoded_op, uint32_t *uaddr, 135 int *res) 136 { 137 int op = (encoded_op >> 28) & 7; 138 int cmp = (encoded_op >> 24) & 15; 139 int oparg = (encoded_op << 8) >> 20; 140 int cmparg = (encoded_op << 20) >> 20; 141 int oldval = 0, ret; 142 143 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) 144 oparg = 1 << oparg; 145 146 switch (op) { 147 case FUTEX_OP_SET: 148 ret = futex_xchgl(oparg, uaddr, &oldval); 149 break; 150 case FUTEX_OP_ADD: 151 ret = futex_addl(oparg, uaddr, &oldval); 152 break; 153 case FUTEX_OP_OR: 154 ret = futex_orl(oparg, uaddr, &oldval); 155 break; 156 case FUTEX_OP_ANDN: 157 ret = futex_andl(~oparg, uaddr, &oldval); 158 break; 159 case FUTEX_OP_XOR: 160 ret = futex_xorl(oparg, uaddr, &oldval); 161 break; 162 default: 163 ret = ENOSYS; 164 break; 165 } 166 167 if (ret != 0) 168 return (ret); 169 170 switch (cmp) { 171 case FUTEX_OP_CMP_EQ: 172 *res = (oldval == cmparg); 173 break; 174 case FUTEX_OP_CMP_NE: 175 *res = (oldval != cmparg); 176 break; 177 case FUTEX_OP_CMP_LT: 178 *res = (oldval < cmparg); 179 break; 180 case FUTEX_OP_CMP_GE: 181 *res = (oldval >= cmparg); 182 break; 183 case FUTEX_OP_CMP_LE: 184 *res = (oldval <= cmparg); 185 break; 186 case FUTEX_OP_CMP_GT: 187 *res = (oldval > cmparg); 188 break; 189 default: 190 ret = ENOSYS; 191 } 192 193 return (ret); 194 } 195 196 static int 197 linux_futex(struct thread *td, struct linux_futex_args *args) 198 { 199 struct linux_pemuldata *pem; 200 struct proc *p; 201 202 if (args->op & LINUX_FUTEX_PRIVATE_FLAG) { 203 args->flags = 0; 204 args->op &= ~LINUX_FUTEX_PRIVATE_FLAG; 205 } else 206 args->flags = FUTEX_SHARED; 207 208 args->clockrt = args->op & LINUX_FUTEX_CLOCK_REALTIME; 209 args->op = args->op & ~LINUX_FUTEX_CLOCK_REALTIME; 210 211 if (args->clockrt && 212 args->op != LINUX_FUTEX_WAIT_BITSET && 213 args->op != LINUX_FUTEX_WAIT_REQUEUE_PI && 214 args->op != LINUX_FUTEX_LOCK_PI2) 215 return (ENOSYS); 216 217 switch (args->op) { 218 case LINUX_FUTEX_WAIT: 219 args->val3 = FUTEX_BITSET_MATCH_ANY; 220 /* FALLTHROUGH */ 221 222 case LINUX_FUTEX_WAIT_BITSET: 223 LINUX_CTR3(sys_futex, "WAIT uaddr %p val 0x%x bitset 0x%x", 224 args->uaddr, args->val, args->val3); 225 226 return (linux_futex_wait(td, args)); 227 228 case LINUX_FUTEX_WAKE: 229 args->val3 = FUTEX_BITSET_MATCH_ANY; 230 /* FALLTHROUGH */ 231 232 case LINUX_FUTEX_WAKE_BITSET: 233 LINUX_CTR3(sys_futex, "WAKE uaddr %p nrwake 0x%x bitset 0x%x", 234 args->uaddr, args->val, args->val3); 235 236 return (linux_futex_wake(td, args)); 237 238 case LINUX_FUTEX_REQUEUE: 239 /* 240 * Glibc does not use this operation since version 2.3.3, 241 * as it is racy and replaced by FUTEX_CMP_REQUEUE operation. 242 * Glibc versions prior to 2.3.3 fall back to FUTEX_WAKE when 243 * FUTEX_REQUEUE returned EINVAL. 244 */ 245 pem = pem_find(td->td_proc); 246 if ((pem->flags & LINUX_XDEPR_REQUEUEOP) == 0) { 247 linux_msg(td, "unsupported FUTEX_REQUEUE"); 248 pem->flags |= LINUX_XDEPR_REQUEUEOP; 249 } 250 251 /* 252 * The above is true, however musl libc does make use of the 253 * futex requeue operation, allow operation for brands which 254 * set LINUX_BI_FUTEX_REQUEUE bit of Brandinfo flags. 255 */ 256 p = td->td_proc; 257 Elf_Brandinfo *bi = p->p_elf_brandinfo; 258 if (bi == NULL || ((bi->flags & LINUX_BI_FUTEX_REQUEUE)) == 0) 259 return (EINVAL); 260 args->val3_compare = false; 261 /* FALLTHROUGH */ 262 263 case LINUX_FUTEX_CMP_REQUEUE: 264 LINUX_CTR5(sys_futex, "CMP_REQUEUE uaddr %p " 265 "nrwake 0x%x uval 0x%x uaddr2 %p nrequeue 0x%x", 266 args->uaddr, args->val, args->val3, args->uaddr2, 267 args->ts); 268 269 return (linux_futex_requeue(td, args)); 270 271 case LINUX_FUTEX_WAKE_OP: 272 LINUX_CTR5(sys_futex, "WAKE_OP " 273 "uaddr %p nrwake 0x%x uaddr2 %p op 0x%x nrwake2 0x%x", 274 args->uaddr, args->val, args->uaddr2, args->val3, 275 args->ts); 276 277 return (linux_futex_wakeop(td, args)); 278 279 case LINUX_FUTEX_LOCK_PI: 280 args->clockrt = true; 281 /* FALLTHROUGH */ 282 283 case LINUX_FUTEX_LOCK_PI2: 284 LINUX_CTR2(sys_futex, "LOCKPI uaddr %p val 0x%x", 285 args->uaddr, args->val); 286 287 return (linux_futex_lock_pi(td, false, args)); 288 289 case LINUX_FUTEX_UNLOCK_PI: 290 LINUX_CTR1(sys_futex, "UNLOCKPI uaddr %p", 291 args->uaddr); 292 293 return (linux_futex_unlock_pi(td, false, args)); 294 295 case LINUX_FUTEX_TRYLOCK_PI: 296 LINUX_CTR1(sys_futex, "TRYLOCKPI uaddr %p", 297 args->uaddr); 298 299 return (linux_futex_lock_pi(td, true, args)); 300 301 /* 302 * Current implementation of FUTEX_WAIT_REQUEUE_PI and FUTEX_CMP_REQUEUE_PI 303 * can't be used anymore to implement conditional variables. 304 * A detailed explanation can be found here: 305 * 306 * https://sourceware.org/bugzilla/show_bug.cgi?id=13165 307 * and here http://austingroupbugs.net/view.php?id=609 308 * 309 * And since commit 310 * https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=ed19993b5b0d05d62cc883571519a67dae481a14 311 * glibc does not use them. 312 */ 313 case LINUX_FUTEX_WAIT_REQUEUE_PI: 314 /* not yet implemented */ 315 pem = pem_find(td->td_proc); 316 if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) { 317 linux_msg(td, "unsupported FUTEX_WAIT_REQUEUE_PI"); 318 pem->flags |= LINUX_XUNSUP_FUTEXPIOP; 319 } 320 return (ENOSYS); 321 322 case LINUX_FUTEX_CMP_REQUEUE_PI: 323 /* not yet implemented */ 324 pem = pem_find(td->td_proc); 325 if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) { 326 linux_msg(td, "unsupported FUTEX_CMP_REQUEUE_PI"); 327 pem->flags |= LINUX_XUNSUP_FUTEXPIOP; 328 } 329 return (ENOSYS); 330 331 default: 332 linux_msg(td, "unsupported futex op %d", args->op); 333 return (ENOSYS); 334 } 335 } 336 337 /* 338 * pi protocol: 339 * - 0 futex word value means unlocked. 340 * - TID futex word value means locked. 341 * Userspace uses atomic ops to lock/unlock these futexes without entering the 342 * kernel. If the lock-acquire fastpath fails, (transition from 0 to TID fails), 343 * then FUTEX_LOCK_PI is called. 344 * The kernel atomically set FUTEX_WAITERS bit in the futex word value, if no 345 * other waiters exists looks up the thread that owns the futex (it has put its 346 * own TID into the futex value) and made this thread the owner of the internal 347 * pi-aware lock object (mutex). Then the kernel tries to lock the internal lock 348 * object, on which it blocks. Once it returns, it has the mutex acquired, and it 349 * sets the futex value to its own TID and returns (futex value contains 350 * FUTEX_WAITERS|TID). 351 * The unlock fastpath would fail (because the FUTEX_WAITERS bit is set) and 352 * FUTEX_UNLOCK_PI will be called. 353 * If a futex is found to be held at exit time, the kernel sets the OWNER_DIED 354 * bit of the futex word and wakes up the next futex waiter (if any), WAITERS 355 * bit is preserved (if any). 356 * If OWNER_DIED bit is set the kernel sanity checks the futex word value against 357 * the internal futex state and if correct, acquire futex. 358 */ 359 static int 360 linux_futex_lock_pi(struct thread *td, bool try, struct linux_futex_args *args) 361 { 362 struct umtx_abs_timeout timo; 363 struct linux_emuldata *em; 364 struct umtx_pi *pi, *new_pi; 365 struct thread *td1; 366 struct umtx_q *uq; 367 int error, rv; 368 uint32_t owner, old_owner; 369 370 em = em_find(td); 371 uq = td->td_umtxq; 372 error = futex_key_get(args->uaddr, TYPE_PI_FUTEX, GET_SHARED(args), 373 &uq->uq_key); 374 if (error != 0) 375 return (error); 376 if (args->ts != NULL) 377 linux_umtx_abs_timeout_init(&timo, args); 378 379 umtxq_lock(&uq->uq_key); 380 pi = umtx_pi_lookup(&uq->uq_key); 381 if (pi == NULL) { 382 new_pi = umtx_pi_alloc(M_NOWAIT); 383 if (new_pi == NULL) { 384 umtxq_unlock(&uq->uq_key); 385 new_pi = umtx_pi_alloc(M_WAITOK); 386 umtxq_lock(&uq->uq_key); 387 pi = umtx_pi_lookup(&uq->uq_key); 388 if (pi != NULL) { 389 umtx_pi_free(new_pi); 390 new_pi = NULL; 391 } 392 } 393 if (new_pi != NULL) { 394 new_pi->pi_key = uq->uq_key; 395 umtx_pi_insert(new_pi); 396 pi = new_pi; 397 } 398 } 399 umtx_pi_ref(pi); 400 umtxq_unlock(&uq->uq_key); 401 for (;;) { 402 /* Try uncontested case first. */ 403 rv = casueword32(args->uaddr, FUTEX_UNOWNED, &owner, em->em_tid); 404 /* The acquire succeeded. */ 405 if (rv == 0) { 406 error = 0; 407 break; 408 } 409 if (rv == -1) { 410 error = EFAULT; 411 break; 412 } 413 414 /* 415 * Nobody owns it, but the acquire failed. This can happen 416 * with ll/sc atomic. 417 */ 418 if (owner == FUTEX_UNOWNED) { 419 error = thread_check_susp(td, true); 420 if (error != 0) 421 break; 422 continue; 423 } 424 425 /* 426 * Avoid overwriting a possible error from sleep due 427 * to the pending signal with suspension check result. 428 */ 429 if (error == 0) { 430 error = thread_check_susp(td, true); 431 if (error != 0) 432 break; 433 } 434 435 /* The futex word at *uaddr is already locked by the caller. */ 436 if ((owner & FUTEX_TID_MASK) == em->em_tid) { 437 error = EDEADLK; 438 break; 439 } 440 441 /* 442 * Futex owner died, handle_futex_death() set the OWNER_DIED bit 443 * and clear tid. Try to acquire it. 444 */ 445 if ((owner & FUTEX_TID_MASK) == FUTEX_UNOWNED) { 446 old_owner = owner; 447 owner = owner & (FUTEX_WAITERS | FUTEX_OWNER_DIED); 448 owner |= em->em_tid; 449 rv = casueword32(args->uaddr, old_owner, &owner, owner); 450 if (rv == -1) { 451 error = EFAULT; 452 break; 453 } 454 if (rv == 1) { 455 if (error == 0) { 456 error = thread_check_susp(td, true); 457 if (error != 0) 458 break; 459 } 460 461 /* 462 * If this failed the lock could 463 * changed, restart. 464 */ 465 continue; 466 } 467 468 umtxq_lock(&uq->uq_key); 469 umtxq_busy(&uq->uq_key); 470 error = umtx_pi_claim(pi, td); 471 umtxq_unbusy(&uq->uq_key); 472 umtxq_unlock(&uq->uq_key); 473 if (error != 0) { 474 /* 475 * Since we're going to return an 476 * error, restore the futex to its 477 * previous, unowned state to avoid 478 * compounding the problem. 479 */ 480 (void)casuword32(args->uaddr, owner, old_owner); 481 } 482 break; 483 } 484 485 /* 486 * Inconsistent state: OWNER_DIED is set and tid is not 0. 487 * Linux does some checks of futex state, we return EINVAL, 488 * as the user space can take care of this. 489 */ 490 if ((owner & FUTEX_OWNER_DIED) != FUTEX_UNOWNED) { 491 error = EINVAL; 492 break; 493 } 494 495 if (try != 0) { 496 error = EBUSY; 497 break; 498 } 499 500 /* 501 * If we caught a signal, we have retried and now 502 * exit immediately. 503 */ 504 if (error != 0) 505 break; 506 507 umtxq_lock(&uq->uq_key); 508 umtxq_busy(&uq->uq_key); 509 umtxq_unlock(&uq->uq_key); 510 511 /* 512 * Set the contested bit so that a release in user space knows 513 * to use the system call for unlock. If this fails either some 514 * one else has acquired the lock or it has been released. 515 */ 516 rv = casueword32(args->uaddr, owner, &owner, 517 owner | FUTEX_WAITERS); 518 if (rv == -1) { 519 umtxq_unbusy_unlocked(&uq->uq_key); 520 error = EFAULT; 521 break; 522 } 523 if (rv == 1) { 524 umtxq_unbusy_unlocked(&uq->uq_key); 525 error = thread_check_susp(td, true); 526 if (error != 0) 527 break; 528 529 /* 530 * The lock changed and we need to retry or we 531 * lost a race to the thread unlocking the umtx. 532 */ 533 continue; 534 } 535 536 /* 537 * Substitute Linux thread id by native thread id to 538 * avoid refactoring code of umtxq_sleep_pi(). 539 */ 540 td1 = linux_tdfind(td, owner & FUTEX_TID_MASK, -1); 541 if (td1 != NULL) { 542 owner = td1->td_tid; 543 PROC_UNLOCK(td1->td_proc); 544 } else { 545 umtxq_unbusy_unlocked(&uq->uq_key); 546 error = EINVAL; 547 break; 548 } 549 550 umtxq_lock(&uq->uq_key); 551 552 /* We set the contested bit, sleep. */ 553 error = umtxq_sleep_pi(uq, pi, owner, "futexp", 554 args->ts == NULL ? NULL : &timo, 555 (args->flags & FUTEX_SHARED) != 0); 556 if (error != 0) 557 continue; 558 559 error = thread_check_susp(td, false); 560 if (error != 0) 561 break; 562 } 563 564 umtxq_lock(&uq->uq_key); 565 umtx_pi_unref(pi); 566 umtxq_unlock(&uq->uq_key); 567 umtx_key_release(&uq->uq_key); 568 return (error); 569 } 570 571 static int 572 linux_futex_unlock_pi(struct thread *td, bool rb, struct linux_futex_args *args) 573 { 574 struct linux_emuldata *em; 575 struct umtx_key key; 576 uint32_t old, owner, new_owner; 577 int count, error; 578 579 em = em_find(td); 580 581 /* 582 * Make sure we own this mtx. 583 */ 584 error = fueword32(args->uaddr, &owner); 585 if (error == -1) 586 return (EFAULT); 587 if (!rb && (owner & FUTEX_TID_MASK) != em->em_tid) 588 return (EPERM); 589 590 error = futex_key_get(args->uaddr, TYPE_PI_FUTEX, GET_SHARED(args), &key); 591 if (error != 0) 592 return (error); 593 umtxq_lock(&key); 594 umtxq_busy(&key); 595 error = umtx_pi_drop(td, &key, rb, &count); 596 if (error != 0 || rb) { 597 umtxq_unbusy(&key); 598 umtxq_unlock(&key); 599 umtx_key_release(&key); 600 return (error); 601 } 602 umtxq_unlock(&key); 603 604 /* 605 * When unlocking the futex, it must be marked as unowned if 606 * there is zero or one thread only waiting for it. 607 * Otherwise, it must be marked as contested. 608 */ 609 if (count > 1) 610 new_owner = FUTEX_WAITERS; 611 else 612 new_owner = FUTEX_UNOWNED; 613 614 again: 615 error = casueword32(args->uaddr, owner, &old, new_owner); 616 if (error == 1) { 617 error = thread_check_susp(td, false); 618 if (error == 0) 619 goto again; 620 } 621 umtxq_unbusy_unlocked(&key); 622 umtx_key_release(&key); 623 if (error == -1) 624 return (EFAULT); 625 if (error == 0 && old != owner) 626 return (EINVAL); 627 return (error); 628 } 629 630 static int 631 linux_futex_wakeop(struct thread *td, struct linux_futex_args *args) 632 { 633 struct umtx_key key, key2; 634 int nrwake, op_ret, ret; 635 int error, count; 636 637 if (args->uaddr == args->uaddr2) 638 return (EINVAL); 639 640 error = futex_key_get(args->uaddr, TYPE_FUTEX, GET_SHARED(args), &key); 641 if (error != 0) 642 return (error); 643 error = futex_key_get(args->uaddr2, TYPE_FUTEX, GET_SHARED(args), &key2); 644 if (error != 0) { 645 umtx_key_release(&key); 646 return (error); 647 } 648 umtxq_lock(&key); 649 umtxq_busy(&key); 650 umtxq_unlock(&key); 651 error = futex_atomic_op(td, args->val3, args->uaddr2, &op_ret); 652 umtxq_lock(&key); 653 umtxq_unbusy(&key); 654 if (error != 0) 655 goto out; 656 ret = umtxq_signal_mask(&key, args->val, args->val3); 657 if (op_ret > 0) { 658 nrwake = (int)(unsigned long)args->ts; 659 umtxq_lock(&key2); 660 count = umtxq_count(&key2); 661 if (count > 0) 662 ret += umtxq_signal_mask(&key2, nrwake, args->val3); 663 else 664 ret += umtxq_signal_mask(&key, nrwake, args->val3); 665 umtxq_unlock(&key2); 666 } 667 td->td_retval[0] = ret; 668 out: 669 umtxq_unlock(&key); 670 umtx_key_release(&key2); 671 umtx_key_release(&key); 672 return (error); 673 } 674 675 static int 676 linux_futex_requeue(struct thread *td, struct linux_futex_args *args) 677 { 678 int nrwake, nrrequeue; 679 struct umtx_key key, key2; 680 int error; 681 uint32_t uval; 682 683 /* 684 * Linux allows this, we would not, it is an incorrect 685 * usage of declared ABI, so return EINVAL. 686 */ 687 if (args->uaddr == args->uaddr2) 688 return (EINVAL); 689 690 nrrequeue = (int)(unsigned long)args->ts; 691 nrwake = args->val; 692 /* 693 * Sanity check to prevent signed integer overflow, 694 * see Linux CVE-2018-6927 695 */ 696 if (nrwake < 0 || nrrequeue < 0) 697 return (EINVAL); 698 699 error = futex_key_get(args->uaddr, TYPE_FUTEX, GET_SHARED(args), &key); 700 if (error != 0) 701 return (error); 702 error = futex_key_get(args->uaddr2, TYPE_FUTEX, GET_SHARED(args), &key2); 703 if (error != 0) { 704 umtx_key_release(&key); 705 return (error); 706 } 707 umtxq_lock(&key); 708 umtxq_busy(&key); 709 umtxq_unlock(&key); 710 error = fueword32(args->uaddr, &uval); 711 if (error != 0) 712 error = EFAULT; 713 else if (args->val3_compare == true && uval != args->val3) 714 error = EWOULDBLOCK; 715 umtxq_lock(&key); 716 umtxq_unbusy(&key); 717 if (error == 0) { 718 umtxq_lock(&key2); 719 td->td_retval[0] = umtxq_requeue(&key, nrwake, &key2, nrrequeue); 720 umtxq_unlock(&key2); 721 } 722 umtxq_unlock(&key); 723 umtx_key_release(&key2); 724 umtx_key_release(&key); 725 return (error); 726 } 727 728 static int 729 linux_futex_wake(struct thread *td, struct linux_futex_args *args) 730 { 731 struct umtx_key key; 732 int error; 733 734 if (args->val3 == 0) 735 return (EINVAL); 736 737 error = futex_key_get(args->uaddr, TYPE_FUTEX, GET_SHARED(args), &key); 738 if (error != 0) 739 return (error); 740 umtxq_lock(&key); 741 td->td_retval[0] = umtxq_signal_mask(&key, args->val, args->val3); 742 umtxq_unlock(&key); 743 umtx_key_release(&key); 744 return (0); 745 } 746 747 static int 748 linux_futex_wait(struct thread *td, struct linux_futex_args *args) 749 { 750 struct umtx_abs_timeout timo; 751 struct umtx_q *uq; 752 uint32_t uval; 753 int error; 754 755 if (args->val3 == 0) 756 error = EINVAL; 757 758 uq = td->td_umtxq; 759 error = futex_key_get(args->uaddr, TYPE_FUTEX, GET_SHARED(args), 760 &uq->uq_key); 761 if (error != 0) 762 return (error); 763 if (args->ts != NULL) 764 linux_umtx_abs_timeout_init(&timo, args); 765 umtxq_lock(&uq->uq_key); 766 umtxq_busy(&uq->uq_key); 767 uq->uq_bitset = args->val3; 768 umtxq_insert(uq); 769 umtxq_unlock(&uq->uq_key); 770 error = fueword32(args->uaddr, &uval); 771 if (error != 0) 772 error = EFAULT; 773 else if (uval != args->val) 774 error = EWOULDBLOCK; 775 umtxq_lock(&uq->uq_key); 776 umtxq_unbusy(&uq->uq_key); 777 if (error == 0) { 778 error = umtxq_sleep(uq, "futex", 779 args->ts == NULL ? NULL : &timo); 780 if ((uq->uq_flags & UQF_UMTXQ) == 0) 781 error = 0; 782 else 783 umtxq_remove(uq); 784 } else if ((uq->uq_flags & UQF_UMTXQ) != 0) { 785 umtxq_remove(uq); 786 } 787 umtxq_unlock(&uq->uq_key); 788 umtx_key_release(&uq->uq_key); 789 if (error == ERESTART) 790 error = EINTR; 791 return (error); 792 } 793 794 static void 795 linux_umtx_abs_timeout_init(struct umtx_abs_timeout *timo, 796 struct linux_futex_args *args) 797 { 798 int clockid, absolute; 799 800 /* 801 * The FUTEX_CLOCK_REALTIME option bit can be employed only with the 802 * FUTEX_WAIT_BITSET, FUTEX_WAIT_REQUEUE_PI, FUTEX_LOCK_PI2. 803 * For FUTEX_WAIT, timeout is interpreted as a relative value, for other 804 * futex operations timeout is interpreted as an absolute value. 805 * If FUTEX_CLOCK_REALTIME option bit is set, the Linux kernel measures 806 * the timeout against the CLOCK_REALTIME clock, otherwise the kernel 807 * measures the timeout against the CLOCK_MONOTONIC clock. 808 */ 809 clockid = args->clockrt ? CLOCK_REALTIME : CLOCK_MONOTONIC; 810 absolute = args->op == LINUX_FUTEX_WAIT ? false : true; 811 umtx_abs_timeout_init(timo, clockid, absolute, args->ts); 812 } 813 814 int 815 linux_sys_futex(struct thread *td, struct linux_sys_futex_args *args) 816 { 817 struct linux_futex_args fargs = { 818 .uaddr = args->uaddr, 819 .op = args->op, 820 .val = args->val, 821 .ts = NULL, 822 .uaddr2 = args->uaddr2, 823 .val3 = args->val3, 824 .val3_compare = true, 825 }; 826 int error; 827 828 switch (args->op & LINUX_FUTEX_CMD_MASK) { 829 case LINUX_FUTEX_WAIT: 830 case LINUX_FUTEX_WAIT_BITSET: 831 case LINUX_FUTEX_LOCK_PI: 832 case LINUX_FUTEX_LOCK_PI2: 833 if (args->timeout != NULL) { 834 error = linux_get_timespec(&fargs.kts, args->timeout); 835 if (error != 0) 836 return (error); 837 fargs.ts = &fargs.kts; 838 } 839 break; 840 default: 841 fargs.ts = PTRIN(args->timeout); 842 } 843 return (linux_futex(td, &fargs)); 844 } 845 846 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 847 int 848 linux_sys_futex_time64(struct thread *td, 849 struct linux_sys_futex_time64_args *args) 850 { 851 struct linux_futex_args fargs = { 852 .uaddr = args->uaddr, 853 .op = args->op, 854 .val = args->val, 855 .ts = NULL, 856 .uaddr2 = args->uaddr2, 857 .val3 = args->val3, 858 .val3_compare = true, 859 }; 860 int error; 861 862 switch (args->op & LINUX_FUTEX_CMD_MASK) { 863 case LINUX_FUTEX_WAIT: 864 case LINUX_FUTEX_WAIT_BITSET: 865 case LINUX_FUTEX_LOCK_PI: 866 case LINUX_FUTEX_LOCK_PI2: 867 if (args->timeout != NULL) { 868 error = linux_get_timespec64(&fargs.kts, args->timeout); 869 if (error != 0) 870 return (error); 871 fargs.ts = &fargs.kts; 872 } 873 break; 874 default: 875 fargs.ts = PTRIN(args->timeout); 876 } 877 return (linux_futex(td, &fargs)); 878 } 879 #endif 880 881 int 882 linux_set_robust_list(struct thread *td, struct linux_set_robust_list_args *args) 883 { 884 struct linux_emuldata *em; 885 886 if (args->len != sizeof(struct linux_robust_list_head)) 887 return (EINVAL); 888 889 em = em_find(td); 890 em->robust_futexes = args->head; 891 892 return (0); 893 } 894 895 int 896 linux_get_robust_list(struct thread *td, struct linux_get_robust_list_args *args) 897 { 898 struct linux_emuldata *em; 899 struct linux_robust_list_head *head; 900 l_size_t len; 901 struct thread *td2; 902 int error; 903 904 if (!args->pid) { 905 em = em_find(td); 906 KASSERT(em != NULL, ("get_robust_list: emuldata notfound.\n")); 907 head = em->robust_futexes; 908 } else { 909 td2 = linux_tdfind(td, args->pid, -1); 910 if (td2 == NULL) 911 return (ESRCH); 912 if (SV_PROC_ABI(td2->td_proc) != SV_ABI_LINUX) { 913 PROC_UNLOCK(td2->td_proc); 914 return (EPERM); 915 } 916 917 em = em_find(td2); 918 KASSERT(em != NULL, ("get_robust_list: emuldata notfound.\n")); 919 /* XXX: ptrace? */ 920 if (priv_check(td, PRIV_CRED_SETUID) || 921 priv_check(td, PRIV_CRED_SETEUID) || 922 p_candebug(td, td2->td_proc)) { 923 PROC_UNLOCK(td2->td_proc); 924 return (EPERM); 925 } 926 head = em->robust_futexes; 927 928 PROC_UNLOCK(td2->td_proc); 929 } 930 931 len = sizeof(struct linux_robust_list_head); 932 error = copyout(&len, args->len, sizeof(l_size_t)); 933 if (error != 0) 934 return (EFAULT); 935 936 return (copyout(&head, args->head, sizeof(l_uintptr_t))); 937 } 938 939 static int 940 handle_futex_death(struct thread *td, struct linux_emuldata *em, uint32_t *uaddr, 941 unsigned int pi, bool pending_op) 942 { 943 uint32_t uval, nval, mval; 944 int error; 945 946 retry: 947 error = fueword32(uaddr, &uval); 948 if (error != 0) 949 return (EFAULT); 950 951 /* 952 * Special case for regular (non PI) futexes. The unlock path in 953 * user space has two race scenarios: 954 * 955 * 1. The unlock path releases the user space futex value and 956 * before it can execute the futex() syscall to wake up 957 * waiters it is killed. 958 * 959 * 2. A woken up waiter is killed before it can acquire the 960 * futex in user space. 961 * 962 * In both cases the TID validation below prevents a wakeup of 963 * potential waiters which can cause these waiters to block 964 * forever. 965 * 966 * In both cases it is safe to attempt waking up a potential 967 * waiter without touching the user space futex value and trying 968 * to set the OWNER_DIED bit. 969 */ 970 if (pending_op && !pi && !uval) { 971 (void)futex_wake(td, uaddr, 1, true); 972 return (0); 973 } 974 975 if ((uval & FUTEX_TID_MASK) == em->em_tid) { 976 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED; 977 error = casueword32(uaddr, uval, &nval, mval); 978 if (error == -1) 979 return (EFAULT); 980 if (error == 1) { 981 error = thread_check_susp(td, false); 982 if (error != 0) 983 return (error); 984 goto retry; 985 } 986 987 if (!pi && (uval & FUTEX_WAITERS)) { 988 error = futex_wake(td, uaddr, 1, true); 989 if (error != 0) 990 return (error); 991 } else if (pi && (uval & FUTEX_WAITERS)) { 992 error = futex_wake_pi(td, uaddr, true); 993 if (error != 0) 994 return (error); 995 } 996 } 997 998 return (0); 999 } 1000 1001 static int 1002 fetch_robust_entry(struct linux_robust_list **entry, 1003 struct linux_robust_list **head, unsigned int *pi) 1004 { 1005 l_ulong uentry; 1006 int error; 1007 1008 error = copyin((const void *)head, &uentry, sizeof(uentry)); 1009 if (error != 0) 1010 return (EFAULT); 1011 1012 *entry = (void *)(uentry & ~1UL); 1013 *pi = uentry & 1; 1014 1015 return (0); 1016 } 1017 1018 #define LINUX_HANDLE_DEATH_PENDING true 1019 #define LINUX_HANDLE_DEATH_LIST false 1020 1021 /* This walks the list of robust futexes releasing them. */ 1022 void 1023 release_futexes(struct thread *td, struct linux_emuldata *em) 1024 { 1025 struct linux_robust_list_head *head; 1026 struct linux_robust_list *entry, *next_entry, *pending; 1027 unsigned int limit = 2048, pi, next_pi, pip; 1028 uint32_t *uaddr; 1029 l_long futex_offset; 1030 int error; 1031 1032 head = em->robust_futexes; 1033 if (head == NULL) 1034 return; 1035 1036 if (fetch_robust_entry(&entry, PTRIN(&head->list.next), &pi)) 1037 return; 1038 1039 error = copyin(&head->futex_offset, &futex_offset, 1040 sizeof(futex_offset)); 1041 if (error != 0) 1042 return; 1043 1044 if (fetch_robust_entry(&pending, PTRIN(&head->pending_list), &pip)) 1045 return; 1046 1047 while (entry != &head->list) { 1048 error = fetch_robust_entry(&next_entry, PTRIN(&entry->next), 1049 &next_pi); 1050 1051 /* 1052 * A pending lock might already be on the list, so 1053 * don't process it twice. 1054 */ 1055 if (entry != pending) { 1056 uaddr = (uint32_t *)((caddr_t)entry + futex_offset); 1057 if (handle_futex_death(td, em, uaddr, pi, 1058 LINUX_HANDLE_DEATH_LIST)) 1059 return; 1060 } 1061 if (error != 0) 1062 return; 1063 1064 entry = next_entry; 1065 pi = next_pi; 1066 1067 if (!--limit) 1068 break; 1069 1070 sched_relinquish(curthread); 1071 } 1072 1073 if (pending) { 1074 uaddr = (uint32_t *)((caddr_t)pending + futex_offset); 1075 (void)handle_futex_death(td, em, uaddr, pip, 1076 LINUX_HANDLE_DEATH_PENDING); 1077 } 1078 } 1079