D3D12.X: a Release on a thread XCL did not create is made on a Wine thread #2488

Open
vakaobr wants to merge 4 commits from upstream-pr/2498 into main
vakaobr commented 2026-11-25 20:41:47 +00:00 (Migrated from github.com)

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


Stacked on #2445; only the top commit belongs here. Part of #258. It replaces #2474, which added a second ring next to #2445's.

A Release that D3DMetal makes on a thread of its own (no Wine TEB) runs none of XCL's release path there: the caller's reference goes into #2445's late-release ring, and a drain gives it back through res_release on a Wine thread. With #2445 a kept last Release is already queued; the rest of res_release still ran on the host's thread, so a last Release that is not kept (XCL_LATE_RELEASE=0, or two last-but-one releases that miss each other) took XCL's locks there, and a contended one waits in Wine's ntdll forever.

What is in it

  • crates/xcl/src/hle/d3d12x/late_release.rs: is_foreign_thread (on a native host, #2445's real::native_host, a stack at or above 0x7000_0000_0000; measured on an M5 Pro: Wine threads 0x0180ea9f..0x0220fb37, D3DMetal 0x7ff2063df667..0x7ff2066c3677); defer_release puts the reference into #2445's ring with bit 0 set (OWED; object addresses are aligned), because an entry there otherwise means "released for good, one reference kept" and a foreign Release may not be the last; drain_known runs owed entries through real::res_release and the others through #2445's path unchanged. Tests wine_thread_is_not_foreign, spawned_threads_are_not_foreign, force_foreign_flag_overrides_detection.
  • crates/xcl/src/hle/d3d12x/real.rs: on a foreign thread res_release reads the count with the host's AddRef / Release, defers the caller's reference and returns the count it will have. Test a_release_on_a_foreign_thread_is_given_back_by_a_drain.

Tested

  • MacBook Pro M5 Pro, macOS 27.2.1, CrossOver 26.3.0, D3DMetal 3.0 (the test device's tables are in no PE module, so NATIVE_HOST is set). Rust 1.100.0 x86_64-pc-windows-gnu, debug, on #2445's head 6979e9f.
  • Each test in its own process under Wine (<exe> --exact <test> --test-threads=1), on #2445's head and on this commit:
    • the 8 late_release tests (5 from #2445, 3 new): pass; #2445's 5 also pass on its head;
    • the 3 real::native_host_tests (2 from #2445, 1 new): pass; the new one 6 of 6 runs;
    • gpu_mirror_upload_follows_cpu_writes_and_later_mapping_changes and a_buffer_released_while_the_state_is_held_does_not_wait_for_it: pass on both.
  • a_release_on_a_foreign_thread_is_given_back_by_a_drain with the new branch in res_release taken out fails: "the foreign thread's Release was made on that thread" (count 1, not 2).
  • gpu_compute_e2e::console_wave64_compute_through_the_pso_hook_dispatch_and_read_back page-faults inside D3DMetal on the test's own thread, the same on #2445's head and on this commit (2 runs each); it does not reach this code.

Regression run

  • None: I have no title of my own to run scripts/smoke.ps1 on.

Not tested

  • Windows, Wine on Linux, the whole suite, any title.
  • A real D3DMetal thread taking the new path in a test: the foreign thread is forced on a Wine thread (FORCE_FOREIGN).
  • The detection tests run with NATIVE_HOST unset (no device in their process); the stack check on a Wine thread with it set is covered only by the new GPU test.

AI use

  • Claude Code with Claude Opus 5.5: wrote the change and the tests on top of #2445, ran the tests on the Mac above, and wrote the commit message and this description.
<!-- greyfinch-sync:ivall/xcl:issue:5498832315 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2498). Originally posted by [vakaobr](https://github.com/vakaobr) on 2026-11-25T20:41:47Z. --- Stacked on #2445; only the top commit belongs here. Part of #258. It replaces #2474, which added a second ring next to #2445's. A `Release` that D3DMetal makes on a thread of its own (no Wine TEB) runs none of XCL's release path there: the caller's reference goes into #2445's late-release ring, and a drain gives it back through `res_release` on a Wine thread. With #2445 a kept last `Release` is already queued; the rest of `res_release` still ran on the host's thread, so a last `Release` that is not kept (`XCL_LATE_RELEASE=0`, or two last-but-one releases that miss each other) took XCL's locks there, and a contended one waits in Wine's ntdll forever. **What is in it** - `crates/xcl/src/hle/d3d12x/late_release.rs`: `is_foreign_thread` (on a native host, #2445's `real::native_host`, a stack at or above `0x7000_0000_0000`; measured on an M5 Pro: Wine threads `0x0180ea9f..0x0220fb37`, D3DMetal `0x7ff2063df667..0x7ff2066c3677`); `defer_release` puts the reference into #2445's ring with bit 0 set (`OWED`; object addresses are aligned), because an entry there otherwise means "released for good, one reference kept" and a foreign `Release` may not be the last; `drain_known` runs owed entries through `real::res_release` and the others through #2445's path unchanged. Tests `wine_thread_is_not_foreign`, `spawned_threads_are_not_foreign`, `force_foreign_flag_overrides_detection`. - `crates/xcl/src/hle/d3d12x/real.rs`: on a foreign thread `res_release` reads the count with the host's AddRef / Release, defers the caller's reference and returns the count it will have. Test `a_release_on_a_foreign_thread_is_given_back_by_a_drain`. **Tested** - MacBook Pro M5 Pro, macOS 27.2.1, CrossOver 26.3.0, D3DMetal 3.0 (the test device's tables are in no PE module, so `NATIVE_HOST` is set). Rust 1.100.0 `x86_64-pc-windows-gnu`, debug, on #2445's head `6979e9f`. - Each test in its own process under Wine (`<exe> --exact <test> --test-threads=1`), on #2445's head and on this commit: - the 8 `late_release` tests (5 from #2445, 3 new): pass; #2445's 5 also pass on its head; - the 3 `real::native_host_tests` (2 from #2445, 1 new): pass; the new one 6 of 6 runs; - `gpu_mirror_upload_follows_cpu_writes_and_later_mapping_changes` and `a_buffer_released_while_the_state_is_held_does_not_wait_for_it`: pass on both. - `a_release_on_a_foreign_thread_is_given_back_by_a_drain` with the new branch in `res_release` taken out fails: "the foreign thread's Release was made on that thread" (count 1, not 2). - `gpu_compute_e2e::console_wave64_compute_through_the_pso_hook_dispatch_and_read_back` page-faults inside D3DMetal on the test's own thread, the same on #2445's head and on this commit (2 runs each); it does not reach this code. **Regression run** - None: I have no title of my own to run `scripts/smoke.ps1` on. **Not tested** - Windows, Wine on Linux, the whole suite, any title. - A real D3DMetal thread taking the new path in a test: the foreign thread is forced on a Wine thread (`FORCE_FOREIGN`). - The detection tests run with `NATIVE_HOST` unset (no device in their process); the stack check on a Wine thread with it set is covered only by the new GPU test. **AI use** - Claude Code with Claude Opus 5.5: wrote the change and the tests on top of #2445, ran the tests on the Mac above, and wrote the commit message and this description.
SlopTart commented 2026-11-25 21:05:29 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by SlopTart on 2026-11-25T21:00:53Z.


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 all nine new tests (wine_thread_is_not_foreign, spawned_threads_are_not_foreign, a_release_on_a_foreign_thread_is_given_back_by_a_drain, the_ring_gives_back_what_was_handed_over_in_order, writers_and_readers_at_once_lose_nothing_and_take_nothing_twice, a_full_ring_keeps_the_resource_and_does_not_wait, only_a_table_in_no_pe_module_makes_a_native_host, a_kept_last_release_on_a_native_host_waits_for_no_lock, force_foreign_flag_overrides_detection), plus existing tests matching the same substrings: 22 passed, 1 ignored. This is squarely Wine-lane territory: under wine-11.0 the PE-table native-host detection and the wine-thread non-foreign classification both behave as the PR intends.

<!-- greyfinch-sync:ivall/xcl:comment:7174492479 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2498#issuecomment-7174492479). Originally posted by [SlopTart](https://github.com/SlopTart) on 2026-11-25T21:00:53Z. --- `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 all nine new tests (`wine_thread_is_not_foreign`, `spawned_threads_are_not_foreign`, `a_release_on_a_foreign_thread_is_given_back_by_a_drain`, `the_ring_gives_back_what_was_handed_over_in_order`, `writers_and_readers_at_once_lose_nothing_and_take_nothing_twice`, `a_full_ring_keeps_the_resource_and_does_not_wait`, `only_a_table_in_no_pe_module_makes_a_native_host`, `a_kept_last_release_on_a_native_host_waits_for_no_lock`, `force_foreign_flag_overrides_detection`), plus existing tests matching the same substrings: 22 passed, 1 ignored. This is squarely Wine-lane territory: under wine-11.0 the PE-table native-host detection and the wine-thread non-foreign classification both behave as the PR intends.
DavidM1911 commented 2026-11-25 22:06:36 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T21:05:51Z.


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:7174607221 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2498#issuecomment-7174607221). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-25T21:05:51Z. --- 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!
github-actions[bot] commented 2026-11-30 08:40:10 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-11-30T08:40:10Z.


Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

<!-- greyfinch-sync:ivall/xcl:comment:7273194398 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2498#issuecomment-7273194398). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-30T08:40:10Z. --- Nothing has happened on this PR for 3 days, so it is marked `stale` and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto `main` if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.
vakaobr commented 2026-11-30 13:22:05 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by vakaobr on 2026-11-30T13:22:05Z.


Still waiting on #2445, which this is stacked on and which needs a rebase by its author; I'll rebase this once it is.

<!-- greyfinch-sync:ivall/xcl:comment:7279743149 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2498#issuecomment-7279743149). Originally posted by [vakaobr](https://github.com/vakaobr) on 2026-11-30T13:22:05Z. --- Still waiting on #2445, which this is stacked on and which needs a rebase by its author; I'll rebase this once it is.
Foniass commented 2026-12-01 08:12:08 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T08:12:08Z.


Triage (#1607) – head 85a02de

Stacked on #2445 (itself stacked on #1789): the bottom three commits are theirs, only 85a02de (foreign-thread Release deferred through the late-release ring with the OWED bit) belongs here. Not a duplicate of either; nothing of it is on main.
The stack underneath is #2445's old head 6979e9f: its Ring::push checks room and then fetch_adds the head, relying on RING_WRITERS reserve slots, while #2445's current head 6df8f64 claims the index with a compare-exchange so a full ring can never overwrite an unread slot. This PR still carries the old ring and its tests; it needs a rebase onto #2445's current head.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7305006952 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2498#issuecomment-7305006952). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T08:12:08Z. --- **Triage (#1607)** – head 85a02de Stacked on #2445 (itself stacked on #1789): the bottom three commits are theirs, only `85a02de` (foreign-thread Release deferred through the late-release ring with the `OWED` bit) belongs here. Not a duplicate of either; nothing of it is on main. The stack underneath is #2445's old head `6979e9f`: its `Ring::push` checks room and then `fetch_add`s the head, relying on `RING_WRITERS` reserve slots, while #2445's current head `6df8f64` claims the index with a compare-exchange so a full ring can never overwrite an unread slot. This PR still carries the old ring and its tests; it needs a rebase onto #2445's current head. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2498:upstream-pr/2498'
git switch 'upstream-pr/2498'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2498'
git switch 'upstream-pr/2498'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2498'
git switch 'upstream-pr/2498'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2498'
git switch 'main'
git merge --squash 'upstream-pr/2498'
git switch 'main'
git merge --ff-only 'upstream-pr/2498'
git switch 'main'
git merge 'upstream-pr/2498'
git push origin 'main'
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!2488
No description provided.