hwrt: avoid concurrent-THIN TLAS reallocation and mid-frame descriptor swap #279

Open
hingeowl wants to merge 1 commit from hingeowl/Darner:fix/hwrt-tlas-concurrent-thin-race into main
Contributor

Summary

Fixes a Hardware RT 0x887A0006 (DXGI_ERROR_DEVICE_HUNG) / nvlddmkm Event ID 13 (Graphics SM Warp Exception: MMU Fault) race when two DARNER_THIN=1 worker threads record TLAS builds concurrently.

Root Cause

In crates/darner-gpu/src/hwrt.rs, alloc_or_reuse_as() gated in-place reuse on Arc::strong_count(existing) == 1 for both BLAS and TLAS so a build would not overwrite a buffer aliased by execute_copy().

However, execute_tlas() holds its own local dest_as: Arc<HostAs> across BuildRaytracingAccelerationStructure, ResourceBarrier, and emit_postbuild() (~0.5 ms). When two DARNER_THIN=1 worker threads enter execute_tlas() for the same TLAS console_va within that window:

  1. Thread A's local dest_as raises Arc::strong_count(existing) from 1 to 2.
  2. Thread B calls alloc_or_reuse_as(), sees Arc::strong_count(existing) != 1, allocates a brand-new unbuilt 4 MiB TLAS buffer, overwrites as_map[console_va], and immediately calls crate::descriptor::refresh_tlas_views().
  3. refresh_tlas_views() overwrites live shader-visible SRV descriptors with the uninitialized TLAS GPU VA while earlier frames are still executing TraceRay on the GPU, causing an immediate SM Warp MMU Fault (nvlddmkm Event ID 13) and 0x887A0006.

(Note: execute_tlas's plane_upload closure already had a comment noting this exact Arc::strong_count hazard under DARNER_THIN, but execute_tlas itself still held dest_as across the build.)

Evidence

Observed in two independent 4K + Hardware RT playtest crashes with the exact same NVIDIA shader fault PC (nvlddmkm Event ID 13, ESR ...=0xf812b60 / 0x1104). In both crash logs, every preceding frame had a single worker thread recording TLAS builds until the final frame right before device removal, where two worker threads overlapped and recorded 4 TLAS builds within 0.1 ms (Crash 1) and 0.5 ms (Crash 2) of each other. Increasing DARNER_THIN worker thread count (e.g., PR #37) increases the overlap frequency and reproduces the fault sooner.

Fix

  • Add copied: AtomicBool on HostAs, set in execute_copy() when a HostAs is aliased to a second console_va.
  • In alloc_or_reuse_as(), for AsKind::Tlas (when !force_new && !tlas_fresh()), check !existing.copied.load(Ordering::Acquire) instead of Arc::strong_count(existing) == 1.
  • Add a matching double-check under the as_map.write() lock before inserting a newly allocated TLAS so two threads racing on a first build/resize also avoid redundant reallocation and double refresh_tlas_views().

Validation

  • cargo test -p darner-gpu --locked -- hwrt:: — 26 passed, 0 failed.
  • Built release darner cleanly.
  • Related to #268 (which fixes the separate BLAS backlog/budget TDR and MeshletCache/HostAs BLAS UAFs).
### Summary Fixes a Hardware RT `0x887A0006` (`DXGI_ERROR_DEVICE_HUNG`) / `nvlddmkm` Event ID 13 (`Graphics SM Warp Exception: MMU Fault`) race when two `DARNER_THIN=1` worker threads record TLAS builds concurrently. ### Root Cause In `crates/darner-gpu/src/hwrt.rs`, `alloc_or_reuse_as()` gated in-place reuse on `Arc::strong_count(existing) == 1` for both BLAS and TLAS so a build would not overwrite a buffer aliased by `execute_copy()`. However, `execute_tlas()` holds its own local `dest_as: Arc<HostAs>` across `BuildRaytracingAccelerationStructure`, `ResourceBarrier`, and `emit_postbuild()` (~0.5 ms). When two `DARNER_THIN=1` worker threads enter `execute_tlas()` for the same TLAS `console_va` within that window: 1. Thread A's local `dest_as` raises `Arc::strong_count(existing)` from `1` to `2`. 2. Thread B calls `alloc_or_reuse_as()`, sees `Arc::strong_count(existing) != 1`, allocates a **brand-new unbuilt 4 MiB TLAS buffer**, overwrites `as_map[console_va]`, and immediately calls `crate::descriptor::refresh_tlas_views()`. 3. `refresh_tlas_views()` overwrites live shader-visible SRV descriptors with the uninitialized TLAS GPU VA while earlier frames are still executing `TraceRay` on the GPU, causing an immediate SM Warp MMU Fault (`nvlddmkm` Event ID 13) and `0x887A0006`. *(Note: `execute_tlas`'s `plane_upload` closure already had a comment noting this exact `Arc::strong_count` hazard under `DARNER_THIN`, but `execute_tlas` itself still held `dest_as` across the build.)* ### Evidence Observed in two independent 4K + Hardware RT playtest crashes with the exact same NVIDIA shader fault PC (`nvlddmkm` Event ID 13, `ESR ...=0xf812b60 / 0x1104`). In both crash logs, every preceding frame had a single worker thread recording TLAS builds until the final frame right before device removal, where two worker threads overlapped and recorded `4 TLAS` builds within `0.1 ms` (Crash 1) and `0.5 ms` (Crash 2) of each other. Increasing `DARNER_THIN` worker thread count (e.g., PR #37) increases the overlap frequency and reproduces the fault sooner. ### Fix - Add `copied: AtomicBool` on `HostAs`, set in `execute_copy()` when a `HostAs` is aliased to a second `console_va`. - In `alloc_or_reuse_as()`, for `AsKind::Tlas` (when `!force_new && !tlas_fresh()`), check `!existing.copied.load(Ordering::Acquire)` instead of `Arc::strong_count(existing) == 1`. - Add a matching double-check under the `as_map.write()` lock before inserting a newly allocated TLAS so two threads racing on a first build/resize also avoid redundant reallocation and double `refresh_tlas_views()`. ### Validation - `cargo test -p darner-gpu --locked -- hwrt::` — 26 passed, 0 failed. - Built release `darner` cleanly. - Related to #268 (which fixes the separate BLAS backlog/budget TDR and `MeshletCache`/`HostAs` BLAS UAFs).
When two DARNER_THIN worker threads enter execute_tlas() for the same TLAS console VA within ~0.5 ms of each other, the first thread's local Arc<HostAs> raises Arc::strong_count(existing) from 1 to 2. alloc_or_reuse_as() treated strong_count > 1 as a shared copy, allocated a brand new unbuilt 4 MiB TLAS buffer, and immediately called descriptor::refresh_tlas_views() to point live shader-visible SRV descriptors at the uninitialized buffer while earlier frames were tracing it on the GPU — causing nvlddmkm Event 13 (SM Warp Exception: MMU Fault) and 0x887A0006.

Track execute_copy() aliasing explicitly via HostAs::copied (AtomicBool) and check !existing.copied for AsKind::Tlas in alloc_or_reuse_as() (with a double-check under the write lock), so concurrent worker threads reuse the same TLAS buffer in place and never expose an unbuilt TLAS to in-flight TraceRay shaders.
First-time contributor

PASS

Review of 353404b72.

The TLAS reuse and in-flight descriptor handling changes were audited, including the interaction with the adjacent #268 work. 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 — 25 passed, 0 failed.

Not run in-game.

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

**PASS** Review of `353404b72`. The TLAS reuse and in-flight descriptor handling changes were audited, including the interaction with the adjacent `#268` work. 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` — 25 passed, 0 failed. Not run in-game. AI-assisted: yes (GitHub Copilot, Auto mode).
Contributor

Code review only (read the diff at 353404b7), nothing built or run. I agree with lewis499: no blocking issue.

  • The copied flag goes in before the alias does (under the as_map write lock), and the second check under that lock drops the extra TLAS without a refresh_tlas_views. Looks right.
  • The cause (two thin workers within 0.1–0.5 ms) is likely but read from two logs. A longer RT run with DARNER_THIN and more workers (#37), with and without this PR, would settle it.
  • The title needs a type tag for the label bot, e.g. [pr-fix][RAYTRACING].
  • Merges cleanly with #268 and #318 (same file).

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

Code review only (read the diff at `353404b7`), nothing built or run. I agree with lewis499: no blocking issue. - The `copied` flag goes in before the alias does (under the `as_map` write lock), and the second check under that lock drops the extra TLAS without a `refresh_tlas_views`. Looks right. - The cause (two thin workers within 0.1–0.5 ms) is likely but read from two logs. A longer RT run with `DARNER_THIN` and more workers (#37), with and without this PR, would settle it. - The title needs a type tag for the label bot, e.g. `[pr-fix][RAYTRACING]`. - Merges cleanly with #268 and #318 (same file). 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-tlas-concurrent-thin-race:hingeowl-fix/hwrt-tlas-concurrent-thin-race'
git switch 'hingeowl-fix/hwrt-tlas-concurrent-thin-race'

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-tlas-concurrent-thin-race'
git switch 'hingeowl-fix/hwrt-tlas-concurrent-thin-race'
git rebase 'main'
git switch 'main'
git merge --ff-only 'hingeowl-fix/hwrt-tlas-concurrent-thin-race'
git switch 'hingeowl-fix/hwrt-tlas-concurrent-thin-race'
git rebase 'main'
git switch 'main'
git merge --no-ff 'hingeowl-fix/hwrt-tlas-concurrent-thin-race'
git switch 'main'
git merge --squash 'hingeowl-fix/hwrt-tlas-concurrent-thin-race'
git switch 'main'
git merge --ff-only 'hingeowl-fix/hwrt-tlas-concurrent-thin-race'
git switch 'main'
git merge 'hingeowl-fix/hwrt-tlas-concurrent-thin-race'
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!279
No description provided.