Repository navigation
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
Conversation
…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.
|
Independent Windows test of Setup: Windows 11 x64, MSVC 19.44.35220 (
AI-assisted review/test. |
|
Independent timing of
At 99,776 pages that's the difference cynbun hit under Wine 11.20: on AI-assisted (Claude Code, Claude Opus 5.5). |
dotaku22
left a comment
There was a problem hiding this comment.
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
203a348cfwith the same added check passes, including readback of the first and last pages. - PR
5ba2a7f95returns 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
xmemCTest 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).
| 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); |
There was a problem hiding this comment.
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.
| 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).
|
@dotaku22 thanks, confirmed and fixed in
AI-assisted (Claude Code). |
There was a problem hiding this comment.
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). Fullctest -j 6with the Vulkan driver hidden: 3 of 271 failed, all three atvkCreateInstancebecause 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, throughrt::shims::find; not pushed). A singleXMemMapPhysicalPagesof 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,XMemMapPhysicalPagesScatterunmapping 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.
|
Thanks, good catch. Pushed b170ab9: Measured under Wine 11.21 (Linux, Ryzen 7 5800H), with temporary timing in
1024 keeps the boot map close to the uncapped time and makes the split cost flat. A new check ( |
|
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. |
|
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. |
|
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 Synthetic 64 KiB pages, consecutive pages of one allocation, mapped in one call; ms, median where more than one run (single runs for
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. 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 AI-assisted. |
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 aMapViewOfFile3per 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 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:
g_mapped(address → page) becomesg_views(view base → page count, section, first offset, protection).XMemFreePhysicalPagesalready allows that.XMemMapPhysicalPagesunmaps 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.Tested
Linux, Wine 11.21, clang-cl + xwin cross build, GTX 1650 Mobile, own copy of the retail executable (outcomes only):
xmem_testunder Wine on this branch (based onmain203a348cf):xmem_test: OK (177 checks). There are four new checks: four consecutive pages are one view (VirtualQueryreports one allocation of their size); unmapping the middle page leaves it reserved; the three pages around it keep their contents; and a page maps again into the gap.main'sxmem.cpp:xmem_test: FAILED (177 checks, 1 failure), at "consecutive pages of one allocation are mapped as one view". The split checks pass there too, as they should with one view per page.gta6rt --runon an integration tree (main+ the boot-path PRs, up to d3d12: CreateCommandSignature creates a DISPATCH_MESH signature on a device with mesh shaders (ExecuteIndirect with it not replayed yet) #7330, d3d12_xs: Xbox device slot 61 is CreateSamplerX, routed to the PC CreateSampler (RT-5 stop 20) #7115 and d3d12_xs: a console indirect argument of type 105 is created as DISPATCH (device slot 41, RT-5 stop 19) #7163), one variable changed:XMemMapPhysicalPages;python3 -I scripts/check_no_game_data.py: exit 0.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
docs/design.md, with how it was confirmed. (Not applicable: no translator change.)