Skip to content

drm/asahi: g17: never hand out a context a queue record still names - #1870

Draft
DjDeveloperr wants to merge 2 commits into
mainfrom
g17/context-slot-fence
Draft

DjDeveloperr wants to merge 2 commits into
mainfrom
g17/context-slot-fence

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

A returned G17 application context is meant to wait until no installed queue record names it (mmu::ContextHold) and the firmware has consumed a control record published after the last such record went. The allocator kept one exception, from 34b438d: when every free context still waited, Uat::new_execution_context took the one with the oldest fence and counted an overrun, rather than refuse a queue.

That exception is reached as soon as contexts run short. An idle compute queue keeps its record, and with it its last owner's context, until the queue gets its next owner. With nearly every context in use, the only free contexts are those, and a new queue gets one of them.

What was seen

A18 Pro test boot, nine Vulkan clients creating and destroying devices, 54 to 56 of the 58 contexts in use:

  • The overrun count in debugfs capacity went from 0 to 51 in about 75 seconds.
  • The GPU page-faulted three times in that window. Each fault was in a context that had been handed out a few seconds earlier while an idle, ownerless compute queue's record still named it.
  • Each recovery cost the device of the client that had just been given the context, and failed the work other clients had in flight.

The link between each faulting context and the idle queue comes from capacity samples taken two seconds apart, so it rests on the last binding each queue showed.

Change

  • mmu.rs: the allocator takes a context only when it is ready: free, named by no record, fence met (ContextRoom, context_room). The oldest-fence fallback is gone for application contexts. pick_returned is unchanged for its other user, the QoS IDs.
  • g17/runtime.rs, Backend::new: the queue creator samples how far the firmware consumed the control ring and asks for a ready context before it reserves anything. With no free context it waits for closing queues, as before. With free contexts that all still wait it polls every 10 ms, because neither a replaced record nor firmware progress notifies, within the same bound. After that the queue is refused at the ExecutionContexts site (EBUSY to the client), as it already is when logical queues or render identities run out.
  • debugfs capacity: execution_contexts_fenced free_waiting=N waits=M replaces overruns=. waits counts queue creations that found only waiting contexts.
  • tools/asahi/g17-context-reuse-tests.py: compiles the production choice and the creator's wait rule on the host. Its mutations restore the reuse of a waiting context, ignore the records' holds, and drop the bound.

Second commit: the same rule on the compute queue's side

An installed compute queue keeps the scheduler page and work storage of an exited last owner, with the hold on its context. bind_owner dropped them as soon as the next owner of another VM bound the queue and the record was rewritten. But a queue's context, scheduler page and QoS ID also reach the firmware in the configuration a publication carries, sent when they differ from the last one published. Until the new owner's first publication has run, the last configuration the firmware ran for that queue is the exited owner's. That publication can be far away (it waits for ring room, work states and inputs, and a failed first attempt is rolled back with the queue left bound).

In that window the hold was gone, so one control record later the context counted as ready for another queue: the state the first commit refuses from the allocator's side. The scheduler page was unmapped and the work storage returned to the object cache in the same window.

  • queue/compute.rs: the pages are kept as Superseded from the rebind until a publication made after it is witnessed, by the witnesses that already retire Previous (exact retirement, the drain witness of a quarantined queue, an acknowledged kill).
  • debugfs capacity: retained= and superseded= on the compute_queue line.
  • tools/asahi/g17-capacity-fault-tests.py: the rebind and both witness paths on the production methods, with two more negative controls.

This one rests on reading the publication path, not on an observed fault. It only keeps things longer.

Cost, and what this does not do

  • At context exhaustion a new queue now waits up to 8 s and is then refused, where it used to be created.
  • Every idle compute queue without an owner pins one context until its next owner has run a publication, so with N such queues only 58 - N contexts can be open. Rewriting the idle queue's record would not free it: the firmware's configuration of the queue names the context too, and replacing that takes a publication on the queue. Not attempted here.
  • A fence needs one more control record after the last hold drops. Idle power transitions publish one; a GPU that stays busy with no control traffic for the whole bound ends in a refusal.
  • The code is shared with the M5, which gets the same wait-or-refuse at exhaustion.
  • QoS IDs keep the oldest-fence fallback: 126 IDs for at most 58 contexts and 18 retained records, so it is not reached. The object cache's own fallback is untouched.

Test plan

  • tools/asahi/g17-context-reuse-tests.py: 6 tests pass; the 3 mutations are killed.
  • tools/asahi/g17-capacity-fault-tests.py: passes; all 8 negative controls fail as they should.
  • tools/asahi/run-host-tests.py --only g17: 35 of 35.
  • Module build against the current Neo kit. Not compiled yet.
  • Neo (J700) test boot: the same multi-client load on the tip and on the tip plus these commits. Pass = waits above zero and no GPU recovery.
  • M5 Pro: a check of the wait-or-refuse at context exhaustion.

Draft until the module build, the Neo run and the M5 check are done. Not to be merged before they pass.

🤖 Generated with Claude Code

A returned application context waits until no installed queue record
names it (mmu::ContextHold) and the firmware has consumed a control
record published after the last such record went. The allocator kept one
exception: when every free context still waited, it took the one with
the oldest fence and counted an overrun rather than refuse a queue.

That exception is reached as soon as the contexts run short. An idle
compute queue keeps its record, and with it its last owner's context,
until the queue gets its next owner. With nearly every context in use
the only free ones are those, and a new queue got one of them. On the
A18 Pro, with nine Vulkan clients holding 54 of the 58 contexts, the
overrun count went from 0 to 51 in 75 seconds and the GPU faulted three
times. Each fault was in a context handed out within the previous six
seconds while an idle compute queue's record still named it, and each
took the device of the client that had just been given the context.

Take a context only when it is ready. The queue creator asks before it
reserves anything, after sampling how far the firmware consumed the
control ring. With no free context it waits for closing queues as
before. With free contexts that all still wait it polls every 10 ms,
since neither a replaced record nor firmware progress notifies, within
the same bound; then the queue is refused (ENOSPC at the
ExecutionContexts site, EBUSY to the client), as it already is when the
logical queues or their render identities run out.

debugfs capacity reports the creations that had to wait
(execution_contexts_fenced waits=) in place of the overruns.

tools/asahi/g17-context-reuse-tests.py compiles the production choice
and the creator's wait rule on the host; its mutations restore the
reuse of a waiting context, ignore the records' holds and drop the
bound.

Signed-off-by: Diljit Singh <[email protected]>
An installed compute queue keeps the scheduler page and work storage of
an exited last owner, and with them the hold on its application context
(retained_owner). bind_owner dropped them as soon as the next owner of
another VM bound the queue and the queue record was rewritten.

The record is not the only place that names the old owner. A queue's
context, scheduler page and QoS ID also reach the firmware in the
configuration a publication carries, sent when they differ from the last
one published. Until the new owner's first publication has run, the last
configuration the firmware ran for that queue is the exited owner's. That
publication can be far away: it waits for ring room, work states and the
submission's inputs, and a first attempt that fails is rolled back with
the queue left bound.

In that window the context's hold was gone, so one control record later
the context counted as ready for another queue, the same state the
previous commit refuses from the allocator's side. The scheduler page
was unmapped and the work storage went back to the object cache in the
same window.

Keep the pages as Superseded from the rebind until a publication made
after it is witnessed: an exact retirement, the drain witness of a
quarantined queue, or an acknowledged kill, the witnesses that already
retire Previous. An older configuration still waiting for its witness
keeps its place when the queue changes owner again without a
publication.

debugfs capacity gains retained= and superseded= on the compute_queue
line.

tools/asahi/g17-capacity-fault-tests.py runs the rebind and both witness
paths on the production methods, with negative controls for dropping the
pages at the rebind and for retiring them without a publication.

Signed-off-by: Diljit Singh <[email protected]>
m0d16l14n1 added a commit to m0d16l14n1/linux that referenced this pull request Nov 15, 2026
DjDeveloperr: drm/asahi: g17: never hand out a context a queue record still names

# Conflicts:
#	drivers/gpu/drm/asahi/g17/runtime.rs
@RyanTheTide

Copy link
Copy Markdown
Contributor

Blockers: 2.

  1. The branch no longer merges cleanly with integration/aurora (merge state DIRTY), and the description says the module has not been compiled. Please rebase and build it against the current tree. integration/aurora still has the overrun fallback this removes (drivers/gpu/drm/asahi/mmu.rs on integration, context_fence_overruns), so the change is still needed.
  2. drivers/gpu/drm/asahi/g17/runtime.rs:54: context_wait_ms() polls every 10 ms for the full bound (runtime.rs:36, 4 x the job timeout) whenever the room is Waiting. It does not tell apart a context still held by a record from one only waiting for its fence. effective_fences() turns a held context's fence into u64::MAX (mmu.rs:2015-2022). Such a context becomes ready only when its record is rewritten and then published. The idle ownerless compute queue in the reported case is exactly that, and with the second commit its hold lasts until the next owner's first publication. When every free context is held this way, the creator sleeps for the whole bound and is then refused anyway. That is an 8 s stall on device creation at context exhaustion, with nothing able to change in between. ContextRoom should report "held" separately from "fence pending". Poll only for a pending fence, and for held-only either refuse at once or reclaim an idle ownerless queue's record.

The core change is sound. new_execution_context() now takes a context only when context_room() reports it ready, and pick_returned() keeps its oldest-fence fallback for the QoS IDs only. Superseded is retired by the same witnesses as Previous, and its published flag is set only after publish_outer(), so a rolled-back first attempt does not retire it early.

HW row: J700 on this head with the nine-client device create/destroy load, against the same load on the base: 0 GPU recoveries, execution_contexts_fenced waits= > 0, refusals counted, the longest device-creation stall reported, and 0 AVC, 0 failed units, 0 new kernel errors. Plus the M5 Pro context-exhaustion check from the test plan.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown
Contributor

integration/aurora 110861f still has the oldest-fence overrun fallback in Uat::new_execution_context, so this is still needed. It does not apply cleanly to 110861f. In Backend::new the tip now has queues of one VM share an execution context (exec_share::join), and contexts take their render identity at creation. A rebase onto 110861f that takes a fresh context only on ContextRoom::Ready and keeps the shared-context path resolves it. On that tree the new g17-context-reuse host test passes, but the tip's g17-compute-owed-stamp-tests no longer compiles, because its harness does not know the new Superseded type. @DjDeveloperr, please rebase onto the current tip, update that harness, and check how the record holds interact with a context shared by several queues. The earlier point about the 10 ms poll when every free context is held by a record (instead of only fence-pending) is still open.

A rebased copy is on this repository as rebase/20261123/pr-1870 (110861f plus this PR's commits, authorship unchanged, conflicts resolved as described above). It is there to save you the rebase; take it or redo it as you prefer.

Build check of 110861f plus this change (arm64, LLVM=1, Image, dtbs and every directory it touches, Fedora config plus asahi.config): builds, no warnings in the files it touches. Host tests (tools/asahi/run-host-tests.py --only) on 110861f plus this change: g17 fails (80/94 passed); g17-capacity-fault-tests passes (1/1); g17-context-reuse-tests passes (4/4).

This branch has not been deployed

No deployments
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