Skip to content

drm/asahi: g17: reuse cached queue records after a later release - #3721

Open
DjDeveloperr wants to merge 3 commits into
mainfrom
neo/g17p-recycle-queue-records
Open

DjDeveloperr wants to merge 3 commits into
mainfrom
neo/g17p-recycle-queue-records

Conversation

@DjDeveloperr

Copy link
Copy Markdown
Contributor

What: Reuse a returned G17P render queue-state object once the firmware has consumed a scheduler release published after the object's fence. Until now it waited for firmware stop.

  • drm/asahi: g17: keep G17P render queue records firmware-cached #3705 maps G17P render queue records firmware-cached. It keeps a returned queue-state object out of reuse until firmware stop, which only comes at driver teardown. After 64 retired render graphs, every new graph got uncached records again. On the MacBook Neo one multi-queue CTS run filled the pool, and q4 on new queues went from 40.35 to 42.52 µs, main's level (row).
  • The scheduler release record carries what the older firmware's DestroyContext carried: the context page and its bytes 0, 1, 4 and 0x23 (0x26 here). The older driver uses that command as a full firmware data-cache flush (flush_fw_cache in gpu.rs), and frees firmware-cached memory only after one. This PR applies the same rule to cached queue records: once a release published after the object's fence is consumed, the firmware no longer holds the record lines, so the pool may zero and rewrite them.
  • channel.rs tracks the latest scheduler release and the latest one the firmware consumed. object.rs marks cached queue states and checks that rule instead of the plain fence. Other families keep their fences.
  • tools/asahi/g17-object-fence-tests.py: a new test and three new mutations (release published before the return, reuse without a later release, retention until stop).

Tested: J700 (A18 Pro, G17P), Mesa 089aeb0e private ICD. One trial boot at 98cef721, run in the same order as the #3705 row: packet rows, then the full multi-queue CTS, then q4 again on queues created after the CTS. Median µs, range and visits, lower is better:

q4 triangle this PR #3705 head (its row)
before the CTS 40.37 (40.15–40.44, 10) 40.35 (39.89–40.64, 10)
new queues after the CTS 40.31 (40.18–40.44, 6) 42.52 (42.29–42.60, 6)
  • Queue-state pool after rows, CTS and the q4 rerun: 10, 13, 13 of 64 used, 0 uncached fallbacks. At the drm/asahi: g17: keep G17P render queue records firmware-cached #3705 head: 54, 64, 64 used, 81 uncached fallbacks.
  • dEQP-VK.synchronization2.op.multi_queue.* 16,680 Pass, 7,080 NotSupported, 0 Fail. dEQP-VK.synchronization.op.multi_queue.* 12,798 Pass, 9 NotSupported, 0 Fail. 0 recoveries.
  • compute.light d1 105.89 µs, d16 15.03 µs. Every readback and per-queue CRC check passed.
  • 0 kernel warnings, 0 GPU faults, 0 AVC denials, 0 failed units, SELinux enforcing, SLC on 4 planes. Stock Fedora -41 was restored and verified afterwards.
  • A test-only build of this commit (never for merge) took the newest reusable queue state first, so each reuse came as soon after its flush as possible. Before every render publication it read both records back and compared the host words with what the new owner wrote. Same boot sequence: 132 reuses, 108 render graphs built on reused records, 481,283 checks on those graphs (524,288 in all), 0 mismatches. The only record words the firmware changed were +0x1c and +0x94, as on drm/asahi: g17: keep G17P render queue records firmware-cached #3705. The CTS ran on reused records with the same result (16,680 and 12,798 Pass, 0 Fail, 0 recoveries, 0 GPU faults), and q4 was 40.27 µs before and 40.30 µs after.
  • Host tests: tools/asahi/run-host-tests.py --only g17 298/298, including the three new mutations. checkpatch.pl --strict: 0 errors, 0 warnings.

Risk: two-way door · G17P Macs (A18 Pro, M4 Pro/Max) reuse a cached render queue-state object after a later scheduler release instead of never. If a release did not flush the firmware data cache, the firmware could read a stale record line after reuse. The rule relies on the release behaving like the older DestroyContext. The test-only check above found no stale host word in a reused record.

Not yet:

Details

Why the pool filled

Pool::return_object gave a cached Family::QueueState object no fence (None), so Retained::ready never became true. The object kept its reserved slot until Pool::stop_after_firmware, which runs only from Firmware::drop. Each new render graph allocates one queue-state object, so 64 retired graphs filled the family, and every later graph fell back to an uncached allocation.

The rule

  • published_release() records the control-record count of each scheduler release at publication (runtime/teardown.rs). observe_control moves the consumed mark up to that release once the firmware has consumed it.
  • A cached queue state keeps the fence its family already had (control_published() on the A18 Pro, control_fence() elsewhere). It becomes reusable only when a release published after that fence has been consumed (release_witnessed() > fence). A release published before the return never counts, so the flush runs after the firmware last named the records.
  • The older driver frees firmware-cached memory the same way: it collects freed objects as garbage and frees them only after a later DestroyContext flush (gpu.rs). On G17 the release record has the same contents.
  • Every closed context publishes a release, so under churn the pool refills from retired graphs. Graphs retired after the latest consumed release wait for the next one.

Not run

  • The asahi KUnit suites: this change adds no KUnit test, and no warm KUnit build tree was available. The pool logic is covered by the host harness.

linux#3170 mapped the render queue records uncached because the host
rewrote them in place. Memory::set_owner retargeted a pair's records to
another context of the same VM, and on G17P bind_work_slot stored the
work slot at +0x38 of both records on every render submission, while
the previous kick could still run. Firmware-cached G17 lines are not
coherent with host stores and cannot be flushed. A dirty firmware line
can revert such a store when it is evicted, and the firmware can keep
reading its stale copy: the J716c QueueRecord.job_list_va revert.

On the A18 Pro the uncached records cost a four-queue render row about
5% against the 7.3.0-41 kernel, which still cached them.

Stop rewriting the records and map them firmware-cached again on G17P:

- Only the same logical context keeps a pair's graph. Any other
  context, also of the same VM, gets its own graph and queue records,
  as on M5. The outgoing graph's VM pool mappings move out first, as
  M5 already does. A kept graph's records already name its context:
  that is checked, never written.
- G17P writes the work slot into both records once, for the graph's
  first publication, before any kick names them. The context's render
  channel for the pair keeps that slot from then on. A pair whose
  records carry slot N takes only channel N, and waits for that
  channel's publications on another pair to retire, as for any channel
  move. A different slot is refused.

Invariant: the host writes a G17P render queue record only before the
first kick that names it (whole in Graph::new, the work slot in the
first bind_work_slot). Only the firmware writes it after that kick.
Every host-owned word in a firmware cache line therefore equals the
value in memory, so the firmware never reads a stale host word, and a
dirty line it evicts writes back the same host words. The pointer
blocks, job list and item rings, which the host writes on every kick,
stay on the uncached pages after the records. The pool zeroes a
returned queue-state object only after its fence is met, as for every
pooled firmware-cached family. M5 keeps its records uncached.

Signed-off-by: DjDeveloperr <hey@djdev.me>
A control-ring witness proves that the firmware consumed a release. It
does not establish that the firmware discarded the old queue records.
Recycling their backing lets the pool zero and reinitialize cache lines
that the firmware can still hold across queue owners.

Keep a returned queue-state object with a cached prefix in its reserved
pool slot until firmware stop. No control witness makes it reusable.
This keeps the mapping and its backing alive within the existing slot
and byte limits. Once full, the pool supplies fresh uncached objects.

Exercise successive queue lifetimes under both release policies in the
host pool harness. Check distinct backing and addresses, the uncached
fallback at capacity, and teardown at firmware stop.

Fixes: 08a29b9 ("drm/asahi: g17: keep G17P render queue records firmware-cached")
Signed-off-by: DjDeveloperr <hey@djdev.me>
A returned G17P queue-state object, whose records the firmware caches,
waits for firmware stop before reuse, because consuming its owner's
scheduler release does not show that the firmware dropped their lines.
Firmware stop only comes at teardown. After 64 retired render graphs,
every new graph gets uncached records again. On the J700 one
multi-queue CTS run filled the pool, and q4 on new queues went from
40.35 to 42.52 us, main's level.

The scheduler release carries what the older firmware's DestroyContext
did: the context page and its bytes 0, 1, 4 and 0x23 (here 0x26). That
command also flushes the firmware data cache, and the older firmware's
driver frees firmware-cached memory only after one (flush_fw_cache).
Do the same: reuse a returned cached queue state once a release
published after its fence has been consumed. Track the latest release
and the latest consumed one next to the control-ring counts.

The host harness checks that a release published before the return
does not count, that a later one counts only once consumed, and that
retired graphs with releases between them never fall back to uncached
records. Three new mutations cover each rule.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners December 1, 2026 02:07
@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (bot) blast:m4 Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (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 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Merge readiness at 98cef721: not ready yet

Check Detail
CI at head ok 2 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 in the PR body at 98cef721
Conflicts ok none
PR text ok Risk: two-way · G17P Macs (A18 Pro, M4 Pro/Max) reuse a cached render queue-state object after a later scheduler release instead of never. If a release did not flush the firmware data cache, the firmware could read a stale record line after reuse. The rule relies on the release behaving like the older DestroyContext. The test-only check above found no stale host word in a reused record.

Areas: area:gpu

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

@github-actions github-actions Bot removed the ci-failing A check failed at the PR head (bot) label Dec 2, 2026
@DjDeveloperr
DjDeveloperr changed the base branch from neo/g17p-cached-render-queue-records to main December 2, 2026 20:52
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Independent review of 98cef721f703, by a reviewing agent that didn't write or plan it. The code has no blocking findings. Merge still waits on hardware evidence for the cache-flush premise.

What holds up:

  • published_release() runs under the device mutex right after publish_control.
  • RELEASE_CONSUMED only rises when the release count is at or below the consumed count, so released > fence implies the old fence <= witnessed.
  • A release counted for an object was always published after that object was returned.
  • No wraparound (u64 counts).
  • Recovery publishes no releases, so nothing is reused during it.
  • The rule applies only to QueueState with prefix != 0. M5, G17S/C/G and the older families keep the plain fence.
  • The three new mutations are killed.

Test gap (not blocking, worth fixing): g17-object-fence-tests.py copies release_witnessed() and friends, but its release()/consume() are hand-written copies. So observe_control() (g17/channel.rs:80-83), published_release() (channel.rs:66-68) and the call in runtime/teardown.rs:135 are never compiled by any test. Removing the if release <= consumed guard from channel.rs still passes all 298 g17 host tests, yet it lets a queue state be reused before the firmware has run the release. Pulling those two functions into the harness with block(), plus a mutation for the guard, would pin it.

Premise, still a hardware question:

  • The tree now says both things. queue/render/memory.rs:154 and queue/compute.rs:203 still say G17 firmware-cached lines "cannot be flushed", while memory.rs:159-161 says the release flushes the firmware data cache. One of them should go.
  • The evidence for the flush is the DestroyContext analogy (from the older single-coprocessor firmware) and the J700 readback run (132 reuses, 0 mismatches). That run has no positive control: the same readback build reusing at the plain fence, without a release, which would show it can catch a surviving line at all. It also can't see the firmware reading a stale clean line, and it says nothing about the A18 Pro's second coprocessor.
  • M4 Pro/Max on 26B123 sends the release as opcode 0x13, the M5 encoding, so the analogy is weakest there. That is also where no row exists yet. A per-firmware Variant flag, like objects_after_release, would keep unmeasured firmware on drm/asahi: g17: keep G17P render queue records firmware-cached #3705's retain-until-stop behaviour, at a performance cost only.

Local checks on this head merged with main d4b3c9a7ca8a (arm64, LLVM=1):

  • tools/asahi/run-host-tests.py --only g17: 336/336 pass.
  • W=1 build of drivers/gpu/drm/asahi/ links aurora.o. Its 39 warnings are all dead-code warnings in files this PR doesn't touch.

What's left: an M4 Pro/Max (G17P) row at this head, J616s or J616c, ideally on 26B123; the comment contradiction resolved; and the positive control or the per-firmware flag.

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) 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.

1 participant