HLE: physical-page writes open the title's views before they write #2504

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

@⁠BastienRodz on GitHub · 2026-11-25 14:57 UTC · original
BastienRodz:hle/xmem-guard-before-physical-writes → main · +86 −8 · 5 files · 1 commits · open

What's wrong

xmem::page_window and xmem::write_physical write DirectStorage page destinations through our own views of the physical pages, which the write watch's guards on the title's views cannot see. Since 2921092, xmem tells the watch where the pages show when the write ends (report_written), which covers views mapped during the write. But it only tells the watch afterwards. kernel_will_write is meant to come before the write too ("Before our own write into title memory [...] opens its guarded blocks, marks them changed and keeps them open while the result lives"), because that is when it saves an armed page's bytes. Told only afterwards, an armed page keeps the streamed bytes as its old ones, and copy_back_marked puts the copy's stale bytes back over the streamed data.

What the fix does

  • A small PhysicalWrite opens the title views showing the destination pages with kernel_will_write when the write begins, and holds them until it is done.
  • In write_physical it is opened before the copy. In PageWindow it is a field, opened once the view is mapped (a failed map opens nothing) and released after main's end-of-write report.
  • Main's end-of-write report (report_written with the pages looked up at the end) is unchanged.
  • victory-gpu gets a test-hooks feature that re-exports Tracked/track, so victory-hle's test can use a real tracker. Only victory-hle's dev-dependency enables it, so normal builds keep the same public API.

Tests

New modules::xmem::tests::physical_writes_guard_views_before_writing_and_catch_late_mappings: a title view of a physical page is tracked, taken and armed, then a PageWindow writes the page. The saved bytes must be the old ones (0x11), not the streamed ones; a view mapped during the write must be marked changed; and write_physical must also save before it writes.

  • On main (da82e44) it fails: the saved byte is the streamed 0x99 (left: 153, right: 17).
  • With the fix it passes, as does main's page_windows_report_where_the_pages_show_when_done. modules::xmem:: 12/12 and modules::dstorage:: 16/16 pass (Wine 10, x86_64-pc-windows-msvc). Clippy is clean with and without test-hooks.

Not tested

No title run. How often the title streams into pages a submission has armed is not measured. #2213 also changes report_written and may conflict.

Fixes #2503

AI-assisted (Gemini 4 Neon found and wrote it, Claude Code reviewed it, moved the test access behind a feature, rebased it onto 2921092 and verified it). No game data used.

🤖 Generated with Claude Code

**@⁠BastienRodz** on GitHub · 2026-11-25 14:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/2504) `BastienRodz:hle/xmem-guard-before-physical-writes` → `main` · +86 −8 · 5 files · 1 commits · open ### What's wrong `xmem::page_window` and `xmem::write_physical` write DirectStorage page destinations through our own views of the physical pages, which the write watch's guards on the title's views cannot see. Since 2921092, `xmem` tells the watch where the pages show when the write ends (`report_written`), which covers views mapped during the write. But it only tells the watch afterwards. `kernel_will_write` is meant to come before the write too ("Before our own write into title memory [...] opens its guarded blocks, marks them changed and keeps them open while the result lives"), because that is when it saves an armed page's bytes. Told only afterwards, an armed page keeps the streamed bytes as its old ones, and `copy_back_marked` puts the copy's stale bytes back over the streamed data. ### What the fix does - A small `PhysicalWrite` opens the title views showing the destination pages with `kernel_will_write` when the write begins, and holds them until it is done. - In `write_physical` it is opened before the copy. In `PageWindow` it is a field, opened once the view is mapped (a failed map opens nothing) and released after main's end-of-write report. - Main's end-of-write report (`report_written` with the pages looked up at the end) is unchanged. - `victory-gpu` gets a `test-hooks` feature that re-exports `Tracked`/`track`, so `victory-hle`'s test can use a real tracker. Only `victory-hle`'s dev-dependency enables it, so normal builds keep the same public API. ### Tests New `modules::xmem::tests::physical_writes_guard_views_before_writing_and_catch_late_mappings`: a title view of a physical page is tracked, taken and armed, then a `PageWindow` writes the page. The saved bytes must be the old ones (`0x11`), not the streamed ones; a view mapped during the write must be marked changed; and `write_physical` must also save before it writes. - On `main` (da82e44) it fails: the saved byte is the streamed `0x99` (`left: 153`, `right: 17`). - With the fix it passes, as does main's `page_windows_report_where_the_pages_show_when_done`. `modules::xmem::` 12/12 and `modules::dstorage::` 16/16 pass (Wine 10, x86_64-pc-windows-msvc). Clippy is clean with and without `test-hooks`. ### Not tested No title run. How often the title streams into pages a submission has armed is not measured. #2213 also changes `report_written` and may conflict. Fixes #2503 AI-assisted (Gemini 4 Neon found and wrote it, Claude Code reviewed it, moved the test access behind a feature, rebased it onto 2921092 and verified it). No game data used. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
DirectStorage's page destinations are written through our own views of
the physical pages (xmem::page_window, write_physical), which the GPU
write watch's guards on the title's views do not see. xmem tells the
watch where the pages show when the write ends (report_written), but
kernel_will_write is meant to come first as well: "Before our own write
into title memory [...] opens its guarded blocks, marks them changed and
keeps them open while the result lives", saving an armed page's bytes
before the write lands. Told only afterwards, an armed page kept the
streamed bytes as its old ones, and copy_back_marked put the copy's
stale bytes back over the streamed data.

PhysicalWrite opens the title views showing the pages when the write
begins and holds them until it is done; the end-of-write report still
tells views mapped in the meantime.

victory-gpu exposes its trackers (Tracked, track) under a new
`test-hooks` feature, which only victory-hle's tests enable.

Co-Authored-By: Gemini 4 Neon <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:07:39 +00:00
Author
Owner

@⁠BastienRodz on GitHub · 2026-11-25 15:49 UTC · original

Rebased onto main da82e44, on top of 2921092 ("writes into physical pages are reported where the pages show when the write ends"). That commit handles views mapped during the write and keeps reporting at the end. This PR now only adds the other half: the views showing the pages when the write begins are opened with kernel_will_write before it and held until it is done, so armed pages save their bytes from before the write. Main's report at the end is kept unchanged. On the new main the test still fails without the fix (the armed page saves the streamed 0x99, not 0x11) and passes with it; main's page_windows_report_where_the_pages_show_when_done passes too. xmem 12/12, dstorage 16/16.

**@⁠BastienRodz** on GitHub · 2026-11-25 15:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/2504#issuecomment-7167348066) Rebased onto main da82e44, on top of 2921092 ("writes into physical pages are reported where the pages show when the write ends"). That commit handles views mapped during the write and keeps reporting at the end. This PR now only adds the other half: the views showing the pages when the write begins are opened with `kernel_will_write` before it and held until it is done, so armed pages save their bytes from before the write. Main's report at the end is kept unchanged. On the new main the test still fails without the fix (the armed page saves the streamed `0x99`, not `0x11`) and passes with it; main's `page_windows_report_where_the_pages_show_when_done` passes too. xmem 12/12, dstorage 16/16.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 18:31 UTC · original · edited 2026-11-26 03:22 UTC

Automated triage for the maintainers.

Verdict: merge candidate. I reviewed the rebased head 4876663. PhysicalWrite::begin (crates/victory-hle/src/modules/xmem.rs) opens the title views showing the destination pages with kernel_will_write before write_physical copies and once page_window has mapped its view, and holds them until the write is done. Armed pages therefore save their bytes from before the streamed data. Main's end-of-write report_written is unchanged. shown_at returns title addresses, as report_written already assumes, and a failed View::map returns before anything is opened.

The new test reproduces the bug (saved byte 0x99 vs 0x11). For the maintainer: the Cargo change only adds a test-hooks feature to victory-gpu and enables it from victory-hle's dev-dependency on the same workspace crate. No new dependency.

CI: check passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 18:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/2504#issuecomment-7171013607) · edited 2026-11-26 03:22 UTC <!-- victory-triage v1 head=48766637ae3e1c3217b2526aa6e9c62024f8fe75 verdict=merge-candidate at=2026-11-26T03:22:18Z --> Automated triage for the maintainers. **Verdict: merge candidate.** I reviewed the rebased head `4876663`. `PhysicalWrite::begin` (`crates/victory-hle/src/modules/xmem.rs`) opens the title views showing the destination pages with `kernel_will_write` before `write_physical` copies and once `page_window` has mapped its view, and holds them until the write is done. Armed pages therefore save their bytes from before the streamed data. Main's end-of-write `report_written` is unchanged. `shown_at` returns title addresses, as `report_written` already assumes, and a failed `View::map` returns before anything is opened. The new test reproduces the bug (saved byte `0x99` vs `0x11`). For the maintainer: the Cargo change only adds a `test-hooks` feature to `victory-gpu` and enables it from `victory-hle`'s dev-dependency on the same workspace crate. No new dependency. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

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

Reviewed all 5 files at 48766637 merged onto origin/main (63bcca43) and verified across victory-hle, RTX 5090, and WARP (VICTORY_D3D12_WARP=1) with VICTORY_D3D12_DEBUG=1.

Lock ordering, write-watch & #[cfg] audit

  • No lock-order cycles: PhysicalWrite::begin (crates/victory-hle/src/modules/xmem.rs:461-474) collects and deduplicates title view addresses via shown_at(pages) — releasing PHYSICAL before calling victory_gpu::kernel_will_write(page, PAGE) (GUARDS). Since victory-gpu has no dependency on victory-hle, there is no GUARDS -> PHYSICAL path.
  • Pre-write save & drop ordering: write_physical (xmem.rs:663) opens views before copying and page_window (xmem.rs:729) opens them only after View::map succeeds (so failed maps open nothing). Opening via kernel_will_write before the write saves armed pages' pre-write bytes and pins them in g.busy; when PageWindow drops, Drop::drop runs report_written(&shown_at(&self.pages)) first to catch late mappings, then view unmaps, then _write (xmem.rs:694) drops its KernelWrite guards (clearing g.busy and marking changed once more).
  • Clean feature/non-Windows gating: test-hooks (crates/victory-gpu/Cargo.toml:26) re-exports Tracked/track strictly behind #[cfg(all(windows, feature = "test-hooks"))] (crates/victory-gpu/src/lib.rs:80-81) and is enabled only in crates/victory-hle/Cargo.toml:54 under [dev-dependencies], keeping default builds and non-Windows targets unchanged.

Verification

  • cargo run -p xtask -- policy: 191 files ok.
  • cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings (default, --all-features, and -p victory-gpu --features test-hooks): all clean.
  • cargo test -p victory-hle --lib -- --test-threads=1 modules::xmem:: modules::dstorage::: 31/31 passed (including physical_writes_guard_views_before_writing_and_catch_late_mappings at xmem.rs:1506 and page_windows_report_where_the_pages_show_when_done).
  • cargo test -p victory-gpu --lib watch:: shadow:: with VICTORY_D3D12_DEBUG=1: 25/25 passed on RTX 5090 and 25/25 passed on WARP (--features diagnostics).
**@⁠fuzeofficlal** on GitHub · 2026-11-26 15:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/2504#pullrequestreview-6076554645) · **approved these changes** Reviewed all 5 files at `48766637` merged onto `origin/main` (`63bcca43`) and verified across `victory-hle`, RTX 5090, and WARP (`VICTORY_D3D12_WARP=1`) with `VICTORY_D3D12_DEBUG=1`. ### Lock ordering, write-watch & `#[cfg]` audit - **No lock-order cycles**: `PhysicalWrite::begin` (`crates/victory-hle/src/modules/xmem.rs:461-474`) collects and deduplicates title view addresses via `shown_at(pages)` — releasing `PHYSICAL` before calling `victory_gpu::kernel_will_write(page, PAGE)` (`GUARDS`). Since `victory-gpu` has no dependency on `victory-hle`, there is no `GUARDS -> PHYSICAL` path. - **Pre-write save & drop ordering**: `write_physical` (`xmem.rs:663`) opens views before copying and `page_window` (`xmem.rs:729`) opens them only after `View::map` succeeds (so failed maps open nothing). Opening via `kernel_will_write` before the write saves armed pages' pre-write bytes and pins them in `g.busy`; when `PageWindow` drops, `Drop::drop` runs `report_written(&shown_at(&self.pages))` first to catch late mappings, then `view` unmaps, then `_write` (`xmem.rs:694`) drops its `KernelWrite` guards (clearing `g.busy` and marking changed once more). - **Clean feature/non-Windows gating**: `test-hooks` (`crates/victory-gpu/Cargo.toml:26`) re-exports `Tracked`/`track` strictly behind `#[cfg(all(windows, feature = "test-hooks"))]` (`crates/victory-gpu/src/lib.rs:80-81`) and is enabled only in `crates/victory-hle/Cargo.toml:54` under `[dev-dependencies]`, keeping default builds and non-Windows targets unchanged. ### Verification - `cargo run -p xtask -- policy`: 191 files ok. - `cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings` (default, `--all-features`, and `-p victory-gpu --features test-hooks`): all clean. - `cargo test -p victory-hle --lib -- --test-threads=1 modules::xmem:: modules::dstorage::`: **31/31 passed** (including `physical_writes_guard_views_before_writing_and_catch_late_mappings` at `xmem.rs:1506` and `page_windows_report_where_the_pages_show_when_done`). - `cargo test -p victory-gpu --lib watch:: shadow::` with `VICTORY_D3D12_DEBUG=1`: **25/25 passed** on RTX 5090 and **25/25 passed** on WARP (`--features diagnostics`).
Author
Owner

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

LGTM — verified on Windows against origin/main (63bcca4).

  • Opening the title's watch-guarded views before physical-page writes cleanly ensures watch trackers and guards stay coherent with physical-page updates.
  • cargo test -p victory-gpu --lib watch:: -- --test-threads=1: all passed.
  • cargo test -p victory-hle --lib modules::xmem:: -- --test-threads=1: all passed.
  • cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 21:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/2504#pullrequestreview-6080873953) · **approved these changes** LGTM — verified on Windows against `origin/main` (`63bcca4`). - Opening the title's watch-guarded views before physical-page writes cleanly ensures watch trackers and guards stay coherent with physical-page updates. - `cargo test -p victory-gpu --lib watch:: -- --test-threads=1`: all passed. - `cargo test -p victory-hle --lib modules::xmem:: -- --test-threads=1`: all passed. - `cargo clippy -p victory-gpu -p victory-hle --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/2504/head:github-bot/gh-2504'
git switch 'github-bot/gh-2504'

Merge

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