Skip to content

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

Draft
jgoyvaerts wants to merge 1 commit into
aurora-silicon:integration/aurorafrom
jgoyvaerts:m4pro/g17-submit-kvvec
Draft

jgoyvaerts wants to merge 1 commit into
aurora-silicon:integration/aurorafrom
jgoyvaerts:m4pro/g17-submit-kvvec

Conversation

@jgoyvaerts

@jgoyvaerts jgoyvaerts commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

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 up to 64 commands per ioctl (MAX_COMMANDS_PER_SUBMISSION), each (Validated, Order, Destinations) over 1 KB, which needs order 3 KVVec
prepare_submission's guards the same command count KVVec
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
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 also use GFP_KERNEL_ACCOUNT, so a client that submits huge batches or waits on huge prefixes is charged in its own memory cgroup. 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.
  • 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.

Overlap with #2228. #2228 moves pending/guards into a per-queue Scratch; it is amended to make that Scratch KVVec + GFP_KERNEL_ACCOUNT too. This PR fixes the code as it is on integration/aurora, so it can land on its own. Whichever of the two lands second resolves a few lines in prepare_submission.

Testing

  • KUnit (arm64 qemu, asahi + SEP + ISP config): 738 tests, 737 pass, 1 skip.
  • tools/asahi/run-host-tests.py --only g17: 90/90.
  • checkpatch: 0 errors. The 1 warning is the quoted kernel log line.
  • The same change is in our next M4 Pro test kernel (with compaction back on).

Not done yet (why this is a draft)

  • Not booted. The evidence will be the dEQP-VK run that hit this, on our next M4 Pro (J616s) test kernel: 0 invoked oom-killer / page allocation failure lines, plus the usual regression checks (SELinux denials, failed units, new kernel errors).
  • Not tested on the M4, the M4 Max or the M5. It's shared G17 queue code with no chip gate.
  • Not changed: file.rs SyncItem::parse_array() is also a KVec sized by userspace (the in/out sync counts). It's shared by every GPU generation, so it gets its own patch.

Assisted-by: LLM (Claude:Opus 5.5)

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 <jonathan.goyvaerts@gmail.com>
@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.
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