feat(libkernel): implement the umutex, condition variable and rwlock _umtx_op operations as measured on a PS5 #10789
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10789"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-25T00:50:31Z.
What
_umtx_opthrew 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:MUTEX_TRYLOCK(4), free / contested-freeMUTEX_TRYLOCK/MUTEX_LOCK(5), owned by the callerUNLOCKfrees it)MUTEX_TRYLOCK, owned by another threadEBUSYMUTEX_LOCK/MUTEX_WAIT(17), owned by another, 1 ms timeoutETIMEDOUT,UMUTEX_CONTESTEDleft setMUTEX_UNLOCK(6), not the owner / unownedEPERMMUTEX_WAIT, unownedMUTEX_WAKE(18), contested, no waitersUMUTEX_PRIO_INHERITtrylock/unlockUMUTEX_PRIO_PROTECTtrylockEINVALSET_CEILING(7) on a non-PP objectEINVALCV_SIGNAL(9) /CV_BROADCAST(10), no waitersc_has_waiters0CV_WAIT(8), 1 ms timeoutETIMEDOUT; the mutex is left unlocked,c_has_waiters0RW_RDLOCK(12) /RW_WRLOCK(13) /RW_UNLOCK(14), free0x80000000/ 0RW_RDLOCKagainst a writer, 1 ms timeoutETIMEDOUT,URWLOCK_READ_WAITERSleft setRW_WRLOCKagainst a reader, 1 ms timeoutETIMEDOUT, no waiter bit left,rw_blocked_writers0RW_UNLOCK, unlockedEPERMThe owner a mutex records is
scePthreadGetthreadid, which on the console equalsthr_self. Two answers differ from FreeBSD 11's kern_umtx.c, and this follows the console: relocking a mutex the caller owns succeeds instead ofEBUSY, and a reader that times out leavesURWLOCK_READ_WAITERSset. Blocking, contention and wakeups (who is woken on unlock,UMUTEX_CONTESTEDwhen more than one waiter remains, writers before readers unlessURWLOCK_PREFER_READER) follow kern_umtx.c.CV_WAITqueues the waiter before it unlocks the mutex, so a signal sent between the unlock and the sleep is not lost. Its timeout is atimespecinuaddr2, relative unlessCVWAIT_ABSTIME, onc_clockidwithCVWAIT_CLOCKID.Robust umutexes, unlocking or setting the ceiling of a priority-protected one,
MUTEX_WAKE2and theSEM2,SHMand robust-list operations still throw. Priority inheritance is not applied, andCVWAIT_CHECK_UNPARKINGis ignored. The debt entry records this.Tested
_umtx_opfrom a payload: the table above.guest_umtx_opgainsMutexOwnershipFollowsTheConsole,MutexLockWaitsForTheOwnerToUnlock,ConditionWaitReleasesTheMutexAndWakes,RwlockStateFollowsTheConsoleandRwlockUnlockWakesTheBlockedWriter; the old check thatMUTEX_LOCKthrows is replaced by operation 7'sEINVAL. Linux x86-64 container (Ubuntu 24.04, GCC 13.3, Release): passes 5 of 5 runs.pr_overlap_encoding, ismain's on that runner.check_conventions.pypasses. #9823 also edits_umtx_op'sdefault:lines (it swaps theNotImplemented_nid_no_patchthere for a plain throw). It implements none of these operations, but whichever lands second will need a small rebase there.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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 --checkand the current-main conventions check pass. I built and ran the exactGuestUmtxOp.cpptest with the changedSyncOnAddress.cppand realTimedWait.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_oppasses itsvaluetoRwlockLockonly indirectly today: the dispatcher callsRwlockLock(rwlock, writer, ...)and dropsvalue, while the helper derives reader preference only fromrwlock->flags. FreeBSD stable/11'sdo_rw_rdlockuses the operation'sfflagas well asrw_flagswhen deciding whetherURWLOCK_WRITE_WAITERSblocks 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, ...)returnsETIMEDOUTand leaves state0x60000000instead of taking the read lock. Please thread the read-lockvalueinto 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.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 intoRwlockLockand combinesURWLOCK_PREFER_READERwith 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 focusedGuestUmtxOpharness with temporary host hooks, and ran it five times successfully.tools/check_conventions.pyandgit diff --checkalso pass against current main. The author reports matching PS5 measurements for the same four cases. Hosted product jobs are skipped by PR workflow policy.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T08:19:56Z.
Review: APPROVED
Re-reviewed exact head
491fe942705cef60c011a19222ef6650ac9b3646against livemain4c7d275bef89d309313d16bc2bc05d37a1b50be2.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 incrementinghasWaiters, 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.SyncOnAddress.cppsince 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_op10/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.
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
83b1015c4fb99e527b72f458848d0230094758a1preserves 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.