Skip to content

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

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

lookevink wants to merge 2 commits into
aurora-silicon:mainfrom
lookevink:feat/g18-compute-fastpath

Conversation

@lookevink

@lookevink lookevink commented Dec 2, 2026 •

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.
Tested: At 1d99fe4df987 on the current merge tree, G18 client handoff and job error host checks passed (12/12 and 10/10 mutations caught). 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 · M6 (T8152 / G18G) application compute. The prepare and pool diagnostics retain exact-plan replacement and per-job remapping.
Not yet: Boot and application-compute A/B at 1d99fe4df987 on J873g, including alternating pool plans and same-VM reuse, plus a J804 result.

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 retains the combined capacity when the union fits.
    • 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_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.
  • Diagnostics: g18_app_prepare_test and g18_app_pool_test bypass reuse. The comparison below used a switch in the earlier A/B build.
  • 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 1d99fe4d: not ready yet

Check Detail
CI at head ok 4 checks passed
Review no approved by DjDeveloperr, but GitHub does not count it yet (code owner from Core, Omarchy Mac or Aurora Maintainers needed, or approval went stale)
Requested changes no requested by aurora-silicon-review
Hardware row at head ok comment at 1d99fe4d
Conflicts ok none
PR text ok Risk: two-way · M6 (T8152 / G18G) application compute. The prepare and pool diagnostics retain exact-plan replacement and per-job remapping.

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
Keep exact-plan replacement for diagnostics and pool replacement tests.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@github-actions github-actions Bot added the needs-hw-row No hardware row at the PR head (bot) label Dec 3, 2026
@DjDeveloperr

Copy link
Copy Markdown
Contributor

Pushed 1d99fe4df987 to remove the G18 fast-path module knob while retaining the diagnostic and pool-test bypasses; both G18 host suites passed (12/12 and 10/10 mutations), and RyanTheTide’s approval plus a J873g boot/compute row are due.

@github-actions github-actions Bot removed the needs-hw-row No hardware row at the PR head (bot) label Dec 3, 2026

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

The fast path now selects itself, with the diagnostic and pool-test bypasses intact. The host fixtures caught all 22 mutations. This resolves my earlier change request. I pushed the fix, so a separate maintainer approval is still needed. The J873g boot and compute row is pending.

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

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verdict: FINISH at 1d99fe4

Failure: The high-water pool can fail a dispatch that fits the base driver's exact-plan allocation. The head's Rust plan functions produce two valid four-block plans whose union uses five blocks. The exact replacement can reuse all four retired scratch objects. The union also allocates a fresh 2 MiB object. If that extra allocation fails, application_compute.rs:289–305 and :335–342 propagate the error without trying the exact plan. The retired aliases are still present, so application_compute_unpublished() is false and boot/compute_session.rs:13–19 treats the error as fatal.

Keep the exact-plan path available when an unpublished high-water replacement fails to allocate.

Checked: Ran the descriptor and union arithmetic extracted from this head and traced the four-block and five-block allocations. Also checked the reuse keys for VM identity, pool generation and preemption mode, and the recovery and quarantine invalidation paths.

@github-actions github-actions Bot added the changes-requested A reviewer requested changes (bot) label Dec 5, 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