Skip to content

drm/asahi: g17: bound what a queue's pending commands keep allocated - #3602

Draft
jgoyvaerts wants to merge 10 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-pending-cap
Draft

jgoyvaerts wants to merge 10 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-pending-cap

Conversation

@jgoyvaerts

@jgoyvaerts jgoyvaerts commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

What: drm_sched credits bound only the commands on the firmware rings. Behind them a G17 queue could keep any number of commands pending behind a stalled job or a foreign fence, and their packets, fence vectors and publication links grow with the square of the queue's depth: bounded only under a memcg memory.max. Each queue now has a budget: 128 units per pending command plus one per fence reference or link it holds, 2^18 units, 262,144 (about 8 MB). A submission to a full queue sleeps, interruptibly and before anything is published, until the queue's earlier work gives enough back, as amdgpu's CS ioctl waits for the job sched_jobs back. Main's submission order window (2048 ordinals, work-state lease splits capped at half of it) is already enough for the budget, so a full queue always sleeps before the window could refuse it with EBUSY: every ordinal waiting ahead of the turn is the lease of at least one pending command, each holding at least 128 units, so an admitted submission finds fewer than 2048 of them. A compile-time assert and a test keep it so.
Tested: QEMU arm64 KUnit (asahi kunitconfig) at 62886a7 (main dbfbced merged in): Ran 953 tests: passed: 952, skipped: 1. Each new test fails on the code before its change (the room test on the first commit: Ran 937, 1 failed; the window test with the window at 1024 and the assert removed: Ran 953, 1 failed). tools/asahi host tests at 62886a7: the same failure set as on main (apple-dcp, g15-pb-growth, g15-stats-drain, sep-suspend-errors, test-m3-uapi: environment, none in the files this touches). J616s M4 Pro, backport of the first two commits on test kernel 7.3.0-m4.205 (m4/private-205 = 89a8b725782f, not this head): Cyberpunk 2077 benchmark peak 549 pending commands per queue, 0 submit errors, 0 lost devices; Hearthstone + desktop budget peak 2.6%; a deep compute stream hit the old order window (EBUSY) and the following teardown crashed the GPU firmware, see the comment and the Risk line.
Risk: two-way door · M4 and M5 (the G17 queue and the shared submit ioctl; other GPUs' queues have no budget and never wait). The submit ioctl may now sleep; no uAPI flag, and existing Borealis and Gallium need no change. A queue that legitimately keeps more than the budget pending gets a slower submit instead of more depth. The bound is per queue and multiplies by the number of queues: with render-pair lending (up to about 126 queues) that is about 126 × 8 MB ≈ 1 GB if every queue were full. Cyberpunk's busiest queue filled it on m4.211: 311,000 of the 262,144 units (2.4 MB if every unit were an 8-byte fence reference, 10 MB if every one were a 32-byte link). With the publication links cut at their source (#3681, backport on m4.213) it peaked at 84.8%; #3681's second commit removes most of the rest (the producer fences). A refusal found on hardware is what the window check is for: on m4.205, before main's 2048 window, a client with 1024 one-command submissions pending got EBUSY (a lost device), and the client's teardown then faulted the GPU and crashed its firmware. With the budget below the window that refusal cannot happen; the teardown crash is a separate, known kernel bug, not fixed here.
Not yet: a hardware run at this head; the reference cuts Cyberpunk needs to stay well below the budget are in #3681 (links merged into test kernels, the producer-fence failure log on its way); what the throttling costs (a capped-vs-uncapped Cyberpunk A/B on one test kernel, queued); and a deep-stream row past the budget, hang-risk until the separate teardown crash is fixed. Not run on M5.

Details

What a pending command keeps. Its Packet (about 1.3 KB of validated command), Packet::inputs and Packet::firmware (one fence per host-waited or firmware-waited producer), its scheduler job's dependencies until the job runs, the timestamp waits its frontier entries inherit, and one publication link per covered pending publication. A consumer covers every pending producer its prefixes reach, so these grow as m consumers × n producers. Counting commands alone leaves that quadratic term (about 24·N² bytes per queue); counting only the frontier misses commands that finished but whose publication still waits behind a stalled predecessor.

The budget (g17/queue/budget.rs). Budget::charge(units) returns a Charge that gives the units back when it drops:

  • the Packet holds 128 + inputs + firmware + dependencies + timestamp waits and drops it in drm_sched free_job;
  • the publication holds one per link and gives them back once every link has been delivered, each link freed by the drain. Not when the publication itself drops: the queue's own frontier keeps it until the next submission, which may be the one waiting.

Every release runs in process context (free_job and event work, ioctls, queue teardown). The budget lock is taken only when a release crosses below the limit, and it orders that wake-up after a waiter's check.

The numbers. 2048 commands' worth is a policy constant: about 2.3 times the deepest queue measured, Cyberpunk 2077 at 874 pending commands on one queue. That is 32 times an M4 Pro queue's hardware credits (32 render and 32 compute); amdgpu's default software queue is 16 times its hardware depth, xe's is 1000 jobs. 128 units is a command's own state (about 2.5 KB) in 32-byte link units, rounded up. A dependency-free queue gets exactly 2048 commands; one whose commands carry hundreds of references throttles earlier, with memory bounded either way. A submission is admitted whole once the queue is below its budget, so a queue ends at most one submission past it, sized by that submission's own arguments.

The wait (file.rs submit, Queue::room()). Under the queue lock the queue reports whether it is full. If it is, the ioctl drops the lock, sleeps on the budget (wait_interruptible, ERESTARTSYS on a signal), takes the lock again and checks again, so concurrent submitters on one queue keep the bound. It sleeps before Outputs::new(), so nothing is installed in the output syncobjs and a restarted ioctl starts clean (as G16's and G18's submit waits do).

Why it cannot wait on itself. Every input is a fence that exists at submission (an unmaterialized timeline point is refused), and Mesa keeps wait-before-signal in its own submit thread. The commands holding the budget depend only on accepted work and existing fences, which the watchdog (compute allowance up to compute_timeout_max_ms, render renewals) and recovery end in finite time. A failed or killed VM's commands are cancelled and freed like any others, and the next submission then gets EIO. The sleeping ioctl holds no queue, device or VM lock and no preparation lease, so recovery never waits for it.

Userspace. Borealis and the Gallium driver submit through libdrm's drmIoctl, which restarts an interrupted ioctl; a submission just takes longer, as on amdgpu. A refusal was rejected: EAGAIN turns into a busy loop inside drmIoctl for the whole stall (how xe's job limit behaves), and any other errno makes Borealis report a lost device, so it would need a uAPI flag and new Mesa.

The drm_asahi_queue_limits documentation says that SUBMIT may sleep.

Evidence

Peak pending commands per queue, from a passive tracefs capture of asahi_g17_submit/schedule/complete/output (commands from acceptance until their submission's out-fence signals), J616s M4 Pro, 7.3.0-m4.202, before this change:

workload queue peak scheduler deps per command p50 / p99 / max peak live deps on one queue
Cyberpunk 2077 built-in benchmark, 1920x1200 Low, 1 cold + 3 warm (766 s, 2.29M commands) 874 2 / 598 / 794 24,131
Hearthstone + desktop, 600 s (725k commands) 339 1 / 16 / 128 405
desktop clients (Slack, T3, Chromium, Hyprland) ≤ 68 ≤ 1 –

Counting scheduler dependencies (not yet the firmware vectors and links), Cyberpunk's deepest queue would use about 137k of the 262k units (880 × 128 + 24k); the hardware row measures the real figure.

KUnit (QEMU arm64, asahi kunitconfig)
red (first commit + the room test): Ran 937 tests: passed: 935, failed: 1, skipped: 1
  asahi_g17_cross_engine_timestamps.a_queue_has_room_until_its_pending_commands_hold_the_budget
green at 62886a7f9ddf: Ran 953 tests: passed: 952, skipped: 1
red (the window test, window at 1024, assert removed): Ran 953 tests: passed: 951, failed: 1, skipped: 1
  asahi_g17_work_states.an_admitted_submission_always_finds_room_in_the_window
new cases: asahi_g17_queue_budget.{charges_fill_the_budget_until_they_drop,
  a_waiter_wakes_once_a_charge_drops_elsewhere},
  asahi_g17_publication_order.link_units_come_back_once_the_links_are_delivered,
  asahi_g17_cross_engine_timestamps.a_queue_has_room_until_its_pending_commands_hold_the_budget

drm_sched credits bound only the commands on the firmware rings. Behind
them a userspace queue can keep any number of commands pending behind a
stalled job or a foreign fence: a compute job may run for 30 s, a chain
of slow jobs longer. Each keeps its Packet (about 1.3 KB of validated
command), its fence vectors and its scheduler dependencies, and its
publication keeps one link per covered pending publication. Several of
those grow with the number of other pending commands a command covers,
so the total grows with the square of the queue's depth. Charging the
allocations to the memcg bounds it only under a memory.max.

Give each G17 queue a budget of units (queue::budget): 128 per command
for its own state, plus one per fence reference, scheduler dependency,
inherited timestamp wait and publication link it holds. The Packet
holds the charge for its own state and vectors and gives it back when it
is freed (drm_sched free_job). The publication holds the one for its
links and gives it back once every link has been delivered, each freed
by the drain: not when the publication itself goes, since its own
queue's frontier keeps it until the next submission, which may be the
one waiting for those units. Every release runs in process context
(free_job and event work, ioctls, queue teardown), and the budget's
lock is taken only when a release takes it below the limit.

The budget is 2048 commands' worth, a policy constant: about 2.3 times
the deepest queue measured. On an M4 Pro with m4.202, Cyberpunk 2077
keeps up to 874 commands pending on one queue, with up to about 24,000
scheduler dependencies among them, and a Hearthstone session up to 339.
2048 is 32 times the queue's hardware credits there (32 render and 32
compute); amdgpu's default software queue is 16 times its hardware
depth. A command's own state counts 128 units (about 2.5 KB, 80
publication links' worth, rounded up), so the whole budget is about
8 MB of references.

Add Queue::room() and the wait in the submit ioctl: a submission to a
full queue sleeps, interruptibly and without the queue's lock, until
the queue has room, and checks again under the lock. It sleeps before
anything is published, so a signal restarts the ioctl with nothing
installed in its output syncobjs. Nothing reports the G17 queue full
yet; the next commit does.

Signed-off-by: Jonathan Goyvaerts <[email protected]>
A G17 queue now reports itself full once its pending commands hold its
whole budget, and the submit ioctl sleeps until they have given enough
of it back. As amdgpu's CS ioctl waits for the job sched_jobs
submissions back, the wait is interruptible and comes before anything is
published.

It cannot wait on itself. Every input is a fence that exists when the
submission is made (an unmaterialized timeline point is refused), and
userspace keeps wait-before-signal in its own submit thread, so the
commands holding the budget depend only on accepted work and existing
fences, which the watchdog and recovery end in finite time. A failed or
killed VM's commands are cancelled and freed like any others, and the
next submission is then refused with EIO. The sleep holds no queue,
device or VM lock and no preparation lease, so nothing that frees the
budget waits for it.

Userspace needs no change: Borealis and the Gallium driver see a
submission that takes longer, as on amdgpu, and libdrm restarts an
interrupted one.

Signed-off-by: Jonathan Goyvaerts <[email protected]>
A context refuses a new submission with EBUSY once ORDER_WINDOW (1024)
of its submissions wait behind the oldest one still waiting for its
work-state node (WorkStateLease::deferred()). Vulkan sees a lost device.
The queue budget is meant to make a submitter sleep instead, but it
counts commands: a client submitting one dependency-free command per
ioctl can queue 2048 behind a stalled job, so the window refused it
first. On an M4 Pro (7.3.0-m4.205 with the budget backported) a compute
stream of 0.5 ms kicks at depth 4096 got that EBUSY while its queue held
about half its budget.

Make the window 4096 submissions, 512 bytes of bits per context, and
assert that it stays above the most submissions the budget lets wait:
each holds at least one command's units, and one submission of up to
MAX_COMMANDS_PER_SUBMISSION is admitted below the budget, so at most
2048 + 64. A full queue then always makes its submitter sleep before
the window could refuse it. Order's bitmap is indexed modulo the
window and walked by Order::pass(), which needs no other change.

Signed-off-by: Jonathan Goyvaerts <[email protected]>
@jgoyvaerts

Copy link
Copy Markdown
Contributor Author

Hardware results on a backport, not the exact heads. J616s M4 Pro, test kernel 7.3.0-m4.205: m4/private-205 = 89a8b725782f, i.e. #3600 + #3601 + the first two commits of #3602 cherry-picked onto aurora main a851ca0 with the test-kernel set. #3602's order-window commit (1a9a915) was written after these runs and is on no test kernel yet. Each row ran alone on the GPU.

row what ran result
1 Cyberpunk 2077 built-in benchmark, 1920x1200 Low, cold + 7 warm and cold + 2 warm, headless gamescope, read-only tracefs capture of asahi_g17_* submit/schedule/complete/output pass: 24.6-25.9 fps (20.0-21.0 on m4.202, before these changes); busiest queue 549 pending commands until their out-fence (874 on m4.202), so the budget never filled; 0 submit errors, 0 lost devices, 0 GPU recoveries
1b the same benchmark, cold + 1 warm, same capture 0 recoveries, peak 529 per queue, 17.9 / 20.8 fps; the budget probes saw nothing (the charges are inlined), so the exact budget use is still to measure
2 Hearthstone (already running) + desktop, 600 s, with probes on the inlined budget charges pass: busiest queue used 6,912 budget units (2.6% of 262,144)
3 gpubench-vk compute.light at depths 64-16384, then compute.heavy (kind=alu, ~0.5 ms kicks) at depths 64-16384 compute.light pass at every depth, 19 µs per submit at 64 and at 16384. compute.heavy pass at 64 and 1024. At 4096 it failed, and the run crashed the GPU firmware; see below

Row 3, what happened. At depth 4096 one submission failed with EBUSY. It was not from the budget, which only ever sleeps and held about half its units at the time (132k of 262k). It came from the context's submission order window (ORDER_WINDOW = 1024 in g17/context/identity.rs, on main since c510e9d), which refuses a new submission once 1024 are waiting behind the oldest. The budget counts commands, so a client submitting one command per ioctl reached the window first. Borealis reported a lost device, and gpubench went on to its next depth and then exited with work still queued. 0.8 s after the EBUSY:

  • the GPU faulted on an unmapped read at 0x10100 from that client's queue (recovery gen 493, then gen 494);
  • the GPU firmware crashed in the kill that followed (PC 0x18d5c, FAR 0x8, the same signature seen once on m4.202);
  • the desktop compositor lost its GPU, and the machine was rebooted.

The window refusal is fixed in #3602 by its third commit: the window is 4096, asserted above the most submissions the budget lets wait (2048 + 64), so a full queue makes the submitter sleep first. The fault-then-firmware-crash after a lost device is a separate kernel bug, already being looked at elsewhere. Until it is fixed, row 3 at depth ≥ 4096 counts as hang-risk.

Still to run: the Cyberpunk benchmark with probes on the inlined charges (the real budget use), and the dEQP-VK synchronization and synchronization2 slices against the stored baseline, both on the next test kernel.

Cyberpunk 2077 filled the queue budget. On an M4 Pro (7.3.0-m4.211 with
the budget backported) its two busiest queues peaked at 311,000 and
309,000 of the 262,144 units, read from the inlined charges with
kprobes, so their submitters slept. They held 555 and 522 pending
commands: 71,000 units of command state, and about 240,000 units of
references, 430 per command (publication links, firmware producer
fences and scheduler dependencies).

Make the budget 2^20 units, about 32 MB per queue if it were all links,
three times that peak. A queue whose commands wait on nothing may now
keep 8192 pending, so up to 8192 + 64 submissions wait, past the
submission order window of 4096. Make the window 16384 (2 KB of bits
per context) to keep the assert that a full queue makes its submitter
sleep before the window could refuse it.

Signed-off-by: Jonathan Goyvaerts <[email protected]>
@jgoyvaerts

Copy link
Copy Markdown
Contributor Author

Results on a backport, not the exact heads. J616s M4 Pro, test kernel 7.3.0-m4.211 (m4/private-211 = f5b29074bf46): #3600, #3601 and #3602's first three commits, including the 4096-submission order window, on aurora main a851ca0 with the test-kernel set. The queue budget was still 262,144 units there. #3602's head is now 5f940654b4e6, which raises it to 2^20 because of row 1c. Each row ran alone on the GPU.

row what ran result
1c Cyberpunk 2077 built-in benchmark, 1920x1200 Low, cold + warm, headless gamescope; kprobes on the budget's two inlined charges in Queue::submit (the new used after each charge), plus the asahi_g17_* tracepoints 23.6 / 25.9 fps, 0 GPU recoveries, 0 client failures. The two busiest queues peaked at 310,982 and 309,291 units, 118% of the 262,144-unit budget, so their submitters slept on it. They held 555 and 522 pending commands: about 71,000 units of command state and about 240,000 of references (publication links, firmware producer fences, scheduler dependencies), about 430 per command
4a dEQP-VK synchronization.* and synchronization2.* (6,213 cases), dq-fast against a stored baseline pass: 6,210 Pass, 3 NotSupported, 0 recoveries. All 431 differences from the baseline are NotSupported → Pass, from a newer Mesa (r11cba886 against the baseline's c356a149)
4b the timeline-semaphore case list (5,753 cases) did not complete. 24 deqp-vk clients lost their device with ETIMEDOUT, all in timeline_semaphore.one_to_n (sync and sync2), with 0 recoveries

On 4b:

  • The ETIMEDOUT came from the runtime side (fail_vm after a publication or pool-release stall), not from the submit ioctl. The budget never returns an error; it only sleeps.
  • No submission failed, and it happened alongside the test kernel's kill/free-list release work. It is being looked at with that work as a suspected kill-gate issue, not the budget or the input changes.

What changed because of 1c:

  • drm/asahi: g17: bound what a queue's pending commands keep allocated #3602 now gives a queue 2^20 units, about 32 MB of references, so Cyberpunk's peak uses about 30% of it.
  • The submission order window grows to 16,384 so the budget still fills before the window could refuse anything.
  • Still to come: a capped-vs-uncapped Cyberpunk A/B on one kernel (budgets 1×, 4× and none) to measure what the throttling costs, and a look at why a Cyberpunk command holds about 430 references.

This reverts commit 5f94065.

The 4x budget was a stopgap for Cyberpunk filling the queue. The way
forward is to cut the references a command holds at their source,
which the budget then needs no extra room for, so the budget goes back
to 2^18 units and the order window to 4096.

Signed-off-by: Jonathan Goyvaerts <[email protected]>
@jgoyvaerts

Copy link
Copy Markdown
Contributor Author

Hardware result on a backport, not the exact head. J616s M4 Pro, test kernel 7.3.0-m4.213 (m4/private-213, on m4/private-212): #3681's link cut (cherry-pick of 087a904), #3600, #3601 and #3602's first two commits, with main's 2048-submission order window. #3602's budget was 262,144 units per queue.

Cyberpunk 2077 built-in benchmark, 1920x1200 Low, cold + warm, headless gamescope, alone on the GPU. Kprobes on the budget's two inlined charges read the new used after each one, beside the asahi_g17_* tracepoints. 0 trace overruns.

m4.211 (before the link cut) m4.213 (with it)
fps cold / warm 23.6 / 25.9 33.3 / 35.4
links per command, mean (p50 / p99) 302-319 (358 / 415) 1.4 (1 / 3)
producer fences + host waits per command, mean 167-176 183-189
scheduler dependencies per command, mean 5.7-6.0 2.1
busiest queue's budget peak 310,982 units (118.6%: submitter slept) 222,180 units (84.8%)
second queue 309,291 (118.0%) 208,241 (79.4%)
peak pending commands per queue 555 607
submit failures, GPU recoveries 0, 0 0, 0
  • The link cut does what the proof says. A command now holds 1.4 links on average, against ~310. Most of the fps difference against m4.211 comes from other changes in the test kernel (memory limits, queue lending, storm fixes) and is not attributed to this.
  • The budget no longer fills, but sits at ~85%. What remains is the producer fences, now 98% of the references (about 190 per command): one completed fence per covered unfinished producer of the other engine, kept for error propagation.
  • Replacing those with a per-engine failure log is written up but not implemented. The owner decides it from this number.

jgoyvaerts and others added 5 commits November 30, 2026 13:53
…eue budget"

This reverts commit 1a9a915.

Main now has ORDER_WINDOW 2048 with work-state lease splits capped at
half of it, and that window is already enough for the queue budget: a
following commit asserts and tests it. The larger window this commit
set is not needed.

Signed-off-by: Jonathan Goyvaerts <[email protected]>
…w refuses

A context refuses a new submission with EBUSY, a lost device to Vulkan,
once ORDER_WINDOW ordinals wait behind its oldest one. The queue budget
must make a submitter sleep before that can happen. Every ordinal ahead
of the turn that has not left is the lease of at least one pending
command (a split is taken only for a following command), a submission
is admitted only while its queue holds less than its budget, and each
pending command holds at least COMMAND_UNITS of it. So an admitted
submission finds fewer than UNITS / COMMAND_UNITS such ordinals, and
the window of 2048 is enough for the budget of 2048 commands' worth.

Assert that, and test it with multi-command submissions that split as
often as the split cap allows, mixed with single-command ones, behind
a stalled oldest ordinal. With a window of 1024 the test is refused.

Ordinals of submissions that failed before their turn still count until
the turn passes them; the budget does not bound those.

Signed-off-by: Jonathan Goyvaerts <[email protected]>
(cherry picked from commit e0a2577ef83e7961b3291f1f25527b08488b0307)
The host fixture extracts VmStatus::record from the driver. Provide its wake hook in the fixture, where no thread is waiting on a queue budget, so the control and mutation cases exercise their input-error assertions again.

Signed-off-by: DjDeveloperr <[email protected]>
@DjDeveloperr

Copy link
Copy Markdown
Contributor

J700 (Neo, G17P) boot row at head 22e8797d

Kernel: one trial boot (boot 8e4e4589). Stock Fedora -41 was restored and verified afterwards.

  • Image: main dbfbced7 + drm/asahi: g17: keep G17P render queue records firmware-cached #3705 (7.3.0-neo-fence-kclean, Image sha256 a2c0a040).
  • aurora.ko: built at 05ef58ba, which has the same driver and kernel sources as this head; this head only changes a test script (build ID 1a611d9e, installed sha256 a9bb4113).
  • Mesa is the fixed packet ICD.

Results

  • Ordinary mixed render/compute with checked output, all completed:
    • q4 render triangle 42.4 µs, per-queue CRC ok
    • compute.light d1 105.5 µs and d16 15.0 µs, per-slot CRC ok
    • concurrent render + compute; 4-queue and 4-process compute; 4-queue render
    • cross-queue timeline and binary semaphores; host signal
    • queue churn (create/use/close/reuse, CRC ok)
  • Independent queues kept making progress: the multi-queue CTS gave sync2 16,680 Pass / 7,080 NotSupported / 0 Fail and sync 12,798 Pass / 9 NotSupported / 0 Fail, with 0 recoveries. Identical case for case to the other boots.
  • Two clients in separate cgroups, run, closed and then reused: kernel_resident 308 → 447 MB while running, back to 292 MB six seconds after close. user_resident went back to its starting value, so the charges were returned.
  • Health: 0 WARN, 0 GPU faults, 0 AVC, 0 failed units. The only flagged line is the out-of-tree taint from building the module with M=.

Not covered:

  • maximum queue charge and budget waits: no submitter got near the 2,048-command / 262,144-unit window, and the run did not trace the charge;
  • interrupted waits;
  • queue or VM teardown while a waiter is blocked.

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 22e8797. The order model kept the oldest ticket live through more than a full window of younger cancellations. Queue and VM close wake a budget waiter while its external charge is still held. The follow-up repairs the G15 fixture's wake stub. Its control, mutations and historical reference now pass. The mixed-queue and teardown hardware row is still open.

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