GPU: scan all sub-regions before marking a 64 KiB page unmapped #3125

Open
github-bot wants to merge 1 commit from github-bot/gh-3125 into main AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 09:12 UTC · original
fuzeofficlal:fix/gpu-watch-partial-commit-pages → main · +179 −5 · 2 files · 1 commits · open

Summary

In crates/victory-gpu/src/watch.rs, protect_noting checks whether a 64 KiB page (PAGE) that VirtualProtect(..., PAGE_READONLY) failed on is unmapped by calling VirtualQuery once at page * PAGE and testing n == 0 || info.State != MEM_COMMIT.

Because Windows commits memory at 4 KiB granularity while watch::PAGE is 64 KiB, a 64 KiB block whose leading 4 KiB OS page is MEM_RESERVE and whose remaining 4 KiB sub-pages are MEM_COMMIT returns info.State == MEM_RESERVE with info.RegionSize == 4096. unmapped(page) then returns true, which causes:

  1. take_split to classify the entire 64 KiB page as unmapped so Shadow::update_tracked zero-fills all 64 KiB in the host shadow buffer, discarding live committed title data (even though host::copy_mapped already supports 4 KiB fallback copies for partially committed blocks).
  2. protect_noting to include the page in taken, removing it from changed (and marking the tracker clean) even though the committed sub-pages could not be guarded, so subsequent CPU writes to those sub-pages are silently missed.
  3. guard_aliases, Tracked::taken, and title_memory_aliased (which all go through protect) to treat partially committed pages as guarded.

Changes

  • crates/victory-gpu/src/watch.rs:
    • Update unmapped inside protect_noting to walk all VirtualQuery sub-regions across [page * PAGE, (page + 1) * PAGE) and return false as soon as any sub-region has info.State == MEM_COMMIT. Fully unmapped/reserved 64 KiB pages (RegionSize >= PAGE) still finish in a single VirtualQuery call.
    • Add watch::tests::partially_committed_pages_are_not_taken_as_unmapped covering leading-reserved/tail-committed pages, Tracked::taken, and transitioning to guarded/clean once the full 64 KiB page is committed.
  • crates/victory-gpu/src/shadow.rs:
    • Add shadow::tests::partially_committed_page_copies_committed_tail_and_tracks_updates verifying end-to-end Shadow::update copies committed sub-pages and picks up later title writes instead of zeroing the host buffer and marking it clean.

Testing

  • Verified both new tests fail on main (63bcca43) before the fix (watch classifies the partially committed 128 KiB range as zero-fill/unmapped; shadow reads [0] instead of 0xDEAD_BEEF from the committed tail).
  • cargo test -p victory-gpu --locked --lib -- --skip lists_share_a_table_and_copies_keep_their_views --skip dynamic_slots_are_held_until_released (RTX 5090): 231 passed, 0 failed, 1 ignored.
  • VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --locked --features diagnostics (WARP): 248 passed, 0 failed, 1 ignored.
  • cargo clippy --workspace --all-targets --locked -- -D warnings and --all-features: clean.
  • cargo test -p xtask --locked policy::tests::repository_passes_policy: passed.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 09:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/3125) `fuzeofficlal:fix/gpu-watch-partial-commit-pages` → `main` · +179 −5 · 2 files · 1 commits · open ## Summary In `crates/victory-gpu/src/watch.rs`, `protect_noting` checks whether a 64 KiB page (`PAGE`) that `VirtualProtect(..., PAGE_READONLY)` failed on is unmapped by calling `VirtualQuery` once at `page * PAGE` and testing `n == 0 || info.State != MEM_COMMIT`. Because Windows commits memory at 4 KiB granularity while `watch::PAGE` is 64 KiB, a 64 KiB block whose leading 4 KiB OS page is `MEM_RESERVE` and whose remaining 4 KiB sub-pages are `MEM_COMMIT` returns `info.State == MEM_RESERVE` with `info.RegionSize == 4096`. `unmapped(page)` then returns `true`, which causes: 1. `take_split` to classify the entire 64 KiB page as `unmapped` so `Shadow::update_tracked` zero-fills all 64 KiB in the host shadow buffer, discarding live committed title data (even though `host::copy_mapped` already supports 4 KiB fallback copies for partially committed blocks). 2. `protect_noting` to include the page in `taken`, removing it from `changed` (and marking the tracker clean) even though the committed sub-pages could not be guarded, so subsequent CPU writes to those sub-pages are silently missed. 3. `guard_aliases`, `Tracked::taken`, and `title_memory_aliased` (which all go through `protect`) to treat partially committed pages as guarded. ## Changes - `crates/victory-gpu/src/watch.rs`: - Update `unmapped` inside `protect_noting` to walk all `VirtualQuery` sub-regions across `[page * PAGE, (page + 1) * PAGE)` and return `false` as soon as any sub-region has `info.State == MEM_COMMIT`. Fully unmapped/reserved 64 KiB pages (`RegionSize >= PAGE`) still finish in a single `VirtualQuery` call. - Add `watch::tests::partially_committed_pages_are_not_taken_as_unmapped` covering leading-reserved/tail-committed pages, `Tracked::taken`, and transitioning to guarded/clean once the full 64 KiB page is committed. - `crates/victory-gpu/src/shadow.rs`: - Add `shadow::tests::partially_committed_page_copies_committed_tail_and_tracks_updates` verifying end-to-end `Shadow::update` copies committed sub-pages and picks up later title writes instead of zeroing the host buffer and marking it clean. ## Testing - Verified both new tests fail on `main` (`63bcca43`) before the fix (`watch` classifies the partially committed 128 KiB range as zero-fill/unmapped; `shadow` reads `[0]` instead of `0xDEAD_BEEF` from the committed tail). - `cargo test -p victory-gpu --locked --lib -- --skip lists_share_a_table_and_copies_keep_their_views --skip dynamic_slots_are_held_until_released` (RTX 5090): 231 passed, 0 failed, 1 ignored. - `VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --locked --features diagnostics` (WARP): 248 passed, 0 failed, 1 ignored. - `cargo clippy --workspace --all-targets --locked -- -D warnings` and `--all-features`: clean. - `cargo test -p xtask --locked policy::tests::repository_passes_policy`: passed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:07:59 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 09:20 UTC · original

Automated triage for the maintainers.

Merge candidate. The unmapped check in protect_noting (crates/victory-gpu/src/watch.rs) made a single VirtualQuery at page * PAGE, which reports only the first region. So a 64 KiB page with a reserved head and a committed tail counted as unmapped: take_split/Shadow::update_tracked zero-filled it and dropped it from changed. The new loop walks the page's regions, returns false at the first MEM_COMMIT, and stops if a region fails to advance. It runs only after VirtualProtect has already failed. Both new tests build a real partial commit with VirtualAlloc and check the copy, the dirty state, and the switch to guarded once the page is fully committed. One cost to know about: a page that stays partly committed is never guarded, so it is copied at every refresh. That is correct, but it will show in the copy statistics.

CI: check passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 09:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/3125#issuecomment-7187822675) <!-- victory-triage v1 head=7357b44dd327335790c1c98ad2181000ac5b0f09 verdict=merge-candidate at=2026-11-26T09:22:00Z --> Automated triage for the maintainers. **Merge candidate.** The `unmapped` check in `protect_noting` (`crates/victory-gpu/src/watch.rs`) made a single `VirtualQuery` at `page * PAGE`, which reports only the first region. So a 64 KiB page with a reserved head and a committed tail counted as unmapped: `take_split`/`Shadow::update_tracked` zero-filled it and dropped it from `changed`. The new loop walks the page's regions, returns false at the first `MEM_COMMIT`, and stops if a region fails to advance. It runs only after `VirtualProtect` has already failed. Both new tests build a real partial commit with `VirtualAlloc` and check the copy, the dirty state, and the switch to guarded once the page is fully committed. One cost to know about: a page that stays partly committed is never guarded, so it is copied at every refresh. That is correct, but it will show in the copy statistics. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 09:26 UTC · original · approved these changes

Verified on RTX 5090 against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib watch:: -- --test-threads=1: 16/16 passed (including partially_committed_pages_are_not_taken_as_unmapped).
  • cargo test -p victory-gpu --lib shadow:: -- --test-threads=1: 11/11 passed (including partially_committed_page_copies_committed_tail_and_tracks_updates).
  • cargo test -p victory-gpu --lib host::title_copies:: -- --test-threads=1: 2/2 passed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly scans all sub-regions before treating a partially committed 64 KiB page as unmapped.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 09:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/3125#pullrequestreview-6071529726) · **approved these changes** Verified on RTX 5090 against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib watch:: -- --test-threads=1`: 16/16 passed (including `partially_committed_pages_are_not_taken_as_unmapped`). - `cargo test -p victory-gpu --lib shadow:: -- --test-threads=1`: 11/11 passed (including `partially_committed_page_copies_committed_tail_and_tracks_updates`). - `cargo test -p victory-gpu --lib host::title_copies:: -- --test-threads=1`: 2/2 passed. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly scans all sub-regions before treating a partially committed 64 KiB page as unmapped. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
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/3125/head:github-bot/gh-3125'
git switch 'github-bot/gh-3125'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3125'
git switch 'github-bot/gh-3125'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3125'
git switch 'github-bot/gh-3125'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3125'
git switch 'main'
git merge --squash 'github-bot/gh-3125'
git switch 'main'
git merge --ff-only 'github-bot/gh-3125'
git switch 'main'
git merge 'github-bot/gh-3125'
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!3125
No description provided.