Skip to content

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

Closed
tightfall wants to merge 1 commit into
Marvisak:mainfrom
tightfall:xmem-alias-guards
Closed

tightfall wants to merge 1 commit into
Marvisak:mainfrom
tightfall:xmem-alias-guards

Conversation

@tightfall

Copy link
Copy Markdown

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 + GPU: keep replaced host resources for 6 frames; bigger dynamic descriptor pool, no fallback to stale tables #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

…ll its views

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 <noreply@anthropic.com>
LAKSHYA-cmd-tech added a commit to LAKSHYA-cmd-tech/VIctory that referenced this pull request Nov 24, 2026
GPU: a write through one view of physical pages opens the guards of all its views (Marvisak#1122)

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested merged cleanly onto main (ffe310e) on Windows 11 x64 (Rust 1.98.1 MSVC) with NVIDIA GeForce RTX 5090 (Blackwell) and WARP (VICTORY_D3D12_WARP=1):

  • cargo test -p victory-gpu (RTX 5090): 51 passed, 0 failed, 1 ignored
  • VICTORY_D3D12_WARP=1 cargo test -p victory-gpu (WARP): 51 passed, 0 failed, 1 ignored
  • cargo clippy -p victory-gpu --all-targets: 0 warnings
  • cargo xtask policy: ok

Clean guard-opening fix across aliased physical-page views, verified on Blackwell hardware and WARP. Approving.

AI-assisted (Gemini 4 Neon).

@adamdotpy

Copy link
Copy Markdown

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.

@tightfall

Copy link
Copy Markdown
Author

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.

@aycow

aycow commented Nov 24, 2026

Copy link
Copy Markdown

@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?

@Zipettte

Copy link
Copy Markdown

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).

simnJS added a commit to MiamiGame/XTP that referenced this pull request Nov 24, 2026
…e guards of all its views

BTP 6 writes physical pages through one view (a pool) while the GPU reads
them through another (its tiled heap, map_alias). The write watch guarded
view memory block by block, so a read-only guard on one view did not see
writes through the other, and the host copy kept what it first took
(stale mesh data).

xmem keeps an index of the addresses showing each physical page and
answers the GPU side's range lookups (xtp_gpu::set_alias_lookup). Guarding
a block guards the other views of its page, a write to any of them opens
them all, and a report of a change to one view opens the others.

Ported from Marvisak/VIctory#1122 (head 9ed6093) by tightfall, adapted to
XTP's watch (run-merged opening, GUARDED, title_memory_remapped), with:
- the late view aycow found on #1122 (fa2de5f): a view mapped after a copy
  guarded another one was never guarded. xmem notes a view before it
  reports it, and the report opens the page's other views from the index;
- adamdotpy's review there: links are pruned exactly (opening a page's
  views unlinks them all) and the comments say a block is a page;
- a page with a view that cannot be made read-only is left open, not
  clean; the fault handler keeps the title's last error;
- one index lookup per run guarded, and pages shown once cost nothing to
  look up (a set of shared addresses).
@Marvisak

Copy link
Copy Markdown
Owner

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 Marvisak closed this Nov 25, 2026
LukeFarch pushed a commit to LukeFarch/VIctory that referenced this pull request Nov 25, 2026
The title shows one physical page at several addresses. A copy taken
through one of them was guarded at that address only, so a write through
another left the copy as it was for the rest of the run. On one machine
the first room's walls and a face's shading came back in two runs out of
two; the walls also came back by themselves in some earlier runs, so this
is not proof.

Code of victory-fork Marvisak#115 by Spexivus (the index of the views of each
page, the guard over all of them), with joroyt's correction of it (another
view is made read-only, not noted clean) and the set of aliased blocks of
VIctory Marvisak#1122 by tightfall. Added here: a page of more than 64 views is
guarded one view at a time, a switch (VICTORY_VIEW_GUARD=0), counts in the
diagnostics log, and tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Ported onto this fork's own-write settling (own_write_begin and
own_write_end open and guard through the same paths).

Claude-Session: https://claude.ai/code/session_01DnmJgv1UMusjz7uG9bKMA7
bodnjenie14 added a commit to bodnjenie14/VIctory that referenced this pull request Nov 25, 2026
Support unaligned 12-byte typed views while retaining their backing heaps.
Track writes across views of physical pages and keep DirectStorage kernel
destinations writable until the read completes.

Include the Wave64 lowering, indirect scratch, readback and diagnostic changes
from the RTX build used for the apartment and prison captures.

Physical-page handling adapted from myokex/victory-amd@c6dc4d8, incorporating
Marvisak#1122 and fxceless/victory-fork#115. Other incorporated changes
include fxceless/victory-fork#220, Marvisak#253 and Marvisak#260.

Verified on RTX 5070 Ti. The apartment capture shows restored clothing and
interior surfaces. Prison lighting and gameplay performance remain incomplete.
Vaximous pushed a commit to Vaximous/VIctory that referenced this pull request Nov 25, 2026
…rites counted; levels on demand (off); warm-up; card memory numbers

Memory sync:
- The guard over every view of a physical page (code by myokex, victory-amd
  c6dc4d8, after pull request Marvisak#115 by Spexivus, its correction by joroyt and
  a part of VIctory Marvisak#1122 by tightfall): the title shows one page at several
  addresses and a copy was guarded at one only. 0.8 to 1.0 million writes
  through another view in a five-minute run on the test machine.
  VICTORY_VIEW_GUARD=0 turns it off.
- Kernel reads into title memory are read beside and copied in (Joshua Odom,
  10ea38e).
- Writes of the storage layer into physical pages are counted where the
  title has the pages mapped and can open the guards there
  (VICTORY_PAGE_WRITES, off: counted only). Failed page-destination reads
  are retried.

Card memory:
- Levels on demand, off unless VICTORY_LEVELS_ON_DEMAND=1: a texture the
  title fills takes card memory for the levels the title holds; held levels
  get memory only while the card has room and are asked for again. Its test
  runs on the software adapter; not run on a card yet.
- Refused texture slots are held back so a pass reaches the slots behind
  them; room can be kept for card copies of title buffers (off).

Tools: translate --warm all; the shader cache is keyed on the translator's
source; counters for copy-back races, page writes and the copies the
texture sweep never lets go; a walls trace.

Game runs of these changes on an RTX 4070: five five-minute runs without a
device loss or a failed request. This merged tree was built; its test
suites were run on the tree the changes come from (gpu 129, hle 97).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
MfDev0 pushed a commit to MfDev0/victory that referenced this pull request Nov 25, 2026
…rites counted; levels on demand (off); warm-up; card memory numbers

Memory sync:
- The guard over every view of a physical page (code by myokex, victory-amd
  c6dc4d8, after pull request JMAN730#115 by Spexivus, its correction by joroyt and
  a part of VIctory Marvisak#1122 by tightfall): the title shows one page at several
  addresses and a copy was guarded at one only. 0.8 to 1.0 million writes
  through another view in a five-minute run on the test machine.
  VICTORY_VIEW_GUARD=0 turns it off.
- Kernel reads into title memory are read beside and copied in (Joshua Odom,
  10ea38e).
- Writes of the storage layer into physical pages are counted where the
  title has the pages mapped and can open the guards there
  (VICTORY_PAGE_WRITES, off: counted only). Failed page-destination reads
  are retried.

Card memory:
- Levels on demand, off unless VICTORY_LEVELS_ON_DEMAND=1: a texture the
  title fills takes card memory for the levels the title holds; held levels
  get memory only while the card has room and are asked for again. Its test
  runs on the software adapter; not run on a card yet.
- Refused texture slots are held back so a pass reaches the slots behind
  them; room can be kept for card copies of title buffers (off).

Tools: translate --warm all; the shader cache is keyed on the translator's
source; counters for copy-back races, page writes and the copies the
texture sweep never lets go; a walls trace.

Game runs of these changes on an RTX 4070: five five-minute runs without a
device loss or a failed request. This merged tree was built; its test
suites were run on the tree the changes come from (gpu 129, hle 97).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
PastiiJay pushed a commit to PastiiJay/klbf-lab-beta that referenced this pull request Nov 25, 2026
…rites counted; levels on demand (off); warm-up; card memory numbers

Memory sync:
- The guard over every view of a physical page (code by myokex, victory-amd
  c6dc4d8, after pull request Marvisak#115 by Spexivus, its correction by joroyt and
  a part of VIctory Marvisak#1122 by tightfall): the title shows one page at several
  addresses and a copy was guarded at one only. 0.8 to 1.0 million writes
  through another view in a five-minute run on the test machine.
  VICTORY_VIEW_GUARD=0 turns it off.
- Kernel reads into title memory are read beside and copied in (Joshua Odom,
  10ea38e).
- Writes of the storage layer into physical pages are counted where the
  title has the pages mapped and can open the guards there
  (VICTORY_PAGE_WRITES, off: counted only). Failed page-destination reads
  are retried.

Card memory:
- Levels on demand, off unless VICTORY_LEVELS_ON_DEMAND=1: a texture the
  title fills takes card memory for the levels the title holds; held levels
  get memory only while the card has room and are asked for again. Its test
  runs on the software adapter; not run on a card yet.
- Refused texture slots are held back so a pass reaches the slots behind
  them; room can be kept for card copies of title buffers (off).

Tools: translate --warm all; the shader cache is keyed on the translator's
source; counters for copy-back races, page writes and the copies the
texture sweep never lets go; a walls trace.

Game runs of these changes on an RTX 4070: five five-minute runs without a
device loss or a failed request. This merged tree was built; its test
suites were run on the tree the changes come from (gpu 129, hle 97).
LeanWasTaken pushed a commit to LeanWasTaken/VIctory-bc250 that referenced this pull request Nov 26, 2026
The title shows one physical page at several addresses. A copy taken
through one of them was guarded at that address only, so a write through
another left the copy as it was for the rest of the run. On one machine
the first room's walls and a face's shading came back in two runs out of
two; the walls also came back by themselves in some earlier runs, so this
is not proof.

Code of victory-fork Marvisak#115 by Spexivus (the index of the views of each
page, the guard over all of them), with joroyt's correction of it (another
view is made read-only, not noted clean) and the set of aliased blocks of
VIctory Marvisak#1122 by tightfall. Added here: a page of more than 64 views is
guarded one view at a time, a switch (VICTORY_VIEW_GUARD=0), counts in the
diagnostics log, and tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants