fix(libkernel): release every level of a recursive mutex during a cond wait #1480

Merged
awake-devel merged 1 commit from upstream-pr/1480 into main 2026-10-09 19:05:19 +00:00
awake-devel commented 2026-10-08 08:54:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by awake-devel on 2026-10-08T08:54:49Z.


What

A condition wait on a recursive mutex that the waiter has locked more than once deadlocks. waitUntil in Pthread/src/Cond.cpp cleared _count and _owner, but the condition variable unlocks the native std::recursive_timed_mutex only once. Every scePthreadMutexLock on a recursive mutex locks the native mutex again, so after N locks the waiter still holds N-1 levels during the wait. The thread that should take the mutex and signal blocks in scePthreadMutexLock, and the waiter never wakes (an untimed wait hangs; a timed wait times out forever).

FreeBSD libthr, which the PS5 libkernel is based on, releases the mutex completely for the wait and restores the recursion count afterwards: _mutex_cv_unlock / _mutex_cv_detach save m_count, set it to 0 and unlock, and _mutex_cv_lock / _mutex_cv_attach put it back (thr_mutex.c, thr_cond.c cond_wait_kernel / cond_wait_user).

The fix unlocks the remaining N-1 native levels before the wait and locks them again after it. This covers scePthreadCondWait, scePthreadCondTimedwait, pthread_cond_wait and pthread_cond_timedwait, which all go through waitUntil. The FreeBSD behaviour is recorded in TechnicalDebt as unverified on hardware.

guest_cond_recursive_mutex locks a recursive mutex twice and waits with each of the four functions while another thread locks the mutex and signals. After the wake the waiter unlocks twice and a third unlock returns SCE_KERNEL_ERROR_EPERM.

This PR was investigated and written by Claude Code (AI), and posted by me after review.

Tested

Arch Linux, GCC, Release, -DBUILD_TESTING=ON, APS5_WRITE_WATCH_IMPORTS=watch (Intel UHD 630 + GTX 1050 Ti).

  • guest_cond_recursive_mutex: passes; with only the Cond.cpp change reverted it fails (ctest timeout, the signalling thread never gets the mutex).
  • Full ctest -j4: 424/424 passed, 0 skipped, 0 failed (main bc1fbe81: 423/423, 0 skipped; the new test is the only difference).
  • Full suite under VK_LAYER_KHRONOS_validation: 424/424 passed, 0 skipped; 27 Validation Error messages with the same VUIDs and counts as main (52 VUID-vkDestroyDevice-device-05137, 2 VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605).
  • tools/check_conventions.py: passes.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

https://claude.ai/code/session_01HeC9bA3n74TzJRYv9AgBE8

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4784305364 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1480). Originally posted by [awake-devel](https://github.com/awake-devel) on 2026-10-08T08:54:49Z. --- ### What A condition wait on a recursive mutex that the waiter has locked more than once deadlocks. `waitUntil` in `Pthread/src/Cond.cpp` cleared `_count` and `_owner`, but the condition variable unlocks the native `std::recursive_timed_mutex` only once. Every `scePthreadMutexLock` on a recursive mutex locks the native mutex again, so after N locks the waiter still holds N-1 levels during the wait. The thread that should take the mutex and signal blocks in `scePthreadMutexLock`, and the waiter never wakes (an untimed wait hangs; a timed wait times out forever). FreeBSD libthr, which the PS5 libkernel is based on, releases the mutex completely for the wait and restores the recursion count afterwards: `_mutex_cv_unlock` / `_mutex_cv_detach` save `m_count`, set it to 0 and unlock, and `_mutex_cv_lock` / `_mutex_cv_attach` put it back ([thr_mutex.c](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/lib/libthr/thread/thr_mutex.c), [thr_cond.c](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/lib/libthr/thread/thr_cond.c) `cond_wait_kernel` / `cond_wait_user`). The fix unlocks the remaining N-1 native levels before the wait and locks them again after it. This covers `scePthreadCondWait`, `scePthreadCondTimedwait`, `pthread_cond_wait` and `pthread_cond_timedwait`, which all go through `waitUntil`. The FreeBSD behaviour is recorded in TechnicalDebt as unverified on hardware. `guest_cond_recursive_mutex` locks a recursive mutex twice and waits with each of the four functions while another thread locks the mutex and signals. After the wake the waiter unlocks twice and a third unlock returns `SCE_KERNEL_ERROR_EPERM`. This PR was investigated and written by Claude Code (AI), and posted by me after review. ### Tested Arch Linux, GCC, Release, `-DBUILD_TESTING=ON`, `APS5_WRITE_WATCH_IMPORTS=watch` (Intel UHD 630 + GTX 1050 Ti). - `guest_cond_recursive_mutex`: passes; with only the `Cond.cpp` change reverted it fails (ctest timeout, the signalling thread never gets the mutex). - Full `ctest -j4`: 424/424 passed, 0 skipped, 0 failed (`main` bc1fbe81: 423/423, 0 skipped; the new test is the only difference). - Full suite under `VK_LAYER_KHRONOS_validation`: 424/424 passed, 0 skipped; 27 `Validation Error` messages with the same VUIDs and counts as `main` (52 `VUID-vkDestroyDevice-device-05137`, 2 `VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605`). - `tools/check_conventions.py`: passes. ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01HeC9bA3n74TzJRYv9AgBE8
oneandonlydean commented 2026-10-09 09:56:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T09:56:08Z.


Could you rebase onto current main? The full build checks out this branch with main's workflow, and it stops at tools/check_test_registration.py not being on the branch yet, so nothing gets compiled. After a rebase it can get a full build and go in.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078616334 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1480#issuecomment-6078616334). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T09:56:08Z. --- Could you rebase onto current main? The full build checks out this branch with main's workflow, and it stops at `tools/check_test_registration.py` not being on the branch yet, so nothing gets compiled. After a rebase it can get a full build and go in.
popcoder565 commented 2026-10-09 15:45:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T15:45:23Z.


Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head 7406c975, merged onto main d70b8998 in one build with 38 other PRs that had no Windows run.

  • This PR's tests pass: guest_cond_recursive_mutex. None of them reported skipping a case.
  • Full ctest in that build: 519 of 519. That needed local fixes to three other PRs in the batch that don't build or pass on Windows: #1611, #1796 and #1824 (reported there).

No title was run.

🤖 Addressed by Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6084254864 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1480#issuecomment-6084254864). Originally posted by [popcoder565](https://github.com/popcoder565) on 2026-10-09T15:45:23Z. --- Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head `7406c975`, merged onto `main` d70b8998 in one build with 38 other PRs that had no Windows run. - This PR's tests pass: `guest_cond_recursive_mutex`. None of them reported skipping a case. - Full ctest in that build: 519 of 519. That needed local fixes to three other PRs in the batch that don't build or pass on Windows: #1611, #1796 and #1824 (reported there). No title was run. _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_
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!1480
No description provided.