Skip to content

drm/asahi: g17: cut the CPU cost of a submission - #597

Closed
RyanTheTide wants to merge 4 commits into
aurora-wipfrom
claude/g17-submit-fastpath
Closed

RyanTheTide wants to merge 4 commits into
aurora-wipfrom
claude/g17-submit-fastpath

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Light GPU work at queue depth is CPU bound in the g17 submit path on both the J616s (M4 Pro) and the Neo. On the J616s, render.light kind=tri is 1.35-1.77x slower than macOS at depth 4-64 and compute.light 1.19-1.27x at depth 8-64. ftrace puts DRM_IOCTL_ASAHI_SUBMIT at 17.3 us (render tri d16), 10.0 us (compute d16) and 5.1 us (compute d64), with the GPU idle about 77% of the time at compute d64. perf shows about 82% of the time in the kernel. This series takes the four cheapest hot spots, one commit each, with the correctness argument in each message.

  1. Return a direct render pair without waiting for the device mutex. A direct render publication took the device mutex twice, to lend the pair and to take it back, and spun on it against the event worker (4.6% of samples in mutex_spin_on_owner). A successful publication now returns the pair at once if the mutex is free, otherwise it puts the loan in a per-slot return box and queues an event pass. The next device-mutex holder that needs the pair settles the box with the same return_render(): the owner's next lend, both sections of each event pass, and both stop paths after the leases drain. A boxed pair stays lent (its entry keeps preparing), which every path already handles, and the window is bounded by the queued pass.
  2. Reuse the per-submission vectors. The command and VM-pin vectors of an ioctl and the publication DeferredBatch (64 entries, cleared by init_on_alloc) were allocated on every submission (krealloc 3.75%). Queues and backends now keep them, and drop order on failures is unchanged.
  3. Read the clock once per direct publication. note_submission() and the QoS rx update each read the clock (ktime_get 5.4%, 2.8% from the QoS update alone). Direct render and compute publications now read it once.
  4. Store only the descriptor words that change. write_descriptors() cleared and rewrote about 11 KiB per render into slots that the firmware read last (12.7% inclusive). Both descriptors are now built in a host image kept with the pair, and only the 64-bit words that differ from the slot are stored. The slot ends byte-identical to before whatever it held, so this makes no assumption about what the firmware writes.

G16 (T6040) and G17 (T8140) share all of this code. The G13/G14 backend and G15 are not touched. Nothing new is configurable.

Depends on #585 (the integration/aurora 695d974 sync). The branch is based on its merge commit, so until #585 merges the diff here also shows that sync. The g17 code is identical in integration/aurora 695d974.

Testing

  • Builds with the universal config (fleet9 release.config: Fedora Asahi base + asahi.config): the asahi module of this branch, and of integration 695d974 with the series applied, built against the fleet9 tree. Each commit builds on its own. With CLIPPY=1 there are no new warnings in g17, only the seven that were already there. checkpatch is clean apart from the Co-Authored-By trailer. A full Image and modules build of the branch is queued on the buildbox.
  • Host tests (tools/testing/selftests/drivers/gpu/asahi-m4/run-host-tests.py and tools/asahi/g17-*-tests): the same results before and after. Everything passes except g17-compute-deadline-tests, which fails the same way on the base (its harness fails to compile fixed.rs).
  • Not yet run on hardware. This stays a draft until it has numbers. Per commit the expected effect is about 0.4-0.8 us, 0.2-0.4 us, 0.3-0.5 us and 0.8-2 us per render submit at depth. Commit 4 is the least certain, since it trades stores for compare loads. These are estimates from the profile only. The A/B plan uses gpubench render.light kind=tri and compute.light at depths 1, 4, 16 and 64, three interleaved boots per module, trace.sh ioctl means and a CTS subset on the J616s. The plan and the A/B modules (fleet9 vermagic) are in reports/m4pro-gpu-submit-fastpath-20261030/. The method, raw before and after numbers and the run-to-run spread will go here.

Neo A/B

