fix(libkernel): read a size-0 _umtx_op timeout as a timespec #16873
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/16873"
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 JAB-dev on 2026-11-07T19:41:10Z.
Unity's worker threads spin in Monopoly (PPSA17419): every timed wait they make through
PS5Util.prxexpires the moment it starts. Reading that timeout the way the PS5 kernel does takes the language-select screen from 10.8 to 13.4 FPS (+24%) on the same machine, and from 9.4 to 13.5 FPS (+42%) withAPS5_DRAW_PIPELINE=1.What
_umtx_opwith size argument 0 reads its timeout as a relativestruct timespec, and an invalid one fails withEINVALlike an explicit size. That is what the PS5 kernel does: in firmware 13.60 theUMTX_OP_WAIT,WAIT_UINTandWAIT_UINT_PRIVATEhandlers compare the size argument with 16, copy a 16-bytetimespecfor any size up to 16 (0 included) and 24 bytes above it, and returnEINVAL(22) for a negativetv_secor atv_nsecabove 999999999 (unsigned compare, so negative too); firmware 5.50 has the same code. FreeBSD 11'sumtx_copyin_umtx_timedoes the same. Since the change that added microsecond size-0 timeouts, main reads a 4-byte microsecond count there.UmtxDeadlineserves the wait,SEM_WAIT, umutex and rwlock operations, so they all change the same way; condition-variable waits decode their timeout separately and do not change.Monopoly's
PS5Util.prxexportzlqfTyrQSPk(RVA0x102a0) isjmp 0x10500. It takes a pointer to a 32-bit microsecond count, converts the count to atimespecon its stack, checks it as one, and passes it to_umtx_opwith size argument 0:On an earlier build, with a libkernel that logged every size-0 caller, the only one in a run of the language screen returned to
PS5Util.prx+0x10799and passed00000000 00000000 000f4240 00000000, atimespecof 1 ms. Read as microseconds, every such wait has a 0 or near-0 count: about 1.6 million waits a second expired on entry, and five Unity threads used about 92% of a core each. On the current base, sampled stacks of those five threads onmainreturn toPS5Util.prx+0x10799; with this change three threads use 52-77% of a core each and every other thread less than 7%.That change's Metal Gear Solid 4 (PPSA18194) listing shows the game passing a pointer to a 4-byte microsecond count to
zlqfTyrQSPk, the same export whose Monopoly build converts that count to atimespecbefore it calls_umtx_op. The moves it quotes (mov %rdx,%r8,mov %rsi,%rdx,mov $0x2,%esi,xor %ecx,%ecx) also appear in Monopoly's helper at0x10700, which runs after the conversion. I can't check that title'sPS5Util.prxbuild because I don't have it. It also named SCElibc.prx: thelibc.prxMonopoly ships calls_umtx_opwith size 0 only from its once-flag wait, with a null timeout (xor %edx,%edxbefore the wrapper that moves it intor8), so that call doesn't change. The olderPS5Util.prxbuilds I checked importsceKernelSyncOnAddressWaitand don't call_umtx_op. A caller that did pass a bare 4-byte count with size 0 would get the console's reading: 16 bytes read as atimespec. @leorossi2005, could you check whether Metal Gear Solid 4 still gets past its startup barrier with this change?Tested
APS5_TRACE_SECONDS=40:20and no other switch, Monopoly (PPSA17419) language-select screen,mainat07c8408713and this change at5ece2d8b9e(the same base), captured alternately (each trace lost slices once its buffer filled; the totals behind the summary lines are complete):mainTraces, in capture order:
main: aps5-trace-07c8408713eb-134008.json.gzmain: aps5-trace-07c8408713eb-141636.json.gzSummary lines, in capture order:
APS5_DRAW_PIPELINE=1: Windows 11, RTX 4070 SUPER (NVIDIA 610.88), Monopoly (PPSA17419) language-select screen,mainatdd00a9c54eagainst the same build with this change in libkernel (measured before the rebase onto the current base, which changed libkernel only inSocket/src/Udp.cpp), four alternated pairs of 75 s runs, FPS from 30 s on:mainThree other titles on the same base with
APS5_DRAW_PIPELINE=1,mainagainst this change: a Unity title whose olderPS5Util.prxwaits throughsceKernelSyncOnAddressWaitshows 48.8, 52.8, 49.7 against 55.6, 50.1, 53.7 FPS on its title screen (within its run-to-run spread); two titles capped at 60 FPS stay at 60; the same kinds of log lines either way.ctest -R guest_umtx_op: passes with this change. Onmainit fails at the size-0timespec {0, 20000000}, which returns at once instead of after 20 ms. Size-0timespecs with negative seconds or 1e9 nanoseconds fail withEINVALforUMTX_OP_WAITandUMTX_OP_WAIT_UINT.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T23:12:52Z.
Review: COMMENTED
Read
5ece2d8b9eagainstmain.Semantics. FreeBSD 11.4's
umtx_copyin_umtx_time(sys/kern/kern_umtx.c:3329-3346) copies atimespecfor any size up tosizeof(struct timespec), 0 included, setsCLOCK_REALTIMEwith no flags, and fails withEINVALfor a negativetv_secor atv_nsecoutside [0, 1e9). The wait, umutex, rwlock and semaphore handlers all go through it, whileCV_WAITusesumtx_copyin_timeout(:3511-3527), which matches leavingConditionWaitalone. With the size-0 branch gone,UmtxDeadline(SyncOnAddress.cpp:304-317) does exactly this.Generality. This removes a reading taken from one title's call site rather than adding one; the test is the only other in-tree caller of
_umtx_op_nid_postfix.Test. On
mainthe size-0{0, 20000000}is read as 0 µs and returnsETIMEDOUTat once, so the 15 ms check fails; here it waits. The new size-0EINVALcases follow the same copy-in.Whether Metal Gear Solid 4 still gets past its barrier stays open, as the description says.
Not run here; this is from reading the code.
AI-assisted: yes.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T12:24:40Z.
Review: APPROVED
Reviewed — looks good
The reviewed change at head
5ece2d8b9eb61e9ca08c9e1abeda1771a8a83332reads size-zero_umtx_optimeouts asKernelTimespec, with tests for a timed wait and invalid values; the source and tests support the stated behavior.What I ran
No commands were run. I inspected the prepared diff and manifest, then read both changed files at the pinned head:
core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cppandcore/libs/tests/GuestUmtxOp.cpp.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No tests or builds were run. Performance measurements, console behavior, and the Metal Gear Solid 4 startup behavior were not independently verified. No files were excluded from review; both manifest files were included and read.
To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-12-03T10:35:54Z.
Review: APPROVED
I read the diff at
5ece2d8b9e. Windows 11 with GCC 15.2 and Linux (Ubuntu 24.04 in WSL2, GCC 13.3), merged intomainatb97867d3ce6together with other open PRs (so not an isolated build).guest_umtx_op: pass on both.main's): aborts on both.Not checked: a title.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T13:09:11Z.
Review: COMMENTED
Built the changed sources and ran
GuestUmtxOpon Linux against the existing runtime. The fixture passed, including the size-zero relative timespec cases.AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.