HLE: a view mapped during a physical write joins it #3488

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

@⁠kotekompotek on GitHub · 2026-11-27 08:34 UTC · original
kotekompotek:hle/late-physical-write-views → main · +157 −14 · 5 files · 1 commits · open

A title view of a physical page mapped while a write into that page is still open now joins the write: xmem immediately opens a kernel_will_write lease for the new view and holds it in the active write, so a tracker can no longer take/arm the view clean while streamed bytes are landing. Fixes #3481.

This is the pre-drop interval the #2566 discussion left as separate follow-up. The failure exists independently on plain main, so this is based on main rather than stacked on #2566; the two are conceptually compatible but touch the same physical-write area — whichever lands second needs a normal rebase (mechanical merge-tree check shows a textual conflict in xmem.rs only).

What is in it

  • xmem.rs: Physical tracks active writes by the same slice key as shown; ActiveWrite keeps the section alive, counts overlapping writers, and holds KernelWrite leases for late-mapped views; PhysicalWrite is the RAII registration begun by write_physical and — for a successful page_window — kept for the window's whole lifetime. map opens kernel_will_write for a view mapped over an active write; unmap drops only the exact view's lease.
  • Regression tests: the issue's CPU-only integration probe end-to-end through real physical allocation, page_window, XMemVirtualAlloc/XMemMapPhysicalPages and the real victory-gpu write watch, plus the positive control (kernel_will_write held before tracking).
  • Test access: narrow test-hooks feature on victory-gpu exposing watch::track/Tracked for integration tests — same shape as #2504's plumbing; production builds unaffected.

Tested

  • Windows 10 Pro 19045, i5-8300H, GTX 1050 (driver 32.0.15.8228), Rust MSVC, debug.
  • Before the fix on plain main 63bcca43: late_window_view_must_be_dirty_before_window_drop FAILS with the issue's exact signature — "late title view is still clean after streamed bytes landed, before drop".
  • After: both focused tests pass. Full victory-hle suite 154 passed / 0 failed (baseline 152 + the 2 new). Full victory-gpu suite 226 passed / 5 failed / 1 ignored — the 5 failures are the documented environment-dependent baseline, bit-identical set on unmodified main.
  • cargo clippy --locked -p victory-gpu -p victory-hle --all-targets -- -D warnings clean, including --all-features.
  • cargo test --locked -p xtask: 6 passed — includes the repository policy test.

Not tested
End-to-end interaction with the unmerged #2566/#2504 behavior in a running title (synthetic probes only, per the issue's own reproduction).

AI use: implementation and verification AI-assisted (Devin / SWE-2.1).

**@⁠kotekompotek** on GitHub · 2026-11-27 08:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/3488) `kotekompotek:hle/late-physical-write-views` → `main` · +157 −14 · 5 files · 1 commits · open A title view of a physical page mapped while a write into that page is still open now joins the write: xmem immediately opens a `kernel_will_write` lease for the new view and holds it in the active write, so a tracker can no longer take/arm the view clean while streamed bytes are landing. Fixes #3481. This is the pre-drop interval the #2566 discussion left as separate follow-up. The failure exists independently on plain main, so this is based on main rather than stacked on #2566; the two are conceptually compatible but touch the same physical-write area — whichever lands second needs a normal rebase (mechanical merge-tree check shows a textual conflict in xmem.rs only). **What is in it** - `xmem.rs`: `Physical` tracks active writes by the same slice key as `shown`; `ActiveWrite` keeps the section alive, counts overlapping writers, and holds `KernelWrite` leases for late-mapped views; `PhysicalWrite` is the RAII registration begun by `write_physical` and — for a successful `page_window` — kept for the window's whole lifetime. `map` opens `kernel_will_write` for a view mapped over an active write; `unmap` drops only the exact view's lease. - Regression tests: the issue's CPU-only integration probe end-to-end through real physical allocation, `page_window`, `XMemVirtualAlloc`/`XMemMapPhysicalPages` and the real victory-gpu write watch, plus the positive control (`kernel_will_write` held before tracking). - Test access: narrow `test-hooks` feature on victory-gpu exposing `watch::track`/`Tracked` for integration tests — same shape as #2504's plumbing; production builds unaffected. **Tested** - Windows 10 Pro 19045, i5-8300H, GTX 1050 (driver 32.0.15.8228), Rust MSVC, debug. - Before the fix on plain main `63bcca43`: `late_window_view_must_be_dirty_before_window_drop` FAILS with the issue's exact signature — "late title view is still clean after streamed bytes landed, before drop". - After: both focused tests pass. Full `victory-hle` suite 154 passed / 0 failed (baseline 152 + the 2 new). Full `victory-gpu` suite 226 passed / 5 failed / 1 ignored — the 5 failures are the documented environment-dependent baseline, bit-identical set on unmodified main. - `cargo clippy --locked -p victory-gpu -p victory-hle --all-targets -- -D warnings` clean, including `--all-features`. - `cargo test --locked -p xtask`: 6 passed — includes the repository policy test. **Not tested** End-to-end interaction with the unmerged #2566/#2504 behavior in a running title (synthetic probes only, per the issue's own reproduction). AI use: implementation and verification AI-assisted (Devin / SWE-2.1).
A DirectStorage write through a page window or write_physical was reported
to a title view mapped meanwhile only when the write ended. The view's
tracker could be taken and armed before streamed bytes landed and stay
clean while they were already visible, so a marked copy-back in that
interval could put stale bytes over them.

