runtime: commit xmem pages and startup blocks on first touch (opt-in) #2201
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2209"
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?
Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-25T05:15:06Z.
Change
Two experimental opt-in switches for #1199, one commit each: memory is committed on first touch instead of up front. Refs #1199.
1.
X2W_XMEM_LAZY_COMMIT=1: mapped physical pages (#1199 item 2)A fresh physical-page slot is committed on its first CPU access instead of when its page is mapped. Slab commit counts against the system commit limit, not the process.
xmem.dllloads with the switch on, commits the slot behind a faulting address and resumes the thread. A thread that loses the race to the same slot retries once. GPU-view addresses resolve to the CPU-view slot.x2w_xmem_commit_range(address, bytes)first:ReadFileimport is wrapped, because a kernel-mode write into an uncommitted mapped page fails withERROR_NOACCESSinstead of faulting;accessible(write), the DirectStorage destination check) commit first.x2w_xmem_write_physicalstill commit before use.2.
X2W_XMEM_LAZY_STARTUP=1(with the first): large startup blocksThe startup commit profile (#1098) only reserves blocks of 32 MiB and up. The same handler commits them 256 KiB at a time on first touch. Their commit counts against the process, so this is what the
-MemoryGiBjob cap sees.x2w_xmem_commit_rangealso commits the chunks behind a range in such a block.x2w_xmem_commit_block(address), which commits the whole block once and removes it from the list.VirtualFreeimport is wrapped to remove a released block from the list first.X2W_XMEM_STARTUP_BELOW_40stay committed up front; blocks below 32 MiB are committed as before; if the list cannot record a block, it is committed in full.Without the switches nothing changes: no handler, no wrapped import, and both exports return TRUE. The second commit can be dropped on its own if the startup-block policy should stay as it is.
Docs: two rows in
docs/startup-runtime.mdSwitches, a sentence in Startup memory accounting, and two sections indocs/experiments.md: "Mapped pages committed on first touch" and "Startup blocks committed on first touch".Verification
Commit
ea454aff(review fixes onb4c66763; the first part alone:eb587ad6) onab9a020c, Windows 10 22H2 (build 19045), Visual Studio 2026 18.12 (MSVC 19.52), Windows SDK 10.0.26100, CMake 4.3.1, Python 3.13.7.The first commit alone passes
xmem_36/36 and the second set 69/69.Startup runs on Windows 10 22H2, 16 GB RAM, RTX 3080 Ti,
-MemoryGiB 16, same input.System commit (GiB, two runs each):
X2W_XMEM_LAZY_COMMITProcess commit and end of run:
X2W_XMEM_LAZY_COMMITThe job-cap stops are
XMemMapPhysicalPagesScatterfailing with error 1455 while the process commit heartbeat reads 16,389 MiB.With
-StartupBelow40as well, so the run gets past the builder's address check (#1758):Two of the three runs with both switches are on this PR's final commit (
b4c66763with #1277 merged for Windows 10).Evidence and follow-up
xmem.dll(MIT, as the repository); no game data is used.xmem_lazy_commit_offandxmem_lazy_startup_offare the controls: with a switch at0the same mapping or blocks are committed up front, and the lazy assertions would fail on that path.xmem_lazy_importsis a source-built check whose ownReadFileandVirtualFreeimports are wrapped, since the Python checks run inpython.exe. Removing the commit in theReadFilewrapper or the list removal in theVirtualFreewrapper makes it fail.x2w_xmem_commit_rangetoo. The ones listed are those the startup runs reached.-StartupBelow40, the 2.2 GiB block below 2^40 is committed up front, and in one run the process commit peaked 0.15 GiB under the 16 GiB cap. So this is the next limit on a 16 GB host.Checklist
AI-assisted: prepared with Claude Code; the commands and results above were run on the author's PC.
🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T05:20:38Z.
Review: COMMENTED
Review of head
b4c6676319da17de47d1782d4f7491731f052a7d(Linux-side review; no Windows run by me).Checked
git diff --stat origin/main...HEAD: 10 files, +674/-21. Title is neutral, PR is MERGEABLE, CI is green (Windows build+test, Linux Python checks, gfx1013 fixtures all pass).python3 tests/docs_links_check.py->Checked 18 Markdown files and 415 links.python3 tests/bidi_controls_check.py->no bidirectional controls.git diff --check origin/main...HEAD-> clean.ast.parseof both new tests: OK. They loadxmem_shim.dll, so they only run on Windows; the CI Windows job covers them. I did not queue a Win2025 run.src/xmem_mapped_pages.cppend to end for the new paths. The handler takesmemory.mutexlike the existing remap handler. I found no site that touches a lazily mapped page while holding that mutex (commit_slot's memset hits a freshly committed view;x2w_xmem_write_physicalusessection_for(..)with commit).x2w_xmem_commit_rangecall inread_file_committed, and drop thelazy_blocks.blocks.eraseinvirtual_free_lazy. Each should fail one of the new tests.Findings
src/xmem_mapped_pages.cpp(x2w_xmem_commit_range, the loop after thelazy_startup()block): when the range starts in a lazy block and runs past it, the physical-page loop restarts fromstart, not fromfrom. It is harmless (backing_atreturns null for block addresses), but a multi-GiB range costs a 64 KiB-step map lookup per page undermemory.mutex. Start the loop atfrom & ~(page_bytes-1).read_file_committed: only theReadFileimport of the main module is wrapped. Other kernel-mode accessors of an uncommitted slot (ReadFileEx,WriteFilefrom a lazy page, imports in other modules) would fail withERROR_NOACCESSthe same way. Fine for an opt-in experiment, but please say so indocs/experiments.md.retriedis shared by the startup and slot paths. After a startup-path retry it is not cleared, so a later fault on the same chunk address range is passed on. A comment would help.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T05:49:51Z.
Review: COMMENTED
Re-review of head
ea454aff6382601d690f3888d4eee1fe886bd957(Linux-side; no Win2025 run by me). Previous review was onb4c66763.Checked
git diff --stat b4c66763 ea454aff: 4 files (CMakeLists.txt,docs/experiments.md,src/xmem_mapped_pages.cpp, newtests/xmem_lazy_imports_check.cpp), +152/-13. PR is MERGEABLE, title neutral.gh pr checks: Windows build+test, Linux Python checks and gfx1013 fixtures pass.python3 tests/docs_links_check.py->Checked 18 Markdown files and 416 links.python3 tests/bidi_controls_check.py->no bidirectional controls.git diff --check origin/main...HEAD-> clean.src/xmem_mapped_pages.cppand the new test.__try/__except(MSVC-only SEH), also in untouchedlocal_log.h, so I could not build or run the new test on Linux. The CI log for the Windows job was not retrievable, so I cannot confirm from the log thatxmem_lazy_importsran. The job is green and the test is registered inCMakeLists.txt.ReadFilewrapper, drop the list removal in theVirtualFreewrapper). Please confirm on Windows that each one turnsxmem_lazy_importsred.Previous findings
start: fixed. The loop now starts atfrom & ~(page_bytes-1)(line 932), after the lazy blocks.docs/experiments.md(listsReadFileEx, overlapped reads,WriteFilefrom a lazy page, other modules' imports).retried: fixed. The startup path now has its ownretried_chunk+retried_releases; the slot path keeps its ownretried. The release counter is bumped only when a block really left the list.Findings on the new head
virtual_free_lazy: the counter is bumped beforereal_virtual_free. A racing thread can see the new count while the old block is still reserved; the only effect is one extra retry, so it is harmless. A one-line comment would help.tests/xmem_lazy_imports_check.cpp: the temp file fromGetTempFileNameAis not deleted ifCreateFileAfor reading fails. Cosmetic.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-25T05:44:45Z.
Thanks for the review. All three findings are addressed in
ea454aff:x2w_xmem_commit_range: the slot loop now starts where the lazy blocks end (from), not atstart.docs/experiments.mdnow says only the main module'sReadFileimport is wrapped, and thatReadFileEx, overlapped reads issued elsewhere,WriteFilefrom a lazy page and other modules' imports fail withERROR_NOACCESSthe same way.retried: the block path now has its own retry, keyed on the chunk and on a count of released lazy blocks, as the remap handler keys on(page, break_id). A fault that stays is still passed on, and a new block later placed at the same addresses can still be raced once. Before, a staleretriedcould pass that one on.About the suggested mutants: neither would have failed the Python tests. Both wrappers patch the main module's imports, and there the main module is
python.exe. I added a source-built check,xmem_lazy_imports(tests/xmem_lazy_imports_check.cpp), whose ownReadFileandVirtualFreeimports are wrapped:PASS(8 checks)x2w_xmem_commit_rangeinread_file_committedReadFile failed with error 998,check_failed: readfile_into_lazy_pageeraseinvirtual_free_lazycheck_failed: released_block_not_committed,check_failed: released_block_still_reservedctest --preset release --no-tests=error -R '^xmem_': 39/39 passed. The affected startup set: 72/72 passed (Windows 10 22H2, release). The branch still merges cleanly with currentmain(8b5a0f7c). A startup run on this commit (with #1277 for Windows 10, both switches and-StartupBelow40) reached the same command-list slot 148 stop as before, with no commit failure.AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-25T05:54:22Z.
Thanks for the re-review. Both nits are done in
76b995fd:virtual_free_lazy: a comment says the count is bumped before the release, so a racing thread retries at most once more.xmem_lazy_imports_check: the temp file is deleted when it cannot be reopened for reading.On the mutants: confirmed on Windows, see the table in my previous comment. Each one turns
xmem_lazy_importsred:check_failed: readfile_into_lazy_page(error 998) for the first, andreleased_block_not_committedwithreleased_block_still_reservedfor the second. On76b995fd,ctest --preset release --no-tests=error -R '^xmem_'passes 39/39.AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T06:03:43Z.
Review: COMMENTED
Re-review of head
76b995fdadb445042399d7a9339418b6ff8301c8(previous reviews coveredb4c6676andea454af).Delta since
ea454af(git diff ea454af..HEAD --stat: 2 files, 7 insertions, 2 deletions)src/xmem_mapped_pages.cpp:980: comment only. It documents that the release count is bumped after the block leaves the list and beforeVirtualFree. I tracedlazy_commit_fault(lines 178-212): a thread that faults in that window sees the new count with the old block still reserved. The keyed retry (retried_chunk/retried_releases) lets it retry once and then pass the fault on, so the comment is accurate and no behavior changes.tests/xmem_lazy_imports_check.cpp:99-104: theReadFilecall is now guarded againstINVALID_HANDLE_VALUE, and the temp file is deleted withDeleteFileAif the reopen fails. This fixes the earlier temp-file leak and the call on an invalid handle. Correct.Findings
Checks run locally (Linux)
git diff --check origin/main...HEAD: clean.python3 tests/docs_links_check.py: self-test passed, 18 Markdown files and 416 links checked.python3 tests/bidi_controls_check.py: self-test passed, 1490 files, no bidirectional controls.gh pr checks 2209: Linux Python checks, Windows build and test, and gfx1013 fixtures all pass. The coverage chart and /O2 jobs are skipped. The PR is mergeable and the title is neutral.Not run
xmem_lazy_imports,xmem_lazy_commitandxmem_lazy_startup.No blocking issues in this delta.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T06:11:56Z.
Review: CHANGES_REQUESTED
One release-path blocker on
76b995fd:virtual_free_lazyerases the lazy-block record before it knows whetherVirtualFreesucceeded. A failedMEM_RELEASEleaves the range reserved. A later first touch then has no record and cannot commit the page. Please keep the reservation and its registration consistent across failure and synchronize successful release with in-flight commit. A source-built check can tryMEM_RELEASEwith a nonzero size, verify failure, then write to an untouched page and finally release with size zero. Please also check that a failed handler installation leaves lazy allocation disabled rather than returning inaccessible pages.Imported from R0Y00/Xbox2Windows. Originally posted by stan495 on 2026-11-25T06:34:24Z.
Review: APPROVED
Verified head
76b995fdon Windows x64 (devpreset, MSVC19.44.35228.0, Python 3.12):cmake --build --preset dev --target xmem_shim xmem_lazy_imports_check-> succeededctest --preset dev --no-tests=error -R '^xmem_lazy_' -V-> 5/5 passed (xmem_lazy_commit,xmem_lazy_commit_off,xmem_lazy_startup,xmem_lazy_startup_off,xmem_lazy_imports)Verdict: Approved at
76b995fd.Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T07:24:26Z.
Review: COMMENTED
Re-review at head
ac423c5fdf816992f59575de1454c9f3f0ee01d5(delta since76b995fd:c40e3dacplus two upstream merges).Read of
c40e3dac(src/xmem_mapped_pages.cpp,tests/xmem_lazy_imports_check.cpp,CMakeLists.txt, docs):virtual_free_lazynow runsVirtualFreeunder the exclusive list lock, erases the block only if the release succeeded, and preservesGetLastError. A refused release (nonzero size) keeps the block reserved and lazy. This fixes the earlier gap.x2w_xmem_commit_rangehold the shared lock from lookup to commit, andx2w_xmem_commit_blockholds the exclusive lock. A release cannot run in between. No lock-order issue: the list lock is dropped beforememory.mutexis taken in every path I read.lazy_handler_installedis constant-initialized and set before the wrappers are installed, solazy_commit()andlazy_startup()act as off when the handler is refused.lazy_startup()no longer caches a handler-dependent value.release_with_size_refused,refused_release_block_still_lazy) and the handler-failure path (xmem_lazy_handler_failure, test-controls build only).Commands run locally (Linux, worktree at this head):
gh pr checks 2209: Linux Python checks, Windows build+test, gfx1013 fixtures all pass./O2job skipping.python3 tests/docs_links_check.py-> pass (18 files, 427 links)python3 tests/bidi_controls_check.py-> pass (1524 files)git diff --check origin/main...HEAD-> cleantests/xmem_lazy_*_check.pyneed the Windows binaries as arguments, so they were not run here. The new C++ tests are covered by the green Windows CI job only. I did not queue a separate Windows VM run, and I did not run a mutation of the new logic because the tests cannot execute on Linux.Findings:
virtual_free_lazyholds the exclusive lock across the realVirtualFree. Fine for these block sizes, but a slow release will stall any first-touch fault on another thread. Worth a comment, not a blocker.MEM_DECOMMITof part of a lazy block is not handled by the wrapper (unchanged from before this delta).Scope: MIT, no third-party code, neutral title, mergeable. Opt-in switches, so default behaviour is unchanged.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T07:34:45Z.
Review: APPROVED
The failed-release and handler-installation findings are resolved. On the exact SHA, Windows Release passed all 24 selected lazy-commit, mapped-page, slot 148 and upload checks. The failed release retained the reservation and a subsequent write committed its page. The forced handler failure committed both memory paths up front. The earlier Windows 10 WARP timeout is tracked separately on the same upstream slot 148 fixture.
Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-25T07:21:50Z.
Thanks. Fixed in
c40e3dac, and the branch is merged with currentmain(ac423c5f; one conflict with #2225 inmap_pages, which keeps both the deferred commit and #2225'ssection-backingrecord).Release path (
virtual_free_lazy)VirtualFreenow runs under the lazy-list lock. The block leaves the list only when the release succeeds, so a refusedMEM_RELEASEleaves it reserved and still committed on first touch. The caller's error is kept.x2w_xmem_commit_rangeandx2w_xmem_commit_blockhold the same lock from the lookup to the commit, so a successful release cannot run between them.Handler installation
AddVectoredExceptionHandlersucceeds. Otherwise both switches act as off: pages and blocks are committed up front, no import is wrapped, and the log says so.Checks (
tests/xmem_lazy_imports_check.cpp)xmem_lazy_imports(new case)MEM_RELEASEwith a nonzero size fails withERROR_INVALID_PARAMETER; a write to an untouched page is then committed; release with size 0 succeedsrefused_release_block_still_lazy,refused_release_write_committedxmem_lazy_handler_failure(new)X2W_XMEM_TEST_LAZY_HANDLER_FAILURE=1: a mapped page and a 64 MiB startup block are committed and writablectest --preset release --no-tests=error -R '^(xmem_|startup_slot148|startup_upload)'onac423c5f: 47/48. The one failure isstartup_slot148_acceptance_nodebug_warp(10 s timeout), the Windows 10 WARP no-debug case Jess-Doit reported onmainin #870.dev: 47/47. Windows 10 19045, MSVC 19.52.AI-assisted (Claude Code).