[pr-fix][RAYTRACING] hwrt: fix BLAS TDR bursts, ArenaSlot UAF on open thin lists, and in-flight HostAs reuse #268

Open
hingeowl wants to merge 1 commit from hingeowl/Darner:fix/hwrt-blas-tdr-and-uaf into main
Contributor

Summary

Fixes DXGI_ERROR_DEVICE_HUNG (0x887A0006) inside BuildRaytracingAccelerationStructure (D3D12_AUTO_BREADCRUMB_OP_BUILDRAYTRACINGACCELERATIONSTRUCTURE, op 31) and nvlddmkm Event ID 13 Graphics SM Warp Exception: MMU Fault during extended Hardware RT (DARNER_HW_RT=1) play sessions with large BLAS rehydrate/compaction streaming bursts.

Crash evidence (RTX 4090, 4K + DLSS Quality + 2x FG + HW RT)

  1. 30 min playtest (0x887A0006): nvlddmkm Event ID 13 Graphics SM Warp Exception: MMU Fault (Multiple Warp Errors) immediately following ~300+ BLAS rehydrates and ~100+ BLAS/TLAS builds recorded concurrently across worker threads with 26 submissions in flight.
  2. 19.5 min playtest (DARNER_D3D12_DRED=light, 0x887A0006): DRED auto-breadcrumb stopped at command 503 of 1075 (op 31) (BuildRaytracingAccelerationStructure) after waiting 2,288.4 ms (Windows 2 s TDR, nvlddmkm Event ID 153), immediately following 60+ mode 1 compaction copies + 201 rehydrates + 21 builds packed into one command list.

What it changes (crates/darner-gpu/src/hwrt.rs)

  1. Avoid unbounded forced BLAS builds in copy_waiting():
    • Previously, every HwRtOp::Copy(dest, src) whose src was deferred in Backlog called q.budget.force(...) + execute_blas_batch synchronously, bypassing BUILDS_PER_FRAME and emitting a separate BLAS wave per copy into the same command list.
    • Because execute_copy aliases as_map[dest] = as_map[src] (no GPU compaction copy), copy_waiting() now uses Backlog::copy_entry to re-key (mode == 1 compaction) or clone (mode != 1) the waiting recipe to dest (b.dest_va = dest), letting it build under the normal per-frame budget while TLAS masks dest until ready.
  2. Tighten per-frame BLAS burst budget (64 builds / 16 MiB inputs, chunks 32 / 8 MiB):
    • Lowered from 256 / 64 MiB (chunks 64 / 16 MiB) so 200–400 BLAS area-streaming/rehydrate bursts spread cleanly across several frames instead of packing 500–1,075 RT ops into a single submission and hitting Windows' 2 s TDR.
  3. Fix MeshletCache ArenaSlot use-after-free under DARNER_THIN=1 (cached_meshlet_ib):
    • cached_meshlet_ib previously called list.keep(&entry.buffer) (keeping the 32 MiB ArenaBlock buffer) without holding Arc<DecodedIb> (which owns slot: Option<ArenaSlot>). If MeshletCache evicted entry while a thin worker-thread list was still open (not yet submitted), DecodedIb::drop called retire(slot) against an earlier fence, allowing ArenaSlot::drop to free and arena_write to overwrite those 4 KiB units before GPU execution.
    • Now calls list.hold(Box::new(entry.clone())) on both cache-hit and cache-miss paths.
  4. Fix in-place BLAS HostAs overwrite race (emit_blas_wave):
    • emit_blas_wave previously kept only the COM ID3D12Resource (list.keep(&r.dest_as.buffer)), letting Arc::strong_count drop back to 1 while the list was still open or in flight so alloc_or_reuse_as / flush_blas_waves (Arc::strong_count == 1) could overwrite an in-flight BLAS buffer in place.
    • Now calls list.hold(Box::new(r.dest_as.clone())) (and r.src_as when present), leaving AsKind::Tlas untouched per the existing descriptor-view invariant.

Verification

  • Unit tests: cargo test -p darner-gpu --locked -- hwrt:: — 26 passed; 0 failed (including new backlog_copy_entry_moves_compact_and_clones_other_without_forcing_build).
  • bench-kit cam-city60 (HW RT, RTX 4090, DARNER_D3D12_DRED=light): 100% clean (646 frames, 0.00 m error, zero nvlddmkm events), 10.27 fps median (97.38 ms, p95 = 122.02 ms, p99 = 164.53 ms, >100 ms = 39.8% vs 10.01 fps / 128.36 ms p95 / 49.2% >100 ms pre-fix even without DRED).
  • bench-kit full 172 s cam-city stress test (HW RT, RTX 4090, DARNER_D3D12_DRED=light): 100% clean (1,629 frames, 0.00 m error, zero nvlddmkm events), surviving 26,796 BLAS builds, 55,295 refits, 10,586 rehydrates, 7,002 copies, 4,525 evictions, and 71,435 meshlet IBs.
  • Extended gameplay: 1+ hour continuous 4K + DLSS Quality + 2x FG + Hardware RT playtest with 0 crashes.
## Summary Fixes `DXGI_ERROR_DEVICE_HUNG` (`0x887A0006`) inside `BuildRaytracingAccelerationStructure` (`D3D12_AUTO_BREADCRUMB_OP_BUILDRAYTRACINGACCELERATIONSTRUCTURE`, op 31) and `nvlddmkm` Event ID 13 `Graphics SM Warp Exception: MMU Fault` during extended Hardware RT (`DARNER_HW_RT=1`) play sessions with large BLAS rehydrate/compaction streaming bursts. ### Crash evidence (RTX 4090, 4K + DLSS Quality + 2x FG + HW RT) 1. **30 min playtest (`0x887A0006`):** `nvlddmkm` Event ID 13 `Graphics SM Warp Exception: MMU Fault` (`Multiple Warp Errors`) immediately following ~300+ BLAS rehydrates and ~100+ BLAS/TLAS builds recorded concurrently across worker threads with 26 submissions in flight. 2. **19.5 min playtest (`DARNER_D3D12_DRED=light`, `0x887A0006`):** DRED auto-breadcrumb stopped at **`command 503 of 1075 (op 31)`** (`BuildRaytracingAccelerationStructure`) after waiting `2,288.4 ms` (Windows 2 s TDR, `nvlddmkm` Event ID 153), immediately following 60+ `mode 1` compaction copies + 201 rehydrates + 21 builds packed into one command list. ## What it changes (`crates/darner-gpu/src/hwrt.rs`) 1. **Avoid unbounded forced BLAS builds in `copy_waiting()`:** - Previously, every `HwRtOp::Copy(dest, src)` whose `src` was deferred in `Backlog` called `q.budget.force(...)` + `execute_blas_batch` synchronously, bypassing `BUILDS_PER_FRAME` and emitting a separate BLAS wave per copy into the same command list. - Because `execute_copy` aliases `as_map[dest] = as_map[src]` (no GPU compaction copy), `copy_waiting()` now uses `Backlog::copy_entry` to re-key (`mode == 1` compaction) or clone (`mode != 1`) the waiting recipe to `dest` (`b.dest_va = dest`), letting it build under the normal per-frame budget while TLAS masks `dest` until ready. 2. **Tighten per-frame BLAS burst budget (`64` builds / `16 MiB` inputs, chunks `32` / `8 MiB`):** - Lowered from `256` / `64 MiB` (chunks `64` / `16 MiB`) so 200–400 BLAS area-streaming/rehydrate bursts spread cleanly across several frames instead of packing 500–1,075 RT ops into a single submission and hitting Windows' 2 s TDR. 3. **Fix `MeshletCache` `ArenaSlot` use-after-free under `DARNER_THIN=1` (`cached_meshlet_ib`):** - `cached_meshlet_ib` previously called `list.keep(&entry.buffer)` (keeping the 32 MiB `ArenaBlock` buffer) without holding `Arc<DecodedIb>` (which owns `slot: Option<ArenaSlot>`). If `MeshletCache` evicted `entry` while a thin worker-thread list was still open (not yet submitted), `DecodedIb::drop` called `retire(slot)` against an earlier fence, allowing `ArenaSlot::drop` to free and `arena_write` to overwrite those 4 KiB units before GPU execution. - Now calls `list.hold(Box::new(entry.clone()))` on both cache-hit and cache-miss paths. 4. **Fix in-place BLAS `HostAs` overwrite race (`emit_blas_wave`):** - `emit_blas_wave` previously kept only the COM `ID3D12Resource` (`list.keep(&r.dest_as.buffer)`), letting `Arc::strong_count` drop back to `1` while the list was still open or in flight so `alloc_or_reuse_as` / `flush_blas_waves` (`Arc::strong_count == 1`) could overwrite an in-flight BLAS buffer in place. - Now calls `list.hold(Box::new(r.dest_as.clone()))` (and `r.src_as` when present), leaving `AsKind::Tlas` untouched per the existing descriptor-view invariant. ## Verification - **Unit tests:** `cargo test -p darner-gpu --locked -- hwrt::` — **26 passed; 0 failed** (including new `backlog_copy_entry_moves_compact_and_clones_other_without_forcing_build`). - **`bench-kit` `cam-city60` (HW RT, RTX 4090, `DARNER_D3D12_DRED=light`):** **100% clean** (`646` frames, `0.00 m` error, zero `nvlddmkm` events), `10.27 fps` median (`97.38 ms`, `p95 = 122.02 ms`, `p99 = 164.53 ms`, `>100 ms = 39.8%` vs `10.01 fps` / `128.36 ms` p95 / `49.2% >100 ms` pre-fix even without DRED). - **`bench-kit` full 172 s `cam-city` stress test (HW RT, RTX 4090, `DARNER_D3D12_DRED=light`):** **100% clean** (`1,629` frames, `0.00 m` error, **zero `nvlddmkm` events**), surviving `26,796` BLAS builds, `55,295` refits, `10,586` rehydrates, `7,002` copies, `4,525` evictions, and `71,435` meshlet IBs. - **Extended gameplay:** 1+ hour continuous 4K + DLSS Quality + 2x FG + Hardware RT playtest with **0 crashes**.
Fixes DXGI_ERROR_DEVICE_HUNG (0x887A0006) at BuildRaytracingAccelerationStructure (DRED op 31, command 503/1075 after 2.29 s) and SM Warp MMU Fault during large HW RT streaming/rehydrate bursts:

1. Avoid unbounded forced BLAS builds in copy_waiting(): re-key (mode 1 compact) or clone waiting Backlog recipes to dest so deferred copies build under the normal per-frame budget instead of forcing 60+ synchronous BLAS batches into a single command list.
2. Tighten per-frame BLAS build budget from 256/64 MiB (chunks 64/16 MiB) to 64/16 MiB (chunks 32/8 MiB) so 200-400 BLAS rehydrate bursts spread across several frames instead of exceeding Windows' 2 s TDR.
3. Hold Arc<DecodedIb> on HostList in cached_meshlet_ib() (hit and miss) so MeshletCache evictions while a DARNER_THIN=1 list is still open cannot retire and overwrite ArenaSlot units before GPU execution.
4. Hold Arc<HostAs> on HostList in emit_blas_wave() (dest and src) so alloc_or_reuse_as() / flush_blas_waves() (Arc::strong_count == 1) never overwrite an in-flight BLAS buffer in place.
First-time contributor

PASS

Review of 75abcd65b.

Audited copy_waiting source/destination bookkeeping, moved refit addresses, held-list lifetimes, and overlap with #279. No blocking correctness issue found.

Windows validation: G1 PASS; G2 PASS; cargo xtask policy PASS (555 files ok); cargo test --locked -p darner-gpu --lib hwrt — 26 passed, 0 failed.

Not run in-game.

AI-assisted: yes (GitHub Copilot, Auto mode).

**PASS** Review of `75abcd65b`. Audited `copy_waiting` source/destination bookkeeping, moved refit addresses, held-list lifetimes, and overlap with `#279`. No blocking correctness issue found. Windows validation: G1 PASS; G2 PASS; `cargo xtask policy` PASS (`555 files ok`); `cargo test --locked -p darner-gpu --lib hwrt` — 26 passed, 0 failed. Not run in-game. AI-assisted: yes (GitHub Copilot, Auto mode).
Contributor

