Skip to content

soc: apple: mailbox: take GPU firmware messages in the hard interrupt - #2489

Draft
RyanTheTide wants to merge 1 commit into
mainfrom
perf-j616s/g17-mbox-hardirq
Draft

RyanTheTide wants to merge 1 commit into
mainfrom
perf-j616s/g17-mbox-hardirq

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On the M4 Pro (J616s, T6040) a depth-1 GPU round trip is 18 % slower than macOS on the same machine (fill 720p 198.6 vs 170.0 us, compute.light 111.8 vs 90.9 us). Every GPU completion takes two scheduler hops before its fence signals: the GPU mailbox receive interrupt has no primary handler (oneshot thread only), so the hard IRQ wakes the receive thread, which drains the FIFO, which queues the event work. Every other ASC mailbox drains its FIFO in the hard interrupt.

Change

Add a primary handler for the GPU (ASCWrap v6 GFX/GFX1 role) receive interrupt that drains up to the existing APPLE_G17_MBOX_RX_BUDGET and returns IRQ_HANDLED. A pass that drains the whole budget is treated as a flood and handed to the existing thread, so the flood bound from 9827b6b is kept. ASCWrap v8 keeps the threaded path. Selection is the mailbox's hardware role (from the DT/ADT ASCWrap description), no chip or board list, no parameter.

Testing

J616s, one boot, old and new receive paths interleaved, 5 runs each, gpubench depth 1, submitting thread on a P core:

row threaded rx (before) hard-IRQ rx (after) macOS
fill 720p frame 198.6 us [198.1-199.0] 181.6 us [181.1-181.8] 170.0
compute.light round trip 111.8 us [111.1-111.9] 92.2 us [92.1-93.0] 90.9

compute.light reaches macOS parity (1.01x); fill goes from 1.17x to 1.07x. Boot clean (0 kernel errors, 0 failed units, Enforcing), no GPU faults or timeouts during the rows. Build: drivers/soc/apple/mailbox.o with the fleet config, W=1, no warnings; checkpatch clean (one --no-tree commit-id lookup). KUnit: a flooding FIFO drains exactly one budget in the hard interrupt and is handed to the thread.

Still to run

  • Neo (T8140 G17P) same rows (queued in NEO-QUEUE) before this is a default there; T6050 owners asked for a row.
  • KUnit run on the buildbox (case added; compile only so far).
Reviewer and lane detail
  • The test kernel (7.3.0-aurora-pj6a = fleet75 48cac48 + this change + an unrelated arm64/fpsimd change) carried a temporary runtime switch for the same-boot A/B. That switch is removed in this PR; the code path is otherwise identical.
  • Evidence: traces/perf/20261120-perf-j616s/README.md (A/B section), raw ab-0956/ (gpubench JSON per run).
  • Hop trace on fleet75 before the change (ftrace, ~318 frames): hard IRQ to receive thread 7 us median, waiter wake 10 us; the GPU IRQ is always taken on CPU0.
  • The receive thread's rx_util_min clamp now only applies to floods, so CPU0's cluster no longer gets the RT-thread frequency bump on every completion; the numbers above include that effect.
  • Lane note: work/swarm/lanes/PERF-J616S.md.

Since commit 9827b6b ("drm/asahi: bound repeated timeout recovery
and G17 mailbox stalls") the GPU mailbox (ASCWrap v6 GFX/GFX1 role)
receives through a oneshot threaded interrupt with no primary handler.
Every GPU completion therefore costs two scheduler hops before its
fence signals:

  hard IRQ -> wake irq/N receive thread (SCHED_FIFO) -> context switch
  -> drain FIFO -> RTKit early hook queues the GPU event work
  -> wake the event kworker -> context switch -> signal fences

The thread buys nothing on the common path. The consumer is RTKit's
receive callback, which for GPU event messages only queues the event
work, and it already runs atomically (the thread calls it under rx_lock
with interrupts off). Every other ASC mailbox drains its FIFO in the
hard interrupt.

Add a primary handler for the GPU receive interrupt that drains up to
APPLE_G17_MBOX_RX_BUDGET messages and returns IRQ_HANDLED, so the event
work is queued straight from the interrupt and the receive thread's
wakeup leaves the completion path. A pass that drains the whole budget
is a flood: the handler returns IRQ_WAKE_THREAD, the line stays masked
(IRQF_ONESHOT) and the thread continues with its existing pause after
each full budget, so that commit's flood bound is kept. ASCWrap v8
keeps the threaded path and its storm guard. The receive thread's
minimum-utilization clamp (rx_util_min) now applies only to floods.

Measured on a MacBook Pro M4 Pro (T6040, J616s), one boot, the old and
new receive paths interleaved, 5 runs each, depth-1 GPU round trips
(gpubench, submitting thread on a P core):

  fill 720p          198.6 -> 181.6 us per frame  (-8.6 %)
  compute.light       111.8 ->  92.2 us            (-17.5 %)

macOS on the same machine: 170.0 and 90.9 us.

Add a KUnit case: a flooding FIFO drains exactly one budget in the hard
interrupt and is handed to the thread.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Blockers: 1.

  1. drivers/soc/apple/mailbox.c:986-1001: the new primary handler runs in true hard-IRQ context, and that context is not allowed to take what this path takes. Because the line is IRQF_ONESHOT, irq_setup_forced_threading() (kernel/irq/manage.c:1317) does not thread it, and __handle_irq_event_percpu() (kernel/irq/handle.c:197-199) does not mark it threaded for lockdep. Yet it takes rx_lock, a spinlock_t (include/linux/soc/apple/mailbox.h:73), and runs the whole RTKit receive path. That path includes kzalloc_obj(..., GFP_ATOMIC) (drivers/soc/apple/rtkit.c:1689), kfifo/queue_work and the GPU driver's early hooks. On PREEMPT_RT those are sleeping locks taken in hard IRQ context. On any PROVE_LOCKING kernel (PROVE_RAW_LOCK_NESTING defaults to y on arm64) the first GPU completion gives BUG: Invalid wait context. Before this change the same code ran in the IRQ thread, where spinlock_t is valid. The fix is to make the hard-IRQ part raw-safe: a raw_spinlock_t for the FIFO drain, with the messages handed to a consumer that does not allocate or take sleeping locks. The other option is to keep the receive work in the thread and remove the wakeup cost another way. Prove it with a CONFIG_PROVE_LOCKING boot that runs the gpubench rows with no lockdep report.
  2. mailbox.c:1000 with :1025: during a flood the hard handler drains a full budget, then the thread drains another full budget before its 1-2 ms pause. That doubles the per-cycle bound from 9827b6b (128 messages, 64 of them with IRQs off). The new KUnit case (mailbox-g17-test.h:36-58) only checks the hard half. Either have the thread pause right away when the hard pass used the whole budget, or say in the comment that the bound is now 2x, and test the full hard+thread cycle.
  3. The A/B ran on a kernel with a runtime switch that this head no longer has, and only on the J616s. Every Mac whose GPU mailbox has the ASCWrap v6 GFX/GFX1 role takes this path, and the Neo row is still pending.
  4. Nits: mailbox.c:149 adds an unrelated blank line, and mailbox-g17-test.h:57 has a stray blank line before }. The body also names local queue and evidence paths; keep the numbers and drop the paths.

HW row: head build, plus a PROVE_LOCKING boot, on J616s and Neo J700 (and J713 if its GPU mailbox has the v6 GFX role): gpubench depth-1 fill/compute.light interleaved against the threaded path (N>=5), a GPU flood/recovery row, 0 lockdep reports, 0 GPU faults/timeouts, 0 AVC, 0 failed units.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Review note: the receive budget and the hard/threaded split here are applied to the G17 role only. The other roles still request a hard handler and drain the I2A FIFO without a budget (apple_mbox_poll_locked, budget U32_MAX for non-G17). Non-atomic sends on a mailbox without a send-empty line still use the 500 ms atomic poll there. Could this series apply the budget and the sleeping send poll to every role, keyed on what the mailbox has (send-empty line, role), instead of the G17 check? A per-RTKit cap on queued receive work, with the IRQ held until the worker catches up, would also fit here, so a slow receive worker cannot grow the queue without limit.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant