D3D12.X: drop global FENCES lock across GetCompletedValue and scope CPU-signal refreshes to the signalled fence #2649
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!2649
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2649"
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?
@Decodere on GitHub · 2026-11-26 05:04 UTC · original
Decodere:fix-fence-x-lockfree-refresh-and-scoped-signal→main· +264 −63 · 1 files · 1 commits · openWhat is in it
Part of #1833
Part of #1773
crates/xcl/src/hle/d3d12x/fence_x.rspreviously held the globalFENCES: Mutex<Option<HashMap<usize, Fence>>>lock across D3D12 COMID3D12Fence::GetCompletedValue()calls inrefresh()andpending_summary(), serializing concurrentnote_queue_signal,note_queue_wait,note_cpu_signal, andcompleted_value_addresscalls behind driver calls. In addition,note_cpu_signal()lockedFENCEStwice and invoked full-tablerefresh()(scanning every tracked fence including non-cell fences), andcompleted_value_address()refreshed all fences instead of only the newly published fence.Scoped solely to
crates/xcl/src/hle/d3d12x/fence_x.rs(+264/-63lines;the_cell_follows_the_fenceuntouched, and does not duplicate#906rewind polling,#1297intermediate pre-cell arming, or#1411Wine WARP fallback):#[repr(C, align(64))] struct Cell(AtomicU64)and wrapped each cell instruct CellMirror { cell: Box<Cell>, refresh_lock: Mutex<bool> }(true= live,false= retired). KeepingBox<Cell>on its own heap cache line preserves permanent*const u64addresses and preventsrefresh_locktraffic from false-sharing titlemonitorx/mwaitxwakes.static FENCESwithstruct FenceTable { inner: Mutex<State> }(State { map: HashMap<usize, Fence>, cells: Vec<(usize, Arc<CellMirror>)> }, singletontable() -> &'static FenceTable) so unit tests can exercise isolated tables without polluting global state.FenceTable::refresh_one(fence, mirror)acquires onlymirror.refresh_lock, returns immediately if!*live, callsborrowed(fence, |f| f.GetCompletedValue()), and storesOrdering::Releaseintomirror.cell.0— serializing concurrent updates per fence without holding the global table lock.FenceTable::refresh()clonesstate.cells, dropsself.inner, and refreshes only cell-backed fences lock-free (skipping all non-cell fences).FenceTable::note_cpu_signal(fence)locksself.inneronce, clonesf.cellfor onlyfence, dropsself.inner, and callsrefresh_oneon that single fence.FenceTable::completed_value_address_with(this, arm_fn)returns immediately if a cell already exists; otherwise publishesArc<CellMirror>tomapandcells, dropsself.inner, invokesarm_fnwhenpending > 0, refreshes onlythis, and returnsmirror.cell.0.as_ptr().FenceTable::pending_summary()snapshots(p, signalled, waited, has_cell)underself.inner, drops the lock, then queriesGetCompletedValue()lock-free and formats the exact same diagnostic string.FenceTable::forget_fence(fence)(wired toforget_fence_for_test) removes the entry frommapandcells, dropsself.inner, and sets*mirror.refresh_lock.lock() = falseso any concurrentrefresh()snapshot either completes before return or bails out on!*live, preventing UAF.MockFenceCOM vtables (AtomicU64/AtomicUsize+offset_of!(ID3D12Fence_Vtbl, GetCompletedValue)):refresh_and_pending_summary_do_not_block_concurrent_queue_and_cpu_signalsnote_cpu_signal_and_refresh_avoid_unrelated_and_non_cell_fencesper_cell_lock_prevents_stale_refresh_overwriteTested
rustc 1.100.0, DXC1.9.2609.5 (01b62ad4), release builds.b49be183: a temporary pure-CPU mock COM probe against unmodifiedfence_x.rsfailed (unrelated_calls=1,global_lock_held=true), confirmingnote_cpu_signalqueried unrelated fences andrefresh()held globalFENCESacrossGetCompletedValue().XCL_ADAPTER=warp,XCL_WARP=1):cargo test --release -p xcl --lib -- hle::d3d12x::fence_x::tests:: --skip the_cell_follows_the_fence --nocapture->3 passed; 0 failed; 0 ignored(0skipped,0.02s).cargo test --release -p xcl --lib -- hle::d3d12x::fence_x::tests:: --nocapture->4 passed; 0 failed; 0 ignored(0skipped,0.29s, including untouched real-D3D12the_cell_follows_the_fence).cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture:37binaries,1438passed,0failed,47ignored (0warnings infence_x.rs),29pre-existing environment-gatedSKIPPEDlines (none infence_x):21wave64-only GPU tests (no wave64on RTX 4060 32-lane waves)3per-lane wave32 shader tests (refused)3D3D12 debug layer tests (no d3d12SDKLayers.dll/ debug layer unavailable)1heap-window OOM fallback test (no heap windows)1PSO driver-cache measurement test (cannot disable the driver cache)Regression run
Paired 60 s retail smoke (
scripts/smoke.ps1, RTX 4060): both base and HEAD sustained60 60 60presents per 2 s with0device removals and0crashes (1319vs1549total frames,13957 MBvs13968 MBpeak commit).Base (
origin/main):HEAD (
fix-fence-x-lockfree-refresh-and-scoped-signal):Not tested
the_cell_follows_the_fenceon a hardware-free WARP-only host (that existing test callsD3D12CreateDevice(None, ...)first before falling back to WARP, so on this RTX 4060 host it ran on RTX 4060; the three new mock COM concurrency/scoping tests are pure CPU with zero D3D12 device dependency).AI use
Google Antigravity (Gemini 4 Neon) authored the
FenceTable/CellMirrorrefactoring, mock COM concurrency/scoping tests, and verification runs; verified via failing-first probe, pure-CPU and RTX 4060fence_xtests, full RTX 4060 release workspace tests, and paired 60 s retail smoke runs.Encapsulate fence_x tracking in `FenceTable` (`State { map, cells }`) backed by `Arc<CellMirror>` (`Box<Cell>` on its own 64-byte cache line for `monitorx`/`mwaitx` plus a per-fence `refresh_lock: Mutex<bool>` live guard). `refresh()`, `note_cpu_signal()`, `pending_summary()`, and `completed_value_address()` now drop the global mutex before any `ID3D12Fence::GetCompletedValue()` COM call; `note_cpu_signal()` and `completed_value_address()` refresh only the target fence rather than scanning all tracked fences; `refresh()` visits only cell-backed fences; and `refresh_lock` serializes concurrent per-fence updates and guards test fence teardown against UAF. Part of #1833 Part of #1773 Assisted-by: Google Antigravity (Gemini 4 Neon)@SlopTart on GitHub · 2026-11-26 05:29 UTC · original
Tested: current PR head merged onto current
origin/main(clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo targetx86_64-pc-windows-gnurun under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29).gpu_suite (cold cache): 165 passed / 3 failed, no crashes. The three are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, promoted-UAV-clear driver gap, PR #2490); none caused by this PR. This PR does not change the mad/mac red on RADV.Targeted run of
note_cpu_signal_and_refresh_avoid_unrelated_and_non_cell_fences,per_cell_lock_prevents_stale_refresh_overwrite,refresh_and_pending_summary_do_not_block_concurrent_queue_and_cpu_signals: 3 passed. Lock-scoping behavior is timing-sensitive under Wine too, so the concurrency tests here do exercise the interesting part on this lane.Not tested: retail titles (no title data on this lane, counts only).
AI use: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.
@luckyteagirl on GitHub · 2026-11-26 05:07 UTC · original · reviewed
Could this snapshot keep test-owned fences live until
GetCompletedValuereturns?queue_slot_18_before_the_layout_is_known_writes_8_bytesregisters a stack-backed fence and removes it inCleanup::drop. A concurrent wait-watchdog summary can copy that raw address, release the table lock, then query it after cleanup and the test return.forget_fenceonly waits for cell refreshes, and this fixture never creates a cell. Main's diagnostic kept the same table lock asforget_fence_for_test, which serialized the query and cleanup. A test-build lifetime guard covering all entries, or a test-only locked diagnostic traversal, would preserve that ordering. Source review only at head65304399; no crash reproduced. AI-assisted with Codex (GPT-6.2 Sol).@Aniseto on GitHub · 2026-11-26 10:53 UTC · original
Ran the fence refresh tests at
65304399:Environment: Windows 11 Pro build 26200 x64, NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only, 16 GB) driver 32.0.16.2124, Intel Core i5-12400F, 64 GB RAM, rustc 1.100.0 (0ef1bc386 2026-11-09) MSVC 14.44.35207, DXC 1.9.2609.5, debug build.
All four pass here, including the three new mock-COM concurrency/scoping tests and the existing real-D3D12
the_cell_follows_the_fencetest on the RTX 5070 Ti.What this adds: the author measured on an RTX 4060 (Ada) and WARP; this is a second NVIDIA host (Blackwell, different driver) running the same real-D3D12 fence test. The thread has no report from another machine.
Not tested: the full workspace suite, a paired 60 s smoke run, AMD and Intel GPUs, native wave64, Wine, release profile, title data.
AI-assisted (OpenCode), model: DeepSeek V4.1 Flash Free - checked out the PR's head commit in a detached worktree, built it, ran the tests, wrote this report; the worktree was not modified; no game data was used.
@pickles976 on GitHub · 2026-11-26 11:25 UTC · original
Wine/NVIDIA verification of exact head
6530439984ad79923fcb7698af76aa0c2d0b9fb2(Linux 6.17, Wine 11.0, RTX 4060 Laptop, NVIDIA 590.48.1, DXVK 3.1.1, Rust 1.98.1 x86_64-pc-windows-msvc). Caveat: locally built diagnostic-relaxed vkd3d-proton 3.2.02576c0b4adfae4a+(assertions disabled, allocator/listfrom_ifacevtable identity checks removed, null checks kept) — not stock-runtime certification. Unchanged owner code; freshCompiling xcl .../xcl-pr2649/...(26.58 s); one test thread; 9 GiB/512 MiB cgroup scope.hle::d3d12x::fence_x::tests4/4 pass, 0.71 s:the_cell_follows_the_fenceon the real adapter: queueSignal(6)/Signal(7)observed throughcompleted_value_addresscells, CPUfence.Signal(9)throughnote_cpu_signal— including the queued-before-address-asked ordering.refresh_and_pending_summary_do_not_block_concurrent_queue_and_cpu_signals,note_cpu_signal_and_refresh_avoid_unrelated_and_non_cell_fences,per_cell_lock_prevents_stale_refresh_overwrite— all pass under--test-threads=1with their internal concurrency harnesses (no deadlocks, no stale overwrite).Log kept locally as
verify-pr2649-fences.log.One scoping note from reading the code alongside the GTA VI Game Init stall I reported at #850: the title-crash symptom (
1 CPU-write submissions using legacy fallback after 10000 ms (fence at 0, wanted 1; 2 queued writes)) comes fromhle/d3d12x/cpu_writes.rs, which has its ownFENCES: Mutex<Option<HashMap<usize, QueueFence>>>distinct from this PR'sfence_xmap, and its worker already waits off-lock (fence::waitin thexcl-cpu-writesthread; the lock is taken only for map insert/remove/lookup). So this PR is a latency/correctness improvement for thefence_xcell path, but it does not plausibly change that crash. No title run, no perf claim.AI use: omp, harness model
openai-codex/gpt-6-astra, ran the checks and drafted this report. No upstream code changes or assets attached.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.