Skip to content

hk: send a G17P host-only event wait after vkSetEvent - #555

Closed
jgoyvaerts wants to merge 1 commit into
aurora-silicon:aurora-wipfrom
jgoyvaerts:pr/hk-g17p-host-event
Closed

jgoyvaerts wants to merge 1 commit into
aurora-silicon:aurora-wipfrom
jgoyvaerts:pr/hk-g17p-host-event

Conversation

@jgoyvaerts

Copy link
Copy Markdown

Problem. On G17P, vkCmdWaitEvents2 is a one-thread compute launch that polls the event word (hk_g17p_meta_wait_addr()). A host vkSetEvent made while that poll runs is never seen, so the wait never ends and the kernel watchdog kills the queue after 30 s.

G17P loads, coherent loads and atomics don't see a host write made while their command runs, only one made before the command starts. Measured with a repro that ends a stuck wait through a GPU set from a second queue:

  • a device acquire fence per poll iteration doesn't help (0–1/3);
  • an atomic poll doesn't help (0/6);
  • a 64 MB CPU cache flush doesn't help;
  • no bind flag gives a less-cached GPU mapping.

Change. A wait whose first synchronization scope is the host stage alone waits on a host-set event (VUID-vkCmdWaitEvents2-pEvents-03839). Its poll now gets a hardware command of its own:

  • If the event is still reset when the submission is built, queue_submit_looped() ends the ioctl before that command. The queue's submit thread sends the rest once vkSetEvent has set the event, so the poll starts after the host write and sees it.
  • Work after the wait on the same queue starts after the set too, as on hardware that blocks the queue at the wait.
  • A per-event syncobj doesn't work here: the kernel resolves an ioctl's input syncs at submit and rejects a point nobody has signalled yet. So the wait is a CPU wait on the submit thread. hk_QueueSubmit2() starts that thread for the first command buffer with such a wait, the same way the runtime does for wait-before-signal.
  • vkDestroyDevice ends a wait whose set never came, so the queue can join its thread.
  • Device-set waits and command buffers without host-only waits are unchanged.

Testing. M4 Pro (G17P), GPU runs guarded, 0 recoveries. New shim test hk-g17p-host-event-wait:

  • It fails on aurora-wip (host wait: 1 ioctls after vkSetEvent ... expected 2 starting with compute) and passes with this change.
  • It also checks that an event already set gives one ioctl, a device-set wait gives one ioctl, the wait doesn't continue the previous command buffer's command, and vkDestroyDevice returns when the set never comes.
  • Removing the merge refusal or the destroy wake-up makes it fail.
Testing detail

Repro (wait seen at once, out of 3):

host set after the poll started before this
0 µs (set before the poll) 3 3
2 ms 0 3
20 ms 0 3

Cost (fill, then wait or barrier, then fill; medians of 300 runs, two rounds), submit call / round trip:

case before this
plain command buffer 10 / 83–85 µs 9–11 / 82–83 µs
device-set wait 9–10 / 80–86 µs 9–10 / 76–77 µs
host wait, event already set 9–11 / 83–88 µs 2–3 / 95–112 µs
host wait, set right after submit (set to fence) 66–74 µs, only when the poll had not started 115–127 µs

A host-only wait costs one extra ioctl plus the submit-thread hop, about 30–45 µs. Plain submissions on a queue that once had such a wait are 9–36 µs slower, because the queue keeps its submit thread. DXVK and vkd3d-proton don't wait on events inside command buffers.

  • dEQP-VK synchronization*.event* (6213): 5779 P / 434 NS, identical to before.
  • 7106 cases (replay, descriptor_buffer, external memory, sparse buffer): identical.
  • api.command_buffers (131): 65 P / 66 NS.
  • vkd3d-proton d3d12 suite: 452 P / 89 S / 3 F, identical per test.
  • dEQP and d3d12 ran on our m4 stack with the same change. meson --suite asahi ran on this branch, rebased onto aurora-wip 67066c92f.

Not covered:

  • A legacy vkCmdWaitEvents whose source mask mixes the host stage with device stages stays on the plain poll.
  • An event set at submit but reset by an earlier vkCmdResetEvent2 in the same submission isn't split.
  • After a device loss without vkDestroyDevice, a held-back wait keeps waiting for the host's set.
  • A queue without a submit thread (MESA_VK_ENABLE_SUBMIT_THREAD=0) keeps the plain poll.
  • Only G17P hardware was tested; virtio wasn't.

Assisted-by: LLM

🤖 Generated with Claude Code

On G17P, vkCmdWaitEvents2 is a one-thread compute launch that polls the
event word (hk_g17p_meta_wait_addr()). A host vkSetEvent made while that
poll runs is never seen: G17P loads, coherent loads and atomics do not see
a host write made while their command runs, only one made before the
command starts, so the wait never ends and the kernel's watchdog kills
the queue. A set made before the poll starts is seen.

A wait whose first synchronization scope is the host stage alone waits on
a host-set event (VUID-vkCmdWaitEvents2-pEvents-03839; vkCmdSetEvent2
cannot name the host stage). Its poll now starts a hardware command of
its own: the stream is not merged with the one before it when the
command buffer ends. If the
event is still reset when the submission is built, queue_submit_looped()
ends the ioctl before that command and sends the rest once vkSetEvent
has set it, so the poll starts after the host write and sees it. Work
after the wait on the queue starts after the set too, as on hardware
that blocks the queue at the wait.

The kernel resolves an ioctl's input syncs when it is submitted, so it
cannot wait for a host signal that comes later; the wait is a CPU wait on
the queue's submit thread. hk_QueueSubmit2() starts that thread for the
first command buffer holding such a wait, as the runtime does for a wait
before signal. vkDestroyDevice ends a wait whose set never came, so the
queue can join its thread. A queue without a submit thread (MESA_VK_ENABLE_SUBMIT_THREAD=0)
or whose submissions run under a lock keeps the plain poll. A legacy
vkCmdWaitEvents mask that mixes the host stage with device stages for
other events stays on the plain poll as well.

Each cut adds one ioctl; submissions without host-only waits are
unchanged.

Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
Assisted-by: LLM
@DjDeveloperr

Copy link
Copy Markdown

Rebased onto aurora-wip 4596393 for the mesa merge train (owner-authorized; authorship kept, conflict resolution only). Old head 190bd13 -> new head 4b25c7f.

One conflict, in queue_submit_recordings() (hk_queue.c, the sibling-queue eligibility): aurora-wip added !packed_bo && (submit packing, #488/#506) where this PR adds !host_waits &&. Kept both: !packed_bo && !host_waits &&. Everything else applied automatically (aurora-wip renamed queue_submit to queue_submit_recordings); git range-diff shows no other change to your commit. G17P is excluded from packing, so the two conditions don't interact there.

Next: independent review, then batch 2 of the train (host build + meson test --suite asahi, Neo G17P row, M5 Pro no-regression gate). Host build check of this head is still pending.

@DjDeveloperr

Copy link
Copy Markdown

Merge train (batch 2): independent review of the rebased head 4b25c7f

Verdict: CLEAN WITH NITS. Independent code-only review (Opus 5.5, xhigh). No blocker or major. The mechanism checks out as traced: the poll's own command, where the ioctl splits, the barriers across the split, the submit-thread locking, and the wake on vkDestroyDevice. The rebase resolution (!packed_bo && !host_waits) is correct, and merged after #564 the hk_queue.c changes compose line for line.

Two MINORs for you, @jgoyvaerts (neither blocks the train; please say whether you want to address them before or after the merge):

  • The decision to hold back is taken when the submission is built (hk_queue.c:1988-1998 hk_event_host_unset, hk_event.c:89-99). If a GPU vkCmdResetEvent2 from an earlier submission hasn't run yet, the event still reads as set, so the new wait goes out at once. The reset then clears it and the poll never sees the next vkSetEvent: the original 30 s hang, in that ordering only.
  • A CPU deadlock where there used to be a GPU timeout (hk_queue.c:1467-1473, :613-621). Sequence: submit a command buffer with a host-only wait, then vkQueuePresentKHR or a sync-file export of its semaphore/fence, and only then vkSetEvent. The SYNC_FD export does a VK_SYNC_WAIT_PENDING wait with UINT64_MAX (vk_semaphore.c:676-680, vk_fence.c:446), and WSI implicit sync calls it on every present (wsi_common_drm.c:190-203). With the ioctl held back that wait never returns, so the thread blocks forever instead of the device being lost.

NITs:

  • the hk_cmd_pending chunk count ignores the new cuts (latent, unreachable today);
  • the alloca for the cuts is unbounded;
  • vkResetEvent writes the event without the lock;
  • the test can't tell the poll from a fill and cites a function that doesn't exist.

Reach: G17P only, on by default there; other chips unchanged.

Train: batch 2 (merge-train/b2). Gate: the Neo (G17P) row incl. synchronization2.op.*event*, synchronization.op.*.event* and your host-event repro, plus the M5 Pro no-regression gate.

@DjDeveloperr

Copy link
Copy Markdown

Merge-train status (02:4x): held for its Neo (G17P) row. The independent review is clean with nits, posted above. This PR reaches the A18 Pro (G17P), so per the train's rule it merges only after both of these:

The Neo has been offline since about 02:04 (it idle-suspended and needs a physical wake), so its row can't run until it is back. Everything else for this PR will be ready by then. G17P owners' rows (A18 Pro / M4 Pro / M4 Max) on the head above would unblock it sooner.

DjDeveloperr added a commit that referenced this pull request Nov 20, 2026
@jgoyvaerts

Copy link
Copy Markdown
Author

Closing this. @DjDeveloperr, please drop it from merge-train/b2. #366, #564, #565 and #446 don't depend on it.

While working on your two MINORs I checked the spec. The usage this PR handles is invalid:

  • VUID-vkSetEvent-event-09543: "event must not be waited on by a command buffer in the pending state". The vkSetEvent page adds that an event a command buffer waits on from the host must be set before that command buffer is submitted.
  • The Queue Forward Progress section says host-set events must be set before the command buffer waits on them. It says implementations would detect the stall and time out, and calls the functionality "defunct".

Valid apps set the event before vkQueueSubmit. The poll then starts after the host write, and that already worked on aurora-wip (3/3 in the repro). This PR helps only the invalid ordering. Both of your MINORs come from that ordering too. For the present / SYNC_FD export case, the PR turns the spec's timeout into a CPU deadlock, and there's no clean fix without a host-signalled in-sync in the uAPI. The need came from our own probe, not from an app or CTS: dEQP synchronization*.event* gives 5779 passes with and without it.

Not doing it is the right fix: aurora-wip keeps its current behaviour, and queues no longer pay for keeping their submit thread. Thanks for the review that led here.

@jgoyvaerts jgoyvaerts closed this Nov 20, 2026
DjDeveloperr added a commit that referenced this pull request Nov 21, 2026
DjDeveloperr added a commit that referenced this pull request Nov 21, 2026
@DjDeveloperr

Copy link
Copy Markdown

@jgoyvaerts, a small request from the merge train: could you rebase this onto current aurora-wip (afcc08b)? It's conflict-free; the train's local rebase gives identical commits.

  • Why: on the current base 4596393 the four assistant-file copies are stale, so agent-policy.sh pr fails on this head. ci: sync the assistant-file copies with .github/aurora/AGENTS.md #580 synced them on aurora-wip, and a rebase picks that up.
  • Maintainer edits are off on this PR, so we can't push the rebase ourselves. Alternatively, turn them on and we'll push it with your authorship kept.
  • Status otherwise: the review is clean with nits (above). The M5 Pro gate is queued. The Neo (G17P) row is pending: this PR reaches G17P, so it merges only after that row. An asahi CI run on the current head is in progress on the DjDeveloperr/mesa mirror (ci/train-pr555).

DjDeveloperr added a commit that referenced this pull request Nov 21, 2026
DjDeveloperr added a commit that referenced this pull request Nov 21, 2026
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