Skip to content

xmem: map consecutive physical pages as one view, so the retail boot's 100,000-page XMemMapPhysicalPages fits the title's hang watchdog under Wine - #7584

Open
gkissel wants to merge 3 commits into
ItsNotRexed:mainfrom
gkissel:fix/xmem-physical-page-runs
Open

gkissel wants to merge 3 commits into
ItsNotRexed:mainfrom
gkissel:fix/xmem-physical-page-runs

Conversation

@gkissel

@gkissel gkissel commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: RT-671 (#8504 (comment))

What and why

XMemMapPhysicalPages (#6919) maps each 64 KiB page as its own view: a placeholder split plus a MapViewOfFile3 per page. Under Wine that is slow, and it gets slower as views pile up. On the retail boot, one call maps 99,776 pages into a single window (counted with a temporary log, not part of this PR):

  • the first 65,536 pages took 28.6 s, about 0.36 ms a page, and each block of 16,384 pages took longer than the one before (5.9, 6.3, 8.0, 8.4 s);
  • the title has a hang watchdog that ends the process when its main thread hasn't checked in for 30 seconds. The mapping call is still running when it fires, so every run stops there.

The smaller calls before it (16 to 1,600 pages) were each one contiguous run of a single allocation, and so is this one.

The change:

  • Consecutive pages of one allocation (same section, consecutive offsets) are mapped as one view. g_mapped (address → page) becomes g_views (view base → page count, section, first offset, protection).
  • Unmapping or remapping a page inside a larger view splits it. The view is unmapped whole (the placeholder is kept), and the pages on either side are mapped again from the same section and offsets, so they keep their contents. The view holds a reference to the section, so this works even if the pages were freed while mapped. XMemFreePhysicalPages already allows that.
  • XMemMapPhysicalPages unmaps its range first, then maps runs. One behaviour change: when a page number isn't a live page, the pages before it stay mapped as before, but the rest of the range is now left unmapped, not left as it was. The measured callers only test the result for zero.
  • The scatter function and a 0 page entry go through the same split path.

Tested

Linux, Wine 11.21, clang-cl + xwin cross build, GTX 1650 Mobile, own copy of the retail executable (outcomes only):

Related

#7492 and #7499 (cache modifiers in the view protection) touch view_protect, not this code. They should merge with this cleanly, and the integration run above had the same change applied locally.

AI-assisted (Claude Code). No game files, bytes, disassembly or addresses in this PR.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable: no translator change.)

…MemMapPhysicalPages fits the title's hang watchdog under Wine

One view per page cost a placeholder split and a MapViewOfFile3 per 64 KiB page. Under Wine that took
0.36 ms a page and more as views accumulate; the retail boot's 99,776-page call ran past the title's
30-second hang watchdog. Consecutive pages of one allocation now map as one view. A page remapped or
unmapped later splits its view and the neighbours are mapped again from the same section.
Copilot AI balanced review requested due to automatic review settings November 23, 2026 17:07

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@MartinIndra02

Copy link
Copy Markdown

Independent Windows test of 5ba2a7f95: pass.

Setup: Windows 11 x64, MSVC 19.44.35220 (RelWithDebInfo, Windows SDK 10.0.26100.0), no game files.

  • xmem_test.exe: PASS (exit 0).

AI-assisted review/test.

@aslan2142

Copy link
Copy Markdown
Contributor

Independent timing of XMemMapPhysicalPages under Wine 11.22 on Linux (build: clang for mingw; synthetic calls, no game code). Consecutive pages, mapped in one call:

Pages main (bf85ff703, one view per page) this PR's xmem.cpp
1,000 17 ms 0.1 ms
10,000 338 ms 0.6 ms
99,776 (the boot's count, per cynbun) 21.7 s 10.4 ms

At 99,776 pages that's the difference cynbun hit under Wine 11.20: on main the title's startup watchdog ends the process before mapping finishes (#7599 (comment)).

AI-assisted (Claude Code, Claude Opus 5.5).

@dotaku22 dotaku22 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.

I reproduced a remapping regression on native Windows 11 build 26200, MSVC 19.44, Windows SDK 10.0.26100, RelWithDebInfo. This used CPU-only synthetic memory.

In test_physical_pages, after the existing check that the four pages are reserved again, call api.map(window, kPages, pages) a second time. The existing middle-page unmap and remap leave the range split into separate host placeholders. The second whole-run map should return window and preserve the page contents.

  • Parent 203a348cf with the same added check passes, including readback of the first and last pages.
  • PR 5ba2a7f95 returns null on the second whole-run map with Win32 error 87 (ERROR_INVALID_PARAMETER). The stock checks pass.
  • With the inline suggestion, the entire augmented xmem CTest passes, including readback, cleanup, and the existing assertion that a fresh contiguous run uses a single view.

The coalesced size must fit one matching placeholder for MEM_REPLACE_PLACEHOLDER. Microsoft's MapViewOfFile3 documentation

AI-assisted (Codex).

Comment thread runtime/shims/xmem.cpp Outdated
Comment on lines +438 to +446
bool map_view(uintptr_t address, const View& view) {
const SIZE_T size = static_cast<SIZE_T>(view.count * kPhysicalPageSize);
void* const target = reinterpret_cast<void*>(address);
if (!isolate_placeholder(target, size)) return false;
if (!placeholder_api().map_view3(view.section->handle, GetCurrentProcess(), target, view.offset, size,
MEM_REPLACE_PLACEHOLDER, view.protect, nullptr, 0)) {
return false;
}
g_mapped.erase(it);
g_views.emplace(address, view);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A guest run can span several host placeholders after a partial unmap. Splitting the mapping at the host boundaries fixed the reproduction locally. A fresh placeholder still maps as one view.

Suggested change
bool map_view(uintptr_t address, const View& view) {
const SIZE_T size = static_cast<SIZE_T>(view.count * kPhysicalPageSize);
void* const target = reinterpret_cast<void*>(address);
if (!isolate_placeholder(target, size)) return false;
if (!placeholder_api().map_view3(view.section->handle, GetCurrentProcess(), target, view.offset, size,
MEM_REPLACE_PLACEHOLDER, view.protect, nullptr, 0)) {
return false;
}
g_mapped.erase(it);
g_views.emplace(address, view);
bool map_view(uintptr_t address, const View& view) {
// Previous partial unmaps can leave several host placeholders in one guest run.
for (uint64_t mapped = 0; mapped < view.count;) {
const uintptr_t base = address + mapped * kPhysicalPageSize;
void* const target = reinterpret_cast<void*>(base);
MEMORY_BASIC_INFORMATION info{};
if (!VirtualQuery(target, &info, sizeof(info)) || info.State != MEM_RESERVE ||
info.RegionSize < kPhysicalPageSize) {
SetLastError(ERROR_INVALID_ADDRESS);
return false;
}
const uint64_t available = info.RegionSize / kPhysicalPageSize;
const uint64_t remaining = view.count - mapped;
const uint64_t count = available < remaining ? available : remaining;
const SIZE_T size = static_cast<SIZE_T>(count * kPhysicalPageSize);
const uint64_t offset = view.offset + mapped * kPhysicalPageSize;
if ((info.AllocationBase != target || info.RegionSize != size) &&
!VirtualFree(target, size, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER)) {
return false;
}
if (!placeholder_api().map_view3(view.section->handle, GetCurrentProcess(), target, offset, size,
MEM_REPLACE_PLACEHOLDER, view.protect, nullptr, 0)) {
return false;
}
g_views.emplace(base, View{count, view.section, offset, view.protect});
mapped += count;
}

After a partial unmap or remap, a run of consecutive pages can lie across several host placeholders, and
MEM_REPLACE_PLACEHOLDER needs the view to fill one placeholder exactly, so mapping the whole run again failed with
ERROR_INVALID_PARAMETER. map_view now maps one view per placeholder the run spans; a fresh window is still one view.
Found and fixed by dotaku22 in review; xmem_test gains their check (the whole run maps again over a split range).
@gkissel

gkissel commented Nov 23, 2026

Copy link
Copy Markdown
Author

@dotaku22 thanks, confirmed and fixed in 4a7bfd5c4.

  • Your check reproduces under Wine 11.21 too. With it added to test_physical_pages on 5ba2a7f95, the result is FAIL a run maps again over a range an earlier partial unmap split, xmem_test: FAILED (179 checks, 1 failure).
  • map_view now maps one view per host placeholder the run spans, as in your suggestion. One simplification: the split goes through the existing isolate_placeholder, which already skips the VirtualFree when the placeholder matches the range exactly. A fresh window is still one view, and the existing "consecutive pages … one view" check still passes.
  • The test keeps your check, plus an unmap of the remapped run.
  • After the change: xmem_test: OK (179 checks) under Wine 11.21 (clang-cl + xwin). check_no_game_data.py exits 0.

AI-assisted (Claude Code).

@throny throny left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tested 4a7bfd5 on Windows 11, MSVC 14.44, i9-12900K (no SSE4a/CLZERO/MONITORX, has CLWB): pass, with one cost to weigh

  • Build (CMake + Ninja, RelWithDebInfo, -j 6): OK. xmem_test: OK (179 checks). Full ctest -j 6 with the Vulkan driver hidden: 3 of 271 failed, all three at vkCreateInstance because I hid the driver (same at every head I tested today).
  • Native timing (scratch harness linking this head's xmem.cpp, and the merge-base 203a348's, through rt::shims::find; not pushed). A single XMemMapPhysicalPages of consecutive pages takes 16,384 pages: 205.6 → 0.7 ms, and 99,776 pages (the boot's count): 480.7 → 6.3 ms.
  • Contents correct on both builds at both sizes. After a scatter-unmap of 3 pages inside the run, the neighbours keep their contents. Remapping the whole range in reversed page order over the split views shows every page's own marker, and so does remapping in the original order. The final unmap leaves the range reserved again.
  • Cost: unmap_range (runtime/shims/xmem.cpp:469) splits a run by unmapping it whole and remapping both sides. With 99,776 pages, XMemMapPhysicalPagesScatter unmapping 3 single pages inside one run takes 96.3 ms here, versus 0.1 ms on main: about 32 ms per page, growing with the run's size. A title that later frees or replaces single pages inside a big boot-time run pays that per page. A reversed remap of the full range is also slower (842 vs 600 ms). Not a blocker for the boot, which this fixes; worth a follow-up if per-page unmaps show up after boot.

AI-assisted.

…cheap

unmap_range splits a view by unmapping it and remapping both sides; with one view per run
that cost grew with the run (review on ItsNotRexed#7584). Capping views at 64 MiB bounds it.
@gkissel

gkissel commented Nov 23, 2026

Copy link
Copy Markdown
Author

Thanks, good catch. Pushed b170ab9: map_view now caps each host view at 1024 pages (64 MiB), so a single-page unmap inside a long run only remaps the rest of one capped view.

Measured under Wine 11.21 (Linux, Ryzen 7 5800H), with temporary timing in xmem_test (removed before the commit), 2 runs each, on a 99,776-page run:

view cap map whole run unmap 3 single pages remap whole run
none (previous head) 11–15 ms 21–26 ms 12–21 ms
256 pages 74–77 ms 0.3 ms 32 ms
1024 pages (pushed) 26–27 ms 0.5 ms 15–19 ms

1024 keeps the boot map close to the uncapped time and makes the split cost flat. A new check (test_long_run: 1,100 pages, region size, contents after a single-page unmap) fails without the cap and passes with it. xmem_test: OK (186 checks) under Wine. I haven't measured it on native Windows; your numbers there would be welcome.

@morodirule

Copy link
Copy Markdown

Independent check of the approach on Arch Linux + Wine 11.22 (clang-cl 23.1 + xwin cross build): full xmem_test green (186 checks), and a temporary 8192-page map/unmap benchmark (removed afterwards) gives map 0.6 ms / unmap 0.0 ms on this head — same order as the reporter's real-boot numbers, ~4 orders of magnitude under the per-page path (my own measurement of the old path on the same box: 570 ms / 542 ms). The 1024-page cap + per-placeholder spans read as the right call vs a single whole-run view: unmap cost stays bounded and fragmented placeholders are handled without a fallback path. No game files (synthetic pages only). AI-assisted.

@morodirule

Copy link
Copy Markdown

Independent Linux check on Arch + Wine 11.22 (clang-cl 23.1 + xwin): xmem_test 186/186 OK. Plus a head-to-head benchmark of the run approach (temporary 8192-page map/unmap harness, removed after): map 0.6 ms, unmap 0.0 ms here, vs 570 ms / 542 ms for the per-page path measured the same way. Same order as the reporter's 28.6 s real-boot figure, confirming the approach on a second machine. The per-placeholder spans + 1024-page cap are the right call over a single whole-run view where placeholders are fragmented; closing my duplicate #8095 in favor of this one. No game files. AI-assisted.

@isfcrctfs

Copy link
Copy Markdown

Tested b170ab9 on Linux + Wine 11.22-staging (clang-cl 23.1.5 + xwin cross build), 16 cores, no GPU. The head applied cleanly to main@12806e56; xmem_test: OK (186 checks). Negative control (this head's tests against main's xmem.cpp): FAILED (186 checks, 2 failures) — the one-view check and the 1024-page cap check. The "177 checks / four new checks" in the description predates test_long_run; this head adds 13 checks.

Synthetic 64 KiB pages, consecutive pages of one allocation, mapped in one call; ms, median where more than one run (single runs for main at 65k and 99,776):

pages main (one view per page) b170ab9
4,096 296 1.1
16,384 2,818 7.1
65,536 47,000–67,000 30–56
99,776 175,369 59–87

At 99,776 pages that is 175.4 s → 60 ms; the null-array unmap of the same run goes 2,030 ms → 1.8 ms. main cannot finish that call inside the 30 s watchdog, this head returns in 60 ms.

Coverage gaps (behaviour is correct where I exercised it, so this is tests, not code): no check that a freed page number cannot be mapped again (only a never-allocated one), and none for a multi-page partial unmap or a remap inside a live run. I exercised those plus a split after XMemFreePhysicalPages while the pages are still mapped; the neighbours keep their contents (51 independent checks).

AI-assisted.

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.

8 participants