Repository navigation
runtime: commit xmem pages and startup blocks on first touch (opt-in) - #2209
Conversation
…IT, R0Y00#1199) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…UP, R0Y00#1199) With X2W_XMEM_LAZY_STARTUP=1 and X2W_XMEM_LAZY_COMMIT=1, commit_startup_block reserves blocks of 32 MiB and up; the fault handler commits them 256 KiB at a time on first touch, x2w_xmem_commit_range commits the chunks behind a range for writers that cannot fault, and x2w_xmem_commit_block commits a whole block before slot 133 places a GPU resource in it. The title's VirtualFree import drops released blocks from the list. Blocks placed below 2^40 stay committed up front. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tedin7
left a comment
There was a problem hiding this comment.
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.- Read
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). - Mutation: not run. The tests need the Windows DLL, so a Linux mutation would prove nothing. Suggested mutants for the author: drop the
x2w_xmem_commit_rangecall inread_file_committed, and drop thelazy_blocks.blocks.eraseinvirtual_free_lazy. Each should fail one of the new tests.
Findings
- P1: none.
- P2
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). - P2
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. - nit: the thread-local
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.
- x2w_xmem_commit_range starts the slot loop where the lazy startup blocks end, not at the range start. - The startup-block fault path has its own retry, keyed on the chunk and on the number of released lazy blocks, so a new block at the addresses of a released one can still be raced once. - docs: only the main module's ReadFile import is wrapped; name the kernel-mode accessors that are not. - xmem_lazy_imports: a source-built check whose own ReadFile and VirtualFree imports are wrapped. Without the commit in the ReadFile wrapper the read fails with ERROR_NOACCESS; without the list removal in the VirtualFree wrapper the handler commits a new reservation at the released addresses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the review. All three findings are addressed in
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
AI-assisted (Claude Code). |
tedin7
left a comment
There was a problem hiding this comment.
Re-review of head ea454aff6382601d690f3888d4eee1fe886bd957 (Linux-side; no Win2025 run by me). Previous review was on b4c66763.
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.- Read the three fixes in
src/xmem_mapped_pages.cppand the new test. - Syntax check with the MinGW cross compiler fails on
__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. - Mutation: not run on Linux for the same reason. The new test's own description gives the two mutants (drop the commit in the
ReadFilewrapper, drop the list removal in theVirtualFreewrapper). Please confirm on Windows that each one turnsxmem_lazy_importsred.
Previous findings
- P2 commit-range loop restarting from
start: fixed. The loop now starts atfrom & ~(page_bytes-1)(line 932), after the lazy blocks. - P2 other kernel-mode accessors: fixed in
docs/experiments.md(listsReadFileEx, overlapped reads,WriteFilefrom a lazy page, other modules' imports). - nit shared
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
- P1: none.
- P2: none.
- nit
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. - nit
tests/xmem_lazy_imports_check.cpp: the temp file fromGetTempFileNameAis not deleted ifCreateFileAfor reading fails. Cosmetic.
AI-assisted; commands listed were executed locally.
…mp file - virtual_free_lazy: comment that the count is bumped before the release; a racing thread retries at most once more. - xmem_lazy_imports_check: delete the temp file when it cannot be reopened for reading. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks for the re-review. Both nits are done in
On the mutants: confirmed on Windows, see the table in my previous comment. Each one turns AI-assisted (Claude Code). |
tedin7
left a comment
There was a problem hiding this comment.
Re-review of head 76b995fdadb445042399d7a9339418b6ff8301c8 (previous reviews covered b4c6676 and ea454af).
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
- P1: none.
- P2: none.
- Nit: none new.
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
- The changed code is Windows-only (Win32 fault handler and CTest binaries), so I could not do a mutation run on Linux. I did not queue a Win2025 run because the delta is a comment plus test hardening. The Windows CI job passing on this head covers
xmem_lazy_imports,xmem_lazy_commitandxmem_lazy_startup.
No blocking issues in this delta.
AI-assisted; commands listed were executed locally.
R0Y00
left a comment
There was a problem hiding this comment.
One release-path blocker on 76b995fd: virtual_free_lazy erases the lazy-block record before it knows whether VirtualFree succeeded. A failed MEM_RELEASE leaves 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 try MEM_RELEASE with 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.
stan495
left a comment
There was a problem hiding this comment.
Verified head 76b995fd on Windows x64 (dev preset, MSVC 19.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.
Resolve the map_pages conflict with R0Y00#2225: keep the deferred commit for fresh slots (section_for(page, false)) and record R0Y00#2225's section-backing failure. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review of R0Y00#2209: virtual_free_lazy removed a block from the lazy list before VirtualFree ran, so a refused MEM_RELEASE left a reserved block that no first touch could commit. The release now runs under the list lock and the block leaves the list only when it succeeds. The fault handler, x2w_xmem_commit_range and x2w_xmem_commit_block hold the same lock from the lookup to the commit, so a release cannot run between them. If the fault handler cannot be installed, the switches now act as off: pages and blocks are committed up front and no import is wrapped. xmem_lazy_imports adds a refused release (nonzero size) followed by a write to an untouched page; xmem_lazy_handler_failure runs the test-controls build with the handler refused (X2W_XMEM_TEST_LAZY_HANDLER_FAILURE=1). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks. Fixed in Release path (
Handler installation
Checks (
AI-assisted (Claude Code). |
tedin7
left a comment
There was a problem hiding this comment.
Re-review at head ac423c5fdf816992f59575de1454c9f3f0ee01d5 (delta since 76b995fd: c40e3dac plus 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.- The fault handler and
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.- New tests cover the refused release (
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:
- P1: none.
- P2: none.
- Nit:
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. - Nit: a
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.
R0Y00
left a comment
There was a problem hiding this comment.
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.
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