XMem: coalesce split placeholders before retrying a mapping #1060

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

@⁠gmankab on GitHub · 2026-11-25 17:52 UTC · original
gmankab:fix/coalesce-physical-placeholders → main · +41 −14 · 1 files · 1 commits · open

This fixes a physical-page mapping case relevant to the measured streaming remap bottleneck. After two adjacent one-page views are unmapped, Windows leaves two placeholders. The runtime may then request one view of consecutive physical pages across both slots. The replacement requires a single placeholder with the same address and size.

A small Wine probe reproduced the failure with the split placeholders. After explicitly coalescing them, the same two-page mapping succeeded and both bytes read back correctly. The patch retries a failed multi-page replacement only after coalescing the exact range. The runtime regression exercises XMemMapPhysicalPages with two distinct byte values.

All 12 xmem tests passed. The patch covers correctness of this remap sequence. The impact on in-game streaming times is still unmeasured.

Microsoft's placeholder requirements for MapViewOfFile3

Tested-on: Fedora Linux 45 x86_64, Wine in the Victory Flatpak, DRM devices hidden
Assisted-by: gpt-6.2 sol xhigh

**@⁠gmankab** on GitHub · 2026-11-25 17:52 UTC · [original](https://github.com/fxceless/victory-fork/pull/1060) `gmankab:fix/coalesce-physical-placeholders` → `main` · +41 −14 · 1 files · 1 commits · open This fixes a physical-page mapping case relevant to the measured streaming remap bottleneck. After two adjacent one-page views are unmapped, Windows leaves two placeholders. The runtime may then request one view of consecutive physical pages across both slots. The replacement requires a single placeholder with the same address and size. A small Wine probe reproduced the failure with the split placeholders. After explicitly coalescing them, the same two-page mapping succeeded and both bytes read back correctly. The patch retries a failed multi-page replacement only after coalescing the exact range. The runtime regression exercises `XMemMapPhysicalPages` with two distinct byte values. All 12 `xmem` tests passed. The patch covers correctness of this remap sequence. The impact on in-game streaming times is still unmeasured. [Microsoft's placeholder requirements for MapViewOfFile3](https://learn.microsoft.com/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffile3) Tested-on: Fedora Linux 45 x86_64, Wine in the Victory Flatpak, DRM devices hidden Assisted-by: gpt-6.2 sol xhigh
Unmapping adjacent one-page file views returns separate placeholders.
A later request can map consecutive physical pages over both addresses,
but MapViewOfFile3 requires a single placeholder of the same size as the
replacement. The former preparation only split a placeholder, so that
sequence can leave the destination unmapped.

If a multi-page replacement fails, coalesce the exact placeholder
range and retry only when the merge succeeds. Extend the physical-page
test to write distinct values and read back both pages after replacing
the adjacent old slots.

Tested-on: Fedora Linux 45 x86_64, Wine in the Victory Flatpak, DRM devices hidden
Assisted-by: gpt-6.2 sol xhigh
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:56:53 +00:00
Author
Owner

@⁠Solarthis on GitHub · 2026-11-25 20:35 UTC · original

The mechanism this retry exists for, on the record, and the three things it does not fix.

A one-page remap into an N-page view asks the kernel for two things it refuses: VirtualFree(base, PAGE, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER) where base is inside the view (the unmap acts on the whole view, and the sub-range is not itself a placeholder), then MapViewOfFile3(…, MEM_REPLACE_PLACEHOLDER, base, PAGE, …) where the placeholder is N pages (a placeholder is replaced only at exactly its own size). Both calls fail and nothing stays mapped: a hole. A placeholder is PAGE_NOACCESS, so the CPU faults and a host-pointer import reads zeros (black); where the failure is silent the read falls through into the neighbouring page's offset (red/garbage). First one-page remap into any batched view, 3 launches of 3.

Coalescing at the exact range fixes the kernel side, and #1063 needs it. What is left over is per view rather than per page:

  • inplace::views_gone drops the whole view record on one page's remap; cover() falls to None/Holes, and #606's re-upload then covers all N pages.
  • AllocationBase moves to the run base — #1063's own test asserts it — so anything keyed on it (census, import windows) widens to the run.
  • The title maps and then DirectStorage fills. A view made before the fill reads zeros.

The 11–13% coalesce refusals tikD1337 reports on #1063 are that same rule seen at the ends: a coalesce takes whole placeholders only, so a leftover piece of a larger view at either end refuses it. Splitting both ends first is the right fix.

Our runtime (a separate implementation of the same API surface) keeps a page state victory has not got — pending, recorded with no view. It coalesces and isolates the exact byte range before every map, and a one-page remap inside a first-use view demotes the view instead: one unmap, the survivors pending, a view again on use. View length then costs a remap nothing and the break cannot happen. Covered by a test that replays the title's own order, mid-run remap included, after which a fill lands in the run; xmem 259/259.

**@⁠Solarthis** on GitHub · 2026-11-25 20:35 UTC · [original](https://github.com/fxceless/victory-fork/pull/1060#issuecomment-7173886900) The mechanism this retry exists for, on the record, and the three things it does not fix. A one-page remap into an N-page view asks the kernel for two things it refuses: `VirtualFree(base, PAGE, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER)` where `base` is inside the view (the unmap acts on the whole view, and the sub-range is not itself a placeholder), then `MapViewOfFile3(…, MEM_REPLACE_PLACEHOLDER, base, PAGE, …)` where the placeholder is N pages (a placeholder is replaced only at exactly its own size). Both calls fail and nothing stays mapped: a hole. A placeholder is `PAGE_NOACCESS`, so the CPU faults and a host-pointer import reads zeros (black); where the failure is silent the read falls through into the neighbouring page's offset (red/garbage). First one-page remap into any batched view, 3 launches of 3. Coalescing at the exact range fixes the kernel side, and #1063 needs it. What is left over is per view rather than per page: - `inplace::views_gone` drops the whole view record on one page's remap; `cover()` falls to `None`/`Holes`, and #606's re-upload then covers all N pages. - `AllocationBase` moves to the run base — #1063's own test asserts it — so anything keyed on it (census, import windows) widens to the run. - The title maps and then DirectStorage fills. A view made before the fill reads zeros. The 11–13% coalesce refusals tikD1337 reports on #1063 are that same rule seen at the ends: a coalesce takes whole placeholders only, so a leftover piece of a larger view at either end refuses it. Splitting both ends first is the right fix. Our runtime (a separate implementation of the same API surface) keeps a page state victory has not got — pending, recorded with no view. It coalesces and isolates the exact byte range before every map, and a one-page remap inside a first-use view demotes the view instead: one unmap, the survivors pending, a view again on use. View length then costs a remap nothing and the break cannot happen. Covered by a test that replays the title's own order, mid-run remap included, after which a fill lands in the run; xmem 259/259.
This pull request has changes conflicting with the target branch.
  • crates/victory-hle/src/modules/xmem.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1060/head:github-bot/gh-1060'
git switch 'github-bot/gh-1060'

Merge

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