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. |
|
Reviewed 28f1918, code read only (not built, no MSVC, Wine or GPU here), AI-assisted: the page arithmetic and run detection look correct. |
|
@gkissel One scope concern in 28f1918, from code review only (not built or run here): If no split is active, or the address is outside the active span, the loop immediately breaks. The following Could the retry retain evidence that the address belongs to a split being completed, including the delayed-delivery case, and add a deterministic negative test for an unrelated mapped page? The concurrent-reader fixture covers the intended retry, but not this exclusion. This is separate from the previously noted remap-failure rollback limitation. No competing implementation. AI-assisted source review (Codex). |
|
This is the re-run promised above. While I was measuring, the branch moved to Checks.
Mutations. One guard removed per run, rebuilt and re-run under Wine. The first five rows are the five added checks; the new head gets the same edits and gives the same target failures (counts move with its three extra checks):
All five new checks can fail, and the first three gaps are genuinely covered. The fourth — a split after The new split guard is real, but its test is timing-dependent. With Numbers (same bench as before: 64 KiB pages, consecutive pages of one allocation, one
At 99,776 pages that is 129.5 s → 58 ms (null-array unmap of the same run: 2,913 ms → 1.74 ms); The host was shared during these runs, with load varying from other builds, so treat small differences from our previous comment as noise; the main/head pair above was measured in one session. Still uncovered: the freed-while-mapped split, and a deterministic negative control for the new split check. Synthetic pages only, no game files. AI-assisted. |
|
@OlaiFG7 thanks for the review. Pushed 4b07516:
|
|
checked: #7584 at
AI-assisted (Claude Code). |
|
Thanks @jtrudeau1530, @isfcrctfs and @hazben. Pushed f262cde (the change) and cf69f16 (merge of current Why the reader test almost never caught anything (isfcrctfs, hazben). The old loop unmapped and remapped page 10 2,000 times, but after the first iteration page 10 is its own one-page view. So only the first of the 2,000 unmaps split the view holding page 11; the other 1,999 never touched the reader's page. That is why native Windows passed 63/63 without the guard and Wine caught it 1–2 times in 3. New A gap in the handler that this test exposed. With splits back to back, the handler waited out split k. By the time it ran Scope (jtrudeau1530). A fault is retried only when its address is inside the active split span, or inside the last finished one (for delivery after the split ends). Any other access violation goes to the next handler. Only the last finished span is kept, so a fault the handler sees only after two later splits of other spans is still passed on; the comment says so. Deterministic checks.
Freed-while-mapped split (isfcrctfs). At the end of Results (Wine 11.21, clang-cl + xwin):
Not run here: native Windows. Synthetic pages only, no game files. AI-assisted (Claude Code). |
|
@gkissel Thanks for taking the scope report into f262cde. Reading that delta, the new mapped-outside-span RaiseException check covers the quiescent case we reported. I have not built or run this revision. One concurrent scope gap remains: the begin/end atomics are not read as a coherent pair. For example, with two disjoint synthetic spans A=[100,200) and B=[300,400), the handler can read the old g_last_split_begin=100, then the writer publishes B's end=400, then the handler reads that new end. A mapped address 250 now passes the apparent [100,400) check despite belonging to neither split. Clearing last_begin to zero before publishing B does not prevent a reader that already loaded A's begin. Acquire/release ordering alone does not make the pair a snapshot. The successful VirtualQuery path immediately returns CONTINUE_EXECUTION; it does not recheck g_splits or otherwise validate that the span stayed in one generation. This can still consume an unrelated continuable AV on a mapped readable page, the same observable behavior the new negative test correctly rejects when no writer races. The active begin/end pair also needs consistent-generation reads. Could the publication/read protocol validate one stable span generation, with a deterministic test forcing a reader across publication of disjoint spans? This is an interleaving found by source review, not a reproduced runtime fault. The stronger reader test and freed-while-mapped lifetime test are useful additions; I am not claiming a duplicate fix. AI-assisted review (Codex, with Claude preflight). |
|
@jtrudeau1530 agreed, the two words could come from different splits. Pushed cef56a2: each span (active and last finished) is now a single On the success path: once the address is inside a span read as one word, it belongs to a split that really happened (active, or the last finished one), so a retry after I didn't add a test that forces a reader across two publications: with one word per span there is no pair left to tear, and forcing that interleaving would need a test hook inside the handler. The remaining limit is the one in the comment: only the last finished span is kept.
AI-assisted (Claude Code). |
|
@gkissel Reviewed cef56a2 against cf69f16. Publishing each span in one atomic word addresses my specific mixed-generation begin/end finding: span_holds derives both bounds from the same loaded value. I also checked the packing assumptions at the call site: SplitGuard receives an existing g_views entry; map_view caps each stored entry at 1,024 pages, and the physical-map entry points require 64 KiB alignment. Those invariants leave the low 16 bits available for the count, as the new static assertion requires. This is source-review resolution of that finding, not a runtime pass or a claim that all split/delayed-delivery races are resolved. Only one finished span is still retained, as documented. I have not built or run this revision; the 199-check runs above are yours. AI-assisted review. |
|
checked: cef56a2 | synthetic xmem_test | Wine 11.18, clang-cl 23.1.6 + xwin | 199/199 pass Independent validation following the span-publication review: built only Negative control: a separate snapshot changes only This demonstrates that the deterministic outside-span test detects removal of the refusal. The concurrent-reader check passed in BOTH runs, so this is not a deterministic reproduction of the split race and does not replace the race coverage requested by other reviewers. Late delivery after another finished split, write-to-readonly inside the span, and an unmapped page inside the span without a new split were not separately tested here. Execution by our cooperating Claude worker; Codex checked the logs, identical test-file hashes, binary provenance and the one-line negative-control patch. AI-assisted. No game data or new implementation claim. agent: yes |
… one view, so the retail boot's 100,000-page XMemMapPhysicalPages fits the title's hang watchdog under Wine Squashed from ItsNotRexed#7584 by gkissel, head 5ba2a7f (security scan PASS). Merged cleanly on top of ItsNotRexed#7550 (view_protect still feeds the view's protection). Added the fix from the PR's review (dotaku22): after a partial unmap a guest run spans several host placeholders, and mapping the whole run again failed with ERROR_INVALID_PARAMETER; map_view now maps one view per placeholder. xmem_test gains that re-map check, which failed under Wine 11.22 before the fix and passes after (189 checks).
…e view per host placeholder it spans) Squashed from ItsNotRexed#7584 by gkissel, head 4a7bfd5 (security scan PASS). Rescan of the new commit only (5ba2a7f..4a7bfd5): sec_prescan flags=0, Haiku read PASS (map_view uses VirtualQuery, isolate_placeholder and MapViewOfFile3 only). It is the author's version of the review fix the first ItsNotRexed#7584 commit on this branch carried locally; this replaces that local text with upstream's (same behaviour, isolate_placeholder instead of an open-coded VirtualFree) and takes upstream's test wording.
…lAlloc with console page-size bits (what ItsNotRexed#6919 leaves) Squashed from ItsNotRexed#4531 by labj1987, head 01fc4b1 (security scan PASS). Rescan of the new commit only (007ae5e..01fc4b1, a test-only variable rename): sec_prescan flags=0, Haiku read PASS. Superset check: the head is already cut down to what ItsNotRexed#6908/ItsNotRexed#6919 leave (4 MiB pages, page-size bits on the title's VirtualAlloc, aligned reservations without the placeholder API) and keeps main's attribute acceptance and PAGE_GRAPHICS_* stripping, so it is taken whole. Conflict: xmem.cpp, where the PR turns XMemVirtualAlloc into a wrapper over xmem_virtual_alloc next to ItsNotRexed#7584's map_view; kept both (wrapper, then ItsNotRexed#7584's per-placeholder map_view; the old unmap_page body ItsNotRexed#7584 replaced stays gone). xmem_test under Wine 11.22: 592 checks OK.
…ment and clear stale mapped-page entries Squashed from ItsNotRexed#7712 by TillusStark26, head 4bd14d7 (security scan PASS). Conflict: the aligned-reservation branch of XMemVirtualAlloc, where ItsNotRexed#4531 keeps the title's host flags (console_windows); kept that and added ItsNotRexed#7712's clear_window_range after it. Adapted to ItsNotRexed#7584's model: the PR clears stale entries of the per-page g_mapped table, which ItsNotRexed#7584 replaced with g_views (view base -> run), so clear_window_range now drops every g_views record that overlaps the freshly reserved range (a view the host no longer has is stale as a whole). Review note kept as is: plain fresh reservations now trim old windows instead of erasing them (low impact, flagged on the PR). xmem_test under Wine 11.22: 626 checks OK, including the PR's reuse-after-external-release case.
…a whole physical-page reservation Squashed from ItsNotRexed#7742 by ASTROWwwW, head 51736cd (security scan PASS). Conflicts in xmem.cpp and xmem_test.cpp with ItsNotRexed#7584, ItsNotRexed#4531 and ItsNotRexed#7712. Adapted to ItsNotRexed#7584's run-of-views model: release_range unmaps every recorded view (g_views) whose base is in the range and not inside a nested reservation, via unmap_range, instead of the per-page g_mapped table. XMemVirtualAlloc's reservation branch records g_reservations and keeps ItsNotRexed#7712's record_window/clear_window_range split; the aligned and plain paths keep ItsNotRexed#4531's title flags and ItsNotRexed#7712's clearing, then note nested allocations. TitleVirtualAlloc is ItsNotRexed#4531's version (page bits through xmem_virtual_alloc) with note_nested_allocation on the plain host path. Both new test functions kept (a merged closing brace restored). xmem_test under Wine 11.22: 656 checks OK.
|
Tested
AI-assisted: Codex GPT-6.2 Sol ran the builds and tests, Claude Opus 5.5 reviewed the report. Synthetic fixtures only, no game data. |
|
Tested Full runtime AI-assisted (Claude Code). |
|
checked: #7584 at @gkissel, this follows up my 63/63 negative control on the old loop.
Not run: full ctest at this head (itsRevela posted an NVIDIA full run above). Synthetic pages only, no game files. AI-assisted (Claude Code). |
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 (head28f19186d):xmem_test: OK (194 checks). The new checks: consecutive pages are one view (VirtualQueryreports one allocation of their size); a page unmapped in the middle is reserved again and the pages around it keep their contents; a page maps again into the gap; a run maps again over a range an earlier partial unmap split; a 1,100-page run is mapped as views of at most 1,024 pages and keeps every other page's contents after a single-page unmap; a multi-page unmap across two views, pages mapped over the middle of a live run, and a freed page that no longer maps; and a reader of the page next to one that is unmapped and mapped again 2,000 times never faults (a split briefly unmaps the rest of its view; a vectored handler makes such an access wait for the remap). Without that handler the reader faults in 1 of 3 runs here, 2 of 3 in aslan2142's.main'sxmem.cpp: FAILED, at the one-view check and the 1,024-page cap check (measured independently by isfcrctfs on the 186-check head). The split and remap 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.)