Repository navigation
Conversation
…-TITLE-WRITEBACK-SAME-PAGE-RANGES) 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 ProjectLeonida#1608. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lixonik
left a comment
There was a problem hiding this comment.
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: snapshottingheld_pagesacross 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 inruntime/CMakeLists.txt,runtime/shims/d3d12_vulkan_device.cpp, andruntime/test/d3d12_title_va_test.cpp; if#2470lands, pulling in#2468'shost_heapheld_pages_member and--window-writeback-fault-tiletest covers both issues cleanly. - Two minor perf notes inline on
held_pagesallocation and repeatedreopen_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 passedscripts/check_no_game_data.pyandgit diff --check: clean
AI-assisted.
| 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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Thanks, done in d7b81dd. The merge with main brought in #2468's held_pages_ (a leonida::host_heap Vector member), and this branch now uses it for the snapshot over all ranges. I size it before lock_ is taken (only write_back uses it, and one write_back runs at a time under shadow_mutex), so the lock-ordering point still holds and the capacity is reused between calls. AI-assisted (Claude Code).
| 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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Good catch, thanks. Done in d7b81dd: before calling reopen_locked for a run, write_back now checks whether any page in [at, at + pages * kPage) still has kProtected, and skips the call (and its VirtualQuery) when an earlier range already reopened them. reopen_locked only touches pages of ours, so the result is the same. d3d12_title_window_write_back_ranges and the #2468 tile test still pass. AI-assisted (Claude Code).
|
Independent Linux/Wine + NVIDIA check at head
AI-assisted. |
…ck-same-page-ranges ubiquitarianism#2468 (RT-WRITEBACK-TILE-REOPEN) took write_back's per-page held decision before the copies for one range. This branch takes it for all of a tracker's ranges in one call, so the two are now one snapshot over every range, kept in ubiquitarianism#2468's host-heap member held_pages_ (sized before lock_ is taken). Both tests stay: d3d12_title_window_write_back_fault_tile and d3d12_title_window_write_back_ranges; submit_write takes both va2 and base. Also from lixonik's review: a run whose pages an earlier range already reopened (none still kProtected) skips reopen_locked and its VirtualQuery. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Merged Thanks @lixonik for the review and the RTX 5070 run, and @djrolfes for the Linux/Wine check. How this PR relates to #2468 (OreoFR, RT-WRITEBACK-TILE-REOPEN, now merged):
Also from lixonik's notes: a run whose pages an earlier range already reopened skips Re-tested after the merge on RTX 4070 SUPER, driver 621.01, Windows 11. |
|
Merge test for the #2464 campaign on an 8 GB laptop (profile M). The PR head already contains main ec91d69. The numbers were recomputed from the raw logs. AI-assisted (Claude Code, Claude Opus 5.5).
|
|
Addendum to the RUN above: I built this PR's |
|
@OreoFR thanks for the merge run and for checking the test on main. That check matters. The same-page case is not in the default run of The case: 16 disjoint four-byte ranges (words 0, 4, ..., 60) are noted on one guarded page of a write-watched window. The GPU writes all of them. The first range differs from the title's copy and reopens the page. Every later range on that page must still be copied back. Rebuilt and run now (profile M: RTX 4070 SUPER, driver 621.01, Windows 11), with this PR's
On main only the first range gets back: it reopens the page, and each later range then sees the page as written by the title and drops the GPU's data. The 48 words between the ranges stay the title's in both runs, so nothing is copied that should not be. The plain To reproduce on your machine: AI-assisted (Claude Code) |
|
@Tyrannys thanks, that was it. With the flag, on the same machine (RTX 4070 Laptop, NVIDIA 621.40, Windows 11):
So the test fails on main and passes with the PR: the same-page fix is shown here too. This replaces "it also passes on main" in my addendum above. AI-assisted (Claude Code, Claude Opus 5.5). |
lixonik
left a comment
There was a problem hiding this comment.
Re-reviewed d7b81dd43 (merge with main / #2468 + review fixes) and tested on RTX 5070:
- Both earlier comments are cleanly addressed:
held_pages_(leonida::host_heap) is sized beforeAcquireSRWLockExclusive(&lock_)across[lo, hi), and subsequent ranges on already-reopened pages skipreopen_locked/VirtualQuerywhen no page in the run retainskProtected. - Tested on NVIDIA GeForce RTX 5070 (Windows 11, driver 610.88, Vulkan 1.4.341, MSVC 19.44;
VK_LAYER_KHRONOS_validationnot installed locally):ctest -R "^(d3d12_title_window_write_back|d3d12_title_window_write_back_fault|d3d12_title_window_write_back_fault_tile|d3d12_title_window_write_back_ranges|d3d12_placed_write_back)$": 5/5 passed (1.14 s).
Looks good to merge.
AI-assisted (Antigravity, Gemini).
Claimed in the coordination issue as: RT-TITLE-WRITEBACK-SAME-PAGE-RANGES (#1608 (comment)). Found by @73N1X (their finding in #1608, 2026-11-27 11:40).
Depends on: none (builds on #2468, now merged;
upstream/mainmerged in)What and why
DeviceState::write_back_titleshanded each finished range toShadowPages::write_backon its own. When the first range that differs is on a guarded page,write_backreopens that page, andreopen_lockedclearskProtected. Every later disjoint range on that page then fails theheld()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_backtakes all of one tracker's finished ranges in one call (const std::pair<uintptr_t, uintptr_t>*, count, optionalreachedcount). 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_titlessorts 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 fromreached.lock_is taken. A thread that faults on a guarded page waits onlock_inreopen(), and it may hold the heap lock while it waits.How it relates to the other work on this path:
heldonce per page before the copies of one range. This PR now builds on it: the same snapshot, in d3d12: a write-back's later runs in a tile its faulting copy reopened still go back (RT-WRITEBACK-TILE-REOPEN) #2468'sheld_pages_member, covers all of a tracker's ranges, and d3d12: a write-back's later runs in a tile its faulting copy reopened still go back (RT-WRITEBACK-TILE-REOPEN) #2468's tile test stays.note_title_write. This PR does not touch that function and is about disjoint ranges.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.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.ec91d695c: fails 3/3.1/16 four-byte ranges on one guarded page reached it, 48/48 words between them kept the title'sWrite-back, shadow and title-memory tests (
-R "write_back|writeback|shadow|title_memory|title_va|title_window|placed"): 19/19 pass, includingd3d12_title_window_write_back_fault(#2418),d3d12_placed_write_backandd3d12_shadow_keep_refill(_off).Full suite (
ctest -C Release -j 4): 498/502. The four failures:big_compile_gatetimed out under-j 4and passes alone.xs_proxy_mockfailed under-j 4and passes alone.xbox_present(present_planes) andd3d12_set_descriptor_heaps(recorded_commands) also fail on the base with the originald3d12_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
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a)🤖 Generated with Claude Code