HLE: DirectStorage page writes are opened before the bytes land #2566

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

@⁠Calvix2k on GitHub · 2026-11-25 15:56 UTC · original
Calvix2k:codex/hle-dstorage-writes-before → main · +127 −16 · 3 files · 1 commits · open

Fixes #2503.

The failure

xmem told the write watch about DirectStorage's page-destination writes only after writing: report_written called kernel_will_write and dropped it at once, at the end of write_physical and in PageWindow::drop. watch::writing saves an armed page's bytes before it changes, so reporting after the write saved the bytes DirectStorage had just written. A marked copy-back then compared the copy against those bytes, took the copy's old bytes for the GPU's, and put them back over the streamed data. A title view mapped over the pages while a PageWindow was open was not in the list at all, so its tracker missed the write.

What the change does

  • write_physical opens the title views showing the bytes (write_before → kernel_will_write) before it copies, and drops them when it is done.
  • PageWindow opens them when it is created and holds them until it drops: the decode runs with the pages opened, marked changed, and an armed page keeps what was there before it.
  • Views mapped meanwhile cannot be opened before the write. Both paths tell them at the end with victory_gpu::title_memory_written, a new function in watch.rs that marks the page changed without saving bytes — reading them then would keep the write instead of what it replaced.
  • An address that stops showing a page meanwhile is not told again: the unmap already marked it.

Tests

  • a_window_opens_the_title_views_before_its_bytes: the view is opened when the window is created (a test-only hook records it), and it is not reported again at drop. Before the change nothing was opened and the view was reported at drop.
  • page_windows_report_where_the_pages_show_when_done: a view mapped during the window is still told; the write_physical half now asserts that the existing view is opened and nothing is reported afterwards.
  • a_write_reported_after_the_fact_saves_nothing (watch.rs): the bytes kept for an armed page are the ones from before the write, not the ones the write left.

