feat(libkernel): robust umutexes and UMTX_OP_ROBUST_LISTS in _umtx_op #19453

Merged
SHOnnay merged 7 commits from upstream-pr/19453 into main 2026-11-21 19:57:39 +00:00
SHOnnay commented 2026-11-13 10:52:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SHOnnay on 2026-11-13T10:52:32Z.


What

Robust umutexes and _umtx_op UMTX_OP_ROBUST_LISTS (26) no longer throw. They follow FreeBSD stable/11 at 5ed7eb0d97ba: sys/kern/kern_umtx.c, sys/sys/umtx.h, sys/sys/_umtx.h, sys/kern/kern_thr.c, and lib/libthr/thread/thr_mutex.c for the userland side. The kern_umtx.c licence notice is copied verbatim at the top of SyncOnAddress.cpp, followed by a "Derived from" line.

Values

  • UMUTEX_RB_OWNERDEAD is UMUTEX_CONTESTED | 0x10 and UMUTEX_RB_NOTRECOV is UMUTEX_CONTESTED | 0x11.
  • EOWNERDEAD is 96 and ENOTRECOVERABLE is 95.
  • m_rb_lnk is a pointer at offset 16 of struct umutex, static-asserted.
  • AnyPS5 thread ids start at 100000, so 0x10 and 0x11 never match a real owner.

Locking (lock and trylock, as do_lock_normal)

  • An RB_OWNERDEAD owner is taken over by a compare-and-swap to id | CONTESTED; the call returns EOWNERDEAD with ownership.
  • An RB_NOTRECOV owner returns ENOTRECOVERABLE.
  • MUTEX_WAIT treats both values as free, and MUTEX_WAKE handles both as do_wake_umutex does.

Unlocking (as umtx_unlock_val)

  • A thread-exit unlock leaves RB_OWNERDEAD.
  • Otherwise UMUTEX_NONCONSISTENT leaves RB_NOTRECOV. A condition-variable wait goes through the same path.

Operation 26 (as __umtx_op_robust_lists): a size above 24 gives EINVAL and a failed copy gives EFAULT. The three list heads are kept per thread.

Thread exit

  • ThreadLifecycle::ReleaseRobustUmutexes ports the robust part of umtx_thread_cleanup (umtx_cleanup_rb_list and umtx_handle_rb).
  • It walks the list and the private list and handles the inactive mutex, with FreeBSD's 1000-entry cap and diagnostics. Every guest read goes through GuestCopyIn.
  • A non-robust mutex in a list stops the walk with EINVAL, as on FreeBSD.
  • It is called from finishThread in Pthread/src/Thread.cpp after the key destructors and before joiners are released, as sys_thr_exit runs umtx_thread_exit before waking the thread state. finishThread covers both a normal return and scePthreadExit.

Still throwing: robust umutexes that are also priority-inherit or priority-protect. Lists registered by the main thread or by adopted host threads are not released at exit. Both are in the narrowed debt entry.

Related: #19450 adds MUTEX_WAKE2, which throws for UMUTEX_ROBUST. FreeBSD treats a robust mutex as a normal one there, so whichever of the two merges second should drop that throw.

Tested

macOS 27 on Apple M1. libkernel does not build on macOS, so the registered guest_umtx_op test was not run; a Linux or Windows run is needed.

Harness. A temporary harness, not committed, built the unchanged SyncOnAddress.cpp and TimedWait.cpp with the extended GuestUmtxOp.cpp and minimal stand-ins. Its stand-in scePthreadCreate runs the entry on a std::thread and then calls ReleaseRobustUmutexes, so the one-line finishThread hook itself was not exercised.

Run Result
Branch, x86-64 under Rosetta exit 0
Branch, x86-64 ASan + UBSan exit 0
Branch, arm64 ASan + UBSan (x86 asm load replaced by memcpy in a copy) exit 0
Branch, arm64 TSan no reports in the new code
Base code with the new test not implemented: _umtx_op operation 26, exit 134

TSan reports one race, in the existing ConditionWaitReleasesTheMutexAndWakes: a plain read of hasWaiters.

New test cases

  • A scePthreadCreate thread registers robust lists, locks two linked robust mutexes and one held only as inactive, and exits. All three are left RB_OWNERDEAD.
  • The next lock gets EOWNERDEAD with ownership. Clearing NONCONSISTENT and unlocking frees the mutex.
  • Unlocking with NONCONSISTENT set gives RB_NOTRECOV. After that, lock and trylock return ENOTRECOVERABLE, and wait and wake return 0.
  • A thread blocked in MUTEX_LOCK gets EOWNERDEAD when the owner exits.
  • MUTEX_WAKE releases a waiter on an RB_OWNERDEAD mutex.
  • Operation 26 refuses size 25.
  • A non-robust mutex in the list stays owned.

Mutation check: 9 of 10 hand mutations failed the harness, by abort or hang:

  • no RB_OWNERDEAD takeover
  • NONCONSISTENT ignored
  • inactive mutex skipped
  • exit unlock leaves the mutex unowned
  • lock returning 0 instead of the error
  • no size check
  • RB_NOTRECOV not checked
  • wait ignoring the robust values
  • wake skipping the robust values

The tenth, dropping a contested-bit guard in wake, was equivalent, so I removed that guard.

$ python3 tools/check_conventions.py --base upstream/main
All conventions checks passed.
$ python3 core/libs/tests/test_public_headers.py --audit-only
413 libraries, 34505 exports: unique declarations and umbrella coverage verified

Overlap. No open PR implements robust support.

  • #17887 leaves one trivial conflict next to UMTX_OP_MEASURED_NOOP, and the _umtx_op debt line needs a hand merge.
  • #16873 applies cleanly.
  • #19450 touches the same constants block and debt line.

Not verified:

  • the console
  • a registered Linux or Windows run
  • the real finishThread hook path

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5294203317 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19453). Originally posted by [SHOnnay](https://github.com/SHOnnay) on 2026-11-13T10:52:32Z. --- ### What Robust umutexes and `_umtx_op` `UMTX_OP_ROBUST_LISTS` (26) no longer throw. They follow FreeBSD stable/11 at [`5ed7eb0d97ba`](https://github.com/freebsd/freebsd-src/tree/5ed7eb0d97ba4436218e810f61bd059acba984c2): `sys/kern/kern_umtx.c`, `sys/sys/umtx.h`, `sys/sys/_umtx.h`, `sys/kern/kern_thr.c`, and `lib/libthr/thread/thr_mutex.c` for the userland side. The kern_umtx.c licence notice is copied verbatim at the top of `SyncOnAddress.cpp`, followed by a "Derived from" line. **Values** - `UMUTEX_RB_OWNERDEAD` is `UMUTEX_CONTESTED | 0x10` and `UMUTEX_RB_NOTRECOV` is `UMUTEX_CONTESTED | 0x11`. - `EOWNERDEAD` is 96 and `ENOTRECOVERABLE` is 95. - `m_rb_lnk` is a pointer at offset 16 of `struct umutex`, static-asserted. - AnyPS5 thread ids start at 100000, so 0x10 and 0x11 never match a real owner. **Locking** (lock and trylock, as `do_lock_normal`) - An `RB_OWNERDEAD` owner is taken over by a compare-and-swap to `id | CONTESTED`; the call returns `EOWNERDEAD` with ownership. - An `RB_NOTRECOV` owner returns `ENOTRECOVERABLE`. - `MUTEX_WAIT` treats both values as free, and `MUTEX_WAKE` handles both as `do_wake_umutex` does. **Unlocking** (as `umtx_unlock_val`) - A thread-exit unlock leaves `RB_OWNERDEAD`. - Otherwise `UMUTEX_NONCONSISTENT` leaves `RB_NOTRECOV`. A condition-variable wait goes through the same path. **Operation 26** (as `__umtx_op_robust_lists`): a size above 24 gives `EINVAL` and a failed copy gives `EFAULT`. The three list heads are kept per thread. **Thread exit** - `ThreadLifecycle::ReleaseRobustUmutexes` ports the robust part of `umtx_thread_cleanup` (`umtx_cleanup_rb_list` and `umtx_handle_rb`). - It walks the list and the private list and handles the inactive mutex, with FreeBSD's 1000-entry cap and diagnostics. Every guest read goes through `GuestCopyIn`. - A non-robust mutex in a list stops the walk with `EINVAL`, as on FreeBSD. - It is called from `finishThread` in `Pthread/src/Thread.cpp` after the key destructors and before joiners are released, as `sys_thr_exit` runs `umtx_thread_exit` before waking the thread state. `finishThread` covers both a normal return and `scePthreadExit`. **Still throwing:** robust umutexes that are also priority-inherit or priority-protect. Lists registered by the main thread or by adopted host threads are not released at exit. Both are in the narrowed debt entry. **Related:** #19450 adds `MUTEX_WAKE2`, which throws for `UMUTEX_ROBUST`. FreeBSD treats a robust mutex as a normal one there, so whichever of the two merges second should drop that throw. ### Tested macOS 27 on Apple M1. libkernel does not build on macOS, so the registered `guest_umtx_op` test was **not run**; a Linux or Windows run is needed. **Harness.** A temporary harness, not committed, built the unchanged `SyncOnAddress.cpp` and `TimedWait.cpp` with the extended `GuestUmtxOp.cpp` and minimal stand-ins. Its stand-in `scePthreadCreate` runs the entry on a `std::thread` and then calls `ReleaseRobustUmutexes`, so the one-line `finishThread` hook itself was not exercised. | Run | Result | |---|---| | Branch, x86-64 under Rosetta | exit 0 | | Branch, x86-64 ASan + UBSan | exit 0 | | Branch, arm64 ASan + UBSan (x86 `asm` load replaced by `memcpy` in a copy) | exit 0 | | Branch, arm64 TSan | no reports in the new code | | Base code with the new test | `not implemented: _umtx_op operation 26`, exit 134 | TSan reports one race, in the existing `ConditionWaitReleasesTheMutexAndWakes`: a plain read of `hasWaiters`. **New test cases** - A `scePthreadCreate` thread registers robust lists, locks two linked robust mutexes and one held only as inactive, and exits. All three are left `RB_OWNERDEAD`. - The next lock gets `EOWNERDEAD` with ownership. Clearing `NONCONSISTENT` and unlocking frees the mutex. - Unlocking with `NONCONSISTENT` set gives `RB_NOTRECOV`. After that, lock and trylock return `ENOTRECOVERABLE`, and wait and wake return 0. - A thread blocked in `MUTEX_LOCK` gets `EOWNERDEAD` when the owner exits. - `MUTEX_WAKE` releases a waiter on an `RB_OWNERDEAD` mutex. - Operation 26 refuses size 25. - A non-robust mutex in the list stays owned. **Mutation check:** 9 of 10 hand mutations failed the harness, by abort or hang: - no `RB_OWNERDEAD` takeover - `NONCONSISTENT` ignored - inactive mutex skipped - exit unlock leaves the mutex unowned - lock returning 0 instead of the error - no size check - `RB_NOTRECOV` not checked - wait ignoring the robust values - wake skipping the robust values The tenth, dropping a contested-bit guard in wake, was equivalent, so I removed that guard. ```text $ python3 tools/check_conventions.py --base upstream/main All conventions checks passed. $ python3 core/libs/tests/test_public_headers.py --audit-only 413 libraries, 34505 exports: unique declarations and umbrella coverage verified ``` **Overlap.** No open PR implements robust support. - #17887 leaves one trivial conflict next to `UMTX_OP_MEASURED_NOOP`, and the `_umtx_op` debt line needs a hand merge. - #16873 applies cleanly. - #19450 touches the same constants block and debt line. **Not verified:** - the console - a registered Linux or Windows run - the real `finishThread` hook path ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!19453
No description provided.