Skip to content

drm/asahi: g17: attach publication links lazily with a single atomic phase - #4020

Merged
DjDeveloperr merged 3 commits into
mainfrom
feat/g17-publication-lazy-attach
Dec 4, 2026
Merged

DjDeveloperr merged 3 commits into
mainfrom
feat/g17-publication-lazy-attach

Conversation

@erikwb

@erikwb erikwb commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

What: Defer predecessor link attachment in G17 Publication so in-order command completions skip predecessor mutex locks at enqueue and avoid draining eager successor lists on the completion worker. Split out from #3443 (based on @DjDeveloperr's lazy-attachment design) as a standalone change independent of #3681's queue-side link reduction and producer failure log.
Tested: J716c (16" M5 Max, T6050 / G17C) at exact head 76b855c7237e, SLC enabled (32 planes), default command line, Borealis system Mesa: booted cleanly to desktop, Vulkan CTS 25,017 cases (24,985 Pass, 32 NotSupported, 0 Fail, 0 Crash, 0 DeviceLost), 0 GPU faults or recoveries, 0 new kernel errors. Arm64 Asahi KUnit 579 tests (574 passed, 5 skipped, 0 failed); G17 host suite 343/343 passed (g17-publication-lazy-tests: 10/10 including all 9 contract mutations).
Risk: two-way door · G17 Macs (M4, M5, Neo)
Not yet: J700 / M4 hardware rows; combined performance A/B on top of #3681.

Details
  • Lazy deferred links (drivers/gpu/drm/asahi/g17/completion/publication.rs): Prepared::activate() stores pre-allocated predecessor links on the successor (State::deferred_links, Link::target = None so unfinished teardown cannot form an Arc self-cycle) instead of locking every predecessor during enqueue.
  • Single-atomic phase (Publication::phase: AtomicU8): Replaces separate atomic flags with a single 6-state phase (PHASE_PENDING, PHASE_SETTLED_CLEAN, PHASE_FAILING_PENDING, PHASE_FAILED_PENDING, PHASE_FAILING_AFTER_SETTLE, PHASE_FAILED_AFTER_SETTLE).
    • In-order completions fast-path cleanly settled predecessors via one Acquire load (PHASE_SETTLED_CLEAN) without taking the predecessor mutex.
    • claim_locked() atomically clears upstream_released and transitions to PHASE_FAILING_* under state.lock() before publish(Err) signals the error fence off-lock, so late attachments queue on state.successors until state.published is recorded and probes wait until publish(Err) transitions to PHASE_FAILED_*.
    • A timestamp-free publication whose predecessors all settled cleanly (predecessor_error.is_none()) transitions to PHASE_FAILED_AFTER_SETTLE once its error fence has signalled, matching eager settlement (is_pending() == false) even without an intervening probe.
  • Unbounded iterative subscription (Publication::subscribe): When an out-of-order waiter queues on a deferred timestamp-free publication, subscribe() walks an intrusive stack through State::subscribe_next (no recursion, no allocation, no depth bound) so writer completion alone releases the waiter.
  • Bounded single-pass probe (Publication::pending_within): Scans deferred_links in one pass, stopping at the first predecessor not in PHASE_SETTLED_CLEAN, consuming published failures via swap_remove, probing timestamp-free predecessors up to PENDING_PROBE_DEPTH, and releasing already-attached successors on clean settlement.
  • Same-engine status recheck (drivers/gpu/drm/asahi/g17/queue/mod.rs): Rechecks entry.status() < 0 when pending_publication() returns None so an error signalling between those two reads still enters host_waits.
  • Tests: Added KUnit coverage in asahi_g17_publication_order and asahi_g17_cross_engine_timestamps, plus standalone host test tools/asahi/g17-publication-lazy-tests.py (9 behavioral scenarios + 9 contract mutations).
Evidence
  • Exact head: 76b855c7237e7584da4619749f5528443c2efa68 (based on main 37a630623169)
  • Host tests (python3 tools/asahi/run-host-tests.py --only g17): 343/343 passed in 7 s (g17-publication-lazy-tests: 10/10 passed in 0 s).
  • Arm64 Asahi KUnit (./tools/testing/kunit/kunit.py run --arch arm64 --make_options LLVM=1 --kunitconfig drivers/gpu/drm/asahi): Ran 579 tests: passed: 574, skipped: 5 (0 failed).
  • J716c (16" M5 Max, T6050 / G17C) exact-head trial boot:
    • Kernel built from 76b855c7237e, default command line, apple-mcc 2220000000.memory-controller: system level cache allocation enabled on 32 planes, Borealis system Mesa, deqp-vk 7ba03cdac7ae (6 workers, shader cache on):
    • Sweep 1 (targeted sync/query/timestamp, 7 s): 732 cases — 728 Pass, 4 NotSupported, 0 Fail, 0 Crash, 0 DeviceLost, 0 GPU-error dmesg lines (synchronization.smoke.* 11/11, synchronization.basic.* 29/29, synchronization2.op.single_queue.barrier.*_read_image_compute* 412/412, query_pool.occlusion_query.copy_* 159/159, query_pool.concurrent_queries.* 2/2, multiview.queries.* 16/16, pipeline.monolithic.timestamp.basic_graphics_tests.* 80/80, api.copy_and_blit.core.image_to_image.simple_tests.* 19 Pass / 4 NotSupported).
    • Sweep 2 (full query_pool + timeline semaphores + monolithic timestamps, 3m50s): 24,285 cases — 24,257 Pass, 28 NotSupported, 0 Fail, 0 Crash, 0 DeviceLost, 0 GPU-error dmesg lines (query_pool.* 18,242 Pass / 28 NotSupported, synchronization.timeline_semaphore.* 2,880/2,880, synchronization2.timeline_semaphore.* 2,873/2,873, pipeline.monolithic.timestamp.* 262/262).
    • Restored cleanly to the default boot afterwards; trial artifacts removed.

…ic phase

Defer predecessor attachment in Publication so in-order command
completions avoid locking predecessors during enqueue and avoid draining
eager successor lists on the completion worker:

- Keep pre-allocated predecessor links on the successor in
  State::deferred_links with Link::target = None so an unfinished drop
  never forms an Arc cycle.
- Replace separate atomic pending/clean flags with a single AtomicU8
  phase (PHASE_PENDING, PHASE_SETTLED_CLEAN, PHASE_FAILING_PENDING,
  PHASE_FAILED_PENDING, PHASE_FAILING_AFTER_SETTLE,
  PHASE_FAILED_AFTER_SETTLE) so lockless readers in is_pending(),
  link_deferred(), and failed_after_release() never see conflicting
  states. In-order successors skip locking cleanly settled predecessors
  via one Acquire load on PHASE_SETTLED_CLEAN.
- Revoke upstream_released and transition to PHASE_FAILING_* inside
  claim_locked() before publish(Err) signals the error fence off-lock,
  so late attachments wait on state.successors until state.published is
  recorded, queries see is_pending() == true, and probes wait until
  publish(Err) transitions to PHASE_FAILED_*.
- When an out-of-order waiter queues on a deferred timestamp-free
  publication, subscribe the deferred chain iteratively through
  State::subscribe_next (no recursion, no allocation, no depth bound),
  so writer completion alone releases the waiter.
- Bound Publication::is_pending() probing (PENDING_PROBE_DEPTH), consume
  published failures without looping, and release any already-attached
  successors when a probe settles a clean chain.

Based on the lazy publication attachment design from #3443 by
DjDeveloperr, split out cleanly from the queue-side clique reduction
handled independently in #3681.

Add KUnit tests and the g17-publication-lazy-tests.py host test with
contract mutations.

Signed-off-by: Erik Bourget <erik@ebourget.net>
@erikwb
erikwb requested review from a team and DjDeveloperr as code owners December 3, 2026 00:24
@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) blast:neo 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 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Merge readiness at c9ff3ad2: not ready yet

Check Detail
CI at head ok 9 checks passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head ok comment at c9ff3ad2
Conflicts ok none
PR text ok Risk: two-way · G17 Macs (M4, M5, Neo)

Areas: area:gpu

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

RyanTheTide
RyanTheTide previously approved these changes Dec 3, 2026

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

Reviewed 76b855c. Deferred links gain their target after being moved out of the successor. The sentinel covers immediate notifications while the links are attached, and late subscribers wait for publication of a claimed error. The same-engine queue path rechecks completion status before bypassing the host wait. The reported J716c CTS and arm64 KUnit results exercise this head. J700 coverage is pending.

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

Copy link
Copy Markdown
Contributor

Checked 76b855c7237e on top of #3681 (c1fae1db7ecc) and current main. The two are correct together. On top of #3681 the new probe makes submitting slower when many commands are pending, so please look at that before the A/B.

Merge. #3681 should land first. After it, this PR conflicts only in completion/publication.rs, and the resolution is to keep both sides: the imports, and reach beside phase in the struct and in new(). queue/mod.rs merges by itself. I have that merge built (W=1, also with KUNIT=y) and tested (g17 host tests 347/347; 357/357 with #3390 and #3411 as well). I pushed nothing here; the branch needs one forward merge after #3681 lands.

Correctness. I ran the real publication and ordering code of main, #3681, this PR and both together against each other on the host (20,000 random command sequences without failures, 60,000 with failures, 118,000 commands accepted while a failure was being published, plus a threaded run):

pending can now go from false to true again (a settled publication that then fails). #3681's reach assumes it only goes from true to false. In that window a command skips the direct link this PR alone would take. Main doesn't take it either, so nothing gets worse.

Cost (host model on x86, so compare the columns, not the numbers). With #3681 a command links only the newest earlier command, so is_pending() follows PENDING_PROBE_DEPTH publications, each with a lock and a reference, where this PR alone stops at the writer's link. build() asks up to three times per candidate.

µs per enqueue main this PR #3681 both
600 pending behind one unfinished writer 36 48 4 75-125
128 in flight, a writer every 40 5.7 20 2.4 17
32 in flight, a writer every 40 1.1 1.5 0.7 1.9
worst writer completion, 600 in flight 7,900 23 19 1

So on top of #3681 this mostly trades a 19 µs worst completion for a slower submit. Also, after a writer completes with N commands in flight, about N/4 later commands still chain (none on main), because the probe stops four deep and build() now walks newest first.

Settling oldest first before the newest-first walk, with a probe depth of 0, removes the extra chaining and cost 29 µs instead of 74 in the 600 case. That is a quick test, not a patch. The A/B on top of #3681 should include submit time (asahi_g17_submit phase 1 to 2, p50/p99) beside fps.

A behaviour change, not blocking. A writer succeeds, then P fails, then C (waiting for P) succeeds, all accepted before the writer finished: on main C succeeds, here C is cancelled, because C's link to P is still deferred when P fails. It needs a real failure, but it's different from eager settlement unless something probed P in between.

Rows. The J716c result ran without #3681, so it doesn't cover the merged head. Owed: J700, and the A/B. No J773g row: T8132 runs g16/, which uses none of the changed code (T6040 and T6041 do run it).

@erikwb

erikwb commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

We carry this PR in our unified daily kernel across J716c (M5 Max, T6050), J713 (M4 Air, T8132), and J313 (M1 Air, T8103) alongside #3681.

Deferring publication predecessor attachment and tracking publication state in a single atomic phase avoids locking settled predecessors on the in-order submit path and eliminates eager successor-list draining on completion, pairing cleanly with #3681 to lower host submission overhead under async compute on J716c (part of the unified daily stack running Cyberpunk 2077 2.5 T2 at 60.14 FPS with zero GPU faults).

Booted and verified on J716c, J713, and J313 (5/5 Vulkan CTS smoke pass on all three Macs, zero GPU errors).

@github-actions github-actions Bot added ci-failing A check failed at the PR head (bot) merge-conflict Conflicts with the base branch (bot) labels Dec 3, 2026
@DjDeveloperr

DjDeveloperr commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Forward-merged main cff875630c61 as 6ac9e855c3d3. The only PR-diff change in completion/publication.rs is keeping AtomicU64 in the import for main's reach counters. The other resolved hunks retain the existing phase and reach fields and their initializers. On PC, the arm64 W=1 Asahi objects linked with 46 warnings and zero errors, and G17 host checks passed 347/347. The G17 sources and host fixtures are unchanged from the tested tree across the ANE and T8132-only base updates. Neo compared the J700 P2 kit's source blobs and confirmed it covers lazy attachment together with #3681's reach and failure-log behavior. P2 also carries #3390 and #3411, which are present in the P1 control, and uses an older base outside the GPU driver. The P1/P2 hardware comparison is still pending.

Keep main's publication reach tracking alongside the single-atomic
phase. Preserve both the reach initializer and the lazy links.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@github-actions github-actions Bot added needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) and removed ci-failing A check failed at the PR head (bot) merge-conflict Conflicts with the base branch (bot) labels Dec 3, 2026
Signed-off-by: DjDeveloperr <hey@djdev.me>
@github-actions github-actions Bot added the merge-conflict Conflicts with the base branch (bot) label Dec 3, 2026
@DjDeveloperr

Copy link
Copy Markdown
Contributor

Forward-merged main c5bdd6820568 as c9ff3ad23af9. The only conflict was in tools/asahi/run-host-tests.py. Both the direct-compute render-prefix and lazy-publication suites and their mutations are registered. The PR's added and removed lines match the previous head in all four files.

  • python3 -I tools/asahi/run-host-tests.py --jobs 8 --only g17- in the cached Ubuntu 24.04 container, pinned to CPUs 4–23, passed 353/353.
  • ARCH=arm64 ~/bin/aurora-build ./linux-build/build kernel --profile asahi linux-edge4020-1125 -- -j8 W=1 modules_prepare drivers/gpu/drm/asahi/ linked aurora.o with 37 Rust warnings and zero errors.

The queued J700 P1/P2 run still measures lazy attachment on its original base. Current main adds #3429's render-prefix path, so the combination still needs a hardware check. Neo has the source comparison. Fresh approval is due at this head.

@github-actions github-actions Bot removed the merge-conflict Conflicts with the base branch (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.

I pushed the forward merge at c9ff3ad23af9. The conflict resolution keeps both test registrations. Lazy attachment and #3681's reach and failure counters are intact. The affected host tests passed 353/353, the warm arm64 link passed, and the required hosted checks passed. A maintainer who did not push this head still needs to approve.

Neo's sealed J700 P1/P2 pair runs the earlier GPU base. Keep that result and its scope. Coverage for #4020 together with #3429 still needs a J700 run. J714s can supply shared G17 regression coverage.

@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: MERGE at c9ff3ad

Checked: Deferred links start without a reference to their own publication. Attachment reserves a completion sentinel before operating off-lock, and the iterative subscription walk drains each pending link once. Error publication revokes the clean fast path before the fence signals, and same-engine selection keeps a fence that fails during the pending-publication check. The bundled host harness passed and rejected all nine negative controls. I also checked the arm64 link and 353 host checks recorded for this revision.

Follow-ups: The publication-state rewrite and the queue error-fence check are independently reviewable. A future revision could carry them as separate changes.

@RyanTheTide

Copy link
Copy Markdown
Contributor

J616s (16" MacBook Pro, apple,t6040, probes through g17::Gpu / G17P firmware interface; Mesa calls it "Apple M4 Pro (G16S B1)"), stub firmware 26.6.2. A/B PASS at exact head c9ff3ad23af9: no correctness change, and deep pending chains are faster.

  • B = 7.3.0-mrp4020 from head c9ff3ad23af9. A = 7.3.0-mrp4020base from its merge base with main, c5bdd6820568. Both contain drm/asahi: g17: cut the references a pending command holds #3681 (c1fae1db7ecc) and drm/asahi: g17: allow compute_direct() on mixed render/compute contexts #3429 (0663be9696f1). No other patches; same config. Each is a one-shot boot with its own t6040-j616s.dtb (identical bytes in A and B) via aura-try (aura main 70d844e3, U-Boot 5c0d5fe1). The saved default was not changed. Default cmdline. Same userspace in both: system Mesa 26.3.0-devel d4ad4a4dce (Borealis).
  • Boot IDs: B 3b20d4ca-21f6-47a8-9988-9cedc9ef3970, A 29de0954-6802-4e35-893c-b17be440faa3. Internal panel only, AC, GNOME session up (idle).

Correctness (same in A and B)

  • Timeline-dependency CTS, dEQP-VK.synchronization.timeline_semaphore.* (2,880) plus dEQP-VK.synchronization2.timeline_semaphore.* (2,873), 4 processes: 5,753/5,753 Pass in both (0 Fail/Crash/Timeout). Wall time about 13 min (B) and 12 min (A).
  • Deep pending chain, gpubench compute.chain mode=split_submits passes=128: 128 dependent dispatches, each in its own vkQueueSubmit2 chained by a timeline semaphore, at depths 1/4/16, 2 repeats. The output CRC was 89a78526 (= expected) in every configuration of both arms.
  • GPU faults/recoveries/timeouts in the kernel log: 0 in both (before, after the CTS, at end). Kernel err lines: none on B. A had one mmc0: Card is consuming too much power! (SD reader, not GPU).

Timing (µs; per configuration, us_per_submit = window rate, latency/submit CPU = all samples; two repeats shown as r1 / r2)

config metric A base B head
chain d=1 latency p95 7900 / 8252 6661 / 6502
latency median 1953 / 1966 2056 / 1958
submit CPU p95 5999 / 6040 6012 / 5945
us/submit 3205 / 3205 3247 / 3012
chain d=4 latency median 10339 / 54568 9234 / 54290
latency p95 49088 / 54838 54283 / 54486
submit CPU median 2450 / 13509 2162 / 13434
chain d=16 latency median 217992 / 217208 39527 / 42281
latency p95 222989 / 219833 217196 / 220136
submit CPU median 13384 / 13384 2148 / 2197
submit CPU p95 13530 / 13564 13328 / 13458
us/submit 13889 / 13889 3165 / 13158
sync.signal kind=queue sema=timeline us per hop, median (p95) 19.90 (20.93) 16.60 (16.65)
sync.signal kind=host latency median (p95) 154.0 (163.8) 161.4 (167.1)
submit.empty d=1 / d=16 latency median 0.875 / 20.44 0.875 / 20.35

How to read this:

  • At depth ≥ 4 both kernels run in two modes. In one, vkQueueSubmit itself takes about 13 ms (submit CPU median about 13.4 ms, roughly 54 ms latency at d=4 and 218 ms at d=16). In the other it takes about 2 ms. The plain fleet94 kernel shows the same two modes.
  • At d=16 A was in the 13 ms mode in both repeats. B stayed in the 2 ms mode for most samples in both repeats, so its median latency is 5.3x lower and its median submit CPU time 6x lower. B's p95 still contains 13 ms submits, so p95 is unchanged.
  • At d=1, B's latency p95 is 16–21% lower, and the cross-queue timeline hop is 17% faster.
  • At d=4 the modes mix the same way in both arms. The host-signal latency moves +5% (within run-to-run spread).
  • Two repeats per arm on one boot each: treat the d=16 mode split as a strong hint, not a settled distribution.

Afterwards the Mac went back to its production default (fleet94, Enforcing, 0 failed units).

@github-actions github-actions Bot removed the needs-hw-row No hardware row at the PR head (bot) label Dec 3, 2026
@DjDeveloperr
DjDeveloperr merged commit 99ca7de into main Dec 4, 2026
12 checks passed
@DjDeveloperr
DjDeveloperr deleted the feat/g17-publication-lazy-attach branch December 4, 2026 04:08
@DjDeveloperr

Copy link
Copy Markdown
Contributor

Merged c9ff3ad23af9 as 99ca7de1edd4 under the 12-02 last-pusher override after the independent exact-head approval.

The exact-head J616s A/B uses the G17P firmware interface and includes both #3681 and #3429 in both arms. All 5,753 timeline CTS cases passed on each arm, along with the checked deep chains. That covers the shared publication path and the interaction the extra J700 row was meant to exercise. The earlier J716c result covers T6050 at 76b855c7237e.

Merge tree 8001ddec4661 on main ade524730340: warm ARM64 W=1 linked aurora.o with 0 errors and 46 existing warnings, and the G17 host suite passed 402/402, including the lazy-publication test and all nine negative controls.

DjDeveloperr added a commit to jgoyvaerts/linux that referenced this pull request Dec 4, 2026
main's aurora-silicon#4020 rewrote the G17 publication links that this branch keeps
in one charged block. Links now stay deferred on the successor until
its finish(), a waiter that subscribes it, or a probe that finds every
predecessor settled, and the phase is one atomic. That conflicts in
g17/completion/publication.rs and g17/queue/mod.rs.

- publication.rs: main's logic is kept as it is. Only the storage
  changes back to this branch's: all of a successor's links are one
  LinkBlock, a KVVec charged with GFP_KERNEL_ACCOUNT, instead of a KVec
  of (predecessor, KBox<Link>) pairs. A deferred link holds its
  predecessor, and the deferred links are listed through Link::next, so
  finish(), subscribe() and the probe take, consume and settle them as
  main does with its vector. An attached link holds its successor until
  release(), and State::drop releases the links of a predecessor that
  is dropped unpublished, as before.
- queue/mod.rs: main's firmware_published, kept beside the firmware
  waits, is a charged KVVec like them. main's skip of producers that
  were already published inline stays.
- The KUnit case that drops an unpublished predecessor now finishes the
  waiter first, since only then is its link installed in the
  predecessor. It also checks a successor dropped while its links are
  still deferred.
- tools/asahi/g17-publication-lazy-tests.py and
  g17-inline-cross-engine-tests.py match source text that moved:
  add_successor() takes a NonNull<Link>, activate() defers the chained
  links, link_deferred() skips a settled predecessor in a match guard,
  the probe unlinks a failed predecessor's link, and the producer and
  firmware_published pushes use GFP_KERNEL_ACCOUNT. Their models and
  assertions are unchanged.

Signed-off-by: DjDeveloperr <hey@djdev.me>
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) blast:neo 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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants