D3D12.X: mem_heap_release frees HEAP_UMA backing immediately while placed resources may still be in flight #1394

Open
opened 2026-11-26 04:52:42 +00:00 by github-bot · 1 comment
Owner

@⁠microwave805 on GitHub · 2026-11-24 15:31 UTC · original

Checked on main (930e8b4).

Problem

Unlike res_release (which holds a resource's final Release via late_release::hold — and PR #1070 defers its backing-memory teardown), mem_heap_release (crates/xcl/src/hle/d3d12x/real.rs:3007-3021) destroys the ID3D12Heap and immediately frees its HEAP_UMA block without waiting for in-flight GPU work or held placed resources:

unsafe extern "system" fn mem_heap_release(this: *mut c_void) -> u32 {
    let f: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(orig_fn(this, 2));
    let n = f(this);
    if n == 0 {
        let gone = HEAP_UMA.lock().unwrap().as_mut().and_then(|m| m.remove(&(this as usize))).and_then(|e| e.block);
        if let (Some(p), Some(u)) = (gone, crate::gpu::uma::get()) {
            if u.is_sparse(p) {
                super::sparse::free(p);
            } else {
                u.free(p);
            }
        }
    }
    n
}
  • For coherent UMA blocks, u.free(p) (gpu/uma.rs:575-604) immediately returns the memory to Uma::inner.free, where the next u.alloc() zeroes (write_bytes at uma.rs:569) and reuses it.
  • For sparse UMA blocks, sparse::free(p) immediately parks/unmaps tiles, decommits virtual pages (MEM_DECOMMIT), and releases the sparse address range.

Minimal trigger

  1. Title creates a console ID3D12Heap backed by HEAP_UMA, places a resource R on it, submits GPU work referencing R, and releases both R and the heap while the GPU queue is busy (a common pattern; R is kept alive by late_release, or R is still referenced by in-flight command lists).
  2. mem_heap_release sees n == 0 and immediately frees/decommits/zeroes the heap's backing UMA/sparse block while the GPU is still accessing R.

(Checked #1040 / PR #1070 — neither touches mem_heap_release or HEAP_UMA.)

Suggested fix

Hold the final ID3D12Heap release and defer freeing its HEAP_UMA block through late_release until in-flight GPU queues have passed.


AI use: Antigravity (Gemini 4 Neon) performed the read-only source audit and drafted this issue; checked and verified on 930e8b4 before opening.

**@⁠microwave805** on GitHub · 2026-11-24 15:31 UTC · [original](https://github.com/xcl-dev/xcl/issues/1394) Checked on `main` (`930e8b4`). ### Problem Unlike `res_release` (which holds a resource's final `Release` via `late_release::hold` — and PR #1070 defers its backing-memory teardown), `mem_heap_release` (`crates/xcl/src/hle/d3d12x/real.rs:3007-3021`) destroys the `ID3D12Heap` and immediately frees its `HEAP_UMA` block without waiting for in-flight GPU work or held placed resources: ```rust unsafe extern "system" fn mem_heap_release(this: *mut c_void) -> u32 { let f: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(orig_fn(this, 2)); let n = f(this); if n == 0 { let gone = HEAP_UMA.lock().unwrap().as_mut().and_then(|m| m.remove(&(this as usize))).and_then(|e| e.block); if let (Some(p), Some(u)) = (gone, crate::gpu::uma::get()) { if u.is_sparse(p) { super::sparse::free(p); } else { u.free(p); } } } n } ``` - For coherent UMA blocks, `u.free(p)` (`gpu/uma.rs:575-604`) immediately returns the memory to `Uma::inner.free`, where the next `u.alloc()` zeroes (`write_bytes` at `uma.rs:569`) and reuses it. - For sparse UMA blocks, `sparse::free(p)` immediately parks/unmaps tiles, decommits virtual pages (`MEM_DECOMMIT`), and releases the sparse address range. ### Minimal trigger 1. Title creates a console `ID3D12Heap` backed by `HEAP_UMA`, places a resource `R` on it, submits GPU work referencing `R`, and releases both `R` and the heap while the GPU queue is busy (a common pattern; `R` is kept alive by `late_release`, or `R` is still referenced by in-flight command lists). 2. `mem_heap_release` sees `n == 0` and immediately frees/decommits/zeroes the heap's backing UMA/sparse block while the GPU is still accessing `R`. *(Checked #1040 / PR #1070 — neither touches `mem_heap_release` or `HEAP_UMA`.)* ### Suggested fix Hold the final `ID3D12Heap` release and defer freeing its `HEAP_UMA` block through `late_release` until in-flight GPU queues have passed. --- **AI use:** Antigravity (Gemini 4 Neon) performed the read-only source audit and drafted this issue; checked and verified on `930e8b4` before opening.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:52:42 +00:00
Author
Owner

@⁠ySKELLETONX on GitHub · 2026-11-24 16:06 UTC · original

I'll take this, stacked on #1070 (it gives late_release::hold the block to free once the queues are past): mem_heap_release holds the heap's last Release the way res_release does and passes its HEAP_UMA block along, so a heap released while a queue still has work keeps its block until the queues are past it. With a GPU test like #1070's (a queue held on a fence: the block stays allocated until the fence is signalled).

Written with Claude Code (Claude Opus 5.5).

**@⁠ySKELLETONX** on GitHub · 2026-11-24 16:06 UTC · [original](https://github.com/xcl-dev/xcl/issues/1394#issuecomment-7137141839) I'll take this, stacked on #1070 (it gives `late_release::hold` the block to free once the queues are past): `mem_heap_release` holds the heap's last `Release` the way `res_release` does and passes its `HEAP_UMA` block along, so a heap released while a queue still has work keeps its block until the queues are past it. With a GPU test like #1070's (a queue held on a fence: the block stays allocated until the fence is signalled). Written with Claude Code (Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.