Skip to content

d3d12: a write-back that faults does not take its tracker's lock again (RT-WRITEBACK-FAULT-RELOCK) - #2418

Merged
aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
OreoFR:fix/write-back-fault-relock
Nov 27, 2026
Merged

aslan2142 merged 2 commits into
ProjectLeonida:mainfrom
OreoFR:fix/write-back-fault-relock

Conversation

@OreoFR

@OreoFR OreoFR commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-WRITEBACK-FAULT-RELOCK

What and why

ShadowPages::write_back (runtime/shims/d3d12_vulkan_device.cpp) copies a window's GPU writes back to the title's pages while holding the tracker's SRW lock (#2129; #2404 made the copy 256-byte pieces). It reopens its own guard first. But the page can still be read-only because of another guard: the geometry watch, or another view's shadow over the same pages. The memcpy then faults, and shadow_write_fault asks every tracker over the address, this one included. reopen() took lock_ again on the thread that already held it. SRW locks aren't recursive, so the title's submitting thread hung for good: the main loop froze, with no device loss and no gate wait.

Seen on an RTX 4070 Laptop (NVIDIA 621.40), retail: the main loop froze at 209, 267, 309 s… in four runs. The symbolized thread dumps after each freeze show the submitting thread at:

CommandQueue::ExecuteCommandLists -> VulkanQueue::execute_now -> DeviceState::refresh_shadows
  -> write_back_titles -> ShadowPages::write_back (memcpy) -> [access violation]
  -> shadow_write_fault -> ShadowPages::reopen -> SRW lock wait

The change: write_back records its thread in writer_ while it holds lock_. reopen() and reopen_range() skip taking the lock on that thread, since they already run under it. Other threads wait on the lock as before. The docs paragraph on the write-back says so.

Tested

  • Retail, same build and recipe: without the fix, frozen at 267 s, with the dump above. With it, still submitting past 480 s; at 280 s one dump caught a thread waiting in the fault handler briefly, gone by 330 s. MEASURED (n=1 each).
  • The 13 tests/test_docs_*.py pass.
  • No unit test: ShadowPages and shadow_write_fault sit in the anonymous namespace of d3d12_vulkan_device.cpp. A regression test needs a title page guarded by two trackers at once, with a window write-back over it. I can add one to d3d12_title_va_test if a reviewer wants it in this PR.

GPUs tested: NVIDIA GeForce RTX 4070 Laptop 8 GB, driver 621.40, Windows 11.

AI-assisted: yes (Claude Code, Claude Opus 5.5)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: runtime only)

🤖 Generated with Claude Code

…n in the fault handler

ShadowPages::write_back copies a window's GPU writes back to the title's pages under the tracker's
SRW lock (ProjectLeonida#2129, and the 256-byte pieces of ProjectLeonida#2404). It reopens its own guard first, but the page can
still be read-only because of another guard (the geometry watch, another view's shadow). The copy
then faults, shadow_write_fault asks every tracker over the address, and this one's reopen() took
lock_ again on the thread that already held it. SRW locks are not recursive, so the title's
submitting thread hung for good: on an RTX 4070 Laptop (NVIDIA 621.40) the main loop froze at
209-309 s with no device loss, and the thread dumps showed the submitting thread inside
write_back -> (fault) -> shadow_write_fault -> reopen, waiting on the SRW lock, in four runs.

write_back records its thread in writer_ while it holds lock_; reopen() and reopen_range() skip
taking the lock on that thread (they run under it). Other threads still wait on the lock as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@tiguan26 tiguan26 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving 95d2584.

Read the change against the whole of write_back and the fault handler. Both exits of write_back clear writer_ before releasing lock_, and a relaxed atomic is enough here: only a thread ever stores its own id, so it always sees its latest store. shadow_write_fault calls three things on a tracker, contains (no lock), reopen and reopen_range, and the PR covers both that lock. write_back runs from the title write-back pass under shadow_mutex, and none of the three exclusive g_shadow_pages_lock sites calls it, so the handler's shared take of that lock cannot become the same hang one level up. If the handler's reopen changes a page's state mid-copy, held() only becomes more cautious (that GPU result is dropped), never a stale copy.

The branch sits on current main (d1dff79). Cross-built with clang-cl 19 + xwin, run under Wine 11.22 on llvmpipe: d3d12_title_va, d3d12_title_va_shadow, d3d12_title_window_write_back, d3d12_placed_write_back, both d3d12_shadow_keep_refill, shadow_keep and title_va_alias pass. The faulting write-back itself does not happen in those tests, so the hang is covered by your four 621.40 runs, not by these. AI-assisted (Claude Code, Claude Opus 5.5).

…eturns (ProjectLeonida#2418)

d3d12_title_window_write_back_fault (d3d12_title_va_test --window-writeback-fault) makes the write-back's
copy fault on a page the tracker did not protect itself, the case review asked for: the ring is
write-watched, a title write makes its first page hot (writable, asked about rather than guarded), and
a geometry watch cell over the first 64 KiB, what a geometry view over the same pages arms, makes that
page read-only behind the tracker's back. The next submission's write-back faults there; the handler
reopens the cell and asks every tracker over the address, the writing one too. Without 95d2584 that
tracker took its own SRW lock again on the thread that held it and the submission never returned: a
watchdog thread ends the process with a FAIL line after 20 s. With it the submission returns and the
GPU's words, and the title's own word, are in the pages.

RTX 4070 Laptop, NVIDIA 621.40, Windows 11: passes (8 of 8 runs) with the fix; with the fix's
d3d12_vulkan_device.cpp change reverted locally it fails at the watchdog. d3d12_title_va,
d3d12_title_window_write_back and d3d12_placed_write_back pass either way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@OreoFR

OreoFR commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Added a regression test for the faulting write-back (0d61aa9), the case tiguan26 pointed out the existing tests never reach: d3d12_title_window_write_back_fault (d3d12_title_va_test --window-writeback-fault).

How it works: the ring is a write-watched reservation. The first submission makes its window and the tracker guards the pages. A title write then makes the first page hot, so it is writable and asked about instead of guarded. Next, a geometry watch cell over the first 64 KiB (geometry_writes::acquire + arm, what a geometry view over the same pages arms) makes that page read-only without the tracker knowing. The next submission's write-back skips that page in reopen_locked (it is not kProtected), and its memcpy faults. shadow_write_fault reopens the cell and then calls reopen() on the tracker whose copy faulted. A watchdog thread ends the process with a FAIL line if the submission has not returned after 20 s.

Results on an RTX 4070 Laptop, NVIDIA 621.40, Windows 11:

  • With 95d2584: passes (8 of 8 runs). Every word the shader wrote reaches the title's pages (64 of 64) and the title's own word is kept.
  • With the fix's d3d12_vulkan_device.cpp change reverted locally: the submission hangs, and the watchdog fails the test at 20 s.
  • d3d12_title_va, d3d12_title_window_write_back and d3d12_placed_write_back pass either way.

AI-assisted (Claude Code, Claude Opus 5.5).

@tiguan26 tiguan26 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving 0d61aa9. Thanks for the test.

d3d12_title_window_write_back_fault builds exactly the case the other tests never reached: a page the tracker treats as hot (writable, asked about) that a geometry cell then makes read-only behind its back, so the write-back's memcpy faults and the handler calls reopen() on the tracker whose own thread holds the lock. The watchdog turns the old hang into a FAIL line instead of a stuck run, and the checks afterwards (the GPU's words reached the page, the title's own word 1000 kept) cover the result as well as the return.

The new test skips here, as it says it will: Wine has no write watching, so I could not run it, and your 621.40 runs are the evidence for the fault path. On the new head, cross-built with clang-cl 19 + xwin under Wine 11.22 on llvmpipe, the other eight title and write-back tests still pass (d3d12_title_va, _shadow, d3d12_title_window_write_back, d3d12_placed_write_back, both d3d12_shadow_keep_refill, shadow_keep, title_va_alias). AI-assisted (Claude Code, Claude Opus 5.5).

@aslan2142

Copy link
Copy Markdown
Collaborator

Merging.

Thanks @OreoFR, and @tiguan26 for the review.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

@aslan2142
aslan2142 merged commit 04d6d25 into ProjectLeonida:main Nov 27, 2026
7 of 12 checks passed
@OreoFR

OreoFR commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

In-game A/B for @aslan2142, as asked. Two builds that differ only in this PR: plain main d1dff79 vs main + #2418, with nothing else merged. Same shader pack, same settings, 11 min each, RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11.

build submissions world phase (indirect draws) write-back hang in the thread dumps device loss / exceptions
main stopped at t=290 s, nothing after reached, then frozen yes, at 330 s and 380 s: the submitting thread inside write_back → fault → reopen waiting on the lock it holds 0 / 0
main + #2418 until the end (t=648 s) reached and running none 0 / 0

No regression seen: no crash, no new hang, no device loss, and the run went on submitting for the whole 11 minutes. MEASURED (n=1 each). AI-assisted (Claude Code, Claude Opus 5.5).

aslan2142 pushed a commit that referenced this pull request Nov 27, 2026
… still go back (RT-WRITEBACK-TILE-REOPEN)

ShadowPages::write_back decided page by page, inside its loop, whether a page's GPU result goes back. A copy
that faults on another guard (a geometry watch cell) makes the handler reopen the whole 64 KiB tile on the
write-back's own thread (reopen_range, under the lock #2418 lets it reuse), which clears kClean and kProtected
there. The runs after it in the tile then read as written by the title: their GPU results were dropped from
a batch already taken off the queue, and the next forward copy put the title's older bytes over them.

The decision is now taken for every page of the range before the first copy, into a host-heap vector the
tracker keeps. A page the title wrote before the write-back (not guarded and not clean after the asking)
is still held, so the title's words win as before.

Test: d3d12_title_window_write_back_fault_tile (d3d12_title_va_test --window-writeback-fault-tile), the
#2418 fault case with a second dispatch on the third page of the tile and a title write on the second.
Found by DadaCap's review of #2418.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants