Skip to content

D3D12.X: readable checks remember pages and probe instead of asking Wine - #3021

Draft
voiceovarian1-max wants to merge 1 commit into
xcl-dev:mainfrom
voiceovarian1-max:pr/nv-readable-cache
Draft

voiceovarian1-max wants to merge 1 commit into
xcl-dev:mainfrom
voiceovarian1-max:pr/nv-readable-cache

Conversation

@voiceovarian1-max

Copy link
Copy Markdown

For nvidia-dev. shader::readable guards XCL's reads of title pointers: vtables in is_adopted, the title's view descs, code bytes. Under Wine each answer is a system call (K32QueryWorkingSetEx / VirtualQuery) under Wine's virtual-memory lock, ~20 µs each and more while other threads commit or free memory. In a retail title's world there were 70,000–126,000 checks per 2 s, about 4 per draw or dispatch, which took 1.5–2.8 s of the title's threads per 2 s.

There are two parts:

  • Page memory (all platforms). A thread remembers the pages it found readable (ranges of up to 4 pages) for 250 ms, in a ring of 64. A page freed within that time still reads as readable on that thread. The checks guard against garbage pointers, which point at pages never seen readable. readable_fresh skips the remembered pages for memory that may have been freed since: the next page of an instruction (eh::code_bytes_at).
  • Probe (Wine only). A range of up to 16 pages is read a byte a page with IsBadReadPtr instead of being asked of the system. Wine's IsBadReadPtr catches access violations only, and a guard page it read ended the process. So during the probe a vectored handler puts the guard back and turns the guard-page fault into an access violation. XCL_READABLE_PROBE=0 asks the system, as before.

What is in it

  • gpu/shader.rs: SEEN, readable_fresh, probe_on, probe_guard_page, readable_asked (the old body).
  • eh.rs: code_bytes_at uses readable_fresh.
  • hle/d3d12x/list_x.rs: the GPU-address test checks the freed page on a thread of its own, because the page it bound is remembered on its thread. gpu/shader.rs's working-set test expects the probe's answer under Wine.

Tested

  • Linux (CachyOS, kernel 7.3), Proton Experimental (Wine 11.0) with vkd3d-proton 3.1, RTX 4070 Ti 12 GB, NVIDIA 620.73.04, i7-14700K. Rust 1.100.0, x86_64-pc-windows-gnu, release. Head 14842863 on nvidia-dev 3f39671d.
  • Whole lib suite under Wine (cargo test -p xcl --lib -- --test-threads=4): 1691 passed, 8 failed, 28 ignored. The same 8 tests fail on nvidia-dev 3f39671d.
  • Retail-title runs on our Linux tree, which carries the same change, not this branch:
    • with page memory only: checks asked of the system fell from 70,000–126,000 to 4,000–20,000 per 2 s (1.5–2.8 s of thread time down to 0.4–1.5 s)
    • with the probe: the rest (16,500–21,800 per 2 s, 1.9–2.5 s) were gone, and a 300 s run had 3,187 frames against 3,108 without it

Regression run

  • None: scripts/smoke.ps1 needs Windows, and we only have Linux. On Windows only the page memory applies.

Not tested

  • Windows (the cost of VirtualQuery there was not measured, so whether the page memory helps there is unknown), AMD, a retail title on this branch.

AI use

  • Claude Code with Claude Opus 5.5: found the cost with a counter in a run, wrote the change, the test change and this description, and ran the tests.

🤖 Generated with Claude Code

`shader::readable` guards XCL's reads of title pointers (vtables in
`is_adopted`, view descs, code bytes). Under Wine each answer is a system call
under Wine's virtual-memory lock, ~20 us and more while other threads commit
or free memory. In a retail title's world: 70,000-126,000 checks per 2 s,
1.5-2.8 s of the title's threads per 2 s.
- A thread remembers the pages (ranges of up to 4) it found readable for
  250 ms (`SEEN`, a ring of 64). shortcut: a page freed within that time
  still reads as readable on that thread; the checks guard against garbage
  pointers, which point at pages never seen readable. Measured: checks asked
  of the system 70,000-126,000 -> 4,000-20,000 per 2 s.
