Skip to content

borealis: order same-queue binary semaphore waits on the GPU - #1176

Merged
RyanTheTide merged 3 commits into
aurora-silicon:aurora-wipfrom
oreater:pr/borealis-gpu-binary-semaphore-ordering
Nov 28, 2026
Merged

RyanTheTide merged 3 commits into
aurora-silicon:aurora-wipfrom
oreater:pr/borealis-gpu-binary-semaphore-ordering

Conversation

@oreater

@oreater oreater commented Nov 27, 2026

Copy link
Copy Markdown

What: Order same-queue binary semaphore waits on the GPU via hk_sync_queue::wait_floor instead of kernel host-scheduler input syncs whenever the kernel advertises DRM_ASAHI_FEATURE_EXACT_PRIOR_BARRIERS and the signalling submission ran on the same primary kernel queue, eliminating host-fence round trips in chained render/overlay and mixed render/compute submissions.

Tested: J716c Apple M5 Max at b98042329d1: 334/334 Clang debug CI tests (--suite asahi --suite poly) PASS, 16-link same-queue binary-semaphore render chain -75.2% (2.694 -> 0.668 ms, 4.04x), 16-link alternating render/compute chain -67.5% (2.455 -> 0.799 ms, 3.07x) with exact pixel/SSBO readback, 216/293 Vulkan CTS PASS across synchronization{,2}.*binary_semaphore* + api.external.semaphore.{opaque_fd,sync_fd} (0 fail, 0 diffs), 0 kernel errors.

Risk: two-way door · M3, M4, M5 (non-serialized queues with DRM_ASAHI_FEATURE_EXACT_PRIOR_BARRIERS) — A plain revert restores kernel input syncs for all binary semaphore waits; G13/G14 (serialized queues without exact_prior) and virtio stay on the existing path. Payload tracking wraps drm_syncobj init/signal/reset/move/import/export (leaving finish unchanged so vk_sync_type_is_drm_syncobj() holds), copy_sync_payloads, empty submits, and vkDestroySemaphore/vkDestroyFence; shared payloads (VK_SYNC_IS_SHARED), sibling-queue runs (gpu_wait_generation), queue replacements, prepended commands, and split submissions conservatively fall back to kernel input syncs.

Not yet: Hardware-tested on J716c Apple M5 Max; needs hardware test runs on M3 (G15) and M4 (G16/G17P). Cross-queue waits, timeline semaphores, and queues that have run a sibling kernel queue keep kernel input syncs.

Details and verification evidence

Summary

  • src/asahi/vulkan/hk_gpu_waits.h, hk_gpu_waits.c, meson.build:
    • Add per-device 32-entry binary vk_sync tag table recording (queue, generation, submitted[VDM/CDM]) for non-shared, non-timeline wrapped drm_syncobj semaphores, gated purely on !dev->dev.is_virtio && (features & DRM_ASAHI_FEATURE_EXACT_PRIOR_BARRIERS).
    • Wrap init, signal, signal_many, reset, reset_many, move (forgetting if either side carries VK_SYNC_IS_SHARED), import_opaque_fd, export_opaque_fd, import_sync_file, and export_sync_file, while preserving finish (vk_drm_syncobj_finish).
    • Wrap vk_device::copy_sync_payloads (hk_gpu_waits_copy_sync_payloads()) to forget signals and consume waits before delegating to vk_drm_syncobj_copy_payloads().
  • src/asahi/vulkan/hk_physical_device.{h,c}, hk_device.{h,c}:
    • Store syncobj_base_type and wrap syncobj_sync_type when DRM_ASAHI_FEATURE_EXACT_PRIOR_BARRIERS is supported on non-virtio devices (asserting vk_sync_type_is_drm_syncobj() stays true).
    • Initialize dev->gpu_waits in hk_CreateDevice() and clean up in both fail_dev: and hk_DestroyDevice().
  • src/asahi/vulkan/hk_sync.{h,c}, hk_queue.{h,c}:
    • Add hk_sync_queue::wait_floor[HK_SYNC_NUM_SQ] applied in resolve_one() and hk_sync_host_flush_prefix().
    • Convert matching same-queue binary waits on non-split, primary, non-replaced, non-prepended submissions into wait_floor, prevent sibling routing while wait_floor is active, tag binary signals before ioctl submission (forgetting on failure), bump queue->gpu_wait_generation on queue restart or sibling use, forget tags on empty submits, and clear tags in hk_DestroySemaphore()/hk_DestroyFence().
  • src/asahi/vulkan/tests/test-hk-sync-prior.c:
    • Add unit tests verifying wait_floor applies to both render (prior + prior_ta) and compute commands across subqueues, preserves tighter in-submission barriers, propagates into hk_sync_host_flush_prefix(), and does not leak to the next submission.

Build & Artifacts

  • Host: J716c Apple M5 Max (G17C B1), Fedora Remix 44, kernel 7.3.0-40.j716c19.fc44.aurora.aarch64
  • Base (origin/aurora-wip 0b8e9b836cc): libvulkan_asahi.so SHA-256 dfeef6f446046502ef9ed398f89657d523b6762fa230798828523f16a85548ad
  • Head (b98042329d1): libvulkan_asahi.so SHA-256 0973af5fd9a61d8a3d641abee24ca067bb9f1d0da335b1ae2c3d41588e252641

Verification

  • CI-matching Clang 22.1.8 debugoptimized (--suite asahi --suite poly, Gallium + Vulkan + drm-shim, assertions enabled): 334/334 PASS, 0 FAIL.
  • Live M5 Max A/B (strict 3-round ABBA = 6 runs per arm, MESA_SHADER_CACHE_DISABLE=1; exact pixel 128,64,32,255, exact SSBO chain arithmetic, wait==signal reuse, empty-submit bridge, and destroy/recreate reuse PASS on every run):
Workload (same-queue binary semaphore chain) Base median Head median Delta
16-link 1024x1024 additive render chain (gfx16) 2.6936 ms 0.6676 ms -75.2% (4.04x)
16-link alternating render/compute chain (mixed16) 2.4554 ms 0.7985 ms -67.5% (3.07x)
32-link tiny already-signalled compute chain (comp32) 0.6527 ms 0.6552 ms +0.4% (within noise)
  • Vulkan CTS (deqp-vk, base vs head, MESA_SHADER_CACHE_DISABLE=1):
    • 293-case slice across dEQP-VK.synchronization.smoke.*, dEQP-VK.synchronization{,2}.basic.binary_semaphore.*, dEQP-VK.synchronization{,2}.op.{single,multi}_queue.binary_semaphore.*, and dEQP-VK.api.external.semaphore.{opaque_fd,sync_fd}.*: 216/293 PASS, 0 FAIL, 77 NotSupported (0 status diffs vs base).
  • Host health: SELinux=Enforcing, 0 DRM/asahi/GPU kernel errors.

When a binary semaphore was last signalled by a submission on the same
queue and the kernel supports DRM_ASAHI_FEATURE_EXACT_PRIOR_BARRIERS,
waiting on it means ordering after the signalling submission's VDM and
CDM ordinals rather than stalling on a host scheduler fence round trip.

- Wrap drm_syncobj payload-changing, init, import, and export operations
  (keeping finish intact so vk_sync_type_is_drm_syncobj() holds) and
  device copy_sync_payloads, plus hk_DestroySemaphore/hk_DestroyFence,
  so any external mutation, shared payload, move of a shared payload,
  empty submit, or destruction invalidates the cached tag.
- Tag binary signals before kernel submission (forgetting on failure),
  and convert matching same-queue binary waits on non-split, primary
  exact-prior submissions into hk_sync_queue::wait_floor applied across
  resolve_one() and hk_sync_host_flush_prefix().
- Keep sibling kernel queue submissions safe via wait_floor guards and
  queue->gpu_wait_generation invalidation on queue restart or sibling use.
- Extend hk_sync_prior unit tests for wait_floor ordering, deferred host
  flush floor propagation, and next-submission isolation.

Signed-off-by: oreater <145754695+oreater@users.noreply.github.com>
@oreater
oreater requested review from a team and DjDeveloperr as code owners November 27, 2026 00:03
@RyanTheTide

Copy link
Copy Markdown

This branch no longer merges into current aurora-wip (d2e883ab2e3): git merge-tree reports a conflict in src/asahi/vulkan/hk_queue.c. Could you merge aurora-wip into the branch (a normal merge, no rebase or force push) and re-run the asahi + poly suites at the new head? I will review the content once it merges cleanly. Several open queue/pack PRs touch the same file, so resolving against the tip as it is now will save a second round.

@RyanTheTide RyanTheTide 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, then merge at b98042329d1ba

Blockers: none

Checked: Read the whole diff (hk_gpu_waits.c/.h, hk_queue.c, hk_sync.c/.h, hk_device.c, hk_physical_device.c/.h, meson.build, the test).

  • Payload changes: init, signal(_many), reset(_many), move, opaque-fd and sync_file import/export, and copy_sync_payloads are all wrapped. Finish is left alone, so vk_sync_type_is_drm_syncobj() still holds. hk's direct drmSyncobjTransfer writes are either the noted signal or the empty-submit path, which forgets the record.
  • Reused addresses: a vk_sync created at a freed address goes through init, which forgets the old record. Records are compared by pointer and never dereferenced, so a stale entry after destroy cannot be used.
  • Temporary imports and threaded submit: shared syncs are refused. A temporary import is a different vk_sync, so its wait keeps the kernel input sync. The threaded-submit vk_sync_move into the wait temporaries happens after the signal was noted, and the record moves with it.
  • Cross-queue and ordering: a record only counts on the same hk_queue and the same generation. The generation is bumped on hk_queue_restart_sync, on sibling use, and while sib.used is set. Split submits, prepended CDM commands and replaced queues all keep the input sync, so a wait from another queue still waits in the kernel. The lock is a leaf, so it cannot deadlock against the queue locks.
  • The floor: resolve_one() applies wait_floor to prior/prior_ta on both subqueues. It is at or below the base, so it is always an absolute prior. hk_sync_host_flush_prefix() honours it. A non-zero floor keeps the waiting submit off sibling queues.
  • Gating: the path depends on DRM_ASAHI_FEATURE_EXACT_PRIOR_BARRIERS plus the queue's own exact_prior, and virtio is excluded. There is no chip gate.
  • Timeline semaphores are untouched.

To finish:

  1. Merge aurora-wip into the branch. In queue_submit_segment(), keep wip's prev_pt / hk_pack_obs_arm() blocks and pass allow_gpu_waits in both queue_submit_recordings() calls. The test-hk-sync-prior.c conflict is two appended tests: keep both. A scratch merge done that way passes the local gate (see Evidence).
  2. Rows owed at the merged head: the path is taken wherever the kernel reports exact prior barriers, which includes G17P and G16, not only M5. Run the binary_semaphore and sync_fd CTS groups from the body on a Neo (G17P) and a J713 (G16), plus a regression Mac. Please also correct the Risk line: it should name what enables the path (the kernel feature and exact_prior queues), not "M3, M4, M5".

Non-blocking:

  • The new test only covers the resolver's wait_floor. Nothing tests the bookkeeping in hk_gpu_waits.c: take across queues and generations, a wait on another queue consuming the record, carrying the record through the threaded-submit move, refusing shared or timeline syncs, and forgetting on a failed submit. A small host test there would protect the part most likely to drop a wait in a later change.
  • Fences also fill the 32-entry ring through note_signals, but no queue ever consumes them. One device-wide mutex is also taken on every reset or signal, including vkResetFences, and once per element in the _many loops.
  • Landing order: this conflicts with #1162 and #1184 in queue_submit_recordings()'s parameter list. #1162 first, then this one.
Evidence
  • Local x86 drm-shim gate (stands in for CI; docker aurora/hk6356-mesa-x86-tests:local, clang release with asserts, meson test --suite asahi --suite poly):
    • base aurora-wip fa8970cda14f: 336 OK / 0 FAIL
    • scratch merge of b98042329d1ba into fa8970cda14f, resolved as above: 336 OK / 0 FAIL, including the 11 new floor: cases in the sync-prior test.

@RyanTheTide

Copy link
Copy Markdown

The 24 h window on the merge request from 11-27 04:20Z has passed, so per our PR fast-track policy I forward-merged aurora-wip (18d3cdc3692d) into this branch as d099116f9f14. It's a normal merge commit, with no rebase or force push.

Conflicts, kept both sides:

  • src/asahi/vulkan/hk_queue.c queue_submit_segment(): both queue_submit_recordings() calls pass your allow_gpu_waits and keep aurora-wip's prev_pt / hk_pack_obs_arm() observation bookkeeping (borealis: budget repeated looping compute packs by observed duration #1186).
  • src/asahi/vulkan/tests/test-hk-sync-prior.c: your wait-floor block and aurora-wip's new blocks (test_vertex_image_bloom(), singleton render-pass forget) both run.

Host check at d099116f9f14 (x86 clang, release + asserts, drm-shim): every Vulkan test passes, including hk_sync_prior. The suite shows 313/339 overall. The 26 failures are all Gallium run-shim.py tests that this harness fails without your change too.

Before merge it goes into the next combined lab regression row (M1 Pro G13S + M4 G16G). If you'd rather resolve it differently, push on top and I'll re-run.

@RyanTheTide

Copy link
Copy Markdown

Second forward merge, a84f45c61103, onto aurora-wip after #1162 landed. Conflicts were the queue_submit_recordings() parameters: #1162 added done_cache and this PR adds allow_gpu_waits. Both are kept (..., extra_bo_count, done_cache, allow_gpu_waits, untouched) at all three call sites, and the wait-floor check in the direct path now calls hk_sibling_packed_atomic_busy(dev, queue, done_cache). Host suite 316/342, every Vulkan test passing including hk_sync_prior (the 26 failures are the Gallium run-shim.py harness ones). It goes into the next combined row before merge.

@RyanTheTide

Copy link
Copy Markdown

Merge-train landing at a84f45c61103.

  • Review: RyanTheTide review-lane APPROVE (no blockers) at b980423; the later commits are my two aurora-wip forward merges (host suite green) (non-author).
  • Feature row: the author's exact-head hardware row on this PR.
  • Regression: combined A/B overlay of aurora-wip plus this PR and the other queued PRs, user space, default kernel command line. M1 Pro (G13S, fleet89): 0 status changes. M4 Pro (G16S, fleet89): 0 regressions. The only flips there are a few synchronization multi_queue timeouts under 4-job load, flipping in both directions. Every flipped case passes alone on both arms, and the base arm shows the same flake. Each Mac ran 26,595 dEQP-VK + 8,153 dEQP-GLES cases, with 0 GPU firmware crashes, Enforcing, 0 failed units. Evidence traces/gpu/20261128-mergetrain-mesa.

Revert: git revert -m 1 <merge commit> on aurora-wip. A regression gets reverted first and fixed after.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants