Skip to content

D3D12.X: defer a resource release arriving on a thread XCL did not create - #2474

Closed
vakaobr wants to merge 3 commits into
xcl-dev:mainfrom
vakaobr:d3d12x/defer-foreign-thread-release
Closed

vakaobr wants to merge 3 commits into
xcl-dev:mainfrom
vakaobr:d3d12x/defer-foreign-thread-release

Conversation

@vakaobr

@vakaobr vakaobr commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Stacked on #2361 and #1789: the first commit here is #1789's; only the top commit is new.

On a Mac, D3DMetal calls Release on adopted resources from a native thread of its own, at submit time. That thread has no Wine TEB. res_release takes XCL's mutexes and, for a last reference, the late-release hold: uncontended, a std Mutex is a few atomics and gets through, but a contended one waits in Wine's ntdll, which never returns there. This keeps all of that off the host's thread: on a foreign thread the Release goes into a lock-free ring, and a Wine thread (the late-release worker, or the next res_release on a Wine thread) runs it through res_release. Detection is on a macOS host only (util::mac_host, #2361): a thread whose stack pointer is at or above 0x7000_0000_0000 is a native thread (measured on this Mac: Wine threads 0x0180ea9f..0x0220fb37, D3DMetal's 0x7ff2063df667..0x7ff2066c3677). Windows and Wine on Linux are unchanged. Part of #258.

What is in it

  • (D3D12.X: read the late-release setting at adoption, not at first release #1789) crates/xcl/src/hle/d3d12x/late_release.rs, real.rs: the late-release setting is read when the device is adopted, not at the first release.
  • crates/xcl/src/hle/d3d12x/late_release.rs: the ring (one consumer at a time, 16,384 slots; a full ring keeps the reference and the worker logs how many), the foreign-thread check, the drain; tests for the ring and the detection.
  • crates/xcl/src/hle/d3d12x/real.rs: res_release split into res_release, foreign_release and release_cleanup; a foreign Release is queued.

Tested

Regression run

  • None: I have no title of my own to run scripts/smoke.ps1 on. The new path only runs on a macOS host.

Not tested

  • Windows, Wine on Linux, the whole suite, any title.

AI use

util::mac_host(): the process runs under Wine on a Mac (CrossOver or the
Game Porting Toolkit), where D3D12 is Apple's D3DMetal and the title's
code runs through Rosetta 2. ntdll's wine_get_host_version names the
host's system; "Darwin" is a Mac. XCL_MAC_HOST=1 / 0 overrides it.

Nothing calls it yet. The Mac changes that need to know they run there
(the barrier default, title textures on shared pages) build on it; until
now only Ahmet-Dedeler's mac7 branch had it. Part of xcl-dev#258.
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.
…eate

D3DMetal calls Release on adopted resources from a native thread of its
own at submit time (4 per drain in the two tests below).  That thread has
no Wine TEB.  res_release takes XCL's mutexes and, for a last reference,
the late-release hold; uncontended, a std Mutex is a few atomics and gets
through, but a contended one waits in Wine's ntdll, which never returns
there.  This commit keeps all of that off the host's thread.

On a foreign thread the caller's Release does not run at all: the pointer
goes into a lock-free ring, and a Wine thread (the late-release worker, or
the next res_release on a Wine thread) runs it through res_release.
Checking for the last reference there instead (AddRef returning 2) is not
safe: a Release on another thread between that check and ours can take
the count to 0 on the host's thread.  The returned count is read with an
AddRef and its Release, which cannot reach 0 while the caller's reference
is still held.

The ring has one consumer at a time (a flag; a drain that finds it set
returns at once), and the consumer empties a slot before moving the tail
past it, so a producer reusing the slot never has its entry erased.  It
has 16,384 slots: every foreign Release goes through it, and a full ring
keeps the reference (the object leaks; the worker logs how many).

Detection: only on a macOS host (util::mac_host, read once at adoption),
so the path is off on Windows and on Wine on Linux.  There, a thread whose
stack pointer is at or above 0x7000_0000_0000 is a native thread.
Measured on an M5 Pro (CrossOver 26.3, Rosetta 2): Wine threads
0x0180ea9f..0x0220fb37, D3DMetal threads 0x7ff2063df667..0x7ff2066c3677.
The TEB pointer (gs:[0x30]) is not read: that read hangs on a D3DMetal
thread (measured).

Tested on main 85e3826 + xcl-dev#553 + ddf3be7: console_wave64_compute_through_
the_pso_hook_dispatch_and_read_back and gpu_mirror_upload_follows_cpu_
writes_and_later_mapping_changes hang without this series, and pass with
"read the late-release setting at adoption" alone and with the series.  No
test here hangs without this commit; it covers contention those tests do
not produce.

Part of xcl-dev#258.
gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 25, 2026
…#2127, xcl-dev#2476, xcl-dev#2479, xcl-dev#2467, xcl-dev#2444, xcl-dev#2447, xcl-dev#2448

Integrates compositor RT tracking fix (xcl-dev#2480), split shader cache budget
(xcl-dev#2475), foreign-thread deferred release (xcl-dev#2474), Wine 4-thread DXC cap
(xcl-dev#2127), DXIL feature/reload fixes (xcl-dev#2476, xcl-dev#2479), DXC loader cleanup
(xcl-dev#2467), console xcl-dev#14 null ppv guard (xcl-dev#2444), root-sig flag stripping
(xcl-dev#2447), and two-pass stream canonicalization (xcl-dev#2448).

Assisted-by: Antigravity (Gemini 4 Neon)
gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 25, 2026
…extures)

Integrates xcl-dev#2436 cleanly alongside xcl-dev#2385 (top-mip shedding) and xcl-dev#2474
(foreign-thread release_cleanup).

Assisted-by: Antigravity (Gemini 4 Neon)
@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

@vakaobr

vakaobr commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Replaced by #2498, which sits on top of #2445 and uses its ring and native-host gate instead of a second ring here (see #258). Closing this one.

@vakaobr vakaobr closed this Nov 25, 2026
@SlopTart

Copy link
Copy Markdown

gpu_ suite (cold cache): 164 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.
Targeted run of the new tests (deferred_ring_* x5, a_mac_host_is_darwin_under_wine_unless_forced, force_foreign_flag_overrides_detection): 7 passed. Worth noting for the host-detection tests: on this Wine/Linux lane the mac-host detection reads as expected and the force flag behaves.

@bmjask bmjask left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No confirmed ring/lifetime defect found: foreign_release preserves caller semantics, reserved-slot handling prevents consumer overtaking, and full-ring leaking is an intentional safety choice; current Windows CI is green. This draft/stack needs required smoke evidence. Tests do not cover full adopted-resource lifecycle (foreign Release -> Wine drain -> bookkeeping cleanup/late release) or validate the D3DMetal native stack-pointer classification. Add target-path integration and supported CrossOver false-positive coverage.

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.

4 participants