Skip to content

HLE: keep one view of a physical-page section instead of a view per write (VICTORY_XMEM_PERSISTENT_VIEW=1) - #231

Open
HailLucifer666 wants to merge 3 commits into
fxceless:mainfrom
HailLucifer666:perf/xmem-persistent-section-views
Open

HailLucifer666 wants to merge 3 commits into
fxceless:mainfrom
HailLucifer666:perf/xmem-persistent-section-views

Conversation

@HailLucifer666

Copy link
Copy Markdown

Problem

  • write_physical and page_window reach the physical pages through View::map, which maps a view of the section, commits it with VirtualAlloc(MEM_COMMIT), writes, then unmaps it in Drop for View. Every write pays a MapViewOfFile, a commit and an UnmapViewOfFile, once per run of consecutive pages, however few bytes it touches.
  • page_window and write_physical 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 streamed while playing. An unmap invalidates that mapping's translations on every processor, so the cost is charged to all cores rather than to the writer alone.

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 are already 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 the host can refuse — the whole-section map, the commit — falls back to View::temporary, so the worst case is today's behaviour. Mapping is the seam that lets tests force either path, since the switch is read once per process.
  • Opt-in with VICTORY_XMEM_PERSISTENT_VIEW=1, matching the other rollout switches here. It holds a whole-section mapping for the section's life, which is 6.1 GiB of address space for the title's largest allocation, so it should be proven by a live run before it becomes the default. No frame-rate claim is made here — this tree cannot measure that.

Verification

  • cargo test -p victory-hle xmem:: --locked — 16 passed (5 new, all executing: parse_persistent_views_accepts_only_an_explicit_yes, persistent_views_write_and_commit_once, persistent_page_windows_share_one_mapping, temporary_views_never_map_the_section_whole, ensure_committed_refuses_ranges_outside_the_section).
  • cargo clippy --workspace --all-targets --locked -- -D warnings — clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings — clean
  • cargo test -p xtask --locked — 4/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; removing the commit call makes the persistent write fault with STATUS_ACCESS_VIOLATION, which proves the commit is load-bearing; forcing the committed_all fast path off leaves 16/16 green, so it is documented as the one surviving mutation rather than given a test that could not fail.
  • The default path is byte-identical to before (View::temporary is the old View::map body), so every pre-existing test is the negative control for the persistent one.

Notes for review

  • Deliberately untouched: XMemMapPhysicalPages, its placeholders, and the unmap() bookkeeping — the persistent view is not in physical.views, so remapping and unmapping behave exactly as before.
  • Merge adjacency: Texture guard: see writes through every view of its pages (boot video glitch) #115 also edits write_physical and the PageWindow fields; this touches PageWindow only by changing the type of its view field. Whichever lands second rebases cheaply.

@avt0m4t48 avt0m4t48 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2. Borrow only the requested byte range in a persistent view. On 5faabc4, View::bytes() constructs an exclusive slice over every page in the mapped run. PageWindow::bytes() and the buffered write path then narrow it. Because the persistent view shares one virtual address, independent DirectStorage requests writing disjoint ranges of the same page can still form overlapping mutable references. The workers execute outside the scheduler lock. View · Scheduler · Rust slice safety contract

Could the view take a checked offset and length and create the mutable slice only over those bytes? Actual overlapping writes would still need an enforced exclusion rule before using one address for both. The existing DirectStorage fixture submits overlapping destinations through the in-place and buffered paths. This is a source-review finding. Existing fixture

@HailLucifer666

Copy link
Copy Markdown
Author

Thanks — this was right, and the second half of it was the more important half.

View::bytes() formed its slice over the whole run, and only write_physical / PageWindow::bytes narrowed it afterwards. Through the temporary path that was harmless, because every view has an address of its own. Through the persistent path the section has one address, so narrowing after the fact was too late: two requests writing different bytes of one page each held a slice over the whole page.

Both halves are now done, in 8ad1bd62:

  1. View::bytes_at(from, len) takes the offset and length and returns exactly those bytes, on either path. Nothing in the module forms a slice over a whole run any more, so writers whose bytes do not touch hold disjoint slices even inside one page.

  2. The exclusion rule is enforced, not assumed. Section records the byte ranges its live persistent views hold; hold refuses a claim that touches a live one, and View::persistent returns None when refused — so the caller lands on 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 base. A view gives its range back when it drops.

You were also right that I had nothing to point at. I had written that the title "writes each destination alone" and cited dstorage, but the page-destination branch states only a size fact — and zlib_into_pages_in_place_and_across_allocations enqueues two requests with the same destination pages from the same offset, one through the in-place path and one through the buffered path. That is the case the claim rule now handles.

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. Making that correct would need an ordering guarantee on the title's side that I cannot verify from here.

Verification (all local; the fork's CI runs sit at action_required until a maintainer approves them):

  • cargo test -p victory-hle xmem:: --locked — 18 passed, including two new tests: a_view_slice_covers_only_the_bytes_asked_for and a_second_overlapping_persistent_view_falls_back_to_a_temporary_one.
  • clippy -D warnings clean, with and without --all-features; cargo test -p xtask 4 passed; cargo test --workspace --locked green.
  • Mutation probes, each verified applied and restored: bytes_at returning the whole run fails with left: 65536, right: 3; its end check removed makes the past-the-end case read on instead of refusing; skipping the claim makes the second overlapping view Persistent; never releasing it makes the re-claim fail.

One residual I should name rather than leave implied: a leaked PageWindow would hold its range for good, so later writes to those bytes would keep taking the temporary path — slower and correct, degrading toward the old behaviour rather than toward unsoundness.

HailLucifer666 and others added 3 commits November 25, 2026 15:09
…rite (VICTORY_XMEM_PERSISTENT_VIEW=1)

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 <noreply@deepseek.com>
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 <noreply@deepseek.com>
…t touch

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 <noreply@deepseek.com>
@HailLucifer666

Copy link
Copy Markdown
Author

Your P2 is fixed on ec89292d, in both halves you asked for.

The range. View::bytes_at(from, len) builds the mutable slice over [from, from + len) and nothing else — a checked from.checked_add(len) <= whole first, so a range that does not lie inside the view is a panic with the numbers in it rather than a slice over someone else's pages. Two independent DirectStorage requests writing disjoint ranges of the same page now get disjoint slices even though they lie in one page, which the old whole-run slice could not give them.

The exclusion. You were right that disjoint bytes are not enough on their own — the same virtual address shared by two lifetimes still needs a rule. Section::hold now refuses a second live persistent view whose bytes touch an existing one, and that request falls back to a temporary copy instead (a_second_overlapping_persistent_view_falls_back_to_a_temporary_one). So actual overlaps are excluded rather than merely being unlikely, and only ranges that never touch share the mapping.

Tests: a_view_slice_covers_only_the_bytes_asked_for pins the range; the fallback test pins the refusal. cargo test -p victory-hle --lib xmem::tests → 19 passed, 0 failed.

Probe: hand back the whole run again (from_raw_parts_mut(ptr, whole), the old shape) → six tests fail, including the range one and the write path; restored byte-identical.

Still true, and worth saying so that it is not read as covered: this is proven against the simulated fixtures only. The DirectStorage workers run outside the scheduler lock, and the only thing that can confirm the ordering in production is the title running a real streaming load with VICTORY_XMEM_PERSIST=1.

KomanderDG88 added a commit to KomanderDG88/VIniVIdiVIci that referenced this pull request Nov 25, 2026
…less#868), one view per section (fxceless#231)

- Input (diagnostics builds): a key of VICTORY_KEYS is held until the
  title's reads have seen it down, with repeats and a log line per press.
- fxceless#868 (Chipsrain, iGoldenNinja): VICTORY_CENSUS=1
  logs what each card copy sends, compares and fetches back;
  VICTORY_SKIP_WC_BACK=1 is an experiment that leaves on the card what
  shaders wrote to write-combining title memory. Both off.
- fxceless#231 (HailLucifer666):
  VICTORY_XMEM_PERSISTENT_VIEW=1 keeps one view of a physical-page
  section instead of a view per write. Off.

Nothing changes with the switches unset. Tested as ce18a1a: victory-gpu
library suite three runs clean (198 passed, 5 ignored), victory-hle 116
of 116 with and without diagnostics. The build measured next is of that
commit; this merge differs from it in the README only.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
KomanderDG88 added a commit to KomanderDG88/VIniVIdiVIci that referenced this pull request Nov 25, 2026
…ess#231) and scripted keys held until read

Three rows under what this fork adds, all off by default or diagnostics
only, none measured here yet. English and Spanish.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants