D3D12.X: add opt-in watch_cold core module for read-only cold blocks #3639
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3639"
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 ivall/xcl. Originally posted by iggyvilla on 2026-12-02T02:47:00Z.
Stacked on #3597; only the top commit belongs here.
TL;DR — Before vs After
#3639v1)#3639Part 1 core)watch_cold.rs+mod.rs), core module only; call-site wiring split into a small stacked follow-up PRXCL_WATCH_COLD=1, off by default)XCL_WATCH_COLD=1, off by default;#[allow(dead_code)]until wiring PR lands)VirtualProtecterror 87 skipped upfront!uma.shadowed() && uma.in_committed_windowupfront skip (removed obsoleteprot_cache/CHANGESring)FREEZE_ENDchecked beforeCOLDlock inchanged_nowFREEZE_ENDchecked underCOLDlock inchanged_now, closing pin-vs-freeze race;thaw_rangecleanup on failed freezeRX 6650 XT)1654frames (60 61 60,15902 MB) onamd-dev1641frames (60 60 60,16237 MB, 0 removals, 0 crashes)What is in it
Adds the opt-in
hle::d3d12x::watch_coldcore module (XCL_WATCH_COLD=1, default off, requiresXCL_GUEST_WRITE_GUARD=1from #3597) so cold watched title memory can be protectedPAGE_READONLYand excluded from per-submitGetWriteWatchscans.In response to review feedback on PR size and complexity:
prot_cache,CHANGESring buffer, and diagnosticSTATScounters made redundant by the O(1) upfront imported-coherent-UMA skip (!uma.shadowed() && uma.in_committed_window), shrinkingwatch_cold.rsfrom 2,739 lines to 702 lines (619 runtime + 83 tests).title_textures/kernelx/lazy/sparsecall-site wiring (and thetracks_hot_blocks()fix formark_hot/all_hot/ sibling warming) into a separate small stacked follow-up PR.OPENEDVEH ring, unlockedVirtualProtectserialized withguest_write::while_unpinnedandmeets_guarded_view, post-protectGetWriteWatchscan (take_after),guard/guard_free/changedpinning +FREEZE_END/HANDLINGdrain, multi-span 4 MiB fault windows, range-local gap coalescing (XCL_WATCH_COLD_GAP_MB), and per-block hot/cold streak holding.Tested
cargo test -p xcl --lib -- hle::d3d12x::watch_cold::tests --test-threads=1): 4 passed, 0 failed (freeze_fault_window_and_guard_round_tripcovering freeze / 4 MiB multi-span fault / thaw / pin,coalesce_calls_merges_within_gap_and_stays_inside_range,hot_streak_holds_block_after_consecutive_frame_faults,look_with_freezes_clean_pieces_and_skips_hot_blocks).cargo test --workspace --no-fail-fast -- --test-threads=4): 2,966 passed, 16 pre-existing failed, 49 ignored (same pre-existing failures as cleanorigin/amd-dev41aa60ce: 3 RDNA2 f32/NaN tests, 6 parallelsparse/title_texturescontention tests, 5e2edebug-layer/title tests, 2two_pass_driver_cachetests).cargo build --release -p xcl): succeeded cleanly.Regression run
Baseline (
origin/maind8bd9a38):Baseline (
origin/amd-dev41aa60ce):PR head (
a2a329de, default flags):Not tested
XCL_WATCH_COLD=1active (call-site wiring intotitle_textures/kernelx/sparseis split into the stacked follow-up PR).AI use
watch_cold.rsto remove obsolete paths and split out call-site wiring, tightenedchanged_nowsynchronization, ran unit/workspace tests and 60 s smoke on local AMD hardware, and wrote this description.Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T02:57:16Z.
Review (head
3a164a8c, diff read, not a merge-check):3a164a8cchanges 2,890 lines (watch_cold.rsalone 2,739), none generated or test data; without the unit tests about 1,700 lines are runtime code, and the description does not say why it cannot be split. Please split it along CONTRIBUTING.md, each part under ~400 lines and stacked on #3597 (with aStacked on #3597line at the top): (1) preparation with no behaviour change: theguest_writeadditions (FREEZE_LOCK,while_unpinned,meets_guarded_view, the wait inopen_with) with their test, and therefresh_hot/hot_setsplit intitle_textures.rs; (2) thewatch_coldcore (claim / freeze / fault / thaw,look_with) with its tests, not yet called; (3) turning it on: theguard/guard_free/changedhooks inkernelx.rs,kernelx/lazy.rsandsparse.rsand thelook_withdispatch inrefresh_parts; (4) the window and gap tunables; (5) theSTATScounters,line()and thesubmit_cost.rshook.XCL_WATCH_COLD=1turns offmark_hotandall_hot.hot_looks()(title_textures.rs~2081) now returns false whenwatch_cold::on(), somark_hot(~2520) returns early and blocks a texture creation (~891) or a sparse commit (sparse.rs~3773, ~6400) marks hot are no longer taken by the next look, whileall_hot(~3518) returns true, soupload_all(~3570) skips thewritten_pagespass and a texture spanning taken and untaken blocks goes up partly old and partly new. The PR's own sibling-block warming (watch_cold.rs~1494) is a no-op for the same reason; the 4 MiB test only checks the same look.Not reviewed line by line because of the size. Fine: opt-in and checked once; with
XCL_WATCH_COLDunset every hook returns before taking a lock; the freeze is serialized against guest writes throughwhile_unpinnedwith a write-watch take after the protection.AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by iggyvilla on 2026-12-02T07:09:44Z.
Thanks for the thorough review! Updated #3639 to address PR size and structure:
prot_cache,CHANGESring, and diagnosticSTATScounters (made redundant by the upfront O(1)!uma.shadowed() && uma.in_committed_windowskip), shrinkingwatch_cold.rsfrom 2,739 lines to 702 lines (704 lines total across 2 files, down from 2,890 lines across 8 files).changed_now(now checksFREEZE_ENDunderCOLDlock before thawing) and switched failed-freeze cleanup tothaw_range.FREEZE_LOCK/while_unpinned/meets_guarded_viewinto #3597 and split thetitle_textures/kernelx/lazy/sparsecall-site wiring — including thetracks_hot_blocks()fix formark_hot/all_hot/ sibling-block warming you flagged — into a small stacked follow-up PR (opening next).Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T07:17:46Z.
Follow-up review (head
a2a329de, since3a164a8c, diff read, not a merge-check):a2a329deis 704 lines in 2 files (watch_cold.rs, 619 runtime) with aStacked on #3597line, still above ~400, and the window, gap and hot-hold tunables (part 4 of the suggested split) are in it.XCL_WATCH_COLD=1turns offmark_hotandall_hot: not in this part (title_textures.rsis not touched); the description puts the fix in the wiring PR that follows.New:
fault()records(lo, hi)inOPENED(~331) andapply_faults()later callscarveon it (~250), which removes everyCOLDspan in that range without thawing it (~185-199);freezechecks overlap only againstCOLD(~487), not windows still pending inOPENED. With the default 2 MiB windowmultimakes(lo, hi)the whole block (~288-289), so a fault taken during a look, afterapply_faults(~566) and beforefreeze(~615) freezes a sibling piece of the same block, gets that sibling dropped at the nextapply_faults; the same happens to later parts of a multi-part freeze when a fault lands between twoVirtualProtectcalls (~497-503). Those pages stayPAGE_READONLYwith noCOLDentry, so the next write makesfault()return false (~276-282): an unhandled access violation, andfreezenever picks them up again (~473).look_withpicks the firstFREEZE_PER_LOOKpieces withclean >= FREEZE_AFTERbefore trying them (~608) and resetscleanonly on success (~616), so a piecefreezealways refuses (imported coherent UMA ~457, a guarded view, a part not read-write ~473) is picked again every look; 64 of them early inrangesmean nothing after them is ever frozen.rangeschanges,look_withunregisters every old range and then registers the new ones one by one (~571-578); anopen_with/writablecheck in between finds a frozen page outside any registered range, does not pin or touch it, and aReadFileinto it fails withERROR_NOACCESSwhilekernel_writeskips its retry.freezeis the first caller ofwhile_unpinned. That makes #3597'sopen_withlost wakeup (guest_write.rs~330-336, see the follow-up there) reachable once this is wired.fault()is called directly, ~647, sohandler~344-352 never runs);guard/guard_free/changedreturn early becauseon()is false; the over-8-parts window, the full ring and every concurrent case are untested.Fine: nothing calls the module yet, so the default path is unchanged;
changed_nowchecksFREEZE_ENDunderCOLD; a failed protection is rolled back withthaw_range; the handler's common path does not allocate and its lock order does not cycle withfreeze,look_withoropen_with;take_afterruns after the protection.AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T13:23:18Z.
merge-check
base:
41aa60ce84head:
a2a329de2bmerge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2972 passed, 9 failed, 49 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on amd-dev); failing: gpu::recomp::lower_dxil::tests::f32_programs_on_the_gpu, gpu::recomp::tests::gpu_fract_f32_edges, hle::d3d12x::sparse::tests::private_physical_pages_reach_the_gpu_on_mapping, hle::d3d12x::title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels, hle::d3d12x::title_textures::tests::remapped_pages_are_compared_with_what_they_held, hle::d3d12x::title_textures::tests::stream_recheck_keeps_the_whole_upload_baseline_over_partial_uploads, hle::d3d12x::title_textures::tests::stream_recheck_partial_upload_without_an_entry_queues_a_whole_upload, hle::d3d12x::title_textures::tests::stream_recheck_schedule_catches_late_writes_and_resets_milestones (the same tests fail on amd-dev
41aa60cealone on this card, so none is from this PR)review: read by one reviewer at this same head; the review is at https://github.com/xcl-dev/xcl/pull/3639#issuecomment-7338009919 and its findings stand. Most serious:
apply_faultsrunscarveover a fault's whole 2 MiB window (~250) without thawing it, so a sibling piece frozen after that fault is dropped while still read-only and the next write to it is an unhandled access violation.performance: 60 s smoke of a retail title, amd-dev
41aa60cevs this merge (a2a329de): 1671 -> 1680 frames shown (100 %; last presents per 2 s: 60 60 60), peak commit 16722 -> 16659 MB, 0 device removals, 0 crashes.verdict: not yet:
apply_faultsrunscarveover a fault's whole 2 MiB window (~250) without thawing it, so a sibling piece frozen after that fault is dropped while still read-only and the next write to it is an unhandled access violation (see the review)hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5
AI-written and posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T17:36:28Z.
Code review location:
crates/xcl/src/hle/d3d12x/watch_cold.rs; line 250Critical:
apply_faultscarves queued fault windows out of COLD without thawing them, so any spanfreezeadded inside that window after the fault was queued is dropped while its pages are still PAGE_READONLY. The default window is 2 MiB = BLOCK, somultiis true and the window is the whole block, not clipped to the span. If a title thread faults on cold span S duringlook_with'stakecalls,faultthaws S and only queues window W in the OPENED ring. Then the samelook_withcallsfreeze(P)for a piece P in W (e.g. another watched range in the block): P doesn't overlap S, so it's protected read-only and inserted into COLD. The nextapply_faultscarves W and removes P's entry without thawing, so on the next title write to Pfaultfinds nothing in COLD, VirtualQuery shows PAGE_READONLY, and the handler returns EXCEPTION_CONTINUE_SEARCH: an unhandled access violation. Thawing any COLD span inside the window before carving (or refusing to freeze inside a queued window) would fix it.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T17:36:28Z.
Code review location:
crates/xcl/src/hle/d3d12x/watch_cold.rs; line 326Critical: A fault during
freeze's VirtualProtect loop over a multi-part span thaws only the parts already protected; the loop then protects the rest, and the queued window later removes the whole span from COLD.freezeinserts span [a, end) with parts p0..p2 (e.g. mixed write-combine and plain read-write regions) and protects p0. If another thread writes to p0 and faults, FREEZE_END overlaps, somultiis false and the window is clipped to the span.thawrestores p0 and skips p1/p2, which are still read-write. The freeze thread then protects p1/p2 and returns true, and the nextapply_faultscarves the span out of COLD, leaving p1/p2 read-only and untracked, so the next write there is an unhandled access violation. Rechecking for a fault on the span after the loop, and thawing everything if one happened, would close it.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T17:36:28Z.
Code review location:
crates/xcl/src/hle/d3d12x/watch_cold.rs; line 572High: When the watched ranges change,
look_withunregisters the old ranges from guest_write but doesn't thaw cold spans inside ranges that were dropped. If a range R with frozen spans is dropped while its memory stays alive,guest_write::unregister(R)runs but COLD still holds R's spans as PAGE_READONLY, andhot_piecesno longer looks at R, so they stay frozen for good. A later ReadFile/NtReadFile into R is no longer pinned or touched byguest_write::open, so the kernel fails it with ERROR_NOACCESS / STATUS_ACCESS_VIOLATION and the data silently isn't loaded. There's also a short window between unregistering the old ranges and registering the new ones where kernel writes into still-cold ranges fail. Thawing dropped ranges' spans before unregistering would fix it.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T17:36:28Z.
Code review location:
crates/xcl/src/hle/d3d12x/watch_cold.rs; line 438High: For
guard_free(a, 0)the length comes from VirtualQuery's RegionSize ata, which covers only the first region with uniform attributes, and freezing itself splits allocations into read-only and read-write regions. If the title calls VirtualFree(base, 0, MEM_RELEASE) on a 64 MiB allocation whose second block is frozen, RegionSize at base stops at the first protection change (e.g. 2 MiB), sochanged_nowandthaw_rangeclear only that part, and the cold spans after it stay in COLD after release. If the address is reused for a new watched allocation,hot_pieceskeeps leaving those stale spans out of the GetWriteWatch scan, so CPU writes there never reach the GPU, andfaultcan make a page the title marked PAGE_READONLY on purpose writable. Walking regions with the same AllocationBase to get the whole allocation would fix it.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T17:36:28Z.
Code review location:
crates/xcl/src/hle/d3d12x/watch_cold.rs; line 456Medium:
look_withandfreezenever checkon()orguest_write::enabled(), whileguard,guard_freeandchangeddo nothing when the switch is off. If the follow-up wiring callslook_withwithout theon()gate (it'spub), or XCL_WATCH_COLD=1 is set without XCL_GUEST_WRITE_GUARD=1, pages are still frozen read-only. Then title decommits and protects skipchanged_nowbecauseon()is false, and guest_write'sopendoesn't touch pages, so kernel reads into frozen memory fail with ERROR_NOACCESS and frozen state outlives decommit. Returning early fromlook_with/freezeunless both switches are on would make the module safe to wire up.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T17:36:28Z.
Review: COMMENTED
Review of
a2a329de: 5 finding(s) (2 critical, 2 high, 1 medium), most severe first.crates/xcl/src/hle/d3d12x/watch_cold.rs:250:apply_faultscarves queued fault windows out of COLD without thawing them, so any spanfreezeadded inside that window after the fault was queued is dropped while its pages are still PAGE_READONLY. (inline)crates/xcl/src/hle/d3d12x/watch_cold.rs:326: A fault duringfreeze's VirtualProtect loop over a multi-part span thaws only the parts already protected; the loop then protects the rest, and the queued window later removes the whole span from COLD. (inline)crates/xcl/src/hle/d3d12x/watch_cold.rs:572: When the watched ranges change,look_withunregisters the old ranges from guest_write but doesn't thaw cold spans inside ranges that were dropped. (inline)crates/xcl/src/hle/d3d12x/watch_cold.rs:438: Forguard_free(a, 0)the length comes from VirtualQuery's RegionSize ata, which covers only the first region with uniform attributes, and freezing itself splits allocations into read-only and read-write regions. (inline)crates/xcl/src/hle/d3d12x/watch_cold.rs:456:look_withandfreezenever checkon()orguest_write::enabled(), whileguard,guard_freeandchangeddo nothing when the switch is off. (inline)These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T17:36:30Z.
@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.