GPU: keep alias groups transitive and symmetrically guarded in title_memory_aliased #3159

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3161 into main
fuzeofficlal commented 2026-11-26 10:35:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T10:35:33Z.


Summary

watch::title_memory_aliased maintains the alias_of graph and page-guard state when a physical page is mapped at multiple virtual addresses. On main, three gaps in title_memory_aliased can leave alias groups partially linked or asymmetrically guarded, causing writes through another mapping of the same physical page to go silently unseen:

  1. One-way guard check misses writes after a title_memory_remapped -> take -> title_memory_aliased race:
    In xmem::map, title_memory_remapped(base, len) releases GUARDS before the per-page loop calls title_memory_aliased(base + k * PAGE, shown). If a GPU submission runs Tracked::take on base + k * PAGE in that window, page enters g.protected (PAGE_READONLY) and its tracker becomes clean while shown stays PAGE_READWRITE. title_memory_aliased only checked others.iter().any(|&o| g.protected.contains(&(o / PAGE))), so it neither marked page changed nor guarded/opened the group—missing any write through shown during the window and leaving shown permanently PAGE_READWRITE for future writes. The same issue occurs when others itself mixes guarded and unguarded mappings.
  2. Non-transitive alias links drop cross-mapping notifications and break on unmap:
    title_memory_aliased only linked page directly with each element of others. When a <-> b already exists and title_memory_aliased(c, &[a]) is called, b and c are never linked: take(b) leaves c unguarded, writes (on_write / writing) and retake at c miss trackers holding b, dropping a tracker on c opens c despite b's live tracker, and unmapping a via title_memory_remapped(a, PAGE) removes a from both adjacency lists and severs b and c completely.
  3. Self-aliasing inserts a self-link:
    Passing an address in the same 64 KiB page as at in others inserted page -> page into alias_of.

Changes

  • crates/victory-gpu/src/watch.rs:
    • In title_memory_aliased, return early for empty others or self-only inputs without creating self-links.
    • Expand page's existing component (new_group) and the merged connected component (clique) via expand_aliases, then link every distinct pair in clique so alias groups remain complete cliques (preserved when title_memory_remapped removes a page and its incident edges).
    • Keep guard state symmetric across the merged clique:
      • If only new_group is unguarded and every existing mapping in the clique is guarded, guard all pages in new_group with protect(&mut g, &unguarded); if any page in new_group (including a busy sibling) cannot be guarded, call changed(&mut g, page, page + 1, false) to mark all trackers and open every guard in the clique without saving post-write bytes.
      • Otherwise, whenever the merged clique has both guarded and unguarded mappings (such as page already guarded while an existing mapping stayed PAGE_READWRITE, or mixed guard states in others), call changed(&mut g, page, page + 1, false) so neither raced writes nor subsequent writes through previously unguarded mappings go unseen.
    • Added 4 focused unit tests in watch::tests covering the guarded-to-unguarded race and mixed others, transitive alias propagation/retake/drop/bridge-unmap, guarding a newly joined group and busy-sibling rollback, and self-alias handling.

Verification

  • Verified all 4 new tests fail on main (63bcca43) before the fix and pass after the fix.
  • cargo test -p victory-gpu --locked watch::tests: 19/19 passed.
  • VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --features diagnostics --locked (watch::tests, bindless::tests::heap_views_follow_writes_through_another_mapping, shadow::tests): all passed on hardware GPU and WARP.
  • VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --features diagnostics --locked: 250 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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:5508403608 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3161). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T10:35:33Z. --- ### Summary `watch::title_memory_aliased` maintains the `alias_of` graph and page-guard state when a physical page is mapped at multiple virtual addresses. On `main`, three gaps in `title_memory_aliased` can leave alias groups partially linked or asymmetrically guarded, causing writes through another mapping of the same physical page to go silently unseen: 1. **One-way guard check misses writes after a `title_memory_remapped` -> `take` -> `title_memory_aliased` race**: In `xmem::map`, `title_memory_remapped(base, len)` releases `GUARDS` before the per-page loop calls `title_memory_aliased(base + k * PAGE, shown)`. If a GPU submission runs `Tracked::take` on `base + k * PAGE` in that window, `page` enters `g.protected` (`PAGE_READONLY`) and its tracker becomes clean while `shown` stays `PAGE_READWRITE`. `title_memory_aliased` only checked `others.iter().any(|&o| g.protected.contains(&(o / PAGE)))`, so it neither marked `page` changed nor guarded/opened the group—missing any write through `shown` during the window and leaving `shown` permanently `PAGE_READWRITE` for future writes. The same issue occurs when `others` itself mixes guarded and unguarded mappings. 2. **Non-transitive alias links drop cross-mapping notifications and break on unmap**: `title_memory_aliased` only linked `page` directly with each element of `others`. When `a <-> b` already exists and `title_memory_aliased(c, &[a])` is called, `b` and `c` are never linked: `take(b)` leaves `c` unguarded, writes (`on_write` / `writing`) and `retake` at `c` miss trackers holding `b`, dropping a tracker on `c` opens `c` despite `b`'s live tracker, and unmapping `a` via `title_memory_remapped(a, PAGE)` removes `a` from both adjacency lists and severs `b` and `c` completely. 3. **Self-aliasing inserts a self-link**: Passing an address in the same 64 KiB page as `at` in `others` inserted `page -> page` into `alias_of`. ### Changes - **`crates/victory-gpu/src/watch.rs`**: - In `title_memory_aliased`, return early for empty `others` or self-only inputs without creating self-links. - Expand `page`'s existing component (`new_group`) and the merged connected component (`clique`) via `expand_aliases`, then link every distinct pair in `clique` so alias groups remain complete cliques (preserved when `title_memory_remapped` removes a page and its incident edges). - Keep guard state symmetric across the merged clique: - If only `new_group` is unguarded and every existing mapping in the clique is guarded, guard all pages in `new_group` with `protect(&mut g, &unguarded)`; if any page in `new_group` (including a busy sibling) cannot be guarded, call `changed(&mut g, page, page + 1, false)` to mark all trackers and open every guard in the clique without saving post-write bytes. - Otherwise, whenever the merged clique has both guarded and unguarded mappings (such as `page` already guarded while an existing mapping stayed `PAGE_READWRITE`, or mixed guard states in `others`), call `changed(&mut g, page, page + 1, false)` so neither raced writes nor subsequent writes through previously unguarded mappings go unseen. - Added 4 focused unit tests in `watch::tests` covering the guarded-to-unguarded race and mixed `others`, transitive alias propagation/retake/drop/bridge-unmap, guarding a newly joined group and busy-sibling rollback, and self-alias handling. ### Verification - Verified all 4 new tests fail on `main` (`63bcca43`) before the fix and pass after the fix. - `cargo test -p victory-gpu --locked watch::tests`: 19/19 passed. - `VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --features diagnostics --locked` (`watch::tests`, `bindless::tests::heap_views_follow_writes_through_another_mapping`, `shadow::tests`): all passed on hardware GPU and WARP. - `VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --features diagnostics --locked`: 250 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.
Dukestar69 commented 2026-11-26 10:48:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T10:48:43Z.

Review: APPROVED


Verified against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib watch:: -- --test-threads=1: 19/19 passed (including aliasing_a_guarded_page_to_an_unguarded_mapping_does_not_miss_writes, aliasing_a_newly_mapped_page_guards_its_group_and_handles_busy_siblings, aliasing_through_one_member_joins_the_whole_alias_group, and self_aliasing_adds_no_self_link_and_preserves_guards).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly keeps alias groups transitive and symmetrically guarded in title_memory_aliased.

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

<!-- greyfinch-sync:Marvisak/VIctory:review:6072773251 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3161#pullrequestreview-6072773251). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T10:48:43Z. **Review:** APPROVED --- Verified against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib watch:: -- --test-threads=1`: 19/19 passed (including `aliasing_a_guarded_page_to_an_unguarded_mapping_does_not_miss_writes`, `aliasing_a_newly_mapped_page_guards_its_group_and_handles_busy_siblings`, `aliasing_through_one_member_joins_the_whole_alias_group`, and `self_aliasing_adds_no_self_link_and_preserves_guards`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly keeps alias groups transitive and symmetrically guarded in `title_memory_aliased`. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Jackster commented 2026-11-26 23:02:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T23:02:20Z.


Windows 11, 63bcca4 + this PR, Radeon RX 7900 XTX:

  • cargo test -p victory-gpu --lib: 235 passed / 0 failed / 1 ignored (unpatched 63bcca4 here: 231; +4).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.

All four alias tests pass: aliasing_a_guarded_page_to_an_unguarded_mapping_does_not_miss_writes, aliasing_through_one_member_joins_the_whole_alias_group, aliasing_a_newly_mapped_page_guards_its_group_and_handles_busy_siblings, self_aliasing_adds_no_self_link_and_preserves_guards. Synthetic only. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7205199930 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3161#issuecomment-7205199930). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T23:02:20Z. --- Windows 11, `63bcca4` + this PR, Radeon RX 7900 XTX: - `cargo test -p victory-gpu --lib`: 235 passed / 0 failed / 1 ignored (unpatched `63bcca4` here: 231; +4). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. All four alias tests pass: `aliasing_a_guarded_page_to_an_unguarded_mapping_does_not_miss_writes`, `aliasing_through_one_member_joins_the_whole_alias_group`, `aliasing_a_newly_mapped_page_guards_its_group_and_handles_busy_siblings`, `self_aliasing_adds_no_self_link_and_preserves_guards`. Synthetic only. AI-assisted.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3161:upstream-pr/3161'
git switch 'upstream-pr/3161'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3161'
git switch 'upstream-pr/3161'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3161'
git switch 'upstream-pr/3161'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3161'
git switch 'main'
git merge --squash 'upstream-pr/3161'
git switch 'main'
git merge --ff-only 'upstream-pr/3161'
git switch 'main'
git merge 'upstream-pr/3161'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!3159
No description provided.