Skip to content

drm/asahi: g17: a per-VM fence-error count mirror (M5 command-memory reap without a sync_file per fence) - #2059

Draft
DjDeveloperr wants to merge 3 commits into
mainfrom
djdev/g17-vm-fence-error-count
Draft

DjDeveloperr wants to merge 3 commits into
mainfrom
djdev/g17-vm-fence-error-count

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

On M5, Borealis keeps each submission's command memory until its output fences have signalled, then reads every fence's error through a sync_file (export, SYNC_IOC_FILE_INFO, close, plus a syncobj query) to decide whether the memory can be reused or the firmware may still own the work. That read is most of the CPU cost Borealis adds to a light submission. The VM status mirror can't replace it: work cancelled or dropped by another VM's recovery fails its fence without setting the VM's sticky error.

This adds a second, opt-in mirror: a per-VM count of output fences that signalled with an error.

  • uapi (DRM_ASAHI_FEATURE_VM_FENCE_ERROR_COUNT, bit 39, GET_PARAMS group DRM_ASAHI_PARAM_GROUP_VM_FENCE_ERROR_MIRROR, struct drm_asahi_vm_fence_error_mirror): userspace registers one naturally aligned u64 in a BO of its VM, in the shape of the existing VM status mirror. Invalid flags, alignment, bounds, a nonzero tail, a busy slot or a destroyed VM are refused.
  • g17: every error fence installed into a SUBMIT's outputs is counted before it is installed or signals: Submission::member_done before the aggregate takes a member's error, the allocation-failure fence in Outputs::new, and the failed-SUBMIT fallback in Outputs::drop. The word only grows (release ordering), survives the mirror being replaced, and stops being written when the VM or file closes. Kernels without the feature change nothing for userspace.
  • The one documented limit: a G17P cumulative output that inherits an earlier point's error. M5 sends none, and the Borealis user requires non-cumulative outputs.

The user is aurora-silicon/mesa (Borealis, M5 only): aurora-silicon/mesa#390. A submission whose queue point is confirmed reached, with the count unchanged since before its first submit ioctl, is marked succeeded without the per-fence read.

Measured on the M5 Pro (J714s), earlier head of the same change

Same boot, 3 interleaved passes per arm, stock Borealis vs Borealis using the count, gpubench (us per submission, lower is better):

row stock with the count
compute.light d8 / d64 14.77 / 14.81 14.38 / 14.39
render.light tri d16 / d64 26.52 / 27.10 26.00 / 26.46
compute.light, 4 / 8 queues 43.33 / 18.65 29.60 / 16.13

record CPU per submission at compute.light d64: 2.21 -> 1.38 us. 0 GPU faults or recoveries.

Checks

  • This head: integration/aurora 551b937 + the two commits. The feature bit is now 1 << 39: integration took bit 38 for DRM_ASAHI_FEATURE_RENDER_MESH_OUTPUT meanwhile (the only rebase conflict, plus a trivial host-test stub). Fedora compile/link with the M5 config: OK, no new warnings. Host tests g17-fence-error-count-tests.py --mutations and g15-input-error-tests.py --mutations pass.
  • Earlier head (same code on an older integration): KUnit (QEMU arm64, all asahi suites) 291 passed, 1 skipped (unrelated); M5 Pro dEQP-VK synchronization*, synchronization2*, api fence/semaphore (146,727 cases, 16 jobs) with the Borealis user: 0 Fail, 0 lost devices, no kernel lines, in every round.
  • Pending before this leaves draft: the same list on this head on the M5 Pro, and an A18 Pro row (the counting runs in shared g17 code).

🤖 Generated with Claude Code

hk on M5 keeps a submission's command memory pinned until it learns the
status of each of the submission's output fences, and today reads it with
a sync_file per submission (export, SYNC_IOC_FILE_INFO, close). The VM
status mirror cannot stand in for that read: work cancelled (ECANCELED)
or dropped (ENODATA) fails its fences while the VM status stays 0.

DRM_ASAHI_FEATURE_VM_FENCE_ERROR_COUNT (bit 38) and GET_PARAMS group
DRM_ASAHI_PARAM_GROUP_VM_FENCE_ERROR_MIRROR (4) register a 64-bit word,
in a GEM object of the calling file, that counts the VM's SUBMIT ioctls
whose output fence ended with an error, of any errno, one per such
SUBMIT whether or not it named output syncs, including a failed SUBMIT
that installs an error fence instead. The kernel updates the word before
it installs or signals each such fence, and the count only grows. A
client that samples the word before a submission's first SUBMIT and finds
it unchanged once all of that submission's output fences have signalled
knows that none of them failed, provided the submission has no
DRM_ASAHI_SYNC_TIMELINE_SYNCOBJ_CUMULATIVE output (such an output can
carry an earlier point's error without a new count). So it can skip the
sync_file reads when nothing failed.

struct drm_asahi_vm_fence_error_mirror is { vm_id, handle, offset, flags }:
the offset is 8-byte aligned, flags are zero, and bytes of a longer
request past the struct must be zero. Each VM takes one registration;
the kernel keeps its own reference to the object until the VM is
destroyed. Devices that do not count fence errors return EOPNOTSUPP.

The VM status mirror's documentation now says what the code does: the
kernel keeps its reference to the object until the VM is destroyed, not
for the lifetime of the VM status; registration stores the VM's first
reported error in the word (0 if none) rather than zeroing it; and a VM
that does not exist or is being destroyed fails registration with ENOENT.

Signed-off-by: Diljit Singh <[email protected]>
hk on M5 keeps command memory pinned until it learns each submission's
fence status, and today reads it with a sync_file per submission. A
per-VM count of failed output fences, mirrored to a word hk registers,
lets it skip that read when nothing failed.

The count lives in the VM status, next to its sticky error and the status
mirror. The mutex that guards the status mirror now guards the count and
its word too.

Why the count can replace the read:
- Every error fence a g17 SUBMIT installs in its outputs is counted, at
  one point per kind of fence, both in g17/fence.rs:
  - A published submission's outputs hold the aggregate fence of its
    ioctl, and Submission::member_done is the only place that sets that
    fence's error and signals it. Completion, recovery, quarantine,
    cancellation, timeouts, device loss, completions failed before they
    reach the firmware, dropped members and ioctl errors after
    publication all end there. It counts in its error branch, on the
    final error (a member can note the error before any member
    completes), once per fence.
  - A SUBMIT that fails before publication installs an error fence
    through Outputs instead: the queue's allocation-failure fence
    (Outputs::new) or the fallback fence carrying the ioctl's error
    (Outputs::drop), ERESTARTSYS included. Both count before installing
    it. A restarted SUBMIT adds a second link at the same timeline point,
    which a syncobj query can report as reached while earlier work still
    runs; that link always follows a count change, so every submission
    sampled before it takes the slow path.
  A G17P cumulative output can also carry an earlier point's error, which
  only that point's own submission counts; M5 sends no cumulative output.
- The count is stored before the fence is installed or signals. The
  increment and the store of the new count to the client's word happen
  under the mutex first, and the signal's fully ordered test_and_set_bit
  orders them before the fence reads as signalled. The kernel stores its
  own value with a single store and never reads or modifies the client's
  word.
- The u64 cannot wrap in practice: it grows by one per failed SUBMIT.
  The increment is a wrapping add only so that it can never panic.
- Release at VM drop. The object reference and mapping go where the
  status mirror's do, when the client's VM handle is dropped, not with
  the status, which retained work can keep alive. Later failures are
  still counted but stored nowhere. A registration of either mirror that
  races with the VM's destruction is now refused (ENOENT), so a released
  status holds no object and its last put, which can come from a fence
  callback in atomic context, sleeps nowhere.

Submission::new captures the VM status from its context, and Outputs
takes it from the queue's context. Only the g17 backend advertises
DRM_ASAHI_FEATURE_VM_FENCE_ERROR_COUNT and accepts the registration,
through a new Gpu::supports_vm_fence_error_count that defaults to false.
The base M5 backend signals its own fences outside these points, so it
does not. A failed fence of a VM without a registration costs one
increment under the mutex. A successful SUBMIT adds only four
reference-count operations: Outputs and the published Submission each
take a reference to the VM status and drop it later.

GET_PARAMS group 4 requires zero flags, a nonzero handle, an 8-byte
aligned offset and zero bytes past the struct in a longer request (at
most 4 KiB), looks the VM and the GEM object up in the calling file, maps
the object with the status mirror's checks (at most 64 KiB, the word
inside it), refuses a second registration with EBUSY, and sets the word
to the current count before keeping it. Other backends return EOPNOTSUPP.

Tests: KUnit cases in g17/fence.rs and g17/status.rs. A new host test,
tools/asahi/g17-fence-error-count-tests.py, runs the production
member_done, Outputs and VM status bodies on models. It checks that
errors are counted and successes are not, that the word is stored before
a published fence signals and before an unpublished one is installed,
registration and release of both mirrors, and concurrent counts; with
--mutations, ten source changes must each fail their own check. The g15
input-error host test models the status mirror mutex's new contents.

Signed-off-by: Diljit Singh <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor

Blockers: 2.

  1. tools/asahi/run-host-tests.py:146: the new tools/asahi/g17-fence-error-count-tests.py is not in the runner's table, so host-tests fails on this head ("not registered in run-host-tests.py: tools/asahi/g17-fence-error-count-tests.py", exit 2; the runner refuses unregistered scripts at line 272). Add an entry like the g15 one, with both the plain and the --mutations run, so the mutation check the PR relies on runs in CI.
  2. Commit 14f8778 ("drm/asahi: uapi: add a per-VM fence-error count mirror") still says the feature is bit 38, but include/uapi/drm/asahi_drm.h:546 defines it as 1 << 39 (bit 38 is DRM_ASAHI_FEATURE_RENDER_MESH_OUTPUT). For a uapi commit the message is the reference people read; fix it to bit 39. Both commit messages also name the userspace driver "hk"; it is Borealis.

The counting itself looks right: fence.rs:285 counts in member_done's final-error branch before set_error/signal, fence.rs:558 before the allocation-failure fence is installed, and fence.rs:624 before the fallback is set and signalled; a publish() that fails after Submission::new is counted through finish_enqueue, and one that fails in Submission::new through the fallback. The word is written only under the status mutex, and both registrations now refuse a closing VM. The PR body ends with "Generated with Claude Code"; it should end with an Assisted-by: LLM (...) line.

HW row: this head on the M5 Pro (J714s) with the Borealis user from mesa#390: dEQP-VK synchronization*, synchronization2*, api fence/semaphore at 0 Fail / 0 lost devices, the gpubench light rows, and a forced-error run (one cancelled + one recovered submission) that shows the word move. Also a no-regression run on one other g17-backend Mac (A18 Pro or M4 Pro) on the default command line, since member_done/Outputs are shared g17 code: same CTS groups, 0 GPU faults, 0 AVC, 0 failed units, 0 new kernel errors.

Assisted-by: LLM (Claude:Opus 5.5)

satchlj added a commit that referenced this pull request Nov 21, 2026
The G14 firmware-27.2 runtime refuses render commands, but nothing in
GET_PARAMS says so. Graphics userspace therefore takes the device for
GL: on phellos (J413) with the GPU published, kmsro picked the aurora
render node for the internal panel, and the greeter's Hyprland and
sddm-greeter-qt aborted after their first renders failed with EINVAL.

Add DRM_ASAHI_FEATURE_COMPUTE_ONLY (bit 41; bits 39 and 40 are claimed by
open PRs #2059 and #2523) and set it on that runtime. It is a property
of the device, not of a board or chip list: Mesa can decline to create
a graphics screen or a graphics queue on a device that reports it, and
fall back to software rendering, while compute keeps working. Kernels
without the bit keep their current behaviour.

Signed-off-by: Satya Benson <[email protected]>
satchlj added a commit that referenced this pull request Nov 21, 2026
The G14 firmware-27.2 runtime refuses render commands, but nothing in
GET_PARAMS says so. Graphics userspace therefore takes the device for
GL: on phellos (J413) with the GPU published, kmsro picked the aurora
render node for the internal panel, and the greeter's Hyprland and
sddm-greeter-qt aborted after their first renders failed with EINVAL.

Add DRM_ASAHI_FEATURE_COMPUTE_ONLY (bit 41; bits 39 and 40 are claimed by
open PRs #2059 and #2523) and set it on that runtime. It is a property
of the device, not of a board or chip list: Mesa can decline to create
a graphics screen or a graphics queue on a device that reports it, and
fall back to software rendering, while compute keeps working. Kernels
without the bit keep their current behaviour.

Signed-off-by: Satya Benson <[email protected]>
satchlj added a commit that referenced this pull request Nov 22, 2026
The G14 firmware-27.2 runtime refuses render commands, but nothing in
GET_PARAMS says so. Graphics userspace therefore takes the device for
GL: on phellos (J413) with the GPU published, kmsro picked the aurora
render node for the internal panel, and the greeter's Hyprland and
sddm-greeter-qt aborted after their first renders failed with EINVAL.

Add DRM_ASAHI_FEATURE_COMPUTE_ONLY (bit 41; bits 39 and 40 are claimed by
open PRs #2059 and #2523) and set it on that runtime. It is a property
of the device, not of a board or chip list: Mesa can decline to create
a graphics screen or a graphics queue on a device that reports it, and
fall back to software rendering, while compute keeps working. Kernels
without the bit keep their current behaviour.

Signed-off-by: Satya Benson <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor

This no longer applies to the current integration/aurora tip 110861f. Feature bit 39 has since been taken by DRM_ASAHI_FEATURE_ASYNC_SUBMIT, so DRM_ASAHI_FEATURE_VM_FENCE_ERROR_COUNT needs the next free bit, 40 (GET_PARAMS group 4 is still free), and the Borealis user in aurora-silicon/mesa#390 has to follow. The other conflicts, in g17/mod.rs (the reworked supports_soft_faults()) and g17/status.rs (imports and the new restart-charge test), keep both sides. Still open from the 11-21 review: register g17-fence-error-count-tests.py in run-host-tests.py so host-tests passes, give the bit number in the uapi commit message, and call the userspace driver Borealis in both messages.

A rebased copy is on this repository as rebase/20261123/pr-2059 (110861f plus this PR's commits, authorship unchanged, conflicts resolved as described above). It is there to save you the rebase; take it or redo it as you prefer.

Build check of 110861f plus this change (arm64, LLVM=1, Image, dtbs and every directory it touches, Fedora config plus asahi.config): builds, no warnings in the files it touches. Host tests (tools/asahi/run-host-tests.py --only) on 110861f plus this change: g15-input-error-tests does not start (the runner rejects an unregistered test file); g17 does not start (the runner rejects an unregistered test file); g17-fence-error-count-tests does not start (the runner rejects an unregistered test file).

satchlj added a commit that referenced this pull request Nov 24, 2026
The G14 firmware-27.2 runtime refuses render commands, but nothing in
GET_PARAMS says so. Graphics userspace therefore takes the device for
GL: on phellos (J413) with the GPU published, kmsro picked the aurora
render node for the internal panel, and the greeter's Hyprland and
sddm-greeter-qt aborted after their first renders failed with EINVAL.

Add DRM_ASAHI_FEATURE_COMPUTE_ONLY (bit 41; bits 39 and 40 are claimed by
open PRs #2059 and #2523) and set it on that runtime. It is a property
of the device, not of a board or chip list: Mesa can decline to create
a graphics screen or a graphics queue on a device that reports it, and
fall back to software rendering, while compute keeps working. Kernels
without the bit keep their current behaviour.

Signed-off-by: Satya Benson <[email protected]>
Bit 39 is ASYNC_SUBMIT. Bits 48 and 49 are already used by pending
features. Keep the optional GET_PARAMS mirror registration at group 4.

Signed-off-by: DjDeveloperr <[email protected]>
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Moved the VM fence-error counter to bit 50 at f8a0ddec31d7 to match mesa#390; both UAPI probes produced byte-identical AArch64 objects, and the fence-ordering fixture passed all ten mutation controls.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants