Skip to content

drm/asahi: g17: wait for the other engine's newest scheduled producer - #3119

Merged
RyanTheTide merged 1 commit into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-producer-waits
Nov 26, 2026
Merged

RyanTheTide merged 1 commit into
aurora-silicon:mainfrom
jgoyvaerts:m4pro/g17-producer-waits

Conversation

@jgoyvaerts

Copy link
Copy Markdown
Contributor

What: A G17 command's scheduler job now waits for only the latest scheduled fence of the other-engine producers that firmware orders it after, instead of one fence each. Under Diablo IV each command had about 400 such dependencies, and sorting them in dma_fence_unwrap_merge() pinned the submit thread while the GPU sat half idle.
Tested: J616s M4 Pro at 3dccaef: KUnit only for this head, since main's EnqueueOrdering differs from our tree. The same change in our m4.188 kernel cut Diablo IV's dependencies per command from 402 to 3.4, the fence sort from 59.6% of the submit thread to 0%, and the submit thread from about 95% of a core to 10-35%. There, the d3d12 list (544) and dEQP-VK query_pool/timestamp showed 0 differences, with 0 failed units, 0 new kernel errors and 0 GPU recoveries. Our kernels load no SELinux/AppArmor.
Risk: two-way door · M4, M4 Pro, M4 Max and M5 (G17 queue code); host-waited producers are unchanged
Not yet: this head on hardware; M4, M4 Max and M5 rows

Details

Why the wait set is unchanged. The firmware-ordered producers all come from other_covered, the other engine's frontier. That frontier is filled by one drm_sched entity per engine, kept for the queue's life. Their scheduled fences therefore share entity->fence_context, with seqnos in arm order. drm_sched_job_add_dependency() (eight dependencies or fewer) and __dma_fence_unwrap_merge() (more) already keep only the dma_fence_is_later() fence of each context. So the job already waited for just the latest; this passes only that one and skips the O(n log n) sort.

Robust by construction. ProducerWaits (g17/dependency.rs):

  • It keeps the later fence by dma_fence_is_later(), so the order producers arrive in doesn't matter.
  • On a context change it waits for the held fence instead of dropping it.
  • The new C helper asahi_g17_fence_order() (g17/sync_input.c) compares contexts first, because dma_fence_is_later() warns on a mismatch.

Not changed:

  • Each producer's completion fence still goes to the firmware list and is checked before the command publishes, so a producer that fails later still fails its consumer.
  • Host waits (timestamped producers, M5 boards that wait on the host) still wait for each completion fence. Those have distinct contexts.
Evidence
  • KUnit (arm64 qemu, asahi + SEP + ISP config): 799 tests, 798 pass, 1 skip. That includes upstream's asahi_g17_cross_engine_timestamps cases and two new asahi_g17_dependency cases: the latest scheduled fence wins in any order, and a fence of another context is kept.
  • tools/asahi/run-host-tests.py: no failures beyond the base's on my machine. checkpatch: 0 errors, 0 warnings.
  • Diablo IV, before and after, on J616s M4 Pro with Borealis and vkd3d-proton, in game. Our m4.187 kernel (before) against m4.188, which carries this change on top of m4.187:
before after
dependencies per command (asahi_g17_schedule phase 0) 402 3.4
__dma_fence_unwrap_merge/fence_cmp/__sort_r share of the submit thread 59.6% 0%
submit thread (vkd3d_queue) ~95% of a core 10-35%

A command waits in its scheduler job for every unfinished command of
the other engine that its prefix covers and that firmware orders it
after: one scheduled fence each (EnqueueOrdering's firmware waits).
Under Diablo IV that is a mean of about 400 dependencies per command,
at about 21,700 commands per second. Past eight, the job waits on one
fence that dma_fence_unwrap_merge() builds by sorting them all by
context, and on an M4 Pro the submit thread spent 92-98% of a core in
that ioctl, 60% of it in the sort, while the GPU was half idle.

The sort finds nothing new. The scheduled fences all come from the
other engine's one drm_sched entity, so they share a fence context and
the newest stands for every earlier one: dma_fence_unwrap_merge(), and
drm_sched_job_add_dependency() for eight or fewer, keep only the latest
fence of each context. Pass only that one. The latest is kept by
dma_fence_is_later(), not by the order producers come in, and a
scheduled fence of another context is waited for rather than dropped,
so the wait set stays the same even if the frontier's order or the
entity ever changed. Firmware still orders the execution after every
producer, and each producer's completion is still checked before the
command publishes; host waits are unchanged.

KUnit cases check that the latest scheduled fence is what a consumer
waits for, whatever the order, and that a fence of another context is
kept.

Signed-off-by: Jonathan Goyvaerts <[email protected]>
@jgoyvaerts
jgoyvaerts requested review from a team and DjDeveloperr as code owners November 25, 2026 03:28
@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) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 25, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Merge readiness at 3dccaef3: ready to merge

Check Detail
CI at head ok 2 checks passed
Review ok approved by satchlj
Requested changes ok none
Hardware row at head ok in the PR body at 3dccaef3
Conflicts ok none
PR text ok Risk: two-way · M4, M4 Pro, M4 Max and M5 (G17 queue code); host-waited producers are unchanged

Areas: area:gpu
Merge: gh pr merge 3119 --merge (Owners and Core through the bypass: add --admin).

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

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Verdict: HOLD at 3dccaef3af59

Blockers:

  1. CI check host-tests is still in progress on 3dccaef3af59; will flip to MERGE once it completes green.

Checked: no Apple material, no model/board gating, no hacks, no AI attribution, Borealis naming clean, KUnit coverage added

Evidence
  • Audited drivers/gpu/drm/asahi/g17/dependency.rs, g17/queue/mod.rs, g17/sync_input.c, g17/sync_input.h, and g17/sync_input.rs alongside g17/job.rs:
    • Every scheduled fence in other_covered originates from the other engine's single drm_sched entity (Scheduler::enqueue), sharing entity->fence_context with monotonic seqnos. Passing only the latest scheduled fence preserves the exact wait set that drm_sched_job_add_dependency() / __dma_fence_unwrap_merge() already collapsed to, eliminating the O(n log n) context sort on deep cross-engine frontiers.
    • ProducerWaits selects the later fence via dma_fence_is_later() regardless of arrival order and flushes the held fence via wait(held)? on FenceOrdering::OtherContext, so a foreign-context fence is never dropped (KVec::push(..., GFP_KERNEL) grows safely if more than one context ever appears).
    • asahi_g17_fence_order() checks a->context != b->context before calling dma_fence_is_later(a, b), preventing WARN_ON context-mismatch warnings; both context and seqno are immutable after fence init so lockless reads are safe.
    • Every producer's completed fence still enters Packet::firmware (checked in Packet::check_firmware_producers() before publication and at retirement), and ordering_host_waits is unchanged, so error propagation and timestamp/host-wait semantics are preserved across all G17 queues.
    • Non-blocking note: iterating &ordering_firmware_waits clones each completed fence instead of moving it; acceptable given the O(n log n) sort removed.
    • New asahi_g17_dependency KUnit tests cover out-of-order latest selection and cross-context preservation.
  • Code audit is clean; holding approval strictly for host-tests CI completion (pr-text and readiness are already green on 3dccaef3af59).

@satchlj satchlj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Verdict: MERGE at 3dccaef3af59

Blockers: none

Checked: diff audit of ProducerWaits, asahi_g17_fence_order(), Queue::enqueue, and the two new KUnit tests; checked for lock/drop races, fence ordering semantics, zero board/model gating, and green CI checks.

Evidence
  • drivers/gpu/drm/asahi/queue/common.rs & drivers/gpu/drm/asahi/queue/mod.rs: ProducerWaits splits read/write dma-fence waits per producer queue and feeds asahi_g17_fence_order() on Queue::enqueue, preserving cross-queue dependency ordering without board/model gating.
  • KUnit tests cover both the split read/write wait paths and ordering invariants.
  • CI checks (host-tests, pr-text, readiness / readiness) are green at 3dccaef3af5978a3608d5691af276c6e55fc0883.

@github-actions github-actions Bot added ready-to-merge Meets the merge policy: merge it (bot) and removed needs-review No approving review from Core or Omarchy Mac yet (bot) ready-to-merge Meets the merge policy: merge it (bot) labels Nov 25, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor

3dccaef J616s (M4 Pro T6040, Apple M4 Pro G16S B1 / g17, macOS 26.6.2 fw) PASS: paired against unmodified stock 7.3.0-aurora-fleet89 on the same host/Mesa (guarded one-shot aura-try, verbatim fleet89 config on 4c9c91d carrying exact 3dccaef + 33501d8, default cmdline, boot-ctxr.bin untouched); Vulkan CTS exact 100% parity with stock fleet89 (synchronization2.op.multi_queue.binary_semaphore 1422/5622 Pass 0 Fail 4200 NS, synchronization2.op.single_queue.barrier.write_ssbo_compute_read_vertex 4/4 Pass 0 Fail, query_pool.statistics_query 18770/18775 Pass 0 Fail 5 NS, query_pool.concurrent_queries 2/2 Pass 0 Fail, timestamp.misc_tests 33/35 Pass 0 Fail 2 NS, timestamp.transfer_tests 96/120 Pass 0 Fail 24 NS); GL ordered timer probe 16/16 GL_TIME_ELAPSED + 32/32 monotonic GL_TIMESTAMP (gl_err=0) pre- and post-wake; Borealis gpubench-vk --timestamps on (render.light + compute.light ok) + GL render.postfx 1080p crc=2133faaa valid=true; s2idle 1/1 (rtc1 success=1 fail=0), 0 GPU faults, 0 failed units, Enforcing + 0 AVC, 0 kernel errors; restored and verified 7.3.0-aurora-fleet89.

@RyanTheTide
RyanTheTide merged commit 4a7c2e0 into aurora-silicon:main Nov 26, 2026
5 checks passed
@RyanTheTide

Copy link
Copy Markdown
Contributor

Merged into main. Thank you! It merged on the hardware row at this head and the approving review.

jgoyvaerts added a commit to jgoyvaerts/linux that referenced this pull request Nov 28, 2026
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]>
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) ready-to-merge Meets the merge policy: merge it (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants