fix(agc): release parked host imports a pinned guest range waits for #4291

Merged
diversao merged 1 commit from upstream-pr/4291 into main 2026-10-12 10:52:41 +00:00
diversao commented 2026-10-12 05:03:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by diversao on 2026-10-12T05:03:13Z.


What

A guest munmap/mprotect/free of a range that a replaced Vulkan device had host-imported waits out the whole pin wait (APS5_PIN_WAIT_MS, 60 s) and then fails with EINVAL.

Driver::AttachWindow replaces a device that was created before the window (PrepareForReplacement drains it with vkDeviceWaitIdle) and keeps it in replacedDevices. The next refreshImports from the new device moves the old device's imports into HostImports::parked. Each parked RetiredImport keeps its HostImport::ranges, so the guest allocation stays pinned. parked is only cleared by ClearHostImports when that device is destroyed, which does not happen before shutdown. The pin waiter (WaitForLeasesAndImports) never looks at parked. Every round finds the recorder idle and no live import over the range, yields, and the guest thread spins until the deadline.

releaseParkedImports drops the parked imports that overlap the range being changed, in both branches of the waiter, and reports progress so the registry is rescanned. A parked import belongs to a device that PrepareForReplacement already waited idle, so no GPU work can still read it.

New test agc_parked_import_unmap (--parked-import-unmap-only): imports a guest range on one device, imports it again on a second device (which parks the first import), then unmaps it with APS5_PIN_WAIT_MS=3000. On main it fails after 3 s with guest allocation ... is still pinned by GPU work or an import (waited 3000 ms for the lease (APS5_PIN_WAIT_MS)). With the change it passes at once.

Tested

Windows 11 Pro 26200, NVIDIA RTX 4070 (driver 617.14), i5-12600KF, WinLibs GCC 15.2.0 r7, Release.

  • agc_parked_import_unmap: fails on main after 3.7 s with the still pinned error above, passes with the change in 0.8 s.
  • Full ctest: 935 of 937 passed. hardware_oracle needs an AMD GPU on Linux. agc_driver_prepared_shaders_deferred_undecodable exited with 0xc0000374 once under --parallel 6; alone it passed 5 of 5 times with this change and 5 of 5 without it. It does not touch host imports.
  • tools/check_conventions.py --base origin/main: passed.
  • Homebrew ps5-opengl OpenGL 4.6 Showcase (PPSA99005 v1.0.1, catalog zip, sha256 d16eb7f779d382d21e7f3374a3871e54cce3dea150a9e7de1603a9011833c9f9; eboot.bin and sce_module/libc.prx unwrapped from fSELF), on main 34a16b7f0 with #4030 applied for its flat interpolation stop. The title initialises AGC before its window exists, so a headless device is replaced at AttachWindow. Its first munmap(0x21f3d8000, 0x10000) then waited 60 s (about 1 million waiter rounds per second, all idle, no live import over the range, one parked import over it) with a black window, and returned EINVAL. With the change the first frame is presented at once. Diagnosis: APS5_NO_HOST_IMPORT=1 removes the wait. A local counter in the waiter showed parkedOverlap=1 on every round.
  • Homebrew ps5-doom v1.1.1, same build: unchanged, launcher at 59.9 fps.

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 (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4825142000 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4291). Originally posted by [diversao](https://github.com/diversao) on 2026-10-12T05:03:13Z. --- ### What A guest `munmap`/`mprotect`/free of a range that a replaced Vulkan device had host-imported waits out the whole pin wait (`APS5_PIN_WAIT_MS`, 60 s) and then fails with `EINVAL`. `Driver::AttachWindow` replaces a device that was created before the window (`PrepareForReplacement` drains it with `vkDeviceWaitIdle`) and keeps it in `replacedDevices`. The next `refreshImports` from the new device moves the old device's imports into `HostImports::parked`. Each parked `RetiredImport` keeps its `HostImport::ranges`, so the guest allocation stays pinned. `parked` is only cleared by `ClearHostImports` when that device is destroyed, which does not happen before shutdown. The pin waiter (`WaitForLeasesAndImports`) never looks at `parked`. Every round finds the recorder idle and no live import over the range, yields, and the guest thread spins until the deadline. `releaseParkedImports` drops the parked imports that overlap the range being changed, in both branches of the waiter, and reports progress so the registry is rescanned. A parked import belongs to a device that `PrepareForReplacement` already waited idle, so no GPU work can still read it. New test `agc_parked_import_unmap` (`--parked-import-unmap-only`): imports a guest range on one device, imports it again on a second device (which parks the first import), then unmaps it with `APS5_PIN_WAIT_MS=3000`. On `main` it fails after 3 s with `guest allocation ... is still pinned by GPU work or an import (waited 3000 ms for the lease (APS5_PIN_WAIT_MS))`. With the change it passes at once. ### Tested Windows 11 Pro 26200, NVIDIA RTX 4070 (driver 617.14), i5-12600KF, WinLibs GCC 15.2.0 r7, Release. - `agc_parked_import_unmap`: fails on `main` after 3.7 s with the `still pinned` error above, passes with the change in 0.8 s. - Full `ctest`: 935 of 937 passed. `hardware_oracle` needs an AMD GPU on Linux. `agc_driver_prepared_shaders_deferred_undecodable` exited with 0xc0000374 once under `--parallel 6`; alone it passed 5 of 5 times with this change and 5 of 5 without it. It does not touch host imports. - `tools/check_conventions.py --base origin/main`: passed. - Homebrew [ps5-opengl](https://github.com/blackbearreloaded/ps5-opengl) OpenGL 4.6 Showcase (PPSA99005 v1.0.1, catalog zip, sha256 `d16eb7f779d382d21e7f3374a3871e54cce3dea150a9e7de1603a9011833c9f9`; `eboot.bin` and `sce_module/libc.prx` unwrapped from fSELF), on `main` 34a16b7f0 with #4030 applied for its flat interpolation stop. The title initialises AGC before its window exists, so a headless device is replaced at `AttachWindow`. Its first `munmap(0x21f3d8000, 0x10000)` then waited 60 s (about 1 million waiter rounds per second, all `idle`, no live import over the range, one parked import over it) with a black window, and returned `EINVAL`. With the change the first frame is presented at once. Diagnosis: `APS5_NO_HOST_IMPORT=1` removes the wait. A local counter in the waiter showed `parkedOverlap=1` on every round. - Homebrew [ps5-doom](https://github.com/lowbit/ps5-doom) v1.1.1, same build: unchanged, launcher at 59.9 fps. ### 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 (Claude Code) 🤖 Generated with [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!4291
No description provided.