fix(agc): retire host imports before guest unmap #2575

Merged
oneandonlydean merged 3 commits from upstream-pr/2575 into main 2026-10-10 07:44:27 +00:00
oneandonlydean commented 2026-10-10 06:49:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-10T06:49:17Z.


Carrying @MarcZX100's fix for #2352 while PR intake is paused. The change is theirs (from MarcZX100/AnyPS5:fix/host-import-unmap-lifetime, c6bffc00), with its two commits squashed and rebased onto current main. A second commit adds the two-line follow-up that @popcoder565 found and @zaydzaari confirmed in the issue.

What

  • Unmap order (MarcZX100): the pin waiter now receives the mutation range. Before the unmap callback releases the pages, it drains GPU work and retires every overlapping host import. A host import keeps a strong reference to its registered range until it is retired. Deferred retirement keeps only the device handle and the Vulkan destroy functions, so the static import registry no longer holds a Context or its BufferPool past the device. agc_driver_recorder_tests --host-import-unmap-only checks that the import is already gone inside the unmap callback, before the block is released.
  • Follow-up: refreshImports loads vkDestroyBuffer and vkFreeMemory when they are still null, not only when the device changes. A context without a device, like the graphics test's mock, otherwise never loaded them, so retiring an import called a null pointer (agc_driver_graphics segfaulted at rip 0, through importCrossingTests → GuestAllocationsRemove → WaitForLeasesAndImports → retireImport → ~RetiredImport).

Why

Fixes #2352. A guest unmap released host-imported pages before the import that covers them was freed. The Vulkan spec requires the imported range to stay valid for the import's lifetime. On Windows with NVIDIA 616.92, freeing an import whose pages are already gone loses the device when the release follows the import closely: 12 of 60 runs at about 10 µs, 1 of 20 at 1 ms, and 0 when the import is retired first.

Tested

  • MarcZX100 (Linux x86-64, GCC 12.2, lavapipe, on main aad16d05): agc_driver_recorder_tests --host-import-unmap-only passes, and agc_driver_sampled_cache_budget and _memory pass.
  • popcoder565 (Windows 10, RTX 3060 Laptop, NVIDIA 616.92, MinGW GCC 15.2): the unmap probe lost the device in 0 of 40 runs (7 of 40 on main), with the import already retired at release in every run. agc_driver_recorder_tests passed 20/20, --host-import-unmap-only 10/10, and the sampled-cache tests 5/5 each, with no exit crash. With the follow-up, agc_driver_graphics passed 5/5 and the full ctest 543/543.
  • zaydzaari (Windows 11, RTX 5070, NVIDIA 610.47): with the follow-up only, agc_driver_graphics_tests passed 5/5, --host-import-unmap-only 10/10, and the sampled-cache, guest_memory_tests, agc_write_tracking_tests and guest_agc_driver_resources_tests passed.
  • On current main 092e133e: the branch merges cleanly and tools/check_conventions.py passes. CI runs the build and tests.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4809369528 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2575). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-10T06:49:17Z. --- Carrying @MarcZX100's fix for #2352 while PR intake is paused. The change is theirs (from `MarcZX100/AnyPS5:fix/host-import-unmap-lifetime`, c6bffc00), with its two commits squashed and rebased onto current `main`. A second commit adds the two-line follow-up that @popcoder565 found and @zaydzaari confirmed in the issue. ### What - **Unmap order (MarcZX100):** the pin waiter now receives the mutation range. Before the unmap callback releases the pages, it drains GPU work and retires every overlapping host import. A host import keeps a strong reference to its registered range until it is retired. Deferred retirement keeps only the device handle and the Vulkan destroy functions, so the static import registry no longer holds a `Context` or its `BufferPool` past the device. `agc_driver_recorder_tests --host-import-unmap-only` checks that the import is already gone inside the unmap callback, before the block is released. - **Follow-up:** `refreshImports` loads `vkDestroyBuffer` and `vkFreeMemory` when they are still null, not only when the device changes. A context without a device, like the graphics test's mock, otherwise never loaded them, so retiring an import called a null pointer (`agc_driver_graphics` segfaulted at `rip 0`, through `importCrossingTests` → `GuestAllocationsRemove` → `WaitForLeasesAndImports` → `retireImport` → `~RetiredImport`). ### Why Fixes #2352. A guest unmap released host-imported pages before the import that covers them was freed. The Vulkan spec requires the imported range to stay valid for the import's lifetime. On Windows with NVIDIA 616.92, freeing an import whose pages are already gone loses the device when the release follows the import closely: 12 of 60 runs at about 10 µs, 1 of 20 at 1 ms, and 0 when the import is retired first. ### Tested - MarcZX100 (Linux x86-64, GCC 12.2, lavapipe, on `main` aad16d05): `agc_driver_recorder_tests --host-import-unmap-only` passes, and `agc_driver_sampled_cache_budget` and `_memory` pass. - popcoder565 (Windows 10, RTX 3060 Laptop, NVIDIA 616.92, MinGW GCC 15.2): the unmap probe lost the device in 0 of 40 runs (7 of 40 on `main`), with the import already retired at release in every run. `agc_driver_recorder_tests` passed 20/20, `--host-import-unmap-only` 10/10, and the sampled-cache tests 5/5 each, with no exit crash. With the follow-up, `agc_driver_graphics` passed 5/5 and the full ctest 543/543. - zaydzaari (Windows 11, RTX 5070, NVIDIA 610.47): with the follow-up only, `agc_driver_graphics_tests` passed 5/5, `--host-import-unmap-only` 10/10, and the sampled-cache, `guest_memory_tests`, `agc_write_tracking_tests` and `guest_agc_driver_resources_tests` passed. - On current `main` 092e133e: the branch merges cleanly and `tools/check_conventions.py` passes. CI runs the build and tests. ### 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
github-actions[bot] commented 2026-10-10 06:52:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T06:52:04Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#1337 core/libs/prx/libSceAgcDriver/tests/Recorder.cpp L3610
#1961 core/libs/prx/libc/include/GuestAllocations.hpp L51
core/libs/prx/libc/src/GuestAllocations.cpp L46, L131-132
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094836643 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2575#issuecomment-6094836643). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T06:52:04Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #1337 | `core/libs/prx/libSceAgcDriver/tests/Recorder.cpp` [L3610](https://github.com/boykopovar/AnyPS5/blob/4e9d298eac26dbe65acec2d80309f36ed7fcac79/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp#L3610) | | | | #1961 | `core/libs/prx/libc/include/GuestAllocations.hpp` [L51](https://github.com/boykopovar/AnyPS5/blob/4e9d298eac26dbe65acec2d80309f36ed7fcac79/core/libs/prx/libc/include/GuestAllocations.hpp#L51)<br>`core/libs/prx/libc/src/GuestAllocations.cpp` [L46](https://github.com/boykopovar/AnyPS5/blob/4e9d298eac26dbe65acec2d80309f36ed7fcac79/core/libs/prx/libc/src/GuestAllocations.cpp#L46), [L131-132](https://github.com/boykopovar/AnyPS5/blob/4e9d298eac26dbe65acec2d80309f36ed7fcac79/core/libs/prx/libc/src/GuestAllocations.cpp#L131-L132) | | |
adriadam10 commented 2026-10-10 07:42:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T07:42:02Z.


Checked on Linux (lavapipe): clean rebase onto current main, agc_driver_graphics, agc_driver_host_import_teardown, agc_driver_sampled_cache_budget_memory and guest_memory pass, and --host-import-unmap-only passes. The Windows/NVIDIA results from @popcoder565 and @zaydzaari cover the device-loss case, which we can't run here.

Added one commit:

  • agc_driver_recorder_tests --host-import-unmap-only was not registered in ctest, so CI never ran it. Registered it as agc_host_import_unmap (skip code 77, same as the neighbouring entries). With the non-test changes reverted it fails, so it pins the fix.

Merging on green CI.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6095224209 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2575#issuecomment-6095224209). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T07:42:02Z. --- Checked on Linux (lavapipe): clean rebase onto current `main`, `agc_driver_graphics`, `agc_driver_host_import_teardown`, `agc_driver_sampled_cache_budget_memory` and `guest_memory` pass, and `--host-import-unmap-only` passes. The Windows/NVIDIA results from @popcoder565 and @zaydzaari cover the device-loss case, which we can't run here. Added one commit: - `agc_driver_recorder_tests --host-import-unmap-only` was not registered in ctest, so CI never ran it. Registered it as `agc_host_import_unmap` (skip code 77, same as the neighbouring entries). With the non-test changes reverted it fails, so it pins the fix. Merging on green CI.
nikhilpanchal1 commented 2026-10-10 14:40:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nikhilpanchal1 on 2026-10-10T14:40:09Z.


Follow-up: #2779 covers host-import construction cleanup and mutation-wait failures. Eight isolated cases fail on main@5edae353 and pass with the fix; all 698 existing test outcomes are unchanged, and 450 repeated lifetime checks pass. Retirement ownership changes are excluded. Windows/NVIDIA validation would be especially helpful.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6098644050 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2575#issuecomment-6098644050). Originally posted by [nikhilpanchal1](https://github.com/nikhilpanchal1) on 2026-10-10T14:40:09Z. --- Follow-up: #2779 covers host-import construction cleanup and mutation-wait failures. Eight isolated cases fail on main@5edae353 and pass with the fix; all 698 existing test outcomes are unchanged, and 450 repeated lifetime checks pass. Retirement ownership changes are excluded. Windows/NVIDIA validation would be especially helpful.
nikhilpanchal1 commented 2026-10-10 15:54:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nikhilpanchal1 on 2026-10-10T15:54:28Z.


@adriadam10 Windows/NVIDIA results are now available for #2779: RTX 5070 and RTX 3060 Laptop. The 5070 report reproduced the baseline lease timeout and registry-lock failure, then passed with the fix. The 3060 report notes one intermittent agc_driver crash of unknown relation. Both tested db76c694; the smaller current revision passes 450 Linux lifetime runs. Could you keep this follow-up on the backlog for review?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6099365264 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2575#issuecomment-6099365264). Originally posted by [nikhilpanchal1](https://github.com/nikhilpanchal1) on 2026-10-10T15:54:28Z. --- @adriadam10 Windows/NVIDIA results are now available for #2779: RTX 5070 and RTX 3060 Laptop. The 5070 report reproduced the baseline lease timeout and registry-lock failure, then passed with the fix. The 3060 report notes one intermittent agc_driver crash of unknown relation. Both tested db76c694; the smaller current revision passes 450 Linux lifetime runs. Could you keep this follow-up on the backlog for review?
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!2575
No description provided.