d3d12: every title write-back range on one guarded page goes back (RT-TITLE-WRITEBACK-SAME-PAGE-RANGES) #2470

Open
github-bot wants to merge 1 commit from github-bot/gh-2470 into main AGit
Owner

@⁠Tyrannys on GitHub · 2026-11-27 14:43 UTC · original
Tyrannys:rt-title-writeback-same-page-ranges → main · +192 −46 · 3 files · 1 commits · open

Claimed in the coordination issue as: RT-TITLE-WRITEBACK-SAME-PAGE-RANGES (#1608). Found by @⁠73N1X (their finding in #1608, 2026-11-27 11:40).

Depends on: #2468 conflicts with this PR (same lines, see below)

What and why

DeviceState::write_back_titles handed each finished range to ShadowPages::write_back on its own. When the first range that differs is on a guarded page, write_back reopens that page, and reopen_locked clears kProtected. Every later disjoint range on that page then fails the held() check (not protected, not clean) and is dropped as if the title had written the page. But only the write-back itself wrote there, so the GPU's later words on that page never reach the title, and the next forward copy writes the title's older bytes over them in the shadow. CPU code that reads GPU results back (the write-back case) sees stale words.

Fix:

  • ShadowPages::write_back takes all of one tracker's finished ranges in one call (const std::pair<uintptr_t, uintptr_t>*, count, optional reached count). It decides which pages are held once for every page the ranges cover, after the journal, pin and write-watch checks and before the first copy. Then it copies range by range as before.
  • write_back_titles sorts the finished ranges (batches can overlap) and gives each shadow the ranges over it in one call. The log line's range count now comes from reached.
  • The per-page decisions are allocated before lock_ is taken. A thread that faults on a guarded page waits on lock_ in reopen(), and it may hold the heap lock while it waits.

How it relates to the other work on this path:

  • #2468 (OreoFR, RT-WRITEBACK-TILE-REOPEN) takes a snapshot of held before the copies in one call, so that a faulting copy's tile reopen does not drop later runs. This PR takes the same kind of snapshot, but across all of a tracker's ranges, so it also covers the #2468 case. The two change the same lines of write_back, so whichever lands second needs a rebase. After this PR, #2468's fix reduces to its test. I'm glad to rebase either way.
  • #2352 merges touching ranges in note_title_write. This PR does not touch that function and is about disjoint ranges.
  • #2461 changes the batch-draining loop in write_back_titles. This PR changes only the loop after it (the per-range calls). The two edit neighbouring lines, so a textual conflict is possible, but no logic is shared.
  • RT-WRITEBACK-FAULT-RELOCK (OreoFR, #2418 is merged): the writer_ handling is unchanged.

Known limit, unchanged by this PR: a page opened by the write-back stays writable by the title until the next refresh guards it again. Before this PR that was already true for the pieces of a single range after the first.

Tested

New test d3d12_title_window_write_back_ranges (d3d12_title_va_test --window-writeback-ranges). It makes a title window and lets a submission that does not reach the ring guard the pages. Then the GPU writes new words into the window through its arena address, so nothing is noted for that write. Before that submission, the test notes 16 four-byte ranges 16 bytes apart on the first page. The next refresh copies them back.

  • base ec91d695c: fails 3/3. 1/16 four-byte ranges on one guarded page reached it, 48/48 words between them kept the title's
  • with the fix: passes 3/3. 16/16 ranges reach the page, the 48 words between them keep the title's, and no word has any other value.

Write-back, shadow and title-memory tests (-R "write_back|writeback|shadow|title_memory|title_va|title_window|placed"): 19/19 pass, including d3d12_title_window_write_back_fault (#2418), d3d12_placed_write_back and d3d12_shadow_keep_refill(_off).

Full suite (ctest -C Release -j 4): 498/502. The four failures:

  • big_compile_gate timed out under -j 4 and passes alone.
  • xs_proxy_mock failed under -j 4 and passes alone.
  • xbox_present (present_planes) and d3d12_set_descriptor_heaps (recorded_commands) also fail on the base with the original d3d12_vulkan_device.cpp, so they are not from this change.

No game files or shaders were needed.

GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11

AI-assisted: yes (Claude Code)

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)

🤖 Generated with Claude Code

**@⁠Tyrannys** on GitHub · 2026-11-27 14:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2470) `Tyrannys:rt-title-writeback-same-page-ranges` → `main` · +192 −46 · 3 files · 1 commits · open Claimed in the coordination issue as: RT-TITLE-WRITEBACK-SAME-PAGE-RANGES (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608). Found by @⁠73N1X (their finding in #1608, 2026-11-27 11:40). Depends on: #2468 conflicts with this PR (same lines, see below) ### What and why `DeviceState::write_back_titles` handed each finished range to `ShadowPages::write_back` on its own. When the first range that differs is on a guarded page, `write_back` reopens that page, and `reopen_locked` clears `kProtected`. Every later disjoint range on that page then fails the `held()` check (not protected, not clean) and is dropped as if the title had written the page. But only the write-back itself wrote there, so the GPU's later words on that page never reach the title, and the next forward copy writes the title's older bytes over them in the shadow. CPU code that reads GPU results back (the write-back case) sees stale words. Fix: - `ShadowPages::write_back` takes all of one tracker's finished ranges in one call (`const std::pair<uintptr_t, uintptr_t>*`, count, optional `reached` count). It decides which pages are held once for every page the ranges cover, after the journal, pin and write-watch checks and before the first copy. Then it copies range by range as before. - `write_back_titles` sorts the finished ranges (batches can overlap) and gives each shadow the ranges over it in one call. The log line's range count now comes from `reached`. - The per-page decisions are allocated before `lock_` is taken. A thread that faults on a guarded page waits on `lock_` in `reopen()`, and it may hold the heap lock while it waits. How it relates to the other work on this path: - **#2468** (OreoFR, RT-WRITEBACK-TILE-REOPEN) takes a snapshot of `held` before the copies in one call, so that a faulting copy's tile reopen does not drop later runs. This PR takes the same kind of snapshot, but across all of a tracker's ranges, so it also covers the #2468 case. The two change the same lines of `write_back`, so whichever lands second needs a rebase. After this PR, #2468's fix reduces to its test. I'm glad to rebase either way. - **#2352** merges touching ranges in `note_title_write`. This PR does not touch that function and is about disjoint ranges. - **#2461** changes the batch-draining loop in `write_back_titles`. This PR changes only the loop after it (the per-range calls). The two edit neighbouring lines, so a textual conflict is possible, but no logic is shared. - **RT-WRITEBACK-FAULT-RELOCK** (OreoFR, #2418 is merged): the `writer_` handling is unchanged. Known limit, unchanged by this PR: a page opened by the write-back stays writable by the title until the next refresh guards it again. Before this PR that was already true for the pieces of a single range after the first. ### Tested New test `d3d12_title_window_write_back_ranges` (`d3d12_title_va_test --window-writeback-ranges`). It makes a title window and lets a submission that does not reach the ring guard the pages. Then the GPU writes new words into the window through its arena address, so nothing is noted for that write. Before that submission, the test notes 16 four-byte ranges 16 bytes apart on the first page. The next refresh copies them back. - base `ec91d695c`: fails 3/3. `1/16 four-byte ranges on one guarded page reached it, 48/48 words between them kept the title's` - with the fix: passes 3/3. 16/16 ranges reach the page, the 48 words between them keep the title's, and no word has any other value. Write-back, shadow and title-memory tests (`-R "write_back|writeback|shadow|title_memory|title_va|title_window|placed"`): 19/19 pass, including `d3d12_title_window_write_back_fault` (#2418), `d3d12_placed_write_back` and `d3d12_shadow_keep_refill(_off)`. Full suite (`ctest -C Release -j 4`): 498/502. The four failures: - `big_compile_gate` timed out under `-j 4` and passes alone. - `xs_proxy_mock` failed under `-j 4` and passes alone. - `xbox_present` (`present_planes`) and `d3d12_set_descriptor_heaps` (`recorded_commands`) also fail on the base with the original `d3d12_vulkan_device.cpp`, so they are not from this change. No game files or shaders were needed. GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11 AI-assisted: yes (Claude Code) ### 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) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
DeviceState::write_back_titles handed each finished range to
ShadowPages::write_back on its own. The first range that differed reopened
its guarded page (kProtected cleared), and every later disjoint range on that
page then read as written by the title and was dropped, though only the
write-back itself had written there.

Each shadow now takes all of its finished ranges in one call, sorted by
address, and decides which pages are held once for all of them, before the
first copy. The per-page decisions are allocated before the tracker's lock is
taken.

New test d3d12_title_window_write_back_ranges: 16 four-byte ranges 16 bytes
apart on one guarded page of a title window. On main 1/16 reach the page
(3/3 runs); with the fix 16/16, and the words between them keep the title's.

Found by 73N1X in #1608.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 14:46:04 +00:00
Author
Owner

@⁠lixonik on GitHub · 2026-11-27 15:20 UTC · original · reviewed

Reviewed and tested at 27c167376 (merged cleanly onto main ec91d695c).

Batching all finished ranges per tracker into one ShadowPages::write_back() call and snapshotting held_pages once before the first copy cleanly fixes the multi-range-on-one-guarded-page drop, and passes all write-back GPU tests on RTX 5070.

Two coordination / optimization notes:

  • Overlap with #2468: snapshotting held_pages across all ranges in one call also addresses #2468's same-tile reopen bug (including #2468's noted multi-range-in-one-tile limit). Both PRs touch the same lines in runtime/CMakeLists.txt, runtime/shims/d3d12_vulkan_device.cpp, and runtime/test/d3d12_title_va_test.cpp; if #2470 lands, pulling in #2468's host_heap held_pages_ member and --window-writeback-fault-tile test covers both issues cleanly.
  • Two minor perf notes inline on held_pages allocation and repeated reopen_locked() calls for ranges sharing a page.

Verified locally on Windows 11 (MSVC 19.44.35230.0, Ninja Release, NVIDIA GeForce RTX 5070, driver 610.88, Vulkan 1.4.341; note VK_LAYER_KHRONOS_validation is not installed locally so Vulkan validation layers did not run):

  • d3d12_title_window_write_back, d3d12_title_window_write_back_fault, d3d12_title_window_write_back_ranges (16/16 four-byte ranges reached the guarded page, 48/48 surrounding words kept), d3d12_placed_write_back: 4/4 passed
  • scripts/check_no_game_data.py and git diff --check: clean

AI-assisted.

**@⁠lixonik** on GitHub · 2026-11-27 15:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2470#pullrequestreview-6090272733) · **reviewed** Reviewed and tested at `27c167376` (merged cleanly onto `main` `ec91d695c`). Batching all finished ranges per tracker into one `ShadowPages::write_back()` call and snapshotting `held_pages` once before the first copy cleanly fixes the multi-range-on-one-guarded-page drop, and passes all write-back GPU tests on RTX 5070. Two coordination / optimization notes: - **Overlap with `#2468`**: snapshotting `held_pages` across all ranges in one call also addresses `#2468`'s same-tile reopen bug (including `#2468`'s noted multi-range-in-one-tile limit). Both PRs touch the same lines in `runtime/CMakeLists.txt`, `runtime/shims/d3d12_vulkan_device.cpp`, and `runtime/test/d3d12_title_va_test.cpp`; if `#2470` lands, pulling in `#2468`'s `host_heap` `held_pages_` member and `--window-writeback-fault-tile` test covers both issues cleanly. - Two minor perf notes inline on `held_pages` allocation and repeated `reopen_locked()` calls for ranges sharing a page. Verified locally on Windows 11 (MSVC 19.44.35230.0, Ninja Release, NVIDIA GeForce RTX 5070, driver 610.88, Vulkan 1.4.341; note `VK_LAYER_KHRONOS_validation` is not installed locally so Vulkan validation layers did not run): - `d3d12_title_window_write_back`, `d3d12_title_window_write_back_fault`, `d3d12_title_window_write_back_ranges` (16/16 four-byte ranges reached the guarded page, 48/48 surrounding words kept), `d3d12_placed_write_back`: 4/4 passed - `scripts/check_no_game_data.py` and `git diff --check`: clean AI-assisted.
@ -421,0 +436,4 @@
if (lo >= hi) return 0;
// Allocated before lock_ is taken: a thread that faults on a guarded page waits on lock_ in reopen(), and may
// hold the heap's lock while it does.
std::vector<std::uint8_t> held_pages((hi - lo) / kPage);
Author
Owner

@⁠lixonik on GitHub · 2026-11-27 15:20 UTC · original

Good that held_pages is allocated before AcquireSRWLockExclusive(&lock_) and destroyed after ReleaseSRWLockExclusive(&lock_) on every return path, avoiding the fault-handler process-heap deadlock. Keeping it as a reusable Vector<std::uint8_t> (leonida::host_heap) member on ShadowPages (as in #2468) would also avoid per-call process-heap allocations when two ranges touch opposite ends of a large tracker.

**@⁠lixonik** on GitHub · 2026-11-27 15:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2470#discussion_r4721091784) Good that `held_pages` is allocated before `AcquireSRWLockExclusive(&lock_)` and destroyed after `ReleaseSRWLockExclusive(&lock_)` on every return path, avoiding the fault-handler process-heap deadlock. Keeping it as a reusable `Vector<std::uint8_t>` (`leonida::host_heap`) member on `ShadowPages` (as in `#2468`) would also avoid per-call process-heap allocations when two ranges touch opposite ends of a large tracker.
@ -475,0 +505,4 @@
const std::uint8_t* copy = t.shadow + (c - t.begin);
if (std::memcmp(reinterpret_cast<const void*>(c), copy, e - c) != 0) {
if (!opened) {
reopen_locked(at, at + pages * kPage);
Author
Owner

@⁠lixonik on GitHub · 2026-11-27 15:20 UTC · original

Minor: bool opened is reset per range r, so when many disjoint ranges share one guarded page (e.g. 130 four-byte ranges on one page), range 0 clears kProtected and ranges 1..129 still enter reopen_locked(at, at + pages * kPage) and issue a VirtualQuery each time. Skipping reopen_locked when no page in [at, at + pages * kPage) still has kProtected set avoids the redundant VirtualQuery calls.

**@⁠lixonik** on GitHub · 2026-11-27 15:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2470#discussion_r4721091793) Minor: `bool opened` is reset per range `r`, so when many disjoint ranges share one guarded page (e.g. 130 four-byte ranges on one page), range 0 clears `kProtected` and ranges 1..129 still enter `reopen_locked(at, at + pages * kPage)` and issue a `VirtualQuery` each time. Skipping `reopen_locked` when no page in `[at, at + pages * kPage)` still has `kProtected` set avoids the redundant `VirtualQuery` calls.
Author
Owner

@⁠djrolfes on GitHub · 2026-11-27 15:26 UTC · original

Independent Linux/Wine + NVIDIA check at head 27c167376 (merged cleanly onto origin/main ec91d695c in a local integration stack alongside #2467, #2463, #2469, #2461, #2126, #2091, and two local V# patches).

  • Environment: Linux 7.2.15, wine-11.22, clang-cl + xwin cross-build, NVIDIA GeForce RTX 3070 Ti 8 GB, driver 620.73.04, Ryzen 7 5800X, 32 GB RAM. No game files.
  • CTest:
    • d3d12_title_window_write_back_ranges: passed (6.06 s) — 16/16 four-byte ranges on one guarded page reached it, 48/48 words between them kept the title's, 0 words neither.
    • d3d12_title_va, d3d12_title_va_shadow, d3d12_title_window_cap, d3d12_title_window_write_back: all passed (~6.0 s each).
    • d3d12_title_window_write_back_fault: skipped as expected under Wine (write watching is off).
  • Code review: allocating held_pages before taking lock_ and computing page hold state once across all sorted ranges per shadow cleanly avoids both the same-page reopen drop and the lock-inversion hazard.

AI-assisted.

**@⁠djrolfes** on GitHub · 2026-11-27 15:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2470#issuecomment-7222061018) Independent Linux/Wine + NVIDIA check at head `27c167376` (merged cleanly onto `origin/main` `ec91d695c` in a local integration stack alongside `#2467`, `#2463`, `#2469`, `#2461`, `#2126`, `#2091`, and two local V# patches). - **Environment:** Linux `7.2.15`, `wine-11.22`, `clang-cl` + `xwin` cross-build, NVIDIA GeForce RTX 3070 Ti 8 GB, driver `620.73.04`, Ryzen 7 5800X, 32 GB RAM. No game files. - **CTest:** - `d3d12_title_window_write_back_ranges`: **passed** (`6.06 s`) — `16/16 four-byte ranges on one guarded page reached it, 48/48 words between them kept the title's, 0 words neither`. - `d3d12_title_va`, `d3d12_title_va_shadow`, `d3d12_title_window_cap`, `d3d12_title_window_write_back`: all **passed** (`~6.0 s` each). - `d3d12_title_window_write_back_fault`: skipped as expected under Wine (`write watching is off`). - **Code review:** allocating `held_pages` before taking `lock_` and computing page hold state once across all sorted ranges per shadow cleanly avoids both the same-page reopen drop and the lock-inversion hazard. AI-assisted.
This pull request can be merged automatically.
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 origin '+refs/pull/2470/head:github-bot/gh-2470'
git switch 'github-bot/gh-2470'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2470'
git switch 'github-bot/gh-2470'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2470'
git switch 'github-bot/gh-2470'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2470'
git switch 'main'
git merge --squash 'github-bot/gh-2470'
git switch 'main'
git merge --ff-only 'github-bot/gh-2470'
git switch 'main'
git merge 'github-bot/gh-2470'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.