Hardware: Neo (J700, A18 Pro, G17P B1), kernel 7.3.0-aurora-fleet9, Mesa 26.3.0-devel 108c7218bb (the installed Borealis ICD, VK_DRIVER_FILES set to it on both arms). A is the integration 695d974 asahi module rebuilt the same way (build ID 1c327b87), B is A plus the four commits (build ID eb119626). Both carry fleet9 vermagic and the fleet9 build key (65:E3:85:8E...), the same key as the Neo's own asahi.ko. The module was swapped by an updates/asahi.ko override and a reboot per arm, and the loaded build ID was checked after every boot.

Method. Six boots in the order A B A B A B. Each arm waits for P-core below 50 C, sleeps 60 s, then runs gpubench-vk render.light --param kind=tri and compute.light at depths 1, 4, 16 and 64 (300 ms warm-up, 5 x 250 ms windows, timestamps off), as the session user with the GNOME session idle. Each cell below is the median of the five windows for one boot. trace.sh was not available on the Neo, so there are no ioctl means here.

test d metric A r1 A r2 A r3 B r1 B r2 B r3 A med B med B/A verdict
render tri 1 us_per_submit 117.98 118.65 117.10 103.01 123.82 123.89 117.98 123.82 1.050 noise (B spread 17%)
render tri 4 us_per_submit 34.41 34.46 34.31 34.29 34.18 34.19 34.41 34.19 0.994 small win (-0.6%, ranges do not overlap)
render tri 16 us_per_submit 27.31 27.32 27.31 27.31 27.30 27.33 27.31 27.31 1.000 flat
render tri 64 us_per_submit 27.12 27.12 27.13 27.25 27.13 27.14 27.12 27.14 1.001 flat
render tri 1 submit_cpu_us 11.42 11.12 11.35 10.75 12.18 12.08 11.35 12.08 1.065 noise (B spread 12%)
render tri 4 submit_cpu_us 14.77 14.79 14.71 14.58 14.54 14.79 14.77 14.58 0.987 flat
render tri 16 submit_cpu_us 12.64 12.48 12.21 13.54 13.35 13.00 12.48 13.35 1.070 regression (+7%, ranges do not overlap)
render tri 64 submit_cpu_us 11.14 11.14 11.42 11.46 11.48 11.31 11.14 11.46 1.028 flat to slightly worse
compute 1 us_per_submit 102.21 102.42 102.21 102.00 102.33 102.25 102.21 102.25 1.000 flat
compute 4 us_per_submit 19.82 19.85 19.84 19.82 19.82 19.80 19.84 19.82 0.999 flat
compute 16 us_per_submit 13.58 13.58 13.58 13.58 13.65 13.62 13.58 13.62 1.003 flat
compute 64 us_per_submit 13.84 13.86 13.86 13.88 13.90 13.91 13.86 13.90 1.003 flat (+0.3%)
compute 1 submit_cpu_us 11.58 10.92 11.48 10.52 11.38 9.42 11.48 10.52 0.917 noise (B spread 19%)
compute 4 submit_cpu_us 8.75 8.67 8.43 8.60 8.58 8.62 8.67 8.60 0.993 flat
compute 16 submit_cpu_us 7.54 7.58 7.58 7.50 7.75 7.67 7.58 7.67 1.011 flat
compute 64 submit_cpu_us 5.00 5.04 5.21 5.12 5.38 5.08 5.04 5.12 1.017 flat

On the Neo the series is flat. Throughput at depth 4 to 64 does not move for either test (render sits at 27.1 to 27.3 us per submit and compute at 13.6 to 13.9 us on both arms, so on this machine these rows look bound by something other than the submit ioctl). The only clean movement in submit CPU time is render d16, which is about 0.9 us (7%) worse with B on all three boots. Depth 1 is latency bound and too noisy to call either way.

CTS. One run per module on its first boot (A r1, B r1), Vulkan CTS 1.4.7.0 with deqp-runner 0.23.3, 2 jobs, the vk-default mustpass filtered by the kit's subset regexes (command buffers, sync2 basic and implicit, timeline semaphores, draw renderpass, occlusion queries, compute basic and indirect dispatch, memory pipeline barriers, fragment operations, rasterization primitives; the renderpass2 suballocation, multi-subpass/multi-cmdbuf and pipeline.monolithic.timestamp regexes match no names in 1.4.7.0). 17127 cases. A has 15780 pass, 1347 skip, 0 fail, 0 crash, 0 flake. B has the same, with an identical per-test status list. No asahi errors, faults or timeouts in dmesg on any of the six boots. The Neo's thermal guard paused the CTS at 95 C and resumed it at 85 C on both arms (P-core peak 105.5 C on A, 105.9 C on B during CTS; 78 to 80 C during the bench-only boots).

The raw gpubench JSON, CTS results and per-boot dmesg are in reports/m4pro-gpu-submit-fastpath-20261030/neo-ab/. The Neo is back on its own fleet9 asahi.ko. On this data the PR stays a draft. The render d16 submit CPU increase is worth checking against the J616s numbers, since commit 4 (compare-then-store descriptors) is the one expected to trade stores for loads.

🤖 Generated with Claude Code

RyanTheTide and others added 4 commits October 30, 2026 12:53
…evice mutex

A direct render publication takes the device mutex twice: once to lend
the owner's installed pair, and again to take it back after publishing.
At queue depth the event worker holds that mutex for its completion
passes, so the submitter spins on it twice per command. On the J616s at
render.light kind=tri depth 16 the spin is 4.6% of all CPU samples and
the mutex path 8.3%, with DRM_IOCTL_ASAHI_SUBMIT at 17.3 us per call.

After a successful publication, try the mutex. If it is free, return
the pair as before. If it is busy, put the loan in a per-slot return
box in the shared device state, queue an event pass and leave. The next
holder of the device mutex that needs the pair settles the box by
running the same return_render() the publisher would have run: the
owner's next lend, the start of each event pass (both of its locked
sections), and both stop paths once the preparation leases are
drained. A failed publication still returns its pair at once, since the
device-mutex path may retry it right away.

Why this is safe:
- A boxed pair is still lent. Its entry keeps `preparing` set, so every
  device-mutex path treats it exactly as a pair whose publisher has not
  returned yet, which they already handle (the event worker skips it
  and records a missed event, timeouts defer within their renewal
  bound, close marks it for release on return). Boxing only makes that
  window last until the next settle.
- The window is bounded. The publisher queues an event pass after
  filling the box and that pass settles it first thing, before it scans
  completions, so completions of the boxed pair are not delayed past
  the pass that would have seen them.
- The loan goes into the box before the preparation lease is dropped.
  Recovery and the stop paths drain the leases before they settle, so
  they always see every loan, and recovery never runs with a pair still
  lent by a finished publication.
- A slot is lent once at a time, so its box cell is free whenever a
  publisher fills it. If it ever were not, the publisher returns the
  loan under the mutex as before.
- The box lock nests inside the device mutex and is held only to move
  one loan. Waiters are woken and growth is queued after the settling
  holder drops the device mutex, as the publisher does today. The
  packets dropped while settling are never the last references, since
  each is published and its lent pair cannot have retired it.

The device mutex is then waited for once per contended publication,
for the lend. G16 (T6040) and G17 (T8140) share this code; the G13/G14
and G15 backends are not touched.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each submit ioctl allocates the vector of validated commands and the
vector of VM pins, and each publication allocates a DeferredBatch whose
completion vector holds 64 entries. With init_on_alloc, as in the
distribution configs, each of these is also cleared. krealloc() is 3.75%
of the CPU samples at render.light kind=tri depth 16 on the J616s.

Keep them instead. A queue keeps its two emptied command vectors for its
next ioctl, unless they grew past eight commands. A backend keeps the
finished publication batch for its next publication. The ioctl and the
scheduler publish one owner's commands in turn, so one spare is enough;
a publisher that finds it taken allocates its own, as before.

Nothing else changes. The vectors are emptied at the points where the
old ones were dropped, in the same order: on a validation or pinning
failure the pins go first, then the commands, then the parse pin not yet
moved into the pin vector, and unenqueued commands are dropped when the
ioctl's closure returns. A batch is kept only when it is empty, which
DeferredBatch::finish() leaves it, with its full capacity.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
A direct render publication reads the monotonic clock in
Port::note_submission() and again in the QoS update, which asks the
feed whether the GPU is the bottleneck. On the J616s these reads are
5.4% of the CPU samples at render.light kind=tri depth 16, 2.8% from
the QoS update alone.

Read it once when the publication starts and pass that time to both.
Direct compute publication does the same. The device-mutex hosts and
the completion path still read the clock where they did.

The QoS update compares the time against a GPU-bound state that lingers
for milliseconds, and the idle logic uses the last submission time at
millisecond scale, so a time read a few microseconds earlier in the
same publication gives the same answers.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Each render rewrites its tiling and fragment descriptors in the shared
slot ring: it clears all 0x9c0 + 0x2240 bytes and then writes every
field. The slot was last used 32 commands earlier, and the firmware
read it then, so every store has to take its line back. At
render.light kind=tri depth 16 on the J616s, write_descriptors() is
12.7% of the CPU samples and the clear accounts for most of the 5% in
memset.

Build both descriptors in a cache-aligned host image kept with the
pair, then store into the slot only the 64-bit words that differ from
what it holds. Between commands with the same render state only a few
words change (ordinals, stamps and the per-command addresses), so most
lines are only read. The TA descriptor prefetch now asks for reading
for the same reason; the queue configuration records are still
prefetched for writing.

The slot ends up byte-identical to what the old code left there, the
same clear plus field writes, whatever the slot held before and whoever
wrote it. Nothing else can access the slot meanwhile, since the command
holds its exclusive claim and is not published, so the comparison needs
no assumption about which words the firmware touches. The fragment
slot's MCache spill table is still left alone, and set_mcache() still
writes the slot directly. A descriptor that fails to build leaves the
slot untouched instead of half written, and its claim is released as
before.

The image is 11.6 KiB per pair. Both descriptor variants (25G83 and the
earlier programs) go through the same writers, so this covers G16 and
G17 alike.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

J616s A/C for this PR without commit 4. A = integration 695d974 asahi.ko (build 1c327b87), C = A + commits 1 to 3 (build 01cee971), both on 7.3.0-aurora-fleet9. Each arm was a fresh one-shot boot in the order A C A C A C, with the module loaded from updates/ (fleet9's initramfs rebuilt without asahi so both arms load the same way). The machine was headless (display off), with a 60 s settle before each run and the Borealis overlay ICD from the parity runs.

gpubench-vk, median of 3 boots, each value the median of 5 x 250 ms windows:

test            kind    d metric            A med  A rng%    C med  C rng%    C/A
compute.light           1 submit_cpu_us     15.00    30.4    13.52    20.7  0.901   n=3/3
compute.light           1 us_per_submit     91.17    11.9    92.66     5.8  1.016   n=3/3
compute.light           4 submit_cpu_us     11.22    16.3    11.00    33.3  0.980   n=3/3
compute.light           4 us_per_submit     19.68    11.4    21.29    13.3  1.082   n=3/3
compute.light          16 submit_cpu_us      7.75    29.8     8.19    18.8  1.056   n=3/3
compute.light          16 us_per_submit     11.95     3.2    12.05     1.9  1.009   n=3/3
compute.light          64 submit_cpu_us      5.33    38.3     4.96    17.7  0.930   n=3/3
compute.light          64 us_per_submit     11.82     3.3    11.99     1.6  1.015   n=3/3
render.light    tri     1 submit_cpu_us     19.50    23.3    21.00    20.2  1.077   n=3/3
render.light    tri     1 us_per_submit    106.25     8.1   104.60     2.8  0.985   n=3/3
render.light    tri     4 submit_cpu_us     15.39     6.1    15.42    35.5  1.002   n=3/3
render.light    tri     4 us_per_submit     32.04     1.9    32.45     7.6  1.013   n=3/3
render.light    tri    16 submit_cpu_us     14.94     5.2    14.89    16.5  0.997   n=3/3
render.light    tri    16 us_per_submit     25.33     1.5    25.52     2.0  1.008   n=3/3
render.light    tri    64 submit_cpu_us      8.38    12.4     9.42    33.4  1.124   n=3/3
render.light    tri    64 us_per_submit     25.19     1.0    25.30     1.7  1.004   n=3/3

DRM_IOCTL_ASAHI_SUBMIT mean from the ftrace runs (r2 and r3; the r1 traces were lost to an argument bug in my wrapper):

A r2 A r3 C r2 C r3
render tri d16 18.25 us 15.02 us 14.50 us 15.98 us
compute d16 9.95 us 9.95 us 9.96 us 9.54 us
compute d64 4.26 us 5.21 us 6.01 us 4.64 us

CTS subset (kit cts-subset.txt against the full mustpass, 17285 cases, one run per arm): A 15977 pass / 1308 skip / 0 fail, C 15977 pass / 1308 skip / 0 fail, no flakes, no asahi lines in the kernel log. Peak temperature was 89.5 C during CTS and below 77 C during the benchmarks.

On the J616s, commits 1 to 3 show no regression, but there is no gain I can measure either. All C/A ratios for us_per_submit fall between 0.985 and 1.08, inside the boot-to-boot spread (up to 13%), and submit_cpu_us moves both ways by about as much as its own spread. The estimate in the A/B plan was a 2 to 3% cut in submit ioctl time. That is below what 3 boots per arm can resolve on this machine, so measuring it needs a longer run per arm or a perf profile of the ioctl.

Raw data: reports/m4pro-gpu-submit-fastpath-20261030/j616s-ac/.

🤖 Generated with Claude Code

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Closing this one. Both machines say the same thing: the submit ioctl is not what limits light work at queue depth, so cutting its CPU cost does not move throughput. The branch claude/g17-submit-fastpath stays in place in case the profile changes later.

What the A/B runs showed (median of 3 boots per arm, interleaved):

  • Neo (A18 Pro), A vs all 4 commits: us_per_submit B/A is 0.994 to 1.003 at depth 4, 16 and 64 for both render tri and compute, with boot spread under 0.5%. So the result is a real zero, not noise hiding a gain. CTS subset 15780 pass, 1347 skip, 0 fail on both arms, no asahi kernel lines.
  • J616s (M4 Pro), A vs commits 1 to 3: C/A 0.985 to 1.08, inside a boot spread of up to 13%. CTS subset 15977 pass, 0 fail on both arms. Data in the comment above.

Why it does not help. At depth the time per submission stops falling and sits at a floor that is well above the CPU cost of the ioctl:

Neo us_per_submit d16 / d64 Neo submit_cpu_us d64 J616s us_per_submit d16 / d64 J616s submit_cpu_us d64
render tri 27.31 / 27.12 11.1 25.33 / 25.19 8.4
compute 13.58 / 13.86 5.0 11.95 / 11.82 5.3

Going from depth 16 to 64 changes nothing, and the submitting thread is busy for well under half of each period. If the ioctl were the limit, the period would track submit_cpu_us and more depth would hide the GPU side. Neither happens. The floor is about 25 to 27 us per render job and 12 to 14 us per compute job on both SoCs, so it looks like a fixed per-job cost after the kernel hands the job over: the firmware picking up the work command, running it, and signalling completion, one job after another on the queue. That fits the earlier parity profile, where the GPU was idle about 77% of the time at compute depth 64 while the ioctl took about 5 us.

So the next thing to look at for the light-work gap against macOS is per-job firmware turnaround and batching (more work per firmware command, fewer completion events per job), not the host submit path. A firmware-side trace of job start and end stamps against the submit timestamps would show how the floor splits between firmware scheduling and GPU execution.

Raw data: reports/m4pro-gpu-submit-fastpath-20261030/neo-ab/ and j616s-ac/.

🤖 Generated with Claude Code

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