Skip to content

drm/asahi: g17: keep G17P render queue records firmware-cached - #3705

Open
DjDeveloperr wants to merge 2 commits into
mainfrom
neo/g17p-cached-render-queue-records
Open

DjDeveloperr wants to merge 2 commits into
mainfrom
neo/g17p-cached-render-queue-records

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

What: Map the G17P render queue records firmware-cached again, and never store to a record after a kick has named it.

  • drm/asahi: g17: keep firmware-mutable pooled state uncached #3170 made these records uncached because the host rewrote them in place. Memory::set_owner retargeted a pair's records to another context of the same VM. On G17P, bind_work_slot also stored the work slot at +0x38 on every render submission while the previous kick could still run. drm/asahi: g17: keep G17P work nodes and compute descriptors firmware-cached #3186 left them uncached for the same reason.
  • On the MacBook Neo that cost the four-queue render triangle about 5% against 7.3.0-41, which still cached them.
  • Only the same logical context now keeps a pair's graph. Any other context, also of the same VM, gets its own graph and queue records, as M5 already does.
  • G17P writes the work slot into both records once, before the graph's first kick. From then on, the context's render channel for that pair keeps that slot: a pair whose records carry slot N takes only channel N.
  • Invariant: the host writes a G17P render queue record only before the first kick that names it. After that, only the firmware writes it. So every host word in a firmware cache line equals the value in memory, and an evicted dirty line writes back the same host words. The pages with the pointer blocks, job list and item rings stay uncached. M5 keeps its records uncached.
  • tools/asahi/g17-object-fence-tests.py pins the policy with five new mutations, and a KUnit test covers the channel rule.

Tested: J700 (A18 Pro, G17P), Mesa 089aeb0e private ICD. One-shot boots of main e6c7b032633c without and with this commit, built from one tree with the same config, 10 visits per row per boot, 8 boots in A B B A, B A, A B order. Median µs per boot; lower is better:

row main this PR paired change 7.3.0-41
render triangle, 4 queues 42.33 – 42.63 40.17 – 40.28 −5.2% (−5.06, −5.75, −4.83, −5.25) 40.1
compute.light d1 105.51, 105.53 105.84, 105.80 +0.3% (+0.32, +0.25) 108.4
compute.light d16 14.97, 15.01 15.03, 14.99 +0.1% (+0.45, −0.19) 15.2
  • Every readback and CRC check passed.
  • The d1 and d16 rows come from the last two pairs, which waited until 150 s after boot. A boot job runs about 80–120 s after boot on every kernel and slows both kernels alike, so the first four boots, which started at about 35 s, only give the triangle row.
  • Every boot had SELinux enforcing, SLC on 4 planes, 0 kernel warnings, 0 GPU faults, 0 AVC denials and 0 failed units.
  • On 3 boots of each kernel, dEQP-VK.synchronization2.op.multi_queue.* gave 16,680 Pass, 7,080 NotSupported, 0 Fail. dEQP-VK.synchronization.op.multi_queue.* gave 12,798 Pass, 9 NotSupported, 0 Fail. Results were identical case for case, with 0 recoveries.
  • EGL context churn, one boot of each kernel: dEQP-EGL.functional.{create_context,create_context_ext,multicontext,multithread,sharing,thread_cleanup}.* gave 1,607 Pass, 105 NotSupported, 0 Fail on both.
  • A test-only build of this commit read both records' host words back on every render publication. Over 262,144 checks, none differed from what the host wrote. The only record words the firmware changed in memory were +0x1c and +0x94; +0x1c shares a cache line with the work slot and job_list_va.
  • Host tests: tools/asahi/run-host-tests.py --only g17 295/295, neo-auxiliary-tests 3/3, legacy-wq-prepub-abort-tests 10/10. The asahi KUnit suites on arm64 QEMU: 494 pass, 0 fail.
  • checkpatch.pl --strict: 0 errors, 1 warning (a 101-column line). It is kept so this head stays byte-identical to the tested aurora.ko.
  • Exact head 6d632d43, one J700 boot (row): q4 40.35 µs before the pool filled; sync2 op.multi_queue 16,680 Pass / 0 Fail, sync 12,798 Pass / 0 Fail; 0 GPU faults, 0 warnings.

Risk: two-way door · G17P Macs (A18 Pro, M4 Pro/Max) get cached render queue records. On every G17, a context that rebinds an idle pair of another context of the same VM now builds a new graph, as M5 already did. That rebind did not occur in any of the workloads above, because a closed context's pairs are parked, and parked pairs already get new records.

Not yet:

Details

Rows not run

Why the old writes were unsafe

Firmware-cached G17 lines are not coherent with host stores, and G17 has no firmware cache flush. If the firmware holds a record line dirty, a later eviction writes its stale copy of a host word back over the host's store (the J716c job_list_va revert). It can also keep reading that stale copy. On G17P the firmware writes +0x1c of a render queue record, which is in the same line as +0x10 (job_list_va) and +0x38 (work slot).

What changed

  • queue/render/memory.rs: Family::QueueState maps its first pages, which hold one record per QID, firmware-cached on G17P. bind_work_slot stores +0x38 only while the graph has no slot yet, and refuses a different slot afterwards. set_owner is replaced by a read-only check that the kept records name the context.
  • queue/render.rs, runtime/render/prepare.rs: only the same context keeps a graph. For a same-VM rebind the outgoing graph's VM pool mappings move out first, as on M5.
  • context.rs, context/identity.rs: RenderSlots::bind takes the slot the pair's records carry.
  • fw/queue.rs: QueueRecord::names_owner for the check.

Per-boot medians (µs)

boot kernel triangle q4 d1 d16
1 this PR 40.28 (boot job) (boot job)
2 main 42.43 (boot job) (boot job)
3 main 42.63 (boot job) (boot job)
4 this PR 40.18 (boot job) (boot job)
5 main 42.33 105.51 14.97
6 this PR 40.28 105.84 15.03
7 this PR 40.17 105.80 14.99
8 main 42.40 105.53 15.01

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 <[email protected]>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 30, 2026 19:25

@aurora-silicon-review aurora-silicon-review Bot 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: HOLD at 08a29b94

Blockers:

  • Hosted CI workflows (Apple build and KUnit, Host tests, pr-text, pr-readiness) are still queued at 08a29b94.
  • Pending second-generation / cross-variant hardware rows listed under Not yet:
    • M5 (J716c / G17C or M5) dEQP-VK.synchronization2.op.multi_queue.* regression check (shared rebind and render-channel paths).
    • M4 Pro/Max (G17P) hardware verification row (also receives firmware-cached render queue records).

Checked:

  • Audited all 8 files (+186/-150):
    • drivers/gpu/drm/asahi/g17/queue/render/memory.rs: RECORD_BYTES (static_assert!(RECORD_BYTES <= POINTERS[0])) maps only the G17P queue-record pages firmware-cached while keeping POINTERS, job lists, and item rings uncached (and all of Family::QueueState uncached on M5). bind_work_slot() writes WORK_SLOT (+0x38) only once before the graph's first kick (work_slot == None) and rejects any mismatched slot (EINVAL). set_owner() is replaced by read-only names_owner().
    • drivers/gpu/drm/asahi/g17/queue/render.rs & runtime/render/prepare.rs: unified per-context graph ownership (keep_graph = Arc::ptr_eq(self.context(), &context) / foreign = !Arc::ptr_eq(pair.context(), &self.context)), moving outgoing same-VM pool mappings out before installing the new graph so live records are never retargeted in place.
    • drivers/gpu/drm/asahi/g17/context.rs, context/identity.rs, fw/queue.rs: RenderSlots::bind() / get() enforce required: Option<u32> via allows(required, index) so a G17P pair whose records carry slot N only takes channel N (covered by new KUnit test a_pair_takes_only_the_channel_its_records_carry); QueueRecord::names_owner() safely copies packed fields by value.
    • tools/asahi/g17-object-fence-tests.py & run-host-tests.py: 5 new policy mutations enforce the M5-uncached / G17P-cached / write-once work-slot / per-context graph / pinned render-channel invariants.
    • Exact-head 08a29b94 J700 (A18 Pro, G17P) verification: -5.2% 4-queue render triangle (42.33-42.63 -> 40.17-40.28 µs), 16,680 sync2 + 12,798 sync multi-queue CTS + 1,607 EGL churn cases identical with 0 recoveries/faults/AVCs, 262,144/262,144 host-word readbacks unchanged, 295/295 g17 host tests and 494/494 asahi KUnit tests pass.
Evidence
  • Code is clean with zero defects found; ready to upgrade once hosted CI finishes green and the M5 + M4 Pro/Max hardware rows land.

@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) 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) labels Nov 30, 2026
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Merge readiness at 6d632d43: not ready yet

Check Detail
CI at head no failing: pr-text / pr-text, pr-text / pr-text, KUnit isp, KUnit isp, KUnit jpeg, KUnit jpeg
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 6d632d43
Conflicts ok none
PR text ok Risk: two-way · G17P Macs (A18 Pro, M4 Pro/Max) get cached render queue records. On every G17, a context that rebinds an idle pair of another context of the same VM now builds a new graph, as M5 already did. That rebind did not occur in any of the workloads above, because a closed context's pairs are parked, and parked pairs already get new records.

Areas: area:gpu

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

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 <[email protected]>

@aurora-silicon-review aurora-silicon-review Bot 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: HOLD at 6d632d43

Blockers:

  1. CI checks are still queued at 6d632d43. Build, host tests (g17-object-fence-tests.py), KUnit, and pr-text runs are still queued/running (2P/0F/18Q).
  2. Exact-head hardware verification at 6d632d43. The earlier J700 row ran at 08a29b94 before 6d632d43 changed Pool::return_object() to retain cached Family::QueueState objects until firmware stop (with uncached fallback once POOL_SLOTS fills). Please post an exact-head 6d632d43 G17P verification row alongside the remaining M5 (synchronization2.op.multi_queue.*) and M4 Pro/Max (G17P) regression rows.

Checked:

  • Delta 08a29b94 -> 6d632d43 (drivers/gpu/drm/asahi/g17/object.rs, queue/render/memory.rs, tools/asahi/g17-object-fence-tests.py, +67/-14):
    • Retained::fence changed to Option<u64> with ready() returning false for None; Pool::return_object() sets fence = None for Family::QueueState if lease.prefix != 0, closing the cross-owner firmware-cache reuse hazard where a control-ring release witness does not guarantee firmware cache line eviction.
    • Pool::allocate() (match oldest { Some((index, _, true)) => ... }) never reuses fence: None entries, guarantees a return slot via inner.reserved[family_index], and falls back cleanly to deferred uncached allocations once POOL_SLOTS or POOL_BYTES fills.
    • cached_queue_lifetimes() in g17-object-fence-tests.py exercises 0..=POOL_SLOTS generations across both after_release modes.
Evidence
  • Verified drivers/gpu/drm/asahi/g17/object.rs:624-640, 825-933, and 936-988 at 6d632d4341c599150e7f739ff9051d27fd23e34f.
  • Note on steady-state behavior: after POOL_SLOTS render graphs have been created and destroyed on G17P without a firmware stop, Family::QueueState cache capacity is permanently occupied by retained fence: None entries and subsequent render graphs take the uncached fallback (consistent with memory.rs:159-160).

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

FINISH, then merge at 08a29b9452fb. One cache-lifetime issue remains on G17P.

  1. When a parked pair goes to a new context, the object pool can hand it the same pages and firmware address. drivers/gpu/drm/asahi/g17/object.rs:854 zeroes the entire old object, then drivers/gpu/drm/asahi/g17/queue/render/memory.rs:198 writes the new owner's records. The fence at g17/channel.rs:55 observes the control-ring consumer. It proves the old release was processed, but supplies no guarantee that firmware discarded its cached record lines. A surviving line can give the next kick the old job_list_va, or write it back later. Keep kicked, cached pages out of reuse until firmware stops (new allocations can fall back to uncached pages), or establish that the release cleans and invalidates these lines before the host writes them.
What I checked

