resource, descriptor: fix slot-pinned dead RT refcount guard, make_again race, twin check, and unmapped guard #4736

Closed
nidea1 wants to merge 2 commits from upstream-pr/4736 into main
nidea1 commented 2026-12-01 04:25:30 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by nidea1 on 2026-12-01T04:25:30Z.


Refs #4462, #2361

What it changes

Fixes five bugs in #2361's dead render-target reclamation (reclaim_dead_render_targets_with, evict_dead_rt_slots, HostHeap::texture_slot_evicted_raw) that caused slot-pinned dead render targets to fail reclamation or race under low bury lag / concurrent invalidation:

  1. RAII EvictedDeadRtSlots refcount guard across Phase 2 (descriptor.rs, descriptor/host.rs, resource.rs): Previously HostHeap::unmake moved each reset slot's COM ref into RETIRED, so references(&host.texture) stayed >= 2 and == 1 always failed (freeing 0 bytes and looping texture_made every 5 s); conversely relaxing to <= 1 + reset falsely reclaimed live RTs with extra COM holders under VICTORY_BURY_LAG=0 or concurrent host::bury(). Holding the evicted slot refs in EvictedDeadRtSlots across Phase 2 allows an exact references(&host.texture) == 1 + evicted.held_refs_for(raw) check under any bury lag, then retires all held refs via host::retire_resource on drop after resource locks are released.
  2. make_again key-zeroed busy guard (descriptor/host.rs): HostHeap::texture_slot_evicted_raw now returns DeadRtSlotEvict::Busy when made == 0 while kept still holds expected_raw (matching texture_slot_evicted), so partially invalidated multi-slot RTs abort and restore already-evicted slots instead of skipping.
  3. Complete depth-stencil Twin holder check and byte accounting (resource.rs): twin_idle() now checks Arc::strong_count(twin) == 1, twin.colour, twin.plane, and twin.through references (and twin_bytes() includes twin.plane.bytes), preventing reclamation while HostList::twin_use or a UAV plane slot still holds the twin.
  4. Whole-backing unmap verification without lock contention (resource.rs): Enforces Backing::Address and backing_wholly_unmapped() after dropping r.host.made before queueing slot eviction, and verifies take_unmapped_host in Phase 2 without clearing r.host.used on concurrent remaps (restoring evicted slots via texture_made).
  5. Independent unmapped_idle_ms Phase 1 gating (resource.rs): Evaluates both idle_superseded and idle_unmapped before skipping in Phase 1 and queries record_frame_ring separately with unmapped_idle_ms (30 s) for unmapped slot-pinned candidates.

How it was tested

  • GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): Headless D3D12 unit/integration tests on RTX 3060 Ti 8 GB (Windows 11, 32 GB RAM); live gameplay run on RTX 3060 Ti not yet measured (follow-up live test report pending).
  • Before: Under VICTORY_BURY_LAG=0 or concurrent host::bury(), slot-pinned dead RTs either looped texture_made every 5 s without freeing memory (references == 1 failed against RETIRED) or falsely reclaimed live RTs with extra COM holders under a <= 1 + reset check; make_again-zeroed slots were skipped instead of aborting, and depth-stencil Twin plane/Arc holders were unchecked.
  • After:
    • cargo test -j 2 --locked -p victory-gpu --lib -- resource::tests:: — 71 passed, 0 failed, 1 ignored.
    • VICTORY_BURY_LAG=0 cargo test -j 2 --locked -p victory-gpu --lib -- resource::tests::slot_pinned resource::tests::evict_dead_rt_slots resource::tests::dead_depth_rt resource::tests::reclaim_dead_render_targets resource::tests::dead_rt_reclaimable — 9 passed, 0 failed (including new multi-slot make_again and depth-stencil Twin holder regression tests).
    • cargo test -j 2 --locked -p victory-gpu --lib -- descriptor:: — 120 passed, 0 failed, 2 ignored.
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: 0 warnings/errors in touched files (origin/main 5fabb46e currently fails workspace Clippy on 5 pre-existing #4634 errors in host/{font,menu,panel}.rs, tracked in #4707).
    • cargo xtask policy: 464 files ok.
  • For shader changes, the wave sizes tested (32 and/or 64 lanes): n/a (not a shader change).
  • cargo test passed for the crates it touches: yes (targeted resource::tests:: and descriptor:: suites in victory-gpu; full --workspace and cfg(not(windows)) not run).

Checked for overlap

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5578992382 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4736). Originally posted by [nidea1](https://github.com/nidea1) on 2026-12-01T04:25:30Z. --- Refs #4462, #2361 ## What it changes Fixes five bugs in `#2361`'s dead render-target reclamation (`reclaim_dead_render_targets_with`, `evict_dead_rt_slots`, `HostHeap::texture_slot_evicted_raw`) that caused slot-pinned dead render targets to fail reclamation or race under low bury lag / concurrent invalidation: 1. **RAII `EvictedDeadRtSlots` refcount guard across Phase 2 (`descriptor.rs`, `descriptor/host.rs`, `resource.rs`):** Previously `HostHeap::unmake` moved each reset slot's COM ref into `RETIRED`, so `references(&host.texture)` stayed `>= 2` and `== 1` always failed (freeing 0 bytes and looping `texture_made` every 5 s); conversely relaxing to `<= 1 + reset` falsely reclaimed live RTs with extra COM holders under `VICTORY_BURY_LAG=0` or concurrent `host::bury()`. Holding the evicted slot refs in `EvictedDeadRtSlots` across Phase 2 allows an exact `references(&host.texture) == 1 + evicted.held_refs_for(raw)` check under any bury lag, then retires all held refs via `host::retire_resource` on drop after resource locks are released. 2. **`make_again` key-zeroed busy guard (`descriptor/host.rs`):** `HostHeap::texture_slot_evicted_raw` now returns `DeadRtSlotEvict::Busy` when `made == 0` while `kept` still holds `expected_raw` (matching `texture_slot_evicted`), so partially invalidated multi-slot RTs abort and restore already-evicted slots instead of skipping. 3. **Complete depth-stencil `Twin` holder check and byte accounting (`resource.rs`):** `twin_idle()` now checks `Arc::strong_count(twin) == 1`, `twin.colour`, `twin.plane`, and `twin.through` references (and `twin_bytes()` includes `twin.plane.bytes`), preventing reclamation while `HostList::twin_use` or a UAV plane slot still holds the twin. 4. **Whole-backing unmap verification without lock contention (`resource.rs`):** Enforces `Backing::Address` and `backing_wholly_unmapped()` after dropping `r.host.made` before queueing slot eviction, and verifies `take_unmapped_host` in Phase 2 without clearing `r.host.used` on concurrent remaps (restoring evicted slots via `texture_made`). 5. **Independent `unmapped_idle_ms` Phase 1 gating (`resource.rs`):** Evaluates both `idle_superseded` and `idle_unmapped` before skipping in Phase 1 and queries `record_frame_ring` separately with `unmapped_idle_ms` (30 s) for unmapped slot-pinned candidates. ## How it was tested - GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): Headless D3D12 unit/integration tests on RTX 3060 Ti 8 GB (Windows 11, 32 GB RAM); live gameplay run on RTX 3060 Ti not yet measured (follow-up live test report pending). - Before: Under `VICTORY_BURY_LAG=0` or concurrent `host::bury()`, slot-pinned dead RTs either looped `texture_made` every 5 s without freeing memory (`references == 1` failed against `RETIRED`) or falsely reclaimed live RTs with extra COM holders under a `<= 1 + reset` check; `make_again`-zeroed slots were skipped instead of aborting, and depth-stencil `Twin` plane/Arc holders were unchecked. - After: - `cargo test -j 2 --locked -p victory-gpu --lib -- resource::tests::` — 71 passed, 0 failed, 1 ignored. - `VICTORY_BURY_LAG=0 cargo test -j 2 --locked -p victory-gpu --lib -- resource::tests::slot_pinned resource::tests::evict_dead_rt_slots resource::tests::dead_depth_rt resource::tests::reclaim_dead_render_targets resource::tests::dead_rt_reclaimable` — 9 passed, 0 failed (including new multi-slot `make_again` and depth-stencil `Twin` holder regression tests). - `cargo test -j 2 --locked -p victory-gpu --lib -- descriptor::` — 120 passed, 0 failed, 2 ignored. - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: 0 warnings/errors in touched files (`origin/main` `5fabb46e` currently fails workspace Clippy on 5 pre-existing `#4634` errors in `host/{font,menu,panel}.rs`, tracked in `#4707`). - `cargo xtask policy`: 464 files ok. - For shader changes, the wave sizes tested (32 and/or 64 lanes): n/a (not a shader change). - `cargo test` passed for the crates it touches: yes (targeted `resource::tests::` and `descriptor::` suites in `victory-gpu`; full `--workspace` and `cfg(not(windows))` not run). ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: #4462, #2361, #4432, #4463, #4041, #4707
nidea1 commented 2026-12-01 05:33:08 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by nidea1 on 2026-12-01T05:33:08Z.


Live runtime check on RTX 3060 Ti 8 GB / 32 GB RAM (Windows 11, Series S, DLSS Quality, FSR 3 FG, combined with our other open PRs on 5fabb46eaf, two runs reaching t=162s and t=268s before 0x887A0006 on a host with intermittent nvlddmkm Event 153):

  • 0 HERO LEAD, 0 UPLOAD OVER HOST WRITE, clean visuals at +31s and +150s (24.5–26.0 FPS, 35.9–38.5 ms GPU).
  • Render-target footprint plateaued at 678–696 MB during this short opening scene, so "reclaimed dead idle render targets" did not fire (0 dead-RT reclaim events triggered yet in the 2–4 min window).
  • Full slot-pinned dead-RT reclamation, VICTORY_BURY_LAG=0 exact refcount guard, make_again busy abort, and depth-stencil Twin holder protection remain verified by the headless D3D12 test suite (71/71 resource::tests::, 9/9 under VICTORY_BURY_LAG=0, 120/120 descriptor::); longer multi-scene live RT-churn testing (such as #4462's 1.0 -> 1.78 GB sequence) is still pending.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7301889575 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4736#issuecomment-7301889575). Originally posted by [nidea1](https://github.com/nidea1) on 2026-12-01T05:33:08Z. --- Live runtime check on RTX 3060 Ti 8 GB / 32 GB RAM (Windows 11, Series S, DLSS Quality, FSR 3 FG, combined with our other open PRs on `5fabb46eaf`, two runs reaching `t=162s` and `t=268s` before `0x887A0006` on a host with intermittent `nvlddmkm` Event 153): - `0` `HERO LEAD`, `0` `UPLOAD OVER HOST WRITE`, clean visuals at `+31s` and `+150s` (`24.5–26.0 FPS`, `35.9–38.5 ms` GPU). - Render-target footprint plateaued at `678–696 MB` during this short opening scene, so `"reclaimed dead idle render targets"` did not fire (`0` dead-RT reclaim events triggered yet in the 2–4 min window). - Full slot-pinned dead-RT reclamation, `VICTORY_BURY_LAG=0` exact refcount guard, `make_again` busy abort, and depth-stencil `Twin` holder protection remain verified by the headless D3D12 test suite (`71/71` `resource::tests::`, `9/9` under `VICTORY_BURY_LAG=0`, `120/120` `descriptor::`); longer multi-scene live RT-churn testing (such as #4462's `1.0 -> 1.78 GB` sequence) is still pending.
nidea1 commented 2026-12-01 10:56:38 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by nidea1 on 2026-12-01T10:56:38Z.


Pushed 57e3a622 to fix a parallel test flake in the new unmapped dead-RT unit tests without changing production behavior:

  • Cause: During parallel cargo test --locked -p victory-gpu --lib, unrelated tests in land, shadow, and watch hold watch::title_memory_remapping() (MAPPING.write()) for 50 ms+, causing non-blocking title_memory_reading_of_now() (try_read()) inside reclaim_dead_render_targets_inner() to return None (WouldBlock) and skip Phase 1 or Phase 2 cleanly.
  • Fix: Added a reading_of seam to reclaim_dead_render_targets_inner(), backing_wholly_unmapped_with(), take_unmapped_host_with(), and take_if_unmapped_with(). Production (reclaim_dead_render_targets_with / take_unmapped_host) continues using non-blocking title_memory_reading_of_now(); the dead-RT unit tests pass blocking title_memory_reading_of() (never recursively held) to wait past concurrent remap-lock tests, and explicitly verify both Phase 1 and Phase 2 contended None results (Phase 1 skips without evicting; Phase 2 aborts without detaching or clearing used and restores the evicted slot via make_waiting()).
  • Verification on 57e3a622:
    • cargo test --locked -p victory-gpu --lib: resource::tests:: and descriptor:: passed 13/13 full parallel runs (default + VICTORY_RANGE_LOCK=1), 0 dead-RT failures; --test-threads=1 passed 1101 passed, 0 failed, 66 ignored (note: a few parallel runs hit unrelated pre-existing origin/main flakes in watch.rs, host/uav_pool.rs, and host::refresh_tests).
    • VICTORY_BURY_LAG=0 dead-RT suite (--test-threads=1): 9 passed, 0 failed.
    • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings and --all-features: 0 warnings/errors in resource.rs, descriptor.rs, descriptor/host.rs (only the 5 pre-existing main #4634 errors in host/{font,menu,panel}.rs).
    • cargo xtask policy: 464 files ok (--staged: 1 files ok).
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7308947333 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4736#issuecomment-7308947333). Originally posted by [nidea1](https://github.com/nidea1) on 2026-12-01T10:56:38Z. --- Pushed `57e3a622` to fix a parallel test flake in the new unmapped dead-RT unit tests without changing production behavior: - **Cause:** During parallel `cargo test --locked -p victory-gpu --lib`, unrelated tests in `land`, `shadow`, and `watch` hold `watch::title_memory_remapping()` (`MAPPING.write()`) for 50 ms+, causing non-blocking `title_memory_reading_of_now()` (`try_read()`) inside `reclaim_dead_render_targets_inner()` to return `None` (`WouldBlock`) and skip Phase 1 or Phase 2 cleanly. - **Fix:** Added a `reading_of` seam to `reclaim_dead_render_targets_inner()`, `backing_wholly_unmapped_with()`, `take_unmapped_host_with()`, and `take_if_unmapped_with()`. Production (`reclaim_dead_render_targets_with` / `take_unmapped_host`) continues using non-blocking `title_memory_reading_of_now()`; the dead-RT unit tests pass blocking `title_memory_reading_of()` (never recursively held) to wait past concurrent remap-lock tests, and explicitly verify both Phase 1 and Phase 2 contended `None` results (Phase 1 skips without evicting; Phase 2 aborts without detaching or clearing `used` and restores the evicted slot via `make_waiting()`). - **Verification on `57e3a622`:** - `cargo test --locked -p victory-gpu --lib`: `resource::tests::` and `descriptor::` passed **13/13** full parallel runs (default + `VICTORY_RANGE_LOCK=1`), `0` dead-RT failures; `--test-threads=1` passed `1101 passed, 0 failed, 66 ignored` (note: a few parallel runs hit unrelated pre-existing `origin/main` flakes in `watch.rs`, `host/uav_pool.rs`, and `host::refresh_tests`). - `VICTORY_BURY_LAG=0` dead-RT suite (`--test-threads=1`): `9 passed, 0 failed`. - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` and `--all-features`: `0` warnings/errors in `resource.rs`, `descriptor.rs`, `descriptor/host.rs` (only the 5 pre-existing `main` `#4634` errors in `host/{font,menu,panel}.rs`). - `cargo xtask policy`: `464 files ok` (`--staged`: `1 files ok`).
Dukestar69 commented 2026-12-01 12:05:53 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Dukestar69 on 2026-12-01T12:05:53Z.

Review: APPROVED


Reviewed descriptor.rs, descriptor/host.rs, and
esource.rs: holding evicted dead-RT slot COM references in EvictedDeadRtSlots across the TEXTURES /
.host.made check makes 1 + held_refs_for(raw) exact regardless of VICTORY_BURY_LAG or concurrent host::bury(), and retiring them in Drop after unlocking keeps lock ordering clean. Returning DeadRtSlotEvict::Busy when make_again cleared the key while the slot still holds expected_raw, tightening win_idle (including hrough and plane refcounts), and re-checking unmapped backing under the remap guard after slot eviction all close real UAF/refcount races. Verified on Windows (RTX 5090): cargo test -p victory-gpu --lib resource::tests -- --test-threads=1 (71 passed, 0 failed) and descriptor::host::tests (85 passed, 0 failed).

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:review:6130934811 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4736#pullrequestreview-6130934811). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-12-01T12:05:53Z. **Review:** APPROVED --- Reviewed descriptor.rs, descriptor/host.rs, and esource.rs: holding evicted dead-RT slot COM references in EvictedDeadRtSlots across the TEXTURES / .host.made check makes 1 + held_refs_for(raw) exact regardless of VICTORY_BURY_LAG or concurrent host::bury(), and retiring them in Drop after unlocking keeps lock ordering clean. Returning DeadRtSlotEvict::Busy when make_again cleared the key while the slot still holds expected_raw, tightening win_idle (including hrough and plane refcounts), and re-checking unmapped backing under the remap guard after slot eviction all close real UAF/refcount races. Verified on Windows (RTX 5090): cargo test -p victory-gpu --lib resource::tests -- --test-threads=1 (71 passed, 0 failed) and descriptor::host::tests (85 passed, 0 failed). Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
nidea1 commented 2026-12-01 14:39:23 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by nidea1 on 2026-12-01T14:39:23Z.


Thanks @Dukestar69 for the review and RTX 5090 test verification (resource::tests and descriptor::host::tests) on 57e3a622!

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7314612480 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4736#issuecomment-7314612480). Originally posted by [nidea1](https://github.com/nidea1) on 2026-12-01T14:39:23Z. --- Thanks @Dukestar69 for the review and RTX 5090 test verification (`resource::tests` and `descriptor::host::tests`) on `57e3a622`!
trustno1-coder commented 2026-12-01 18:34:11 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by trustno1-coder on 2026-12-01T18:34:11Z.


Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7321153271 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4736#issuecomment-7321153271). Originally posted by [trustno1-coder](https://github.com/trustno1-coder) on 2026-12-01T18:34:11Z. --- Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

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
trustno1-coder/VIctory-NVIDIA!4736
No description provided.