Skip to content

Memory: writes into a sparse window are checked without VirtualQuery - #501

Open
mrp1no wants to merge 1 commit into
ivall:mainfrom
mrp1no:memory-sparse-writable
Open

mrp1no wants to merge 1 commit into
ivall:mainfrom
mrp1no:memory-sparse-writable

Conversation

@mrp1no

@mrp1no mrp1no commented Nov 24, 2026

Copy link
Copy Markdown

Two checks that a title's memory is committed and writable ask VirtualQuery: the destination of every DirectStorage request (dstorage::writable, at enqueue and at execute) and, on a CPU without CLZERO, every emulated CLZERO line or loop (eh::is_writable, outside the coherent window). VirtualQuery sizes the committed run around the address by walking it to its end, and in a sparse window that run is gigabytes. The same walk was the cost #332 removed from readable, which does not cover these two checks. Related to #278 (the is_writable guards); #314 covers the coherent window only.

In GTA VI both run against the sparse windows all the time. A walk holds the process's address space while it runs, and the title's submit thread waits for it in GetWriteWatch (title_textures::refresh_once, before each submit). The waits showed up as follows:

  • A one-page GetWriteWatch took 10–17 ms in the game and 3 µs in a standalone program.
  • Thread dumps taken while a look had waited more than 3 ms found the submit thread in ZwGetWriteWatch and two threads in VirtualQuery: xcl-dstorage and a title thread in XCL's exception handler.
  • Timed over a run, writable made 15,015 calls that took 101 s in 166 s (6.7 ms on average, at most 138 ms).

sparse::known_writable answers for a range in the sparse windows from their own record of commits. That record is committed / decommitted, which every commit and decommit path in a window already keeps: a page counts as committed when it is in a mapped tile and not a hole. The protection of the pages that are in the working set comes from K32QueryWorkingSetEx, 64 pages a call, touching none, so a guard page keeps its guard. A page the record does not hold as committed, or one in the working set without a writable protection, still takes the VirtualQuery path. The record can only answer yes.

What is in it

  • Memory: writes into a sparse window are checked without VirtualQuery:
    • known_writable, recorded_committed and resident_pages_writable in crates/xcl/src/hle/d3d12x/sparse.rs, called first by dstorage::writable (crates/xcl/src/hle/dstorage.rs) and eh::is_writable (crates/xcl/src/eh.rs).
    • Tests recorded_committed_needs_every_page_in_a_mapped_tile_and_no_hole (whole tiles, a range across tiles, a hole, a tile with no video memory) and resident_pages_writable_judges_the_pages_in_the_working_set (written, read-only and never-touched pages, and that asking leaves a page out of the working set).

Tested

Regression run
scripts/smoke.ps1 -Game <GTA VI> -Seconds 90, no input (the title stays at its first menus), release builds; main at 2997c9a, this branch's base.

main:

xcl-smoke v1
commit: 2997c9aca6ebf4577fed34ebdb3cb0a77dd9ec21
gpu: NVIDIA GeForce RTX 3060
title: dump
seconds: 90
exit: 0x0
frames shown: 1624 (PresentX 1624)
presents per 2 s (last 3): 42 42 44
device removals: 0
crashes: 0
peak commit: 15764 MB

this PR:

xcl-smoke v1
commit: c73b0c8012bd81851a76ced3cd129dc9a16c823a
gpu: NVIDIA GeForce RTX 3060
title: dump
seconds: 90
exit: 0x0
frames shown: 2402 (PresentX 2402)
presents per 2 s (last 3): 59 61 60
device removals: 0
crashes: 0
peak commit: 14469 MB

Not tested

AI use

  • Claude Code, Claude Opus 5.5: the diagnosis (timing, thread dumps, the standalone GetWriteWatch program), the code, the tests and this description. The runs and tests were made on the hardware named above.

🤖 Generated with Claude Code

Two checks that the title's memory is committed and writable asked
VirtualQuery: the destination of every DirectStorage request
(dstorage::writable) and every emulated CLZERO line or loop on a CPU
without CLZERO (eh::is_writable, outside the coherent window).
VirtualQuery sizes the committed run around the address by walking it
to its end, and in a sparse window that run is gigabytes. A retail
game's requests and CLZERO lines there took 6.7 ms a check on average
and up to 138 ms, and while a walk ran its submit thread waited in
GetWriteWatch (title_textures::refresh_once): a one-page GetWriteWatch
took 10-17 ms in the game and 3 us outside it.

known_writable answers for a range in the sparse windows from their
own record of commits (committed / decommitted: every page in a mapped
tile and not a hole), and asks the working set for the protection of
the pages in it (K32QueryWorkingSetEx, 64 pages a call, touching
none). A page the record does not hold as committed, or one in the
working set without a writable protection, still takes the
VirtualQuery path, so the record can only answer yes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant