Repository navigation
drm/asahi: g17: bound what a queue's pending commands keep allocated - #3602
jgoyvaerts wants to merge 10 commits into
Conversation
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]>
19fc706 to
445e8c7
Compare
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]>
|
Hardware results on a backport, not the exact heads. J616s M4 Pro, test kernel 7.3.0-m4.205: m4/private-205 =
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 (
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 |
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]>
|
Results on a backport, not the exact heads. J616s M4 Pro, test kernel 7.3.0-m4.211 (m4/private-211 =
On 4b:
What changed because of 1c:
|
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]>
|
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
|
…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]>
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)
Signed-off-by: DjDeveloperr <[email protected]>
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]>
|
J700 (Neo, G17P) boot row at head Kernel: one trial boot (boot
Results
Not covered:
|
DjDeveloperr
left a comment
There was a problem hiding this comment.
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.
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 jobsched_jobsback. 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::inputsandPacket::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 aChargethat gives the units back when it drops:128 + inputs + firmware + dependencies + timestamp waitsand drops it in drm_schedfree_job;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.rssubmit,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 beforeOutputs::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 insidedrmIoctlfor 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_limitsdocumentation 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: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.