victory-hle(xmem): coalesce contiguous 64 KiB MapViewOfFile3 runs and add lock-free VEH retry around split-and-remap #2425

Open
github-bot wants to merge 2 commits from github-bot/gh-2425 into main AGit
Owner

@⁠faycalbaklouti on GitHub · 2026-11-25 13:14 UTC · original
faycalbaklouti:fix/xmem-coalesce-and-split-veh-retry → main · +793 −29 · 2 files · 2 commits · open

Target upstream: Marvisak/VIctory (also applicable to Belobug/VIctory-Belobug-AMD and fxceless/victory-fork)

Problem

  1. Mapping, scattering, and unmapping physical memory one 64 KiB page at a time via MapViewOfFile3 creates one OS VMA per 64 KiB page, rapidly exhausting vm.max_map_count on Linux/Wine and slowing down large allocations.
  2. When a coalesced multi-page view must be partially unmapped or remapped (split-and-remap), there is a brief window where the old view is unmapped before replacement sub-views are mapped back; concurrent title threads reading/writing untouched pages in that window can fault with an access violation.

Solution

  • Patch 1: Coalesce contiguous physical page runs in map_alias, scatter, and unmap into single MapViewOfFile3 / UnmapViewOfFile calls.
  • Patch 2: Add a lock-free VEH retry handler (on_split_fault) around the coalesced view split-and-remap window so concurrent accesses to untouched sibling pages cleanly spin/retry until the replacement sub-view is mapped.

Verification

  • Both patches apply cleanly in sequence with git apply --check on Marvisak/VIctory main (direct zero-conflict apply).

About our fork & censorship-resistant preservation effort:
This fix comes from gta6-vi-port, our open-source Linux (GE-Proton) + NVIDIA RTX 4090 fork of VIctory and peer-to-peer Radicle archival effort (mirroring 92+ community porting repositories across public Radicle seeds so open-source porting research survives centralized takedowns; open-source code only, no game files/keys/binaries).

**@⁠faycalbaklouti** on GitHub · 2026-11-25 13:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/2425) `faycalbaklouti:fix/xmem-coalesce-and-split-veh-retry` → `main` · +793 −29 · 2 files · 2 commits · open **Target upstream:** `Marvisak/VIctory` (also applicable to `Belobug/VIctory-Belobug-AMD` and `fxceless/victory-fork`) ## Problem 1. Mapping, scattering, and unmapping physical memory one 64 KiB page at a time via `MapViewOfFile3` creates one OS VMA per 64 KiB page, rapidly exhausting `vm.max_map_count` on Linux/Wine and slowing down large allocations. 2. When a coalesced multi-page view must be partially unmapped or remapped (split-and-remap), there is a brief window where the old view is unmapped before replacement sub-views are mapped back; concurrent title threads reading/writing untouched pages in that window can fault with an access violation. ## Solution - **Patch 1:** Coalesce contiguous physical page runs in `map_alias`, scatter, and unmap into single `MapViewOfFile3` / `UnmapViewOfFile` calls. - **Patch 2:** Add a lock-free VEH retry handler (`on_split_fault`) around the coalesced view split-and-remap window so concurrent accesses to untouched sibling pages cleanly spin/retry until the replacement sub-view is mapped. ## Verification - Both patches apply cleanly in sequence with `git apply --check` on `Marvisak/VIctory` `main` (direct zero-conflict apply). --- **About our fork & censorship-resistant preservation effort:** This fix comes from **[gta6-vi-port](https://github.com/go2afri/gta6-vi-port)**, our open-source Linux (GE-Proton) + NVIDIA RTX 4090 fork of VIctory and peer-to-peer Radicle archival effort (mirroring 92+ community porting repositories across public Radicle seeds so open-source porting research survives centralized takedowns; open-source code only, no game files/keys/binaries). - **Canonical Radicle repo:** https://radicle.network/nodes/rosa.radicle.network/rad:z39ofPhQC1siudH9BF3xsmAMmUqxc (`rad:z39ofPhQC1siudH9BF3xsmAMmUqxc`) - **Live 1080p development stream:** https://vdo.ninja/?view=gta6rtx4090live1080p60 (Tor v3 `.onion` VP9+Opus viewer: `http://kwnoaeecpmxd6eetqfa7fxlymvxnh5du2i6wxr44t4mc7cgch5pw23id.onion/`)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:05:25 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 15:32 UTC · original

Automated triage for the maintainers.

