XMem: coalesce split placeholders before retrying a mapping #1058

Open
gmankab wants to merge 1 commit from upstream-pr/1060 into main
gmankab commented 2026-11-25 17:52:42 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by gmankab on 2026-11-25T17:52:42Z.


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

<!-- greyfinch-sync:fxceless/victory-fork:issue:6610428391 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1060). Originally posted by [gmankab](https://github.com/gmankab) on 2026-11-25T17:52:42Z. --- 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
DoubleD17 commented 2026-11-25 21:05:45 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by DoubleD17 on 2026-11-25T20:25:37Z.

Review: COMMENTED


Independent native Windows x64 check of the placeholder-remap sequence in 875aad664052b852ce54731b1dbe64cd50ca2381 (build 26300, AMD Ryzen 7 5800X). In a standalone fixture I mapped and committed two 64 KiB views of a pagefile-backed section, then unmapped both with MEM_PRESERVE_PLACEHOLDER. A third adjacent view stayed mapped. Retry path at this SHA

The initial split request across the two existing placeholders failed with Win32 error 87. The first 128 KiB MapViewOfFile3 replacement failed with error 487. Coalescing exactly those two placeholders and retrying then mapped at the requested address. Readback gave the expected marker bytes 1 and 42 from the two slots, and the neighboring view still read its original marker 90.

This adds coverage on native Windows for the API sequence. The standalone fixture exercised the Windows calls and did not execute the PR's HLE or measure streaming performance.

AI-assisted.

<!-- greyfinch-sync:fxceless/victory-fork:review:6064459502 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1060#pullrequestreview-6064459502). Originally posted by [DoubleD17](https://github.com/DoubleD17) on 2026-11-25T20:25:37Z. **Review:** COMMENTED --- Independent native Windows x64 check of the placeholder-remap sequence in `875aad664052b852ce54731b1dbe64cd50ca2381` (build 26300, AMD Ryzen 7 5800X). In a standalone fixture I mapped and committed two 64 KiB views of a pagefile-backed section, then unmapped both with `MEM_PRESERVE_PLACEHOLDER`. A third adjacent view stayed mapped. [Retry path at this SHA](https://github.com/fxceless/victory-fork/blob/875aad664052b852ce54731b1dbe64cd50ca2381/crates/victory-hle/src/modules/xmem.rs#L774-L825) The initial split request across the two existing placeholders failed with Win32 error 87. The first 128 KiB `MapViewOfFile3` replacement failed with error 487. Coalescing exactly those two placeholders and retrying then mapped at the requested address. Readback gave the expected marker bytes `1` and `42` from the two slots, and the neighboring view still read its original marker `90`. This adds coverage on native Windows for the API sequence. The standalone fixture exercised the Windows calls and did not execute the PR's HLE or measure streaming performance. AI-assisted.
Solarthis commented 2026-11-25 21:05:55 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Solarthis on 2026-11-25T20:35:06Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7173886900 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/1060#issuecomment-7173886900). Originally posted by [Solarthis](https://github.com/Solarthis) on 2026-11-25T20:35:06Z. --- 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 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/1060:upstream-pr/1060'
git switch 'upstream-pr/1060'

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/1060'
git switch 'upstream-pr/1060'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1060'
git switch 'upstream-pr/1060'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1060'
git switch 'main'
git merge --squash 'upstream-pr/1060'
git switch 'main'
git merge --ff-only 'upstream-pr/1060'
git switch 'main'
git merge 'upstream-pr/1060'
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
fxceless/victory-fork!1058
No description provided.