GPU: avoid heap allocations and batch unprotects in watch fault hot path #3343

Open
github-bot wants to merge 2 commits from github-bot/gh-3343 into main AGit
Owner

@⁠danielibabet on GitHub · 2026-11-26 19:19 UTC · original
danielibabet:pr/watch-zero-alloc-faults → main · +51 −4 · 1 files · 2 commits · open

Summary

  • Add a zero-allocation fast path in watch::changed for single-page write faults without aliases (which covers nearly all vectored exception handler faults in on_write), skipping both Vec allocations and alias_of / protected range scans under the global GUARDS mutex.
  • Skip alias_of iteration in watch::aliases when alias_of is empty, and skip alias_of range collection in watch::changed when alias_of is empty.
  • Batch contiguous guarded pages in watch::open_guarded into single VirtualProtect(..., 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 out
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: 0 warnings
  • cargo run -p xtask --locked -- policy: asset policy: 191 files ok
**@⁠danielibabet** on GitHub · 2026-11-26 19:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/3343) `danielibabet:pr/watch-zero-alloc-faults` → `main` · +51 −4 · 1 files · 2 commits · open ### Summary - Add a zero-allocation fast path in `watch::changed` for single-page write faults without aliases (which covers nearly all vectored exception handler faults in `on_write`), skipping both `Vec` allocations and `alias_of` / `protected` range scans under the global `GUARDS` mutex. - Skip `alias_of` iteration in `watch::aliases` when `alias_of` is empty, and skip `alias_of` range collection in `watch::changed` when `alias_of` is empty. - Batch contiguous guarded pages in `watch::open_guarded` into single `VirtualProtect(..., 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 out` - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: 0 warnings - `cargo run -p xtask --locked -- policy`: `asset policy: 191 files ok`
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 19:37:55 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 19:28 UTC · original · reviewed

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 batched VirtualProtect((start * PAGE) as *const _, (end - start) * PAGE, PAGE_READWRITE, &mut old) ignores its return value after removing all pages in start..end from g.protected.
  • On Windows, VirtualProtect fails atomically (0) if [start, end) spans two separate VirtualAlloc / MapViewOfFile allocations (different AllocationBase) or includes a page that was unmapped. When that happens, none of the still-mapped pages in the run are restored to PAGE_READWRITE, yet they have already been removed from g.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_noting right above it, which checks the VirtualProtect return value and falls back to per-page VirtualProtect on failure. Checking VirtualProtect(...) == 0 in open_guarded and falling back to per-page VirtualProtect (or open) for start..end fixes this cleanly.
  • Minor: first + 1 == last can overflow if first == usize::MAX (e.g. first.checked_add(1) == Some(last) is safer).

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 19:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/3343#pullrequestreview-6079540028) · **reviewed** 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 batched `VirtualProtect((start * PAGE) as *const _, (end - start) * PAGE, PAGE_READWRITE, &mut old)` ignores its return value after removing all pages in `start..end` from `g.protected`. - On Windows, `VirtualProtect` fails atomically (`0`) if `[start, end)` spans two separate `VirtualAlloc` / `MapViewOfFile` allocations (different `AllocationBase`) or includes a page that was unmapped. When that happens, none of the still-mapped pages in the run are restored to `PAGE_READWRITE`, yet they have already been removed from `g.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_noting` right above it, which checks the `VirtualProtect` return value and falls back to per-page `VirtualProtect` on failure. Checking `VirtualProtect(...) == 0` in `open_guarded` and falling back to per-page `VirtualProtect` (or `open`) for `start..end` fixes this cleanly. - Minor: `first + 1 == last` can overflow if `first == usize::MAX` (e.g. `first.checked_add(1) == Some(last)` is safer). Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠danielibabet on GitHub · 2026-11-26 19:35 UTC · original

