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

Merged
OreoFR merged 2 commits from upstream-pr/2418 into main 2026-11-27 10:47:43 +00:00
OreoFR commented 2026-11-27 02:52:20 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-27T02:52:20Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5520351767 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2418). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-27T02:52:20Z. --- Claimed in the coordination issue as: [RT-WRITEBACK-FAULT-RELOCK](https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7208477548) ### 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)
tiguan26 commented 2026-11-27 02:57:15 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-27T02:57:15Z.

Review: APPROVED


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

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6082886266 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2418#pullrequestreview-6082886266). Originally posted by [tiguan26](https://github.com/tiguan26) on 2026-11-27T02:57:15Z. **Review:** APPROVED --- 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).
OreoFR commented 2026-11-27 03:32:16 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-27T03:32:16Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7209067395 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2418#issuecomment-7209067395). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-27T03:32:16Z. --- 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).
tiguan26 commented 2026-11-27 04:52:22 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-27T04:52:22Z.

Review: APPROVED


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

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6083634480 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2418#pullrequestreview-6083634480). Originally posted by [tiguan26](https://github.com/tiguan26) on 2026-11-27T04:52:22Z. **Review:** APPROVED --- 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).
aslan2142 commented 2026-11-27 10:47:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-27T10:47:40Z.


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)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7216313044 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2418#issuecomment-7216313044). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-11-27T10:47:40Z. --- 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)
OreoFR commented 2026-11-27 10:48:08 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-27T10:48:08Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7216322358 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2418#issuecomment-7216322358). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-27T10:48:08Z. --- 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).
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
ProjectLeonida/mandy!2418
No description provided.