Skip to content

TMEM allocation never relinquishes its permit — deadlocks the second wave of CTAs #46

Closed

Description

@feitreim

src/tmem.rs has no relinquish anywhere. alloc_block calls tcgen05_alloc
and dealloc_block calls tcgen05_dealloc, and neither issues
tcgen05_relinquish_alloc_permit, which cuda-oxide does expose
(generated/tcgen05.rs:2495, plus the _cg2 form at 2503).

A CTA that exits still holding its allocation permit leaves the SM's
allocator short, and the next CTA scheduled there waits in tcgen05.alloc for
columns that are never coming back.

This is not theoretical — the identical bug was just found and fixed in the GEMM example

examples/src/gemm.rs had the same omission (plus issuing the cta_group::2
allocator instructions from rank 0 alone). #40 found it by being the first
thing that ever launched a kernel twice in one process
. The first launch was
always fine; the second hung forever. Confirmed on a B200 twice, and adding the
relinquish alone was necessary but not sufficient there — the participation
had to be fixed too.

The library's cta_group::1 path has the relinquish half of that same defect.

Why every existing test misses it

Every device case and both example runs launch each kernel once, with a grid
that fits in one wave. A permit leaked at exit is invisible when nothing is
scheduled behind you. modal_app.py::examples cannot see this: one launch is a
strictly weaker claim than two.

So the bug is latent for exactly as long as nobody writes a persistent kernel or
a grid larger than the GPU — which is to say, until the first realistic
workload. #15's persistent scaffold and any benchmark loop both trip it.

What to do

  1. Issue tcgen05_relinquish_alloc_permit on the cta_group::1 path, from the
    warp that allocated, once it will not allocate again.
  2. Get the placement right relative to dealloc_block — relinquishing the
    permit and deallocating are different operations and the ordering
    matters. Read the intrinsic docs rather than inferring from the GEMM fix,
    whose _cg2 path has different participation rules ("one full warp in each
    peer CTA").
  3. Add a device case that launches a kernel twice, or with a grid exceeding
    one wave. Without it this class of bug stays invisible to the harness — and
    it is a whole class, not one instance: the same "fine once, broken twice"
    shape applies to any resource acquired at entry and released at exit.
  4. Lift alloc_cluster out of examples/src/gemm.rs into tmem.rs while here —
    it is now a correct, hardware-verified reference implementation of the _cg2
    path, and it is Cluster scope: the library is CTA-scoped, and the GEMM works around it four ways #24 item 5. Keeping a fixed version in an example while the
    library keeps the bug is the worst of both.

Related: #24 (cluster scope), #15 (persistent scaffold — the first consumer that
would trip this), #40 (found it).

Activity

  1. added
    bug
    on Jul 28, 2026
  2. feitreim commented on Jul 28, 2026

    @feitreim
    OwnerAuthor

    Correction: the premise in this issue was wrong

    I filed this predicting a deadlock — the next CTA on the SM stuck in tcgen05.alloc behind an unrelinquished permit. That does not happen on a B200 for cta_group::1, and the agent measured it rather than taking my word:

    1. Fix stashed, library rebuilt: 19/19, no hang.
    2. Grid raised to 4736 blocks (~32 waves, ~96 leaked permits per SM, each CTA taking all 512 columns), 3 launches: still 19/19.
    3. Controlled A/B in the same binary and same run — two kernels, one via alloc_block, one with a local copy minus the relinquish, each holding an allocation 5 ms across 296 blocks. If a held permit blocked a co-resident CTA's alloc, the leaked variant would serialize at ~2x. Result: relinquished 20.29/20.14 ms vs leaked 20.14/20.15 ms.
    4. Ruled out a silent no-op by grepping emitted PTX: alloc: 7 / relinquish: 7 / dealloc: 7.

    So what shipped is conformance — PTX requires the permit back before CTA exit and doesn't define what happens otherwise; the hardware evidently reclaims it anyway. It is worth having, but nobody should read this as a fixed deadlock.

    Where #40's hang actually came from: the other half of the same defect. gemm_cg2 issued all three cta_group::2 allocator instructions from rank 0 alone, so the pair's dealloc returned only half the columns — and leaked columns genuinely don't come back. That is also why adding relinquish alone would not have fixed the GEMM. The participation rule (one full warp in each peer CTA, each reading its own staging word) is now in tmem::alloc_cluster / dealloc_cluster.

    The device case is a guard, not a reproduction, and its doc says so: relaunch_probe (296 blocks x 3 launches, each CTA taking all 512 columns) cannot fail without the relinquish. It earns its place as the harness's only "fine once, broken twice" case — every other case launches once over a single wave, which by construction cannot see a resource acquired at CTA entry and leaked at exit. TMEM is just the first resource with that shape.

    Also added: finish_or_abort, a polled CUDA event with a 30 s deadline, so a launch that never returns is a reported FAIL instead of a container sitting until its timeout.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions