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

Closed
vakaobr wants to merge 3 commits from upstream-pr/2474 into main
vakaobr commented 2026-11-25 19:41:12 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by vakaobr on 2026-11-25T19:41:12Z.


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

  • (#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

  • MacBook Pro M5 Pro, macOS 27.2.1, CrossOver 26.3.0, D3DMetal 3.0, Rust 1.100.0 x86_64-pc-windows-gnu, debug, at this branch's head (on #2361's 6d6a3e3, on main 65fc114).
  • hle::d3d12x::late_release::tests, each in its own process in the CrossOver bottle: 10 of 10 pass.
  • Earlier, on the same Mac with this change on top of main 85e3826 (+ #553 + the vtable fix now in #1904): console_wave64_compute_through_the_pso_hook_dispatch_and_read_back and gpu_mirror_upload_follows_cpu_writes_and_later_mapping_changes hang without #1789 and pass with it, alone and with this commit. No test here hangs without this commit alone; it covers contention those tests do not produce. With it, #2402's tests run on a Mac (without it, a list touching a placed texture never completes).

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

  • Claude Code: Claude Opus 4.6 wrote the first version and rebased this onto #2361; Claude Opus 5.5 rewrote the ring and the foreign Release after review by Ahmet-Dedeler, ran the tests and wrote this description.
<!-- greyfinch-sync:ivall/xcl:issue:6612295860 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2474). Originally posted by [vakaobr](https://github.com/vakaobr) on 2026-11-25T19:41:12Z. --- 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** - (#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** - MacBook Pro M5 Pro, macOS 27.2.1, CrossOver 26.3.0, D3DMetal 3.0, Rust 1.100.0 `x86_64-pc-windows-gnu`, debug, at this branch's head (on #2361's `6d6a3e3`, on `main` `65fc114`). - `hle::d3d12x::late_release::tests`, each in its own process in the CrossOver bottle: 10 of 10 pass. - Earlier, on the same Mac with this change on top of `main` `85e3826` (+ #553 + the vtable fix now in #1904): `console_wave64_compute_through_the_pso_hook_dispatch_and_read_back` and `gpu_mirror_upload_follows_cpu_writes_and_later_mapping_changes` hang without #1789 and pass with it, alone and with this commit. No test here hangs without this commit alone; it covers contention those tests do not produce. With it, #2402's tests run on a Mac (without it, a list touching a placed texture never completes). **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** - Claude Code: Claude Opus 4.6 wrote the first version and rebased this onto #2361; Claude Opus 5.5 rewrote the ring and the foreign `Release` after review by Ahmet-Dedeler, ran the tests and wrote this description.
upstream-sync closed this pull request 2026-11-25 21:04:53 +00:00
bmjask commented 2026-11-25 21:04:54 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-11-25T20:56:35Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ivall/xcl:review:6064873636 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2474#pullrequestreview-6064873636). Originally posted by [bmjask](https://github.com/bmjask) on 2026-11-25T20:56:35Z. **Review:** COMMENTED --- 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.
DavidM1911 commented 2026-11-25 21:05:19 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T20:37:53Z.


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

  • cargo test -p xcl --lib: 1038 passed, 1 failed in one parallel run; the failing test passes on a single-threaded rerun (1039/0). Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7173955060 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2474#issuecomment-7173955060). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-25T20:37:53Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: 1038 passed, 1 failed in one parallel run; the failing test passes on a single-threaded rerun (1039/0). Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun. Thanks!
vakaobr commented 2026-11-25 21:05:21 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by vakaobr on 2026-11-25T20:41:59Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7174059296 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2474#issuecomment-7174059296). Originally posted by [vakaobr](https://github.com/vakaobr) on 2026-11-25T20:41:59Z. --- 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.
SlopTart commented 2026-11-25 21:05:25 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by SlopTart on 2026-11-25T20:52:01Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7174294621 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2474#issuecomment-7174294621). Originally posted by [SlopTart](https://github.com/SlopTart) on 2026-11-25T20:52:01Z. --- `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.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!2464
No description provided.