Skip to content

drm/asahi: g17: bound a submission's command stream before copying it - #3015

Open
jgoyvaerts wants to merge 1 commit into
aurora-silicon:integration/aurorafrom
jgoyvaerts:m4pro/g17-cmdbuf-bound
Open

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

Conversation

@jgoyvaerts

@jgoyvaerts jgoyvaerts commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

G17 never set max_cmdbuf_size(), so it kept the default of usize::MAX. The submit ioctl copies the whole command stream into one kmalloc'd buffer before any parser sees it, and cmdbuf_size is a u32. So any process with the render node can make the kernel:

  • warn, for anything past KMALLOC_MAX_SIZE (32 MiB with the 16K pages asahi requires), where the page allocator's WARN_ON_ONCE(order > MAX_PAGE_ORDER) fires;
  • attempt a huge contiguous allocation, for anything below that, as large as userspace asked for.

This sets G17's bound from the format's real maximum, not a constant:

  • Per entry: an 8-byte drm_asahi_cmd_header plus a payload of at most u16::MAX bytes (the header's size field).
  • Work commands: at most MAX_COMMANDS_PER_SUBMISSION (64). The parser refuses at >= before counting the new one, so 64 are accepted.
  • Sets: the rest of the stream sets what later work uses. That's three attachment lists, the render resources (v1 and v2 share a slot) and the compute resources. A set that a later one replaces before any work, or one after the last work command, changes nothing. So a stream in which every entry has an effect has at most 5 sets per work command.
  • Bound: MAX_STREAM = 64 × 6 × (8 + 65535) = 25,168,512 bytes (24 MiB). Build-time asserts keep it within INT_MAX (the kvmalloc limit) and, on arm64, within KMALLOC_MAX_SIZE (32 MiB with 16K pages). The second is arm64-only because KMALLOC_MAX_SIZE depends on the architecture's page orders, and a COMPILE_TEST build elsewhere can have a smaller one. A longer stream only repeats sets and gets E2BIG before the copy, as G16 does with its 4 MiB bound.

Real streams are far smaller: Mesa's Vulkan driver sends at most 2 sets per work command, GL 1. Command payloads can be longer than their struct only with zero tails (payload<T>()), which is the usual DRM extension rule, so the header's u16 size really is the per-entry maximum.

Relation to #2755: #2755 makes the default 16 MiB for every GPU. That is below this maximum, so G17 overrides it either way, and the two don't conflict.

Testing

  • KUnit (arm64 qemu, asahi + SEP + ISP config): 746 tests, 745 pass, 1 skip. The new asahi_g17_stream case parses 64 zeroed 65535-byte renders, checks they fit MAX_STREAM, and checks that a 65th is refused.
  • tools/asahi/run-host-tests.py (what CI runs): the asahi-m4 suite passes. test-command-stream.py builds the G17 parser on the host, and now also takes ATTACHMENT_KINDS from the source. checkpatch: 0 errors.
  • On hardware, J616s (M4 Pro), our test kernel. It carries this change with only the INT_MAX assert; the arm64 assert is build-time only, so it behaves the same.
    • Oversized-submit check: DRM_IOCTL_ASAHI_SUBMIT with only cmdbuf_size set. Exactly MAX_STREAM passes the size check and fails at the queue lookup (ENOENT). MAX_STREAM + 1, 64 MiB and UINT32_MAX get E2BIG. 0 kernel warnings and 0 new kernel log lines.
    • vkd3d-proton's 544-test d3d12 list and dEQP-VK query_pool/timestamp (1999 cases): no regressions against the previous kernel, 0 refused submissions.
    • Boot: 0 failed units (system and user), 0 error-level kernel messages, 0 GPU recoveries. This kernel has no SELinux (LSMs: capability, landlock, yama), so there is no denial count.
  • Not tested on the M4, the M4 Max or the M5. An M5 row from someone with an M5 would be welcome.

The trigger was an order-3 GFP_KERNEL OOM kill under deqp-vk on that machine, from these copies. Making the copy itself not need contiguous pages is a separate change, waiting on #2755.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Formal §1–§6 pre-PR review at deff2b333b569dcfd86d414e1ebfeb5855b1f872 (base integration/aurora; live CI host-tests: FAIL).

Strengths

  • Bound derivation (drivers/gpu/drm/asahi/g17/command.rs:10-25): Verified against every cmd_type accepted by Parser::next_parsed() (g17/command.rs:239-308): at most MAX_COMMANDS_PER_SUBMISSION (64) work commands (RENDER/COMPUTE), each affected by at most 5 distinct state-setting kinds (3 attachment lists + render resources v1/v2 + compute resources), each up to size_of::<drm_asahi_cmd_header>() + u16::MAX bytes (25,168,512 B). Cleanly prevents the 4 GiB cmdbuf_size WARN_ON_ONCE(order > MAX_PAGE_ORDER) trigger before file.rs allocates.
  • §1 Code shape / §2 / §3 / §4 / §6: No chip/board/firmware gates; default-cmdline safe; commit deff2b333b56 carries human author/committer Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com> and human Signed-off-by: only with zero AI commit trailers; PR body ends with Assisted-by:.

Blockers

  1. CI failure in host-tests (asahi-m4/test-command-stream.py) — HARD BLOCKER:

    • Live check run host-tests failed (462/463 passed, FAIL asahi-m4, not ok 1 - test-command-stream.py) with:
      error[E0425]: cannot find value ATTACHMENT_KINDS in this scope at Parser.attachments: [Attachments; ATTACHMENT_KINDS] and Attachments::EMPTY; ATTACHMENT_KINDS.
    • Root cause: tools/testing/selftests/drivers/gpu/asahi-m4/test-command-stream.py:63-64 slices drivers/gpu/drm/asahi/g17/command.rs starting at /// Where a render command's stage-prefix fields start (after line 10's const ATTACHMENT_KINDS: usize = 3;), and its g17_constants regex does not extract ATTACHMENT_KINDS.
    • Fix: Update tools/testing/selftests/drivers/gpu/asahi-m4/test-command-stream.py to extract ATTACHMENT_KINDS (or keep ATTACHMENT_KINDS inside the sliced region) so host-tests passes cleanly.
  2. §1 / §5 Hardware Evidence:

    • The J616s (M4 Pro) hardware run was on a different commit ("with a weaker <= INT_MAX assert"), omits the §5 regression health tuple (SELinux denials, failed units) and an explicit oversized-submit E2BIG boundary check (cmdbuf_size > MAX_STREAM returning -E2BIG with 0 kernel warnings), and does not include an M5 (G17G/G17S/G17C) row even though g17/command.rs and g17/mod.rs serve both G17P and M5. Please add an exact-head hardware row with full §5 regression counts after fixing CI (and request an M5 run if no M5 machine is available).

Non-Blocking Notes

  • const _: () = assert!(MAX_STREAM <= kernel::bindings::KMALLOC_MAX_SIZE as usize); (g17/command.rs:25): holds on ARM64 16K (ARCH_FORCE_MAX_ORDER = 11 → 32 MiB), but DRM_ASAHI also allows (COMPILE_TEST && !GENERIC_ATOMIC64) with PAGE_SIZE_16KB. On non-ARM64 16K targets where MAX_PAGE_ORDER = 10, KMALLOC_MAX_SIZE is 16 MiB < 24 MiB, which would break COMPILE_TEST builds.
  • Note that #2755 is closed (unmerged), so the follow-up to switch file.rs's command-buffer copy from KVec to KVVec + GFP_KERNEL_ACCOUNT does not need to wait on #2755.

§1–§6 Checklist

  • §1 No model gating: Pass in code; M5 coverage owed/requested under §1/§5.
  • §2 Production-ready, no hacks: Pass.
  • §3 Safety: Pass.
  • §4 Clean-room & attribution: Pass.
  • §5 Evidence: Request changes (CI host-tests failure + exact-head hardware row with full §5 health tuple).
  • §6 Naming: Pass.

Assisted-by: LLM (Gemini 4 Neon)

The submit ioctl copies the whole command stream from userspace before
any parser sees it, and refuses only one longer than the GPU's
max_cmdbuf_size(). G17 never set one, so it kept the default of
usize::MAX: any cmdbuf_size up to 4 GiB was handed to the allocator.
The copy is one kmalloc(), and past KMALLOC_MAX_SIZE (32 MiB with 16K
pages) the page allocator refuses it with a WARN_ON_ONCE(), so
userspace could trigger a kernel warning. Anything below that became a
physically contiguous allocation as large as userspace asked for.

The format has a real maximum, so take it from there. Each entry is a
command header and a payload of at most u16::MAX bytes. A submission
holds at most MAX_COMMANDS_PER_SUBMISSION work commands, and the rest of
the stream sets what the work commands after it use: three attachment
lists, the render resources and the compute resources. A set that a
later one replaces before any work, or one after the last work command,
changes nothing. The largest stream in which every entry has an effect
is therefore 64 work commands, each after at most one set of each of the
five kinds, every entry at its largest: 64 * 6 * (8 + 65535) bytes,
about 24 MiB. A build-time assert keeps it within INT_MAX, the
kvmalloc() limit, and on arm64 also within KMALLOC_MAX_SIZE, which
depends on the architecture's page orders. Longer streams only repeat
sets, so refuse them with E2BIG before copying, as G16 does with its own
4 MiB bound.

The asahi-m4 command-stream selftest builds the G17 parser on the host;
it now also takes ATTACHMENT_KINDS from the source.

A KUnit test parses a submission of 64 largest work commands, checks
that it fits the bound, and checks that a 65th is refused.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
@jgoyvaerts
jgoyvaerts force-pushed the m4pro/g17-cmdbuf-bound branch from deff2b3 to ec6ae20 Compare November 23, 2026 21:56
@jgoyvaerts

jgoyvaerts commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @DjDeveloperr. New head ec6ae209d790.

  • CI: test-command-stream.py now also takes ATTACHMENT_KINDS from g17/command.rs (its g17_constants regex). The asahi-m4 suite passes, and the full run-host-tests.py has no failures beyond those of the base on my machine.

  • COMPILE_TEST: the KMALLOC_MAX_SIZE assert is now #[cfg(CONFIG_ARM64)], and an unconditional MAX_STREAM <= INT_MAX assert sits beside it (g17/command.rs:34-36).

  • J616s row: a DRM_IOCTL_ASAHI_SUBMIT with only cmdbuf_size set returns:

    • ENOENT (queue lookup) at exactly MAX_STREAM;
    • E2BIG at MAX_STREAM + 1, 64 MiB and UINT32_MAX.

    0 kernel warnings. The boot had 0 failed units, 0 error-level kernel messages and 0 GPU recoveries. That kernel has no SELinux (LSMs capability, landlock, yama). Its copy of this change has only the INT_MAX assert, so runtime is the same; it isn't a build of this exact head, because I don't build kernels for the machine.

  • M5: I have none. A row from someone with an M5 would be welcome.

  • Fix the findings of a whole-tree audit: media input checks, storage flushes, DMA isolation, KSM on user4k, SEP and tooling #2755: noted that it closed. The file.rs copies' switch to KVVec follows as its own PR.

Assisted-by: LLM (Claude:Opus 5.5)

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Formal §1–§6 pre-PR re-review at live head ec6ae209d790eb48ced87505ee54bffb2163d945 (base integration/aurora 70a00e583b03; not superseded; live CI host-tests: PASS, MERGEABLE/CLEAN).

Verification of Fixes Since Prior Review

  • CI host-tests fixed: tools/testing/selftests/drivers/gpu/asahi-m4/test-command-stream.py now extracts ATTACHMENT_KINDS in g17_constants, and live host-tests passes cleanly.
  • COMPILE_TEST build assert fixed: drivers/gpu/drm/asahi/g17/command.rs:25-27 now pairs the unconditional MAX_STREAM <= i32::MAX as usize assert with #[cfg(CONFIG_ARM64)] const _: () = assert!(MAX_STREAM <= kernel::bindings::KMALLOC_MAX_SIZE as usize);. Verified against drivers/gpu/drm/asahi/Kconfig that DRM_ASAHI depends on PAGE_SIZE_16KB, so KMALLOC_MAX_SIZE is always 32 MiB >= 24 MiB (25,168,512 B) on CONFIG_ARM64.
  • §5 Hardware evidence updated: J616s (M4 Pro / G17P) testing now reports exact MAX_STREAM boundary checks (MAX_STREAM passes size check to ENOENT; MAX_STREAM + 1, 64 MiB, and UINT32_MAX return E2BIG with 0 kernel warnings / 0 new log lines), vkd3d-proton (544 d3d12 tests) + dEQP-VK query_pool/timestamp (1999 cases) with 0 regressions / 0 refused submissions, and the §5 boot health tuple (0 failed system/user units, 0 error-level kernel messages, 0 GPU recoveries, SELinux N/A / disabled). KUnit arm64 qemu passes 745/746 (1 skip) including the new asahi_g17_stream test. Per §1, the author proved the change on the G17 Mac they own and requested an M5 run.

Non-Blocking Notes

  • The J616s hardware test kernel differed from ec6ae209d790 only in compile-time asserts and host/KUnit tests (identical runtime max_cmdbuf_size() -> 25,168,512); an exact uname / M5 confirmation row remains welcome if available.

§1–§6 Checklist

  • §1 No model gating: PASS — bound derived from the G17 command-stream format (64 * 6 * (8 + 65535)), no chip/board/firmware gates; tested on owned G17 Mac with M5 run requested.
  • §2 Production-ready, no hacks: PASS — default cmdline, no hacks.
  • §3 Safety: PASS — rejects oversized cmdbuf_size with E2BIG before file.rs allocates, preventing the WARN_ON_ONCE(order > MAX_PAGE_ORDER) trigger.
  • §4 Clean-room & attribution: PASS — commit ec6ae209d790 carries human author/committer (Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>) and human Signed-off-by: only, zero AI commit trailers; PR body carries Assisted-by:.
  • §5 Evidence: PASS — KUnit + CI host-tests green + J616s boundary, workload, and §5 health tuple verified.
  • §6 Naming: PASS.

Ready for MERGETRAIN-LINUX4.

Assisted-by: LLM (Gemini 4 Neon)

@jgoyvaerts

Copy link
Copy Markdown
Contributor Author

J616s row on a kernel carrying this PR's exact bound code. Our m4.183 test kernel has the MAX_STREAM definition, both asserts and the G17 max_cmdbuf_size() override, identical to ec6ae209d790.

  • Oversized-submit check: DRM_IOCTL_ASAHI_SUBMIT with only cmdbuf_size set. Exactly MAX_STREAM (25,168,512) passes the size check and fails at the queue lookup (ENOENT). MAX_STREAM + 1, 64 MiB and UINT32_MAX get E2BIG. 0 kernel warnings.
  • dEQP-VK query_pool/timestamp (1999 cases): 0 differences against the same Mesa build on the previous kernel, 0 GPU recoveries.
  • Boot: 0 failed units (system and user), 0 error-level kernel messages, 0 GPU recoveries. No SELinux on this kernel (LSMs capability, landlock, yama).

An M5 row is still open.

Assisted-by: LLM (Claude:Opus 5.5)

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