GPU: write faults read the bytes they save without the guard lock (revives #3035) #3754
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3754"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by reallyvibe on 2026-11-28T15:09:45Z.
This revives #3035, which was closed without merging, and adds independent validation on native Windows. The optimisation is not mine: the first four commits are #3035's, unchanged (together the same tree as its head
9e0eaa81), applied cleanly ontomain63bcca43, and each one's(cherry picked from …)line records where it came from. They carry a neutral author identity. The fifth commit,5c4bdd6d, is mine and test-only: a deterministic regression test for theOPENEDinvariant (see "Integration risk"). It is the only difference from #3035's head; no production code differs. My part is that test, the re-checking below and the coordination.Claim: https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7238546652
What it changes
watch.rs's write-fault handler saves a page's 64 KiB before its first write, for trackers armed on it. Today it reads those bytes while holdingwatch::GUARDS, so every other write fault and every take waits behind the copy.With this change, when the page is guarded and a tracker would save it, the handler:
OPENEDcount;Otherwise the bytes are read under the lock, as before (one extra 64 KiB read). Only the first armed tracker of the faulting page uses the read-ahead bytes. Alias pages and other trackers of the same page still read under the lock.
Why the kept bytes are the pre-write bytes
open()is the only place a page leavesprotected(the onlyprotected.remove). It bumpsOPENED[page]before callingVirtualProtect(PAGE_READWRITE).Every path that makes a guarded page writable while holding
GUARDSgoes throughopen(): write faults,writing(kernel writes and our own copy-backs), remaps and unmaps (title_memory_remapped), new aliases (title_memory_aliased), all throughchanged→open_guarded, and tracker drops (Tracked::drop).So "guarded at both ends, count unchanged" means no
GUARDSholder opened the page during the read, and title code could not have written it without faulting.Two kinds of writer change a guarded page's bytes without
GUARDS:write_physicalandPageWindow), which are reported only after they land;watch.rsdocuments).Neither ever took
GUARDS, so the lock onmaindid not exclude them either. The read still happens right after the handler first takes the lock, as onmain; only the time between the read and storing the bytes grows, and these writers cannot change bytes already read. Their exposure is unchanged by this PR, not fixed by it.The count is folded onto 4,096 slots. A collision only makes the check fail more often, and then the bytes are read under the lock.
The read goes through
ReadProcessMemory, which fails instead of faulting if the page was freed meanwhile; that case also falls back to the read under the lock.This argument comes from reading the code; the tests below check it under contention but cannot prove it.
Integration risk: pages opened outside
open()The argument above holds on
mainonly because every opening goes throughopen(). neonbyte1 found the case that breaks it on a stack that opens a whole run of guarded pages with oneVirtualProtectand removes them fromprotecteddirectly (comment): a fault's unlocked read can straddle a complete open, copy-back write and re-guard, find the page guarded with an unchangedOPENEDcount, and keep stale bytes. The fix there is to bumpOPENEDfor every page such a path opens. So whichever lands second of this PR and any of these must add those bumps:open_range, guarded pages opened in runs);3a2aa2fc);open_guardedopens runs and removes pages fromprotectedwithoutopen()).opening_guarded_pages_moves_each_ones_opened_count(5c4bdd6d) checks the invariant directly. It opens a guarded three-page run, and the three pages linked to it as another mapping, throughchanged()while holding the guard lock, and asserts that each page's count moved. It uses no threads or timing, and the six pages are consecutive, so each has a count of its own. It covers every opening that goes throughchanged()(write faults,writing, remaps, new aliases), not a path that bypasses it.#3745 (
de7ba3fa) merges with this PR without a textual conflict, and the result drops the bumps. I checked that only as awatch.rssimulation, not a full integration: this branch's tree with the mergedwatch.rs, without #3745's other files.watch::testson the simulationsaved_bytes_are_the_page_as_taken_under_concurrent_faultsalso failed in 4 of 4 runs ("saved other bytes than taken")open_guardedKnown test failures, before and here
shadow::tests::partial_refresh_under_marked_range_updates_saved_bytesfailed once in about 160 full runs of the change (0 in about 170 runs of the base). Its author could not reproduce or explain it. It remains unexplained. Since then it passed in pawrenity's 10 release runs ofshadow::on #3035's head and in one run on the GPU here; neither is enough to rule out a rare failure.mainas well (dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_views), and twodescriptor::hosttests failing now and then on both (views_bound_while_another_slot_gets_its_texture_do_not_wait,a_slot_rewritten_while_its_view_is_prepared_views_the_new_record), which a reviewer traced to the shared test hook #2585 fixes. I did not run those here.Measured before it closed (others' numbers)
mainplus the unmerged PRs that setup needs (listed in #3425, including copy-back in page runs, the case above), not on plainmain:guard_contention(KainanP, comment):GUARDSwait is 0.55× ofmain(quartiles 0.51–0.60), and the refresh's falls from 8.7 to 0.05 ms;guard_lock_contention_benchtables reported a 2–4% loss of fault throughput when writers saturate the lock; fyernald measured a 5–6% gain there instead on another machine, with the 15-writer fault p50 up 15%. I did not rerun that benchmark.OPENEDcomparison replaced bytrue,saved_bytes_are_the_page_as_taken_under_concurrent_faultsfailed 10 of 10 release runs.I have no game files. Everything I ran is synthetic and says nothing about frame time.
Validated by others on this PR
b86f2ed: #3035's two concurrency tests passed 3 of 3 runs each in debug, withVICTORY_WARP=1, and in release;main's code abovemod testsand this PR's tests kept, the same tests also passed 3 of 3 in each mode, as expected:mainnever keeps bytes read without the lock;OPENEDcomparison replaced bytrue):saved_bytes_are_the_page_as_taken_under_concurrent_faultsfailed in 3 of 3 debug and 3 of 3 release runs ("page N saved other bytes than taken");concurrent_writes_and_takes_lose_no_writepassed in all 6;victory-gpulib suite: 231 passed, 2 failed, 2 ignored on this PR, and 229 passed, 2 failed, 1 ignored onmain63bcca4. The same 2 fail on both (dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_views), the NVIDIA failures #3035 reported onmain;5c4bdd6:watch::18 passed, 1 ignored, in 3 of 3 debug and 3 of 3 release runs;Tested here
Windows 11 26H2 (build 26300), Ryzen 7 7445HS, RTX 4050 Laptop (driver 616.64), Rust 1.98.1 MSVC,
CARGO_BUILD_JOBS=1.main63bcca43victory-gpulib tests that need no GPU (debug)guard_lock_contention_bench, plusopening_guarded_pages_moves_each_ones_opened_count; per-test outcomes otherwise identical)shadow::saved-byte and copy-back tests on the GPU (8) †concurrent_writes_and_takes_lose_no_write, release, 20 runs †saved_bytes_are_the_page_as_taken_under_concurrent_faults, release, 20 runs †opening_guarded_pages_moves_each_ones_opened_countalone, 20 runs each: debug, release,--all-featuresdebug and releasewatch::testswith--all-features, debug and releaseOPENEDcomparison replaced bytrue, 10 release runs †cargo clippy -p victory-gpu --all-targets -- -D warnings(default /--all-features)cargo fmt --all --check5c4bdd6d, rechecked forwatch.rsonly, the one file it changes: rustfmt reports nothing in the new test)cargo xtask policy† Run on
b86f2eda, before the test-only commit; that commit changes nothing these runs exercise. Every other row was run on5c4bdd6d.Correction: an earlier version of this description gave the branch's no-GPU count as "143: 142 passed, 1 ignored (the 2 new tests)". #3035 adds 3 tests, one of them an ignored benchmark, so that tree had 144: 142 passed, 2 ignored.
Not run here: the other 91
victory-gputests (GPU and shader tests beyond those 8), and the rest of the workspace. Windows CI run 45754273382 (Tests, Clippy and diagnostics Clippy) succeeded on #3035's head, whose tree is identical tob86f2eda; this PR's own checks cover5c4bdd6d.The tests cannot show whether the read-ahead bytes are ever kept rather than read again: if the check always failed, every test would still pass. The benchmark below is what shows that.
Native-Windows contention benchmark
Same machine. KainanP's
guard_contention(ab96ee44, test-only, on63bcca43) applied to bothmain63bcca43and this branch, plus one identical addition: a write count, printed as writes per second (all writers' writes during the refresh, divided by its duration). Both test binaries were built with the same toolchain and settings (release,--locked). The benchmark'sWAITEDtimes every lock acquisition that findsGUARDStaken, in both builds; this branch's handler takes the lock twice when it reads ahead, and both waits count.Defaults otherwise: 256 MB pool, copies of 32 pages, 1,400 pages dirtied before each refresh, 11 counted rounds per run (each run's figure is its median). One warm-up run of each build per configuration, then 5 measured runs alternating
mainand branch (order flipped each repetition, 10 s pause before each run); nothing armed was extended to 10 runs because 5 left it ambiguous. Heavy background apps were closed; on AC power, Balanced plan.Medians over runs, with the observed range. "Writer wait" is the writers' total wait for
GUARDSper round; "refresh wait" is the refresh's.mainReading:
mainon writes/s, writer wait and refresh wait. The writers' wait is 0.64× ofmain's here (KainanP measured 0.55× on their machine).Limits: one laptop, synthetic workload, 5 runs per configuration (10 for nothing armed), no significance tests, no thermal readings, a few light background apps still running.
Reproduce, and every measured run
The write counter, applied to
watch.rson both sides after KainanP's commit:Steps (Git Bash; the same on both sides):
Each run's medians, from its
guard_contention:line, in the order each pair ran:mainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainmainInteractions
Changed::save. Whichever lands second rebases.page_bytes()could later use #3262's unzeroed read or #3720's pooled pages.open(): see "Integration risk" above.AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-28T17:04:33Z.
NVIDIA row for this PR: its tests are CPU-only (
VirtualAlloc,VirtualProtect, the write-fault handler and threads; no D3D12 device). So the RTX 3070 adds nothing to them, and an NVIDIA run only adds thevictory-gpusuite counts. Both new tests pass. They also pass with main's code, as they should: main reads the bytes under the lock, so it is correct, only slower. Pawrenity's mutant (theOPENEDcheck replaced bytrue) does fail the new test here, in 6 of 6 runs.RTX 3070 (desktop), driver 616.56, Windows 11 Pro 10.0.26100. Head
b86f2edon main63bcca4: four commits, onlycrates/victory-gpu/src/watch.rs(388 insertions, 19 deletions), clean.cargo test --locked -p victory-gpu --lib -- watch::tests::concurrent_writes_and_takes_lose_no_write watch::tests::saved_bytes_are_the_page_as_taken_under_concurrent_faults --nocapture: 2 passed, 0 failed, 0 ignored, in each of these sets of runs:VICTORY_WARP=1(no difference expected, since nothing here opens a device);--release, where the new test does 2,000 rounds instead of 200.Negative control: main's
watch.rsabovemod tests(its lines 1-953) with the PR's whole test module kept.git diff --stat HEAD: 1 file, 19 insertions, 75 deletions.git diff --stat 63bcca4: 313 insertions, all insidemod tests(2 hunks). Result: 2 passed, 0 failed, in 3 of 3 debug, 3 of 3VICTORY_WARP=1and 3 of 3 release runs. The control passes on this machine. That is expected: main never keeps bytes read without the lock, so it never keeps stale bytes. These tests check that the change keeps main's result. They do not show that it is faster, which the description says itself.Mutant control (the description's, from pawrenity): line 964's
opened(page).load(Ordering::Relaxed) == beforereplaced bytrue, then restored.saved_bytes_are_the_page_as_taken_under_concurrent_faultsfailed in 3 of 3 release and 3 of 3 debug runs, withpage N saved other bytes than taken(pages 7, 9, 10 in release; 7, 9, 11 in debug).concurrent_writes_and_takes_lose_no_writepassed in all 6. So on this machine the new test catches the race that the count guards.cargo test --locked -p victory-gpu --lib:63bcca4: 229 passed, 2 failed, 1 ignored.The PR adds the 2 tests above and the benchmark
guard_lock_contention_bench, which is ignored. The same 2 fail on both, as on main on this card before:descriptor::host::tests::dynamic_slots_are_held_until_releasedandmemory::tests::lists_share_a_table_and_copies_keep_their_views. These are the two that the description, quoting #3035, says fail on NVIDIA on main too.The PR does not touch
victory-shader, so I did not run its lib suite.Nothing NVIDIA-specific: the code and its tests are about CPU page guards, and the GPU plays no part.
Not tested:
guard_lock_contention_bench, KainanP'sguard_contention), so nothing here on speed;shadow::tests::partial_refresh_under_marked_range_updates_saved_bytesfailure (it passed in the 2 suite runs above);AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this report.
Update for the new head
5c4bdd6(one commit, a test only:opening_guarded_pages_moves_each_ones_opened_count, +50 inwatch.rs):cargo test --locked -p victory-gpu --lib -- watch::gives 18 passed, 0 failed, 1 ignored in 3 of 3 debug runs and 3 of 3--releaseruns on the same machine.Imported from Marvisak/VIctory. Originally posted by reallyvibe on 2026-11-28T19:12:53Z.
Thanks @upipibi for the RTX 3070 run, including the
maincontrol, the mutant control and the recheck of the new head5c4bdd6. I've added your results to the description under "Validated by others on this PR". Your whole-suite counts also confirm the corrected count there: #3035 adds three tests, one of them the ignored benchmark.AI-assisted (Claude Code).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.