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

Open
kotekompotek wants to merge 1 commit from upstream-pr/3488 into main
kotekompotek commented 2026-11-27 08:34:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-27T08:34:46Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:5523635413 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3488). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-27T08:34:46Z. --- 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).
oni-swr commented 2026-11-27 09:37:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-27T09:37:32Z.


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: https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7214169025

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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7214901316 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3488#issuecomment-7214901316). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-27T09:37:32Z. --- **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://github.com/Marvisak/VIctory/issues/1633#issuecomment-7214169025 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.
playcations commented 2026-11-27 12:28:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by playcations on 2026-11-27T12:28:24Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7218283526 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3488#issuecomment-7218283526). Originally posted by [playcations](https://github.com/playcations) on 2026-11-27T12:28:24Z. --- 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.
neonbyte1 commented 2026-11-28 20:04:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by neonbyte1 on 2026-11-28T20:04:15Z.


A data point for this PR from a tree that had #2504's form and then lost it.

  • Our integration fork (neonbyte1/VIctory) carried #2504 (PhysicalWrite, as merge 48766637a). A later change of ours (02cc2f8eb, lazy views, fork-only) replaced it with a path that only resolves lazy pages. That dropped the pre-write open.
  • On native Windows (RTX 4080, Windows 11; neonbyte1/VIctory#16, item H, run by @DieQuelle), the test this PR family carries, xmem::tests::physical_writes_guard_views_before_writing_and_catch_late_mappings, then failed: the view's saved byte read 0x99 instead of 0x11. The saved bytes were taken after the write instead of before it. Under Wine the same test passed on the same tree, so Linux/Wine runs don't catch this regression.
  • Restoring the pre-write open (our fork, 77ba9cdb7) makes it pass again.
  • On upstream main (63bcca43c): xmem.rs has only the post-write kernel_will_write, no pre-write open, so main has the gap this PR closes. We didn't run this PR's head ourselves; @oni-swr's Windows/WARP verification above covers that.

So, for whoever reviews: the gap is real, it's visible on Windows only, and this PR (with the late-mapping Drop half) is the most complete of #2504/#2566/#3488.

AI-assisted (Claude); measured on my machine and a collaborator's, no game data shared.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7245012726 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3488#issuecomment-7245012726). Originally posted by [neonbyte1](https://github.com/neonbyte1) on 2026-11-28T20:04:15Z. --- A data point for this PR from a tree that had #2504's form and then lost it. - Our integration fork (neonbyte1/VIctory) carried #2504 (`PhysicalWrite`, as merge `48766637a`). A later change of ours (`02cc2f8eb`, lazy views, fork-only) replaced it with a path that only resolves lazy pages. That dropped the pre-write open. - **On native Windows** (RTX 4080, Windows 11; neonbyte1/VIctory#16, item H, run by @DieQuelle), the test this PR family carries, `xmem::tests::physical_writes_guard_views_before_writing_and_catch_late_mappings`, then **failed: the view's saved byte read 0x99 instead of 0x11**. The saved bytes were taken after the write instead of before it. **Under Wine the same test passed** on the same tree, so Linux/Wine runs don't catch this regression. - Restoring the pre-write open (our fork, `77ba9cdb7`) makes it pass again. - **On upstream `main` (`63bcca43c`):** `xmem.rs` has only the post-write `kernel_will_write`, no pre-write open, so main has the gap this PR closes. We didn't run this PR's head ourselves; @oni-swr's Windows/WARP verification above covers that. So, for whoever reviews: the gap is real, it's visible on Windows only, and this PR (with the late-mapping `Drop` half) is the most complete of #2504/#2566/#3488. _AI-assisted (Claude); measured on my machine and a collaborator's, no game data shared._
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3488:upstream-pr/3488'
git switch 'upstream-pr/3488'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3488'
git switch 'upstream-pr/3488'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3488'
git switch 'upstream-pr/3488'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3488'
git switch 'main'
git merge --squash 'upstream-pr/3488'
git switch 'main'
git merge --ff-only 'upstream-pr/3488'
git switch 'main'
git merge 'upstream-pr/3488'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!3488
No description provided.