Good catch @⁠Dukestar69! Fixed both in crates/victory-gpu/src/watch.rs:

  • Check VirtualProtect(...) != 0 in open_guarded before removing start..end from g.protected, and fall back to per-page open(g, page) when the batched call fails (e.g. across allocation boundaries or unmapped pages).
  • Use first.checked_add(1) == Some(last) in changed and open_guarded to avoid usize::MAX overflow.

Re-ran victory-gpu tests (229 passed, 0 failed; 2 skipped), strict clippy (--all-targets --all-features -- -D warnings), and xtask policy (191 files ok).

**@⁠danielibabet** on GitHub · 2026-11-26 19:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/3343#issuecomment-7201747894) Good catch @⁠Dukestar69! Fixed both in `crates/victory-gpu/src/watch.rs`: - Check `VirtualProtect(...) != 0` in `open_guarded` before removing `start..end` from `g.protected`, and fall back to per-page `open(g, page)` when the batched call fails (e.g. across allocation boundaries or unmapped pages). - Use `first.checked_add(1) == Some(last)` in `changed` and `open_guarded` to avoid `usize::MAX` overflow. Re-ran `victory-gpu` tests (229 passed, 0 failed; 2 skipped), strict clippy (`--all-targets --all-features -- -D warnings`), and `xtask policy` (191 files ok).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 20:24 UTC · original · approved these changes

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 under GUARDS while preserving mark_changed -> open ordering and return semantics; first.checked_add(1) == Some(last) prevents usize::MAX overflow.
    • open_guarded (L746-L787): batches contiguous guarded runs into single VirtualProtect(..., PAGE_READWRITE) calls, checks != 0 before removing start..end from g.protected, and falls back to per-page open(g, page) when a batched call fails (e.g. across allocation boundaries or decommitted pages).
    • aliases (L290-L293) and changed (L724-L728): skip alias_of traversal 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 before writing(p, 6 * PAGE), triggering the batched VirtualProtect failure path and confirming per-page open removed all 6 entries from g.protected and restored PAGE_READWRITE on all 5 remaining pages without faulting on subsequent writes.
    • Verified non-contiguous runs ([0, 1] and [4, 5]), usize::MAX boundary 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).
  • Also verified a clean co-merge with #3341 (both touch watch.rs): 0 clippy warnings and 26/26 watch:: + shadow:: tests passing.

Synthetic tests only; looks ready to merge.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 20:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/3343#pullrequestreview-6080118913) · **approved these changes** 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 under `GUARDS` while preserving `mark_changed` -> `open` ordering and return semantics; `first.checked_add(1) == Some(last)` prevents `usize::MAX` overflow. - `open_guarded` (`L746-L787`): batches contiguous guarded runs into single `VirtualProtect(..., PAGE_READWRITE)` calls, checks `!= 0` before removing `start..end` from `g.protected`, and falls back to per-page `open(g, page)` when a batched call fails (e.g. across allocation boundaries or decommitted pages). - `aliases` (`L290-L293`) and `changed` (`L724-L728`): skip `alias_of` traversal 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 before `writing(p, 6 * PAGE)`, triggering the batched `VirtualProtect` failure path and confirming per-page `open` removed all 6 entries from `g.protected` and restored `PAGE_READWRITE` on all 5 remaining pages without faulting on subsequent writes. - Verified non-contiguous runs (`[0, 1]` and `[4, 5]`), `usize::MAX` boundary 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). - Also verified a clean co-merge with `#3341` (both touch `watch.rs`): 0 clippy warnings and 26/26 `watch::` + `shadow::` tests passing. Synthetic tests only; looks ready to merge.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3343/head:github-bot/gh-3343'
git switch 'github-bot/gh-3343'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3343'
git switch 'github-bot/gh-3343'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3343'
git switch 'github-bot/gh-3343'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3343'
git switch 'main'
git merge --squash 'github-bot/gh-3343'
git switch 'main'
git merge --ff-only 'github-bot/gh-3343'
git switch 'main'
git merge 'github-bot/gh-3343'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3343
No description provided.