D3D12.X: drop a heap's, queue's and allocator's books before last Release #2701

Open
github-bot wants to merge 1 commit from github-bot/gh-2701 into main AGit
Owner

@⁠sage-sage on GitHub · 2026-11-26 07:12 UTC · original
sage-sage:d3d12x/release-books-first → main · +203 −49 · 2 files · 1 commits · open

heap_release, mem_heap_release, q_release and alloc_release (real.rs) call the driver's Release first and, when it returns 0, remove what XCL keeps by the object's address. By then the driver has freed the object, and another thread can already have a new one at that address with its own entries recorded; the cleanup removes those. For an ID3D12Heap that is worse than lost bookkeeping: mem_heap_release takes the new heap's HEAP_UMA entry and frees its block of unified memory while the new heap's buffers live in it. A descriptor heap made at a freed one's address loses its console slots, a queue or an allocator its list_x / alloc_pool entries.

The command list and pipeline releases have the same order (#2690); #2691, #1453 and #2422 change those, and this PR does the other four. It uses the same check as res_release, #2691 and #2422 (the last reference is the one AddRef sees as 2). #1457, #1648 and #2086 also change mem_heap_release (for buffers that outlive their heap, a different problem); whichever goes in second needs a small merge there.

What is in it

  • real.rs: release_taking_first(this, take, after) takes a reference first; when AddRef returns 2 the caller holds the last one, so take removes the entries while the address is still this object's, the two references are released, and after frees what must outlive the object (the heap's memory block, a descriptor heap's slots and shadow), after the last Release as before. When two last-but-one releases run at once, both see AddRef above 2; the one whose Release ends at 0 then takes the entries after the object is gone, as main does, so nothing is left behind (the address-reuse window stays open only in that case, as in #2691 and res_release). The four hooks use it.
  • console_heap.rs: unregister is split into take and free for that; unregister itself is unchanged for its other callers.
  • Tests in real.rs (release_order_tests), on a fake object whose last Release records an entry at its address as a new object there would: mem_heap_release_keeps_a_new_heap_at_the_freed_address fails on main (dropped the heap made at the freed address) and pass with this change; take_runs_before_the_last_release_and_after_once_it_is_gone checks the helper's order; two_last_releases_at_once_take_the_entries_once makes two threads' AddRefs meet at a barrier and checks the entries are taken exactly once (it fails, left: (0, 0), without the fallback).

Tested

  • RTX 4080 SUPER (driver 621.40), Ryzen 5 7600X, Windows 11 26H2 build 26300.10075, rustc 1.100.0, DXC 1.9.2602.17 (Windows SDK 10.0.26100), debug build, base b49be18, head 9e305ed
  • cargo test --workspace -- --test-threads=4: 1438 passed, 0 failed, 47 ignored; the three release_order_tests pass
  • the heap test against main's mem_heap_release (this branch's test and helper on main's real.rs): it fails
  • SKIPPED on this adapter: the wave64 tests (NVIDIA, wave32 only), the D3D12 debug layer tests (d3d12SDKLayers.dll not installed), pso_measurement_compiles_an_exact_synthetic_record (Developer Mode off), no heap windows

Regression run

  • None: I own no Series X|S title to run scripts/smoke.ps1 on. This changes the runtime, so it needs a smoke run from someone who has one.

Not tested

  • Any game: no xcl-smoke run, and no title that I know of shows the race (it needs a release and a create on two threads at once with the allocator handing back the address).
  • heap_release, q_release and alloc_release have no test of their own; they use the same helper, which is tested.
  • AMD / Intel GPUs, a release build.

AI use

  • Claude Code, Claude Opus 5.5: found the order while checking the release hooks after a command list fix (the same one as #2691), wrote the change and the tests, ran the tests and wrote this description.

🤖 Generated with Claude Code

**@⁠sage-sage** on GitHub · 2026-11-26 07:12 UTC · [original](https://github.com/xcl-dev/xcl/pull/2701) `sage-sage:d3d12x/release-books-first` → `main` · +203 −49 · 2 files · 1 commits · open `heap_release`, `mem_heap_release`, `q_release` and `alloc_release` (`real.rs`) call the driver's `Release` first and, when it returns 0, remove what XCL keeps by the object's address. By then the driver has freed the object, and another thread can already have a new one at that address with its own entries recorded; the cleanup removes those. For an `ID3D12Heap` that is worse than lost bookkeeping: `mem_heap_release` takes the new heap's `HEAP_UMA` entry and frees its block of unified memory while the new heap's buffers live in it. A descriptor heap made at a freed one's address loses its console slots, a queue or an allocator its `list_x` / `alloc_pool` entries. The command list and pipeline releases have the same order (#2690); #2691, #1453 and #2422 change those, and this PR does the other four. It uses the same check as `res_release`, #2691 and #2422 (the last reference is the one `AddRef` sees as 2). #1457, #1648 and #2086 also change `mem_heap_release` (for buffers that outlive their heap, a different problem); whichever goes in second needs a small merge there. **What is in it** - `real.rs`: `release_taking_first(this, take, after)` takes a reference first; when `AddRef` returns 2 the caller holds the last one, so `take` removes the entries while the address is still this object's, the two references are released, and `after` frees what must outlive the object (the heap's memory block, a descriptor heap's slots and shadow), after the last `Release` as before. When two last-but-one releases run at once, both see `AddRef` above 2; the one whose `Release` ends at 0 then takes the entries after the object is gone, as `main` does, so nothing is left behind (the address-reuse window stays open only in that case, as in #2691 and `res_release`). The four hooks use it. - `console_heap.rs`: `unregister` is split into `take` and `free` for that; `unregister` itself is unchanged for its other callers. - Tests in `real.rs` (`release_order_tests`), on a fake object whose last `Release` records an entry at its address as a new object there would: `mem_heap_release_keeps_a_new_heap_at_the_freed_address` fails on `main` (`dropped the heap made at the freed address`) and pass with this change; `take_runs_before_the_last_release_and_after_once_it_is_gone` checks the helper's order; `two_last_releases_at_once_take_the_entries_once` makes two threads' `AddRef`s meet at a barrier and checks the entries are taken exactly once (it fails, `left: (0, 0)`, without the fallback). **Tested** - RTX 4080 SUPER (driver 621.40), Ryzen 5 7600X, Windows 11 26H2 build 26300.10075, rustc 1.100.0, DXC 1.9.2602.17 (Windows SDK 10.0.26100), debug build, base `b49be18`, head `9e305ed` - `cargo test --workspace -- --test-threads=4`: 1438 passed, 0 failed, 47 ignored; the three `release_order_tests` pass - the heap test against `main`'s `mem_heap_release` (this branch's test and helper on `main`'s `real.rs`): it fails - SKIPPED on this adapter: the wave64 tests (NVIDIA, wave32 only), the D3D12 debug layer tests (`d3d12SDKLayers.dll` not installed), `pso_measurement_compiles_an_exact_synthetic_record` (Developer Mode off), `no heap windows` **Regression run** - None: I own no Series X|S title to run `scripts/smoke.ps1` on. This changes the runtime, so it needs a smoke run from someone who has one. **Not tested** - Any game: no `xcl-smoke` run, and no title that I know of shows the race (it needs a release and a create on two threads at once with the allocator handing back the address). - `heap_release`, `q_release` and `alloc_release` have no test of their own; they use the same helper, which is tested. - AMD / Intel GPUs, a release build. **AI use** - Claude Code, Claude Opus 5.5: found the order while checking the release hooks after a command list fix (the same one as #2691), wrote the change and the tests, ran the tests and wrote this description. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
`heap_release`, `mem_heap_release`, `q_release` and `alloc_release`
called the driver's Release first and, when it returned 0, removed what
XCL keeps by the object's address. Once the driver has freed the object,
another thread can create a new one at the same address and record its
own entries; the cleanup then removes those. For an ID3D12Heap that is
worse than lost bookkeeping: `mem_heap_release` takes the new heap's
`HEAP_UMA` entry and frees its block of unified memory while the new
heap's buffers live in it. A descriptor heap loses its console slots, a
queue or an allocator its list_x / alloc_pool entries.

`release_taking_first` takes a reference first; when AddRef returns 2
the caller holds the last one, so the entries are removed while the
address is still this object's, and what must outlive the object (the
heap's memory block, a descriptor heap's slots and shadow) is freed
after the last Release, as before. `console_heap::unregister` is split
into `take` and `free` for that.

The command list and pipeline releases have the same order; #2691,
#1453 and #2422 change those (#2690).

Assisted-by: Claude Code (Claude Opus 5.5)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 07:16:07 +00:00
Author
Owner

@⁠luckyteagirl on GitHub · 2026-11-26 07:26 UTC · original · reviewed

real.rs:7021–7025: this loses cleanup when two threads release the last two references concurrently. Starting with refcount 2, A's AddRef returns 3 and B's returns 4, so neither calls take. If A then makes its two Release calls (4→3→2) and B makes its two (2→1→0), n is 0 for B but taken is None for both. No queue/allocator/heap bookkeeping is removed; mem_heap_release can also leave its UMA block allocated. Main used the zero-return path, so this is a new leak/stale-address case in the attempted ordering fix.

Could you cover that interleaving with a barrier after both temporary AddRef calls and arrange one-time cleanup even when the final caller did not see AddRef == 2? A simple zero-return fallback would need to account for the address-reuse race this PR is fixing. This is source-derived at exact head 052a3235a0cd4ce38bacf177d45f7ec4a64e6343; I have not run a native concurrency test. AI-assisted review with Codex.

**@⁠luckyteagirl** on GitHub · 2026-11-26 07:26 UTC · [original](https://github.com/xcl-dev/xcl/pull/2701#pullrequestreview-6070025180) · **reviewed** `real.rs:7021–7025`: this loses cleanup when two threads release the last two references concurrently. Starting with refcount 2, A's `AddRef` returns 3 and B's returns 4, so neither calls `take`. If A then makes its two `Release` calls (4→3→2) and B makes its two (2→1→0), `n` is 0 for B but `taken` is `None` for both. No queue/allocator/heap bookkeeping is removed; `mem_heap_release` can also leave its UMA block allocated. Main used the zero-return path, so this is a new leak/stale-address case in the attempted ordering fix. Could you cover that interleaving with a barrier after both temporary `AddRef` calls and arrange one-time cleanup even when the final caller did not see `AddRef == 2`? A simple zero-return fallback would need to account for the address-reuse race this PR is fixing. This is source-derived at exact head `052a3235a0cd4ce38bacf177d45f7ec4a64e6343`; I have not run a native concurrency test. AI-assisted review with Codex.
github-bot force-pushed github-bot/gh-2701 from 052a3235a0 to 9e305ed629 2026-11-26 08:58:51 +00:00 Compare
Author
Owner

@⁠sage-sage on GitHub · 2026-11-26 08:55 UTC · original

@⁠luckyteagirl thank you, that was a real gap: with both temporary AddRefs above 2, nobody cleaned up. Fixed in 9e305ed (squashed): the release whose Release ends at 0 now takes the entries after the object is gone when no call saw AddRef == 2, as main did, so nothing is left behind and the heap's block is freed. The address-reuse window stays open only in that interleaving, as in #2691 and res_release.

New test two_last_releases_at_once_take_the_entries_once, as you suggested: two threads' AddRefs meet at a barrier, so both see more than 2; it checks the entries are taken and after runs exactly once. Without the fallback it fails (left: (0, 0)), with it it passes. cargo test --workspace -- --test-threads=4 on 9e305ed: 1438 passed, 0 failed, 47 ignored (RTX 4080 SUPER, debug).

Claude Code (Claude Opus 5.5) made the change, ran the tests and wrote this reply.

**@⁠sage-sage** on GitHub · 2026-11-26 08:55 UTC · [original](https://github.com/xcl-dev/xcl/pull/2701#issuecomment-7187197834) @⁠luckyteagirl thank you, that was a real gap: with both temporary `AddRef`s above 2, nobody cleaned up. Fixed in `9e305ed` (squashed): the release whose `Release` ends at 0 now takes the entries after the object is gone when no call saw `AddRef == 2`, as `main` did, so nothing is left behind and the heap's block is freed. The address-reuse window stays open only in that interleaving, as in #2691 and `res_release`. New test `two_last_releases_at_once_take_the_entries_once`, as you suggested: two threads' `AddRef`s meet at a barrier, so both see more than 2; it checks the entries are taken and `after` runs exactly once. Without the fallback it fails (`left: (0, 0)`), with it it passes. `cargo test --workspace -- --test-threads=4` on `9e305ed`: 1438 passed, 0 failed, 47 ignored (RTX 4080 SUPER, debug). Claude Code (Claude Opus 5.5) made the change, ran the tests and wrote this reply.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2701/head:github-bot/gh-2701'
git switch 'github-bot/gh-2701'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2701'
git switch 'github-bot/gh-2701'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2701'
git switch 'github-bot/gh-2701'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2701'
git switch 'main'
git merge --squash 'github-bot/gh-2701'
git switch 'main'
git merge --ff-only 'github-bot/gh-2701'
git switch 'main'
git merge 'github-bot/gh-2701'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.