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

Manually merged
github merged 2 commits from github-bot/gh-2418 into main 2026-11-27 10:53:03 +00:00 AGit
Owner

@⁠OreoFR on GitHub · 2026-11-27 02:52 UTC · original
OreoFR:fix/write-back-fault-relock → main · +150 −5 · 4 files · 2 commits · merged 2026-11-27 10:47 UTC

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

**@⁠OreoFR** on GitHub · 2026-11-27 02:52 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2418) `OreoFR:fix/write-back-fault-relock` → `main` · +150 −5 · 4 files · 2 commits · merged 2026-11-27 10:47 UTC Claimed in the coordination issue as: [RT-WRITEBACK-FAULT-RELOCK](https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### 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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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](https://claude.com/claude-code)
ShadowPages::write_back copies a window's GPU writes back to the title's pages under the tracker's
SRW lock (#2129, and the 256-byte pieces of #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 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 02:55:54 +00:00
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-27 02:57 UTC · original · approved these changes

Approving 95d25843e.

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

**@⁠tiguan26** on GitHub · 2026-11-27 02:57 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2418#pullrequestreview-6082886266) · **approved these changes** Approving 95d25843e. 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 (d1dff7949). 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).
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 95d25843e 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 <[email protected]>
Author
Owner

@⁠OreoFR on GitHub · 2026-11-27 03:32 UTC · original

Added a regression test for the faulting write-back (0d61aa9be), 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 95d25843e: 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).

**@⁠OreoFR** on GitHub · 2026-11-27 03:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2418#issuecomment-7209067395) Added a regression test for the faulting write-back (0d61aa9be), 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 95d25843e: 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).
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-27 04:52 UTC · original · approved these changes

Approving 0d61aa9be. 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).

**@⁠tiguan26** on GitHub · 2026-11-27 04:52 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2418#pullrequestreview-6083634480) · **approved these changes** Approving 0d61aa9be. 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).
github manually merged commit 04d6d25183 into main 2026-11-27 10:53:03 +00:00
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-27 10:47 UTC · original

Merging.

  • Why: a write-back that faults took its tracker's lock a second time and froze after the world load. It no longer does.
  • Lock order, reviewed: every exclusive take of g_shadow_pages_lock happens under shadow_mutex, which the write-back already holds, so the handler's shared take cannot form a cycle with one device. With a second D3D12 device it could; that is a follow-up, not a blocker.
  • Tested on main 2859d0c9b + #2418, #2440, #2432 and #2439, integrated RDNA2 GPU (RADV, Mesa 26.3.1):
    • Windows suite under Wine: 425 pass, 25 fail. The only changes against the stored baseline are xdxr_build_check (a timeout under load) and xmem_no_write_watch, both flaky here.
    • Linux runtime ctest: 115 of 116, the same timeout.
  • Not tested: the new test skips under Wine, which has no write watching, so the fail-before and pass-after evidence is the author's runs. No game run from this head.

Thanks @⁠OreoFR, and @⁠tiguan26 for the review.

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

**@⁠aslan2142** on GitHub · 2026-11-27 10:47 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2418#issuecomment-7216313044) Merging. - **Why:** a write-back that faults took its tracker's lock a second time and froze after the world load. It no longer does. - **Lock order, reviewed:** every exclusive take of `g_shadow_pages_lock` happens under `shadow_mutex`, which the write-back already holds, so the handler's shared take cannot form a cycle with one device. With a second D3D12 device it could; that is a follow-up, not a blocker. - **Tested on `main` `2859d0c9b` + #2418, #2440, #2432 and #2439**, integrated RDNA2 GPU (RADV, Mesa 26.3.1): - Windows suite under Wine: 425 pass, 25 fail. The only changes against the stored baseline are `xdxr_build_check` (a timeout under load) and `xmem_no_write_watch`, both flaky here. - Linux runtime `ctest`: 115 of 116, the same timeout. - **Not tested:** the new test skips under Wine, which has no write watching, so the fail-before and pass-after evidence is the author's runs. No game run from this head. Thanks @⁠OreoFR, and @⁠tiguan26 for the review. AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)
Author
Owner

@⁠OreoFR on GitHub · 2026-11-27 10:48 UTC · original

In-game A/B for @⁠aslan2142, as asked. Two builds that differ only in this PR: plain main d1dff7949 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).

**@⁠OreoFR** on GitHub · 2026-11-27 10:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2418#issuecomment-7216322358) In-game A/B for @⁠aslan2142, as asked. Two builds that differ only in this PR: plain `main` d1dff7949 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).
This discussion has been locked. Commenting is limited to contributors.
No description provided.