fix(agc): keep a host import dropped mid-hold for every batch that hold opens #4385

Merged
AlxcBTW merged 5 commits from upstream-pr/4385 into main 2026-10-12 18:34:50 +00:00
AlxcBTW commented 2026-10-12 08:30:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T08:30:33Z.


What

A host import that refreshImports or importAllocation drops in the middle of a GpuMutex hold can be freed before the GPU runs the commands that the same hold still records with it. The GPU then reads an unmapped page and the device is lost. In Sword of the Sea (PPSA25344, UE5) this happens in about 1 of 8 start-ups, at the first scene frame (VK_ERROR_DEVICE_LOST, nvlddmkm 153 on NVIDIA).

Mechanism.

  1. A GpuMutex hold takes a const HostImport*:
    • a build's in-place region (UploadFinish flushes pending images between regions, and a flush can reconcile the imports);
    • an indirect argument or count import between the decision and the record;
    • a draw's vertex or index import.
  2. Later in the same hold, a reconcile drops that import, because its registered run changed (a title growing a heap in place). retireImport erased its map node and handed its RetiredImport holder to the batch open at that moment. If the recorder was idle, it destroyed the holder at once.
  3. The hold then records with the pointer it took. If a Submit or Sync came in between, the command lands in a later batch. The import is freed when the earlier batch completes, before the later batch runs. With an idle recorder the import is gone before the command is even recorded. In both cases the pointer names an erased map node.

Deterministic evidence: test(agc). agc_host_import_retire (mock device, no GPU) grows an imported run and reconciles inside one hold. It covers two cases:

  • with the recorder idle;
  • with the recorder busy, plus a Submit between the batch that keeps the import and a later batch of the same hold, where the keeping batch completes first.

In both cases the dropped import must stay alive until every batch of the hold has completed. On main both checks fail:

FAIL: an import dropped with the recorder idle was destroyed at once, while the hold that took it can still record with it
FAIL: the dropped import was destroyed at the hold's unlock, while a later batch of the hold that may use it is in flight

Field evidence: Sword of the Sea start-up.

  • VK_EXT_device_fault reports READ_INVALID at one page in every device loss.
  • With VK_EXT_device_address_binding_report logged (6 device losses), that page lay inside a host import retired 2.2-3.0 s before the device-loss report each time. The report lags the fault by about 2 s.
  • The import was either the title's GPU buffer/label heap at guest 0x1e40000000, or its shader code region at 0x1540000000.
  • The heap grows in about seven steps during start-up (0xd70000 -> 0x1900000 bytes). Each step changes the registered run, so the next reconcile drops the import and makes a larger one.
  • Within 11-20 ms the freed range was reused by a new 1920x1080 storage texture.
  • In an experiment build that held every dropped import for 10 s, 0 of 26 start-ups lost the device, against 2 of 12 without the hold in the same interleaved series.

fix(agc).

  • retireImport moves the dropped import's map node into its holder (std::map::extract), so pointers taken earlier stay valid.
  • The holder is kept by every batch the same hold opens: the new Recorder::KeepThroughHold, tied to the hold by GuestMemory::GpuHoldSerial, a count of outermost GpuMutex acquisitions. Whatever the hold records with the import completes before the import is destroyed.
  • Drops for pages the guest is releasing (the lease waiter's two retires) keep the old lifetime. FlushDeferredReleases also lets go of what an ended hold kept, so the waiter is not left waiting on pinned ranges.

Related. #290 also drops imports (least-recent eviction under the budget). If both land, its evictions should go through retireImport so they get the same lifetime. That is how we run the two together in our Sword of the Sea test stack.

Tested

Windows 11 (26200), WinLibs GCC 15.2.0, Release, on main d06c06565.

  • main plus only the test commit: agc_host_import_retire fails with the two messages above (Lenovo node, non-GPU suite, on d146ec1b6; the retire path is the same in d06c06565).
  • With the fix: agc_host_import_retire passes (Lenovo node).
  • Non-GPU suite (ctest -E "^agc_driver|^video_out|^guest_video", Lenovo node): 587 of 588 pass. The one failure, log_macros ("a short line differs"), fails the same way on main with only the test commit.
  • GPU tests on NVIDIA (RTX 5070 Laptop, driver 616.92, AMD iGPU hidden): agc_driver_recorder (full, and --host-import-unmap-only), agc_driver_host_import_at_map, agc_driver_host_import_teardown, agc_driver_sampled_cache_budget, agc_driver_graphics and agc_host_import_retire: 7 of 7 pass.
  • Conventions: tools/check_conventions.py --base origin/main passes.
  • Sword of the Sea start-up A/B (not conclusive): 16 start-ups with the fix and 16 without, interleaved, 40 s each. The device-loss rate comes in clusters, and this series fell mostly in a quiet phase: 0 of 16 with the fix, 1 of 16 without. The one loss had the usual signature: READ_INVALID at one page, while the async 120x68 pass of the first scene frame was running. One event cannot show a rate, so I'll add the larger series as a comment.

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:4826866729 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4385). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-12T08:30:33Z. --- ### What A host import that `refreshImports` or `importAllocation` drops in the middle of a GpuMutex hold can be freed before the GPU runs the commands that the same hold still records with it. The GPU then reads an unmapped page and the device is lost. In Sword of the Sea (PPSA25344, UE5) this happens in about 1 of 8 start-ups, at the first scene frame (`VK_ERROR_DEVICE_LOST`, nvlddmkm 153 on NVIDIA). **Mechanism.** 1. A GpuMutex hold takes a `const HostImport*`: - a build's in-place region (`UploadFinish` flushes pending images between regions, and a flush can reconcile the imports); - an indirect argument or count import between the decision and the record; - a draw's vertex or index import. 2. Later in the same hold, a reconcile drops that import, because its registered run changed (a title growing a heap in place). `retireImport` erased its map node and handed its `RetiredImport` holder to the batch open at that moment. If the recorder was idle, it destroyed the holder at once. 3. The hold then records with the pointer it took. If a `Submit` or `Sync` came in between, the command lands in a later batch. The import is freed when the earlier batch completes, before the later batch runs. With an idle recorder the import is gone before the command is even recorded. In both cases the pointer names an erased map node. **Deterministic evidence: `test(agc)`.** `agc_host_import_retire` (mock device, no GPU) grows an imported run and reconciles inside one hold. It covers two cases: - with the recorder idle; - with the recorder busy, plus a `Submit` between the batch that keeps the import and a later batch of the same hold, where the keeping batch completes first. In both cases the dropped import must stay alive until every batch of the hold has completed. On `main` both checks fail: ``` FAIL: an import dropped with the recorder idle was destroyed at once, while the hold that took it can still record with it FAIL: the dropped import was destroyed at the hold's unlock, while a later batch of the hold that may use it is in flight ``` **Field evidence: Sword of the Sea start-up.** - `VK_EXT_device_fault` reports `READ_INVALID` at one page in every device loss. - With `VK_EXT_device_address_binding_report` logged (6 device losses), that page lay inside a host import retired 2.2-3.0 s before the device-loss report each time. The report lags the fault by about 2 s. - The import was either the title's GPU buffer/label heap at guest `0x1e40000000`, or its shader code region at `0x1540000000`. - The heap grows in about seven steps during start-up (0xd70000 -> 0x1900000 bytes). Each step changes the registered run, so the next reconcile drops the import and makes a larger one. - Within 11-20 ms the freed range was reused by a new 1920x1080 storage texture. - In an experiment build that held every dropped import for 10 s, 0 of 26 start-ups lost the device, against 2 of 12 without the hold in the same interleaved series. **`fix(agc)`.** - `retireImport` moves the dropped import's map node into its holder (`std::map::extract`), so pointers taken earlier stay valid. - The holder is kept by every batch the same hold opens: the new `Recorder::KeepThroughHold`, tied to the hold by `GuestMemory::GpuHoldSerial`, a count of outermost GpuMutex acquisitions. Whatever the hold records with the import completes before the import is destroyed. - Drops for pages the guest is releasing (the lease waiter's two retires) keep the old lifetime. `FlushDeferredReleases` also lets go of what an ended hold kept, so the waiter is not left waiting on pinned ranges. **Related.** #290 also drops imports (least-recent eviction under the budget). If both land, its evictions should go through `retireImport` so they get the same lifetime. That is how we run the two together in our Sword of the Sea test stack. ### Tested Windows 11 (26200), WinLibs GCC 15.2.0, Release, on `main` d06c06565. - **`main` plus only the test commit:** `agc_host_import_retire` fails with the two messages above (Lenovo node, non-GPU suite, on d146ec1b6; the retire path is the same in d06c06565). - **With the fix:** `agc_host_import_retire` passes (Lenovo node). - **Non-GPU suite (`ctest -E "^agc_driver|^video_out|^guest_video"`, Lenovo node):** 587 of 588 pass. The one failure, `log_macros` ("a short line differs"), fails the same way on `main` with only the test commit. - **GPU tests on NVIDIA (RTX 5070 Laptop, driver 616.92, AMD iGPU hidden):** `agc_driver_recorder` (full, and `--host-import-unmap-only`), `agc_driver_host_import_at_map`, `agc_driver_host_import_teardown`, `agc_driver_sampled_cache_budget`, `agc_driver_graphics` and `agc_host_import_retire`: 7 of 7 pass. - **Conventions:** `tools/check_conventions.py --base origin/main` passes. - **Sword of the Sea start-up A/B (not conclusive):** 16 start-ups with the fix and 16 without, interleaved, 40 s each. The device-loss rate comes in clusters, and this series fell mostly in a quiet phase: 0 of 16 with the fix, 1 of 16 without. The one loss had the usual signature: `READ_INVALID` at one page, while the async 120x68 pass of the first scene frame was running. One event cannot show a rate, so I'll add the larger series as a comment. ### 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 (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Scramblrrrr commented 2026-10-12 09:17:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T09:17:37Z.


Windows cross-test of #4385 e785a902b on our 007 stack: https://github.com/Scramblrrrr/AnyPS5/tree/codex/first-light-host-import-hold-cross-test head a5c0c9a363fecd06fa1c187742974974e2f27b04. Your test/fix commits and author are preserved with cherry-pick attribution as 3f9f2e2c8 / a5c0c9a36. Base is our public-perf + cache-preserving sparse WIP cc495f66d, not current main.

Baseline check: built the test-only commit 3f9f2e2c8 against that stack's existing production code/headers (no mixed-layout DLL comparison). agc_host_import_retire fails both reported checks: dropped while recorder idle; dropped holder destroyed before a later batch of the same hold completes. With the fix, that test and agc_driver_graphics pass, 2/2 in 8.12 s. Rebuilt dependent recorder fixtures: host_import_at_map, host_import_teardown and sampled_cache_budget pass on RTX 5070 Ti, 3/3 in 2.59 s. Recorder executable full run and --host-import-unmap-only also exit 0; full run says snapshot patching not tested because Windows direct imports here are compared rather than watched. No full-suite claim.

Port detail: our older base lacks main's device-change parking state. The one textual conflict retains its existing immediate destruction on device change, using the extracted node's mapped entry. Mid-hold node ownership/KeepThroughHold behavior is your fix. This does not claim coverage of the newer main parking path or a device-loss frequency result.

Only graphics is staged, SHA256 4B9C9D2BD018E5B7606CDB2B84AA504964270D7A138DC388C920D42F443D8A8D; no other staged module directly uses Recorder's private layout. Quiet 007 startup cross-test now launched, same warm cache/config. Game result pending; no duplicate upstream PR. Windows 11 26200, GCC 15.2 Release, RTX 5070 Ti. AI-assisted: Codex. No title data uploaded.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6121311922 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4385#issuecomment-6121311922). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-12T09:17:37Z. --- Windows cross-test of #4385 e785a902b on our 007 stack: https://github.com/Scramblrrrr/AnyPS5/tree/codex/first-light-host-import-hold-cross-test head a5c0c9a363fecd06fa1c187742974974e2f27b04. Your test/fix commits and author are preserved with cherry-pick attribution as 3f9f2e2c8 / a5c0c9a36. Base is our public-perf + cache-preserving sparse WIP cc495f66d, not current main. Baseline check: built the test-only commit 3f9f2e2c8 against that stack's existing production code/headers (no mixed-layout DLL comparison). agc_host_import_retire fails both reported checks: dropped while recorder idle; dropped holder destroyed before a later batch of the same hold completes. With the fix, that test and agc_driver_graphics pass, 2/2 in 8.12 s. Rebuilt dependent recorder fixtures: host_import_at_map, host_import_teardown and sampled_cache_budget pass on RTX 5070 Ti, 3/3 in 2.59 s. Recorder executable full run and --host-import-unmap-only also exit 0; full run says snapshot patching not tested because Windows direct imports here are compared rather than watched. No full-suite claim. Port detail: our older base lacks main's device-change parking state. The one textual conflict retains its existing immediate destruction on device change, using the extracted node's mapped entry. Mid-hold node ownership/KeepThroughHold behavior is your fix. This does not claim coverage of the newer main parking path or a device-loss frequency result. Only graphics is staged, SHA256 4B9C9D2BD018E5B7606CDB2B84AA504964270D7A138DC388C920D42F443D8A8D; no other staged module directly uses Recorder's private layout. Quiet 007 startup cross-test now launched, same warm cache/config. Game result pending; no duplicate upstream PR. Windows 11 26200, GCC 15.2 Release, RTX 5070 Ti. AI-assisted: Codex. No title data uploaded.
Scramblrrrr commented 2026-10-12 09:26:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Scramblrrrr on 2026-10-12T09:26:04Z.


Quiet 007 startup on the #4385 port a5c0c9a36 reached the rendered title at frame 979, ~1.49 FPS, ~19.9 GB private memory, one bounded trim. Prior cache guard cc495 title was ~1.56. One run each/different camera positions; no significant speed claim or device-loss-rate conclusion. No device loss in this startup. Continuing to offline menu/campaign to record the current stop. Same staged graphics SHA 4B9C9D2B, other modules/cache unchanged. AI-assisted: Codex.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6121459005 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4385#issuecomment-6121459005). Originally posted by [Scramblrrrr](https://github.com/Scramblrrrr) on 2026-10-12T09:26:04Z. --- Quiet 007 startup on the #4385 port a5c0c9a36 reached the rendered title at frame 979, ~1.49 FPS, ~19.9 GB private memory, one bounded trim. Prior cache guard cc495 title was ~1.56. One run each/different camera positions; no significant speed claim or device-loss-rate conclusion. No device loss in this startup. Continuing to offline menu/campaign to record the current stop. Same staged graphics SHA 4B9C9D2B, other modules/cache unchanged. AI-assisted: Codex.
AlxcBTW commented 2026-10-12 09:35:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T09:35:26Z.


@Scramblrrrr thanks for the independent cross-test. It's good to see the test fail both checks on your stack too, and the fix plus the recorder fixtures pass on a 5070 Ti. Your note on the device-change path is right. On current main a drop at device change still goes to main's parking state; this PR only hands the parked holder the extracted map node instead of the map entry, so the parking behaviour itself is unchanged.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6121606654 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4385#issuecomment-6121606654). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-12T09:35:26Z. --- @Scramblrrrr thanks for the independent cross-test. It's good to see the test fail both checks on your stack too, and the fix plus the recorder fixtures pass on a 5070 Ti. Your note on the device-change path is right. On current `main` a drop at device change still goes to main's parking state; this PR only hands the parked holder the extracted map node instead of the map entry, so the parking behaviour itself is unchanged. AI-assisted: yes (Claude Code)
AlxcBTW commented 2026-10-12 12:54:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlxcBTW on 2026-10-12T12:54:03Z.


Merged current main (ffa198407) into this branch, plus one fix commit (eaf5ca240). #4291 renamed RetiredImport::entry to node, so releaseParkedImports now reads a parked import's range through (*it)->node.mapped(). Behaviour is unchanged: parked imports belong to replaced devices and never go through KeepThroughHold.

Tested on this head: Lenovo (Ryzen 7 7730U, GCC 15.2) non-GPU ctest 600/601, where log_macros is the only failure, as on main. On an RTX 5070 Laptop (driver 616.92) 8/8 GPU tests pass, among them agc_host_import_retire, agc_driver_graphics, and agc_driver_recorder including #4291's --parked-import-unmap-only.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6124665544 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4385#issuecomment-6124665544). Originally posted by [AlxcBTW](https://github.com/AlxcBTW) on 2026-10-12T12:54:03Z. --- Merged current `main` (ffa198407) into this branch, plus one fix commit (eaf5ca240). #4291 renamed `RetiredImport::entry` to `node`, so `releaseParkedImports` now reads a parked import's range through `(*it)->node.mapped()`. Behaviour is unchanged: parked imports belong to replaced devices and never go through `KeepThroughHold`. Tested on this head: Lenovo (Ryzen 7 7730U, GCC 15.2) non-GPU ctest 600/601, where `log_macros` is the only failure, as on `main`. On an RTX 5070 Laptop (driver 616.92) 8/8 GPU tests pass, among them `agc_host_import_retire`, `agc_driver_graphics`, and `agc_driver_recorder` including #4291's `--parked-import-unmap-only`. AI-assisted: yes (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!4385
No description provided.