cargo test -p victory-hle --lib: 147 passed. cargo test -p victory-gpu --features diagnostics --lib: 198 passed, 1 ignored, plus the known placed-view failure on a runtime without ID3D12Device10 (#1999, identical on main). Clippy -D warnings clean on both.

The new title_memory_written is exported next to kernel_will_write; it is the "write already happened" half of the pair, and it keeps saved bytes.

**@⁠Calvix2k** on GitHub · 2026-11-25 15:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/2566) `Calvix2k:codex/hle-dstorage-writes-before` → `main` · +127 −16 · 3 files · 1 commits · open Fixes #2503. ### The failure `xmem` told the write watch about DirectStorage's page-destination writes only **after** writing: `report_written` called `kernel_will_write` and dropped it at once, at the end of `write_physical` and in `PageWindow::drop`. `watch::writing` saves an armed page's bytes before it changes, so reporting after the write saved the bytes DirectStorage had just written. A marked copy-back then compared the copy against those bytes, took the copy's old bytes for the GPU's, and put them back over the streamed data. A title view mapped over the pages while a `PageWindow` was open was not in the list at all, so its tracker missed the write. ### What the change does - `write_physical` opens the title views showing the bytes (`write_before` → `kernel_will_write`) before it copies, and drops them when it is done. - `PageWindow` opens them when it is created and holds them until it drops: the decode runs with the pages opened, marked changed, and an armed page keeps what was there before it. - Views mapped meanwhile cannot be opened before the write. Both paths tell them at the end with `victory_gpu::title_memory_written`, a new function in `watch.rs` that marks the page changed **without** saving bytes — reading them then would keep the write instead of what it replaced. - An address that stops showing a page meanwhile is not told again: the unmap already marked it. ### Tests - `a_window_opens_the_title_views_before_its_bytes`: the view is opened when the window is created (a test-only hook records it), and it is not reported again at drop. Before the change nothing was opened and the view was reported at drop. - `page_windows_report_where_the_pages_show_when_done`: a view mapped during the window is still told; the `write_physical` half now asserts that the existing view is opened and nothing is reported afterwards. - `a_write_reported_after_the_fact_saves_nothing` (`watch.rs`): the bytes kept for an armed page are the ones from before the write, not the ones the write left. `cargo test -p victory-hle --lib`: **147 passed**. `cargo test -p victory-gpu --features diagnostics --lib`: **198 passed**, 1 ignored, plus the known placed-view failure on a runtime without `ID3D12Device10` (#1999, identical on `main`). Clippy `-D warnings` clean on both. The new `title_memory_written` is exported next to `kernel_will_write`; it is the "write already happened" half of the pair, and it keeps saved bytes.
`xmem` tells the GPU write watch about DirectStorage's page-destination
writes only after writing: `report_written` called `kernel_will_write` and
dropped it at once, at the end of `write_physical` and in `PageWindow::drop`.
`writing` saves an armed page's bytes before it changes, so reporting after
the write saved the bytes DirectStorage had just written; a marked
copy-back then compared the copy against those bytes, took the copy's old
bytes for the GPU's, and put them back over the streamed data. A title view
mapped over the pages while a `PageWindow` was open was not in the list at
all and its tracker missed the write (#2503).

`write_physical` now opens the title views showing the bytes before it
copies, and drops them when it is done. `PageWindow` opens them when it is
created and holds them until it drops, so the decode runs with the pages
opened and marked changed and an armed page keeps what was there before it.
Views mapped meanwhile cannot be opened before the write; both paths now
tell them at the end with `victory_gpu::title_memory_written`, which marks
the page changed **without** saving bytes (new in `watch.rs`; reading them
then would keep the write).

Tests:

- `a_window_opens_the_title_views_before_its_bytes`: the view is opened when
  the window is created (recorded by a test-only hook), not reported again
  when it drops. Fails before the change: nothing was opened, and the view
  was reported at drop.
- `page_windows_report_where_the_pages_show_when_done`: a view mapped during
  the window is still told, and `write_physical` now opens the existing view
  and reports nothing afterwards (the test asserts that instead).
- `a_write_reported_after_the_fact_saves_nothing` (`watch.rs`): the bytes
  kept for an armed page are the ones from before the write, not the ones the
  write left.

Lib suites on Windows 10 19045 / RX 7800 XT: `victory-hle` 147 passed;
`victory-gpu` 198 passed, 1 ignored, plus the known placed-view failure on a
runtime without `ID3D12Device10` (#1999); clippy `-D warnings` clean.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:09:23 +00:00
Author
Owner

@⁠Vedoxdev123 on GitHub · 2026-11-25 16:09 UTC · original

Tested on Windows with AMD Ryzen 5 5600X + NVIDIA GeForce RTX 4060 (AI-assisted):

cargo test -p victory-gpu --lib -> 194 passed; 2 failed; 1 ignored (up from 192 passed; 3 failed on main; new tests pass and views_bound_while_another_slot_gets_its_texture_do_not_wait passes).

Only the 2 known 1,000,000 view heap cap tests (dynamic_slots_are_held_until_released and lists_share_a_table_and_copies_keep_their_views from #1744) remain.

**@⁠Vedoxdev123** on GitHub · 2026-11-25 16:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/2566#issuecomment-7167640402) Tested on Windows with AMD Ryzen 5 5600X + NVIDIA GeForce RTX 4060 (AI-assisted): `cargo test -p victory-gpu --lib` -> **194 passed; 2 failed; 1 ignored** (up from `192 passed; 3 failed` on `main`; new tests pass and `views_bound_while_another_slot_gets_its_texture_do_not_wait` passes). Only the 2 known 1,000,000 view heap cap tests (`dynamic_slots_are_held_until_released` and `lists_share_a_table_and_copies_keep_their_views` from #1744) remain.
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 16:12 UTC · original

Independent verification (software WARP only)

  • PR head: ea51877ec80881c007999fd07b937bc6ebd9c0b3 (checked out detached)
  • origin/main: 67e8ae1401df112248aed36d0024483181b718b7; read-only git merge-tree --write-tree origin/main <head> merges cleanly (exit 0); GitHub reports MERGEABLE.
  • Environment: Windows 11 Pro 10.0.26200, rustc 1.98.1, VICTORY_D3D12_WARP=1 with VICTORY_DXIL pointing at the Windows SDK 10.0.26100 dxil.dll. Default features, no extra test flags.
  • Not tested: hardware GPU, the title (no game files used).

Commands and results

  • cargo test --locked -p victory-hle
  • cargo test --locked -p victory-gpu
  • focused: cargo test --locked -p victory-hle --lib xmem, cargo test --locked -p victory-gpu --lib a_write_reported_after
  • victory-hle lib: 147 passed, 0 failed (xmem filter: 12 passed, including a_window_opens_the_title_views_before_its_bytes and page_windows_report_where_the_pages_show_when_done); integration tests 1 + 1 passed.
  • victory-gpu lib: 196 passed, 0 failed, 1 ignored (a_write_reported_after_the_fact_saves_nothing passes). The placed-view failure the description mentions does not appear on WARP.

Review: Looks correct. Views are opened (kernel_will_write) before the bytes land and held to the end, so an armed page keeps its pre-write bytes. Views mapped meanwhile are told afterwards with the new title_memory_written, which marks changed without saving. report_new skips addresses already held, so nothing is reported twice. In page_window the view is mapped before the guards open, so the None return leaves no guard behind. No regressions seen in the two crates. Nit only: told/held ordering in PageWindow::drop relies on held being dropped before report_new, which the code does explicitly.

Review and verification by Claude Code (Claude Sonnet 5.5), AI-assisted; no game files, dumps, shader names or hashes involved.

**@⁠oni-swr** on GitHub · 2026-11-25 16:12 UTC · [original](https://github.com/Marvisak/VIctory/pull/2566#issuecomment-7167681647) **Independent verification (software WARP only)** - **PR head:** `ea51877ec80881c007999fd07b937bc6ebd9c0b3` (checked out detached) - **origin/main:** `67e8ae1401df112248aed36d0024483181b718b7`; read-only `git merge-tree --write-tree origin/main <head>` merges cleanly (exit 0); GitHub reports MERGEABLE. - **Environment:** Windows 11 Pro 10.0.26200, rustc 1.98.1, `VICTORY_D3D12_WARP=1` with `VICTORY_DXIL` pointing at the Windows SDK 10.0.26100 `dxil.dll`. Default features, no extra test flags. - **Not tested:** hardware GPU, the title (no game files used). **Commands and results** - `cargo test --locked -p victory-hle` - `cargo test --locked -p victory-gpu` - focused: `cargo test --locked -p victory-hle --lib xmem`, `cargo test --locked -p victory-gpu --lib a_write_reported_after` - victory-hle lib: 147 passed, 0 failed (xmem filter: 12 passed, including `a_window_opens_the_title_views_before_its_bytes` and `page_windows_report_where_the_pages_show_when_done`); integration tests 1 + 1 passed. - victory-gpu lib: 196 passed, 0 failed, 1 ignored (`a_write_reported_after_the_fact_saves_nothing` passes). The placed-view failure the description mentions does not appear on WARP. **Review:** Looks correct. Views are opened (`kernel_will_write`) before the bytes land and held to the end, so an armed page keeps its pre-write bytes. Views mapped meanwhile are told afterwards with the new `title_memory_written`, which marks changed without saving. `report_new` skips addresses already held, so nothing is reported twice. In `page_window` the view is mapped before the guards open, so the `None` return leaves no guard behind. No regressions seen in the two crates. Nit only: `told`/`held` ordering in `PageWindow::drop` relies on `held` being dropped before `report_new`, which the code does explicitly. Review and verification by Claude Code (Claude Sonnet 5.5), AI-assisted; no game files, dumps, shader names or hashes involved.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 19:19 UTC · original · edited 2026-11-25 22:15 UTC

Automated triage for the maintainers.

Needs rebase. This PR no longer merges cleanly into main: main has moved since the last triage, and the branch now conflicts in crates/victory-gpu/src/watch.rs. It will be reviewed again once it is rebased onto current main.

Previous verdict for this head: merge candidate.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 19:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/2566#issuecomment-7172136187) · edited 2026-11-25 22:15 UTC <!-- victory-triage v1 head=ea51877ec80881c007999fd07b937bc6ebd9c0b3 verdict=needs-rebase at=2026-11-25T22:16:00Z --> Automated triage for the maintainers. **Needs rebase.** This PR no longer merges cleanly into `main`: `main` has moved since the last triage, and the branch now conflicts in `crates/victory-gpu/src/watch.rs`. It will be reviewed again once it is rebased onto current `main`. Previous verdict for this head: merge candidate. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠brainbudt on GitHub · 2026-11-25 21:08 UTC · original

Note

🤖 Gemini 4 Neon (with GPT-6.2 Astra, Mistral Large 4, and DeepSeek V4.1 Flash) responding on behalf of brainbudt

Independently verified ea51877 merged onto 13f91ca (and combined with #2553; both merge orders produce the same tree and pass Windows 11 DirectStorage/xmem/watch tests and MSVC workspace cargo check --tests).

Opening existing views before the write fixes the original pre-image ordering, but one remaining race reproduced on Windows 11:

  1. Late-mapped view stays clean until window drop (crates/victory-hle/src/modules/xmem.rs:696-705): Opening a window on an unmapped physical page, mapping a new title view, taking/arming its tracker, and writing through the window leaves the tracker clean until drop—so a marked GPU copy-back in that interval can overwrite the streamed bytes with stale host bytes. Making the new mapping participate in the active physical write (or deferring copy-back until reported) plus a regression covering the pre-drop interval would close this gap.
  2. Test coverage (crates/victory-gpu/src/watch.rs:1225-1246): Changing the end-of-write notification to save=true still passes the new GPU watch test; making that report the first change after take-and-arm catches the mutation.
**@⁠brainbudt** on GitHub · 2026-11-25 21:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/2566#issuecomment-7174668021) > [!NOTE] > 🤖 **Gemini 4 Neon (with GPT-6.2 Astra, Mistral Large 4, and DeepSeek V4.1 Flash) responding on behalf of brainbudt** Independently verified `ea51877` merged onto `13f91ca` (and combined with #2553; both merge orders produce the same tree and pass Windows 11 DirectStorage/xmem/watch tests and MSVC workspace `cargo check --tests`). Opening existing views before the write fixes the original pre-image ordering, but one remaining race reproduced on Windows 11: 1. **Late-mapped view stays clean until window drop (`crates/victory-hle/src/modules/xmem.rs:696-705`):** Opening a window on an unmapped physical page, mapping a new title view, taking/arming its tracker, and writing through the window leaves the tracker clean until drop—so a marked GPU copy-back in that interval can overwrite the streamed bytes with stale host bytes. Making the new mapping participate in the active physical write (or deferring copy-back until reported) plus a regression covering the pre-drop interval would close this gap. 2. **Test coverage (`crates/victory-gpu/src/watch.rs:1225-1246`):** Changing the end-of-write notification to `save=true` still passes the new GPU watch test; making that report the first change after take-and-arm catches the mutation.
github-bot force-pushed github-bot/gh-2566 from ea51877ec8 to 89a4b63df5 2026-11-26 13:59:33 +00:00 Compare
Author
Owner

@⁠Calvix2k on GitHub · 2026-11-26 18:50 UTC · original

The rebase is up (current head pushed against today's main). The late-mapped-view race you describe is not something this change tries to solve - it is the same interval from the other side, a view mapped after the window opened.

Two ways to take it: either you open it as an issue with the test you used, and whoever picks up the view-tracking work fixes it there, or I fold the case into this PR. I would prefer the issue unless you think the fix belongs in the same place as the write pre-image ordering - then say so and I will do it here.

**@⁠Calvix2k** on GitHub · 2026-11-26 18:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/2566#issuecomment-7200904035) The rebase is up (current head pushed against today's main). The late-mapped-view race you describe is not something this change tries to solve - it is the same interval from the other side, a view mapped after the window opened. Two ways to take it: either you open it as an issue with the test you used, and whoever picks up the view-tracking work fixes it there, or I fold the case into this PR. I would prefer the issue unless you think the fix belongs in the same place as the write pre-image ordering - then say so and I will do it here.
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/2566/head:github-bot/gh-2566'
git switch 'github-bot/gh-2566'

Merge

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