feat(libkernel): implement the umutex, condition variable and rwlock _umtx_op operations as measured on a PS5 #10789

Merged
rooklz merged 5 commits from upstream-pr/10789 into main 2026-11-02 10:26:14 +00:00
rooklz commented 2026-10-25 00:50:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-25T00:50:31Z.


What

_umtx_op threw for the umutex, condition variable and read-write lock operations. 26 of the 67 title and homebrew executables I have import _umtx_op, and these are the operations FreeBSD's libthr builds its mutexes, condition variables and rwlocks on. This implements them as a payload process on a PS5 (firmware 13.40) answers:

Operation Console
MUTEX_TRYLOCK (4), free / contested-free 0, owner = thread id (`
MUTEX_TRYLOCK / MUTEX_LOCK (5), owned by the caller 0, owner unchanged (no count: one UNLOCK frees it)
MUTEX_TRYLOCK, owned by another thread EBUSY
MUTEX_LOCK / MUTEX_WAIT (17), owned by another, 1 ms timeout ETIMEDOUT, UMUTEX_CONTESTED left set
MUTEX_UNLOCK (6), not the owner / unowned EPERM
MUTEX_WAIT, unowned 0
MUTEX_WAKE (18), contested, no waiters 0, owner becomes 0
UMUTEX_PRIO_INHERIT trylock/unlock as a normal mutex
UMUTEX_PRIO_PROTECT trylock EINVAL
SET_CEILING (7) on a non-PP object EINVAL
CV_SIGNAL (9) / CV_BROADCAST (10), no waiters 0, c_has_waiters 0
CV_WAIT (8), 1 ms timeout ETIMEDOUT; the mutex is left unlocked, c_has_waiters 0
RW_RDLOCK (12) / RW_WRLOCK (13) / RW_UNLOCK (14), free state 1 / 0x80000000 / 0
RW_RDLOCK against a writer, 1 ms timeout ETIMEDOUT, URWLOCK_READ_WAITERS left set
RW_WRLOCK against a reader, 1 ms timeout ETIMEDOUT, no waiter bit left, rw_blocked_writers 0
RW_UNLOCK, unlocked EPERM

The owner a mutex records is scePthreadGetthreadid, which on the console equals thr_self. Two answers differ from FreeBSD 11's kern_umtx.c, and this follows the console: relocking a mutex the caller owns succeeds instead of EBUSY, and a reader that times out leaves URWLOCK_READ_WAITERS set. Blocking, contention and wakeups (who is woken on unlock, UMUTEX_CONTESTED when more than one waiter remains, writers before readers unless URWLOCK_PREFER_READER) follow kern_umtx.c. CV_WAIT queues the waiter before it unlocks the mutex, so a signal sent between the unlock and the sleep is not lost. Its timeout is a timespec in uaddr2, relative unless CVWAIT_ABSTIME, on c_clockid with CVWAIT_CLOCKID.

Robust umutexes, unlocking or setting the ceiling of a priority-protected one, MUTEX_WAKE2 and the SEM2, SHM and robust-list operations still throw. Priority inheritance is not applied, and CVWAIT_CHECK_UNPARKING is ignored. The debt entry records this.

Tested

  • PS5 (firmware 13.40), the console's _umtx_op from a payload: the table above.
  • guest_umtx_op gains MutexOwnershipFollowsTheConsole, MutexLockWaitsForTheOwnerToUnlock, ConditionWaitReleasesTheMutexAndWakes, RwlockStateFollowsTheConsole and RwlockUnlockWakesTheBlockedWriter; the old check that MUTEX_LOCK throws is replaced by operation 7's EINVAL. Linux x86-64 container (Ubuntu 24.04, GCC 13.3, Release): passes 5 of 5 runs.
  • Hosted Linux runner, verify run on my fork (Ubuntu 24.04, GCC, Release, lavapipe): 2189 of 2190 pass; the failure, pr_overlap_encoding, is main's on that runner.
  • check_conventions.py passes. #9823 also edits _umtx_op's default: lines (it swaps the NotImplemented_nid_no_patch there for a plain throw). It implements none of these operations, but whichever lands second will need a small rebase there.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5000577825 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10789). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-25T00:50:31Z. --- ### What `_umtx_op` threw for the umutex, condition variable and read-write lock operations. 26 of the 67 title and homebrew executables I have import `_umtx_op`, and these are the operations FreeBSD's libthr builds its mutexes, condition variables and rwlocks on. This implements them as a payload process on a PS5 (firmware 13.40) answers: | Operation | Console | |---|---| | `MUTEX_TRYLOCK` (4), free / contested-free | 0, owner = thread id (`| CONTESTED` kept) | | `MUTEX_TRYLOCK` / `MUTEX_LOCK` (5), owned by the caller | 0, owner unchanged (no count: one `UNLOCK` frees it) | | `MUTEX_TRYLOCK`, owned by another thread | `EBUSY` | | `MUTEX_LOCK` / `MUTEX_WAIT` (17), owned by another, 1 ms timeout | `ETIMEDOUT`, `UMUTEX_CONTESTED` left set | | `MUTEX_UNLOCK` (6), not the owner / unowned | `EPERM` | | `MUTEX_WAIT`, unowned | 0 | | `MUTEX_WAKE` (18), contested, no waiters | 0, owner becomes 0 | | `UMUTEX_PRIO_INHERIT` trylock/unlock | as a normal mutex | | `UMUTEX_PRIO_PROTECT` trylock | `EINVAL` | | `SET_CEILING` (7) on a non-PP object | `EINVAL` | | `CV_SIGNAL` (9) / `CV_BROADCAST` (10), no waiters | 0, `c_has_waiters` 0 | | `CV_WAIT` (8), 1 ms timeout | `ETIMEDOUT`; the mutex is left unlocked, `c_has_waiters` 0 | | `RW_RDLOCK` (12) / `RW_WRLOCK` (13) / `RW_UNLOCK` (14), free | state 1 / `0x80000000` / 0 | | `RW_RDLOCK` against a writer, 1 ms timeout | `ETIMEDOUT`, `URWLOCK_READ_WAITERS` left set | | `RW_WRLOCK` against a reader, 1 ms timeout | `ETIMEDOUT`, no waiter bit left, `rw_blocked_writers` 0 | | `RW_UNLOCK`, unlocked | `EPERM` | The owner a mutex records is `scePthreadGetthreadid`, which on the console equals `thr_self`. Two answers differ from FreeBSD 11's [kern_umtx.c](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/kern/kern_umtx.c), and this follows the console: relocking a mutex the caller owns succeeds instead of `EBUSY`, and a reader that times out leaves `URWLOCK_READ_WAITERS` set. Blocking, contention and wakeups (who is woken on unlock, `UMUTEX_CONTESTED` when more than one waiter remains, writers before readers unless `URWLOCK_PREFER_READER`) follow kern_umtx.c. `CV_WAIT` queues the waiter before it unlocks the mutex, so a signal sent between the unlock and the sleep is not lost. Its timeout is a `timespec` in `uaddr2`, relative unless `CVWAIT_ABSTIME`, on `c_clockid` with `CVWAIT_CLOCKID`. Robust umutexes, unlocking or setting the ceiling of a priority-protected one, `MUTEX_WAKE2` and the `SEM2`, `SHM` and robust-list operations still throw. Priority inheritance is not applied, and `CVWAIT_CHECK_UNPARKING` is ignored. The debt entry records this. ### Tested - PS5 (firmware 13.40), the console's `_umtx_op` from a payload: the table above. - `guest_umtx_op` gains `MutexOwnershipFollowsTheConsole`, `MutexLockWaitsForTheOwnerToUnlock`, `ConditionWaitReleasesTheMutexAndWakes`, `RwlockStateFollowsTheConsole` and `RwlockUnlockWakesTheBlockedWriter`; the old check that `MUTEX_LOCK` throws is replaced by operation 7's `EINVAL`. Linux x86-64 container (Ubuntu 24.04, GCC 13.3, Release): passes 5 of 5 runs. - Hosted Linux runner, [verify run on my fork](https://github.com/rooklz/AnyPS5/actions/runs/40144567454) (Ubuntu 24.04, GCC, Release, lavapipe): 2189 of 2190 pass; the failure, `pr_overlap_encoding`, is `main`'s on that runner. - `check_conventions.py` passes. #9823 also edits `_umtx_op`'s `default:` lines (it swaps the `NotImplemented_nid_no_patch` there for a plain throw). It implements none of these operations, but whichever lands second will need a small rebase there. ### 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: none <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
MarcZX10 commented 2026-10-25 00:56:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T00:56:39Z.

Review: CHANGES_REQUESTED


Reviewed exact head eeb5cf0cc0e8cdf1ff73ee03ed6c1b0abe80c91c. The changed source and test translation units pass GCC 12.2 syntax checks with warnings as errors (suppressing pre-existing unused-parameter/unused-variable warnings); git diff --check and the current-main conventions check pass. I built and ran the exact GuestUmtxOp.cpp test with the changed SyncOnAddress.cpp and real TimedWait.cpp, using temporary host hooks for guest thread ID, guest errno, clock and trace calls: 5/5 runs passed, and the same focused suite passed under ASan+UBSan. The PR's hosted product jobs are path-skipped.

I found a functional omission in the read-lock flags. _umtx_op passes its value to RwlockLock only indirectly today: the dispatcher calls RwlockLock(rwlock, writer, ...) and drops value, while the helper derives reader preference only from rwlock->flags. FreeBSD stable/11's do_rw_rdlock uses the operation's fflag as well as rw_flags when deciding whether URWLOCK_WRITE_WAITERS blocks a reader (source). The PR says reader preference affects writer-before-reader behavior, but none of its tests pass that operation flag.

I reproduced the missing case against the exact PR source: with rwlock.state = URWLOCK_WRITE_WAITERS, rwlock.flags = 0, and a short timeout, _umtx_op(RW_RDLOCK, value = URWLOCK_PREFER_READER, ...) returns ETIMEDOUT and leaves state 0x60000000 instead of taking the read lock. Please thread the read-lock value into the helper and add this regression, or provide a PS5 measurement showing the console ignores this flag. Until then the advertised reader-preference behavior is incomplete.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640841715 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10789#pullrequestreview-5640841715). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T00:56:39Z. **Review:** CHANGES_REQUESTED --- Reviewed exact head `eeb5cf0cc0e8cdf1ff73ee03ed6c1b0abe80c91c`. The changed source and test translation units pass GCC 12.2 syntax checks with warnings as errors (suppressing pre-existing unused-parameter/unused-variable warnings); `git diff --check` and the current-main conventions check pass. I built and ran the exact `GuestUmtxOp.cpp` test with the changed `SyncOnAddress.cpp` and real `TimedWait.cpp`, using temporary host hooks for guest thread ID, guest errno, clock and trace calls: 5/5 runs passed, and the same focused suite passed under ASan+UBSan. The PR's hosted product jobs are path-skipped. I found a functional omission in the read-lock flags. `_umtx_op` passes its `value` to `RwlockLock` only indirectly today: the dispatcher calls `RwlockLock(rwlock, writer, ...)` and drops `value`, while the helper derives reader preference only from `rwlock->flags`. FreeBSD stable/11's `do_rw_rdlock` uses the operation's `fflag` as well as `rw_flags` when deciding whether `URWLOCK_WRITE_WAITERS` blocks a reader ([source](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/kern/kern_umtx.c)). The PR says reader preference affects writer-before-reader behavior, but none of its tests pass that operation flag. I reproduced the missing case against the exact PR source: with `rwlock.state = URWLOCK_WRITE_WAITERS`, `rwlock.flags = 0`, and a short timeout, `_umtx_op(RW_RDLOCK, value = URWLOCK_PREFER_READER, ...)` returns `ETIMEDOUT` and leaves state `0x60000000` instead of taking the read lock. Please thread the read-lock `value` into the helper and add this regression, or provide a PS5 measurement showing the console ignores this flag. Until then the advertised reader-preference behavior is incomplete.
MarcZX10 commented 2026-10-25 04:17:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T04:17:21Z.

Review: APPROVED


Reviewed the updated head 2900cecebe859508f4c51a725e1b8662e2a4bd7c. The implementation now passes the RW_RDLOCK operation flags into RwlockLock and combines URWLOCK_PREFER_READER with the rwlock flags. The regression covers the four reported combinations, including reader preference supplied by the operation and a writer-owner timeout. I rebuilt the changed source, test, and TimedWait translation units with GCC 12.2, linked the focused GuestUmtxOp harness with temporary host hooks, and ran it five times successfully. tools/check_conventions.py and git diff --check also pass against current main. The author reports matching PS5 measurements for the same four cases. Hosted product jobs are skipped by PR workflow policy.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641465304 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10789#pullrequestreview-5641465304). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T04:17:21Z. **Review:** APPROVED --- Reviewed the updated head `2900cecebe859508f4c51a725e1b8662e2a4bd7c`. The implementation now passes the RW_RDLOCK operation flags into `RwlockLock` and combines `URWLOCK_PREFER_READER` with the rwlock flags. The regression covers the four reported combinations, including reader preference supplied by the operation and a writer-owner timeout. I rebuilt the changed source, test, and TimedWait translation units with GCC 12.2, linked the focused `GuestUmtxOp` harness with temporary host hooks, and ran it five times successfully. `tools/check_conventions.py` and `git diff --check` also pass against current main. The author reports matching PS5 measurements for the same four cases. Hosted product jobs are skipped by PR workflow policy.
MarcZX10 commented 2026-10-29 08:19:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T08:19:56Z.

Review: APPROVED


Re-reviewed exact head 491fe942705cef60c011a19222ef6650ac9b3646 against live main 4c7d275bef89d309313d16bc2bc05d37a1b50be2.

The three follow-up commits address the reported lost-wakeup race structurally: the wait predicate publishes its waiter bit/count under BucketOf(key).mutex, and last-waiter cleanup now decrements, clears, and rechecks under that same mutex. A new waiter therefore cannot publish between the clear and recheck. I also checked the priority-protected condition-wait guard: it throws before deadline parsing or incrementing hasWaiters, and the regression checks that no waiter state leaks.

Current-main checks:

  • git merge-tree --write-tree: clean.
  • git diff --check: passed.
  • python3 tools/check_conventions.py --base upstream/main --head refs/review/upstream-10789: passed.
  • Main has changed SyncOnAddress.cpp since the PR base, but those hunks are in the standalone SyncOnAddress wait/wake API; the umtx wait predicate/cleanup changes reviewed here remain intact.

Validation evidence: wachi35676 reports guest_umtx_op 10/10 on Windows and Linux, the 4-thread lost-wakeup stress 10/10 on both, 2- and 8-thread stress 3/3, and the mutex/condvar stress passing. The Windows negative control with the fix commits reversed fails 3/3 and reproduces the stuck waiter state. I did not rerun those platform tests locally; hosted product jobs are skipped.

No blocking finding remains.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5694680637 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10789#pullrequestreview-5694680637). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T08:19:56Z. **Review:** APPROVED --- Re-reviewed exact head `491fe942705cef60c011a19222ef6650ac9b3646` against live `main` `4c7d275bef89d309313d16bc2bc05d37a1b50be2`. The three follow-up commits address the reported lost-wakeup race structurally: the wait predicate publishes its waiter bit/count under `BucketOf(key).mutex`, and last-waiter cleanup now decrements, clears, and rechecks under that same mutex. A new waiter therefore cannot publish between the clear and recheck. I also checked the priority-protected condition-wait guard: it throws before deadline parsing or incrementing `hasWaiters`, and the regression checks that no waiter state leaks. Current-main checks: - `git merge-tree --write-tree`: clean. - `git diff --check`: passed. - `python3 tools/check_conventions.py --base upstream/main --head refs/review/upstream-10789`: passed. - Main has changed `SyncOnAddress.cpp` since the PR base, but those hunks are in the standalone SyncOnAddress wait/wake API; the umtx wait predicate/cleanup changes reviewed here remain intact. Validation evidence: wachi35676 reports `guest_umtx_op` 10/10 on Windows and Linux, the 4-thread lost-wakeup stress 10/10 on both, 2- and 8-thread stress 3/3, and the mutex/condvar stress passing. The Windows negative control with the fix commits reversed fails 3/3 and reproduces the stuck waiter state. I did not rerun those platform tests locally; hosted product jobs are skipped. No blocking finding remains.
MarcZX10 commented 2026-10-31 18:59:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T18:59:37Z.

Review: APPROVED


Re-checked — new head, looks good

The rebased head 83b1015c4fb99e527b72f458848d0230094758a1 preserves the previously approved implementation and tests; its current-main preview is clean, conventions pass, and hosted Windows passes. The reported 540/540 CTest run is author-provided; I did not build or run it locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5724430150 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10789#pullrequestreview-5724430150). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T18:59:37Z. **Review:** APPROVED --- # Re-checked — new head, looks good The rebased head `83b1015c4fb99e527b72f458848d0230094758a1` preserves the previously approved implementation and tests; its current-main preview is clean, conventions pass, and hosted Windows passes. The reported 540/540 CTest run is author-provided; I did not build or run it locally.
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!10789
No description provided.