D3D12.X: vram::unmap_superseded unmaps tiles synchronously on pages.queue without waiting on title GPU queues and races with remap_if_unmapped #1897

Open
opened 2026-11-26 05:08:37 +00:00 by github-bot · 3 comments
Owner

@⁠microwave805 on GitHub · 2026-11-24 22:37 UTC · original

Static audit on main @ 689f2db found two synchronization / lifetime bugs in sparse tile unmapping for superseded placed textures (vram::unmap_superseded and title_textures.rs):

Where

  • crates/xcl/src/hle/d3d12x/vram.rs:499-520 (Pages::unmap)
  • crates/xcl/src/hle/d3d12x/vram.rs:772-816 (unmap_superseded, remap_if_unmapped)
  • crates/xcl/src/hle/d3d12x/title_textures.rs:385-394, 479-515 (track, retire_overlapping, forget)

Root causes

  1. Cross-queue synchronous UpdateTileMappings(NULL) without waiting on title GPU queues (vram.rs:499-520, 772-789)
    When a title creates a new placed texture B overlapping an existing placed texture A (standard transient/aliased render-target or texture-pool reuse on console), title_textures::track() / retire_overlapping() immediately calls vram::unmap_superseded(A).
    Pages::unmap() calls self.queue.UpdateTileMappings(A, ..., D3D12_TILE_RANGE_FLAG_NULL, ...) on pages.queue — an independent internal ID3D12CommandQueue that never waits on the title's graphics, compute, or copy queues:

    • If the title has already recorded or submitted draws/copies referencing texture A (which are still in-flight or queued behind a fence on the title's queue), pages.queue immediately null-maps A's tiles out from under the executing/pending GPU work, causing A to read zeros / drop writes or corrupt rendering.
    • Furthermore, release_tiles(..., immediate_cool = true) (vram.rs:527-530) immediately drops unshared tiles into the wall-clock cooling pool with no GPU fence synchronization against the title's queues.
  2. Lock-gap TOCTOU race between unmap_superseded and remap_if_unmapped (title_textures.rs:385-394, 483-493, 501-512)
    Both track() / retire_overlapping() and forget() release TEXTURES before acquiring PAGES to call unmap_superseded() / remap_if_unmapped():

    1. Thread 1 creates B superseding A: under TEXTURES, moves A to shadowed, returns to_unmap = [A], and drops TEXTURES.
    2. Before Thread 1 enters unmap_superseded(A), Thread 2 releases B (forget(B)): under TEXTURES, restores A back into active by_res, returns to_remap = [A], drops TEXTURES, and calls remap_if_unmapped(A).
    3. remap_if_unmapped(A) (vram.rs:804-806) sees t.mapped == true and returns early as a no-op.
    4. Thread 1 now enters unmap_superseded(A) (vram.rs:777-786), sees t.mapped == true, and unmaps the now-active texture A to NULL.
    5. title_textures considers A active and mapped in by_res, while its D3D12 reserved resource is permanently NULL-mapped until released — silently dropping all future reads/writes to A.

Fix direction

  • For the cross-queue hazard: before unmapping A on pages.queue (and before returning its tiles to the pool), pages.queue must Wait on the title queues' current fence values (or defer the UpdateTileMappings(NULL) and tile cooling until after the queues that may reference A reach their submitted fence values, analogous to #1040).
  • For the TOCTOU race: serialize unmap_superseded / remap_if_unmapped state transitions per resource (e.g. monotonic generation/state check under TEXTURES before acting under PAGES, or an explicit desired-mapped state checked inside the lock) so a stale unmap_superseded cannot unmap a resource that was already restored to by_res.

AI use

  • Antigravity (Gemini 4 Neon): static code audit on main @ 689f2db and issue drafting.
**@⁠microwave805** on GitHub · 2026-11-24 22:37 UTC · [original](https://github.com/xcl-dev/xcl/issues/1897) Static audit on `main @ 689f2db` found two synchronization / lifetime bugs in sparse tile unmapping for superseded placed textures (`vram::unmap_superseded` and `title_textures.rs`): ### Where - `crates/xcl/src/hle/d3d12x/vram.rs:499-520` (`Pages::unmap`) - `crates/xcl/src/hle/d3d12x/vram.rs:772-816` (`unmap_superseded`, `remap_if_unmapped`) - `crates/xcl/src/hle/d3d12x/title_textures.rs:385-394, 479-515` (`track`, `retire_overlapping`, `forget`) ### Root causes 1. **Cross-queue synchronous `UpdateTileMappings(NULL)` without waiting on title GPU queues (`vram.rs:499-520, 772-789`)** When a title creates a new placed texture `B` overlapping an existing placed texture `A` (standard transient/aliased render-target or texture-pool reuse on console), `title_textures::track()` / `retire_overlapping()` immediately calls `vram::unmap_superseded(A)`. `Pages::unmap()` calls `self.queue.UpdateTileMappings(A, ..., D3D12_TILE_RANGE_FLAG_NULL, ...)` on `pages.queue` — an independent internal `ID3D12CommandQueue` that **never waits on the title's graphics, compute, or copy queues**: - If the title has already recorded or submitted draws/copies referencing texture `A` (which are still in-flight or queued behind a fence on the title's queue), `pages.queue` immediately null-maps `A`'s tiles out from under the executing/pending GPU work, causing `A` to read zeros / drop writes or corrupt rendering. - Furthermore, `release_tiles(..., immediate_cool = true)` (`vram.rs:527-530`) immediately drops unshared tiles into the wall-clock cooling pool with no GPU fence synchronization against the title's queues. 2. **Lock-gap TOCTOU race between `unmap_superseded` and `remap_if_unmapped` (`title_textures.rs:385-394, 483-493, 501-512`)** Both `track()` / `retire_overlapping()` and `forget()` release `TEXTURES` before acquiring `PAGES` to call `unmap_superseded()` / `remap_if_unmapped()`: 1. Thread 1 creates `B` superseding `A`: under `TEXTURES`, moves `A` to `shadowed`, returns `to_unmap = [A]`, and drops `TEXTURES`. 2. Before Thread 1 enters `unmap_superseded(A)`, Thread 2 releases `B` (`forget(B)`): under `TEXTURES`, restores `A` back into active `by_res`, returns `to_remap = [A]`, drops `TEXTURES`, and calls `remap_if_unmapped(A)`. 3. `remap_if_unmapped(A)` (`vram.rs:804-806`) sees `t.mapped == true` and returns early as a no-op. 4. Thread 1 now enters `unmap_superseded(A)` (`vram.rs:777-786`), sees `t.mapped == true`, and **unmaps the now-active texture `A` to `NULL`**. 5. `title_textures` considers `A` active and mapped in `by_res`, while its D3D12 reserved resource is permanently `NULL`-mapped until released — silently dropping all future reads/writes to `A`. ### Fix direction - For the cross-queue hazard: before unmapping `A` on `pages.queue` (and before returning its tiles to the pool), `pages.queue` must `Wait` on the title queues' current fence values (or defer the `UpdateTileMappings(NULL)` and tile cooling until after the queues that may reference `A` reach their submitted fence values, analogous to `#1040`). - For the TOCTOU race: serialize `unmap_superseded` / `remap_if_unmapped` state transitions per resource (e.g. monotonic generation/state check under `TEXTURES` before acting under `PAGES`, or an explicit desired-mapped state checked inside the lock) so a stale `unmap_superseded` cannot unmap a resource that was already restored to `by_res`. ### AI use - Antigravity (Gemini 4 Neon): static code audit on `main @ 689f2db` and issue drafting.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:08:38 +00:00
Author
Owner

@⁠Anon64 on GitHub · 2026-11-24 23:58 UTC · original

Where the two points of this issue stand in open PRs: point 1 (unmapping on pages.queue without waiting for the title's queues) is #594. Point 2 (the gap between unmap_superseded and forget's remap_if_unmapped) is not covered by #594 or #799. It touches #799's call sites, so I described how #799's lock could cover it there: #799. Checked by reading main fc14fab, not reproduced.

Written with Claude Code (Claude Opus 5.5).

**@⁠Anon64** on GitHub · 2026-11-24 23:58 UTC · [original](https://github.com/xcl-dev/xcl/issues/1897#issuecomment-7148002233) Where the two points of this issue stand in open PRs: point 1 (unmapping on `pages.queue` without waiting for the title's queues) is #594. Point 2 (the gap between `unmap_superseded` and `forget`'s `remap_if_unmapped`) is not covered by #594 or #799. It touches #799's call sites, so I described how #799's lock could cover it there: https://git.gtahub.st/github/xcl-dev--xcl/pulls/799. Checked by reading `main` `fc14fab`, not reproduced. Written with Claude Code (Claude Opus 5.5).
Author
Owner

@⁠JJAutoCompare on GitHub · 2026-11-25 00:41 UTC · original

I compared current main@6567c5f with #799 (c9633c6), #1733 (44089a2), and #594 (3bb3c2f). The stale-unmap path remains open. #799 serializes against refresh uploads but takes REFRESH after the decision to unmap. In that gap forget can restore the predecessor and see it is still mapped, after which the delayed unmap drops the active predecessor's tiles. #1733 handles pending upload accounting. Placement and release

A focused follow-up to #799 can revalidate shadowed state under TEXTURES immediately before unmapping and keep restoration and remap serialized with that check. Avoid acquiring REFRESH in forget, since refresh can drop references that re-enter the release path. A channel-gated test can restore the predecessor after the initial decision and assert the resumed unmap leaves it active and mapped.

The submitted-GPU-work lifetime is separate. #594 adds cross-queue waits, but a mapping fence can also hold up a later title submission that would release an earlier queue. A regression should show that the old resource retains its tiles while the first queue is blocked and that the other queue can still submit the signal that unblocks it. Source review only. No local GPU reproduction for this issue. Assisted by Codex (gpt-6.2-sol).

**@⁠JJAutoCompare** on GitHub · 2026-11-25 00:41 UTC · [original](https://github.com/xcl-dev/xcl/issues/1897#issuecomment-7148783255) I compared current `main@6567c5f` with #799 (`c9633c6`), #1733 (`44089a2`), and #594 (`3bb3c2f`). The stale-unmap path remains open. #799 serializes against refresh uploads but takes `REFRESH` after the decision to unmap. In that gap `forget` can restore the predecessor and see it is still mapped, after which the delayed unmap drops the active predecessor's tiles. #1733 handles pending upload accounting. [Placement and release](https://github.com/xcl-dev/xcl/blob/6567c5ffa38fbc7039b2d6bf520154689cc1f482/crates/xcl/src/hle/d3d12x/title_textures.rs#L385-L515) A focused follow-up to #799 can revalidate shadowed state under `TEXTURES` immediately before unmapping and keep restoration and remap serialized with that check. Avoid acquiring `REFRESH` in `forget`, since refresh can drop references that re-enter the release path. A channel-gated test can restore the predecessor after the initial decision and assert the resumed unmap leaves it active and mapped. The submitted-GPU-work lifetime is separate. #594 adds cross-queue waits, but a mapping fence can also hold up a later title submission that would release an earlier queue. A regression should show that the old resource retains its tiles while the first queue is blocked and that the other queue can still submit the signal that unblocks it. Source review only. No local GPU reproduction for this issue. Assisted by Codex (`gpt-6.2-sol`).
Author
Owner

@⁠Mustafaiq1998x on GitHub · 2026-11-25 02:16 UTC · original

I would like to take point 2 of this issue: the stale unmap race between texture retirement and restoration.

I plan to add a deterministic GPU regression test on an RX 6800 (native wave64), followed by a focused synchronization fix without duplicating #594 or #799.

Is anyone currently implementing this specific fix?

AI-assisted investigation using ChatGPT (GPT-6.2).

**@⁠Mustafaiq1998x** on GitHub · 2026-11-25 02:16 UTC · [original](https://github.com/xcl-dev/xcl/issues/1897#issuecomment-7150529399) I would like to take point 2 of this issue: the stale unmap race between texture retirement and restoration. I plan to add a deterministic GPU regression test on an RX 6800 (native wave64), followed by a focused synchronization fix without duplicating #594 or #799. Is anyone currently implementing this specific fix? AI-assisted investigation using ChatGPT (GPT-6.2).
This discussion has been locked. Commenting is limited to contributors.
No description provided.