Code review only (read the diff at 75abcd65), nothing built or run. I agree with lewis499 that the copy_waiting bookkeeping and the meshlet hold are right. One point needs a measurement:

  • The cost of the HostAs holds is worth measuring. Blocking reuse while a list is in flight is the point of the holds. The side effect: list.hold keeps the Arc until the list is reset, and flush_blas_waves refits in place only when Arc::strong_count(src_entry) == 1 (alloc_or_reuse_as likewise). So a BLAS refit every frame (animated geometry) likely gets a new buffer per refit (RefitPlan::AllocDest, force_new) until its earlier list resets. That means more BLAS memory alive at once and more pressure on the BLAS budget and LRU. A comparison of blas_heap_line() (heaps MB, in use) and the BLAS creation count on the same route, with and without this change, would show how much. If it is large, the hold could be limited to where lists are recorded out of order (DARNER_THIN).
  • Budget 256 → 64 builds a frame: copy_waiting now hides dest until its build runs, so after a large rehydrate burst some objects may be missing from reflections and shadows for more frames. Worth a side-by-side look on a fast drive.
  • Minor: a clone (mode != 1) of a waiting source now builds twice (source and copy), where before one build was aliased.
  • Interacts with #318 (same file, merges cleanly): the extra allocations from the first point go through #318's allocator. Worth testing the two together.

Reviewed with Claude Code (AI-assisted); findings checked by a human before posting.

Code review only (read the diff at `75abcd65`), nothing built or run. I agree with lewis499 that the `copy_waiting` bookkeeping and the meshlet `hold` are right. One point needs a measurement: - **The cost of the `HostAs` holds is worth measuring.** Blocking reuse while a list is in flight is the point of the holds. The side effect: `list.hold` keeps the `Arc` until the list is reset, and `flush_blas_waves` refits in place only when `Arc::strong_count(src_entry) == 1` (`alloc_or_reuse_as` likewise). So a BLAS refit every frame (animated geometry) likely gets a new buffer per refit (`RefitPlan::AllocDest`, `force_new`) until its earlier list resets. That means more BLAS memory alive at once and more pressure on the BLAS budget and LRU. A comparison of `blas_heap_line()` (heaps MB, in use) and the BLAS creation count on the same route, with and without this change, would show how much. If it is large, the hold could be limited to where lists are recorded out of order (`DARNER_THIN`). - **Budget 256 → 64 builds a frame:** `copy_waiting` now hides `dest` until its build runs, so after a large rehydrate burst some objects may be missing from reflections and shadows for more frames. Worth a side-by-side look on a fast drive. - Minor: a clone (`mode != 1`) of a waiting source now builds twice (source and copy), where before one build was aliased. - Interacts with #318 (same file, merges cleanly): the extra allocations from the first point go through #318's allocator. Worth testing the two together. Reviewed with Claude Code (AI-assisted); findings checked by a human before posting.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'fix/hwrt-blas-tdr-and-uaf:hingeowl-fix/hwrt-blas-tdr-and-uaf'
git switch 'hingeowl-fix/hwrt-blas-tdr-and-uaf'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'hingeowl-fix/hwrt-blas-tdr-and-uaf'
git switch 'hingeowl-fix/hwrt-blas-tdr-and-uaf'
git rebase 'main'
git switch 'main'
git merge --ff-only 'hingeowl-fix/hwrt-blas-tdr-and-uaf'
git switch 'hingeowl-fix/hwrt-blas-tdr-and-uaf'
git rebase 'main'
git switch 'main'
git merge --no-ff 'hingeowl-fix/hwrt-blas-tdr-and-uaf'
git switch 'main'
git merge --squash 'hingeowl-fix/hwrt-blas-tdr-and-uaf'
git switch 'main'
git merge --ff-only 'hingeowl-fix/hwrt-blas-tdr-and-uaf'
git switch 'main'
git merge 'hingeowl-fix/hwrt-blas-tdr-and-uaf'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
3 participants
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
TrustNo1/Darner!268
No description provided.