Skip to content

drm/asahi: g17: keep a queue's frontier off contiguous pages - #2719

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

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

Conversation

@jgoyvaerts

@jgoyvaerts jgoyvaerts commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

A G17 logical queue keeps a frontier: one entry for each command whose completion hasn't signalled yet, plus the entries pruned during the current ioctl (finished). Both were KVecs, so they grow by krealloc() into physically contiguous memory. Only userspace bounds them. vkd3d-proton's test_resolve_image_exhaustive_descriptors records 64 x 1024 resolves in one command list, and the frontier grows to thousands of entries. Growing it then needs an order-5 or order-6 block (512 KiB to 1 MiB with 16K pages).

On a fragmented machine that fails even with plenty of memory free. Seen on a J616s (M4 Pro) during the vkd3d-proton d3d12 suite: 4.3 GB free, nothing above order 4.

  • If the finished reserve in prune_frontier() fails, it falls back and only warn_alloc() dumps a trace.
  • If the frontier's own reserve(1) fails, the submit fails with ENOMEM after some of its commands may already be queued. Mesa then marks the queue lost (VK_ERROR_DEVICE_LOST), and the test reads 0 in every layer.
vkd3d_queue: page allocation failure: order:6, mode:0x40cc0(GFP_KERNEL|__GFP_COMP)
 krealloc_node_align_noprof+0x154/0x238
 rust_helper_krealloc_node_align+0x10/0x20
 ...asahi_core3g175queue...14prune_frontier...
asahi 28a600000.gpu: [File 119992 Queue 1]: IOCTL: submit failed! (submission ID: 38521603 err: ENOMEM)

The change makes both lists KVVecs. Only the submit ioctl reads them, and the queue is dropped in sleepable context, so they don't need contiguous pages. kvrealloc() tries kmalloc first (above PAGE_SIZE without direct reclaim or a warning), then falls back to vmalloc. A small frontier keeps its kmalloc memory; a deep one no longer depends on high-order pages.

Two more changes from review:

  • No half-queued submission. The frontier room for the whole submission (each engine's command count) is reserved once in submit(), before its output fences are published, instead of one entry per command in enqueue(). An allocation failure now fails the submission before any of it is queued, like an ENOMEM in prepare_submission(). Engine::of() replaces the three copies of the command-to-engine match this needed a fourth of.
  • Charged to the caller. Userspace decides how long the frontier gets, and with vmalloc behind it, it can grow past what contiguous pages ever allowed. Both reservations use GFP_KERNEL_ACCOUNT, so a runaway client hits its own memory cgroup's limit.

No uAPI change. The other submit-path vectors are bounded: Scratch holds at most 64 commands per submission, and admission footprints are bounded by drm_sched credits.

Considered and not done:

  • Bounding the frontier would mean throttling or blocking submits.
  • Preallocating at queue creation has no bound to preallocate to.

Testing

  • Reproduced on the J616s on the unfixed kernel by running the test alone on a loaded machine (nothing free above order 3), with perf recording kmem:kmalloc of 32 KiB or more. The frontier's reserve in Queue::submit asked for 196608 bytes (2048 -> 4096 entries) and got NULL (order-4 page allocation failure), and the submit failed with ENOMEM. finished reached 163840 bytes (4096 entries) in the same run. No GPU recovery.
  • KUnit (arm64 qemu, asahi + SEP + ISP config) on this branch: 725 tests, 723 pass, 1 skip. The 1 failure is isp_test_sensor_sysfs, which also fails on the base. No Rust warnings in the changed files.
  • tools/asahi/run-host-tests.py --only g17: 90/90.
  • checkpatch: 0 errors. 1 warning for the quoted kernel log line.

On hardware (J616s, our m4.179 kernel)

  • test_resolve_image_exhaustive_descriptors passes. The frontier peaked at 65,536 entries (3 MiB, through the vmalloc fallback), 16 times the depth where m4.177 failed.
  • The d3d12 list (544 tests) shows no regressions against the same Mesa on m4.177; this test and one other went to Pass. The dEQP-VK query_pool/timestamp list (1999 cases) has 0 differences.
  • Regression row: 0 OOM kills and 0 allocation failures; 0 prune_frontier/submit failed lines; 0 GPU recoveries; 0 SELinux denials; 0 failed units; 0 error-level kernel messages. Details in the PR comment.

Not done

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown
Contributor

The change is right for the failure you hit. Nothing needs the frontier contiguous, and kvrealloc() keeps small queues on kmalloc. Two follow-ups worth folding in while you're here:

  1. A partial submit can still happen. KVVec makes the ENOMEM much rarer, but enqueue() still runs self.frontiers[index].reserve(1, GFP_KERNEL)? per command, after earlier commands of the same submission are already queued. An allocation failure there still leaves a half-queued submission, and Mesa still marks the queue lost. Reserving room for the whole submission once at the top of the submit ioctl (at most 64 per engine, per your Scratch bound) moves any ENOMEM to before the first command is queued, so it becomes a clean, retryable error.

  2. Charge the growth to the caller. The frontier is unbounded and driven by userspace. With vmalloc behind it, one process can now pin far more kernel memory than the old order-6 ceiling allowed. Using GFP_KERNEL_ACCOUNT for the frontier and finished reservations charges it to that process's memcg, so a runaway client hits its own limit rather than the host's.

The J616s d3d12 row you list (0 prune_frontier/submit failed under load) is the right proof. If you also note the frontier's peak length in that run, it shows how far past the old order-4 ceiling it went.

Assisted-by: LLM (Claude:Opus 5.5)

@jgoyvaerts

Copy link
Copy Markdown
Contributor Author

Thanks @RyanTheTide, both are folded into the commit (new head 7b024be439a4, force-pushed):

  1. Whole-submission reserve. submit() now counts each engine's commands and reserves that much frontier room once, before outputs.publish(). The per-command reserve(1) in enqueue() is gone. An allocation failure therefore takes the same path as an ENOMEM from prepare_submission(): nothing is queued or published, and it's a plain retryable error. The count uses a new Engine::of(), which also replaces the three existing copies of that match.
  2. GFP_KERNEL_ACCOUNT for the frontier reserve and for finished.

On the peak length: in the m4.177 reproduction the frontier already held more than 2048 entries. The failing reserve asked for 4096 entries (196608 bytes, order 4), and finished reached 4096 entries in the same run. I'll record the peak from the same probe in the d3d12 run on our next test kernel and add it here with the boot evidence.

KUnit: 725 tests, 723 pass, 1 skip (the 1 failure is isp_test_sensor_sysfs, as on the base). Host tests 90/90, checkpatch 0 errors. Still a draft until that kernel has booted. #2732 is rebased onto this.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown
Contributor

Re-checked at 7b024be4. Both points are fixed:

  • Whole-submission reserve: queue/mod.rs:595-604 reserves each engine's command count once, before outputs.publish() (:613). The count matches what enqueue() consumes on both engines: one push_within_capacity per command into frontiers[Engine::of(command).index()] (:433), no other pushes, and the prunes in between only remove. The count is bounded by the per-submission command limit, so it can't overflow. A failed reserve takes the same path as an ENOMEM from prepare_submission().
  • GFP_KERNEL_ACCOUNT on the frontier reserve (:603) and on finished (:185).

One wording nit for the commit message and PR text: "an allocation failure now fails the submission before any of it is queued" holds for the frontier. Other per-command allocations still run after earlier commands are queued: Completion::new (:280), the dependency vectors (:338-351), Packet::new (:414) and the scheduler enqueue (:431). As before, but small. "The frontier's growth can no longer half-queue a submission" would be exact.

Rows: if this merges ahead of #2732, the J616s m4.177 reproduction at 7b024be4 (0 prune_frontier/submit failed lines, with the peak frontier length), plus a Synchronization/timeline-semaphore CTS row on an A18 Pro. Otherwise the rows at #2732's head cover this commit too.

Assisted-by: LLM (Claude:Opus 5.5)

A logical queue keeps one frontier entry per command whose completion
has not signalled yet, and the pruned entries of the current ioctl in
`finished`. Both are KVecs, so they grow by krealloc() into physically
contiguous memory. The frontier is bounded only by how much work
userspace keeps queued: vkd3d-proton's
test_resolve_image_exhaustive_descriptors records 64 x 1024 resolves
in one command list, and the frontier reaches thousands of entries.

Growing it then needs an order-5 or order-6 block (512 KiB to 1 MiB
with 16K pages). On a fragmented machine (4.3 GB free, nothing above
order 4) the allocation fails. If it is `finished`, prune_frontier()
falls back and only warn_alloc() dumps a trace. If it is the frontier's
own reserve, the submit fails with ENOMEM after some of its commands
may already have been queued, so Mesa marks the queue lost and reports
VK_ERROR_DEVICE_LOST:

  vkd3d_queue: page allocation failure: order:6, mode:0x40cc0(GFP_KERNEL|__GFP_COMP)
  ...
   krealloc_node_align_noprof+0x154/0x238
   rust_helper_krealloc_node_align+0x10/0x20
   ..._asahi_core3g175queue...14prune_frontier...
  asahi 28a600000.gpu: [File 119992 Queue 1]: IOCTL: submit failed! (submission ID: 38521603 err: ENOMEM)

The same test reproduces it alone on a loaded machine (nothing free
above order 3): a perf probe on kmem:kmalloc shows the frontier's
reserve asking for 196608 bytes (2048 -> 4096 entries) and getting
NULL, and the submit fails with ENOMEM.

Nothing needs these arrays contiguous: only the ioctl reads them. Make
both KVVecs. kvrealloc() still tries kmalloc first, for a large size
without direct reclaim or a warning, and falls back to vmalloc. Small
frontiers keep their kmalloc memory, and a deep one no longer depends
on high-order pages.

Reserve the frontier room for a whole submission before its output
fences are published, instead of one entry per command as it is
queued, so that an allocation failure fails the submission cleanly
instead of half-way. Userspace decides how long the frontier gets, and
it can now grow past what contiguous pages ever allowed, so charge it
and `finished` to the caller's memory cgroup (GFP_KERNEL_ACCOUNT).

Fixes: 52ef95b ("drm/asahi: add the T8140 firmware interface")
Fixes: 39bdd5b ("drm/asahi: release pruned G17 frontier entries after the ioctl publishes")
Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
@jgoyvaerts
jgoyvaerts force-pushed the m4pro/g17-frontier-kvvec branch from 7b024be to cde874e Compare November 22, 2026 21:27
@jgoyvaerts

Copy link
Copy Markdown
Contributor Author

Rebased onto integration/aurora 110861f4333e (new head cde874ee627d). The patch is unchanged; it applied cleanly. #2732 conflicted with #2500, so the stack moved together.

Assisted-by: LLM (Claude:Opus 5.5)

@jgoyvaerts

Copy link
Copy Markdown
Contributor Author

Hardware evidence, J616s (M4 Pro), our m4.179 kernel. It carries this commit together with #2732's and #2858's, plus compaction re-enabled in our config. Same machine as the m4.177 runs in the description, and the same system Mesa on both kernels.

  • The failing test now passes. vkd3d-proton test_resolve_image_exhaustive_descriptors ran to the end twice (111 s and 117 s). On m4.177 it stopped at the frontier's ENOMEM.
  • Peak frontier length, as you asked. The frontier's room grew to 3,145,728 bytes, which is 65,536 entries: the whole test (64 × 1024 resolves) queued at once. That's 16 times the 4096 entries at which m4.177 failed, and far past what an order-4 block holds. finished reached 65,536 entries (2,621,440 bytes) in one ioctl. Both grew through kvmalloc's vmalloc fallback without a failure. (Measured with a perf probe on kmem:kmalloc, which kvmalloc's kmalloc attempt hits even when it falls back.)
  • d3d12 list (the 544 tests our Mesa gate runs), compared per test with a run of the same Mesa build on m4.177: 452 pass / 89 skip / 3 fail, against 450 / 90 / 4 before. The two changes are both improvements: this test Fail → Pass, and test_fence_signal_ordering_raced_signal Skip → Pass.
  • dEQP-VK query_pool and timestamp list (1999 cases): 1972 Pass, 27 NotSupported, 0 differ from the m4.177 baseline.
  • Regression row for this boot:
    • 0 invoked oom-killer, 0 page allocation failure, 0 prune_frontier/submit failed lines;
    • 0 GPU recoveries;
    • 0 SELinux denials;
    • 0 failed units (system and user);
    • 0 kernel messages at error level.

Moving this out of draft. Still not tested on the M4, the M4 Max or the M5.

Assisted-by: LLM (Claude:Opus 5.5)

@jgoyvaerts
jgoyvaerts marked this pull request as ready for review November 23, 2026 01:39
@RyanTheTide

Copy link
Copy Markdown
Contributor

Still needed: on integration/aurora at daa9e2d both frontier lists in the G17 queue are still KVecs, and cde874e merges onto that tip without conflicts (it is already based on a tip that has #2500). Both review points are folded in. @jgoyvaerts what is left to take it out of draft is the row you listed: the J616s d3d12 reproduction on this head with no prune_frontier or submit failed lines and the peak frontier length, plus a timeline-semaphore CTS row on a G17 Mac, or the same rows at #2732's head if the two go in together.

@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 checklist review at cde874ee627d75331949bedfdd1c853dfccda797 (host-tests: green; mergeStateStatus: CLEAN; not superseded on integration/aurora).

Code, Safety & Attribution Assessment (0 code blockers)

  • §1 No model gating: Pass — shared G17 queue code with no chip/board/firmware gates.
  • §2 Production-ready / Correctness: Pass — frontiers and finished are moved to KVVec (only accessed and dropped in sleepable ioctl / Queue::drop context), Engine::of(&Validated) cleanly deduplicates the command-to-engine match, and submit() reserves exact per-engine command counts for pending before outputs.publish() (drivers/gpu/drm/asahi/g17/queue/mod.rs:595-604) so frontier growth can no longer fail mid-enqueue. prune_frontier() only removes elements via retain(), and finished.reserve() keeps its safe inline-drop fallback.
  • §3 Safety: Pass — eliminates high-order contiguous allocation failures (order:4..6, VK_ERROR_DEVICE_LOST) under deep queues and charges userspace-driven growth of both frontiers and finished to the caller's memcg via GFP_KERNEL_ACCOUNT.
  • §4 Clean-room & attribution: Pass — commit cde874ee627d carries human author/committer (Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>) and human Signed-off-by: only, zero AI commit trailers; Assisted-by: is at the end of the PR body.
  • §6 Naming / CI: Pass — host-tests green, KUnit (723/725, 1 skip, 1 pre-existing isp_test_sensor_sysfs) and run-host-tests.py --only g17 (90/90) pass.

§5 Blocker Holding APPROVE / MERGETRAIN-LINUX4

  • No standalone exact-head hardware run on cde874ee627d: The J616s m4.179 boot bundled draft PR #2732 and draft PR #2858 alongside this commit. Because #2732 replaces frontiers: [KVVec<(u64, Fences)>; 2] and prune_frontier()'s retain() scan with Outstanding<Fences> / Outstanding::prune(), the m4.179 boot never actually exercised #2719's own KVVec frontier and retain() code path standalone (where 65,536 queued entries still traverse prune_frontier() on every enqueue), and #2732 remains a draft.
  • Required before approval:
    1. An exact-head (cde874ee627d on integration/aurora, default cmdline, without #2732/#2858) J616s hardware + regression row (test_resolve_image_exhaustive_descriptors passing with peak frontier size, d3d12 / timeline-semaphore CTS check, 0 page allocation failure / OOM / prune_frontier / submit failed, 0 GPU recoveries, 0 SELinux denials, 0 failed units, 0 new kernel errors) — or stack/merge together once #2732 is ready.
    2. One additional G17 Mac row (M4, M4 Max, or M5) confirming shared G17 queue behavior with 0 regressions (per §1/§5).

(Non-blocking wording nit: the commit message states an allocation failure fails the submission cleanly instead of half-way; that holds specifically for frontier growth, as other small per-command allocations in enqueue() still run after earlier commands are queued.)

Assisted-by: LLM (Gemini 4 Neon)

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.

3 participants