Keep a write registration for each physical page while a write through a
view of our own is under way. Mapping a title view of the page then opens
the view with kernel_will_write, and the registration holds that write
until the last overlapping write ends; unmapping the view drops only its
own lease. Taking the view cannot clean its tracker during the write, and
the end report does not keep streamed bytes as the pre-image.

Add the issue's CPU-only integration probe through xmem and the real write
watch, plus its kernel_will_write positive control. victory-gpu exposes its
trackers behind a test-hooks feature enabled only by victory-hle's
dev-dependency.

Fixes #3481.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 08:35:00 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-27 09:37 UTC · original

Independent Windows/WARP verification of #3488 at 8bd2ec785384edf8a21c268eefac47386bfd4dc5

Environment: Windows 11 Pro build 10.0.26200, rustc 1.98.1, no physical GPU: VICTORY_WARP=1 and VICTORY_D3D12_WARP=1 (WARP only), dxil.dll 1.8.2502.11 from the Windows SDK 10.0.26100.0. Base is main 63bcca43c6; all three PR heads (#3487, #3488, #3489) sit directly on it. --locked, separate worktrees for base and head. Claim: #1633

AI-assisted (Claude); I read the diff and the logs before posting. No game files were used. WARP is not hardware, so this says nothing about driver behaviour on AMD or NVIDIA.

Result: no regression found on WARP; the two new tests pass.

  • victory-gpu --lib: base 231 passed / 0 failed / 1 ignored; head 231 / 0 / 1 (3 runs each, identical).
  • victory-hle (all targets): base 152 lib tests + 2 integration passed, 0 failed (3 runs). Head: 154 lib tests + 2 integration passed (the two new ones are late_window_view_must_be_dirty_before_window_drop and late_window_view_opened_before_take_stays_dirty) in 2 of 3 runs.
  • One failure on head that I could not reproduce: in 1 of 3 full head runs, modules::dstorage::tests::a_page_list_filled_after_its_request_starts failed at dstorage.rs:1642 (left: -2147024809 = 0x80070057, right: 0). I then ran that test alone 60 times and the whole victory-hle lib 10 times, on base, on head, and on head merged with #3487: 0 failures anywhere. Other builds were running on this machine during the failed run, so I read it as a load-sensitive flake, but I cannot exclude a link to this PR, since the test exercises the page-list path it changes. Worth one more run on hardware.

Diff read (no defect found; two things I did not audit):

  • PhysicalWrite::begin takes the physical() lock, and map() now calls victory_gpu::kernel_will_write while holding it (lock order xmem, then watch). I did not check for the reverse order (a watch path calling back into xmem); if there is none, it is fine.
  • track, take, clean, arm, saved and Tracked become pub in watch.rs unconditionally, though they are re-exported only under test-hooks (a dev-dependency of victory-hle). I did not run clippy, so I cannot say whether non-test builds warn.

Overlap with #3487: both edit victory-gpu/src/watch.rs. They merge with no conflict (git merge-tree), the hunks are disjoint (#3487 adds dirty_word; this PR only widens visibility), and I see no semantic interaction. Merged tree: victory-gpu 235 passed / 0 failed, victory-hle 154 passed / 0 failed, 3 runs each.

**@⁠oni-swr** on GitHub · 2026-11-27 09:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/3488#issuecomment-7214901316) **Independent Windows/WARP verification of #3488 at `8bd2ec785384edf8a21c268eefac47386bfd4dc5`** Environment: Windows 11 Pro build 10.0.26200, rustc 1.98.1, no physical GPU: `VICTORY_WARP=1` and `VICTORY_D3D12_WARP=1` (WARP only), `dxil.dll` 1.8.2502.11 from the Windows SDK 10.0.26100.0. Base is `main` 63bcca43c6a00a758510398fa43096ce7e4f7df0; all three PR heads (#3487, #3488, #3489) sit directly on it. `--locked`, separate worktrees for base and head. Claim: https://git.gtahub.st/github/Marvisak--VIctory/issues/1633 AI-assisted (Claude); I read the diff and the logs before posting. No game files were used. WARP is not hardware, so this says nothing about driver behaviour on AMD or NVIDIA. **Result: no regression found on WARP; the two new tests pass.** - `victory-gpu --lib`: base 231 passed / 0 failed / 1 ignored; head 231 / 0 / 1 (3 runs each, identical). - `victory-hle` (all targets): base 152 lib tests + 2 integration passed, 0 failed (3 runs). Head: 154 lib tests + 2 integration passed (the two new ones are `late_window_view_must_be_dirty_before_window_drop` and `late_window_view_opened_before_take_stays_dirty`) in 2 of 3 runs. - One failure on head that I could not reproduce: in 1 of 3 full head runs, `modules::dstorage::tests::a_page_list_filled_after_its_request_starts` failed at `dstorage.rs:1642` (`left: -2147024809` = `0x80070057`, `right: 0`). I then ran that test alone 60 times and the whole `victory-hle` lib 10 times, on base, on head, and on head merged with #3487: 0 failures anywhere. Other builds were running on this machine during the failed run, so I read it as a load-sensitive flake, but I cannot exclude a link to this PR, since the test exercises the page-list path it changes. Worth one more run on hardware. **Diff read (no defect found; two things I did not audit):** - `PhysicalWrite::begin` takes the `physical()` lock, and `map()` now calls `victory_gpu::kernel_will_write` while holding it (lock order xmem, then watch). I did not check for the reverse order (a watch path calling back into xmem); if there is none, it is fine. - `track`, `take`, `clean`, `arm`, `saved` and `Tracked` become `pub` in `watch.rs` unconditionally, though they are re-exported only under `test-hooks` (a dev-dependency of `victory-hle`). I did not run clippy, so I cannot say whether non-test builds warn. **Overlap with #3487:** both edit `victory-gpu/src/watch.rs`. They merge with no conflict (`git merge-tree`), the hunks are disjoint (#3487 adds `dirty_word`; this PR only widens visibility), and I see no semantic interaction. Merged tree: `victory-gpu` 235 passed / 0 failed, `victory-hle` 154 passed / 0 failed, 3 runs each.
Author
Owner

@⁠playcations on GitHub · 2026-11-27 12:28 UTC · original

The original two probes pass on 8bd2ec785384edf8a21c268eefac47386bfd4dc5. I found three narrower publication/lifetime gaps and reproduced each with deterministic CPU-only interleavings through the real mapper and write watch:

  1. Map publication: a tracker can already exist on the reservation. After the OS mapping/title_memory_remapped, but before the new lease is acquired, it can take/arm the view. A private window write is then visible while the tracker is clean; the eventual saving lease records the streamed byte as the pre-image. My probe records clean=true, visible 0x33, saved Some(0x33).
  2. Unmap publication: write.views.remove ends the lease before the host removes the mapping. Taking/arming and writing in the interval after the remap notification but before UnmapViewOfFile2 again gives a clean tracker while the new byte is still visible.
  3. Freed opaque ID: runs has resolved a valid section, but PhysicalWrite::begin looks the IDs up again. Freeing an ID in between skips registration even though an existing view keeps the section alive and map_alias can still make a late view from it. That view takes clean during the window.

I prepared a follow-up branch on your exact head, ending at 652f090eb41e6967c746d78fc52225854426f7bf (complete diff):

  • register directly from the resolved slices;
  • hold a no-save busy lease before publishing a late mapping, and attach it to the active write after successful map/commit;
  • keep removed leases alive through the host unmap, before mapping retained fragments;
  • reject occupied committed destinations before no-save invalidation. A separate failure control requires a rejected map to leave an unrelated armed pool clean and preserve its original byte on the next real write.

All three new race probes fail on your head and pass on the follow-up. The follow-up also covers overlapping windows, split views and address reuse, and preserves the original two tests. All 19 xmem tests pass under Wine. In the broader HLE suite the original head has 148 passes / 6 failures and the follow-up 153 / 6, with exactly the same failure set in this environment.

For the landing-order question, I also resolved the textual conflict with unchanged #2566 at 89a4b63 on a separate integration branch, merge f60fbd6acf4d79f06de8f9bc8c53a3fd2cf14051. All 21 xmem tests pass there. A new control checks that both write_physical and PageWindow retain the original byte for a view armed before the write; it fails without #2566 and passes in the combined tree. This is an offered merge resolution, not a competing PR.

Both trees pass Windows-target Clippy for victory-gpu/victory-hle with --all-targets, default and --all-features, -D warnings, and the asset policy. Environment: Ubuntu26.04.1, Wine11.22 Staging, Rust1.98.1/cargo-xwin; no GPU or game files exposed. Test-only hooks inject the legal interleavings without timing sleeps. Sol source review and a separate Astra concurrency audit informed the follow-up; I inspected the code and executed logs before posting. AI-assisted with Codex GPT-6.1 Sol.

Two inherited areas remain outside these executed success-path probes: an end-of-write address snapshot can be replaced before its notification, and host unmap/rollback errors are ignored. The source audit identified those paths; I did not observe a naturally failing valid host unmap. They deserve separate coverage before claiming all mapping failure/address-reuse races are closed.

**@⁠playcations** on GitHub · 2026-11-27 12:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/3488#issuecomment-7218283526) The original two probes pass on `8bd2ec785384edf8a21c268eefac47386bfd4dc5`. I found three narrower publication/lifetime gaps and reproduced each with deterministic CPU-only interleavings through the real mapper and write watch: 1. **Map publication:** a tracker can already exist on the reservation. After the OS mapping/`title_memory_remapped`, but before the new lease is acquired, it can take/arm the view. A private window write is then visible while the tracker is clean; the eventual saving lease records the streamed byte as the pre-image. My probe records `clean=true`, visible `0x33`, saved `Some(0x33)`. 2. **Unmap publication:** `write.views.remove` ends the lease before the host removes the mapping. Taking/arming and writing in the interval after the remap notification but before `UnmapViewOfFile2` again gives a clean tracker while the new byte is still visible. 3. **Freed opaque ID:** `runs` has resolved a valid section, but `PhysicalWrite::begin` looks the IDs up again. Freeing an ID in between skips registration even though an existing view keeps the section alive and `map_alias` can still make a late view from it. That view takes clean during the window. I prepared a [follow-up branch](https://github.com/playcations/VIctory/tree/followup/physical-write-publication) on your exact head, ending at `652f090eb41e6967c746d78fc52225854426f7bf` ([complete diff](https://github.com/playcations/VIctory/compare/8bd2ec785384edf8a21c268eefac47386bfd4dc5...652f090eb41e6967c746d78fc52225854426f7bf)): - register directly from the resolved slices; - hold a no-save busy lease before publishing a late mapping, and attach it to the active write after successful map/commit; - keep removed leases alive through the host unmap, before mapping retained fragments; - reject occupied committed destinations before no-save invalidation. A separate failure control requires a rejected map to leave an unrelated armed pool clean and preserve its original byte on the next real write. All three new race probes fail on your head and pass on the follow-up. The follow-up also covers overlapping windows, split views and address reuse, and preserves the original two tests. **All 19 xmem tests pass under Wine.** In the broader HLE suite the original head has 148 passes / 6 failures and the follow-up 153 / 6, with exactly the same failure set in this environment. For the landing-order question, I also resolved the textual conflict with unchanged #2566 at `89a4b63` on a [separate integration branch](https://github.com/playcations/VIctory/tree/review/physical-write-preimage-combined), merge `f60fbd6acf4d79f06de8f9bc8c53a3fd2cf14051`. **All 21 xmem tests pass there.** A new control checks that both `write_physical` and `PageWindow` retain the original byte for a view armed before the write; it fails without #2566 and passes in the combined tree. This is an offered merge resolution, not a competing PR. Both trees pass Windows-target Clippy for `victory-gpu`/`victory-hle` with `--all-targets`, default and `--all-features`, `-D warnings`, and the asset policy. Environment: Ubuntu26.04.1, Wine11.22 Staging, Rust1.98.1/cargo-xwin; no GPU or game files exposed. Test-only hooks inject the legal interleavings without timing sleeps. Sol source review and a separate Astra concurrency audit informed the follow-up; I inspected the code and executed logs before posting. AI-assisted with Codex GPT-6.1 Sol. Two inherited areas remain outside these executed success-path probes: an end-of-write address snapshot can be replaced before its notification, and host unmap/rollback errors are ignored. The source audit identified those paths; I did not observe a naturally failing valid host unmap. They deserve separate coverage before claiming all mapping failure/address-reuse races are closed.
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/3488/head:github-bot/gh-3488'
git switch 'github-bot/gh-3488'

Merge

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