Skip to content

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

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

mrp1no wants to merge 1 commit into
xcl-dev: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 the retail title 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. The title's own protections there are recorded too: its VirtualProtect and a commit with a protection other than read-write mark the pages. A no-access or guard page leaves the working set, so the working-set check alone would miss it. Protections set before the device exists are kept with the early commits and marked when init replays them. A page the record does not hold as committed and read-write, 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).
    • protection_changed (sparse.rs), called by the VirtualProtect / VirtualProtectEx hooks and commit_sparse (crates/xcl/src/hle/kernelx.rs); before the device it keeps the ranges in EARLY_NOT_RW, which init marks after replaying the early commits, both under the state's lock.
    • On top of Memory: decommitted sparse tiles stay mapped until the next submit #532's parked tiles and D3D12.X: the late-release drain gets the sparse queue without the sparse state's lock #968's queue handle (QUEUE is replaced before the state, as on main): State::commit_range clears a page's not_rw mark with its hole (a page committed again is read-write until the title says otherwise), and init replays the early commits through it. A tile taken back from parked comes back as a new entry, read-write.
    • 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, a page the title made non-writable and then read-write again) 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) and a_protection_before_the_device_is_marked_after_the_early_commits; Memory: decommitted sparse tiles stay mapped until the next submit #532's a_tile_decommitted_and_committed_again_keeps_its_video_memory also checks that a page made non-writable is read-write after the tile is taken back from parked, and after a decommit and commit of that page alone in a tile that stays mapped (that check fails without the not_rw line in commit_range).

Tested

Regression run
scripts/smoke.ps1 -Game <game folder> (60 s), no input (the title stays at its first menus), release builds; main at c160d86, this branch's base.

main:

xcl-smoke v1
commit: c160d86ef2efe3a07e4dfb473e4fa72eafb413c0
gpu: NVIDIA GeForce RTX 3060
title: dump
seconds: 60
exit: 0x0
frames shown: 1620 (PresentX 1620)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14560 MB

this PR:

xcl-smoke v1
commit: 7c835573cbf70eabfd84149dfc7775006983c101
gpu: NVIDIA GeForce RTX 3060
title: dump
seconds: 60
exit: 0x0
frames shown: 1619 (PresentX 1619)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14557 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

@amirshaker000

Copy link
Copy Markdown

Cross-vendor run of c73b0c8 on an RTX 3070 (wave32, Turing-class driver line), merged onto current main. This PR had no NVIDIA test report and only an Intel CPU, so this adds the GPU vendor and, on this CPU, the eh::is_writable emulated-CLZERO path is the same one your runs took.

Tree: main 7ebf1b8 (merge of #486, the current head) + this PR at c73b0c8, merged locally with no conflicts (3 files, +122). Control: main 7ebf1b8 on its own.

Tested

  • NVIDIA GeForce RTX 3070 8 GB, driver 32.0.16.2114 (NVIDIA branch 611.4-class), i5-12400 (no CLZERO: XCL emulates it), 32 GB RAM, Windows 11 Pro build 26300. rustc 1.97.1 MSVC, cargo 1.97.1, x86_64-pc-windows-msvc, debug profile. DXC 1.8.2502.11 (dxcompiler.dll, Windows SDK 10.0.26100.0, picked by XCL's search order; XCL_DXC_DIR unset).
  • cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4, both trees built and run the same way:
tree passed failed ignored SKIPPED
main 7ebf1b8 (control) 677 0 41 2
main + c73b0c8 679 0 41 2
  • The +2 are this PR's own tests, both ok by name: hle::d3d12x::sparse::tests::recorded_committed_needs_every_page_in_a_mapped_tile_and_no_hole, hle::d3d12x::sparse::tests::resident_pages_writable_judges_the_pages_in_the_working_set.
  • SKIPPED on this adapter, identical on both trees: gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask, gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware (no wave64).
  • No device removals, no crashes in either run.

On the code. Read the full diff. known_writable answers yes only when every page is in mapped with no hole and no resident page has a non-writable or guard protection; everything else falls back to VirtualQuery, so it cannot say yes where the old path said no, except for a committed page outside the working set, which is the documented assumption. One shape worth knowing, not a defect: recorded_committed is one hash lookup per 4 KiB page under STATE, so an emulated CLZERO loop over a large range is still linear in pages. Far cheaper than the VirtualQuery walk it replaces, just not constant.

Not tested

  • AMD GPUs and AMD CPUs (native CLZERO: only the DirectStorage check gains there).
  • Intel GPUs; a release build.
  • Any title, including the world load. No game data was used for this report, so I have no xcl-smoke block to add to yours.
  • The timing improvement itself: I did not reproduce the 10–17 ms GetWriteWatch figure, only that the suite is green and the mechanism is as described.

AI use: Kilo with GLM-5.4 ran the commands and drafted this report; I reviewed the diff and the numbers before posting.

@brothernog

Copy link
Copy Markdown

Tested c73b0c8 on an RTX 3050 (driver 610.88), Windows 11 Home 26300, Rust 1.100.0, debug. cargo test -p xcl --lib sparse: 15 passed, 0 failed, 1 ignored.

One gap in the protection check: resident_pages_writable only judges pages with the Valid bit set, and a page that a title has protected usually doesn't have it. I added a temporary test in sparse.rs (removed afterwards):

  • committed RW pages, touched, then VirtualProtected to PAGE_READWRITE | PAGE_GUARD and to PAGE_NOACCESS, plus a committed page set to PAGE_READONLY and never touched
  • resident_pages_writable returns true for all three.

The title's VirtualProtect goes straight through (hle/kernelx.rs:469), and the sparse record (committed / holes) doesn't see it. So for a committed sparse page the title has guarded, made no-access or made read-only, known_writable says yes where main's VirtualQuery says no:

I confirmed the first half (resident_pages_writable) by running it. The known_writable path as a whole I only read, since I didn't set up a sparse window for it. One way to keep the speedup: record a title's VirtualProtect on sparse-window pages in the kernelx hook and have known_writable refuse those pages, rather than inferring protection from the working set.

This review was AI-assisted.

@mrp1no

mrp1no commented Nov 24, 2026

Copy link
Copy Markdown
Author

Thanks, confirmed: a title's VirtualProtect to no-access or guard takes the page out of the working set, so resident_pages_writable never saw it, and the record alone said yes.

Fixed as you suggested, squashed into the one commit (now 81ef881). The kernelx VirtualProtect and VirtualProtectEx hooks (own process), and commit_sparse with a protection other than read-write, now mark the pages in the sparse record (not_rw). A read-write protection or a fresh commit clears the mark. known_writable does not answer for a marked page, so it takes the VirtualQuery path as on main. The case is in recorded_committed_needs_every_page_in_a_mapped_tile_and_no_hole: a page made non-writable is refused, the rest of its tile is still answered, and the page is answered again once read-write.

The suite and the smoke run on the new head are in the description.

Written with Claude Code, Claude Opus 5.5.

@Kalmarv

Kalmarv commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

At 81c43fe (merged onto main 63cdc25 as 98cc9c9, clean with git merge-tree): the gap in the first bullet below is fixed, and the suite is green. It needs a rebase onto main f4ce27c now: see the last point.

  • How I checked the fix. Read the diff from 81ef881 (sparse.rs only): a protection set with no sparse state is kept in EARLY_NOT_RW, an early commit takes its pages out, and init replays the early commits and then marks those ranges, all under STATE. STATE is taken before EARLY and EARLY_NOT_RW at every site. Ran the new test a_protection_before_the_device_is_marked_after_the_early_commits: it passes, and with early_protection made never to add a range it fails (left: []).
  • What that test does not hold. It calls the helpers and does the replay by hand. With the marking loop taken out of init, or with committed no longer taking an early commit's pages out of EARLY_NOT_RW, all three tests of the PR still pass. init needs a device, so those two places are read, not run.
  • The test comment ("Sub-tile decommits …") is back on its own test.
  • It no longer merges onto main. D3D12.X: the sparse state is replaced outside its lock #443 was merged after 63cdc25 (d852f9a), and 81c43fe conflicts with main f4ce27c in sparse::init: both rewrite the line that installs the state. For the rebase: g.insert(s) drops a state it replaces under the lock, which is what D3D12.X: the sparse state is replaced outside its lock #443 moved out of it (D3D12.X: replacing the sparse state deadlocks when the old state still holds placed buffers #344). The old state has to outlive the guard, for example let old = g.replace(s) and drop(old) after drop(g). main has a test for that now, tests/sparse_reinit.rs. Read, not run.

The report below is from 81ef881. Its timings, costs and four mutations were not rerun.


Tested 81ef881 merged onto main cb868b2 (clean with git merge-tree) on an AMD CPU, which the description lists as not tested. The suite is green and the tests hold the code. One gap in the new protection record, read and not run.

  • Protections set before the device exists are not recorded (read, not run). Fixed at 81c43fe, see above. protection_changed does nothing while there is no sparse state (sparse.rs:1366). Commits made that early are kept as page ranges only (EARLY), and init replays them through committed (sparse.rs:553), so each of those pages starts with not_rw clear. A page the title commits as no-access or guard, or protects, before it creates its device is answered yes by known_writable once the device exists, since such a page is not in the working set either. Keeping the protection with the early ranges would close it. Whether a title protects sparse pages that early I do not know.
  • The cause reproduces without a title. Scratch test: 1 GiB committed in one run, write-watched, every page resident. VirtualQuery at its start takes 8.9 ms (0.3 µs when asked 64 KiB before its end). A clean GetWriteWatch over the GiB takes 1.2 ms alone and 13 ms while another thread calls VirtualQuery at the start. While another thread calls resident_pages_writable over 16 MiB of it instead, the look takes 0.5 ms: the new check does not hold it up. Another job was running on the PC; on the earlier head c73b0c8, with the PC quieter, the same test gave 4.7 ms, 0.70 ms, 7.0 ms and 0.48 ms.
  • What the new check costs (debug, same run): resident_pages_writable 1.1 µs for 64 KiB, 8.3 µs for 1 MiB, 224 µs for 16 MiB, 5.5 ms for 256 MiB; recorded_committed 189 µs for 16 MiB of mapped tiles, under STATE. Both are linear in pages, as @amirshaker000 wrote: a destination of about 250 MiB would cost what the walk of the GiB cost. Only a note unless requests come that large.
  • The tests hold the code. Four mutations, each built and run on the merge. recorded_committed made to ignore a tile's holes, or its not_rw bits, and mark_protection made never to set a bit: recorded_committed_needs_every_page_in_a_mapped_tile_and_no_hole fails each time. resident_pages_writable made to ignore a resident page's protection: resident_pages_writable_judges_the_pages_in_the_working_set fails. Neither test builds on main (new functions), so the mutations are the control.
  • Small. Fixed at 81c43fe. The two tests went in between the doc comment "Sub-tile decommits (two 32 KiB halves, …" and its test decommit_sub_tile_reclaims_pool_tile_when_all_pages_become_holes, so that comment now sits on the first new test.

Tested

  • RTX 5070 Ti (NVIDIA 610.74), Ryzen 9 9950X (CLZERO is native, so only the DirectStorage check can gain here), Windows 11 26200, Rust 1.99, debug.
  • At 81c43fe, merged onto 63cdc25 as 98cc9c9:
    • cargo build --workspace, then cargo test --workspace -- --test-threads=4: 695 passed, 0 failed, 41 ignored over 16 test binaries (lib 655 passed, 21 ignored). SKIPPED at run time: the same two tests as below (no wave64).
    • cargo test -p xcl --lib -- recorded_committed_needs resident_pages_writable_judges a_protection_before_the_device: 3 passed, 0 failed. With early_protection never adding a range: 2 passed, 1 failed. With init not marking the early protections, and with an early commit not clearing them: 3 passed, 0 failed each.
  • At 81ef881, merged onto cb868b2 as daddb1b:
    • cargo build --workspace, then cargo test --workspace -- --test-threads=4: 687 passed, 0 failed, 41 ignored over 16 test binaries (lib 647 passed, 21 ignored). SKIPPED at run time: gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware (no wave64 on this GPU).
    • cargo test -p xcl --lib -- recorded_committed_needs resident_pages_writable_judges: 2 passed, 0 failed.
    • The scratch timing test and the four mutations. They are not in the PR. Timings are medians of 15.

Not tested

  • known_writable itself, its two call sites, and the new hooks in kernelx.rs (read_write, note_protection, the branch in commit_sparse, and committed clearing not_rw): no unit test calls them, mine included. The same goes for init's replay and the early branches of protection_changed and committed at 81c43fe. Whether the e2e title reaches them I did not check.
  • Any title with this PR, so neither the gain nor the world load. On this PC the first menus of a retail title showed no such waits on main 508a0c7 (a one-page GetWriteWatch took 1.5 µs, 8 µs at most; D3D12.X: the write-watch look scans 1.5 GiB before every submit while 4 pages are written (measurement and plan) #582), which fits a CPU that does not emulate CLZERO.
  • A release build, AMD and Intel GPUs, Intel CPUs.

🤖 Claude Fable 5.2

@Chrysana

Copy link
Copy Markdown

Test report for 81ef881 on an AMD GPU (RX 9070 XT), which Not tested lists, on an AMD CPU that runs CLZERO natively (so only the DirectStorage check applies here, as the description says). No regression.

Setup: AMD Radeon RX 9070 XT 16 GB, driver 32.0.32015.2008, Ryzen 7 9800X3D, 31 GB RAM, Windows 11 25H2 build 26200.9974, rustc 1.100.0 x86_64-pc-windows-msvc, DXC 1.9.2602.17. Retail title 1.0.2.1. Debug build for the tests, release builds for the runs.

Tests: cargo test --workspace --no-fail-fast -- --test-threads=4 at 81ef881: 678 passed, 0 failed, 41 ignored. SKIPPED: the two debug-layer tests (the D3D12 debug layer isn't installed here).

Title runs: scripts/smoke.ps1 -Game <folder> -Seconds 60 with XCL_PRESS_A_AT=14,17,21, same saved state. 81ef881 is 22 commits behind main, so it was also merged onto main 18c105a (clean, local merge 2b97ae1, not pushed):

build frames peak commit DirectStorage requests
main 18c105a 1170, 1183 15428, 15441 MB 9767, 0 failed
81ef881 merged onto 18c105a 1181, 1189 15437, 15433 MB 9767, 0 failed
81ef881 (its own base 2997c9a, without #451: the intro video at ~5 fps) 492 15806 MB 18902, 126 failed
  • On 18c105a the title reaches its first menu a little later, so the scripted presses (last at 21 s) come too early and the runs stay in the menus. They cover the DirectStorage requests made there, not the video's streaming or the world load.
  • At its own head the title reaches the intro video and its streaming. The 126 failed requests are the BCPack refusals (BC1/BC5/BC7) that main also has when it gets there.
  • No crash or device removal in any run.

Regression run:

main:

xcl-smoke v1
commit: 18c105af86929d6ae26c44096c85b66f1644e743
gpu: AMD Radeon RX 9070 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1183 (PresentX 1183)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15441 MB

PR merged onto main:

xcl-smoke v1
commit: 2b97ae1fe49ba0631a85106c3b597623d0d77256
gpu: AMD Radeon RX 9070 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1181 (PresentX 1181)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15437 MB

PR head:

xcl-smoke v1
commit: 81ef881a3e3f3167279d7752e6b6ec3c4246a40f
gpu: AMD Radeon RX 9070 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 492 (PresentX 492)
presents per 2 s (last 3): 10 10 10
device removals: 0
crashes: 0
peak commit: 15806 MB

Not tested: the emulated CLZERO path (this CPU has CLZERO); the world load; the time spent in the check itself (no profile); Kalmarv's point about protections set before the device exists.

AI use: Claude Code with Claude Opus 5.5 built the commits, ran the tests and the title runs, read the logs, and wrote this report. The runs were made on my machine.

@mrp1no
mrp1no force-pushed the memory-sparse-writable branch from 81ef881 to 81c43fe Compare November 24, 2026 03:08
@mrp1no

mrp1no commented Nov 24, 2026

Copy link
Copy Markdown
Author

Thanks for the run on AMD and the mutations. Both points are fixed in the one commit, now 81c43fe.

  • Protections before the device. protection_changed with no sparse state now keeps the range in EARLY_NOT_RW, page ranges like EARLY. A non-read-write protection adds the range and a read-write one takes it out; an early commit takes its pages out too, as committed clears not_rw. init replays the early commits and then marks those ranges. It does both under the state's lock, so a commit or a protection on another thread cannot come between the replay and the state being there. (Before, the early commits were replayed after the state was published; a protection set in between could be cleared by the replayed commit.) committed's body is now commit_in, shared by committed and the replay. The record is covered by the new test a_protection_before_the_device_is_marked_after_the_early_commits.
  • Test placement. The "Sub-tile decommits …" comment is back on decommit_sub_tile_reclaims_pool_tile_when_all_pages_become_holes.

Noted on the linear cost. Requests of about 250 MiB would cost what the walk did; I have not seen DirectStorage destinations that large here.

Thanks also to @Chrysana for the AMD runs (RX 9070 XT, native CLZERO): no change in frames or DirectStorage requests merged onto main.

The suite and the smoke run on the new head are in the description.

Written with Claude Code, Claude Opus 5.5.

@ivall

ivall commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Not merged yet: it now conflicts with #532 (merged), which parks decommitted tiles; committed became commit_range with the parked-tile take-back, so commit_in and the not_rw marks need to be redone on top of it rather than merged textually. Kalmarv's earlier note about rebasing onto current main still applies. The direction is wanted (#582's walks): a rebase onto 02bfd1d would get it in.

@mrp1no

mrp1no commented Nov 24, 2026

Copy link
Copy Markdown
Author

Rebased onto 47efa2e (it has 02bfd1d); still one commit, now ce50c24. The marks are redone on top of #532 rather than merged:

  • commit_in is gone. State::commit_range clears a page's not_rw bit along with its hole, so a page committed again is read-write until the title says otherwise. init replays the early commits through commit_range, then marks the early protections, under the same lock as before.
  • A tile taken back from parked is a new TileEntry, so it comes back read-write, like a fresh commit. flush_parked and decommit_range need nothing from this change: a parked tile is not in mapped, and recorded_committed only answers yes for pages in mapped.
  • Memory: decommitted sparse tiles stay mapped until the next submit #532's a_tile_decommitted_and_committed_again_keeps_its_video_memory now also checks two things:
    • a page made non-writable is read-write once the tile is taken back from parked;
    • a page decommitted and committed on its own, in a tile that stays mapped, is read-write again. This second check fails without the not_rw line in commit_range.

cargo test --workspace --no-fail-fast -- --test-threads=4 on ce50c24: 744 passed, 0 failed, 41 ignored (2 SKIPPED, no wave64). New smoke blocks for main 47efa2e and ce50c24 are in the description: 1623 and 1636 frames, no crash, 14555 / 14545 MB peak commit.

(Claude Code, Claude Opus 5.5)

@thehollowedone

thehollowedone commented Nov 24, 2026 •

Copy link
Copy Markdown

Independent DirectStorage-to-GPU check on ce50c24 applied to 930e8b4, compared with unmodified 930e8b4. Windows 11 build 26300, Ryzen 7 9800X3D, RTX 3080 Ti / 621.35, Rust 1.99.1 MSVC, debug.

I used a synthetic file queue to read 8,192 bytes into two pages that straddled a 64 KiB sparse tile boundary. I repeated with different bytes after decommitting and recommitting the same destination. The PR’s known_writable answered true in both phases. Both builds returned S_OK and reported the same two dirty pages through GetWriteWatch. After the sparse upload, a native D3D12 copy from the sparse buffer to a separate readback buffer matched all 8,192 bytes in each phase on both builds.

Follow-up timing on the same revision pair, with a release build. I ran the original writable helper from 930e8b4 and the PR helper in one process over the same native D3D12 sparse allocations. Each of three pairs sampled 64 checks of two-page destinations at four offsets. At 64 MiB, the median was 60.9–63.0 µs before and about 0.4 µs after. At 512 MiB, it was 409.6–414.9 µs before and about 0.4 µs after (p95 672.4–684.2 µs before, about 0.4 µs after). Both helpers rejected a read-only page and accepted it again once the title restored read/write access. This measures permission-check latency in isolation.

This checks preservation of DirectStorage and GPU data across recommit. Throughput, CLZERO emulation, and retail behavior were outside this run. AI-assisted. Codex (GPT-6, exact variant unavailable) wrote and ran the scratch fixtures and drafted this report.

@mrp1no

mrp1no commented Nov 24, 2026

Copy link
Copy Markdown
Author

Thanks for the check. A DirectStorage write that crosses a tile boundary, followed by a decommit and recommit of the same destination, is exactly the path the commit record has to get right. It is good to have it confirmed on another NVIDIA card through a GPU readback.

(Claude Code, Claude Opus 5.5)

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). The title's protections there are recorded too: its
VirtualProtect and a commit with a protection other than read-write
mark the pages, which the record then does not answer for (a no-access
or guard page leaves the working set, so the working-set check alone
would miss it). Protections set before the device exists are kept
with the early commits and marked when init replays them, under the
state's lock. A page the record does not hold as committed and
read-write, 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>
@mrp1no
mrp1no force-pushed the memory-sparse-writable branch from ce50c24 to 7c83557 Compare November 24, 2026 19:41
@mrp1no

mrp1no commented Nov 24, 2026

Copy link
Copy Markdown
Author

Rebased again, now on c160d86; still one commit, now 7c83557. The only conflict was in init with #968's QUEUE. The queue is still replaced before the state, as on main, and the early commits and protections are replayed under the state's lock, as before.

cargo test --workspace --no-fail-fast -- --test-threads=4: 861 passed, 0 failed, 42 ignored. Smoke on main c160d86: 1620 frames; on 7c83557: 1619 frames, no crash, 14560 / 14557 MB peak commit (blocks in the description).

(Claude Code, Claude Opus 5.5)

@throny

throny commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Correction (2026-11-25): this is not caused by this PR. The PR's base, main c160d86 without the PR, does exactly the same here now: 42 GB by about 26 s, stopped by the guard. Yesterday the same commit ran normally on this machine (peak 15.8 GB). So something in the data the newer builds left on this machine (caches, the list of pipelines to preload, …) makes older builds blow up, and it hits every PR on an older base the same way (#1540, #1642 and #1811 too). Please disregard the smoke part below. The suite result (861 / 0 / 42) stands. I'll redo the smoke pair with a fresh data folder for both runs.


Test report for exact head 7c83557: the suite passes, but the title's commit runs away in the smoke run, 3 of 3 times.

Tested

  • Windows 11 Pro 10.0.26300, NVIDIA GeForce RTX 4080 (driver 610.88), Intel i9-12900K, 64 GB (commit limit 68 GB); Rust 1.100.0 MSVC; tests in debug, title runs in release.
  • cargo build --workspace && cargo test --workspace -- --test-threads=4 on 7c83557 (its own base): 861 passed, 0 failed, 42 ignored.
  • scripts/smoke.ps1, the retail title, 60 s, release builds: main 568e7db runs normally (1 599 – 1 603 frames, peak commit 15.8 GB, 0 removals or crashes, twice). This head was stopped by my memory guard (free system commit < 8 GB) in all 3 runs, once with its own separate build folder, so it is not a stale binary.
  • The process commit from the PR's log (memory: … MB committed by the process): 4.4 GB at 2 s, 11.0 GB at 10 s, 17.0 GB at 16 s, then 22.3 GB at 20 s, 29.3 GB at 22 s, 36.3 GB at 24 s, about 3.5 GB/s, while the title is still in its menus (4 096 draws, 432 dispatches so far). On main the same run stays under 16 GB for the whole 60 s.
  • Not from the video-memory arenas: those reach 0.3 GB in total.

So something in the sparse-window path seems to commit or allocate far more than before. I didn't find where from the diff (the K32QueryWorkingSetEx batch is a fixed array). Something treating pages as not committed and committing them again would fit. I can rerun with a setting or log line you name.

Not tested: AMD, the head merged onto current main, a world load.

AI use: Claude Code (Claude Opus 5.5) ran this and wrote the report; the diff was read for safety by a Claude Opus 5.5 sub-agent before it was built.

@xpatfussy69x

Copy link
Copy Markdown

Test report for head 7c83557 as pushed (not merged onto current main) – NVIDIA, wave32. Suite only: no title run.

Tested

  • NVIDIA GeForce RTX 3060 12 GB (Ampere, 32-lane waves only), driver 32.0.16.2124; AMD Ryzen 5 7500F; 32 GB RAM; Windows NT 10.0.26300; Rust 1.100.0 (stable-x86_64-pc-windows-msvc); debug; DXC 1.9.2609.5
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 861 passed, 0 failed, 42 ignored
  • SKIPPED GPU tests on this adapter: 2 (no wave64), 1 (no heap windows)
  • this PR's hle::d3d12x::sparse tests ran and passed, including resident_pages_writable_judges_the_pages_in_the_working_set, recorded_committed_needs_every_page_in_a_mapped_tile_and_no_hole and a_protection_before_the_device_is_marked_after_the_early_commits
  • no XCL_SAMPLE_* data set (game-data tests not run)

Not tested

  • a scripts/smoke.ps1 pair on a title, so nothing here about the commit growth reported in the smoke run above; AMD GPUs / native wave64; release build; the head merged onto current main

AI use

  • Claude (Claude Opus 5.5) wrote the script that ran the suite on my PC, read the log and wrote and posted this report with my approval; the run is mine.

@throny

throny commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Correction to my report above: the memory climb is not from this PR. Its base c160d86 without the PR does the same on this machine now, so it comes from leftover data of newer builds here, not from your change. I've marked the report accordingly. Sorry for the false alarm.

AI use: Claude Code (Claude Opus 5.5).

@throny

throny commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Redone smoke pair, replacing the one in my report above. Test report for exact head 7c83557.

Tested

  • Windows 11 Pro 10.0.26300, NVIDIA GeForce RTX 4080 (driver 610.88, 32-lane waves), Intel i9-12900K, 64 GB; Rust 1.100.0 MSVC; DXC 1.9.2602.17; tests in debug, title runs in release.
  • cargo build --workspace && cargo test --workspace -- --test-threads=4 on 7c83557 as it is (its own base): 861 passed, 0 failed, 42 ignored. SKIPPED: 3 lines (no wave64 on this adapter, and the like).
  • scripts/smoke.ps1, the retail title, 60 s, release builds, main 568e7db first, then this head. Both runs started without the preload list newer builds leave behind (used-pipelines.txt in the DXIL cache), which makes builds on an older base run out of memory at start:

main:

xcl-smoke v1
commit: 568e7db7729c8fa6c8f0f4b32d798faaf11462bb
gpu: NVIDIA GeForce RTX 4080
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1612 (PresentX 1612)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14576 MB

PR:

xcl-smoke v1
commit: 7c835573cbf70eabfd84149dfc7775006983c101
gpu: NVIDIA GeForce RTX 4080
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1610 (PresentX 1610)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14764 MB

Not tested: AMD, the head merged onto current main, a world load.

AI use: Claude Code (Claude Opus 5.5) ran this and wrote the report; the diff was read for safety by a Claude Opus 5.5 sub-agent before it was built.

@SzczepanGrela

Copy link
Copy Markdown

Linux/RADV + Wine GPU test report on PR #501 head 7c83557 (7c835573cbf70eabfd84149dfc7775006983c101, local merge f818dc0b3a6e12ca4c8372998eef614f17a8d20c onto origin/main 568e7db7729c8fa6c8f0f4b32d798faaf11462bb).

Setup

  • OS/kernel: CachyOS Linux (7.3.0-5-cachyos)
  • GPU/driver: AMD Radeon RX 9070 (RADV GFX1201, native wave64) (device 0x7550, Vulkan 1.4.354, Mesa RADV 26.2.5-arch3.1)
  • Wine/DX: GE-Proton11-12 (wine-11.0 (Staging)), vkd3d-proton v3.1, DXVK dxgi.dll, DXC 1.9.2602.17 (21d28f72)
  • Build/env: rustc 1.97.1, cargo test --target x86_64-pc-windows-gnu --release --workspace --no-run (44.48s, exit 0, zero compiler warnings), XCL_REQUIRE_GPU=wave64 XCL_NETWORK=0

Results

  • cargo test --target x86_64-pc-windows-gnu --release -p xcl --lib hle::d3d12x::sparse::tests -- --nocapture: 18 passed, 0 failed, 1 ignored (0.25s, exit 0; early_commit_timing ignored), vs baseline 568e7db (hle::d3d12x::sparse) 15 passed, 0 failed, 1 ignored (0.24s, exit 0). All 3 new tests (resident_pages_writable_judges_the_pages_in_the_working_set, recorded_committed_needs_every_page_in_a_mapped_tile_and_no_hole, a_protection_before_the_device_is_marked_after_the_early_commits) and a_tile_decommitted_and_committed_again_keeps_its_video_memory passed.
  • cargo test --target x86_64-pc-windows-gnu --release -p xcl --lib hle::dstorage::tests: 18 passed, 0 failed, 0 ignored (0.33s, exit 0), matching baseline 568e7db 18 passed, 0 failed, 0 ignored (0.26s, exit 0).
  • cargo test --target x86_64-pc-windows-gnu --release -p xcl --lib eh::tests: 6 passed, 0 failed, 0 ignored (0.06s, exit 0), matching baseline 568e7db 6 passed, 0 failed, 0 ignored (0.06s, exit 0).
  • cargo test --target x86_64-pc-windows-gnu --release -p xcl --lib hle::kernelx::tests: 36 passed, 0 failed, 1 ignored (0.54s, exit 0; scatter_timing ignored), vs baseline 568e7db default parallel 35 passed, 1 failed, 1 ignored in 2 of 2 runs (0.53s each, exit 101; a_page_whose_move_fails_stays_mapped_where_it_was at crates/xcl/src/hle/kernelx.rs:2656:13) and 36 passed, 0 failed, 1 ignored with -- --test-threads=1 (1.27s, exit 0).
  • cargo test --target x86_64-pc-windows-gnu --release -p xcl --lib hle::d3d12x -- --test-threads=4: 159 passed, 21 failed, 2 ignored (1.72s, exit 101), vs baseline 568e7db 156 passed, 21 failed, 2 ignored (1.63s, exit 101; +3 passed from sparse::tests, identical set of 21 pre-existing Wine COM vtable HRESULT(0x80070057) / PoisonError / gpu_depth_buffer_created_for_unordered_access_is_placed failures).
  • cargo test --target x86_64-pc-windows-gnu --release -p xcl --lib gpu::recomp::tests::gpu_sparse_vram_descriptors_buffers_and_xmem -- --exact: 0 passed, 1 failed, 0 ignored (0.53s, exit 101; HRESULT(0x80070057) at crates/xcl/src/gpu/recomp/tests.rs:4116:124), matching baseline 568e7db 0 passed, 1 failed, 0 ignored (0.27s, exit 101, same file, line, and error).
  • cargo test --target x86_64-pc-windows-gnu --release --test sparse_reinit: 0 passed, 1 failed, 0 ignored (0.63s, exit 101; xcl: sparse windows: Invalid parameter. (0x80070057) / the sparse windows were set up again at crates/xcl/tests/sparse_reinit.rs:38:9), matching baseline 568e7db 0 passed, 1 failed, 0 ignored (0.27s, exit 101, same file, line, and error).

Not tested

  • No retail game workload and no scripts/smoke.ps1 (no retail title files locally).
  • #[ignore] timing tests (early_commit_timing, scatter_timing, churn_update_tile_mappings_timing) and --lib / --test targets outside those listed above.
  • gpu_sparse_vram_descriptors_buffers_and_xmem and sparse_reinit hit the pre-existing Wine real::init() HRESULT(0x80070057) failure present on baseline 568e7db.

AI use

  • Diff audit, test execution, and comment drafting used Antigravity (Gemini 4 Neon) multi-agent verification, reviewed by me before posting.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

@Royalflamejlh

Copy link
Copy Markdown
Contributor

The title with a timer on dstorage::writable, with and without #501, on Linux (Wine + vkd3d-proton) and native Windows, both NVIDIA: checks 16,385 to 32,768 (the last of them at 44–91 s) took 22 to 59 µs each on average, with #501 and without it. The write-watch scans took as long with #501 as without. The description's 6.7 ms per check (15,015 checks, 101 s in 166 s) was measured on a CPU without CLZERO, where every emulated CLZERO walks with VirtualQuery too. Both CPUs here run CLZERO natively, so that path never ran. That may be why the numbers differ (a guess). The description's runs were also on an older main (2997c9a).

Setup: main 06c9f55 + #684 e56dbc3 (the #662 fault otherwise) + a timer on writable, logged at powers of two (7a835ef), against the same + #501 7c83557, merged locally as 38ffa23. #501 now conflicts with main 2898511. Release, Series X profile, XCL_PSO_PRELOAD=0.

Both start from a saved profile: the first-run screens were completed once; no story progress.

Linux, without Linux, #501 Windows, without Windows, #501
checks 16,385 to 32,768, per check 38, 44, 38 µs 59, 42, 27 µs 22, 27 µs 25, 24 µs
longest call, to the 32,768th check 57, 50, 56 ms 18, 45, 20 ms 10, 9 ms 10, 3 ms
the 32,768th check at 44.4, 45.3, 48.4 s 48.8, 45.2, 44.3 s 90.9, 91.2 s 90.2, 90.8 s
write-watch scans at 2,048 submits (Linux) / 4,096 (Windows) 1,158, 1,372, 1,499 ms 1,538, 1,427, 1,104 ms 6,650, 6,827 ms 7,402, 6,771 ms
  • On Windows all 4 runs stopped at 98–100 s on the title's assert (0x80000003 at game+0x193e00f, rax 0x8007000e) after heap creations were refused with 0x8007000e: the 8 GB card running out, as before. On Linux one run with Memory: writes into a sparse window are checked without VirtualQuery #501 had a device removal in XCL's queue drain after its 120 s end; the other five logged 'GPU fences still pending after 10 s' there.

Not tested: the world load on Windows; the checks after the 32,768th (after 44–48 s on Linux and 90–91 s on Windows: the timer prints at powers of two and no run reached 65,536); a CPU without CLZERO and its eh::is_writable path; the test suite; AMD and Intel GPUs.

AI use: Claude Code (Claude Opus 5.5) wrote the timer, ran the title on both systems and wrote this comment.

Posted without review by a person: the account owner was away and reads replies later.

@bmjask bmjask left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 protection-tracking bypass: VirtualProtectEx records protection only for pseudo GetCurrentProcess(), not a real self handle. A title can make untouched sparse page READONLY/NOACCESS through real self handle; not_rw is never set, and known_writable accepts nonresident page because protection is checked only for Valid WS entries. DirectStorage/CLZERO can then be permitted to faulting destination. Use CompareObjectHandles/same-process identity and add real-self-handle protected-page plus insufficient-access tests. This mirrors #1191.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.