Skip to content

drm/asahi: g17: keep userspace-sized submit vectors off contiguous pages - #2858

Open
jgoyvaerts wants to merge 9 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-submit-kvvec
Open

jgoyvaerts wants to merge 9 commits into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-submit-kvvec

Conversation

@jgoyvaerts

@jgoyvaerts jgoyvaerts commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

What: Keep the G17 submit path's userspace-sized vectors (the per-command dependency and ordering lists, the publication links, the merge arrays) off contiguous pages and charge them to the caller, after an order-3 GFP_KERNEL OOM kill under deqp-vk. The per-ioctl commands and VM pins already got this in main with #2228.
Tested: J616s (M4 Pro), our m4.201 test kernel = main a851ca0edfc9 + our carried patches + this change backported (not this head itself): dEQP-VK.synchronization{,2}.timeline_semaphore.* 5,753/5,753 Pass; 0 OOM kills, 0 page allocation failures, 0 GPU recoveries; a kmem:mm_page_alloc probe shows 0 asahi order >= 2 allocations that could reach the OOM killer (none from the submit path); compaction is on in that kernel. Cyberpunk 2077 A/B, J616s, each kernel = main a851ca0edfc9 + our carried patches + this change backported (not this head itself): links charged per KBox (m4.201) 18.5-19.2 fps, memcg slab hooks 0.35-0.40% of samples; links uncharged (m4.202) 20.0-21.0 fps, hooks 0.04-0.05%; links in one charged block with the might_sleep() guard (m4.205, the backport of edade58c698e + 25ac1dfa71be; 8 profiled + 3 traced launches, 0 recoveries, 0 client failures) 24.6-25.9 fps, frame p50 38.5-40.6 ms, hooks 0.055-0.077%. So the one charge per job costs ~0.02 percentage points of samples and no frame rate against the uncharged links; m4.205 also carries other G17 changes against m4.202 (pooled fences, a per-queue pending cap, others), so its fps gain belongs to that kernel, not to this PR. At 25ac1dfa71be: KUnit (arm64 qemu, asahi + SEP + ISP config) 930 tests: 929 pass, 1 skip, including the new link-block cases (red on the per-link code; allocating per link again or dropping __GFP_ACCOUNT fails them); the same KUnit with CONFIG_DEBUG_ATOMIC_SLEEP=y runs with 0 "sleeping function called from invalid context" reports, and dropping a link block under a spinlock reports one from LinkBlock's drop; tools/asahi/run-host-tests.py: all 251 g17 entries pass, and the 29 failures are the set that fails on plain main in this environment; checkpatch clean.
Risk: two-way door · M4, M4 Pro, M4 Max and M5 (shared G17 code)
Not yet: a row at this head on current main; the dEQP-VK run with CONFIG_COMPACTION off; the M4, M4 Max and M5 rows; and a cap on pending commands per queue: none exists here, so charging bounds this memory only under a memcg limit (the cap is #3602)

Details

Trigger: an order-3 GFP_KERNEL OOM kill under deqp-vk. On a J616s (M4 Pro, 16K pages) the OOM killer fired five times in one dEQP-VK run, from the submit ioctl, while 26 GB was available. It killed unrelated processes:

deqp-vk invoked oom-killer: gfp_mask=0x40cc0(GFP_KERNEL|__GFP_COMP), order=3
 krealloc_node_align_noprof+0x154/0x238
 rust_helper_krealloc_node_align+0x10/0x20
 ..._asahi_core3g175queue...18prepare_submission+0x690/0xd8c [asahi]
 ..._asahi_core3g175queue...6submit...

That kernel had CONFIG_COMPACTION off, which is our own config and is being fixed there. With nothing to compact, an order <= 3 allocation is never allowed to fail, so it ends in the OOM killer. Compaction makes order 3 recoverable, but the driver still shouldn't need physically contiguous memory per submit for data userspace sizes.

What userspace sizes, and what changes

Vector Sized by Change
prepare_submission's pending and guards up to 64 commands per ioctl (MAX_COMMANDS_PER_SUBMISSION), each (Validated, Order, Destinations) over 1 KB, which needs order 3 already KVVec in main's per-queue Scratch (#2228)
each command's dependencies, checked_inputs, firmware the command's inputs plus the other engine's unfinished prefix, which can be thousands KVVec
Packet::{inputs, firmware} the same lists, kept by the job KVVec
EnqueueOrdering's predecessors, host waits, firmware waits and inherited timestamp waits; Fences::timestamp_waits the covered prefixes of both engines, pushed one at a time from empty KVVec
the publication's links one per covered unfinished predecessor one KVVec block per command, owned by its publication (see below)
sync_input::merge()'s pointer array the dependency count KVVec
asahi_g17_merge_fences()'s cursor array the dependency count kvmalloc_array() / kvfree()

All of them use GFP_KERNEL_ACCOUNT, so a client that submits huge batches or waits on huge prefixes is charged in its own memory cgroup, which is their bound. The links are the same kind of per-job state as the job's fence vectors: a command behind a stalled predecessor adds one link, and one fence, per covered unfinished publication, so nothing but the memcg caps them. Charging one KBox per link put memcg's slab hooks on every submit and on the event work that frees links (measured above), so a command's links are one block, charged once, that its publication owns: it publishes only after every predecessor has consumed its link, and each link holds the publication until then. kvmalloc still tries kmalloc first, so small sizes keep their current memory.

vfree() context. A vmalloc'ed vector must be freed where vfree() may sleep.

  • The command, dependency and merge vectors are freed in the ioctl.
  • EnqueueOrdering is freed in the ioctl. A command's link block is freed with its publication, which drops where its Completion, frontier entry or last link does: the ioctl, the queue's teardown, drm_sched's free_job and kill work, or the event work through Deferred::finish(). None of them holds a spinlock, has interrupts disabled or is in an RCU read side; the hard interrupt and the rescan hrtimer only queue the event work. Dropping a publication already took a mutex (fence::Member -> Submission::release_state()), and LinkBlock's drop now asserts might_sleep().
  • Fences (and their timestamp waits) are held only by the Queue, which main already fills with KVVecs.
  • Packet's two vectors go with the packet. Its holders are runtime state behind mutexes, the drm_sched job (free_job runs from the scheduler's workqueue or at teardown), and the ioctl.
  • No file that holds a Packet takes a spinlock or RCU read lock, or disables IRQs. The only fence callback (sync_scope.c) holds no packet.

Left contiguous, by design. The merged dma_fence_array's own fence array is allocated by core __dma_fence_unwrap_merge() and freed with kfree() in dma_fence_array_release(), so it can't move to vmalloc from the driver. It holds only unsignalled fences, 8 bytes each.

Merged with main. #2228 landed first, with pending/guards in a KVVec Scratch; the merge 77eb06bd5c89 takes main's version there and main's EnqueueOrdering/ProducerWaits logic in enqueue(), and only moves the vectors that hold the fences. ad7c997ec0f2 covers the vectors main added since this branch was written.

Evidence

Testing

  • At cab097479b9a: KUnit (arm64 qemu, asahi + SEP + ISP config) 928 tests, 927 pass, 1 skip; tools/asahi/run-host-tests.py all g17 entries pass (251), with only the 29 failures plain main has in this environment. test-fence-signalling.py's host shim now provides kvmalloc_array()/kvfree() (cab097479b9a).
  • At b99cc53: KUnit 738 tests, 737 pass, 1 skip; --only g17 90/90; checkpatch 0 errors (the 1 warning is the quoted kernel log line). checkpatch on the new commits: 0 errors, 0 warnings.
  • The pre-merge change ran in our M4 Pro m4.179 to m4.185 kernels (with compaction back on).

Not done

The submit ioctl collects its validated commands and their VM pins in
KVecs, and builds each command's dependency, checked-input and firmware
fence lists in KVecs too. All of them are sized by userspace: up to 64
commands per submission, each over a kilobyte once validated, and as
many dependencies as the command waits for, which for the other engine's
unfinished commands can be thousands. kmalloc() needs them physically
contiguous, so a large submission asks for an order-3 block for its
commands alone, on every ioctl. On a machine without compaction that is
never allowed to fail and ends in the OOM killer instead, five times in
one dEQP-VK run on an M4 Pro, with 26 GB available:

  deqp-vk invoked oom-killer: gfp_mask=0x40cc0(GFP_KERNEL|__GFP_COMP), order=3
   krealloc_node_align_noprof+0x154/0x238
   rust_helper_krealloc_node_align+0x10/0x20
   ..._asahi_core3g175queue...18prepare_submission...

Nothing needs these contiguous: only the ioctl and the job read them.
Make them KVVecs, and the cursor array the merge of a long dependency
list allocates a kvmalloc_array(). Small ones keep their kmalloc
memory. Userspace decides how large they get, so charge all of them to
the caller's memory cgroup (GFP_KERNEL_ACCOUNT).

A vmalloc()ed vector must be freed where vfree() may sleep. The
command, dependency and merge vectors are freed in the ioctl. A job's
packet keeps its checked inputs and firmware fences, and its owners
release it under the runtime's mutexes or from drm_sched's free_job()
work, never under a spinlock or in a fence callback.

The fence array of the merged dependency itself is allocated by
__dma_fence_unwrap_merge() and freed with kfree() by dma_fence_array,
so it stays contiguous. It holds only unsignalled fences, 8 bytes each.

Signed-off-by: Jonathan Goyvaerts <[email protected]>
@jgoyvaerts
jgoyvaerts force-pushed the m4pro/g17-submit-kvvec branch from 5ba7b33 to b99cc53 Compare November 22, 2026 20:30
@RyanTheTide

Copy link
Copy Markdown
Contributor

Review at b99cc53 (non-author, draft). No blocker.

  • Every vector that grows with userspace input (commands per ioctl, dependency and prefix lists, the merge arrays) moves to KVVec / kvmalloc_array() with GFP_KERNEL_ACCOUNT; kvmalloc still tries kmalloc first, so the common small case is unchanged, and a large one no longer needs an order-3 block or reaches the OOM killer through compaction-free reclaim.
  • The vfree() context holds as far as I can see: none of the files that hold a Packet (job, queue/, runtime/, trace) use a spinlock, IRQ-off section or RCU read side, the drm_sched free_job runs from the scheduler workqueue, and the C fence callback in sync_scope.c holds no packet. kvfree_atomic-style deferral is not needed.
  • Charging to the caller's memory cgroup is right for data the caller sizes.
  • Merges cleanly on the current integration/aurora tip.

Small points, not blocking:

  • A row showing the same dEQP-VK run on a 16K-page Mac with CONFIG_COMPACTION still off and no OOM kill would prove the change on its own, independent of the config fix.
  • pending starts with capacity 1 and grows per command; with up to MAX_COMMANDS_PER_SUBMISSION entries, with_capacity from the parser's command count (if it is known up front) would save the reallocations.

Assisted-by: LLM (Claude:Opus 5.5)

@jgoyvaerts

Copy link
Copy Markdown
Contributor Author

Partial hardware evidence from the J616s (M4 Pro). The same change, in our tree as one commit on top of #2228's per-queue scratch, is in our m4.179 kernel, which also turns compaction back on. On this boot:

  • The vkd3d-proton test that queues 65,536 commands passed twice. Its per-command dependency and merge vectors grew through kvmalloc without any page allocation failure.
  • The d3d12 list (544 tests) and dEQP-VK query_pool/timestamp (1999 cases) show no regressions against the same Mesa on m4.177.
  • 0 invoked oom-killer and 0 page allocation failure, 0 GPU recoveries.

This stays a draft. I haven't rerun the large dEQP-VK run that hit the OOM killer, and with compaction now on in that kernel, 0 OOM kills on this boot doesn't show this change on its own. The next check is that dEQP-VK run, with a kmem:kmalloc probe, to show the submit path no longer asks for high-order blocks.

Assisted-by: LLM (Claude:Opus 5.5)

Alyx-ML added a commit to Alyx-ML/linux that referenced this pull request Nov 23, 2026
Several ioctls size a kernel allocation straight from userspace:

- SUBMIT copies cmdbuf_size bytes (a u32, so up to 4 GiB) into a KVec.
  Only G16 bounds it before the copy (4 MiB); every other GPU kept the
  trait default of usize::MAX.
- SUBMIT allocates in_sync_count + out_sync_count sync items before any
  per-GPU check.
- VM_BIND copies stride * num_binds bytes and then reserves num_binds
  prepared operations. Without a GPU batch limit (bind_batch_limit()
  returns None), both are up to u32::MAX.

A request past the page allocator's largest order hits
WARN_ON_ONCE_GFP() in __alloc_pages(). Any user with the render node can
trigger it, and with panic_on_warn it takes the machine down. Short of
that limit, a valid but large request still needs high-order contiguous
pages: an order-3 allocation of the sync array failed on J616s under
deqp-vk.

For SUBMIT:

- Copy the command stream and build the sync array in KVVec with
  GFP_KERNEL_ACCOUNT. kvmalloc() falls back to vmalloc() instead of
  failing on fragmented memory, and the memory is charged to the
  caller's memcg. The sync array keeps this type through every
  generation's submit() (G13/G14, G15, G16, G17, G18, M3, M5); the G17
  job vectors are converted in aurora-silicon#2858.
- kvmalloc() refuses, and warns about, more than INT_MAX bytes. The
  trait default for max_cmdbuf_size() is now INT_MAX, so a larger
  command stream fails with E2BIG. This rejects no stream any GPU could
  accept before. A sync array larger than INT_MAX bytes fails with
  E2BIG too.
- G17 returns its own bound: the largest valid G17 stream,
  MAX_COMMANDS_PER_SUBMISSION * (1 + 5) *
  (size_of::<drm_asahi_cmd_header>() + u16::MAX) = 25,168,512 bytes,
  checked at build time to be at most INT_MAX.
- G15 returns its 4 MiB MAX_CMDBUF, as G16 already does. Its parser
  refused a longer stream before, but only after the copy, with EINVAL.
  Such a stream now fails before the copy, with E2BIG.
- G18, M3 and M5 use the shared default until their own largest stream
  is known.

For VM_BIND, pass __GFP_NOWARN on the user-sized allocations, so a
request that is too large fails with ENOMEM and no warning. Its sizes
are not changed. The timestamp mapping limit is left to
aurora-silicon#2531.

Checked by reading the call paths (file.rs submit, parse_array, vm_bind,
vm_bind_g17, mmu/bind.rs, and each generation's submit()). rustfmt is
clean on the changed code. The kernel Rust code was not compiled here.
What userspace can see: on G17, a stream over 25,168,512 bytes, which
its parser cannot accept, now fails before the copy; on G15, a stream
over 4 MiB fails with E2BIG before the copy instead of EINVAL after it;
on G13, G14, G18, M3 and M5, only streams over 2 GiB fail early. The
allocation path changes on every GPU, so deqp-vk or CTS runs on G13, G14
and G17 (J616s among them) are needed before this is merged.
@RyanTheTide

Copy link
Copy Markdown
Contributor

@jgoyvaerts checking open drafts today. To come out of draft, this still needs:

  • It conflicts with main. Merge main in or rebase (no force push needed: a merge commit is fine).
  • Then a test run at the final head on a Mac with the hardware it touches, and mark it ready for review.

If it's no longer needed, say so here and we'll close it. Otherwise it stays open as a draft until these are done.

Conflicts, and how they were resolved:

- g17/queue/mod.rs, prepare_submission(): main (aurora-silicon#2228) keeps pending
  and guards in the per-queue Scratch, already KVVec and charged with
  GFP_KERNEL_ACCOUNT. Main's version is taken; this branch's local
  KVVecs and the ValidatedBatch alias go.
- g17/queue/mod.rs, enqueue(): main (aurora-silicon#3119 and the timestamp overlap
  work) builds the producer waits in EnqueueOrdering and feeds the
  firmware-ordered ones through ProducerWaits. Main's logic is taken;
  dependencies, checked_inputs and firmware become KVVecs charged with
  GFP_KERNEL_ACCOUNT, as this branch did for the old loops.

job.rs, sync_input.rs and sync_input.c merged without conflicts.

No behaviour change beyond this branch's: which fences each command
waits for, checks and hands to firmware is main's; only where the
vectors that hold them are allocated, and who is charged, changes.

Signed-off-by: Jonathan Goyvaerts <[email protected]>
Since this branch was written, main builds a command's producer waits
in two steps: EnqueueOrdering collects the covered predecessors, host
waits, firmware waits and inherited timestamp waits from the frontier,
and the publication's Prepared links one record per predecessor. Each
of those vectors grows with the same covered prefixes this branch's
dependency vectors do, pushed one entry at a time from empty, so a deep
prefix of the other engine reallocates them into high-order contiguous
blocks on every command.

Make them KVVecs charged to the caller with GFP_KERNEL_ACCOUNT, and
charge the per-predecessor link records too.

vfree() context: EnqueueOrdering and Prepared are consumed or dropped
in the ioctl; Prepared::activate() frees its links after the target's
mutex is released. The inherited timestamp waits end up in Fences,
which only the Queue holds, and the Queue already keeps KVVecs.

Signed-off-by: Jonathan Goyvaerts <[email protected]>
test-fence-signalling.py compiles the production g17/sync_input.c
against host shims. Its fence merge now takes its cursor array with
kvmalloc_array(GFP_KERNEL_ACCOUNT) and frees it with kvfree(), so the
shim provides those instead of kmalloc_array(), kfree() and GFP_KERNEL,
which nothing else in the harness uses.

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

Copy link
Copy Markdown
Contributor Author

Merged main (4cc1d6d) in as a merge commit, no force-push. New head: cab097479b9a.

Conflicts, all in g17/queue/mod.rs:

The resolution changes no behaviour: main still decides which fences a command waits for, checks and hands to firmware. Comparing this PR's diff against its old base with the merge's diff against main shows only that move.

Two commits on top, both for what main added since this branch was written:

  • ad7c997ec0f2 drm/asahi: g17: keep the enqueue ordering vectors off contiguous pages. EnqueueOrdering's predecessors, host waits, firmware waits and inherited timestamp waits, Fences::timestamp_waits and the publication's Prepared::links grow with the same covered prefixes and were KVecs pushed from empty. They become KVVec + GFP_KERNEL_ACCOUNT. vfree() context: EnqueueOrdering and Prepared are freed in the ioctl, and activate() frees its links after the target's mutex is released. Fences are held only by the Queue, which main already fills with KVVecs.
  • cab097479b9a selftests: asahi-m4: shim kvmalloc_array() for g17/sync_input.c. test-fence-signalling.py compiles the production sync_input.c against host shims that only had kmalloc_array()/kfree().

Checks at cab097479b9a (local, an M4 Pro host):

  • KUnit (arm64 qemu, asahi + SEP + ISP config): 928 tests, 927 pass, 1 skip.
  • tools/asahi/run-host-tests.py: 738 ok, 29 fail. All 251 g17 entries pass. The 29 failures are the same set that fails on plain main in this environment: apple-dcp, test-m3-uapi, g15-pb-growth/retention/stats-drain (link errors) and sep-suspend-errors (missing rustup 1.93.1).
  • checkpatch: 0 errors, 0 warnings on the new commits.
  • CI: the runs for this head are queued. aurora-silicon CI hasn't been starting runs since about 13:12 UTC today.

Next: the same dEQP-VK run that hit the OOM killer, with a kmem:kmalloc probe on the submit path, on a J616s (M4 Pro) kernel carrying this head. It's queued for our next test kernel.

@jgoyvaerts
jgoyvaerts marked this pull request as ready for review November 28, 2026 18:10
@jgoyvaerts
jgoyvaerts requested review from a team and DjDeveloperr as code owners November 28, 2026 18:10

@aurora-silicon-review aurora-silicon-review Bot 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.

Verdict: FINISH, then merge at cab09747

Blockers:

  1. Wait for the 21 queued CI checks (apple-build, kunit, host-tests, pr-text, readiness, 16 KUnit suites) at cab09747 to finish green.
  2. Complete the queued exact-head J616s (M4 Pro) hardware row at cab09747 (dEQP-VK + regression check; no Mac boot at this head yet).

Checked:

  • All 6 modified files (g17/completion/publication.rs, g17/job.rs, g17/queue/mod.rs, g17/sync_input.c, g17/sync_input.rs, tools/testing/selftests/drivers/gpu/asahi-m4/test-fence-signalling.py): 0 code blockers.
    • Verified all userspace-sized submit vectors (Prepared::links + KBox<Link>, Packet::{inputs, firmware}, Fences::timestamp_waits, dependencies, checked_inputs, EnqueueOrdering vectors, sync_input::merge(), and asahi_g17_merge_fences() cursors) move cleanly to KVVec / kvmalloc_array() with GFP_KERNEL_ACCOUNT.
    • Verified vfree() drop contexts are sleepable: Prepared::activate() releases self.target.state.lock() before consuming/dropping self.links, EnqueueOrdering/merge/dependency vectors drop in the ioctl path, and Packet/Fences drop under queue/scheduler workqueue/teardown contexts (no spinlock/IRQ drop).
    • Verified test-fence-signalling.py shims GFP_KERNEL_ACCOUNT, kvmalloc_array(), and kvfree() cleanly (251/251 g17 host tests, 927/928 KUnit, checkpatch clean, MERGEABLE on main).
Evidence
  • Head: cab09747 (MERGEABLE on main, +57/-48 across 6 files).
  • CI at review time: 21 checks QUEUED/PENDING, 0 failures.
  • Code is clean; ready to approve once CI and the exact-head J616s row at cab09747 pass.

@jgoyvaerts

Copy link
Copy Markdown
Contributor Author

Note on where the hardware row will come from: our J616s test kernels are based on main a851ca0edfc9 plus our carried patches, not on current main, so the kernel that runs the dEQP-VK kmalloc-probe row won't carry cab097479b9a itself. It will carry a backport of this PR's change onto that base:

  • The original change is already there as a carried patch.
  • ad7c997ec0f2 is adapted: that base has no Fences::timestamp_waits yet, so it covers Prepared::links and the one firmware-wait push.
  • The sync_input.c test shim fix is included.

KUnit and the g17 host harnesses pass on the backport. I'll post the run's result here, naming the kernel and that base, and the Tested: line will say the same.

@jgoyvaerts

Copy link
Copy Markdown
Contributor Author

J616s (M4 Pro) result, on our m4.201 test kernel. That kernel is main a851ca0edfc9 plus our carried patches plus this PR's change backported (the original change, ad7c997ec0f2 adapted to that base, and the sync_input.c test shim), not cab097479b9a itself.

  • Cases: dEQP-VK.synchronization{,2}.timeline_semaphore.*, 5,753 cases. This is the group whose submits hit the order-3 OOM kills, including the write_blit_image cases. They ran one deqp-vk process at a time, 50 cases per process, on system Mesa.
  • Results: 5,753/5,753 Pass.
  • Kernel log during the run: 0 invoked oom-killer, 0 page allocation failure, 0 GPU recoveries.
  • Probe: a kmem:mm_page_alloc tracepoint on order >= 2, non-movable allocations, with kernel stacks. It caught 760 such allocations.
    • No allocation in the asahi driver could reach the OOM killer: none entered direct reclaim without __GFP_NORETRY. Nothing came from prepare_submission, enqueue, EnqueueOrdering or Prepared::new.
    • The 758 under ASAHI_VM_BIND are kvmalloc's first kmalloc try for core drm_gem_get_pages()'s page array. That try is GFP_NOWAIT and falls back to vmalloc.
    • The one under ASAHI_SUBMIT is an opportunistic slab refill for Packet::new's render footprint.
  • Caveat: that kernel has compaction on, so the 0 OOM kills alone don't isolate this change; the probe does. I haven't run the compaction-off row Ryan suggested.

Open: a Cyberpunk profile on the same kernel shows more time in the memcg slab charge and free hooks. That is consistent with the per-submit GFP_KERNEL_ACCOUNT on Prepared::links and its Link records, which are freed in the event work. It isn't attributed yet, since other changes are in that kernel too. If it holds, those transient, frontier-bounded allocations would go back to uncharged GFP_KERNEL and stay KVVec. Until then I'm not claiming this change is performance-neutral.

@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (bot) blast:m4 Macs affected, from Merge risk (bot) blast:m5 Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 28, 2026
@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Merge readiness at 5a735f9c: not ready yet

Check Detail
CI at head ok 22 checks passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head no no line with the head SHA 5a735f9c, a Mac and a result in the PR body or a comment
Conflicts no merge conflicts with main: merge it in (no force push)
PR text ok Risk: two-way · M4, M4 Pro, M4 Max and M5 (shared G17 code)

Areas: area:gpu

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

Prepared::new() takes one link record per covered predecessor, and the
vector holding them, with GFP_KERNEL_ACCOUNT. Every submit charges them
to the caller's memory cgroup, and the event work that publishes the
predecessor uncharges them, which puts memcg's slab hooks on both
paths. On an M4 Pro, Cyberpunk 2077 spent 0.35-0.40% of its samples in
them, against 0.03% before, and its median frame time rose from about
48 to 52-53 ms; without the charge both come back.

The charge buys nothing here: the links are transient, bounded by the
frontier, and never outlive the predecessor's publication. Take them
with GFP_KERNEL; the vector stays a KVVec, so a deep prefix still needs
no contiguous pages.

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

Copy link
Copy Markdown
Contributor Author

New head e25c142aa1a1: one commit on top, "drm/asahi: g17: don't charge a publication's links to the caller".

Prepared::links stays a KVVec. The links vector and the per-predecessor Link records go back to GFP_KERNEL. They are transient, bounded by the frontier, and never outlive the predecessor's publication. Charging them put memcg's slab charge and free hooks on every submit and on the event work that frees them.

That is the cost my earlier comment left open, now measured. A Cyberpunk 2077 A/B on our J616s (M4 Pro) test kernels compared the a851 backport with and without this commit. Each run is 8 profiled launches plus 3 traced, with 0 recoveries.

  • Without (m4.201): 18.5-19.2 fps, frame p50 52-53 ms, memcg slab hooks at 0.35-0.40% of samples.
  • With (m4.202): 20.0-21.0 fps, p50 47.8-49.5 ms, hooks at 0.04-0.05%. Before this PR's change they were at 0.03%.
  • GPU busy time and GPU gaps are back to the pre-change range.

Checks at e25c142aa1a1:

  • KUnit (arm64 qemu, asahi + SEP + ISP config): 928 tests, 927 pass, 1 skip.
  • tools/asahi/run-host-tests.py: all 251 g17 entries pass. The 29 failures are the same set that fails on plain main in this environment.
  • checkpatch: clean.

The other vectors keep GFP_KERNEL_ACCOUNT. The dEQP-VK OOM row stays as posted: m4.201 had the links charged, which doesn't affect contiguity.

@aurora-silicon-review aurora-silicon-review Bot 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.

Verdict: FINISH at e25c142. The delta preserves KVVec backing and removes accounting from both the per-submit links vector and each Link. The backported J616s A/B quantifies the accounting cost. Please document the aggregate bound on live uncharged Link records across submissions: a frontier bound per submit does not itself cap the number of successors attaching to the same unfinished predecessor. Identify the submission or fan-out limit that supplies that bound. Exact-head hosted CI and the current-base J616s dEQP-VK regression row remain open.

A command's publication waits on every covered unfinished predecessor
through one link each. Since "drm/asahi: g17: don't charge a
publication's links to the caller", each link is its own uncharged
KBox. Nothing caps how many a client keeps alive: every command behind
a stalled predecessor adds one link per covered unfinished publication,
so n such commands hold n(n-1)/2 links for as long as the stall lasts.

They are per-job state of the same kind as the job's fence vectors
(Packet::inputs and Packet::firmware hold one fence per covered
producer): sized by what the command covers, and charged to the client,
so the client's memory cgroup is their bound, as it is for those
vectors. What made charging them cost frame time was one charge and
uncharge per link: on an M4 Pro, Cyberpunk 2077 spent 0.35-0.40% of
its samples in memcg's slab hooks, on the submit path and in the event
work that frees links.

Allocate all of a successor's links in one block, charged with
GFP_KERNEL_ACCOUNT. The successor's publication owns the block from
activation on, and predecessors' lists point into it. That is one
charge and one uncharge per job, whatever the number of links.

The block outlives every link in it: a publication publishes only
after each predecessor has consumed its link, and each link holds its
successor until then, which a predecessor dropped before publishing
releases (State::drop). A link reaches no other thread while it sits
in one list, under that predecessor's lock, or in the one traversal
that detached it.

KUnit checks that a successor with three predecessors makes exactly
one allocation, charged, and frees it exactly once: after an error in
the middle of its prefix, after a clean settle, when setup is dropped
before activation and when a predecessor is dropped unpublished.
Allocating per link again, or dropping __GFP_ACCOUNT, fails it.

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

Copy link
Copy Markdown
Contributor Author

New head edade58c698e: one commit on top, "drm/asahi: g17: keep a publication's links in one charged block". It replaces e25c142aa1a1's uncharged links.

Why the links are charged again: nothing else caps them. Every command behind a stalled predecessor adds one link per covered unfinished publication, so n such commands hold n(n-1)/2 links for as long as the stall lasts. The job's fence vectors (Packet::inputs and Packet::firmware) grow the same way, one fence per covered producer, and are charged. The links are the same kind of per-job state, so the client's memory cgroup is their bound too.

Where the measured cost came from: one charge and one uncharge per link. Now all of a command's links are one GFP_KERNEL_ACCOUNT block. Its publication owns the block from activation, and predecessors' lists point into it. That is one charge per job, however many links it has.

  • The block outlives its links: a publication publishes only after every predecessor has consumed its link, and each link holds the publication until then.
  • A predecessor dropped before it publishes releases the links still waiting on it.

Tests:

  • New KUnit cases, written first and red on the per-link code. A command with three predecessors makes exactly one allocation, charged, and frees it exactly once in each of these cases:
    • after an error in the middle of its prefix;
    • after a clean settle;
    • when setup is dropped before activation;
    • when a predecessor is dropped unpublished.
  • Mutations: allocating per link again fails them, and so does dropping __GFP_ACCOUNT.
  • KUnit overall: 930 tests, 929 pass, 1 skip.
  • tools/asahi/run-host-tests.py: all 251 g17 entries pass. The 29 failures are the same set that fails on plain main in this environment.
  • checkpatch: clean.

Not yet: the same Cyberpunk A/B with one-block charging. The backport is queued for our next J616s test kernel. Pass means fps within noise of the uncharged run and memcg hooks near 0.05% of samples.

@aurora-silicon-review aurora-silicon-review Bot 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.

Verdict: HOLD — single-block charged link design clean, awaiting queued CI and exact-head J616s hardware rows at edade58c698e

Blockers:

  1. Hosted CI at edade58c698e (apple-build, host-tests, pr-text, and KUnit suites) is still queued and must finish green.
  2. Exact-head G17 hardware verification on current main remains open (J616s dEQP-VK regression row at edade58c698e plus the queued one-block Cyberpunk A/B confirming frame time and memcg hook parity with the uncharged baseline).

Checked:

  • Commit edade58c698e in drivers/gpu/drm/asahi/g17/completion/publication.rs: replaces per-link KBox allocations with a single GFP_KERNEL_ACCOUNT-charged LinkBlock(KVVec<UnsafeCell<Link>>) owned by State::block from Prepared::activate(), restoring client memcg bounding for stalled-predecessor link fan-out while reducing charging to one allocation per job.
  • unsafe lifetime, aliasing, and cycle teardown invariants:
    • Each Link retains Option<Arc<Publication>> (target) until release() drops the taken Arc after removing the link from its predecessor list and (when reused as a traversal frame in drain()) popping it from frames; this guarantees target and target.state.block stay alive for every reachable link even after target publishes.
    • State::drop walks remaining successors and calls release(), breaking the target -> block -> link -> target cycle if a predecessor is dropped before publishing.
    • Pre-activation drops of Prepared free LinkBlock directly without forming a cycle; zero-predecessor submissions perform no link-block allocation.
    • Disjoint UnsafeCell<Link> slots are touched only under their respective predecessor's mutex or by the single owning drain() traversal, and state.block is never mutably borrowed as a slice after installation.
  • New KUnit tests (consumer_links_are_one_charged_allocation, link_block_is_freed_once_on_release_and_drop_paths) covering prefix failure, clean settle, pre-activation drop, unpublished-predecessor drop, and empty predecessor sets (929 pass / 1 skip KUnit, 251/251 g17 host tests, checkpatch clean).
Evidence
  • Verified release() takes (*link.as_ptr()).target.take() into a stack temporary before drop(...) so no reference into LinkBlock remains live if that Arc drop frees Publication and State::block.
  • Verified Prepared::activate() holds target: Arc<Publication> across the entire 0..count installation loop and add_successor() completes target.predecessor_done(immediate) before release(link) and Self::drain(children).
  • MERGEABLE on main; no code blockers found at edade58c698e.

A publication's link block is a KVVec, so a large one may come from
vmalloc, and vfree() may sleep outside interrupt context. Its last
reference drops wherever the publication's does: in the ioctl, at
queue teardown, in drm_sched's free_job and kill work, and in the event
work through Deferred::finish(). None of them holds a spinlock, has
interrupts disabled or is in an RCU read side. The hard interrupt and
the rescan hrtimer only queue the event work. Dropping a publication
already takes a mutex (fence::Member's drop, through
Submission::release_state()), so those places had to be able to sleep
before the block existed.

Make that a checked rule at the free site: might_sleep() when a block
that owns memory is freed. With CONFIG_DEBUG_ATOMIC_SLEEP, KUnit runs
clean, and dropping only an unactivated block under a spinlock reports
"sleeping function called from invalid context" from LinkBlock's drop.

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

Copy link
Copy Markdown
Contributor Author

New head 25ac1dfa71be: one commit on top, "drm/asahi: g17: assert that a link block is freed where it may sleep".

A link block can be large enough to come from vmalloc, and vfree() may sleep outside interrupt context. So I traced every place a publication's last reference can drop:

  • the ioctl;
  • queue teardown;
  • drm_sched's free_job and kill work (the kill callback only schedules work);
  • the event work, through Deferred::finish().

None of them holds a spinlock, has interrupts disabled or is in an RCU read side. The RTKit hard interrupt and the rescan hrtimer only queue the event work. Dropping a publication already took a mutex (fence::Member's drop → Submission::release_state()), so those places had to be able to sleep before this PR. No call site changes.

The commit makes it a checked rule: LinkBlock's drop calls might_sleep() when it frees memory.

  • KUnit with CONFIG_DEBUG_ATOMIC_SLEEP=y: 0 "sleeping function called from invalid context" reports.
  • Dropping only an unactivated block under a spinlock reports one, from LinkBlock's drop.

Checks at 25ac1dfa71be:

  • KUnit: 930 tests, 929 pass, 1 skip.
  • Host tests: all 251 g17 entries pass. The 29 failures are the set that fails on plain main in this environment.
  • checkpatch: clean.

Not yet now also says that the number of pending commands per queue has no cap, so charging bounds this memory only under a memcg limit. A per-queue cap is being looked at separately, for its own PR.

@aurora-silicon-review aurora-silicon-review Bot 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.

Verdict: HOLD — might_sleep() check clean, awaiting queued CI and exact-head J616s hardware rows at 25ac1dfa71be

Blockers:

  1. Hosted CI at 25ac1dfa71be is still queued and must finish green.
  2. Exact-head G17 hardware verification on current main remains open (J616s dEQP-VK regression row plus the queued one-block Cyberpunk A/B confirming frame time and memcg hook parity with the uncharged baseline).

Checked:

  • Commit 25ac1dfa71be (+3/-0 in drivers/gpu/drm/asahi/g17/completion/publication.rs): calls kernel::task::might_sleep() in LinkBlock::drop() whenever self.0.capacity() != 0, enforcing at runtime under CONFIG_DEBUG_ATOMIC_SLEEP that KVVec/vfree() drops only in sleepable context (matching Publication's existing sleepable drop requirement via fence::Member -> Submission::release_state()).
  • Verification reported clean at 25ac1dfa71be: KUnit 929 pass / 1 skip with CONFIG_DEBUG_ATOMIC_SLEEP=y (0 atomic-sleep warnings; negative control under a spinlock triggers from LinkBlock::drop), 251/251 g17 host tests, checkpatch clean.
Evidence
  • Confirmed all Publication drop sites (ioctl, queue teardown, drm_sched free_job/kill work, and event work via Deferred::finish()) are sleepable contexts; hard IRQ and rescan hrtimer only schedule work.
  • MERGEABLE on main; no code blockers found at 25ac1dfa71be.

@jgoyvaerts

Copy link
Copy Markdown
Contributor Author

On the aggregate bound on live uncharged Link records (review of 2026-11-29 03:22 UTC): there isn't one, so the links are charged again, as one block per command, from edade58c698e on.

What bounded live links at e25c142aa1a1: nothing by count.

  • EnqueueOrdering::build() links a command to every covered unfinished publication of its queue, so n commands behind one stalled predecessor hold n(n-1)/2 links.
  • The drm_sched credits cap only the hardware rings; the entity queue behind them has no limit.
  • max_in_flight_per_queue is the advertised render-pair count, not an enforced cap.
  • A stall lasts up to the compute watchdog allowance (30 s by default) per job, and a client can extend it by queueing slow jobs back to back.

Why not a structural cap here:

  • Linking only the newest covered publication is unsound: the newest one needn't have the others as ancestors under its own prefixes and chain rules.
  • Even a sound transitive reduction stays m*n for independent producers covered by many consumers.
  • A chain of per-engine join nodes would keep joins alive after their frontier entries, behind an older stall.
  • And the job's fence vectors (Packet::inputs, Packet::firmware) already hold one fence per covered producer, so the submit path's per-job state is m*n whatever the links do.

What the PR does now: the links are that same kind of per-job state, and get the same bound: the client's memory cgroup.

  • edade58c698e: all of a command's links are one GFP_KERNEL_ACCOUNT block owned by its publication. That is one charge per job instead of one per link, which is what cost frame time before.
  • KUnit checks one charged allocation, freed exactly once, across the error, settle, unactivated-drop and dropped-predecessor paths.
  • 25ac1dfa71be: LinkBlock's drop asserts might_sleep(), since a large block may be vmalloc'ed. The block is freed only in the ioctl, at queue teardown, in drm_sched's free_job or kill work, or in the event work. KUnit with CONFIG_DEBUG_ATOMIC_SLEEP=y runs clean.

Hardware, Cyberpunk 2077 on a J616s (M4 Pro). Every kernel is main a851ca0edfc9 plus our carried patches plus this PR's change backported, not this head itself:

links kernel fps frame p50 memcg slab hooks (share of samples)
one charged KBox per link m4.201 18.5-19.2 52-53 ms 0.35-0.40%
uncharged m4.202 20.0-21.0 47.8-49.5 ms 0.04-0.05%
one charged block per command (this head's design) m4.205 24.6-25.9 38.5-40.6 ms 0.055-0.077%
  • The m4.205 row is 8 profiled and 3 traced launches, with 0 GPU recoveries and 0 client failures.
  • So the one charge per job costs about 0.02 percentage points of samples against uncharged links, and no frame rate.
  • m4.205 also carries other G17 changes against m4.202: pooled fences and a per-queue pending cap among them. Its fps gain, and the lower slab and publication-chain samples, belong to that kernel; the profile doesn't separate this PR's share.

A hard per-client bound on the whole submit path needs a cap on pending commands per queue. That is a uAPI change and is #3602, not part of this PR.

@RyanTheTide

Copy link
Copy Markdown
Contributor

I merged current main into this branch (5a735f9c, normal push, no rebase, your commits unchanged). The last CI failures came from main itself on 11-29: the apple-scaler-recovery and g17-compute-owed-stamp fixtures, plus the apple-dcp harness break fixed in #3672. Re-running a job would reuse the old merge commit, so a fresh merge was the only way to clear them. Both merges were conflict-free. Host tests pass on the merged tree. The PR's own code is unchanged from 25ac1dfa, so the existing hardware rows still cover it. Next: CI at this head and the review app's verdict.

@github-actions github-actions Bot added needs-hw-row No hardware row at the PR head (bot) and removed ci-failing A check failed at the PR head (bot) labels Nov 30, 2026

@aurora-silicon-review aurora-silicon-review Bot 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.

Verdict: HOLD at 5a735f9c.

Checked: Compared the PR patch before and after the forward merge. The file set and added and removed lines match the reviewed revision. Keep hosted checks and the current J616s Vulkan/memory-pressure regression plus the one-block accounting A/B.

@aurora-silicon-review aurora-silicon-review Bot 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.

Verdict: MERGE at 5a735f9cf848

Converts this App's earlier review at the same head to an approval under the 12-01 merge bar (better than before and doesn't break; block only on a shown failure). No code blocker was found in it. The merge gate (a row on one Mac at this head + fast CI) is tracked by pr-readiness, not held here.

Checked: Compared the PR patch before and after the forward merge. The file set and added and removed lines match the reviewed revision. Keep hosted checks and the current J616s Vulkan/memory-pressure regression plus the one-block accounting A/B.

Follow-ups (not blocking): hosted checks, J616s Vulkan/memory-pressure regression, one-block accounting A/B

@github-actions github-actions Bot added the merge-conflict Conflicts with the base branch (bot) label Dec 1, 2026

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

area:gpu Area from CODEOWNERS (bot) blast:m4 Macs affected, from Merge risk (bot) blast:m5 Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) merge-conflict Conflicts with the base branch (bot) needs-hw-row No hardware row at the PR head (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.

2 participants