fix(libSceUsbd): retain devices during active USB operations #9803

Closed
Dellz36 wants to merge 7 commits from upstream-pr/9803 into main
Dellz36 commented 2026-10-22 15:39:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dellz36 on 2026-10-22T15:39:43Z.


What

A device query could pass ownership validation and then use a freed host device when another thread released the last guest reference or closed its owning handle. Final USB shutdown could also destroy state while a query, handler or sleeping waiter still used it.

Retain host devices under the state mutex for the complete device-query/open operation, reject final shutdown while those operations are active, preserve initialization ownership after rejected shutdown, and retain event-lock ownership across backend exceptions. Closing a handle with pending transfers now throws before removing its ownership record. Add deterministic threaded lifetime and event regressions.

This is a follow-up to #9339, including its #9338/#9190 stack. The prerequisite commits remain in the branch ancestry; their device/event/descriptor implementations and exports are not claimed as new work here. The authored follow-up is the final commit after the prerequisite merge. This adds no new-export or title-compatibility claim.

General handle-based concurrent-close pinning and automatic transfer cancellation/draining remain unsupported and are documented in debt/libSceUsbd.md.

Official unmodified tools/pr_overlap.py --repo boykopovar/AnyPS5 --branch fix/usb-operation-lifetimes-20261022 reviewed the complete open-PR inventory. Its only remaining warning is base-conflicted #6044: manually reviewed its USB diff; it relocates GuestUsbd.cpp to tests/input and changes test registration/labels, without implementing these lifetime guards. If it merges first, these test paths/registrations need adapting. No agreed maintainer merge order is claimed; #9339 is explicitly a prerequisite.

Tested

  • Linux x86-64, GCC 13.3, Release: built the complete actual libc and libSceUsbd through an external focused CMake harness importing the production per-library definitions and pinned source-built dependencies. This exact USB branch was tested with upstream libc, without the separate local libc changes: 21 USB cases, each repeated five times, 105 passes, zero skips.
  • Windows x86-64, supported WinLibs GCC 15.2.0 UCRT POSIX SEH r7, Release, BUILD_TESTING=ON: fresh root full-project CMake configuration, selected actual libc/libkernel/libSceUsbd/test dependency builds. 57 selected stream/kernel/USB cases pass once and five repeats pass all 285 executions, zero skips. The USB subset is 21 cases. The Windows checkout includes separately retained libc/kernel candidates; all 20 changed-file hashes match their respective reviewed branches. This is focused combined integration, not an independent USB-only Windows build.
  • Six new device-pinning cases use a real second thread to release the last explicit reference or owning handle during the backend call; cover speed, multi-call iso query, host failure, borrowed ownership, open ownership, and exception cleanup. All six abort at the expected lifetime assertion against the exact pre-pinning source on Linux and pass with the guard. Prior bare-#9339 controls also reproduce pending-exit, pending-close and active-handler-exit failures.
  • Normal committed-branch check_conventions.py and git diff --check pass. Official overlap scan and manual shared-file review completed as described above.
  • Windows setup restored hash-verified official WinLibs/PortableGit; reused the matching cached FFmpeg package through documented FFMPEG_PREBUILT_DIR. An external build-only launcher invokes the unchanged libusb gen-describe.sh to avoid the previously observed PowerShell launcher hang; no third-party source was modified.
  • No full default/all-target build, full CTest suite, PS5/title, physical USB transfer, performance or formal independent review claim. Linux root full configuration is blocked by missing XRandR development headers; linked kernel runtime tests hit the VM's existing CPU-feature guard. No guard bypass. Please run the maintainer-only full Windows/Linux workflow for this PR before merge.

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: #9339
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Review follow-up — updated integration and retest

Updated by merging upstream/main fa107a44c71349da4d9e916881d39158a75a783f, including the reviewer-cited 8499ea52e571b0edfc1feecfd3f6b9944012b359. New head: be8a8466bfaa18dc0ac1f09eaf91d39344d06ae2. The existing prerequisite ancestry and #9339 dependency remain intact; this uses a normal fast-forward push without rewriting published commits.

Focused retest on the resulting head: Linux 21 USB cases × 5 = 105 passes; Windows 21 USB cases × 5 = 105 passes, zero skips. Linux rebuilt actual libraries through the previously disclosed focused harness. Windows used supported GCC15.2 with normal root CMake configuration and actual libc/libkernel/libSceUsbd/test dependencies. The Windows production source was refreshed from the exact updated Git archive; 132 recorded libc/USB/fixture hashes match, and compiled production files no longer include the separate local libc/kernel candidates. The last additional upstream merge only changes graphics files; updated those files and reran the USB build/test commands. Conventions and whitespace pass. This is focused validation, not a full-suite or PS5/title claim.

Merge-order coordination with @TaymKal requested at https://github.com/boykopovar/AnyPS5/pull/6044#issuecomment-6347879073. Proposed #9339 → #9803 → #6044; offered to adapt paths/registrations if #6044 goes first. Author confirmation is pending. No agreement is claimed; the agreed order/dependency will be recorded on the checklist before merge. Existing Depends on: #9339 is unchanged.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4970041582 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9803). Originally posted by [Dellz36](https://github.com/Dellz36) on 2026-10-22T15:39:43Z. --- ### What A device query could pass ownership validation and then use a freed host device when another thread released the last guest reference or closed its owning handle. Final USB shutdown could also destroy state while a query, handler or sleeping waiter still used it. Retain host devices under the state mutex for the complete device-query/open operation, reject final shutdown while those operations are active, preserve initialization ownership after rejected shutdown, and retain event-lock ownership across backend exceptions. Closing a handle with pending transfers now throws before removing its ownership record. Add deterministic threaded lifetime and event regressions. This is a follow-up to #9339, including its #9338/#9190 stack. The prerequisite commits remain in the branch ancestry; their device/event/descriptor implementations and exports are not claimed as new work here. The authored follow-up is the final commit after the prerequisite merge. This adds no new-export or title-compatibility claim. General handle-based concurrent-close pinning and automatic transfer cancellation/draining remain unsupported and are documented in debt/libSceUsbd.md. Official unmodified `tools/pr_overlap.py --repo boykopovar/AnyPS5 --branch fix/usb-operation-lifetimes-20261022` reviewed the complete open-PR inventory. Its only remaining warning is base-conflicted #6044: manually reviewed its USB diff; it relocates GuestUsbd.cpp to tests/input and changes test registration/labels, without implementing these lifetime guards. If it merges first, these test paths/registrations need adapting. No agreed maintainer merge order is claimed; #9339 is explicitly a prerequisite. ### Tested - Linux x86-64, GCC 13.3, Release: built the complete actual libc and libSceUsbd through an external focused CMake harness importing the production per-library definitions and pinned source-built dependencies. This exact USB branch was tested with upstream libc, without the separate local libc changes: 21 USB cases, each repeated five times, 105 passes, zero skips. - Windows x86-64, supported WinLibs GCC 15.2.0 UCRT POSIX SEH r7, Release, BUILD_TESTING=ON: fresh root full-project CMake configuration, selected actual libc/libkernel/libSceUsbd/test dependency builds. 57 selected stream/kernel/USB cases pass once and five repeats pass all 285 executions, zero skips. The USB subset is 21 cases. The Windows checkout includes separately retained libc/kernel candidates; all 20 changed-file hashes match their respective reviewed branches. This is focused combined integration, not an independent USB-only Windows build. - Six new device-pinning cases use a real second thread to release the last explicit reference or owning handle during the backend call; cover speed, multi-call iso query, host failure, borrowed ownership, open ownership, and exception cleanup. All six abort at the expected lifetime assertion against the exact pre-pinning source on Linux and pass with the guard. Prior bare-#9339 controls also reproduce pending-exit, pending-close and active-handler-exit failures. - Normal committed-branch check_conventions.py and git diff --check pass. Official overlap scan and manual shared-file review completed as described above. - Windows setup restored hash-verified official WinLibs/PortableGit; reused the matching cached FFmpeg package through documented FFMPEG_PREBUILT_DIR. An external build-only launcher invokes the unchanged libusb gen-describe.sh to avoid the previously observed PowerShell launcher hang; no third-party source was modified. - No full default/all-target build, full CTest suite, PS5/title, physical USB transfer, performance or formal independent review claim. Linux root full configuration is blocked by missing XRandR development headers; linked kernel runtime tests hit the VM's existing CPU-feature guard. No guard bypass. Please run the maintainer-only full Windows/Linux workflow for this PR before merge. <!-- 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. --> ### 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: #9339 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no --> ### Review follow-up — updated integration and retest <!-- anyps5-9803-review-update-20261022 --> Updated by merging upstream/main `fa107a44c71349da4d9e916881d39158a75a783f`, including the reviewer-cited `8499ea52e571b0edfc1feecfd3f6b9944012b359`. New head: `be8a8466bfaa18dc0ac1f09eaf91d39344d06ae2`. The existing prerequisite ancestry and #9339 dependency remain intact; this uses a normal fast-forward push without rewriting published commits. Focused retest on the resulting head: **Linux 21 USB cases × 5 = 105 passes; Windows 21 USB cases × 5 = 105 passes**, zero skips. Linux rebuilt actual libraries through the previously disclosed focused harness. Windows used supported GCC15.2 with normal root CMake configuration and actual libc/libkernel/libSceUsbd/test dependencies. The Windows production source was refreshed from the exact updated Git archive; 132 recorded libc/USB/fixture hashes match, and compiled production files no longer include the separate local libc/kernel candidates. The last additional upstream merge only changes graphics files; updated those files and reran the USB build/test commands. Conventions and whitespace pass. This is focused validation, not a full-suite or PS5/title claim. Merge-order coordination with @TaymKal requested at https://github.com/boykopovar/AnyPS5/pull/6044#issuecomment-6347879073. Proposed #9339 → #9803 → #6044; offered to adapt paths/registrations if #6044 goes first. **Author confirmation is pending.** No agreement is claimed; the agreed order/dependency will be recorded on the checklist before merge. Existing `Depends on: #9339` is unchanged.
MarcZX10 commented 2026-10-22 16:11:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T16:11:34Z.

Review: CHANGES_REQUESTED


The lifetime changes are backed by a strong report: repeated focused Linux and Windows runs, six targeted lifetime cases, and negative controls. I ran check_conventions.py against current upstream/main and git diff --check; both pass. I did not run the USB runtime suite locally; this checkout has no configured build tree, and the product build jobs are skipped on the PR.

Before approval, please address the current integration state:

  1. This head is based on b7d1d47a5f; current upstream/main is 8499ea52e571b0edfc1feecfd3f6b9944012b359 (8 commits ahead). CONTRIBUTING.md requires branches to stay current. The merge tree is currently clean, but please rebase/update and rerun the focused USB test set on the resulting head.
  2. The PR identifies open #6044 as a test-path/registration overlap and says no merge order has been agreed. Please coordinate with that author and record the agreed order or dependency on the checklist before merge. The existing #9339 prerequisite is already documented.

I found no additional blocker in the test and implementation evidence summarized in the description.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5617303250 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9803#pullrequestreview-5617303250). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T16:11:34Z. **Review:** CHANGES_REQUESTED --- The lifetime changes are backed by a strong report: repeated focused Linux and Windows runs, six targeted lifetime cases, and negative controls. I ran `check_conventions.py` against current `upstream/main` and `git diff --check`; both pass. I did not run the USB runtime suite locally; this checkout has no configured build tree, and the product build jobs are skipped on the PR. Before approval, please address the current integration state: 1. This head is based on `b7d1d47a5f`; current `upstream/main` is `8499ea52e571b0edfc1feecfd3f6b9944012b359` (8 commits ahead). `CONTRIBUTING.md` requires branches to stay current. The merge tree is currently clean, but please rebase/update and rerun the focused USB test set on the resulting head. 2. The PR identifies open #6044 as a test-path/registration overlap and says no merge order has been agreed. Please coordinate with that author and record the agreed order or dependency on the checklist before merge. The existing #9339 prerequisite is already documented. I found no additional blocker in the test and implementation evidence summarized in the description.
MarcZX10 commented 2026-11-14 10:06:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-14T10:06:54Z.

Review: APPROVED


Reviewed — looks good

The changed USB lifetime guards and the added regression coverage are consistent with the stated goal; focused checks are reported by the author, but I did not run them.

What I ran

Reviewed head be8a8466bfaa18dc0ac1f09eaf91d39344d06ae2 using the prepared diff and repo_read.py for targeted source ranges. No commands or tests were run. The description reports Linux and Windows focused USB runs; those are author-reported, not independently verified.

Results

Check Result Note
Diff and targeted source review passed Ownership, event, and packet-query paths read
Tests in this review not run Focused runs are author-reported

Findings

Blocking

None.

Non-blocking

None.

Not verified

The full text of all changed files was not read. Targeted ranges were read from core/libs/prx/libSceUsbd/Export.cpp (lines 1–60, 60–185, 185–246, 246–365, 365–425, 425–580, 580–705, and 760–958), and core/libs/tests/UsbdPacketQueries.cpp (150–270). Other changed files and source ranges were not reviewed. No tests, builds, console behavior, or author-reported test results were independently verified. The review bundle’s files.json was not available through the reader, so exclusions could not be confirmed; manifest.json lists changed files, but its complete inventory was not inspected.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5903647040 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9803#pullrequestreview-5903647040). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-14T10:06:54Z. **Review:** APPROVED --- # Reviewed — looks good The changed USB lifetime guards and the added regression coverage are consistent with the stated goal; focused checks are reported by the author, but I did not run them. ## What I ran Reviewed head `be8a8466bfaa18dc0ac1f09eaf91d39344d06ae2` using the prepared diff and `repo_read.py` for targeted source ranges. No commands or tests were run. The description reports Linux and Windows focused USB runs; those are author-reported, not independently verified. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted source review | passed | Ownership, event, and packet-query paths read | | Tests in this review | not run | Focused runs are author-reported | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The full text of all changed files was not read. Targeted ranges were read from `core/libs/prx/libSceUsbd/Export.cpp` (lines 1–60, 60–185, 185–246, 246–365, 365–425, 425–580, 580–705, and 760–958), and `core/libs/tests/UsbdPacketQueries.cpp` (150–270). Other changed files and source ranges were not reviewed. No tests, builds, console behavior, or author-reported test results were independently verified. The review bundle’s `files.json` was not available through the reader, so exclusions could not be confirmed; `manifest.json` lists changed files, but its complete inventory was not inspected. ## To get this to a good state Nothing is required.

Pull request closed

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!9803
No description provided.