Skip to content

drm/asahi: g17: cut allocations and copies in the submit path - #2228

Open
jgoyvaerts wants to merge 3 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-submit-path-allocs
Open

jgoyvaerts wants to merge 3 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-submit-path-allocs

Conversation

@jgoyvaerts

@jgoyvaerts jgoyvaerts commented Nov 19, 2026 •

Copy link
Copy Markdown

What: Three changes that remove per-submit allocations and copies on the G17 submit path: reuse each queue's publication batch, keep the submit ioctl's command vectors in the queue (as KVVecs charged to the caller), and build each packet in its allocation. They don't change what is submitted or where it runs; per submit, allocations drop from 12 to 9.3 on the J616s.

Tested: head 9c73f8ef6beb has not been booted. J616s M4 Pro (T6040, 25G83) with the previous head 1c3306581218 (all three commits patch-id equal; commit 2 was still the KVec/GFP_KERNEL version) in our combined kernel 7.3.0-m4.179, default command line, boot 2026-11-22 23:51 to 2026-11-23 01:43 UTC: 0 GPU recoveries, 0 kernel lines at err level, 0 failed units, no SELinux/AppArmor loaded (0 avc: lines); the 7 synchronization.timeline_semaphore cases that hit order-3 OOM kills on m4.177, 3 rounds, 21/21 Pass, 0 invoked oom-killer (that kernel has compaction on, so this says nothing about the KVVec change). At this head: KUnit 697/698 (1 skipped), host tests --only g17 90/90, checkpatch 0 errors, CI host-tests green.

Risk: two-way door · the G17 Macs: M4 (incl. Pro/Max), M5 and Neo (A18 Pro). A kept command vector stays at its largest size until the queue closes (at most 64 commands, about 96 KB per queue, vmalloc-backed and charged to the client's memory cgroup). Checked unaffected: only the J616s.

Not yet: any boot of head 9c73f8ef6beb (commit 2's KVVec + GFP_KERNEL_ACCOUNT version); a clean standalone integration/aurora + this PR J616s run; one more G17 Mac (M4 or M5) with gpubench and a deqp-vk subset. The submit ioctl CPU time change is not measurable across boots (see Evidence).

Details
  • Reuse each queue's publication batch. Every publication allocated a DeferredBatch with room for two rings of completions: 64 entries, about 3.5 KB through kvmalloc. It freed the batch when the publication ended, so the direct compute and render paths did a kvmalloc and a kvfree in every submit ioctl.
    • The backend now keeps the batch of its last finished publication and lends it to the next one.
    • A batch that still holds work is dropped as before.
    • Concurrent publishers of one queue each get their own batch, and the first one back is kept. A new batch is allocated without the spare's lock held.
  • Keep the submit ioctl's command vectors in the queue. prepare_submission collected the validated commands and their VM pins in two vectors allocated and freed by every ioctl. The queue now keeps both, empty between ioctls. Whatever an error leaves unsubmitted is released at the same point as before.
    • Both are KVVecs charged to the caller (GFP_KERNEL_ACCOUNT, amended 11-22). The trigger was an order-3 GFP_KERNEL OOM kill under deqp-vk, from this vector's krealloc() in prepare_submission. 64 commands of about 1.5 KB need an order-3 block at 16K pages, and with compaction off in our kernel that allocation went to the OOM killer five times in one run, with 26 GB available. A large vector now falls back to vmalloc. It is freed with the queue, which may sleep. drm/asahi: g17: keep userspace-sized submit vectors off contiguous pages #2858 makes the same change for the code as it is on integration/aurora (the per-ioctl vectors there), plus the dependency and merge vectors this PR doesn't touch.
  • Build each packet in its allocation. Packet::new built the packet, with its 1.3 KB validated command, on the stack and then moved it into the Arc. The compiler kept three stack copies of the command for that. The packet is now allocated first and initialised in place, so the command is copied once. (Arc::init(init!(..)) and Arc::try_init(try_init!(..)) were tried: both compile but grow Packet::new's frame from sub sp, #0x4f0 to #0xd20 and its memcpy calls from 4 to 7.)

Known limit: a kept vector stays at its largest size until the queue closes: at most 64 commands, about 96 KB per queue. It needs no contiguous pages and is charged to the client's memory cgroup.

Evidence

J616s (M4 Pro, 48 GiB, 25G83), daily kernels since 11-18

Before and after are separate boots, compared with gpubench and kmem tracing, with 0 GPU recoveries. The one result that holds across boots is the allocation count.

Allocations (kmem trace, compute.light pinned at depth 1, 139 submits). Per submit, 12 before (11 kmalloc + 1 kmem_cache_alloc) and 9.3 after. The per-ioctl temporaries are gone: the DeferredBatch publication (4096 B), and prepare_submission's pending vector (2048 B) plus its VM-guard vector (192 B). What is left is objects that outlive the ioctl, plus a drm_exec in 2% of submits.

Submit ioctl CPU time, compute.light, in us at depth 1 / 8 / 64. Only rows with the same P-cluster frequency floor compare:

Run Before After
Hardware-minimum floors (1.26 GHz), unpinned 11.0 / - / 4.92 16.29 / - / 6.00
P floors 4.5 GHz, unpinned 9.50 / - / - 10.17 / 8.58 / 4.67
P floors 4.5 GHz, pinned to cpu5 - 9.75 / 7.54 / 4.25
P floors 2.35 GHz, pinned to cpu5 8.75 / 7.58 / 7.71 13.31 / 8.04 / 4.58
P floors 2.35 GHz, pinned to cpu11 10.38 / 8.21 / 6.62 13.93 / 9.12 / 6.81

So the change in submit CPU time is not measurable across boots: depth 64 is lower on cpu5, level on cpu11 and higher unpinned. The differences follow each boot's cluster load, not this code. The same floor gave 8.75 and 13.31 us at depth 1 on the two boots. Depth 1 is set by core type, cluster and frequency, which #2217 and #2222 address. What these commits measurably do is remove three per-submit allocations.

Not an A/B for these commits:

Regression row, combined kernel 7.3.0-m4.179 (previous head 1c3306581218)

integration/aurora bf423bf plus our other open G17 PRs, not a standalone kernel. Boot 2026-11-22 23:51 UTC to 2026-11-23 01:43 UTC (desktop, other GPU tests, our dEQP-VK and GLES runs), default cmdline: 0 kernel lines at err level (same as m4.178), 0 failed units, 0 GPU recoveries. LSMs capability,landlock,yama, no SELinux/AppArmor loaded, 0 avc: lines. The window ends where a deliberate GPU hang test for another PR started.

The 7 synchronization.timeline_semaphore.{one_to_n,wait_before_signal}.write_blit_image_read_{blit,copy}_image.* cases that hit order-3 OOM kills from prepare_submission on m4.177: 3 rounds at -j 2, 21/21 Pass, 0 invoked oom-killer. This kernel has compaction enabled (our m4.177 config had it off), so the row says nothing about the KVVec change in 9c73f8ef6beb.

On this branch

KUnit: 698 tests, 697 pass, 1 skip (needs CONFIG_UCLAMP_TASK), including the new a_publication_batch_comes_back_only_when_idle. tools/asahi/run-host-tests.py --only g17 90/90. checkpatch 0 errors on all three commits. git diff 1c3306581218 9c73f8ef6beb touches only Scratch's declaration, its doc comment and the three reserve/push calls.

Every publication allocated a DeferredBatch with room for two rings of
completions (64 entries, about 3.5 KB through kvmalloc) and freed it when
the publication ended: on the direct compute and render paths this was a
kvmalloc and a kvfree in every submit ioctl.

The backend now keeps the batch of its last publication once it is
finished and lends it to the next one. A batch that still holds work is
dropped as before, and concurrent publishers of one queue (the two
engines' scheduler workers and the ioctl) each get their own: the first
one back is kept. The batches are created and used exactly as before.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
@jgoyvaerts
jgoyvaerts force-pushed the m4pro/g17-submit-path-allocs branch from 954fc9b to 1c33065 Compare November 21, 2026 06:14
@jgoyvaerts

Copy link
Copy Markdown
Author

Commit messages only, for the pre-PR checklist: the commits no longer carry Assisted-by (item 4), and the disclosure is now the PR body's last line. The tree is identical to 954fc9be6f (git diff 954fc9be6f 1c33065812 is empty). New head 1c33065812.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown

Looks good; approve once the HW row below is in.

I checked the three changes. PublicationBatch (drivers/gpu/drm/asahi/g17/runtime.rs:917-925, 1586-1597) takes the spare before allocating, so nothing is allocated under the mutex. It returns a batch only when is_idle() holds, after finish() has drained every vector and cleared has_source_completions, and it is dropped at the end of each publisher with no other lock held. No path replaces a lent batch's vectors, so the room checks (render/tvb/vacated) behave as before. In queue/mod.rs the scratch vectors are taken out of the queue for the ioctl and cleared before finished.clear(), the same point the old vectors were dropped, and the VM pins now outlive the leftover commands on the error path. Packet::new keeps its error behaviour.

  1. Nit: drivers/gpu/drm/asahi/g17/job.rs:154-169 can be a single Arc::init(init!(Self { ... }), GFP_KERNEL), like the Arc::pin_init users elsewhere in g17. That drops the UniqueArc round trip and the irrefutable let Ok(..).

HW row: one more G17 Mac other than the J616s (M4 or M5) on a kernel with only these commits on top of integration/aurora: gpubench compute/render rows plus a deqp-vk subset with no new failures and 0 GPU recoveries, and a regression row on the J616s (0 SELinux denials, 0 failed units, 0 new kernel errors).

Assisted-by: LLM (Claude:Opus 5.5)

@jgoyvaerts

Copy link
Copy Markdown
Author

I tried the nit and left the code as it is; the head stays 1c33065812.

With the arm64 defconfig + asahi.config build (LLVM=1), both Arc::init(init!(Self { ... }), GFP_KERNEL) and Arc::try_init(try_init!(...), GFP_KERNEL) compile cleanly, but they make Packet::new worse. The stack frame grows from sub sp, #0x4f0 to #0xd20, and memcpy calls go from 4 to 7. The InPlaceInit path moves the initializer, together with the ~1.3 KB command it captured, through extra closures by value. That brings back the copies this commit removes, so job.rs:154-169 keeps UniqueArc::new_uninit + init_with.

The J616s regression row will follow the next kernel build of our tree. We'd take your offer to run the other G17 Mac row (M4 or M5) in your lab.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Adversarial review at 1c3306581218 against the pre-PR checklist.

No code, CI, attribution, or policy blockers found:

  • CI & Attribution/Policy (§1–§4, §6): host-tests is green on 1c3306581218; all three commits carry human author/committer and human Signed-off-by: only; PR body disclosure is clean; no model gating or naming issues.
  • PublicationBatch reuse (drivers/gpu/drm/asahi/g17/runtime.rs:917-925, 1429-1436, 1571-1597): spare_batch is a leaf mutex never held across allocation (DeferredBatch::publication()?), kvfree (when a competing batch drops because *spare is already populated, the guard drops before batch), or shared.state. DeferredBatch::is_idle() checks every field (has_source_completions, values, bindings, vacated, render, tvb), and reserve_replays is never called on a publication batch so lent capacities stay bounded.
  • Queue scratch vectors (drivers/gpu/drm/asahi/g17/queue/mod.rs:108-115, 447-530, 587-645): Moving self.scratch out with core::mem::take and clearing pending then guards before restoring self.scratch preserves error-path cleanup semantics. (If outputs.publish() fails after guards.drain_all(), guards drains before scratch.pending.clear(), which matches the pre-existing guards.into_iter() drop order.)
  • In-place Packet::new (drivers/gpu/drm/asahi/g17/job.rs:152-169): UniqueArc::new_uninit + init_with(init!(...)) is sound, avoids the Arc::init closure-move stack/memcpy regression you measured under LLVM=1, and preserves allocation-failure cleanup.

Remaining before approval (§5)

  • Clean same-head HW & regression run on J616s (default command line, without the unrelated tiler-heap PRs / unincluded 4th patch noted in the PR body): 0 SELinux denials, 0 failed units, 0 new kernel errors, 0 GPU recoveries.
  • One additional G17 Mac (M4 or M5) on top of integration/aurora with gpubench + a deqp-vk subset showing no new failures and 0 GPU recoveries.

Assisted-by: LLM (Gemini 4 Neon)

prepare_submission collected the validated commands and their VM pins in
two vectors allocated by every submit ioctl and freed at its end.

The queue now keeps both, empty between ioctls, so their room is allocated
once. Commands are drained from them in the same order, and whatever an
error leaves unsubmitted is released at the same point as before.

Neither needs contiguous pages, and `pending` is large: up to 63
commands of over a kilobyte each come to an order-3 block. As KVecs
that block was a GFP_KERNEL krealloc() that, on a machine without
compaction, ended in the OOM killer during dEQP-VK runs. Keep them in
KVVecs, so a large one falls back to vmalloc(), and charge them to the
caller's memory cgroup (GFP_KERNEL_ACCOUNT), since each userspace
queue keeps its own. They are freed with the queue, which may sleep.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
Packet::new built the packet, with its 1.3 KB validated command, on the
stack and moved it into the Arc. The compiler kept three stack copies of
the command for that, so every submitted command was copied four times
here.

Allocate the packet first and initialise it in place: the command is
copied once. A failed allocation still drops the command unpublished.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
@jgoyvaerts

Copy link
Copy Markdown
Author

@RyanTheTide, an amendment you should know about: new head 9c73f8ef6beb (was 1c3306581218), force-pushed.

The trigger was an order-3 GFP_KERNEL OOM kill under deqp-vk on the J616s. The OOM killer fired five times in one dEQP-VK run, from prepare_submission+0x690 → krealloc() → __kmalloc_large_node, while 26 GB was available. That's the pending vector: 64 commands of about 1.5 KB need an order-3 block at 16K pages. Our kernel had compaction off (our config, fixed separately), so that allocation was never allowed to fail and went to the OOM killer instead.

What changed:

  • Commit 2, "keep the submit ioctl's command vectors in the queue": Scratch::{pending, guards} are now KVVec, with GFP_KERNEL_ACCOUNT on their reserve/push. The change is folded into the commit that introduces Scratch, so the new code is right when it lands rather than fixed in a follow-up. The commit message says why.
  • Freeing: both are freed with the queue, which may sleep, so the vmalloc fallback is safe there.
  • Commits 1 and 3: unchanged. Commit 1 keeps its hash 98e9ab5b4753; commit 3 is only rebased. git diff 1c3306581218 9c73f8ef6beb touches only Scratch's declaration, its doc comment and the three reserve/push calls.

The same problem exists on integration/aurora as it is, where those vectors are still allocated per ioctl. The per-command dependency/checked-input/firmware lists and the merge arrays have it too. #2858 fixes those on its own, so it can land with or without this PR. Whichever lands second resolves a few lines in prepare_submission.

KUnit: 698 tests, 697 pass, 1 skip. Host tests --only g17 90/90. checkpatch 0 errors on all three commits. The hardware rows you and Dj asked for are still to come.

Assisted-by: LLM (Claude:Opus 5.5)

@jgoyvaerts

Copy link
Copy Markdown
Author

J616s (M4 Pro, 48 GiB, 25G83) regression row from our combined m4.179 kernel (integration/aurora bf423bf plus our other open G17 PRs), not a standalone kernel. It carries this PR at the previous head 1c33065 (all three commits patch-id equal), not 9c73f8e: its commit 2 is still the KVec version.

  • Boot 2026-11-22 23:51 UTC to 01:43 UTC (desktop, other GPU tests, our dEQP-VK and GLES runs), default cmdline: 0 AVC denials, 0 failed units, 0 kernel lines at err level (same as m4.178), 0 GPU recoveries. The window ends where a deliberate GPU hang test for another PR started.
  • The 7 synchronization.timeline_semaphore.{one_to_n,wait_before_signal}.write_blit_image_read_{blit,copy}_image.* cases that hit order-3 OOM kills from prepare_submission on m4.177: 3 rounds at -j 2, 21/21 Pass, 0 invoked oom-killer. This kernel has compaction enabled (our m4.177 config had it off), so the row says nothing about the KVVec change in 9c73f8e.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Formal §1–§6 pre-PR checklist re-review at 9c73f8ef6beb34a58a8c9a31cf02d76d675dcb0d (host-tests: green; mergeStateStatus: CLEAN; not superseded on integration/aurora).

Code, Safety & Attribution Assessment (0 code blockers)

  • PublicationBatch reuse (98e9ab5b4753, drivers/gpu/drm/asahi/g17/runtime.rs, runtime/backend.rs, runtime/render/submit.rs): spare_batch is a leaf mutex never held across allocation (DeferredBatch::publication()?) or kvfree (competing drops release the mutex guard before dropping batch), is_idle() verifies has_source_completions and all 5 vectors are empty, and reserve_replays() is never called on lent publication batches so reused capacity remains bounded.
  • Queue Scratch vectors (6408757ed87c, drivers/gpu/drm/asahi/g17/queue/mod.rs): Amending Scratch::{pending, guards} to KVVec with GFP_KERNEL_ACCOUNT avoids high-order contiguous allocation failures on 64-command submissions and charges retained per-queue capacity to the caller's memcg. Both are cleared unconditionally before restoring self.scratch and freed in sleepable context when Queue drops; Validated and Destinations hold host-side references only, so error-path cleanup order is sound.
  • In-place Packet::new (9c73f8ef6beb, drivers/gpu/drm/asahi/g17/job.rs): UniqueArc::new_uninit + init_with(init!(...)) is sound, preserves allocation-failure cleanup, and avoids the LLVM=1 Arc::init closure-move stack/memcpy regression.
  • §1–§4, §6: Pass — no model gating, default command line, all 3 commits carry human author/committer (Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>) and human Signed-off-by: only (zero AI commit trailers), Assisted-by: at end of PR body, host-tests green, KUnit 697/698 pass (1 skip).

§5 Blocker Holding APPROVE / MERGETRAIN-LINUX4

  • No exact-head hardware run for 9c73f8ef6beb: The 2026-11-23 J616s regression row ran the earlier 1c3306581218 KVec revision inside the combined m4.179 kernel rather than 9c73f8ef6beb's KVVec + GFP_KERNEL_ACCOUNT commit 2, and no second G17 Mac row has been run yet.
  • Required before approval:
    1. Clean exact-head (9c73f8ef6beb on integration/aurora, default cmdline, without unrelated PRs) J616s hardware + regression row (gpubench + dEQP-VK cases including the synchronization.timeline_semaphore.* cases exercising Scratch KVVec, 0 OOM / allocation failures, 0 GPU recoveries, 0 SELinux denials, 0 failed units, 0 new kernel errors).
    2. One additional G17 Mac row (M4 or M5) on top of integration/aurora with gpubench + a dEQP-VK subset showing 0 new failures and 0 GPU recoveries.

Assisted-by: LLM (Gemini 4 Neon)

@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (bot) changes-requested A reviewer requested changes (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot) blast:none Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) and removed needs-template PR body lacks Tested or Risk (bot) labels Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Merge readiness at 9c73f8ef: not ready yet

Check Detail
CI at head ok 1 check passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes no requested by DjDeveloperr
Hardware row at head ok in the PR body at 9c73f8ef
Conflicts ok none
PR text ok Risk: two-way · the G17 Macs: M4 (incl. Pro/Max), M5 and Neo (A18 Pro). A kept command vector stays at its largest size until the queue closes (at most 64 commands, about 96 KB per queue, vmalloc-backed and charged to the client's memory cgroup). Checked unaffected: only the J616s.

Areas: area:gpu

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@github-actions github-actions Bot added blast:m4 Macs affected, from Merge risk (bot) blast:m5 Macs affected, from Merge risk (bot) blast:neo Macs affected, from Merge risk (bot) and removed blast:none Macs affected, from Merge risk (bot) labels Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:gpu Area from CODEOWNERS (bot) blast:m4 Macs affected, from Merge risk (bot) blast:m5 Macs affected, from Merge risk (bot) blast:neo Macs affected, from Merge risk (bot) changes-requested A reviewer requested changes (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants