Skip to content

drm/asahi: g17: reuse a given-back compute pool for the next VM - #4275

Open
DjDeveloperr wants to merge 2 commits into
mainfrom
djdev/g17-compute-handover-reuse
Open

DjDeveloperr wants to merge 2 commits into
mainfrom
djdev/g17-compute-handover-reuse

Conversation

@DjDeveloperr

Copy link
Copy Markdown
Contributor

What: When every compute QID is taken, a client that needs a compute channel takes another VM's idle one. Until now each of these handovers freed the old pool's storage and then allocated, zeroed and mapped fresh backing for the new VM before it could bind. A compute pool is tens of MiB (the M5 directory alone is 8 MiB on the J714s, more on bigger boards), and with more clients than channels this happens thousands of times per round: the J714s cross_instance run made 4,100-6,200 handovers per clean round, and on the J700 at 40 clients the per-client submit rate dropped about 10x against clients with their own channel.

This change keeps the storage of a pool that is given back for another VM. Once the firmware has consumed the pool's release, the queue holds the storage together with the last owner's client alias. The re-arm takes it, drops that alias, moves the charge to its own client and clears the storage off the device mutex, the same way render pools already reuse released storage (cache_reclaimed). It still builds fresh backing when nothing was kept, the kept storage is too small, or the new client can't take the charge. The event worker frees kept storage that no re-arm took once no compute admission is waiting or the lower window is under pressure. Pools freed for other reasons (warm cap, idle expiry, a killed or moved queue) are freed as before.

Clearing reused M5 storage now skips the directory and block list, which populate_m5() rewrites in full anyway. G17P storage is cleared and its tables are written again with repopulate().

This replaces linux#3985, which gave compute more QIDs. Compute QIDs only go back to render at driver teardown, so that cost render queues later in a boot (J700: 39/40 clients at 40; J775c queue.exhaust: 40 queues instead of 48). Here the QID budget, render capacity, the release protocol and the isolation between VMs stay the same.

Tested:

  • New host test tools/asahi/g17-compute-handover-reuse-tests.py builds the production code against host stubs. It checks that only a give-back for another VM keeps storage, that the old alias is destroyed before the storage is reused (even while the event worker still holds the rest), that a refused charge leaves the storage untouched, and that reused storage is byte-for-byte the same as fresh backing for both the M5 and G17P layouts. It also checks that other vacates free storage as before and that a second give-back never replaces a kept spare. All 11 mutations are killed. The new queue fields are added to the stubs of g17-growth-tests.py, g17-m5-sibling-rearm-tests.py and g17-capacity-fault-tests.py.
  • run-host-tests.py --only g17 in the CI container (ubuntu2404 image, host-tests.yml toolchains): 416/416 (main: 404/404). The asahi-m4 command-stream selftest also passes.
  • arm64 LLVM=1 W=1 build of drivers/gpu/drm/asahi/ on the 7.3.0-main571a6c45 tree: 43 warning blocks with and without this change, 0 new.
  • checkpatch --strict: 0 errors. The one warning is the usual MAINTAINERS note for a new file.

Risk: two-way · the G17 compute give-back path on every G17 Mac (M5 J714s and J775c, Neo J700, and M4 Pro/Max T6040/T6041). If the clear missed a byte, one VM could read another VM's leftover scratch. The host test compares every byte against fresh backing, and no alias of the old VM survives the re-arm. Kept storage stays charged to the old client until a re-arm takes it, which happens only while a compute admission is waiting.

Not yet:

  • J714s row (M5 lead), aurora.ko only on the booted 7.3.0-main571a6c45 kernel, base main vs head, ABBA: cross_instance rounds with bind counts, queue.exhaust, dEQP-VK.api.smoke, one deep sleep. SLC on, watchdog armed.
  • J700 row (Neo lead), same arms, ABBA: 20/35/40 clients and queue.exhaust, which should match main.
  • The per-handover cost on hardware isn't measured yet. Whether clearing kept storage beats building fresh backing, and by how much, is what these rows show.

When every compute QID is taken, a client that needs a channel takes
another VM's idle one. The pool is released to the firmware, its
storage is freed, and the new owner allocates, zeroes and maps fresh
backing before it can bind. A compute pool is tens of MiB (the M5
directory alone is 8 MiB on the J714s and more on larger boards), so with
more clients than channels each handover costs milliseconds, and the
J714s cross_instance run makes thousands of them per round.

Keep the storage of a pool given back for another VM instead. Once the
firmware consumed the release, the queue holds it with the last owner's
alias. The re-arm takes it, drops that alias, moves the charge to its
own client and clears the storage off the device mutex, as render pools
already reuse released storage (cache_reclaimed). It allocates fresh
backing only when nothing was kept, the kept storage is too small or the
new client cannot take its charge. The event worker frees kept storage
that no re-arm took once no compute admission waits or the lower window
is under pressure.

Pools freed for other reasons (warm cap, idle expiry, a killed or moved
queue) are freed as before. The QID budget, render capacity and the
release protocol are unchanged.

Clearing an M5 backing now skips the directory and block list, which
populate_m5() rewrites in full; G17P backing is cleared and its tables
written again (repopulate()), as for fresh backing.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners December 4, 2026 06:30
@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) blast:neo 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 Dec 4, 2026
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Merge readiness at d33dbcbf: not ready yet

Check Detail
CI at head ok 9 checks passed
Review no needs one approving review from Core, Omarchy Mac or Aurora Maintainers (not the author)
Requested changes no requested by RyanTheTide
Hardware row at head no no line with the head SHA d33dbcbf, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text ok Risk: two-way · the G17 compute give-back path on every G17 Mac (M5 J714s and J775c, Neo J700, and M4 Pro/Max T6040/T6041). If the clear missed a byte, one VM could read another VM's leftover scratch. The host test compares every byte against fresh backing, and no alias of the old VM survives the re-arm. Kept storage stays charged to the old client until a re-arm takes it, which happens only while a compute admission is waiting.

Areas: area:gpu

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

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent review at head 17d3302. Verdict: looks right. I found no bug that should block this. It can merge once the J714s and J700 rows pass. Three small notes are at the end; none of them needs a change.

Can one VM see another VM's data? No.

  • The clear covers every byte. On M5, reset_for_client zeroes everything after the directory and block list. populate_m5 then rewrites both tables, and encode_directory and encode_freelist zero their whole region before they write entries (fw/m5/pool.rs:201, :218). The backing size is always a multiple of 16 KiB (the tables are rounded to 0x4000 and blocks are 2-16 MiB), so the client alias maps nothing past bytes_mut(). On G17P, fill(0) plus repopulate() produces the same bytes as PoolTables::init on zeroed memory. Reused storage comes out byte-for-byte equal to fresh backing.
  • The old owner loses its view before anything is cleared. Every client alias of a compute pool lives in binding.pool_alias (it is mapped only at queue/compute.rs:832, :2116, :2147 and :2601). observe_vacancy moves that alias into the spare, and Vacated::into_pages drops it before it hands back the pages. KernelMapping::drop unmaps and then flushes the TLB, both for the VM's slot and for every context that names the root (mmu.rs), before it returns. The new VM's alias is made only later, in bind_owner_channel, after rearm. take_pages resets client_vm, so serves() holds the pool to one VM at a time, as before.
  • The firmware is done with the storage first. A spare exists only after control_consumed saw the FreeListRelease cursor go by. That is the same proof main uses before it frees the pages, and the one growth and render repopulate use before they reuse storage in place.

Lifetime and concurrency: take_compute_spare and the worker's drop_spare both run under the device mutex and both use Option::take, so the spare can't be freed twice. The clear runs off-lock, on storage only the re-arm owns. ComputeDemand lasts across a waiter's retries, and when the last one drops it queues an event pass, which frees the spares that are left. A parked alias keeps the old VM's page tables alive until it is dropped, as a detached exited owner's binding already does today. Unload and device teardown drop the spare along with its queue, alias first. The charge moves before the clear. On a refusal or a failed populate the pages are dropped, and the charge comes off whichever client held it. A spare that is freed without being taken comes off the old client.

Other paths: only require_fresh_backing sets keep_backing, and it has two callers: give_back_foreign and the shared-idle path. Pools freed for the warm cap or idle expiry, killed queues (recycle_settled clears the flag) and moved queues are freed as before. The render M5 spare path also goes through reset_for_client and writes the same bytes as before. There is no new parameter, constant or board gate.

What I ran (pc, host-tests CI container ubuntu2404, rustc 1.96.0, no network):

  • run-host-tests.py --only g17- at 17d3302: 416/416, with all 11 mutations of the new test killed. No harness outside g17- reads the three changed files.
  • The new test against main's three driver files fails, but only structurally (take_spare is missing).
  • Two mutations of my own. Dropping the zero-fill in encode_directory or in encode_freelist fails the test ("M5: reused storage is exactly fresh backing"), so the "zero only after the tables" shortcut is guarded. Putting main's full clear back still passes, with the same bytes.
  • Main has no drivers/gpu/drm/asahi or tools/asahi changes since f778d6c (current main d2a4949). The queued kits' head driver tree is 154c9cc3, which is this head's tree, and their base tree is 3680a536, which is main's. Both kit arms are still current.

Notes (no change needed):

  1. rearm_vacant (runtime.rs:1890) takes the spare before its later checks. If the attempt then retries (Some(false)) or the descriptor ring can't be built, the cleared storage is freed and the retry builds fresh backing. This is safe; that one handover just doesn't get the reuse.
  2. If take_spare (queue/compute.rs:2314) finds can_rearm() false for a moment (source readers) but the later ready check passes, the queue gets fresh backing and the spare stays parked on a populated queue. It is freed when compute demand drops to zero, or a later re-arm of that queue takes it and clears it. It holds at most one spare per queue, and its alias is only the old owner's view of its own data.
  3. The ComputePool comment (freelist.rs:1065-1068) still says another VM gets the queue only after "fresh, zeroed backing" is installed. Kept storage cleared to the same bytes now counts too. Worth a word the next time the file is touched.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J700 (Neo, G17P) row at head 17d3302d: PASS. ABBA, aurora.ko only, on the 7.3.0-main571a6c45 Image. Base = main f778d6c5's aurora.ko (build ID 605e8de9), head = base + this PR (build ID 8dd2da23).

Boots: baseA 5f142b54, headA 3da16799, headB eff867c5, baseB 336ffa73. Every slot passed its gates (identity, no faults or WARN, watchdog), and the J700 is back on stock and clean.

Clients base submits/s head submits/s head/base cross-VM rebinds base → head compute channels base → head
N=20 52,999 52,939 1.00x 1 → 1 21 → 21
N=35 5,881 17,383 2.96x 9,017 → 19,741 24 → 24
N=40 5,682 19,068 3.36x 8,827 → 19,245 26 → 25
N=40, fresh boot (plan B) 2,178 3,600 1.65x 9,406 → 21,865 20 → 23

The same clients passed and the same refusals (none) at every N. queue.exhaust is 40 on both arms, so no capacity is lost. For information only: the two #3985-era checks n35_compute_channel_per_open_queue and n35_no_thrash fail on both base and head, so they say nothing about this PR.

Evidence (on Mini): work/j700-linux4275-hreuse-20261204/evidence/, which holds compare.txt and the four slot dirs j700-l4275-{baseA-082400,headA-083645,headB-084815,baseB-085823}.

Main since this branch: no forward-merge needed. Main is now 52 commits ahead of the merge base f778d6c5. Its asahi changes (the idle-hold policy, shared sync inputs in queue/mod.rs and job.rs, sysfs) don't touch freelist.rs, queue/compute.rs or runtime.rs, and none of main's new host harnesses stubs the compute Queue. The last 4 commits only change a t8122 DT file. There is no conflict, so the head stays 17d3302d and the hardware rows above match it exactly. The g17 host suite on the tree GitHub would merge (main 3450d3e1 + 17d3302d, merge tree 9f79818b) passed 420/420 on pc in the pinned ubuntu2404 host-tests image. That includes this PR's reuse test with all 11 mutations killed, and main's new g17-command-inputs test.

Still owed before merge:

  • The J714s (M5 Pro, G17S) row. Kit work/j714s-linux4275-hreuse-20261204 (KIT b8528782, offline check re-run OK at this head) is queued on the J714s. It passes if, in both ABBA pairs, head's median clean cross_instance round is below base's and queue.exhaust on head is no lower than base.
  • One approving review from Core or Omarchy Mac by someone other than the author.

@github-actions github-actions Bot removed the needs-hw-row No hardware row at the PR head (bot) label Dec 4, 2026
Signed-off-by: DjDeveloperr <hey@djdev.me>
@github-actions github-actions Bot added the needs-hw-row No hardware row at the PR head (bot) label Dec 4, 2026

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ARM64 W=1 build linked on current main. Its newer kill-retirement guard exposed a missing method in the extracted compute-reuse fixture, so I added the fixture stub in d33dbcb. All 30 reuse, capacity, growth and M5 sibling re-arm host runs now pass. The old and new merge trees have identical driver files. The J700 PASS and the sealed J714s ABBA kit therefore still cover the production code.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: REQUEST_CHANGES at d33dbcbfe4b6936a7dc757865664f72303d6839d

Failure: Vacated::into_pages() treats dropping the old KernelMapping as proof of a successful unmap. The production destructor can return with the mapping retained after an unmap error and provides no status to its caller. This path still hands the same backing to the next VM. The fixture verifies drop order, which does not establish that the old mapping was retired. Require confirmed unmap and translation retirement before reuse, and quarantine the backing if retirement fails.

Checked: The release-completion gate precedes the handoff and both layouts clear the storage before normal reuse.

@github-actions github-actions Bot added the changes-requested A reviewer requested changes (bot) label Dec 5, 2026

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) blast:neo Macs affected, from Merge risk (bot) changes-requested A reviewer requested changes (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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants