Skip to content

GPU: hold SHADER_HEAPS while a shader-visible heap is used (port of upstream 628b95d) - #668

Open
coderanonymus wants to merge 2 commits into
fxceless:mainfrom
coderanonymus:shader-heaps-lock
Open

coderanonymus wants to merge 2 commits into
fxceless:mainfrom
coderanonymus:shader-heaps-lock

Conversation

@coderanonymus

Copy link
Copy Markdown

Port of upstream 628b95d (by Manuel Pérez). It is stacked on #642, which fixes the clippy failure on main.

I left this commit out of #653 because it doesn't build on the fork as it stands. It merges cleanly with #653.

Why

shader_slot() takes the SHADER_HEAPS lock, finds the heap and returns &Heap after the lock is released. A title that releases a descriptor heap on one thread while another writes a descriptor can therefore have Heap::drop free the heap while it is still being read.

The fork has a second caller with the same window. make_waiting() reads the slot (view(handle)) and makes the host view from a heap it no longer holds. The fork's own comment there notes the handle is "read only while its heap is there", but nothing kept the heap there.

What

  • Upstream's commit, cherry-picked as it is:
    • mirror() keeps SHADER_HEAPS locked for its whole run.
    • copy_simple() returns on an invalid heap kind.
    • create_heap refuses a placed heap whose address + size wraps.
    • resolve_queries checks offset + count * bytes for overflow, with a test (resolve_queries_rejects_overflowing_offset).
  • For the fork, in the same commit: make_waiting() finds the heap the same way and keeps SHADER_HEAPS held while it reads the slot and calls HostHeap::mirror(.., true). shader_slot() is gone.

Review

Ultracode-reviewed: three independent read-only reviewers traced the fork's code.

  • Re-entrancy. Everything reachable under the lock was traced, including HostHeap::mirror for both for_draw values, wait_for, host_copy, host texture creation and retire. Nothing takes SHADER_HEAPS again, records a descriptor, or drops a heap.
  • Lock order. Locks held when SHADER_HEAPS is taken: submitted, RELEASING, a queue's held lock, list.state. Locks taken under it: pending, made, VIEWS, WAITING, MADE_LATE, resource, shadow and card locks. No path takes the two sets in the opposite order, so the order is acyclic.
  • The rest of the pick. The fork doesn't rely on copy_simple's old behaviour, and the new test needs no host device.

The cost is wait time, not deadlock. Descriptor writes on other threads can now wait while a slot's host view is made, which can create a host texture. That's a short wait per slot. The first Heap::host() on a heap replays its queued writes while holding pending, so writes to any heap wait once per heap at startup.

Verification

  • cargo clippy --workspace --all-targets --locked -- -D warnings, and the same with --all-features, for x86_64-pc-windows-msvc (cross-checked from Linux): clean.
  • Not run: Windows tests, the game, or a run under load. The new test needs Windows.

No game data used.

🤖 Generated with Claude Code

LF2000 Builder and others added 2 commits November 24, 2026 22:48
- indirect.rs: the timing rig kept the written depth view it never reads
  after setup; drop the field.
- draw.rs: the scissor census sorts by a reversed key.
- resource.rs: placed_filler_twins is read only by the diagnostics
  build's copystat; allow it to be dead without the feature, as
  shadow and pipeline do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mirror() previously dropped the SHADER_HEAPS lock inside shader_slot() before using the returned &Heap, allowing a concurrent Heap::drop to free the heap while mirror() accessed it. Also guards copy_simple() against invalid descriptor kinds, rejects placed heaps whose address+size wraps u64, and checks offset+count*bytes in resolve_queries() against overflow (with a regression test).

(cherry picked from commit 628b95d)

Port notes: the fork's make_waiting() was a second caller of
shader_slot(). It now looks the heap up the same way and keeps
SHADER_HEAPS held while it reads the slot (view()), and while it makes
the host view (heap.host, HostHeap::mirror), so the heap cannot be freed
under it either.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-25T04:16:46.412170Z
We couldn’t complete this review. Comment @codex review to try again.
90ecea5 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants