HLE: a persistent view above the title's memory, three-state commits, and a write generation per page #872

Open
github-bot wants to merge 5 commits from github-bot/gh-872 into main AGit
Owner

@⁠HailLucifer666 on GitHub · 2026-11-25 11:24 UTC · original
HailLucifer666:perf/xmem-high-va-alias-and-dirty-gen → main · +1003 −35 · 1 files · 5 commits · open

⚠️ Stacked on #231 (perf/xmem-persistent-section-views, still open). The diff here shows both because the base branch lives on this fork, not upstream: once #231 lands, this shrinks to the one commit it actually adds. Do not merge before #231.

Three changes to the physical-page path, none of which changes it by default (VICTORY_XMEM_PERSISTENT_VIEW still off):

The view's address. It used to be wherever MapViewOfFile put it, which can be inside the range the title itself owns — below 40 bits, the range decision 0001 says title structures must live in. A view there either takes an address the title is about to ask for, or makes our own mapping look like a title address to the title's own pointer arithmetic. It is now reserved with VirtualAlloc2's address requirement from 1 TiB to GetSystemInfo().lpMaximumApplicationAddress (asked, never assumed) and mapped into that reservation with MapViewOfFile3. A view that comes back below the ceiling anyway is released and the caller keeps its temporary view — the host is not trusted to honour the requirement.

Commit state, three states per page instead of one bit. The bit recorded "committed" but could not promise anything at the moment it mattered: the DirectStorage workers write these sections concurrently, and a thread that lost the race to VirtualAlloc(MEM_COMMIT) would store into a page whose commit was still in flight. Pages are now FREE → COMMITTING (claimed by one thread) → COMMITTED (set only after VirtualAlloc returned); a loser spins on COMMITTING instead of writing. A bounded spin then gives up to the temporary path, which is always correct, so a thread that died mid-commit cannot hang the title.

A write generation per 64 KiB page. Bumped after the bytes are written (release), read with acquire: an unchanged digest is a promise about the bytes, not only about the counter. physical_generation_digest(pages, offset, len) folds a range into one number — the input phase F needs to stop hashing texture memory it already knows. It has no caller here yet beyond its test, and the allow(dead_code) says so rather than pretending otherwise.

Tests (3, all device-free):

  • persistent_view_maps_above_the_title_memory_ceiling — the view lands at or above 1 TiB, a write through it is in the section's pages, and the rule refuses one page below the ceiling.
  • concurrent_commit_state_never_exposes_an_uncommitted_page — eight threads commit the same four uncommitted pages; all eight succeed and every byte each wrote is still there.
  • dirty_generation_digest_changes_on_write_and_stays_stable_when_clean — a physical write moves the range's digest but not an untouched page's; a second identical write moves it again; a page window's bytes count when the window goes; unknown pages and ranges past the end are refused.

/kee probes, each restored byte-identical:

mutation result
generation bump after a physical write removed fails, a write must show
page window's bump on drop removed fails, a page window's bytes must show
above_title_memory always true fails on the page below the ceiling
LowestStartingAddress set to null passes — nothing catches it

That last row is the honest part: this host's allocator is already top-down, so it returns an address above 1 TiB with or without the requirement. The requirement is a belt no test here can reach; the predicate is the pin, and that is the one the probe breaks. Said out loud rather than reported as a passing probe.

Gate on 2853f67e: cargo test -p victory-hle xmem:: → 22 passed; cargo test -p victory-hle → 108 passed, 0 failed; both clippy --workspace --all-targets passes red only on main's three pre-existing lints; xtask 4 passed.

Not covered: no run in the game, so whether the high address helps anything in practice is unmeasured. VICTORY_XMEM_PERSISTENT_VIEW stays off.

