fix(libkernel): defer raises while a thread holds the address-wait lock on Linux and macOS #9074
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9074"
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 imellag on 2026-10-21T11:47:37Z.
What
On Linux and macOS,
sceKernelRaiseExceptionsends SIGUSR1 to the target thread, and the guest handler runs in the signal handler wherever the thread was interrupted. A thread interrupted while it held the address-wait lock ofsceKernelSyncOnAddressWait,sceKernelSyncOnAddressWakeor_umtx_opran the handler with the lock held. A handler that waits, as a stop-the-world collector's thread-stop handler does, then kept the lock, and every other thread that waits on or wakes an address stopped too. #5756 fixed this on Windows (#1488) and left Linux as a debt entry.The address-wait lock is now taken under
DeliveryDeferralon every host, as on Windows:DeliverDeferredsends the recorded signal to the thread again once the lock is released, before the call returns to the guest.sem_t, a dispatch semaphore on macOS) throughTimedWait::SingleWaiterCondition: it releases the lock, lifts the deferral (a recorded raise runs here), waits, restores the deferral and takes the lock again. A wake marks the waiter and posts its semaphore under the lock, so a wake that comes before the wait is kept. On WindowsSingleWaiterConditionisTimedWait::Condition, unchanged.guest_raise_exception, Windows-only until now, run on every host. A new phase raises at a thread in asceKernelSyncOnAddressWaitwithout a timeout and requires the handler to run while it still waits.The Linux debt entry in
docs/dev/debt/libkernel.mdnow names the deferred lock; the other locks of the emulator are still not deferred.#8658 and #4630 bring
sceKernelRaiseExceptionto macOS on this path, so macOS gets the same deadlock without this change. #8658 also renames the test'sSIGUSR1constant toGuestSigUsr1and changes the same debt line; whichever merges second renames the constant in the new phase. #2528 and #8725 changeSyncOnAddress.cppelsewhere; the new phase accepts any non-negativesceKernelSyncOnAddressWakeresult, as #8725 makes it return the number woken.Tested
mainwith only the test change,guest_raise_exceptionfails in every run (6 of 6, for exampleaddress waking round 6: a wake on another address blocked for 500 ms while the handler ran); the unmodified test passes. With this change it passes 25 of 25 runs (about 4 s each) and 10 of 10 after the rebase onto3e91c8edf. Negative controls: with the waiters back onTimedWait::Conditionit fails ataddress idle round 0: the handler did not run while the thread waited without a timeout; without the early return in the SIGUSR1 handler it fails ataddress waking round 22. All 530guest_*tests ran: the 32 failures fail the same way onmain275e89679in the same container (helper modules not built there, and container limits).guest_raise_exceptionpasses 25 of 25 with it (about 6 s each).sampleof the frozen process showed about 25 guest threads in the handler (sceKernelWaitEventFlag), two of them interrupted inside the address-wait lock, and the main guest thread blocked on that lock in_umtx_op. With this change, 16 boots of 75 s each delivered 1,083 to 1,509 raises per boot (about 19,500 in all) and none froze.DeferringDeliverylosing its#ifdef.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)