xmem: keep shared page runs consistent for readers, and join single-page maps into them #2905
No reviewers
Labels
No labels
accessibility
awaiting author
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
priority: P0
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/R0Y00--Xbox2Windows!2905
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2905/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@JohnConnorNPC on GitHub · 2026-11-26 18:32 UTC · original
JohnConnorNPC:memory/merged-page-runs→main· +2534 −85 · 18 files · 17 commits · closed without mergingHow to test
cmake --preset devandcmake --build --preset dev. On Linux, use the clang-cl cross build indocs/non-windows-hosts.md.tools/startup/run.ps1 <usual exe/data/log arguments> -Seconds 0 -MemoryGiB 40 -FakeUser -Render -Translate -GpuMirror -HostCpu -StartupBelow40 -LazyMemory.-LazyMemorysetsX2W_XMEM_LAZY_COMMIT,X2W_XMEM_LAZY_STARTUPandX2W_XMEM_SHARED_VIEWS. WithoutX2W_XMEM_SHARED_VIEWS, the reader waits, wrappers and joins in group 3 stay off.tools/startup/run.sh ... --seconds 0 --memory-gib 40 --fake-user --render --translate --gpu-mirror --host-cpu --startup-below-40, withX2W_XMEM_LAZY_COMMIT=1 X2W_XMEM_LAZY_STARTUP=1 X2W_XMEM_SHARED_VIEWS=1in the environment.
X2W_DEVICE_TYPE=6 X2W_PRESENT_COMPOSITE=1 X2W_D3D12_RELEASE_RETIRED=1 X2W_D3D12_RELEASE_RETIRED_VIEWS=1 X2W_XAPU_TYPED_COMPLETIONS=1.X2W_D3D12_BUDGET=1and used an input script that answers the account prompt once.x2w-d3d12 heartbeat: N frames presentedcount keeps rising past about 170 s, with no start-up hang.undetermined cause (see Evidence).
XMem pages are mapped from 64 KiB sections. With
X2W_XMEM_SHARED_VIEWS=1, pages that are adjacent in theaddress space and adjacent slots of one section share one view, called a run. Unmapping one page of a run takes
the run apart and maps its other pages again (a run break). A console executable under test maps nearly every
page with its own call: 45595 of 45602 recorded runs had one page. Under Wine each
MapViewOfFile3is a serverround trip. In one run its cost rose from 0.04 ms to 0.92 ms per call as the view count grew, so start-up spent
most of its time mapping.
This PR has one aim: XMem page runs under Wine are cheaper to build, and code that reads those pages sees them
committed while a run changes. It has three groups of commits, in commit order.
On Windows, group 3's reader waits, its import wrappers and all joining are behind the shared-views opt-in
(
X2W_XMEM_SHARED_VIEWS=1;run.ps1 -LazyMemorysets it). Without it, readers are called once and pages aremapped as before. Groups 1 and 2, split-first and the fault-retry fixes need no switch. See Switches.
Group 1: slot 31 placeholder splits under Wine (3 commits)
xmem: split slot 31 placeholders when the OS hides the placeholder bit.0x100region flag. As a result, every queue slot 31 split orcoalesce whose run was not exactly one placeholder failed with
ERROR_INVALID_ADDRESS.xmem_placeholder_split.xmem_placeholder_split_control, turns the fallback off and passes only when the split stepfails. It skips (77) where the OS reports the bit.
docs/experiments.mdand the README row.Group 2: DirectStorage copies under the mapping mutex (3 commits)
xmem: copy DirectStorage destinations under the mapping mutex,startup: copy DirectStorage data through x2w_xmem_copy_to_destination.VirtualQuerypre-check could run while a break had the destination page unmapped for a moment.It then refused a destination that was mapped both before and after.
memory.mutex, which every break also holds.raising.
startup_dstorage_break_window.MEM_RESERVEinside it. The requestmust wait for the hold, then complete with every byte in place.
startup_dstorage_break_window_control, builds XCRDAPI without the export lookup and mustreport "refused while the run was broken".
Group 3: page runs (11 commits)
Fewer map calls:
xmem: split an owned placeholder before mapping a page into it.single-page call makes one
MapViewOfFile3attempt instead of two. If the split or the map fails, the oldorder runs.
xmem_map_split_first: 8 maps for 8 pages. Its control,xmem_map_split_first_control(
X2W_XMEM_TEST_MAP_FIRST=1), measures 2 maps per page.Readers outside run breaks. A touch of a page inside a break faults, and the fault handler retries it. A region
query or a kernel copy does not fault: it sees the page as reserved, or fails.
xmem: answer non-faulting readers outside run breaks.x2w_xmem_stable_call(fn, context). It runsfnonce when no break begins or ends around it,and otherwise runs it again under the mapping mutex.
XMemVirtualQuery, and in the stand-insx2w_local::writable,slot 133
measure_placed_bufferandsnapshot_samplers.xmem: answer the main module's VirtualQueryEx outside run breaks.VirtualQueryEximport goes through a wrapper that usesstable_call.xmem: run readers once without shared views, and write their answers after a break.stable_callcallsfndirectly.may be in a lazily committed page.
xmem: write back only what XMemVirtualQuery's query writes, and pass lines at the end: the write-back coversthe bytes the query writes and no more.
xmem: run title ReadFile/WriteFile again after a run break refused the buffer.ReadFileandWriteFileimports are wrapped.ERROR_NOACCESSorERROR_INVALID_USER_BUFFER, a break overlapped it, and its buffer lies in a run.ReadFileinto the run failed with 998 and left the position as it was, andWriteFilefrom it failed with 1784. After it, the read gets all 4096 bytes.Fault retries across breaks:
xmem: retry a fault in the break after the one the handler saw end. This fixes a pre-existing bug:the same page in that next break was then not retried.
xmem_page_runs_check.pycrashed in 6 of 6 runs before the fix and passed 6 of 6after it.
xmem: retry a fault from any of the last 64 breaks, not only the last: a fault whose break is among the last64 is retried. Once its break is older than that, the fault is passed on, like any other fault.
Joins:
xmem: join a page mapped on its own call to the view before it, withX2W_XMEM_SHARED_VIEWS=1only.reservation.
restored.
xmem: stop a joined run at 64 pages.xmem: say the run cap bounds joined runs only, and test the break ring's edge.before this PR.
Tests and changed expectations:
xmem_run_break_readers, with_control,_next_break_control,_default,_lazy,_two_runs,_mirrorand
_ring_edge.page committed. The readers cover
ReadFileandWriteFiletoo.xmem_cross_call_merge, with_control(one view per call underX2W_XMEM_TEST_NO_MERGE=1),_deployed(the deployed shim ignores that switch) and
_default(default mode). Itscappedcase covers the 64-page cap.xmem_page_runs_check.pyexpected "mapping the page again does not join the pieces: three views". The pagenow joins the lower piece, which gives two views.
xmem_cross_call_merge_controlstill asserts thethree-view layout.
xmem_page_runs_device_checknow maps the upper view first and the single pages in reverse slot order.Its view boundaries are the same as before, and its view-count assertion is unchanged.
Switches
stable_callwaits, theVirtualQueryEx/ReadFile/WriteFilewrappers, joins and their capX2W_XMEM_SHARED_VIEWS=1X2W_XMEM_TEST_*(MAP_FIRST, NO_MERGE, MERGE_CAP, HOLD_WINDOW, HOLD_RETRY, FAULT, NO_PLACEHOLDER_FALLBACK)Test
Host: Linux, cross-built with clang-cl 19 and xwin, tests run under Proton Experimental (wine-11.0) in a fresh
prefix. The runner reads ctest's JSON and applies each test's ENVIRONMENT, WILL_FAIL, PASS/FAIL_REGULAR_EXPRESSION,
TIMEOUT and SKIP_RETURN_CODE. Base under the same runner: main
6ee878d8. Head under test:4a43a173.Build scope on this host:
ninja -k 0ends with exit 1 and the same 419 failed targets on base and on thisbranch. They are the MASM fixtures and the generator-driven targets, which the cross toolchain cannot build.
Every runtime DLL and every test this PR adds or touches builds. Tests whose executable did not build are not
run here, and CI is their first run.
6ee878d8(2441 registered):578 passed, 1729 failed, 58 skipped (77) of 2365; nocommand 76.differs from base, will be added here.
^xmem_|^startup_dstorage_break_window|^startup_placed_buffer_range|^startup_translated_sampler_bindings|^startup_local_log):recorded run gave
48 passed, 39 failed of 87. Five of the 39 exited 77.python-linuxcommand from.github/workflows/windows.yml(Python 3.13, requirements-dev):all 210 checks passedon base and on this head (exit 0 both).change, each paired with its check, all fail that check:
break_id = seen | 1; a break ring of 1, or of 128 (no eviction at 64);stable_callnever retries; theReadFile/WriteFilewrapper returns 0, or retries with no bound.ReadFileandVirtualQueryExwrite into an uncommittedmapped slot without faulting, so the lazy-slot cases of the
ReadFilewrapper and of theVirtualQueryExwrapper are unpinned here. Windows CI is their first real check.
Not run here (Windows only, so CI is their first run):
xmem_page_runs_device_warp: the edit compiles, but the test was not run.linear_buffer_backing_*: not touched. Whether the joins change their layout is not measured._warp/_hardwareD3D12 test,offscreen_*,startup_sampler_*.Evidence from a running title
Local runs of a console executable under test on Linux under Wine/Proton. Each run had no timeout, and each
configuration was run once unless a count is given. Switches: 40 GiB, render, host CPU rewrites, GPU mirror, a
fake user, start-up blocks below 2^40, lazy commit and
X2W_XMEM_SHARED_VIEWS=1.Like for like: the same launcher, environment and arguments, with only the build different.
6ee878d8. Main already honours all three memory switches, but its shared views are the basic version:ReadFile/WriteFile):as an earlier split-first-only run, which ended the same way at 636 s.
manifest had the same md5 across the copies, with 218001 files and none new during the run.
address at 204.5 s.
the storage write.
older main did not show them. This PR changes no D3D12 code. Hypothesis, not tested: they come from main.
Other controls:
6ee878d8without shared views: no frame was presented, and start-up stopped on the same watchdog.52905a32): hang at about 47 s in 2 of 2 runs.start-up hang (1064 s and 408 s), with 54545 and 54549 joins and 0 lost.
ring-edge test) are not in any title run.
Measured vs not
linear_buffer_backinglayouts;VirtualQueryEx(it imports it, but the calls were not counted);ReadFile/WriteFileretry assumes the failed call had no partial side effect. Only Wine's fullfailure was measured.
ReadFileandWriteFileimports are wrapped. Not wrapped:ReadFileEx/WriteFileEx,scatter/gather,
DeviceIoControl, sockets, I/O from other modules and I/O throughGetProcAddress.break before it is passed on. A range that is broken often can therefore delay a real crash report by one
retry per break.
heuristic, and
MEM_PRIVATEheap paths.A plain-memory destination is now checked and written by xmem.dll under its mapping mutex when the export is there. Before, the VirtualQuery pre-check could run while an XMem run break had the destination's page unmapped for a moment, and refuse a destination that was mapped before and after. Without xmem.dll the check and the copy run in XCRDAPI as before. A copy that faults on its source now fails the request ("copy faulted (source not readable)") instead of raising in the stand-in.@MasterDomino on GitHub · 2026-11-26 18:58 UTC · original
First Windows run of #2905 at
4a43a173, against its basemain6ee878d8.Environment: Windows 11 build 26300.9974, AMD Radeon RX 9070 XT, driver 32.0.32029.2010, MSVC 19.44.35221,
releasepreset, D3D12 debug layer not enabled. Clean configure and build.Fixtures
109 tests: 104 passed, 1 skipped (77,
xmem_placeholder_split_control, because Windows reports the placeholder bit), 4 failed. All 4 pass onmain6ee878d8with the same command:xmem_page_runs_device_{warp,hardware}:FAIL across two single-page views: the buffer touches 1 view(s), expected 2.linear_buffer_backing_share_{warp,hardware}: thesplitcase fails withslot 133 -> 0x00000000, copied (share 0x80070032, feature 0x00000000, 131072 bytes in 1 refreshes, staging 1).a2andbstill pass.Both tests print
shared views are on (X2W_XMEM_SHARED_VIEWS). This looks like the join of single-page maps merging views that these tests expect to stay separate. These are the two tests the description listed as not yet run.Startup runs: back to back, 600 s limit, with the switches the description asks for:
-MemoryGiB 40 -FakeUser -Render -Translate -GpuMirror -HostCpu -StartupBelow40 -LazyMemory,X2W_DEVICE_TYPE=6 X2W_PRESENT_COMPOSITE=1 X2W_D3D12_RELEASE_RETIRED=1 X2W_D3D12_RELEASE_RETIRED_VIEWS=1 X2W_XAPU_TYPED_COMPLETIONS=1 X2W_D3D12_BUDGET=1,-ContinueAsserts 100000and an input script that answers the prompts. Each arm started from its own copy of the same shader cache.-LazyMemory4a43a173main6ee878d8main6ee878d8On Windows the crash is not caused by this PR:
maincrashes at the same executable offset with the same switches. In both crashes the faulting write address is the base of a 0x50000XMemVirtualAllocreserve (type=0x20402000 attr=0x21, logged asplaceholder reserve). Turning-LazyMemoryoff onmainremoves the crash, and the run gets further in less than 1.5 times the time: twice the textures and 61 pipelines instead of 17. Hypothesis, not tested: under lazy commit, a write to that kind of placeholder reserve is not committed by the fault handler. The same run without lazy commit maps it as before. The Windows main control does not show the early stall that the description reports formainunder Wine.AI-assisted; the commands above were run locally.
@Not-Saii on GitHub · 2026-11-26 19:11 UTC · original
Tested on Windows: the head
4a43a173, andmain6ee878d8as the control.Summary
xmem_page_runs_device_warpfails on this head and passes onmainon the same machine. In its last case, the buffer placed across the boundary between the single-page maps touches 1 view instead of 2. That is the test the PR lists as not run here.main.main. The PR does not cause it.maindoes not show the stall at 16.5 s seen under Wine.main.main: title private commit 7.6–8.1 GiB against 11.4–13.0 GiB, and local VRAM 3.6–5.6 GiB against 6.4–7.0 GiB. With 3 and 2 runs, that may still be variance.Environment
devpreset (a Debug build). Both commits were built the same way, and every run used these builds.X2W_D3D12_DEBUGnot set).4a43a173,main6ee878d8, each built in its own worktree.Build and tests
xmem_page_runs_device_warp. It failed 4 of 4 times on this head (the run above, plus--repeat until-pass:3). Onmain6ee878d8, built the same way on this machine, it passed. The first three cases pass; the last one fails:XMemAllocatePhysicalPageshands out come in an order that reversing makes consecutive.xmem_placeholder_split_control, as expected. The OS reports the placeholder bit, and the start-up log agrees:placeholder bit reported=1.xmem_run_break_readers*case, including_lazyand_ring_edge;xmem_cross_call_merge*case;xmem_map_split_firstand its_control;xmem_placeholder_split;startup_dstorage_break_windowand its_control;xmem_shim_page_runs,xmem_shim_mapped_pagesandupload_reservation;startup_address_console*_warpandstartup_list_resubmit*_warptests._hardwaretests that the regex also matches;linear_buffer_backing_*.Start-up runs
Every run used the same command, with only
-BuildDirand-LogDirchanged between them.Deviations from "How to test":
-Seconds 900instead of-Seconds 0, so every run is bounded.-ContinueAsserts 1000. Without it, runs on this machine stop at a soft assert at 130–170 s. None of the runs below logged a continued assert.mainrun added files (249).Results.
main1, PR head 2,main2.main1main2"None" in the out-of-memory, mapping-failure and device-removed row means that no line matched, and that no
x2w-xmemline reports a non-zero error.XMem lines
VirtualQueryEximport, and 2ReadFile/WriteFileimports.mainhas none of these lines.The stop. All five runs end at the same instruction in the executable.
mainstops the same way, so the PR does not cause it. It may be the same as the stop at the configured address in your Wine runs, but I have not checked that.Measured vs not
main: 2. Single differences between the columns, such asmain1's ring refusals and save write, did not repeat.-Seconds 0, a-LazyMemoryrun that gets past loading, and any_hardwaretest.AI-assisted (Claude Code).
Pull request closed