Within one graph, both slot stores precede publication. Ordinary re-kicks and replay keep the slot, same-context rebind only reads the records, and recovery writes the uncached pointer blocks. The G17S/C/G render records and the compute graph covered by #3170 stay uncached. The change uses the existing firmware layout and derives the prefix from the record size, QID range and page size. Older GPU paths are unaffected.

I ran the pool code from this head in the PR's host harness. With each G17P fence policy, the next allocation returned the same backing and address and zeroed the prior record. That confirms the host write. The harness does not model the firmware cache.

The J700 measurements and 494 passing KUnit tests are recorded at this head. The outstanding hardware checks are the M4 Pro/Max G17P run and the J716c multi-queue run from #3170. For the G17P check, verifying the new owner's words across reuse of the same record address would test the lifetime at issue.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J700 (Neo, G17P) at head 6d632d43: q4 on fresh queues after the cached pool fills

Tested 6d632d4341c5 on one trial boot (boot ee81773f): kernel 7.3.0-neo-fence-qret, vmlinux build ID 9976ce57, aurora.ko ebdfd771. Stock Fedora -41 was restored and verified afterwards.

The boot ran three steps in order: the packet rows, then the full multi-queue CTS, then the q4 triangle again on fresh queues. The G17 capacity file was read before and after each step.

q4 triangle median µs range visits
before the pool filled 40.35 39.89–40.64 10
after the pool filled (fresh queues) 42.52 42.29–42.60 6

That is +5.4 %, back at main's level with uncached records (42.33–42.63). The two ranges do not overlap. Every leg passed its per-queue CRC readback.

Queue-state pool (cached_objects_2):

  • before the rows: 2 of 64 used
  • after the rows: 54 used, 52 waiting for a firmware stop
  • after the CTS: 64 of 64 used, 62 waiting, 53 uncached fallbacks
  • after the q4 rerun: 64 used, 62 waiting, 81 uncached fallbacks

No firmware stop happened during the boot, so retired graphs never gave their slots back. After 64 retired graphs, every new render graph gets uncached records and pays the #3170 cost again. So at this head the q4 fix lasts only until the pool fills. On this boot that took one CTS run.

Correctness on the same boot:

  • sync2 op.multi_queue: 16,680 Pass / 7,080 NotSupported / 0 Fail
  • sync op.multi_queue: 12,798 Pass / 9 NotSupported / 0 Fail
  • 0 recoveries
  • compute.light d1 105.60 µs and d16 14.96 µs, readbacks ok
  • 0 WARN, 0 GPU faults, 0 AVC, 0 failed units, SLC on 4 planes

Result: no correctness regression. The q4 gain is lost once the pool fills (+5.4 %, the same as before the fix). The before/after q4 numbers come from one boot and are not ABBA, because the pool state cannot be undone within a boot.

@aurora-silicon-review aurora-silicon-review Bot 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: MERGE at 6d632d4341c5

Converts this App's earlier review at the same head to an approval under the 12-01 merge bar (better than before and doesn't break; block only on a shown failure). No code blocker was found in it. The merge gate (a row on one Mac at this head + fast CI) is tracked by pr-readiness, not held here.

Checked: - Delta 08a29b94 -> 6d632d43 (drivers/gpu/drm/asahi/g17/object.rs, queue/render/memory.rs, tools/asahi/g17-object-fence-tests.py, +67/-14):

  • Retained::fence changed to Option<u64> with ready() returning false for None; Pool::return_object() sets fence = None for Family::QueueState if lease.prefix != 0, closing the cross-owner firmware-cache reuse hazard where a control-ring release witness does not guarantee firmware cache line eviction.
  • Pool::allocate() (match oldest { Some((index, _, true)) => ... }) never reuses fence: None entries, guarantees a return slot via inner.reserved[family_index], and falls back cleanly to deferred uncached allocations once POOL_SLOTS or POOL_BYTES fills.
  • cached_queue_lifetimes() in g17-object-fence-tests.py exercises 0..=POOL_SLOTS generations across both after_release modes.

Follow-ups (not blocking): queued CI; exact-head G17P row (M5/M4 rows not required)

@RyanTheTide

Copy link
Copy Markdown
Contributor

Row: J616s (M4 Pro, T6040) at 6d632d43: PASS.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants