Skip to content

drm/asahi: g18: keep monotonic compute pool and cache same-VM aliases - #3982

Open
lookevink wants to merge 1 commit into
aurora-silicon:mainfrom
lookevink:feat/g18-compute-fastpath
Open

lookevink wants to merge 1 commit into
aurora-silicon:mainfrom
lookevink:feat/g18-compute-fastpath

Conversation

@lookevink

Copy link
Copy Markdown

What: Eliminate per-dispatch shared compute pool thrashing, UAT pool alias remapping (~526 pages/job), and 48 KiB driver_preempt GEM alloc/free churn on G18G application compute by sizing the compute pool monotonically (G18ComputePlan::covers() / union()) and caching same-VM UAT aliases plus re-zeroed driver_preempt across retired submissions (g18_app_fastpath, default 1).
Tested: At e7fe3d54c (rebased onto b3a5fda63): ARM64 Linux kernel build (make ARCH=arm64 LLVM=-22 W=1 drivers/gpu/drm/asahi/aurora.o, CONFIG_DRM_ASAHI_G18=y, rustc 1.85.0) succeeded with 0 warnings in modified files; host tests tools/asahi/g18-client-handoff-tests.py --mutations (12/12 mutants killed) and tools/asahi/g18-job-error-tests.py --mutations (10/10 mutants killed) passed; standalone unit tests for G18ComputePlan::covers() / union() passed (3/3). Equivalent fastpath implementation hardware-qualified via same-boot A/B (g18_app_fastpath=0 vs 1) on J873g M6 Mac mini (Mac18,5, apple,t8152, G18G B1, Borealis Vulkan): pool reallocations (G18_APP_COMPUTE_POOL_PENDING) dropped 1,104 -> 1, host submit prep (G18_JOB_PHASES) 3-8 us, Qwen2.5-3B Q4_K_M pp128 64.60 -> 92.27 t/s (+43%), pp512 192.57 -> 243.13 t/s (+26%), tg 1.30 -> 2.38 t/s (+83%), coherent output, 0 faults across 300k+ compute submits and a 5-minute soak.
Risk: two-way door · M6 (T8152 / G18G) application compute path only; non-G18 Macs and G18 graphics/render paths are untouched, g18_app_fastpath=0 restores exact-plan pool replacement and per-job remap at runtime, and g18_app_prepare_test/g18_app_pool_test E2E modes bypass the fastpath.
Not yet: Live boot on J873g M6 at the exact rebased head e7fe3d54c (which adds single-read ApplicationComputeKnobs snapshotting, union() chunk-capacity recomputation, and error-rollback/recovery cache clearing on top of the hardware-tested Stage 2 patch), and testing on J804 M6 MacBook Pro.

Details

Root Cause

On G18G application compute dispatches, prepare_application_compute() currently performs three expensive operations on every submission:

  1. Exact-plan shared compute pool replacement: job.pool.shared_compute_plan() != Some(plan) triggers a full replacement pool allocation and firmware descriptor switch whenever consecutive dispatches differ in G18ComputePlan. Multi-kernel compute workloads (such as LLM inference where shaders alternate between 3-block and 4-block scratch plans) thrash between pool sizes on nearly every dispatch (1,104 pool reallocations in a short decode run).
  2. Per-job UAT alias teardown and remapping: job.aliases.clear() unmaps every directory anchor/tail page, block list, and scratch block (~526 UAT entries), then pool.compute_application_aliases() remaps and translation-walks all of them on every dispatch even when the client VM and pool are unchanged.
  3. Per-job driver_preempt GEM churn: job.driver_preempt = None frees the 48 KiB process-VM preemption object at 0x11_0000_0000, and MappedObject::new_wc_in_range_with_guards() allocates, zeroes, maps, and translation-walks a fresh GEM object on every dispatch.

Changes & Safety Invariants

  • Monotonic compute pool (drivers/gpu/drm/asahi/g18/uapi/compute_plan.rs):
    • G18ComputePlan::covers(&self, other) checks every backing, launch-pair, chunk-counter, per-USC chunk, and sma_units dimension.
    • G18ComputePlan::union(&self, other) takes the component-wise maximum across ideal, minimum, and sma_units and recomputes chunks_per_usc/bytes/block_count so backing capacity always covers the combined chunk_counters[1] + sma_units + header (returning None and falling back cleanly to other if a union would exceed G18_COMPUTE_MAX_BYTES). The pool never shrinks under g18_app_fastpath=1.
    • Per-dispatch launch registers (d550/d558 register pair and sma_units) still come from the incoming command's own descriptor and remain bounds-checked against the retained pool.
  • Same-VM cached UAT aliases and reused driver_preempt (drivers/gpu/drm/asahi/g18/initdata/application_compute.rs):
    • Cache hits require all of: knobs.fastpath_active, fail_checkpoint == 0, replacement.is_none(), job.successful_retirement, job.aliases_vm_id == Some(vm.id()) (device-monotonic 64-bit VM ID), job.aliases_pool_gen == binding.generation, job.aliases_preempt_mode == knobs.preempt_mode, !job.aliases.is_empty(), and for preempt_mode == 1 exact live driver_preempt geometry (dp.iova() == driver && dp.logical_size == driver_bytes as usize).
    • Cached kernel mappings live in kernel-managed KernelMapping nodes (refused to userspace VM_BIND via ensure_unmapped / kernel_range()), matching how ApplicationJob retains render aliases across frames for the same VM.
    • On cache hit, job.preempt (mode 0) or job.driver_preempt (mode 1) is zeroed in place via with_bytes_mut prior to self.compute.prepare() (fence(Ordering::SeqCst)) and doorbell publication.
    • Strict invalidation: any VM switch, pool replacement, preempt_mode switch, g18_app_fastpath=0, g18_app_prepare_test, or g18_app_pool_test drops job.aliases and job.driver_preempt before calling pool.compute_application_aliases() (preserving cross-VM scratch zeroing in clear_scratch_for() and the retired < dropped < aliased invariant). Failed preparation attempts also clear cached aliases while the local VM guard is held so application_compute_unpublished() stays true for JobFailure::Refused. Recovery (retire_recovered_application_compute) clears aliases/preempt and cache identity before dropping the VM guard; quarantine (quarantine_application_compute) invalidates cache identity while keeping stranded owners retained.
  • Runtime knob (g18_app_fastpath, 0644, default 1):
    • Follows the existing app_knob! convention in application_compute.rs: read once per submission in ApplicationComputeKnobs::read() (no mid-submit TOCTOU), rejects values > 1 with G18_APP_COMPUTE_REFUSED / -EINVAL, logged in G18_APP_COMPUTE_KNOBS, and bypassed during g18_app_prepare_test / g18_app_pool_test E2E diagnostics.
  • Scope: Orthogonal to drm/asahi: g18: retire jobs sooner and leave the firmware trace off #2109 (boot/recovery.rs, boot/session.rs, initdata/mod.rs, params.rs); zero file overlap.
Evidence

Same-boot M6 hardware A/B (J873g / Mac18,5 / G18G B1, Borealis Vulkan, Qwen2.5-3B Q4_K_M)

Metric g18_app_fastpath=0 g18_app_fastpath=1 Delta
Pool reallocations (G18_APP_COMPUTE_POOL_PENDING) 1,104 1 -99.9%
Kernel host submit prep (G18_JOB_PHASES) per-job remap 3-8 us —
pp128 throughput 64.60 t/s 92.27 t/s +43%
pp512 throughput 192.57 t/s 243.13 t/s +26%
tg throughput 1.30 t/s 2.38 t/s +83%
Output / reliability Coherent Coherent ("The capital of France is Paris."), 0 faults (300k+ submits, 5-min soak) —

Build & host tests at e7fe3d54c

$ make -j8 O=/tmp/g18-fastpath-out ARCH=arm64 LLVM=-22 W=1 drivers/gpu/drm/asahi/aurora.o
  LD [M]  drivers/gpu/drm/asahi/aurora.o   (0 warnings in application_compute.rs / compute_plan.rs)

$ python3 tools/asahi/g18-client-handoff-tests.py --mutations
PASS source: retirement is checked first; previous views are dropped before the pools are aliased
PASS render: 8 frames over 3 VMs, PB growth, target regrowth; no VM sees another's PB blocks, scene scratch, discard page or pool scratch; graph view read-only; same VM clears nothing
PASS compute: 6 submissions over 3 VMs and a replacement pool retaining the scratch objects; no VM sees another's scratch; same VM clears nothing
PASS 12/12 mutants killed

$ python3 tools/asahi/g18-job-error-tests.py --mutations
PASS source: compute classifies a failed preparation by the owner state; nothing of a render is published before prepare()
PASS render: unmapped address, closed VM and failed target allocation fail their job only; the session, its path and the client VMs serve on
PASS render: a first frame refused after its kernel aliases were made is prepared again
PASS render: an unretired job and a crashed firmware stay fatal
PASS compute: only a retired, unbound owner (or an unarmed one not yet built) makes a failed preparation a job failure
PASS 10/10 mutants killed

$ rustc --edition=2021 --test test_compute_plan.rs && ./test_compute_plan
test covers_reflexive_and_ordered ... ok
test union_incomparable_plans_is_monotonic_and_covers_both ... ok
test union_exceeding_max_bytes_returns_none ... ok
test result: ok. 3 passed; 0 failed

On G18G application compute dispatches, prepare_application_compute()
currently does three expensive operations on every submission:

1. Exact-plan shared compute pool replacement: any difference between
   the incoming shader's G18ComputePlan and the retained pool triggers
   a full pool replacement and firmware descriptor switch. Multi-kernel
   compute workloads (such as LLM inference where shaders alternate
   between 3-block and 4-block plans) thrash between pool sizes on
   almost every dispatch, generating 1,100+ pool reallocations per short
   decode run.
2. Per-job UAT pool alias teardown and remapping: job.aliases is
   cleared and every directory page, block list, and scratch block
   (~526 UAT page entries) is unmapped, remapped, and translation-walked
   on every dispatch even when the client VM and pool are unchanged.
3. Per-job driver_preempt GEM alloc/map/free: the 48 KiB process-VM
   preemption object at 0x11_0000_0000 is destroyed and reallocated with
   page-table walks on every dispatch.

Add G18ComputePlan::covers() and G18ComputePlan::union() to size the
shared compute pool monotonically as a high-water mark that never
shrinks, recomputing chunks_per_usc/bytes/block_count so the backing
covers the combined chunk_counters[1] + sma_units + header (falling
back cleanly to the incoming plan if a union would exceed
G18_COMPUTE_MAX_BYTES). Retain the job's UAT pool aliases and
driver_preempt mapping across retired submissions when the client VM
id, pool binding generation, preempt mode, and driver_preempt geometry
all match, re-zeroing the preemption buffer in place before reuse.

Add the 0644 module parameter g18_app_fastpath (default 1; 0 restores
exact-plan pool replacement and per-job remap; other values refuse the
submission), read once per submission in ApplicationComputeKnobs.
Bypass the fastpath during g18_app_prepare_test and g18_app_pool_test
E2E diagnostics, and invalidate cached aliases/driver_preempt on VM
switch, pool replacement, mode change, fastpath=0, preparation error
rollback, recovery, and quarantine.

Measured same-boot A/B on J873g M6 Mac mini (G18G B1, Vulkan via
Borealis, Qwen2.5-3B Q4_K_M, g18_app_fastpath=0 vs 1):
- Pool reallocations (G18_APP_COMPUTE_POOL_PENDING): 1,104 -> 1
- Host submit prep (G18_JOB_PHASES): 3-8 us
- pp128: 64.60 -> 92.27 t/s (+43%)
- pp512: 192.57 -> 243.13 t/s (+26%)
- tg: 1.30 -> 2.38 t/s (+83%)
- Coherent output, 0 faults across 300k+ compute submits and 5-min soak.

Signed-off-by: Kevin loo <kevin@loo.ski>
@lookevink
lookevink requested review from a team and DjDeveloperr as code owners December 2, 2026 19:19
@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (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 Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Merge readiness at e7fe3d54: not ready yet

Check Detail
CI at head ok 4 checks passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes no requested by DjDeveloperr
Hardware row at head ok in the PR body at e7fe3d54
Conflicts ok none
PR text ok Risk: two-way · M6 (T8152 / G18G) application compute path only; non-G18 Macs and G18 graphics/render paths are untouched, g18appfastpath=0 restores exact-plan pool replacement and per-job remap at runtime, and g18apppreparetest/g18apppooltest E2E modes bypass the fastpath.

Areas: area:gpu

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

@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.

One change before merge. drivers/gpu/drm/asahi/g18/initdata/application_compute.rs adds the g18_app_fastpath module knob. New code has to choose its behaviour itself, with no runtime switch. Please make the monotonic pool and the cached aliases the only path (keep the bypass for the diagnostic and pool-test modes), drop the knob, and keep the 0/1 switch in your A/B kit.

I checked covers() and union(), and the cache clearing on error, recovery and quarantine, and found nothing else. pair still comes from the command's own plan, so a larger pool does not trip the descriptor check.

After that, merging needs a J873g boot and compute row at the new head. The numbers in the description come from the earlier equivalent patch, as the Not yet line says.

@github-actions github-actions Bot added the changes-requested A reviewer requested changes (bot) label Dec 2, 2026

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

area:gpu Area from CODEOWNERS (bot) blast:m5 Macs affected, from Merge risk (bot) changes-requested A reviewer requested changes (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants