GPU: a write through one view of physical pages opens the guards of all its views #1122

Closed
github-bot wants to merge 1 commit from gh-pr/1122/head into gh-pr/1122/base
Owner

@⁠tightfall on GitHub · 2026-11-24 15:56 UTC · original
tightfall:xmem-alias-guards → main · +201 −9 · 3 files · 1 commits · closed without merging

Problem

The title maps the same physical pages at more than one address: it writes them through one view (a pool) and the
GPU reads them through another (map_alias). watch.rs guards view memory block by block, and the read-only guard
sits only on the view a copy was taken from. So a write through the other view never faulted, and the host copy kept
what it first took. In the world this showed up as stale mesh data: one page was copied once and never again. After
that came page faults that look like the freed-texture/root-view faults in #59 and #597.

Change

  • xmem keeps an index of the views that show each physical page (map_alias and pool mappings) and answers
    "which other addresses show this page" through watch::set_alias_lookup.
  • watch::protect makes a block's aliases read-only too. They are not marked clean, because no copy of an alias
    has taken them yet.
  • watch::open (fault handler and announced kernel writes) also opens the aliases of a written block. Only blocks
    that have guarded aliases get looked up, so a huge announced write range (the fread case that ec0d155 tests)
    costs the same as before.
  • Test: a_write_through_an_alias_view_is_seen_by_a_guard_on_the_other maps one pagefile section twice and checks
    both directions (a faulting write through either view, and a kernel write announced through writing).

Tested

  • cargo test --workspace on ffe310e + this commit: all pass. cargo clippy --workspace --all-targets clean.
  • With the title, RX 9070, Windows 11, Adrenalin 26.11.2, diag build with DRED. These runs used the same fix on an
    older stack (d2a4bae + #135 + local diagnostics), not this exact rebase:
    • before: world ~1:40 to ~4:44 (freed-texture page faults during DispatchMesh); with the fix: 10:15, then
      17:32 with TdrDelay=10, past the intro into the prologue.
    • The run ended on a different fault (a compute dispatch with a wild address), which I have not traced yet.

This PR was AI-assisted (Claude Code). The hardware runs and the logs are mine.

🤖 Generated with Claude Code

**@⁠tightfall** on GitHub · 2026-11-24 15:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/1122) `tightfall:xmem-alias-guards` → `main` · +201 −9 · 3 files · 1 commits · closed without merging ### Problem The title maps the same physical pages at more than one address: it writes them through one view (a pool) and the GPU reads them through another (`map_alias`). `watch.rs` guards view memory block by block, and the read-only guard sits only on the view a copy was taken from. So a write through the other view never faulted, and the host copy kept what it first took. In the world this showed up as stale mesh data: one page was copied once and never again. After that came page faults that look like the freed-texture/root-view faults in #59 and #597. ### Change - `xmem` keeps an index of the views that show each physical page (`map_alias` and pool mappings) and answers "which other addresses show this page" through `watch::set_alias_lookup`. - `watch::protect` makes a block's aliases read-only too. They are not marked clean, because no copy of an alias has taken them yet. - `watch::open` (fault handler and announced kernel writes) also opens the aliases of a written block. Only blocks that have guarded aliases get looked up, so a huge announced write range (the `fread` case that ec0d155 tests) costs the same as before. - Test: `a_write_through_an_alias_view_is_seen_by_a_guard_on_the_other` maps one pagefile section twice and checks both directions (a faulting write through either view, and a kernel write announced through `writing`). ### Tested - `cargo test --workspace` on `ffe310e` + this commit: all pass. `cargo clippy --workspace --all-targets` clean. - With the title, RX 9070, Windows 11, Adrenalin 26.11.2, diag build with DRED. These runs used the same fix on an older stack (d2a4bae + #135 + local diagnostics), not this exact rebase: - before: world ~1:40 to ~4:44 (freed-texture page faults during DispatchMesh); with the fix: 10:15, then 17:32 with TdrDelay=10, past the intro into the prologue. - The run ended on a different fault (a compute dispatch with a wild address), which I have not traced yet. This PR was AI-assisted (Claude Code). The hardware runs and the logs are mine. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The title writes physical pages through one view (a pool) while the GPU
reads them through another (map_alias). A read-only guard on one view did
not see writes through the other, so its host copy kept what it first
took (seen as stale mesh data in the world, before the page faults).
xmem keeps an index of the views showing each physical page; guarding a
block also guards its aliases, and a write to one opens them all. Only
blocks with guarded aliases are looked up on a write, so a huge kernel
write range costs no more than before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:20:30 +00:00
github-bot closed this pull request 2026-11-26 04:20:30 +00:00
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 16:12 UTC · original

Reviewed on Windows 11 / Rust 1.98.1, 9ed6093 on ffe310e (current main, merges as it stands), with a throwaway target directory — sharing CARGO_TARGET_DIR between two worktrees of the same crate can hand you the other worktree's test binary, and the first count I got was wrong for exactly that reason.

The test pins the fix. With the lookup suppressed (ALIAS_LOOKUP.get().filter(|_| false)) and the test unchanged:

assertion `left == right` failed: written through the alias
  left: Some([])
 right: Some([25747169])
test result: FAILED. 3 passed; 1 failed

Green as pushed: cargo test -p victory-gpu --lib 51 passed, 1 ignored (four watch tests), clippy --workspace --all-targets --all-features --locked -- -D warnings clean, cargo build --release -p victory builds. The test is a real two-view map too — one pagefile section mapped twice, a volatile write through one view, and the byte read back through the other — so it exercises the fault path rather than mocking it.

Why the shape is right. protect guards the aliases of every block it guards and records the pair both ways in aliased; open then only looks up blocks already in aliased, which is what keeps a huge write range from paying a lookup per block — the same trick the protected range already uses. unguard's if g.protected.remove(&block) also fixes something on its own: a block we never protected no longer gets a stray VirtualProtect, so an alias that is unmapped at the time of a fault costs nothing.

The scenario in the comment really is tracked. map_alias reaches note_aliases for every page — it calls map(&mut physical, to + k, PAGE, slice) per page, after unmap has removed the old entries — so the pool-write / tiled-heap-read case is covered by construction, not by luck. And unmap's by_address.get(&at) == Some(&key) check correctly leaves a repurposed page's fresh entry alone when the old section is unmapped under it.

Lock order is acyclic, but it is now three locks and one of them is on the fault path. The edges are PHYSICAL → GUARDS (map calls title_memory_remapped while holding it), GUARDS → ALIASES (aliases_of from protect/open), and PHYSICAL → ALIASES (note_aliases); nothing takes PHYSICAL from inside ALIASES, and other_views calls nothing, so there is no cycle. The comment says "its lock is taken last … nothing is called while it is held" — that sentence is now load-bearing: a fault handler that blocks cannot report a deadlock, so it is worth the same care the next time other_views grows a call.

Two things I would tighten.

  • aliased is the only state here that can go stale, and title_memory_remapped prunes it one-way: it drops the remapped blocks' own entries, but the other end of each pair keeps a reference to a block that is no longer an alias. That is harmless today — aliases_of returns an unmapped address, unguard skips it now that it checks protected, and a lookup that finds nothing costs one hash probe — but it means the set grows and never shrinks back. Dropping the reverse direction in title_memory_remapped (look up the aliases of the remapped blocks and remove those too) keeps it exact and is three lines.
  • BLOCK == PAGE == 64 KiB, but three doc comments here say "all of a block's pages" (guard, protect, and the note on aliased). If blocks were meant to be bigger than a page, the loops are missing; if not, the comments should say block == page, because a reader will go looking for the loop that is not there.

I cannot measure what this costs at runtime — it adds a hash probe per 64 KiB guarded and per block opened with aliases — and I have no title to check the pool/tiled-heap pattern against. The bug it closes is real on the mapping scheme it describes, so this is a correctness fix whose cost is bounded by the number of aliased blocks, which is the right place to spend it.

**@⁠adamdotpy** on GitHub · 2026-11-24 16:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/1122#issuecomment-7137293543) Reviewed on Windows 11 / Rust 1.98.1, `9ed6093` on `ffe310e` (current `main`, merges as it stands), with a throwaway target directory — sharing `CARGO_TARGET_DIR` between two worktrees of the same crate can hand you the other worktree's test binary, and the first count I got was wrong for exactly that reason. **The test pins the fix.** With the lookup suppressed (`ALIAS_LOOKUP.get().filter(|_| false)`) and the test unchanged: ``` assertion `left == right` failed: written through the alias left: Some([]) right: Some([25747169]) test result: FAILED. 3 passed; 1 failed ``` Green as pushed: `cargo test -p victory-gpu --lib` 51 passed, 1 ignored (four watch tests), clippy `--workspace --all-targets --all-features --locked -- -D warnings` clean, `cargo build --release -p victory` builds. The test is a real two-view map too — one pagefile section mapped twice, a volatile write through one view, and the byte read back through the other — so it exercises the fault path rather than mocking it. **Why the shape is right.** `protect` guards the aliases of every block it guards and records the pair *both* ways in `aliased`; `open` then only looks up blocks already in `aliased`, which is what keeps a huge write range from paying a lookup per block — the same trick the `protected` range already uses. `unguard`'s `if g.protected.remove(&block)` also fixes something on its own: a block we never protected no longer gets a stray `VirtualProtect`, so an alias that is unmapped at the time of a fault costs nothing. **The scenario in the comment really is tracked.** `map_alias` reaches `note_aliases` for every page — it calls `map(&mut physical, to + k, PAGE, slice)` per page, after `unmap` has removed the old entries — so the pool-write / tiled-heap-read case is covered by construction, not by luck. And `unmap`'s `by_address.get(&at) == Some(&key)` check correctly leaves a repurposed page's fresh entry alone when the old section is unmapped under it. **Lock order is acyclic, but it is now three locks and one of them is on the fault path.** The edges are `PHYSICAL → GUARDS` (`map` calls `title_memory_remapped` while holding it), `GUARDS → ALIASES` (`aliases_of` from `protect`/`open`), and `PHYSICAL → ALIASES` (`note_aliases`); nothing takes `PHYSICAL` from inside `ALIASES`, and `other_views` calls nothing, so there is no cycle. The comment says "its lock is taken last … nothing is called while it is held" — that sentence is now load-bearing: a fault handler that blocks cannot report a deadlock, so it is worth the same care the next time `other_views` grows a call. **Two things I would tighten.** - `aliased` is the only state here that can go stale, and `title_memory_remapped` prunes it one-way: it drops the remapped blocks' own entries, but the *other* end of each pair keeps a reference to a block that is no longer an alias. That is harmless today — `aliases_of` returns an unmapped address, `unguard` skips it now that it checks `protected`, and a lookup that finds nothing costs one hash probe — but it means the set grows and never shrinks back. Dropping the reverse direction in `title_memory_remapped` (look up the aliases of the remapped blocks and remove those too) keeps it exact and is three lines. - `BLOCK == PAGE == 64 KiB`, but three doc comments here say "all of a block's pages" (`guard`, `protect`, and the note on `aliased`). If blocks were meant to be bigger than a page, the loops are missing; if not, the comments should say block == page, because a reader will go looking for the loop that is not there. I cannot measure what this costs at runtime — it adds a hash probe per 64 KiB guarded and per block opened with aliases — and I have no title to check the pool/tiled-heap pattern against. The bug it closes is real on the mapping scheme it describes, so this is a correctness fix whose cost is bounded by the number of *aliased* blocks, which is the right place to spend it.
Author
Owner

@⁠tightfall on GitHub · 2026-11-24 16:27 UTC · original

In-game A/B on current main, to replace the older-stack numbers in the description with ones for this exact commit.

Setup: RX 9070, Windows 11, Adrenalin 26.11.2, TdrDelay=10. Diag build, VICTORY_D3D12_DRED=1, one run each. Runs 2 and 3 use the same scripted input (VICTORY_KEYS, Enter every 3 s). Time is the first to the last log line.

run build time end
1 ffe310e + this PR (manual input) 3:40 page fault in a mesh dispatch exactly at the end of a 4 MiB title-memory copy (the over-read from #557, which #493 removes)
2 ffe310e + this PR + #493 (c48b240) 6:10 page fault in a compute dispatch (list 37e19ccb… 1x1x1, then the dispatch at command 4), no title memory copy below the address
3 ffe310e + #493, without this PR 7:23 the same compute fault, at another address

So on today's main this change makes no visible difference in the world: with #493 both builds end at the same later compute fault, and one run each is within noise. The longer runs I quoted came from an older stack (d2a4bae + #135), where several copy paths have since changed on main. I can no longer attribute them to this commit, and I won't claim them.

What remains is the correctness case: a guard on one view of physical pages does not see writes through another view of them. The unit test a_write_through_an_alias_view_is_seen_by_a_guard_on_the_other covers both directions (a faulting write through either view, and an announced kernel write). Whether the title still needs this depends on whether it writes pool memory that the GPU reads through a map_alias view without any other refresh. I'll look for that in the logs, and close this PR if it doesn't.

AI-assisted (Claude Code); the runs and the hardware are mine.

**@⁠tightfall** on GitHub · 2026-11-24 16:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/1122#issuecomment-7137698010) In-game A/B on current `main`, to replace the older-stack numbers in the description with ones for this exact commit. **Setup:** RX 9070, Windows 11, Adrenalin 26.11.2, `TdrDelay=10`. Diag build, `VICTORY_D3D12_DRED=1`, one run each. Runs 2 and 3 use the same scripted input (`VICTORY_KEYS`, Enter every 3 s). Time is the first to the last log line. | run | build | time | end | |---|---|---|---| | 1 | `ffe310e` + this PR (manual input) | 3:40 | page fault in a mesh dispatch exactly at the end of a 4 MiB title-memory copy (the over-read from #557, which #493 removes) | | 2 | `ffe310e` + this PR + #493 (`c48b240`) | 6:10 | page fault in a compute dispatch (list `37e19ccb…` 1x1x1, then the dispatch at command 4), no title memory copy below the address | | 3 | `ffe310e` + #493, **without** this PR | 7:23 | the same compute fault, at another address | So on today's `main` this change makes **no visible difference** in the world: with #493 both builds end at the same later compute fault, and one run each is within noise. The longer runs I quoted came from an older stack (`d2a4bae` + #135), where several copy paths have since changed on `main`. I can no longer attribute them to this commit, and I won't claim them. What remains is the correctness case: a guard on one view of physical pages does not see writes through another view of them. The unit test `a_write_through_an_alias_view_is_seen_by_a_guard_on_the_other` covers both directions (a faulting write through either view, and an announced kernel write). Whether the title still needs this depends on whether it writes pool memory that the GPU reads through a `map_alias` view without any other refresh. I'll look for that in the logs, and close this PR if it doesn't. AI-assisted (Claude Code); the runs and the hardware are mine.
Author
Owner

@⁠aycow on GitHub · 2026-11-24 16:34 UTC · original

@⁠tightfall I reproduced a late-alias case on exact 9ed6093. The original view is guarded and clean, then a new virtual alias of the same page is mapped. A write through that new view changes the shared byte, while the original guard still reports clean. Focused patch and two-write regression. After opening the existing guards when the new view enters the mapping index, the next refresh saw the first write, rearmed the views, and saw a second write. All four watch tests and the HLE check passed on Windows. This was a CPU-side guard fixture. Would you prefer to fold this into #1122?

**@⁠aycow** on GitHub · 2026-11-24 16:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/1122#issuecomment-7137879754) @⁠tightfall I reproduced a late-alias case on exact `9ed6093`. The original view is guarded and clean, then a new virtual alias of the same page is mapped. A write through that new view changes the shared byte, while the original guard still reports clean. [Focused patch and two-write regression](https://github.com/aycow/VIctory/commit/fa2de5f9673ccb69e78da3c65290a16cc40c710d). After opening the existing guards when the new view enters the mapping index, the next refresh saw the first write, rearmed the views, and saw a second write. All four watch tests and the HLE check passed on Windows. This was a CPU-side guard fixture. Would you prefer to fold this into #1122?
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 18:01 UTC · original

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 9ed6093: cargo test -p victory-gpu -p victory-hle passes, 146 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

**@⁠Zipettte** on GitHub · 2026-11-24 18:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/1122#issuecomment-7140051508) Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 9ed6093: `cargo test -p victory-gpu -p victory-hle` passes, 146 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed). _Tested with AI assistance (Claude)._
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:32 UTC · original

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

Already in main: the runtime reports where physical pages are mapped (watch::title_memory_aliased) and a write through one mapping marks the trackers of all.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 00:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1122#issuecomment-7148614106) Automated triage for the maintainers. Closing because this change is already in `main`, or `main` has since replaced the code it patches. Already in main: the runtime reports where physical pages are mapped (watch::title_memory_aliased) and a write through one mapping marks the trackers of all. If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

Pull request closed

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!1122
No description provided.