Skip to content

drm/asahi: g17: allow sibling render completion while direct prepare is lent - #3174

Merged
DjDeveloperr merged 1 commit into
mainfrom
fix/g17-freelist-lease-direct-race
Nov 25, 2026
Merged

DjDeveloperr merged 1 commit into
mainfrom
fix/g17-freelist-lease-direct-race

Conversation

@erikwb

@erikwb erikwb commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

What: Track active render free-list preparations with a counter and a per-generation published flag on Lease, and replace snapshot-based Pools::rollback() with per-preparation cancellation (Lease::cancel_prepare() / Firmware::rollback_render_pool()). Fixes a regression introduced when direct render submission was enabled on M5 (#2955) alongside two render pairs per owner, where a sibling render completing while the other pair was lent off-lock hit Lease::complete_render() -> EINVAL and stopped the GPU runtime.

Tested: J716c M5 Max (ewb-m5, 7.3.0-ewb316+) at a4f42a41076e: boots cleanly into Hyprland (0 new kernel errors); gpubench-vk passes 3/3 render.light (depths=1,4,8, >316k depth-8 submits, 23.4–24.0 us/submit; previously crashed GPU runtime immediately at depth=8), 2/2 queue.rebind (depths=14,84, valid CRCs), queue.churn, compute.light/heavy, render.heavy, concurrent.cr, and sync.signal; gpubench-stress-vk --all --duration 45 --kernel-log passes 16/16 (1 skipped: ray query unsupported) with 0 kernel GPU errors; tools/asahi/run-host-tests.py --only g17- passes 120/120 (including new g17-freelist-lease-direct-tests and 4 negative-control mutations); drivers/gpu/drm/asahi/ compiles cleanly with CONFIG_KUNIT=y and zero warnings in modified files under CONFIG_WERROR=y.

Risk: two-way door · M4 Pro and M5 Macs using the G17 GPU driver (G17P/G17G/G17S/G17C).

Not yet: G17P/G17G/G17S hardware not present on this test bench (covered by J716c G17C hardware tests, KUnit compile check, and host mutation tests).

Details

Root cause

Two physical render pairs bound to one logical context share a single RenderPool and Lease entry (drivers/gpu/drm/asahi/g17/runtime/render/pools.rs). At pipelined render depths above SIBLING_BACKLOG (1), work spreads across both pairs:

  1. render_direct() on pair B calls lend_render() -> pools.prepare(), which transitioned the shared Lease from LeasePhase::Ready to LeasePhase::Preparing, then dropped the device mutex while pair B built and published off-lock.
  2. While pair B was lent, observe_renders() retired a completed render on sibling pair A (not preparing), calling Host::complete_render() -> Pools::completed() -> Lease::complete_render().
  3. Lease::complete_render() rejected LeasePhase::Preparing with EINVAL, propagating out of runtime_event_turn() into GPU event service failed: EINVAL and stopping the GPU runtime (device lost).

Additionally, Pools::rollback() previously overwrote self.leases[slot] with a pre-prepare Option<Lease> snapshot. Once sibling completions are permitted while a preparation is lent, restoring a stale snapshot on rollback would drop sibling completion decrements or observed control-ring release witnesses.

Fix

  • drivers/gpu/drm/asahi/g17/freelist.rs: Replace LeasePhase::Preparing with a preparing: u32 counter and published: bool flag on Lease.
    • Lease::complete_render() permits completion whenever phase == Ready, published == true, and references/commands exceed the 3 * preparing / 2 * preparing reserved by live preparations (preserving the EINVAL guard against retiring an unactivated preparation).
    • Lease::cancel_prepare() undoes one unpublished preparation (preparing -= 1, references -= 3, commands -= 2). If references == 0 and !published, it transitions cleanly to ReleaseSent(cursor) (preserving any unconsumed previous_release witness) or Released (if already observed consumed or never released) without emitting a bogus FreeListRelease. If references == 0 and published, pending_release() exposes the final FreeListRelease for the generation.
  • drivers/gpu/drm/asahi/g17/runtime/render/pools.rs & render.rs / direct.rs / submit.rs:
    • Pools::rollback() delegates to Lease::cancel_prepare() and returns whether a final release is now pending.
    • Firmware::rollback_render_pool() flushes any exposed pending release via release_render_lists(), queues the event worker on EAGAIN, and marks the device lost (lose_device_quietly()) on non-EAGAIN transport/invariant failure so a committed control-ring release record cannot strand on mailbox timeout.
  • Tests: Added KUnit tests in drivers/gpu/drm/asahi/g17/freelist.rs and a registered host mutation test in tools/asahi/g17-freelist-lease-direct-tests.py (tools/asahi/run-host-tests.py).
Evidence

Failure before fix (origin/main 4f1168e1a7ff + open daily PRs, J716c 7.3.0-ewb315+)

gpubench: timeline wait for 8 failed: -4
  render.light depth=1 status=ok
  render.light depth=8 status=fail err=GPU wait timed out or submit failed

aurora 2302600000.gpu: GPU event service failed: EINVAL
aurora 2302600000.gpu: GPU runtime stopped, device lost

Verification at PR head (a4f42a41076e, J716c 7.3.0-ewb316+, Borealis Mesa 2e9a5c1f967)

render.light repro pass 1: depth=1 130.31 us (19176 submits), depth=4 27.76 us (90087), depth=8 23.59 us (105969) — all ok
render.light repro pass 2: depth=1 130.38 us (19166 submits), depth=4 28.08 us (89001), depth=8 24.02 us (104070) — all ok
render.light repro pass 3: depth=1 130.24 us (19180 submits), depth=4 27.63 us (90554), depth=8 23.42 us (106733) — all ok
queue.rebind pass 1:       depth=14 2024.29 us (crc valid), depth=84 815.66 us (crc valid) — ok
queue.rebind pass 2:       depth=14 5102.04 us (crc valid), depth=84 807.75 us (crc valid) — ok
queue.churn compute:       depth=4 8771.93 us (crc valid), depth=16 5208.33 us (crc valid) — ok
queue.churn render:        depth=4 10638.30 us, depth=16 8333.33 us — ok
compute.light:             depth=1 124.07 us, depth=8 14.94 us (100249 submits) — ok
compute.heavy alu:         depth=1 260.42 us, depth=4 135.56 us — ok
render.heavy fill 1080p:   depth=1 189.51 us, depth=4 60.89 us, depth=8 60.13 us — ok
concurrent.cr:             depth=4 overlap=1.0561x — ok
sync.signal timeline:      depth=2 16.9972 us/hop — ok

gpubench-stress-vk --all --duration 45 --kernel-log:
  16 passed, 0 failed, 1 skipped (rq.stress: ray query unsupported) in 77 s -> exit 0
  0 kernel GPU errors/warnings in dmesg; Hyprland uninterrupted (pid 767)

Host tests & build checks at a4f42a41076e

python3 tools/asahi/run-host-tests.py --only g17-freelist-lease
  ok g17-freelist-lease-direct-tests (0.3s)
  ok g17-freelist-lease-direct-tests --mutation reject-preparing-complete (0.3s)
  ok g17-freelist-lease-direct-tests --mutation snapshot-rollback (0.3s)
  ok g17-freelist-lease-direct-tests --mutation drop-pending-flush (0.4s)
  ok g17-freelist-lease-direct-tests --mutation drop-doorbell-loss (0.3s)
  5/5 passed

drivers/gpu/drm/asahi/ compiled with CONFIG_KUNIT=y (clean) and checked under CONFIG_WERROR=y (0 warnings in modified files).

…is lent

Two physical render pairs bound to one logical context share a single
free-list Lease. With direct render submission enabled on M5 (PR #2955)
alongside two render pairs per owner, lend_render() prepares the shared
pool lease and releases the device mutex while the lent pair builds and
publishes off-lock.

Previously Lease encoded preparation as a single scalar phase
(LeasePhase::Preparing), so when the event worker retired a completed
render on the sibling pair while the other pair was lent,
Lease::complete_render() saw LeasePhase::Preparing, returned EINVAL, and
stopped the GPU runtime (reproduced immediately by gpubench-vk
render.light depth=8 on J716c).

Track active preparations with a counter (preparing) and whether at
least one render of the generation has published (published):
- Lease::complete_render() admits completions while sibling
  preparations are lent, while still rejecting completions with EINVAL
  if no published render holds references above the reserved
  preparations.
- Add Lease::cancel_prepare() and update Pools::rollback() to undo only
  the rolled-back preparation instead of restoring a pre-prepare
  snapshot, preserving any sibling completions or observed control-ring
  release witnesses that occurred while lent.
- Add Firmware::rollback_render_pool() so a rollback that drops the
  last reference on an already-published generation immediately flushes
  its pending FreeListRelease (or queues events on EAGAIN).
- Add KUnit coverage in freelist.rs and a host mutation test in
  tools/asahi/g17-freelist-lease-direct-tests.py.

Signed-off-by: Erik Bourget <erik@ebourget.net>
@erikwb
erikwb requested review from a team and DjDeveloperr as code owners November 25, 2026 22:42
@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 a4f42a41: not ready yet

Check Detail
CI at head running running: host-tests, KUnit ane-boot, KUnit dcp-topology, KUnit asahi, KUnit spi-hid-apple, KUnit avd
Review ok approved by DjDeveloperr
Requested changes ok none
Hardware row at head ok in the PR body at a4f42a41
Conflicts ok none
PR text ok Risk: two-way · M4 Pro and M5 Macs using the G17 GPU driver (G17P/G17G/G17S/G17C).

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.

Audited full diff across freelist.rs, pools.rs, render.rs, direct.rs, submit.rs, and the new host/KUnit tests: replacing LeasePhase::Preparing with preparing/published counters cleanly allows published sibling renders to retire while another pair is lent off-lock, still rejects retiring an unpublished preparation with EINVAL, preserves previous_release witnesses on unpublished rollback, exposes pending final releases on published/lingering rollback, and flushes/handles EAGAIN vs transport failure safely in rollback_render_pool(). Holding merge for CI completion and a second G17 generation row (e.g., G17P or G16/M4 Pro).

@github-actions github-actions Bot removed the needs-review No approving review from Core or Omarchy Mac yet (bot) label Nov 25, 2026
@DjDeveloperr

Copy link
Copy Markdown
Contributor

Merging under the P0 regression-fix exception (#2955 GPU runtime stop at pipelined render depth >= 8): 20/20 CI checks green at a4f42a41076e, exact-head J716c M5 Max verification passed (>316k depth-8 submits, 16/16 stress), and clean two-way revert ready. Post-merge J700 (G17P) verification is queued.

@DjDeveloperr

Copy link
Copy Markdown
Contributor

Post-merge check on the MacBook Neo (J700, A18 Pro): the depth >= 8 render runs are clean on main.

Kernel: plain origin/main 16da00248bc9, which contains this PR (9be947aa54c2).

Userspace: system Mesa 26.3.0-30.

gpubench-vk render.light (tri, one queue), µs per submission, two dedicated runs:

depth run 1 run 2
8 37.36 37.79
16 36.08 36.51
32 36.07 36.43

Full row set, 3 reps: render.light d1/4/8/32, queues d2/4/8, compute.light, compute.heavy, copy.bw, idle.wake and rt.scene.

  • All 81 results are ok, and all 35 runs exited 0.

Smoke:

  • Compute CRCs: 5/5.
  • dEQP-VK.synchronization2.op.multi_queue.* (23,760 cases): 4266 Pass, 0 Fail, 0 recoveries.
  • Offline Geekbench 6 Vulkan: 8/8, 0 validation errors.

Kernel log for the whole boot: no GPU fault, timeout, quarantine or runtime-stop lines, no WARN/BUG, no AVC denials, 0 failed units.

Two later test kernels on the same base, which add two pending G17 performance fixes, also ran d8/d16/d32 cleanly (about 29–31 µs) with the same clean log.

@RyanTheTide
RyanTheTide deleted the fix/g17-freelist-lease-direct-race branch November 27, 2026 13:21
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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants