Repository navigation
GPU: avoid heap allocations and batch unprotects in watch fault hot path - #3343
danielibabet wants to merge 2 commits into
Conversation
Dukestar69
left a comment
There was a problem hiding this comment.
Tested on Windows against origin/main (63bcca4). cargo test -p victory-gpu --lib watch:: -- --test-threads=1 (15/15), clippy, and xtask policy pass, but there is a bug in the batched open_guarded path:
- In
open_guarded(crates/victory-gpu/src/watch.rs), the batchedVirtualProtect((start * PAGE) as *const _, (end - start) * PAGE, PAGE_READWRITE, &mut old)ignores its return value after removing all pages instart..endfromg.protected. - On Windows,
VirtualProtectfails atomically (0) if[start, end)spans two separateVirtualAlloc/MapViewOfFileallocations (differentAllocationBase) or includes a page that was unmapped. When that happens, none of the still-mapped pages in the run are restored toPAGE_READWRITE, yet they have already been removed fromg.protected, so the next write to any of them will trigger an unhandled access violation instead of being handled by the watch fault handler. - Compare
protect_notingright above it, which checks theVirtualProtectreturn value and falls back to per-pageVirtualProtecton failure. CheckingVirtualProtect(...) == 0inopen_guardedand falling back to per-pageVirtualProtect(oropen) forstart..endfixes this cleanly. - Minor:
first + 1 == lastcan overflow iffirst == usize::MAX(e.g.first.checked_add(1) == Some(last)is safer).
Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
|
Good catch @Dukestar69! Fixed both in
Re-ran |
fuzeofficlal
left a comment
There was a problem hiding this comment.
Verified locally merged onto origin/main (63bcca43) at PR head 65d26e3c (Windows 11 x64, Ryzen 9 9950X, NVIDIA RTX 5090 + Microsoft WARP, VICTORY_D3D12_DEBUG=1):
crates/victory-gpu/src/watch.rs:changed(L717-L722): single-page non-aliased fault fast path avoids heap allocations and range scans underGUARDSwhile preservingmark_changed->openordering and return semantics;first.checked_add(1) == Some(last)preventsusize::MAXoverflow.open_guarded(L746-L787): batches contiguous guarded runs into singleVirtualProtect(..., PAGE_READWRITE)calls, checks!= 0before removingstart..endfromg.protected, and falls back to per-pageopen(g, page)when a batched call fails (e.g. across allocation boundaries or decommitted pages).aliases(L290-L293) andchanged(L724-L728): skipalias_oftraversal when empty.
cargo xtask policy: 191 files ok.cargo clippy --locked -p victory-gpu --all-targets -- -D warnings(default and--all-features): 0 warnings.cargo test --locked -p victory-gpu --features diagnostics --lib -- watch::: 15/15 passed on both RTX 5090 and WARP.- Extra adversarial synthetic probe (not committed):
- Decommitted the middle page (
MEM_DECOMMIT) of a 6-page guarded run beforewriting(p, 6 * PAGE), triggering the batchedVirtualProtectfailure path and confirming per-pageopenremoved all 6 entries fromg.protectedand restoredPAGE_READWRITEon all 5 remaining pages without faulting on subsequent writes. - Verified non-contiguous runs (
[0, 1]and[4, 5]),usize::MAXboundary calls, and an 8-thread concurrent write-fault stress test across 64 guarded pages x 20 rounds (1,280 faults in 8.67 ms, 100% captured).
- Decommitted the middle page (
- Also verified a clean co-merge with
#3341(both touchwatch.rs): 0 clippy warnings and 26/26watch::+shadow::tests passing.
Synthetic tests only; looks ready to merge.
|
Measured in game on an RTX 5090 (driver 620.73.04), Linux, GE-Proton11-14 Wine with its vkd3d-proton, GTA VI's opening prison yard, warm cache; base is
About 1 ms off copy-back, within noise otherwise. For context, our counters put the copy-back of marked pages at ~51 ms/frame here, of which page writes and guard lease/protect rounds are ~94% of the thread time (2,829 lease rounds per frame) and the title reads ~3.5%; the single-page fast path doesn't reach the multi-page runs that dominate in this scene. AI-assisted (Claude); measured on my machine, no game data shared. |
|
Contention numbers for this PR from a shared benchmark (for #3425). Method. The benchmark
Reading. With 4 writers the writers' time in their writes and their wait for Correctness. AI use: Claude Code (Claude Opus 5.5). |
Summary
watch::changedfor single-page write faults without aliases (which covers nearly all vectored exception handler faults inon_write), skipping bothVecallocations andalias_of/protectedrange scans under the globalGUARDSmutex.alias_ofiteration inwatch::aliaseswhenalias_ofis empty, and skipalias_ofrange collection inwatch::changedwhenalias_ofis empty.watch::open_guardedinto singleVirtualProtect(..., PAGE_READWRITE)calls instead of one syscall per page, with a single-page fast path.Verification
cargo test -p victory-gpu --locked -- --test-threads=1 --skip dynamic_slots_are_held_until_released --skip lists_share_a_table_and_copies_keep_their_views:229 passed; 0 failed; 1 ignored; 2 filtered outcargo clippy --workspace --all-targets --all-features --locked -- -D warnings: 0 warningscargo run -p xtask --locked -- policy:asset policy: 191 files ok