fix(libkernel): follow sceKernelSyncOnAddressWait and Wake as measured on a PS5 #8725
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8725"
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 orezraey on 2026-10-20T22:31:27Z.
What
sceKernelSyncOnAddressWait,Wait8/16/32/64andsceKernelSyncOnAddressWakefollowed assumptions: the debt entries said that the by-value comparand, the return values, the timeout and the alignment were assumed. In a payload process on a PS5 (firmware 13.60) they behave differently, and this PR follows the console:mainbefore this PR0x80020023(EAGAIN) at once, timeout left as it wasEAGAIN, timeout untouched0x8002003C(ETIMEDOUT) after the timeout; the timeout is set to 0ETIMEDOUT, timeout untouchedWakeETIMEDOUTat once, even when the value differs and even for a null addressETIMEDOUTif the value matches, 0 if not0x8002000E(EFAULT), timeout untouchedEFAULTWait32at +1, +2,Wait16at +33,Wait64at +41, +44)Wakewith a count of 1 or less (1, 0, −1, −2), three waitersWakewith count 2 orINT_MAX, three waitersWakewith no waiter, a null address or 0x10Wait64compares all 64 bits, and the timeout is a 32-bit count of microseconds: 2000 timed out after about 2.1 ms and 50000 after 50.1 ms. Both were already the case here._umtx_opshares the waiter queue and is unchanged.A misaligned value is read with one x86-64
movin inline assembly, the same single load the console performs. C++ has no atomic load for a misaligned object, and the assembly keeps the read out of the C++ memory model, so a guest thread storing to the word while a wait compares it is not a data race on the host. Like the console's load, it is not atomic when the word crosses a cache line.Debt: the two entries in
docs/dev/debt/libkernel.mdare replaced by one. Its gaps:EFAULTfor 0x10.How the console was measured
resolvelines show that the six functions were taken fromlibkernel_sys.sprxbydlsymand called through those pointers.Waketwice with the same count.main.crenamed<probe>_main.c.Console output (excerpt, lines copied unchanged)
The
kwaitN:prefix names the probe; the rest of each line is copied unchanged.Tested
main(e48379c7af):a09cef91b1. The only conflict was the semaphore debt entry next to this PR's SyncOnAddress entry; both are kept. At this headguest_sync_on_addressandguest_raise_exceptionpass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC),guest_umtx_opthree runs on Windows, andcheck_conventionspasses.main(f26434afa3, with #10706):dcfc5632b0. The test takes its declarations fromlibkernel/include/Api.hppand its error codes fromKernelErrors.hpp; the header'ssceKernelSyncOnAddressWait*takeKernelUseconds*(notconst), matching this PR's definitions, which write the remaining time back.guest_sync_on_address,guest_raise_exceptionandguest_umtx_oppass three runs in a row on Windows (MinGW) and on Linux (WSL Ubuntu, GCC);public_headersaudit andcheck_conventionspass.Build: Windows 11 x86-64, WinLibs MinGW-w64 GCC 15.2.0 (posix-seh, ucrt, r7),
-DBUILD_TESTING=ON, onmain339217ebc.Tests:
guest_sync_on_addresswas rewritten for the measured behaviour. It now checks:EAGAIN,ETIMEDOUTandEFAULT;INT_MAXthe rest, with the counts returned;guest_umtx_opandguest_raise_exceptionpass. The latter neededWake's result checked as a count instead of 0.Old code, new test: with
SyncOnAddress.cppreverted tomain,guest_sync_on_addressaborts at its first check, where the value differs andmainreturns 0.Not run here: macOS.
Console: behaviour measured on a PS5 (firmware 13.60), as described above.
Overlaps:
tools/pr_overlap.pylists #8304 (UMTX_OP_SEM_WAITandUMTX_OP_SEM_WAKE) and #8323 (sharded waiter buckets). Both change lines ofSyncOnAddress.cppnext to the ones changed here, and #8304 the same debt lines. Whichever lands second has to reconcile them.main.GuestSyncOnAddress.cppandGuestRaiseException.cpp.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)libkernel.md); #10706 removes the test declarations this PR adds toGuestRaiseException.cppandGuestSyncOnAddress.cpp(merge order only)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T22:32:05Z.
One concurrency question on
LoadAddressValue: the newly supported misaligned path usesmemcpy, which is an ordinary non-atomic read. A sync-on-address wait normally races with a guest thread updating that word before callingWake;g_waitersLockdoes not synchronize with those guest stores. The aligned path usesatomic_ref, so the misaligned branch appears to introduce a C++ data race/undefined behavior for precisely that usage (even though the console reports that cross-cache-line loads themselves need not be atomic). Could you clarify how concurrent guest stores are represented/synchronized here, or use a host-side access primitive that gives the intended non-atomic hardware semantics without an unsynchronized C++ read? The current misaligned test only wakes without changing the word, so it does not exercise that case.Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-20T22:40:09Z.
Good point, thanks.
f4f9b3creplaces thememcpywith one x86-64movin inline assembly (asm volatilewith a byte-array memory operand, so no misaligned typed lvalue is formed either). That is the single load the console performs on the word, and as an opaque asm load it is outside the C++ memory model, so a guest store racing the comparison is not UB on the host. It has the hardware semantics you describe: atomic within a cache line, possibly torn across one, like the console's own load.guest_sync_on_addressnow also runs 2000 waits on a misaligned 64-bit word (offset 3) while another thread keeps storing to it and callingWake. Each wait returns 0,EAGAINorETIMEDOUT, and the loop finishes.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T23:34:04Z.
@orezraey This now conflicts with
maininSyncOnAddress.cpp: #8304 (merged earlier today) changed the same code, and it isn't a mechanical resolution, so we'd rather you make the call. Could you mergemaininto the branch (or rebase) and push? We'll review the new head and run full CI on it.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T23:39:53Z.
Thanks for the follow-up. I checked the updated load and test: the misaligned comparison now uses one opaque x86-64
movload rather than an ordinary C++memcpyread, and the regression runs 2,000 waits while another thread updates the misaligned 64-bit word and callsWake. That resolves the host data-race concern I raised. I see the maintainer’s separate request to merge/rebase currentmain; I’ll review the updated head after that lands.Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-20T23:45:48Z.
Thanks @oneandonlydean, merged
mainin973234d. I kept #8304'sWaitOnKeyLocked/WaitOnKeyUntilunchanged and rebuilt this PR'ssceKernelSyncOnAddressWaiton them. The ready predicate passed toWaitOnKeyUntilnow records whether the value differed, which gives the measuredEAGAIN, and reads a misaligned word with the single-load helper.WakeAddressreturns the number woken, whichsceKernelSyncOnAddressWakereports; the_umtx_opcallers, the newSEM_WAIT/SEM_WAKEincluded, ignore it as before. Inlibkernel.mdI kept #8304's_umtx_opentry and this PR's singlesceKernelSyncOnAddressWaitentry.guest_sync_on_address,guest_umtx_opandguest_raise_exceptionpass on Windows (GCC 15.2), and the diff againstmaintouches only this PR's four files.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T23:48:08Z.
Review: APPROVED
Reviewed the rebased head
973234db6e. It retains #8304’s waiter-queue implementation and applies the measured wait comparison, timeout writeback, and wake-count behavior on top. Local checks:git diff --checkand the conventions checker pass; GCC syntax-checking of the unaligned-load template instantiated for 8/16/32/64-bit types passes. I could not run the full C++ runtime target locally; the author reports the focused Windows suites passing. No blocking issue found in the rebased diff.Imported from boykopovar/AnyPS5. Originally posted by Filip722 on 2026-10-21T10:09:30Z.
Additional native Linux runtime validation on exact head
973234db6ebef085936474042cfd3afee32226cc(including the merged waiter-queue changes).Environment: Debian 13.7 x86-64, kernel 7.2.9-x64v3-xanmod1, Ryzen 5 7640HS, GCC 15.2.0, CMake 3.31.6, Release. Full-project configuration with
BUILD_TESTING=ON,ANYPS5_RELINKER_ONLY=OFF, bundled dependencies and two build jobs; built the actuallibkerneland the six relevant test executables.guest_sync_on_address,guest_umtx_op,guest_raise_exception,guest_once,guest_kernel_sync, andguest_pthread_semeach passed three consecutive runs: 18 executions, no failures or skips._umtx_opsemaphore cases.SyncOnAddress.cppwith its merge-base version (5b59fbc5539b6c6db7a26972f6069f1c6cc179ac) and rebuiltlibkernel.guest_sync_on_addressaborted. Restoring the exact PR source and rebuilding made it pass again.Commands:
This adds native Linux execution of the project tests; it does not independently repeat the PS5 payload measurements. Windows-only exception-delivery branches, console execution and games were not tested. The documented unreadable non-null address limitation remains outside this check. AI-assisted testing/reporting: yes.