**@⁠HailLucifer666** on GitHub · 2026-11-25 11:24 UTC · [original](https://github.com/fxceless/victory-fork/pull/872) `HailLucifer666:perf/xmem-high-va-alias-and-dirty-gen` → `main` · +1003 −35 · 1 files · 5 commits · open > ⚠️ **Stacked on #231** (`perf/xmem-persistent-section-views`, still open). The diff here shows both because the base branch lives on this fork, not upstream: once #231 lands, this shrinks to the one commit it actually adds. **Do not merge before #231.** Three changes to the physical-page path, none of which changes it by default (`VICTORY_XMEM_PERSISTENT_VIEW` still off): **The view's address.** It used to be wherever `MapViewOfFile` put it, which can be inside the range the title itself owns — below 40 bits, the range decision 0001 says title structures must live in. A view there either takes an address the title is about to ask for, or makes our own mapping look like a title address to the title's own pointer arithmetic. It is now reserved with `VirtualAlloc2`'s address requirement from 1 TiB to `GetSystemInfo().lpMaximumApplicationAddress` (asked, never assumed) and mapped into that reservation with `MapViewOfFile3`. A view that comes back below the ceiling anyway is released and the caller keeps its temporary view — the host is not trusted to honour the requirement. **Commit state, three states per page instead of one bit.** The bit recorded "committed" but could not promise anything at the moment it mattered: the DirectStorage workers write these sections concurrently, and a thread that lost the race to `VirtualAlloc(MEM_COMMIT)` would store into a page whose commit was still **in flight**. Pages are now `FREE` → `COMMITTING` (claimed by one thread) → `COMMITTED` (set only after `VirtualAlloc` returned); a loser spins on `COMMITTING` instead of writing. A bounded spin then gives up to the temporary path, which is always correct, so a thread that died mid-commit cannot hang the title. **A write generation per 64 KiB page.** Bumped *after* the bytes are written (release), read with acquire: an unchanged digest is a promise about the bytes, not only about the counter. `physical_generation_digest(pages, offset, len)` folds a range into one number — the input phase F needs to stop hashing texture memory it already knows. It has no caller here yet beyond its test, and the `allow(dead_code)` says so rather than pretending otherwise. Tests (3, all device-free): - `persistent_view_maps_above_the_title_memory_ceiling` — the view lands at or above 1 TiB, a write through it is in the section's pages, and the rule refuses one page below the ceiling. - `concurrent_commit_state_never_exposes_an_uncommitted_page` — eight threads commit the same four uncommitted pages; all eight succeed and every byte each wrote is still there. - `dirty_generation_digest_changes_on_write_and_stays_stable_when_clean` — a physical write moves the range's digest but not an untouched page's; a second identical write moves it again; a page window's bytes count when the window goes; unknown pages and ranges past the end are refused. `/kee` probes, each restored byte-identical: | mutation | result | |---|---| | generation bump after a physical write removed | fails, `a write must show` | | page window's bump on drop removed | fails, `a page window's bytes must show` | | `above_title_memory` always true | fails on the page below the ceiling | | `LowestStartingAddress` set to null | **passes — nothing catches it** | That last row is the honest part: this host's allocator is already top-down, so it returns an address above 1 TiB with or without the requirement. The requirement is a belt no test here can reach; the predicate is the pin, and that is the one the probe breaks. Said out loud rather than reported as a passing probe. Gate on `2853f67e`: `cargo test -p victory-hle xmem::` → 22 passed; `cargo test -p victory-hle` → 108 passed, 0 failed; both `clippy --workspace --all-targets` passes red only on `main`'s three pre-existing lints; `xtask` 4 passed. Not covered: no run in the game, so whether the high address helps anything in practice is unmeasured. `VICTORY_XMEM_PERSISTENT_VIEW` stays off.
THE BUG.
write_physical and page_window reached the physical pages through View::map,
which mapped a fresh view of the section, committed it with
VirtualAlloc(MEM_COMMIT), wrote, and unmapped it in Drop for View. Every write
therefore paid a MapViewOfFile, a commit and an UnmapViewOfFile however few
bytes it touched, and paid it once per run of consecutive pages.

WHY NOT HYPOTHETICAL.
Both functions have exactly one production caller: the DirectStorage
destination path in dstorage.rs, which tries page_window first and falls back
to write_physical. That is the streaming path, called once per in-flight
request on every worker, for the whole world load and for every texture the
title streams while playing. An unmap invalidates that mapping's translation
entries on every processor, so the cost is charged to all cores rather than
to the writer alone. Which writes are hot enough to move a frame rate is a
measurement on hardware this tree cannot reach, and it is not claimed here;
the saving is real per write either way.

THE FIX.
Section keeps one read-write view of its whole range, mapped on first use and
unmapped when the section goes (MappedBase, dropped with the Section), plus
one bit per 64 KiB page recording which pages have been committed through it.
View becomes an enum: Temporary is the old map/commit/unmap path, Persistent
points into the section's view and unmaps nothing when it drops.
Section::ensure_committed commits a run in a single call the first time and
makes no calls after, reusing commit() so a host at its commit limit still
produces the same crash report. Every half of the persistent path that the
host can refuse -- the whole-section map, the commit -- falls back to the
temporary view, so the worst case is exactly today's behaviour.

Mapping is the seam that lets the tests force either path: the switch is read
once per process through a OnceLock, so a test cannot set it for itself.

Opt-in with VICTORY_XMEM_PERSISTENT_VIEW=1, matching the other rollout
switches in this tree. It holds a whole-section mapping for as long as the
section lives, which is 6.1 GiB of address space for the title's largest
allocation; that should be proven by a run before it becomes the default.

SCOPE.
xmem.rs only. The default path is byte-identical to before: View::temporary is
the old View::map body, and every test that existed before this change is the
negative control for the persistent one. Deliberately untouched:
XMemMapPhysicalPages, its placeholder mappings and the unmap() bookkeeping,
which the persistent view does not appear in. Deliberately recorded instead of
tested: the committed_all early return in ensure_committed is a saved call and
not a guard, so it is documented as the one surviving mutation rather than
given a test that could not fail.

Verified: cargo test -p victory-hle xmem:: --locked 16 passed (5 new, all
executing); cargo clippy --workspace --all-targets --locked -- -D warnings
clean; the same with --all-features clean; cargo test -p xtask --locked 4
passed. Mutation probes, all run 2026-11-25 and restored: mark_committed made
a no-op fails the two committed_all assertions and nothing else; the commit
call removed makes the persistent write fault with STATUS_ACCESS_VIOLATION,
which is what proves the commit is load-bearing; the committed_all fast path
forced off leaves 16 of 16 green, which is why it is documented as surviving.

Co-Authored-By: DeepSeek V4.1 Flash <[email protected]>
Review of the commit before this one found five things worth fixing. All of
them are about the evidence the change rests on, not about the mapping.

THE BUG.
Three of them are claims the earlier commit message or comments got wrong.

The comment on `View::bytes` said two writers through the section's one
mapping write "exactly as they did through separate temporary views". They do
not. Through separate virtual views two writes to the same bytes were a data
race; through one shared base two live `&mut [u8]` over overlapping bytes is
undefined behaviour, and the compiler may assume they do not alias. The
precondition -- no two writes in flight cover the same page -- is real, is
stated in `dstorage` for the buffer branch only, and is not stated at all for
the page branch that calls these helpers. The comment now names it as a
precondition rather than calling it unchanged behaviour.

The comment on `ensure_committed` promised that a host at its commit limit
gets "the same crash report". It gets two: the persistent commit fails and
reports, then the caller retries on the temporary path and that commit fails
and reports again. The comment says so now instead of claiming otherwise.

The `end <= self.size` comparison was described as unobserved and was
declared alongside the one surviving mutation, which understated it: it is
the only thing keeping `committed_all`'s page arithmetic inside `committed`.
It was also invisible to all 16 tests, because both "past the section" rows
are refused by the host's own `VirtualAlloc` first. A zero-length range past
the end is now asserted refused; that row the host cannot supply, so deleting
the comparison turns the test red.

WHY NOT HYPOTHETICAL.
`persistent_page_windows_share_one_mapping` passed with every write on the
temporary path. Its only evidence was `persistent_ptr()` returning the base
-- but the test itself calls that, and `View::persistent` calls it before the
commit can fail, so the `OnceLock` was populated whether or not the
persistent path ever ran. Every byte assertion still held, because the
temporary view writes the same pages. A test that cannot see the thing it is
named for is worse than no test, and this one would have been quoted as proof
that the path works.

And with the switch on, nothing said whether it was actually in effect: the
map half warns per section, the commit half said nothing, and no run could
report "these sections are persistent". That is the exact evidence the switch
becoming the default is supposed to rest on.

THE FIX.
`page_bytes`, the helper the pre-existing tests read bytes through, now asks
for `Mapping::Temporary` explicitly, so those tests are the persistent path's
negative control whatever the environment says.

`persistent_page_windows_share_one_mapping` asserts the pages were committed
through the section's mapping after the first window, which only the
persistent path does. Confirmed by probe: with `View::persistent` made to
return `None`, it now fails with "the window fell back to a temporary view"
where it previously passed.

`ensure_committed_refuses_ranges_outside_the_section` gains the zero-length
row, and the surviving-mutation note now names one mutation because only one
remains.

A `fallback_noted` on `Section` warns once per section when a commit through
the persistent view fails, and `View::persistent` says once, at info, that
pages are being written through the persistent view. A run with the switch on
can now show which it did.

SCOPE.
xmem.rs only. The whole-section-vs-chunked decision, the fallback behaviour
and the default path are unchanged. Two claims in the previous message are
corrected here rather than by rewriting it: "the default path is
byte-identical to before" is true of writes, not of allocation -- every
section now allocates its page bitset either way, a few KiB per GiB; and "the
same crash report" is two reports, as above.

Verified: cargo test -p victory-hle xmem:: --locked 16 passed; the two clippy
commands clean with and without --all-features; cargo test -p xtask 4 passed;
cargo test --workspace --locked 500 passed, 0 failed, 7 ignored.

Mutation runs 2026-11-25, all restored: `View::persistent` returning `None`
outright fails both persistent tests ("fell back to a temporary view", "not
mapped whole"); the `end <= self.size` comparison deleted fails the new
zero-length row; the earlier probes (bitset not marked, commit removed) still
behave as recorded.

Co-Authored-By: DeepSeek V4.1 Flash <[email protected]>
A review of this pull request found two things. The second is what makes the
first worth anything.

THE BUG.
`View::bytes()` built one exclusive slice over the whole mapped run, and
`write_physical` and `PageWindow::bytes` narrowed it afterwards. Through the
temporary path that was harmless: every view has an address of its own, so two
narrowed slices sat at two addresses. Through the persistent path the section
has one address, so narrowing after the fact is too late -- two requests
writing different bytes of the same page each formed a slice over the whole
page, and two live `&mut` covering the same bytes is undefined behaviour
whatever the writers actually touch.

And the disjointness was assumed, not enforced. The previous commit's comment
said the title writes each destination alone and pointed at `dstorage` for it.
The repository's own fixture (`zlib_into_pages_in_place_and_across_allocations`)
enqueues two requests whose destinations are the same pages from the same
offset, one through the in-place path and one through the buffered path. There
was nothing to point at.

THE FIX.
`View::bytes_at(from, len)` returns exactly the bytes asked for, at an offset
the caller states, on either path. Nothing here forms a slice over a whole run
any more, so two writers whose bytes do not touch hold disjoint slices even
inside one page.

Disjointness is now enforced rather than believed. `Section` records the byte
ranges its live persistent views hold; `hold` refuses a claim that touches one
of them, and `View::persistent` returns `None` when refused, so the caller
takes a temporary view, which has an address of its own. Writers whose bytes
really do overlap therefore go through two addresses and never through two
slices at one. A view gives its range back when it drops, so a section's bytes
are held only while they are in use.

SCOPE.
xmem.rs only. The temporary path is unchanged in behaviour -- its views were
already distinct addresses, and the narrowing it did implicitly with a slice
range is now explicit in the view. What this does not do: it does not make
overlapping concurrent writes correct. It makes them fall back to the path
that was correct before, so a writer overlapping another still sees whichever
write lands last, exactly as it did previously.

Verified: cargo test -p victory-hle xmem:: --locked 18 passed (2 new, both
executing); cargo clippy --workspace --all-targets --locked -- -D warnings
clean; the same with --all-features clean; cargo test -p xtask --locked 4
passed; cargo test --workspace --locked green across every crate.

Mutation runs 2026-11-25, all restored and each verified to have applied:
`bytes_at` returning the whole run fails with "left: 65536, right: 3"; its end
check removed makes the past-the-end row read on instead of refusing; the
claim skipped makes the second overlapping view `Persistent`, which is the
state that would have shared one address; and the claim never released makes
"free again" fail.

Co-Authored-By: DeepSeek V4.1 Flash <[email protected]>
The persistent view was mapped wherever the host chose, which can be inside
the address range the title itself owns (below 40 bits, decision 0001): a view
there either takes an address the title is about to ask for, or looks like a
title address to the title's own pointer arithmetic. It is now reserved with
VirtualAlloc2's address requirement from 1 TiB up to GetSystemInfo's
lpMaximumApplicationAddress (asked, never assumed) and mapped into that
reservation with MapViewOfFile3. The rule has a name, `above_title_memory`, so
a test can hold it; the host is not trusted to honour it either, and a view
that comes back below the ceiling is released and the slow path taken.

Commit state went from one bit per page to three states per page, because the
bit could not promise anything: DirectStorage workers write these sections
concurrently, and a thread that lost the race to `VirtualAlloc(MEM_COMMIT)`
would store into a page whose commit was still in flight. A page is now FREE,
COMMITTING while one thread commits it, and COMMITTED after the call returned;
a loser spins on COMMITTING instead of writing. Giving up after a bounded
number of spins takes the temporary view, which is always correct, so a thread
that died mid-commit cannot hang the title.

Each page also carries a write generation, bumped after its bytes are written:
release on the bump, acquire on the read, so an unchanged digest is a promise
about the bytes and not only about the counter. `physical_generation_digest`
folds a range into one number for the GPU side to compare, which is what lets
it stop hashing texture memory it already knows (phase F; the digest has no
caller here yet beyond its test, and says so).

Tests: the view lands above the ceiling and the rule refuses one page below
it; eight threads committing the same four uncommitted pages all succeed and
every byte they wrote is there; a digest moves on a physical write and on a
page window's bytes, and an untouched page's does not.

Probes, restored byte-identical: dropping the generation bump after a physical
write fails "a write must show"; dropping the window's bump on drop fails "a
page window's bytes must show"; making `above_title_memory` always true fails
on the page below the ceiling. Removing LowestStartingAddress from the
reservation does NOT fail any test on this host, whose allocator is already
top-down: that requirement is a belt, and the predicate is the pin.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:52:56 +00:00
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-25 12:23 UTC · original

Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present. I merged this PR's head 2853f67 into main f85101b in a throwaway tree (no conflict). cargo clippy --workspace --all-targets --locked -- -D warnings fails with exactly the errors main itself has on this machine (placed_filler_twins, depth_written, a sort; PR #841 fixes them), also with --all-features; this PR adds none. cargo test --workspace --locked: run 1: stopped at indirect::tests::warp_budget_saturates_without_wraparound_and_preserves_draw_fields (the one that fails on main itself most runs here (PR #849 and #847 fix its assertion)); run 2: stopped at indirect::tests::warp_budget_saturates_without_wraparound_and_preserves_draw_fields (the one that fails on main itself most runs here (PR #849 and #847 fix its assertion)) (main alone: 555 passed, 0 failed, 11 ignored). The title was not run, so this says nothing about in-game effect. No game data.

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-25 12:23 UTC · [original](https://github.com/fxceless/victory-fork/pull/872#issuecomment-7162303684) Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present. I merged this PR's head `2853f67` into `main` `f85101b` in a throwaway tree (no conflict). `cargo clippy --workspace --all-targets --locked -- -D warnings` fails with exactly the errors `main` itself has on this machine (`placed_filler_twins`, `depth_written`, a sort; PR #841 fixes them), also with `--all-features`; this PR adds none. `cargo test --workspace --locked`: run 1: stopped at `indirect::tests::warp_budget_saturates_without_wraparound_and_preserves_draw_fields` (the one that fails on `main` itself most runs here (PR #849 and #847 fix its assertion)); run 2: stopped at `indirect::tests::warp_budget_saturates_without_wraparound_and_preserves_draw_fields` (the one that fails on `main` itself most runs here (PR #849 and #847 fix its assertion)) (`main` alone: 555 passed, 0 failed, 11 ignored). The title was not run, so this says nothing about in-game effect. No game data. AI-assisted (Claude Code); no game data.
github-bot force-pushed github-bot/gh-872 from 2853f67ecd to 08aa56ad89 2026-11-26 14:00:04 +00:00 Compare
Author
Owner

@⁠HailLucifer666 on GitHub · 2026-11-26 13:56 UTC · original

Rebased onto the new #231 head (ad67d255); this head is now 1e653717 (--onto so only this PR's commit replayed). One import conflict, resolved as the union (GetSystemInfo/SYSTEM_INFO plus GetCurrentProcess/GetCurrentThread), a stray brace removed from the Drop match, and one new-toolchain lint fixed (let...else exactly equal to ? in the view-size rounding). Verified: cargo check and clippy -D warnings on victory-hle clean; victory-hle xmem:: 24 passed, 0 failed (the 21 from #231 plus this PR's 3).

**@⁠HailLucifer666** on GitHub · 2026-11-26 13:56 UTC · [original](https://github.com/fxceless/victory-fork/pull/872#issuecomment-7194061623) Rebased onto the new `#231` head (`ad67d255`); this head is now `1e653717` (`--onto` so only this PR's commit replayed). One import conflict, resolved as the union (`GetSystemInfo`/`SYSTEM_INFO` plus `GetCurrentProcess`/`GetCurrentThread`), a stray brace removed from the `Drop` match, and one new-toolchain lint fixed (`let...else` exactly equal to `?` in the view-size rounding). Verified: `cargo check` and `clippy -D warnings` on victory-hle clean; `victory-hle xmem::` 24 passed, 0 failed (the 21 from #231 plus this PR's 3).
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/872/head:github-bot/gh-872'
git switch 'github-bot/gh-872'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-872'
git switch 'github-bot/gh-872'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-872'
git switch 'github-bot/gh-872'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-872'
git switch 'main'
git merge --squash 'github-bot/gh-872'
git switch 'main'
git merge --ff-only 'github-bot/gh-872'
git switch 'main'
git merge 'github-bot/gh-872'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.