Repository navigation
D3D12.X: drop global FENCES lock across GetCompletedValue and scope CPU-signal refreshes to the signalled fence - #2649
Conversation
…PU-signal refreshes to the signalled fence
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 xcl-dev#1833
Part of xcl-dev#1773
Assisted-by: Google Antigravity (Gemini 4 Neon)
luckyteagirl
left a comment
There was a problem hiding this comment.
Could this snapshot keep test-owned fences live until GetCompletedValue returns? queue_slot_18_before_the_layout_is_known_writes_8_bytes registers a stack-backed fence and removes it in Cleanup::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_fence only waits for cell refreshes, and this fixture never creates a cell. Main's diagnostic kept the same table lock as forget_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 head 65304399; no crash reproduced. AI-assisted with Codex (GPT-6.2 Sol).
|
Tested: current PR head merged onto current
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. |
|
Ran the fence refresh tests at 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 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. |
|
Wine/NVIDIA verification of exact head
Log kept locally as One scoping note from reading the code alongside the GTA VI Game Init stall I reported at #850 (comment): the title-crash symptom ( AI use: omp, harness model |
Fence refresh and diagnostics held the global fence table lock across driver GetCompletedValue calls, while CPU signals scanned unrelated fences. Snapshot stable cell mirrors, serialize refreshes per fence, and keep raw diagnostic fixtures alive through teardown without blocking ordinary signal/wait bookkeeping. Adapted from xcl-dev/xcl PR xcl-dev#2649 commit 6530439 by Decodere, with the non-cell diagnostic lifetime race fixed. Cross-target cargo check for library and tests passed; Windows execution was unavailable. Assisted-by: Codex (GPT-5.6-sol)
What 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.