Repository navigation
GPU: write faults read the bytes they save without the guard lock - #3035
liandraraiane wants to merge 4 commits into
Conversation
kotekompotek
left a comment
There was a problem hiding this comment.
LGTM — verified the race argument: a protected page can't be written without faulting, every unguard path goes through open() which bumps OPENED, so still-protected + unchanged counter on re-lock proves the read was pre-write. take_if hands bytes to one tracker, others fall back to under-lock reads. Lock order g→changed→saved consistent throughout. The 12-writer stress test is a real check.
|
Wine/NVIDIA cross-check of the two new concurrency tests — both pass on a different host class. Head
Scope: correctness of the lock-release scheme on this host only. I did not run the Local logs: |
A benchmark of the guard lock under contention (issue Marvisak#400 puts about 940 write faults a frame at about 450 us each, mostly waiting for the lock): writer threads fault on a hot set of guarded pages while a submission thread arms and takes every tracker in rounds. Prints faults a second, fault latency (median, 99th percentile) and a take round's median per writer count. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
VERIFY-3035: verified Tests
Benchmark (
At 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)
AI-assisted (Claude Code). |
fyernald
left a comment
There was a problem hiding this comment.
Ran 9e0eaa8 on Windows 10 22H2 with a Ryzen 7 5700X (8 cores / 16 threads, close to your 5700G), release build. For the base, I copied your new tests and the benchmark onto main 63bcca4's watch.rs without the change.
Correctness, 20 runs each:
| base | this PR | |
|---|---|---|
concurrent_writes_and_takes_lose_no_write |
20/20 | 20/20 |
saved_bytes_are_the_page_as_taken_under_concurrent_faults |
20/20 | 20/20 |
watch:: |
17 passed | 17 passed |
guard_lock_contention_bench, 7 runs per build, alternated, medians:
GUARD_BENCH_SPIN_US=50
| writers | faults/s | fault p99 µs | take round µs | 8-page writing p99 µs |
|---|---|---|---|---|
| 4 | 45,521 → 48,053 | 270.4 → 266.8 | 464.9 → 371.4 | 475.2 → 382.1 |
| 8 | 69,687 → 72,738 | 465.2 → 455.2 | 565.3 → 469.2 | 801.3 → 662.5 |
| 15 | 81,451 → 86,496 | 694.1 → 619.3 | 748.9 → 557.3 | 1,181.1 → 905.5 |
Saturated (GUARD_BENCH_SPIN_US=0)
| writers | faults/s | fault p50 µs | fault p99 µs | 8-page writing p99 µs |
|---|---|---|---|---|
| 4 | 86,946 → 91,901 | 38.0 → 36.6 | 405.7 → 381.1 | 818.2 → 620.3 |
| 8 | 89,959 → 95,125 | 53.1 → 51.0 | 586.2 → 522.9 | 1,147.0 → 762.7 |
| 15 | 93,839 → 98,881 | 62.8 → 72.4 | 937.3 → 812.7 | 1,491.4 → 1,144.3 |
One difference from your numbers: saturated throughput goes up 5–6% here instead of down 2–4%, though the 15-writer fault p50 is up 15%. Faults are slower here than in your table at saturation (38 µs p50 at 4 writers vs your 22 µs), so the second lock taking may cost less than the read it moves out of the lock on this machine.
On the code: open is the only place a page leaves protected, and it bumps the count. Copy-backs and kernel writes reach it through writing, and remaps through changed → open_guarded. So bytes kept after the recheck can't include a write, and I found no path that changes a guarded page's bytes without opening it.
The two descriptor::host flakes you list (views_bound_while_another_slot_gets_its_texture_do_not_wait, a_slot_rewritten_while_its_view_is_prepared_views_the_new_record) are the ones #2585 fixes. It's test-only: their hook was one global shared by tests running in parallel.
Twelve writer threads fault on guarded pages while a taker arms, takes and copies every tracker (and reports pages taken by copy-back): at the end every page whose copy differs from title memory must still be changed, and no write may fault past the handler. Against a version that made pages read-only outside the guard lock with a race between a stale fault and the take, it failed 2 of 10 release runs (the process died); main passes 20 of 20. The contention benchmark gains GUARD_BENCH_SPIN_US, the time each writer works between writes (closer to a title's fault rate than writing back to back). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fuzeofficlal
left a comment
There was a problem hiding this comment.
Static review of 9e0eaa81 (crates/victory-gpu/src/watch.rs).
The lock-splitting design looks sound:
- Capturing
opened = OPENED.load(Acquire)before droppingGUARDSand recheckingg.protected.contains(&page)+OPENEDafter reading the page (while keeping the page read-only duringread_into) prevents ABA across reopens, aliases, and remaps —open()bumpsOPENEDunderGUARDSbeforeVirtualProtectrestores write access, so any concurrent unprotect/write forces a retry or sees the page already open. - Only
holderswith an armed tracker trigger a save, andsavedis populated after re-acquiringGUARDSonly if the page stayed protected.
Two minor non-blocking notes:
- In
Changed::would_save,let saved = ...shadows the earlierMutexGuardrather than explicitly droppingself.0before lockingself.1(the lock orderchanged.0 -> changed.1is still consistent with the rest ofwatch.rs, so no deadlock, just slightly subtle). - In
Changed::save,read.take_if(...)moves the snapshot buffer into the first armed tracker; when multiple armed trackers share the same page, subsequent trackers clone as intended.
A thread opens 8-page ranges with `writing` every 200 us, as a file read or DirectStorage does, and the benchmark prints the median and 99th percentile of each (the opening and the marking after). Issue Marvisak#400 puts `writing` first among the guard lock's holders (174 ms a frame). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Automated triage for the maintainers. In CI: Generated by Claude Code |
|
Verified Tests
Live Title Verification (RX 9070 XT)
Merge Note with
|
Dukestar69
left a comment
There was a problem hiding this comment.
Tested locally merged onto origin/main (63bcca4) on Windows 11 x64:
- Diff review: Reading guarded 64 KiB page bytes outside the guard lock and validating against the per-page
OPENEDgeneration counter + guarded state once back under the lock cleanly avoids holding the global guard lock across 64 KiB copies on write faults while falling back safely if the page was opened meanwhile. cargo test -p victory-gpu watch::tests -- --test-threads=1: all tests passed.cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.
Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
A write fault on a page a tracker is armed for saves the page's 64 KiB before opening it, and did so (an allocation and a kernel copy) while holding the guard lock that every title thread's write fault, every take and every kernel write wait for (issue Marvisak#400: about 450 us a fault, the lock held about 600 ms a world frame). The fault now reads the bytes with the lock released and keeps them only if the page stayed guarded meanwhile: a count per page (folded onto 4,096) is bumped under the lock whenever a page is opened, the only way a guarded page's bytes can change, and checked when the lock is taken again. Otherwise the bytes are read under the lock, as before. The bytes go to the page's own save only, not to its aliases'. A new test checks the oracle under contention: whatever a tracker saved of a page equals the copy taken of it; with the opened-count check removed it fails 10 of 10 runs. The concurrency tests now stop their writers when an assertion fails instead of hanging. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SLrhrPRF9pqyeEQtcgYu3x
…y save without the guard lock
…y save without the guard lock
Problem
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