Proposed close. This maps runs of contiguous physical pages as one view in crates/victory-hle/src/modules/xmem.rs (map_alias, scatter, unmap). Because unmap must then split those views, title memory is briefly unmapped while other threads may use it, and the PR adds a first-chance exception handler (on_split_fault) that spins and resumes faults inside that window or a ring of recent splits.

  • It adds a race main does not have. main maps one view per page and never unmaps live title memory to remap part of it; the new handler only papers over the window, and it runs ahead of the existing guard handler for every access violation in the process.
  • Motivation is outside the target. The stated reason is vm.max_map_count under Linux/Wine; no problem on Windows is shown.
  • It adds apps/victory/src/private_heap.rs with no mod declaration (that file belongs to #2424), and CI is failing.

A measured Windows-side reason to coalesce views, and a split that never leaves live memory unmapped, would change this verdict.

This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 15:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/2425#issuecomment-7167002743) <!-- victory-triage v1 head=440d0115f5124352c55033c851f6f63763fa6c91 verdict=proposed-close at=2026-11-25T15:36:00Z --> Automated triage for the maintainers. **Proposed close.** This maps runs of contiguous physical pages as one view in `crates/victory-hle/src/modules/xmem.rs` (`map_alias`, scatter, `unmap`). Because `unmap` must then split those views, title memory is briefly unmapped while other threads may use it, and the PR adds a first-chance exception handler (`on_split_fault`) that spins and resumes faults inside that window or a ring of recent splits. - **It adds a race `main` does not have.** `main` maps one view per page and never unmaps live title memory to remap part of it; the new handler only papers over the window, and it runs ahead of the existing guard handler for every access violation in the process. - **Motivation is outside the target.** The stated reason is `vm.max_map_count` under Linux/Wine; no problem on Windows is shown. - It adds `apps/victory/src/private_heap.rs` with no `mod` declaration (that file belongs to #2424), and CI is failing. A measured Windows-side reason to coalesce views, and a split that never leaves live memory unmapped, would change this verdict. This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the label. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 16:56 UTC · original

Both mechanisms hold up to close reading:

Coalescing — run detection requires same-section Arc + contiguous index, chunk_len stays inside both the source view and len, map_run falls back to per-page when a single view can't cover a reservation boundary, and MEM_COALESCE_PLACEHOLDERS is only applied to spans of ≥2 adjacent MEM_RESERVE placeholder regions (the kernel call itself rejects non-placeholder spans, so a mixed span just stays split → per-page fallback — safe degradation). The test pins the split-then-realise → single-view cycle end to end.

Split VEH — window opens before UnmapViewOfFile2 (fault can't slip in front), end writes HI before LO so a reader's lo < hi can't see a stale half-open range, the RECENT_SPLITS ring covers faults arriving microseconds after remap, and VirtualQuery + prot check before CONTINUE_EXECUTION keeps genuinely-unmapped addresses propagating. Allocation-free/lock-free as a VEH must be.

One robustness note: the spin loop in on_split_fault is unbounded — if the remapping thread were killed or panicked between begin/end, a faulting thread spins forever (silent deadlock). A bounded spin (say ~10 ms) then CONTINUE_SEARCH keeps the retry semantics for real remaps while making a stuck window a crash instead of a hang. Worth adding before this merges.

**@⁠kotekompotek** on GitHub · 2026-11-25 16:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/2425#issuecomment-7168760447) Both mechanisms hold up to close reading: **Coalescing** — run detection requires same-section `Arc` + contiguous `index`, `chunk_len` stays inside both the source view and `len`, `map_run` falls back to per-page when a single view can't cover a reservation boundary, and `MEM_COALESCE_PLACEHOLDERS` is only applied to spans of ≥2 adjacent `MEM_RESERVE` placeholder regions (the kernel call itself rejects non-placeholder spans, so a mixed span just stays split → per-page fallback — safe degradation). The test pins the split-then-realise → single-view cycle end to end. **Split VEH** — window opens before `UnmapViewOfFile2` (fault can't slip in front), `end` writes HI before LO so a reader's `lo < hi` can't see a stale half-open range, the RECENT_SPLITS ring covers faults arriving microseconds after remap, and `VirtualQuery` + prot check before `CONTINUE_EXECUTION` keeps genuinely-unmapped addresses propagating. Allocation-free/lock-free as a VEH must be. One robustness note: the spin loop in `on_split_fault` is unbounded — if the remapping thread were killed or panicked between `begin`/`end`, a faulting thread spins forever (silent deadlock). A bounded spin (say ~10 ms) then `CONTINUE_SEARCH` keeps the retry semantics for real remaps while making a stuck window a crash instead of a hang. Worth adding before this merges.
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 16:59 UTC · original

Read both halves carefully. The coalescing logic is sound — run detection checks Arc::ptr_eq + index contiguity, map_run degrades to per-page on reservation boundaries, and the scatter batching preserves the map/unmap interleaving correctly. A few things need attention before merge:

1. private_heap.rs is dead code. Nothing declares mod private_heap and no #[global_allocator] installs PrivateHeap — the file compiles to nothing. It's also not mentioned in the PR body. If the intent is the VEH-safe allocator the doc comment describes (which is a real concern for on_split_fault's future growth), wire it in and explain it; otherwise drop the file.

2. Unbounded spin in on_split_fault. A faulting thread spins with no yield while the remapping thread (which holds the physical mutex) finishes. On a busy 4-core box a higher-priority faulting thread can livelock the remapper for a quantum at a time — and if the remapper ever dies mid-window, this spins forever. Suggest a bounded spin then SwitchToThread/YieldProcessor backoff.

3. coalesce_placeholders never checks AllocationBase. It coalesces any run of adjacent MEM_RESERVE regions — including a plain VirtualAlloc reservation parked next to our placeholders. The MEM_COALESCE_PLACEHOLDERS call fails atomically on non-placeholder spans, so today it just silently fails (result ignored → per-page fallback, safe). Still, comparing mbi.AllocationBase while extending span_end is a one-line tightening that avoids relying on the kernel rejecting mixed spans.

4. Sticky ring + masked protect bits. RECENT_SPLITS entries are never cleared — any address that was ever in a split window stays "recently split" forever (16 slots, overwritten). Combined with prot & 0xff stripping PAGE_GUARD/modifier bits, a guard-page fault inside a stale range would be swallowed by EXCEPTION_CONTINUE_EXECUTION before the intended handler sees it. Today nothing in the process uses PAGE_GUARD (gpu write-watch uses PAGE_READONLY, which correctly fails the write-prot check), but worth a defensive Protect & PAGE_GUARD != 0 -> CONTINUE_SEARCH to keep the handler a pure retry mechanism rather than a fault-eater for future protection tricks.

**@⁠kotekompotek** on GitHub · 2026-11-25 16:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/2425#issuecomment-7168840585) Read both halves carefully. The coalescing logic is sound — run detection checks `Arc::ptr_eq` + index contiguity, `map_run` degrades to per-page on reservation boundaries, and the scatter batching preserves the map/unmap interleaving correctly. A few things need attention before merge: **1. `private_heap.rs` is dead code.** Nothing declares `mod private_heap` and no `#[global_allocator]` installs `PrivateHeap` — the file compiles to nothing. It's also not mentioned in the PR body. If the intent is the VEH-safe allocator the doc comment describes (which is a real concern for `on_split_fault`'s future growth), wire it in and explain it; otherwise drop the file. **2. Unbounded spin in `on_split_fault`.** A faulting thread spins with no yield while the remapping thread (which holds the `physical` mutex) finishes. On a busy 4-core box a higher-priority faulting thread can livelock the remapper for a quantum at a time — and if the remapper ever dies mid-window, this spins forever. Suggest a bounded spin then `SwitchToThread`/`YieldProcessor` backoff. **3. `coalesce_placeholders` never checks `AllocationBase`.** It coalesces any run of adjacent `MEM_RESERVE` regions — including a plain `VirtualAlloc` reservation parked next to our placeholders. The `MEM_COALESCE_PLACEHOLDERS` call fails atomically on non-placeholder spans, so today it just silently fails (result ignored → per-page fallback, safe). Still, comparing `mbi.AllocationBase` while extending `span_end` is a one-line tightening that avoids relying on the kernel rejecting mixed spans. **4. Sticky ring + masked protect bits.** `RECENT_SPLITS` entries are never cleared — any address that was ever in a split window stays "recently split" forever (16 slots, overwritten). Combined with `prot & 0xff` stripping `PAGE_GUARD`/modifier bits, a guard-page fault inside a stale range would be swallowed by `EXCEPTION_CONTINUE_EXECUTION` before the intended handler sees it. Today nothing in the process uses `PAGE_GUARD` (gpu write-watch uses `PAGE_READONLY`, which correctly fails the write-prot check), but worth a defensive `Protect & PAGE_GUARD != 0 -> CONTINUE_SEARCH` to keep the handler a pure retry mechanism rather than a fault-eater for future protection tricks.
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/2425/head:github-bot/gh-2425'
git switch 'github-bot/gh-2425'

Merge

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