fix(libkernel): defer raises while a thread holds the address-wait lock on Linux and macOS #9074

Merged
imellag merged 1 commit from upstream-pr/9074 into main 2026-10-24 12:29:36 +00:00
imellag commented 2026-10-21 11:47:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-21T11:47:37Z.


What

On Linux and macOS, sceKernelRaiseException sends 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 of sceKernelSyncOnAddressWait, sceKernelSyncOnAddressWake or _umtx_op ran 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 DeliveryDeferral on every host, as on Windows:

  • On Linux and macOS the deferral is a count per thread. While it is raised, the SIGUSR1 handler only records the signal and returns. DeliverDeferred sends the recorded signal to the thread again once the lock is released, before the call returns to the guest.
  • An address waiter must still take a raise while it waits, or a collector would wait for a thread that only wakes up when a stopped thread wakes it. A condition variable releases the lock and sleeps in one step, so the deferral cannot be lifted there without a window in which the lock is still held. On Linux and macOS an address waiter therefore waits on its own semaphore (sem_t, a dispatch semaphore on macOS) through TimedWait::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 Windows SingleWaiterCondition is TimedWait::Condition, unchanged.
  • The two address-lock phases of guest_raise_exception, Windows-only until now, run on every host. A new phase raises at a thread in a sceKernelSyncOnAddressWait without a timeout and requires the handler to run while it still waits.

The Linux debt entry in docs/dev/debt/libkernel.md now names the deferred lock; the other locks of the emulator are still not deferred.

#8658 and #4630 bring sceKernelRaiseException to macOS on this path, so macOS gets the same deadlock without this change. #8658 also renames the test's SIGUSR1 constant to GuestSigUsr1 and changes the same debt line; whichever merges second renames the constant in the new phase. #2528 and #8725 change SyncOnAddress.cpp elsewhere; the new phase accepts any non-negative sceKernelSyncOnAddressWake result, as #8725 makes it return the number woken.

Tested

  • Linux: Ubuntu 24.04 x86-64 in Docker under Rosetta 2 on an Apple M4 Pro, GCC 14.2. On main with only the test change, guest_raise_exception fails in every run (6 of 6, for example address 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 onto 3e91c8edf. Negative controls: with the waiters back on TimedWait::Condition it fails at address 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 at address waking round 22. All 530 guest_* tests ran: the 32 failures fail the same way on main 275e89679 in the same container (helper modules not built there, and container limits).
  • macOS 27.0.1, Apple M4 Pro, Apple clang 21, x86-64 under Rosetta 2, on mugurc's macOS series (with #4630), the change ported there: the address phases fail in every run without it and guest_raise_exception passes 25 of 25 with it (about 6 s each).
  • God of War: Sons of Sparta (PPSA28997) on that macOS series: about 1 boot in 4 to 8 froze at the title load after a burst of raises. A sample of 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.
  • Windows: not run; the Windows path is unchanged apart from DeferringDelivery losing its #ifdef.

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:4951029155 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9074). Originally posted by [imellag](https://github.com/imellag) on 2026-10-21T11:47:37Z. --- ### What On Linux and macOS, `sceKernelRaiseException` sends 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 of `sceKernelSyncOnAddressWait`, `sceKernelSyncOnAddressWake` or `_umtx_op` ran 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 `DeliveryDeferral` on every host, as on Windows: - On Linux and macOS the deferral is a count per thread. While it is raised, the SIGUSR1 handler only records the signal and returns. `DeliverDeferred` sends the recorded signal to the thread again once the lock is released, before the call returns to the guest. - An address waiter must still take a raise while it waits, or a collector would wait for a thread that only wakes up when a stopped thread wakes it. A condition variable releases the lock and sleeps in one step, so the deferral cannot be lifted there without a window in which the lock is still held. On Linux and macOS an address waiter therefore waits on its own semaphore (`sem_t`, a dispatch semaphore on macOS) through `TimedWait::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 Windows `SingleWaiterCondition` is `TimedWait::Condition`, unchanged. - The two address-lock phases of `guest_raise_exception`, Windows-only until now, run on every host. A new phase raises at a thread in a `sceKernelSyncOnAddressWait` without a timeout and requires the handler to run while it still waits. The Linux debt entry in `docs/dev/debt/libkernel.md` now names the deferred lock; the other locks of the emulator are still not deferred. #8658 and #4630 bring `sceKernelRaiseException` to macOS on this path, so macOS gets the same deadlock without this change. #8658 also renames the test's `SIGUSR1` constant to `GuestSigUsr1` and changes the same debt line; whichever merges second renames the constant in the new phase. #2528 and #8725 change `SyncOnAddress.cpp` elsewhere; the new phase accepts any non-negative `sceKernelSyncOnAddressWake` result, as #8725 makes it return the number woken. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - Linux: Ubuntu 24.04 x86-64 in Docker under Rosetta 2 on an Apple M4 Pro, GCC 14.2. On `main` with only the test change, `guest_raise_exception` fails in every run (6 of 6, for example `address 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 onto 3e91c8edf. Negative controls: with the waiters back on `TimedWait::Condition` it fails at `address 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 at `address waking round 22`. All 530 `guest_*` tests ran: the 32 failures fail the same way on `main` 275e89679 in the same container (helper modules not built there, and container limits). - macOS 27.0.1, Apple M4 Pro, Apple clang 21, x86-64 under Rosetta 2, on mugurc's macOS series (with #4630), the change ported there: the address phases fail in every run without it and `guest_raise_exception` passes 25 of 25 with it (about 6 s each). - God of War: Sons of Sparta (PPSA28997) on that macOS series: about 1 boot in 4 to 8 froze at the title load after a burst of raises. A `sample` of 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. - Windows: not run; the Windows path is unchanged apart from `DeferringDelivery` losing its `#ifdef`. ### 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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
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!9074
No description provided.