Repository navigation
D3D12.X: a native host's last Release only queues the resource - #2445
Ahmet-Dedeler wants to merge 3 commits into
Conversation
D3DMetal calls Release on adopted resources from a native thread of its own at submit time. That thread has no Wine TEB, so the first-time std::env::var read inside the OnceLock in late_release::enabled() enters Wine's ntdll and never returns. Reading the setting once in adopt(), on the Wine thread that creates the object, makes sure the OnceLock is already set before the host's thread can reach it. Part of xcl-dev#258. Assisted-by: Claude Code (Claude Opus 4.6 (1M context))
The steps that drop what XCL keeps by a released resource's address (the maps, sparse pins, title textures, tiled textures, its unified-memory block) move from res_release into real::forget_released, unchanged and in the same order. The next commit calls it from the late-release thread as well. No change in behaviour. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
|
|
Verification on RTX 3060 Ti (Windows 11), branch merged with current
Thanks! |
bmjask
left a comment
There was a problem hiding this comment.
P1: Ring::push has a concurrent reservation race. late_release.rs:172-179 checks head-tail and then separately fetch_adds tail; RING_WRITERS=256 reserves space only under a bounded simultaneous-writer assumption, but no such bound is enforced. For example with N=512 and used=255, 258 writers can all observe occupancy below N-256 before incrementing; the last claim reaches tail+N and writes slots[tail % N] before the reader consumes it, overwriting a pending resource. That loses its pointer/reference and leaves its XCL address state unforgotten. The new 4-writer stress test cannot cover this. Please use CAS reservation/a bounded MPMC queue or enforce the writer bound, with a barrier test exceeding the reserve.
|
Rerun on the new head
So the new test can catch the race, but on an 8-core Ryzen it almost never does: the 300 writers rarely reach the check in the same window. A test that holds writers between the check and the claim (or loops the barrier round many times in one run) would fail reliably. The compare-exchange itself looks right to me by reading. First run, on Tested:
Not tested: a title on Windows with this branch, a release build, AMD / Intel, Wine / D3DMetal (nothing here). AI use: Claude Code (Claude Opus 5.5) ran the tests and the debug-layer comparison, and wrote this report. |
Apple's D3DMetal (CrossOver, the Game Porting Toolkit) calls Release on threads of its own (its queue thread at submit, for a barrier's resource) and from inside calls XCL makes while XCL holds a lock of its own. When that Release is the title's last one and late release keeps the object, res_release took the maps' locks, the sparse state's and the texture table's, and late_release::hold signalled the queues and waited. Whatever the host's thread waits for there, its queue waits for too. (The one such wait seen in a retail run on the Mac port, sparse's forget_buffer, no longer waits since xcl-dev#1981; the others are the same shape and are not waited for here either.) On a host whose D3D12 is in no PE module (class_for: module_of of the table's first method is 0, noted once when the first such table is adopted) a kept last Release now writes the resource into a ring of atomics (late_release::hand_over) and returns: no lock, no fence, no allocation, no thread start, no environment read (xcl-dev#1789 reads XCL_LATE_RELEASE at adoption). The next drain_known, on the late-release thread (started at adoption on such a host) or a submit, forgets what XCL keeps by the address (real::forget_released, the old body of res_release) and holds the object like any other; the address cannot come again before that, the object is still there. With the ring full (16,384 slots) the reference is kept for good and counted. D3D12 on Windows and vkd3d-proton are PE modules: there the flag is never set, the ring stays empty (drain_known reads two atomics) and a Release does what it did. Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6979e9f to
6df8f64
Compare
|
@bmjask you're right, thanks. The check and the Fixed in New test |
|
Re-verification on RTX 3060 Ti (Windows 11), new head merged with current
Thanks! — Devin |
luckyteagirl
left a comment
There was a problem hiding this comment.
late_release.rs:171–176: the capacity check can treat an empty ring as full because it samples head before tail. Start with head = tail = 0: producer A reads i = 0 and pauses; B claims slot 0; the reader drains it and publishes tail = 1 (take, line 204). A then reads that tail, so 0usize.wrapping_sub(1) == usize::MAX and returns false even though head = tail = 1. hand_over_to counts the rejected resource as kept for good (lines 224–229), so an ordinary two-producer/one-reader interleaving leaks a resource and retains its address bookkeeping without the ring ever being full. The newer CAS reservation fixes the writer-overflow race, but not this stale-head/fresh-tail snapshot.
Could the check read tail before the head sample, or refresh head before treating an apparent wrap as full, with a deterministic barrier test after A's head load? This is source-derived at exact head 6df8f647253b3e2cb071543afce1248be3a4f08f; a CPU arithmetic probe confirmed the usize::MAX result, but I have not run the XCL ring test or a native-host title. The behavior is inherited by stacked #2498. AI-assisted review with Codex.
Stacked on #1789; only the top two commits belong here (
D3D12.X: a resource's last Release forgets its address in one functionandD3D12.X: a native host's last Release only queues the resource). The bottom one is #1789's commit, @vakaobr's, rebased onto currentmain(#1789's base predates a change tores_releasethat the second commit builds on). Part of #258.Apple's D3DMetal calls
Releaseon threads of its own (its queue thread at submit) and from inside calls XCL makes while XCL holds a lock of its own. When that is the title's lastReleaseand late release keeps the object,res_releasetook the maps' locks, the sparse state's and the texture table's, andlate_release::holdsignalled the queues and waited. Whatever the host's thread waits for there, its queue waits for too. On a native host, such aReleasenow only writes the resource into a ring of atomics and returns. The late-release thread forgets what XCL keeps by the address and then lets the object go. This is the ring frommac7'sfd1e593, which @vakaobr asked to see opened onmainin #258 so his host-thread part can go on top. That part (no hostReleaseat all on a host thread) is not in this PR.The gate needs no other open PR.
class_foralready callsmodule_ofon the table's first method. When that is 0 (the host's D3D12 is in no PE module), it setsNATIVE_HOSTonce and starts the late-release thread there, on the adopting thread. D3D12 on Windows and vkd3d-proton are PE modules. There the flag is never set, the ring stays empty (drain_knownreads two atomics) and aReleasedoes what it did.What is in it
crates/xcl/src/hle/d3d12x/real.rs: the body ofres_releasethat forgets a released resource moves intoforget_released, unchanged (first commit, no behaviour change).crates/xcl/src/hle/d3d12x/real.rs:NATIVE_HOST, set inclass_forfrommodule_of(...) == 0. With it set, a kept lastReleasecallslate_release::hand_overand returns 0. Testsonly_a_table_in_no_pe_module_makes_a_native_hostanda_kept_last_release_on_a_native_host_waits_for_no_lock.crates/xcl/src/hle/d3d12x/late_release.rs:Ring(16,384 slots; the indices only grow; a writer claims an index with one compare-exchange, only while it is less than N past the tail, so a claimed slot is always one the reader has emptied; one reader at a time, which empties a slot before it moves the tail).hand_over(atomics only; with the ring full the reference is kept for good and counted, never the locking path).drain_knowntakes the ring first: it forgets each resource, then holds it like any other.start_threadis split out ofhold.Tested
x86_64-pc-windows-gnu(mingw-w64 GCC 16.2.0), debug, DXC 1.9.2609.CIworkflow (Windows runner, no GPU) on this head6df8f64, run on my fork because it waits for approval here: Build and test passed.cargo build --workspace --target x86_64-pc-windows-gnuat each of the two commits: builds, no new warnings (the same 3 asmain).<xcl lib test exe> --exact <test> --test-threads=1): passed:late_releasetests: the 4 new ring tests (on6df8f64), plusa_held_resource_goes_when_the_queue_is_past_its_batchandwithout_queues_a_batch_goes_at_once;real::native_host_tests;title_textures::tests::gpu_mirror_upload_follows_cpu_writes_and_later_mapping_changes;sparse::tests::a_buffer_released_while_the_state_is_held_does_not_wait_for_it.a_kept_last_release_on_a_native_host_waits_for_no_lockwith thehand_overbranch taken out ofres_releasefails: "the last Release waited for a lock another thread held: Timeout".d3d12.dll, so the gate test checked the PE-host side. The native path ran through the test's own switch (native_host_tests::FORCED, a thread-local read only after the flag).Regression run
From this Mac,
scripts/smoke.ps1unchanged under PowerShell, with a wrapper as-Xclthat startsxcl.exeunder Wine, both from the same copy of the caches. On this hostmainshows no frame (the title stops about 3 s in, before it creates its device), so neither run reaches a title'sRelease: this pair shows that the build starts and ends asmaindoes.main:
PR:
(
peak commitis 0 because Wine reports no commit charge.)Not tested
main, and the gate on a real D3DMetal table in a test. (In this bottle XCL's own device goes throughd3d12.dll, a PE module, so the gate test saw that side. In earlier retail runs on this Mac the tables of resources, queues, lists and fences were at addresses in no module,PC vtable 0x21a6…in the log, which is what the gate keys on.) Without D3D12.X: preserve native host vtable prefixes and tails #1904,maindoes not get far on D3DMetal. Onmac7, the loading runs I have predate this change.AI use