GPU: write faults read the bytes they save without the guard lock #3035
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3035
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3035"
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?
@liandraraiane on GitHub · 2026-11-26 04:17 UTC · original
liandraraiane:perf/save-outside-guard-lock→main· +388 −19 · 1 files · 4 commits · openProblem
Issue #400 measures the guard lock of
crates/victory-gpu/src/watch.rsheld about 600 ms a world frame on an RTX 2080, with write faults taking about 450 µs each, mostly waiting for it. When a write faults on a page a tracker is armed for, the handler saves the page's 64 KiB before opening it, and it did that (a 64 KiB allocation and aReadProcessMemory) while holding the guard lock, so every other title thread's fault, every take and every kernel write (writing) queued behind the copy.Change
The fault reads the bytes it will save with the lock released, then takes the lock again and keeps them only if the page stayed guarded meanwhile. A guarded page's bytes can only change once it is opened, and
openis the only place that removes a page from the guard set, so a count per page (folded onto 4,096 counters) is bumped there under the lock and compared when the lock is taken again. If it moved, or the page is no longer guarded, the bytes are read under the lock exactly as before. The bytes read ahead go only to the faulting page's own save, never to an alias's. No protection call moves out of the lock (an earlier attempt that made pages read-only outside it was slower: the protection calls cost more while writers keep running).Benchmark
guard_lock_contention_bench(new,#[ignore]d): writer threads fault on 256 guarded pages, a submission thread arms every other tracker and takes all of them in rounds, and a kernel-writer thread opens 8-page ranges withwritingevery 200 µs. Ryzen 7 5700G (16 threads), Windows 11, release. Base = the same benchmark without the change. 15 runs alternated, median with the interquartile range.Writers working 50 µs between writes (
GUARD_BENCH_SPIN_US=50; a title faults far less often than this):writingp99 µsWriters writing back to back (saturated):
writingp99 µsIn the saturated case a fault that saves takes the lock twice, and throughput drops 2–4 %; the tail of kernel writes still improves.
Tests
saved_bytes_are_the_page_as_taken_under_concurrent_faults: 12 writers fault on armed pages while the main thread takes, copies and arms in rounds; whatever a tracker saved of a page must equal the copy taken of it. With the opened-count check removed it fails 10/10 release runs; with this change it passes 20/20.concurrent_writes_and_takes_lose_no_write: no write lost between a take's guard and its copy, no stray fault (it caught a race in the rejected attempt above, 2/10 runs); 20/20 here.cargo test -p victory-gpu --lib: all pass except the two tests that fail on NVIDIA onmainas well (dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_views, the capped view heap).cargo clippy --workspace --all-targets -- -D warnings: clean.rustfmt --checkreports the same 39 differences inwatch.rsas onmain, none in the new code.victory-gpuruns each, interleaved:descriptor::hostviews_bound_while_another_slot_gets_its_texture_do_not_wait(base 4, this 7) anda_slot_rewritten_while_its_view_is_prepared_views_the_new_record(2, 4) fail now and then on both, and on unmodifiedmaintoo (1 of 25 each).shadow::tests::partial_refresh_under_marked_range_updates_saved_bytesfailed once in about 160 full runs of this change (0 in 100 interleaved runs here, 0 in about 170 of the base); I could not reproduce it or find a path that explains it, so I am reporting it.Not tested
The title (no game data here). AMD GPUs. The benchmark is synthetic: the fault rate, page counts and arming pattern are made up, and a title's
VirtualProtectcalls cost much more than here.AI use
Claude Code (Claude Opus 5.5) wrote the change, tests and benchmark, ran every measurement above on this machine and drafted this description; I reviewed and approved it.
🤖 Generated with Claude Code
https://claude.ai/code/session_01SLrhrPRF9pqyeEQtcgYu3x
@pickles976 on GitHub · 2026-11-26 04:45 UTC · original
Wine/NVIDIA cross-check of the two new concurrency tests — both pass on a different host class. Head
9e0eaa811e7f1c2924eab406941705434e593d43, Linux + Wine 11.0 (vkd3d-proton 3.2.0, NVIDIA 590.48.1), RTX 4060 Laptop GPU, i.e. neither the 5700G/Windows nor the 2080 that motivated #400. Wrapper-injected--locked --target x86_64-pc-windows-msvc, one test thread, freshCompiling victory-gpu .../VIctory-pr3035/...provenance.watch::tests::saved_bytes_are_the_page_as_taken_under_concurrent_faults— 1/1 pass (0.31 s): the 12-writer take/copy/arm rounds saved exactly the taken bytes on this host too.watch::tests::concurrent_writes_and_takes_lose_no_write— 1/1 pass (1.19 s): no write lost between a take's guard and its copy, no stray fault.Scope: correctness of the lock-release scheme on this host only. I did not run the
#[ignore]dguard_lock_contention_bench, so no throughput/latency numbers from here — the author's 5700G tables stand unchallenged, and the saturated-case 2–4 % fault-throughput trade-off remains their measurement. Wine'sVirtualProtect/exception path differs from Windows', so a pass here is cross-platform robustness evidence, not a preview of the Windows win. No title run.Local logs:
.compat-runtime/logs/verify-pr3035-wine-saved-bytes.log,verify-pr3035-wine-no-lost-write.log. AI-assisted verification and report (OpenAI Codex).@pawrenity on GitHub · 2026-11-26 05:04 UTC · original
VERIFY-3035: verified
9e0eaa8on NVIDIA GeForce RTX 4070, Windows 11 (MSVC). No defects found.Tests
cargo test -p victory-gpu --libat head: 231 passed, 2 ignored, 2 failed. With--features diagnostics: 246 passed, 2 failed. The two failures aredynamic_slots_are_held_until_releasedandlists_share_a_table_and_copies_keep_their_views, which also fail on main on this card.watch::in release (both new tests included): 20/20 passes.shadow::in release: 10/10 passes, includingpartial_refresh_under_marked_range_updates_saved_bytes.opened(page) == beforecomparison in the handler replaced bytrue,saved_bytes_are_the_page_as_taken_under_concurrent_faultsfails 10/10 release runs (page 9 saved other bytes than taken). The test does catch the race the counter guards against.63bcca4, pass 5/5 in release. They check properties that main already holds, so they are not regression tests for a defect on main.cargo clippy -p victory-gpu --all-targets -- -D warningsis clean, with and without--features diagnostics.63bcca4is clean.Benchmark (
guard_lock_contention_bench, release,GUARD_BENCH_SPIN_US=50 GUARD_BENCH_MS=1000, 3 runs each alternating base/PR; few samples, so indicative only):writingp99 usAt 1, 2 and 4 writers the runs overlap. This agrees in direction with the PR's 5700G tables. I did not run the saturated (spin 0) case.
Review (reading only)
would_savekeepsc.0locked while it takesc.1, because the shadowed guard is not dropped until the end of the function. It is called with the guard lock held, so its order is guards ->c.0->c.1.take_splitandTracked::takenuse the same order (guards ->c.0->Changed::taken'sc.1). The other paths (save,arm,unarm,disarm,saved,refreshed) take onlyc.1, and none takes the guard lock orc.0while holding it. I found no inversion.open()(watch.rs:480) is the only place that removes fromprotected, and it is the onlyPAGE_READWRITEcall.Tracked::drop,writingand its drop,title_memory_remapped,title_memory_aliasedand the handler all reach it throughopen. Every way a page stops being guarded therefore bumpsOPENED. Both the bump and the loads happen under the guard lock, soRelaxedis enough. When the page is guarded at both ends and the count is unchanged, the read lies inside one continuously read-only interval. Any holder that has the page un-changed at re-lock took it inside that interval, so giving the read-ahead to the first saving holder (which may not be the onewould_savematched) is still correct.refreshedrunning between the read and the insert (it finds no saved entry, as on main, and its bytes equal memory); the tracker being dropped meanwhile (changed()readsholdersagain, and an unused read is freed); the page opened and guarded again (the count moves, so it falls back to the read under the lock).writing(for example through an alias mapping that is not guarded) is not seen. The read-ahead window does not make this worse, because main read at fault time with the same exposure.AI-assisted (Claude Code).
@Marvisak on GitHub · 2026-11-26 06:18 UTC · original
Automated triage for the maintainers.
In
crates/victory-gpu/src/watch.rs, the write-fault handler now reads the 64 KiB it is about to save without holding the guard lock. It keeps those bytes only if, back under the lock, the page is still guarded and itsOPENEDcount has not moved. Otherwise it reads under the lock as before (Changed::save,page_bytes). I checked the invariant this relies on: at this head,open()is the only place that removes a page fromprotectedand the onlyPAGE_READWRITEcall, and it bumps the count under the guard lock. So bytes read while the page stayed guarded cannot include a write. The bytes read ahead are used only for the faulting page's own save. Alias pages and other holders still read under the lock. All other callers ofchangedpass&mut None, so their behaviour does not change. This is a host-side locking change with measured benefit (#400 and the benchmark tables), not a change to emulated behaviour. The new tests pass onmaintoo, so they are guards against regressions rather than reproductions of a bug. An independent run in this thread showed that the concurrent-faults test fails when the count check is removed.CI:
checkpassing.Generated by Claude Code
@MeMako on GitHub · 2026-11-26 06:28 UTC · original
Verified
#3035(9e0eaa8) on Windows 11 (10.0.26200, Ryzen 9 7950X, RX 9070 XT, driver32.0.32029.2010, 32 GB RAM).Tests
cargo clippy -p victory-gpu --all-targets --locked -- -D warnings: clean.victory-gpu: 272/272 passed (includingsaved_bytes_are_the_page_as_taken_under_concurrent_faultsandconcurrent_writes_and_takes_lose_no_write, with 0 lost writes in the stress tests).Live Title Verification (RX 9070 XT)
main63bcca4+#2051+#2472+#2918+#1010+#2130with VRAM upload flags off +#3055+#2914), adding#2985+#2849+#3035together in the same live run (n=54world frames /n=38heavy frames vs.n=51/n=38baseline).6,186draws vs.5,290draws in baseline):prep → deferred: 92.7 → 71.8 ms (-20.9 ms / -22.5%)copy_back → marks: 71.6 → 63.5 ms (27.2 → 25.8 µs/page)copy_back: 99.2 → 75.5 ms (-23.7 ms / -23.9%)watch.rstimers reflect#3035and#2849combined;#2985affects texture detile/upload separately.)#2985+#2849+#3035together): steady-tail frame time dropped 453.3 → 423.6 ms (2.20 → 2.40 fps), even with +17% more draws.Merge Note with
#2849୦#2849(batchedopen_range), placing#3035'sopened(page).fetch_add(1, Ordering::Relaxed)insideopen_range'sif g.protected.remove(&page)loop keeps every page open counted underGUARDS.#2918,#3035's new testsaved_bytes_are_the_page_as_taken_under_concurrent_faultsjust adaptst.saved(page)toFinished::Saved(bytes) = t.finish(page).Hardware runs were performed by me on my 7950X / RX 9070 XT; comment drafted with Antigravity.
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.