Skip to content

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

Open
erikwb wants to merge 2 commits into
mainfrom
feat/g17-publication-lazy-attach
Open

erikwb wants to merge 2 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 6ac9e855: 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 no no line with the head SHA 6ac9e855, a Mac and a result in the PR body or a comment
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

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: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-hw-row No hardware row at the PR head (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