- Under Wine, a range of up to 16 pages is read a byte a page with
  `IsBadReadPtr` instead of asked. Wine's IsBadReadPtr catches access
  violations only, so a vectored handler turns a guard-page fault during the
  probe into an access violation and puts the guard back (the guard page
  ended the process before). `XCL_READABLE_PROBE=0`: asked, as before.
  Measured: the remaining 16,500-21,800 checks per 2 s (1.9-2.5 s) gone.
- `readable_fresh` skips the remembered pages, for memory that may have been
  freed since: the next page of an instruction (`eh::code_bytes_at`).
The GPU-address test checks the freed page on a thread of its own, since
the page it bound is remembered on its thread.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@GreemSC

GreemSC commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Tested on AMD under Wine, on a port of this PR rather than its own branch: amd-dev 7c795f5e + a local Wine stack, with readable_asked adapted to amd-dev's resident_and_readable(p, end) -> bool (amd-dev has no pages_readable). RX 7900 XT, Mesa 26.2.5 RADV, Proton-CachyOS, vkd3d-proton master (0bb64c5), kernel 7.2, retail title, Series X profile, 360 s scripted runs.

  • Probe on (the default under Wine): 2 runs out of 2 lost the GPU about 2 minutes into the world.
    • One run ended at about 120 s, after radv/amdgpu: Failed to clear a PRT VA region (-22) and gfxhub page faults.
    • The other run lost the device at 161 s (0x887a0005, gfxhub page faults, a compute ring reset).
  • XCL_READABLE_PROBE=0 on the same exe: the run reached 360 s with no fault (1 run).
  • We never saw the PRT message in two days of runs without this change.

Not verified, a guess: amd-dev commits full read-write sparse title pages on their first CPU access (97d83b5, kernelx::lazy). A probe read of such a page commits it outside the paths that expect it. If so, nvidia-dev would not be affected.

Locally we now leave the probe off unless XCL_READABLE_PROBE=1, and keep the per-thread page memory. Not tested: your branch itself, NVIDIA, Windows.

Tested and written by Claude Code (Claude Opus 5.5).

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 1484286

No open or merged PR makes the same change (#2437 and #501 avoid readable() on other paths).
Problems in gpu/shader.rs:

  • The per-thread SEEN cache is not limited to Wine. On Windows too, readable now answers true for a page that was freed or protected within 250 ms on the same thread. The list_x test had to move its check to a new thread for this reason.
  • Under Wine, IsBadReadPtr on a kernelx::lazy page faults into lazy.rs's vectored handler, which commits the 64 KiB page. Before, VirtualQuery answered "not readable" without committing it, so a check can now commit pages that lazy.rs keeps uncommitted on purpose.
    Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head 1484286, diff read, not a merge-check; draft, PR description red):

  • A measured regression with the probe on. An AMD tester running a port of this change lost the GPU in 2 of 2 runs with the probe on, and ran 360 s clean with XCL_READABLE_PROBE=0. A likely cause: IsBadReadPtr on a not-yet-committed lazy page triggers kernelx/lazy.rs's handler (~52), which commits it. VirtualQuery used to answer "not readable" without committing, so the probe both commits memory lazy.rs keeps uncommitted on purpose and flips the answer to true.
  • The page memory isn't Wine-only (shader.rs ~229). On Windows too, a page freed or protected within 250 ms (SEEN_FOR, ~295) still reads as readable on that thread, so XCL can dereference it and crash. The list_x.rs ~8411 test had to move its check to a new thread for that reason. Nothing clears the memory when a page is freed, and the Windows gain wasn't measured.
  • A weakened assertion. The Wine-only change to the assertion at shader.rs ~710 weakens a test, which AGENTS.md rules out.
  • The suite ran only under Wine, on nvidia-dev, though the PR targets main; there's no Windows run and no smoke pair.
  • Probing a thread's stack guard page under Wine (the handler at ~285 restores it with a 4 KiB VirtualProtect) is unverified.

Fine: the probe itself is gated on under_wine() and the variable (~258), it merges cleanly, and code_bytes_at skips the memory. #2437 and #501 avoid readable() on other paths.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

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.

3 participants