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

Closed
github-bot wants to merge 2 commits from github-bot/gh-4736 into main AGit
Owner

@⁠nidea1 on GitHub · 2026-12-01 04:25 UTC · original
nidea1:fix/dead-rt-slot-unmapped-guard → main · +834 −127 · 3 files · 2 commits · closed without merging

Refs #4463, #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

**@⁠nidea1** on GitHub · 2026-12-01 04:25 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4736) `nidea1:fix/dead-rt-slot-unmapped-guard` → `main` · +834 −127 · 3 files · 2 commits · closed without merging Refs #4463, #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: #4463, #2361, #4433, #4464, #4041, #4708
Fixes five bugs in #2361's dead render-target reclamation (reclaim_dead_render_targets_with, evict_dead_rt_slots, texture_slot_evicted_raw):

- Hold evicted slot COM refs in an RAII EvictedDeadRtSlots guard across Phase 2 verification instead of retiring them inside HostHeap::unmake: previously unmake moved each reset slot's ref into RETIRED so references(&host.texture) stayed >= 2 and == 1 always failed, freeing 0 bytes while looping texture_made every 5 s; conversely allowing <= 1 + reset falsely reclaimed live RTs with extra COM holders under VICTORY_BURY_LAG=0 or concurrent host::bury(). Keeping the evicted refs locally in EvictedDeadRtSlots lets Phase 2 require exact references == 1 + held_refs_for(raw) under any bury lag, then retires all refs via host::retire_resource on drop after resource locks unlock.
- Treat make_again-invalidated slots (made == 0 while kept still holds expected_raw) as Busy in HostHeap::texture_slot_evicted_raw (matching texture_slot_evicted) so partially invalidated multi-slot RTs abort and restore already-evicted slots instead of skipping.
- Check Arc::strong_count(twin) == 1, twin.plane, and twin.through references in twin_idle() (and include twin.plane.bytes in reclaimed byte accounting) so depth-stencil RTs held via HostList::twin_use or UAV plane slots are never reclaimed while in use.
- Enforce Backing::Address and whole-backing unmap verification (backing_wholly_unmapped before queueing slot eviction, take_unmapped_host in Phase 2) without clearing r.host.used on concurrent remaps, restoring evicted slots via texture_made.
- Query record_frame_ring separately with unmapped_idle_ms (30 s) for unmapped slot-pinned candidates and evaluate both idle_superseded and idle_unmapped before skipping in Phase 1.

Refs #4462, #2361
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 04:30:14 +00:00
Author
Owner

@⁠nidea1 on GitHub · 2026-12-01 05:33 UTC · original

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 #4463's 1.0 -> 1.78 GB sequence) is still pending.
**@⁠nidea1** on GitHub · 2026-12-01 05:33 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4736#issuecomment-7301889575) 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 #4463's `1.0 -> 1.78 GB` sequence) is still pending.
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 	itle_memory_reading_of_now() (	ry_read()) inside 
eclaim_dead_render_targets_inner() to return None (WouldBlock) and spuriously fail the unmapped slot-pinned RT tests.

Pass a 
eading_of seam into 
eclaim_dead_render_targets_inner(), acking_wholly_unmapped_with(), 	ake_unmapped_host_with(), and 	ake_if_unmapped_with():
- Production (
eclaim_dead_render_targets_with and 	ake_unmapped_host) continues passing non-blocking watch::title_memory_reading_of_now().
- Dead-RT unit tests pass blocking watch::title_memory_reading_of() (without holding any recursive read lock) so they wait cleanly past concurrent remap-lock tests, plus explicitly verify that a contended None result skips without disturbing the slot or host copy.
Author
Owner

@⁠nidea1 on GitHub · 2026-12-01 10:56 UTC · original · edited 2026-12-01 11:48 UTC

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).
**@⁠nidea1** on GitHub · 2026-12-01 10:56 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4736#issuecomment-7308947333) · edited 2026-12-01 11:48 UTC 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`).
github-bot force-pushed github-bot/gh-4736 from 281054850d to 57e3a6228c 2026-12-01 11:48:11 +00:00 Compare
Author
Owner

@⁠Dukestar69 on GitHub · 2026-12-01 12:05 UTC · original · approved these changes

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).

**@⁠Dukestar69** on GitHub · 2026-12-01 12:05 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4736#pullrequestreview-6130934811) · **approved these changes** 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).
Author
Owner

@⁠nidea1 on GitHub · 2026-12-01 14:39 UTC · original

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

**@⁠nidea1** on GitHub · 2026-12-01 14:39 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4736#issuecomment-7314612480) Thanks @⁠Dukestar69 for the review and RTX 5090 test verification (`resource::tests` and `descriptor::host::tests`) on `57e3a622`!
github-bot closed this pull request 2026-12-01 18:36:51 +00:00
Author
Owner

@⁠trustno1-coder on GitHub · 2026-12-01 18:34 UTC · original

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.

**@⁠trustno1-coder** on GitHub · 2026-12-01 18:34 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4736#issuecomment-7321153271) 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

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/trustno1-coder--VIctory-NVIDIA!4737
No description provided.