fix(agc): keep another device's host imports until its teardown #3976

Merged
pipe1os merged 2 commits from upstream-pr/3976 into main 2026-10-11 21:43:05 +00:00
pipe1os commented 2026-10-11 20:20:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pipe1os on 2026-10-11T20:20:24Z.


What

Fixes #3503.

HostImports is one process-wide state bound to one VkDevice. When another device reconciles it, refreshImports destroyed every import of the previous device at once with vkDestroyBuffer/vkFreeMemory, even though that device's submitted batches could still read them.

agc_driver_depth_fast_clear_submit uses two live devices: the test's own VulkanDevice for draws and the driver's device for the DCB submissions. AgcDriverWaitIdle waits for packet processing, not for the GPU, so the driver's DMA_DATA fill (a vkCmdFillBuffer into the HTILE block's import) can still be queued when the test device's next draw reaches UploadFinish → refreshImports. lavapipe then executes the fill on a destroyed buffer:

[segv] sig=11 addr=0x54 rip=libvulkan_lvp.so+0x38f964
llvmpipe_is_resource_referenced  lp_texture.c:1180
llvmpipe_flush_resource          lp_flush.c:128
llvmpipe_transfer_map_ms         lp_texture.c:935
llvmpipe_clear_buffer            lp_surface.c:489
handle_fill_buffer               lvp_execute.c:2590
lvp_execute_cmd_buffer           lvp_execute.c:5081
lvp_queue_submit                 lvp_device.c:1719
vk_queue_submit_thread_func      vk_queue.c:833

(Mesa 25.2.8-0ubuntu0.24.04.4, symbolized with mesa-vulkan-drivers-dbgsym.) The Khronos validation layer reports it on the same runs:

VUID-vkDestroyBuffer-buffer-00922: vkDestroyBuffer(): can't be called on VkBuffer ... that is currently in use by VkCommandBuffer ...

with the destroy coming from GuestBufferMemory::UploadFinish → refreshImports on the test device's Draw.

The previous device's imports are now kept as RetiredImports per device and destroyed by ClearHostImports of that device, which its VulkanDevice teardown calls after vkDeviceWaitIdle and before vkDestroyDevice. With one device per process nothing changes: the switch happens on device replacement, and the old device's teardown frees the parked imports as before. RetiredImport is forward-declared and held through shared_ptr, so HostImports and Imports() stay where they are; with #2434 the file merges without conflicts.

The other device-keyed caches (Chunks, Mirrors, BDA Tables) only drop shared_ptr/weak_ptr references on a switch, so batches that keep their objects are unaffected; HostImports was the one holding raw handles.

Tested

Linux, Docker ubuntu:24.04 mirroring the CI linux job (GCC 13.3.0, Mesa 25.2.8 lavapipe, GALLIUM_OVERRIDE_CPU_CAPS=avx, ANYPS5_REQUIRE_VULKAN=1, Release, -DANYPS5_ENABLE_SPIRV_TOOLS=ON), seccomp=unconfined so the userfaultfd write watch is active as in CI, 4 CPUs. Tree: main a11c8f7f7 (this file is unchanged up to current main).

  • 8 full ctest --parallel 4 runs did not reproduce it. The test alone, 300 runs, cleared shader cache each run, with 8 busy-loop processes on the 4 CPUs:
    • without the change: 27 of 300 crash (SIGSEGV after 0.4-2.1 s, the same site every time); under the validation layer VUID-vkDestroyBuffer-buffer-00922 appears in the crashing runs;
    • with the change: 0 of 300 crash; 150 runs under the validation layer report no 00922 (or any other VUID besides the old layer's unknown pNext structure at device creation).
  • Full ctest --parallel 4 with the change: 838 tests, the same 4 failures as without it (agc_driver_emulated_color_compare #3526, agc_driver_image_plane_address_on_line_implicit_lod #3630, guest_kernel_stack_protect #3459, guest_truncate, which also fails on unchanged main in this container).
  • python3 tools/check_conventions.py --base origin/main passes.

Windows not tested (the change is platform-independent; RetiredImport keeps the Windows alias unmap it already had).

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:4822050919 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3976). Originally posted by [pipe1os](https://github.com/pipe1os) on 2026-10-11T20:20:24Z. --- ### What Fixes #3503. `HostImports` is one process-wide state bound to one `VkDevice`. When another device reconciles it, `refreshImports` destroyed every import of the previous device at once with `vkDestroyBuffer`/`vkFreeMemory`, even though that device's submitted batches could still read them. `agc_driver_depth_fast_clear_submit` uses two live devices: the test's own `VulkanDevice` for draws and the driver's device for the DCB submissions. `AgcDriverWaitIdle` waits for packet processing, not for the GPU, so the driver's `DMA_DATA` fill (a `vkCmdFillBuffer` into the HTILE block's import) can still be queued when the test device's next draw reaches `UploadFinish` → `refreshImports`. lavapipe then executes the fill on a destroyed buffer: ``` [segv] sig=11 addr=0x54 rip=libvulkan_lvp.so+0x38f964 llvmpipe_is_resource_referenced lp_texture.c:1180 llvmpipe_flush_resource lp_flush.c:128 llvmpipe_transfer_map_ms lp_texture.c:935 llvmpipe_clear_buffer lp_surface.c:489 handle_fill_buffer lvp_execute.c:2590 lvp_execute_cmd_buffer lvp_execute.c:5081 lvp_queue_submit lvp_device.c:1719 vk_queue_submit_thread_func vk_queue.c:833 ``` (Mesa 25.2.8-0ubuntu0.24.04.4, symbolized with `mesa-vulkan-drivers-dbgsym`.) The Khronos validation layer reports it on the same runs: ``` VUID-vkDestroyBuffer-buffer-00922: vkDestroyBuffer(): can't be called on VkBuffer ... that is currently in use by VkCommandBuffer ... ``` with the destroy coming from `GuestBufferMemory::UploadFinish` → `refreshImports` on the test device's `Draw`. The previous device's imports are now kept as `RetiredImport`s per device and destroyed by `ClearHostImports` of that device, which its `VulkanDevice` teardown calls after `vkDeviceWaitIdle` and before `vkDestroyDevice`. With one device per process nothing changes: the switch happens on device replacement, and the old device's teardown frees the parked imports as before. `RetiredImport` is forward-declared and held through `shared_ptr`, so `HostImports` and `Imports()` stay where they are; with #2434 the file merges without conflicts. The other device-keyed caches (`Chunks`, `Mirrors`, BDA `Tables`) only drop `shared_ptr`/`weak_ptr` references on a switch, so batches that keep their objects are unaffected; `HostImports` was the one holding raw handles. ### Tested Linux, Docker `ubuntu:24.04` mirroring the CI `linux` job (GCC 13.3.0, Mesa 25.2.8 lavapipe, `GALLIUM_OVERRIDE_CPU_CAPS=avx`, `ANYPS5_REQUIRE_VULKAN=1`, Release, `-DANYPS5_ENABLE_SPIRV_TOOLS=ON`), `seccomp=unconfined` so the userfaultfd write watch is active as in CI, 4 CPUs. Tree: `main` `a11c8f7f7` (this file is unchanged up to current `main`). - 8 full `ctest --parallel 4` runs did not reproduce it. The test alone, 300 runs, cleared shader cache each run, with 8 busy-loop processes on the 4 CPUs: - without the change: 27 of 300 crash (SIGSEGV after 0.4-2.1 s, the same site every time); under the validation layer `VUID-vkDestroyBuffer-buffer-00922` appears in the crashing runs; - with the change: 0 of 300 crash; 150 runs under the validation layer report no `00922` (or any other VUID besides the old layer's unknown `pNext` structure at device creation). - Full `ctest --parallel 4` with the change: 838 tests, the same 4 failures as without it (`agc_driver_emulated_color_compare` #3526, `agc_driver_image_plane_address_on_line_implicit_lod` #3630, `guest_kernel_stack_protect` #3459, `guest_truncate`, which also fails on unchanged `main` in this container). - `python3 tools/check_conventions.py --base origin/main` passes. Windows not tested (the change is platform-independent; `RetiredImport` keeps the Windows alias unmap it already had). ### 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 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
Kikoarl commented 2026-10-11 21:02:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-11T21:02:57Z.


Tested on Linux (CachyOS x86_64) on real hardware with an NVIDIA GeForce RTX 5060 Ti (driver 615.78.08, Vulkan 1.4):

  • agc_driver_depth_fast_clear_submit: ran 10 consecutive stress runs (--repeat until-fail:10) on physical NVIDIA hardware — 10/10 passed with zero crashes or validation warnings.
  • All depth driver tests (agc_driver_depth_*) pass 100%.
  • Verified clean with tools/pr_overlap.py: no merge conflicts with open PRs.
  • tools/check_conventions.py passes clean.

Validates the fix on physical NVIDIA hardware beyond lavapipe. LGTM!

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6113721809 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3976#issuecomment-6113721809). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-11T21:02:57Z. --- Tested on **Linux (CachyOS x86_64)** on real hardware with an **NVIDIA GeForce RTX 5060 Ti (driver 615.78.08, Vulkan 1.4)**: - `agc_driver_depth_fast_clear_submit`: ran 10 consecutive stress runs (`--repeat until-fail:10`) on physical NVIDIA hardware — **10/10 passed** with zero crashes or validation warnings. - All depth driver tests (`agc_driver_depth_*`) pass 100%. - Verified clean with `tools/pr_overlap.py`: no merge conflicts with open PRs. - `tools/check_conventions.py` passes clean. Validates the fix on physical NVIDIA hardware beyond lavapipe. LGTM!
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!3976
No description provided.