Repository navigation
drm/asahi: g17: allocate compute objects in SUBMIT, not at publication - #3704
Draft
jgoyvaerts wants to merge 7 commits into
Draft
jgoyvaerts wants to merge 7 commits into
jgoyvaerts wants to merge 7 commits into
Conversation
The firmware objects of a G17 client's queues were kernel memory: counted
towards the device total (kernel_resident in gpu_memory) but owned by no
client, and owned by no memory cgroup when a kernel thread populated them.
Under a dq-fast timeline-cases load on an M4 Pro, kernel_resident peaked
at 2.4 GB.
This charges the objects that QUEUE_CREATE builds, the context's scheduler
storage and work state, to the client that creates the queue:
- **Driver accounting:** they count as the client's held memory (fdinfo
drm-resident-system, its limits) instead of kernel memory.
- **Memory cgroup:** their pages are charged to the client's memory
cgroup. That is the cgroup of the task that opened the DRM file, made
the active memory cgroup while the pages are populated.
- **Failure:** QUEUE_CREATE builds them in the client's ioctl, so a
failed allocation comes back from it as ENOMEM, which Borealis reports
as VK_ERROR_OUT_OF_HOST_MEMORY.
- **Lifetime:** the charge belongs to the object. It is released when the
object is freed, also after the client's file is gone while the
firmware still holds the object.
The rest of a queue's objects stay uncharged, as before: render pairs,
kick rings, generations, tiler heaps, compute state and timestamps. G17
builds them when a submission is published, after SUBMIT accepted it. A
memory cgroup at its limit would fail them there, and a failure there
costs the client its device: a test kernel with all of them charged lost
4 devices with ENOMEM when its cgroup filled.
- They are charged once they are built in QUEUE_CREATE or SUBMIT.
- The Allocator now names its client: QUEUE_CREATE's names it, the
publication paths say None.
- `KernelObject::queue_backing()` and `Allocator::backing()`/`object()`
are the way in for both.
Device-global objects stay uncharged kernel memory: the boot-time firmware
structures and heaps (initdata.rs, initdata/m5.rs), the crash buffer, the
PM metrics and the M5 metrics in mod.rs, and the shared compute pool.
Tests:
- **KUnit:** a queue charge is taken past both limits, counts against the
client's and the device's next objects, outlives the client's file and
is released once with its object. A memory-cgroup scope restores what
it replaced.
- **Host test** (g17-queue-object-charge-tests.py):
- only QUEUE_CREATE's Allocator names a client, and every other one
says None;
- uncharged kernel objects appear only in the global boot files and in
the three publication files that build objects outside an Allocator
(buffer.rs, buffer/target.rs, completion.rs);
- three injected faults are caught.
Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
A fresh G17 compute queue was built for the QID publication reserves: the pool's control header names the QID's pool slot, and the kick ring carries the QID. So its objects could only be allocated once the QID was known, at publication, after SUBMIT accepted the job. Only stores depend on the QID. Split construction in two: - **Allocate:** `compute::Kit::new` allocates the pool's control and state (`freelist::PoolObjects`), the kick ring storage (`kick::ComputeRing`) and the graph's objects (`GraphObjects`), with no QID. The pool pages and the descriptor ring are allocated as before. - **Bind:** `compute::Queue::new_channel(kit, pages, ring, id, ...)` binds them to the QID: `ComputePool::bind` writes the control header, `kick::Queue::compute` keeps the id and `Graph::new` writes the owner's queue record. No device memory is allocated. Publication still calls both, in the same order of effects: the pool's row is installed before the queue is built. A later change allocates in SUBMIT. tools/asahi/g17-compute-reserve-tests.py checks that the bind steps allocate nothing, with two mutations that inject an allocation. Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
G17P's device-wide compute aperture (ComputeShared, 22 blocks of 1 MiB plus its lists) and compute scheduler were built by the first compute publication that needed them, on the scheduler's kworker, after SUBMIT had accepted the job. A refused allocation there failed the job, and the client lost its device. A compute SUBMIT now allocates them while they are neither installed nor prepared (`Backend::provision_compute`, from `ensure_compute`). A refusal is that SUBMIT's error. The publication that needs them takes the prepared object and installs it, as before; it allocates nothing, so its builder flags, the waits for another builder and the condition variable they waited on go. Installation keeps its timing: the aperture with the first publication's lease, the scheduler with the first fresh queue's pool row. Both stay device memory: they serve every client until processor stop. Allocated in a client's ioctl, their pages would be charged to that client's memory cgroup, so `memacct::device_memory` charges them to the root cgroup, as when the kworker allocated them. M5 has neither and allocates nothing. Tests: - g17-compute-constructor-tests now runs the production SUBMIT and publication halves (prepared once, a refusal fails the SUBMIT and prepares nothing, publication never builds and keeps the prepared object on cancel, a stale lease or a crash) with three mutations. - g17-admission-order-tests checks that every valid compute SUBMIT provisions and that a refusal fails it. - g17-m5-render-fast-done-tests checks that note_changed wakes render waiters only. - KUnit asahi_memacct: device_memory_is_charged_to_the_root_cgroup. Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
jgoyvaerts
force-pushed
the
m4pro/g17-compute-reserve
branch
from
November 30, 2026 18:58
9a9efbe to
c769558
Compare
A G17 compute queue's backing (the pool pages, at least 23 MiB on G17P, and the descriptor ring) and a fresh queue's kit were allocated when a command was published, on the scheduler's kworker, after SUBMIT had accepted it. A refusal there failed the job, and the client lost its device. With the backing charged to the client's memory cgroup, a client at its cgroup limit lost it that way. Each logical queue (owner) now has a reserve of compute backing: - **SUBMIT provisions.** A compute SUBMIT allocates what its publication would lack (`Backend::provision_backing`): pages that fit the command and a ring, unless the owner's spares or the queues holding its backing have them, and a kit while the owner has no queue of its own. Pages and ring are charged to the client through its Allocator; the kit is device memory (`memacct::device_memory`), since an installed queue outlives the owner. A refusal is that SUBMIT's ENOMEM or ENOSPC, before the command is queued. - **Publication takes.** Re-arming a vacant queue, building a fresh one (now in one device-mutex section with its install), growing a pool and adding an M5 sibling take from the owner's spares and allocate no device memory. A sibling that finds none asks the next SUBMIT for a set. - **Backing is the owner's until it closes.** A queue records whose backing it holds. When it gives the backing back (a pool release the firmware consumed, a grown pool's smaller pages, a recycled ring, a never-published queue's rollback), the pages and ring go back to that owner's spares, reset, while the owner has compute commands. Another open owner's backing is never bound: a queue holding it is given back first, as another VM's is. - **Claims.** Each compute command holds a claim on its owner from before its SUBMIT provisions until the command is dropped. An owner without claims is idle: what its queues give back is freed, and the pool pass frees its spares, so idle memory is what it was. Close frees the rest. A publication whose backing is on its way back waits as it waits for a channel today, and the event pass that observes the consumed release returns the backing and wakes it. That release is published by the pool pass and consumed by the firmware, whatever the waiting client does. Tests: - KUnit asahi_g17_compute_reserve: spares move without being freed, the smallest fitting pages are taken, a full owner frees only covered pages, and each spare is freed once. - g17-compute-reserve-tests: publication paths allocate nothing; SUBMIT charges pages and ring and allocates the kit as device memory; the claim precedes it and the command is queued after; every give-back keeps the backing for its owner; the production keep_backing, stock and trim_reserves free every spare exactly once; a queue-less owner always gets a kit. Six more mutations. - g17-queue-object-charge-tests: SUBMIT's compute backing is the second allocator that names the client; two more mutations. - Fixture updates for the changed functions in the g17 compute, growth, sibling and cleanup tests. Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
jgoyvaerts
force-pushed
the
m4pro/g17-compute-reserve
branch
from
November 30, 2026 21:05
c769558 to
2c8dfe7
Compare
Signed-off-by: DjDeveloperr <hey@djdev.me>
Signed-off-by: DjDeveloperr <hey@djdev.me>
Signed-off-by: DjDeveloperr <hey@djdev.me>
Contributor
|
Fixed compute backing ownership, channel rollback and concurrent shared-aperture allocation in 936c70c. |
This was referenced Dec 2, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What: Stacked on #3683; review only the top three commits. On G17, compute publication no longer allocates. A compute SUBMIT allocates what its publication would need, and a refusal is that SUBMIT's ENOMEM or ENOSPC, before the command is queued. Before, a refusal at publication (the scheduler's kworker, after SUBMIT returned) failed the job, and the client lost its device.
Tested: no Mac at this head yet (936c70c). A partial hardware run on an M4 Pro (J616s) at 2c8dfe7, before the two newest commits: see Evidence. KUnit (arm64 qemu) at 2c8dfe7: with CONFIG_MEMCG=y 955 run, 954 passed, 1 skipped, 0 failed; g17-compute-reserve host test and its 8 mutations pass. 2c8dfe7 differs from c769558 only in building
Spares::is_emptyandtake_allfor KUnit alone (a non-KUnit build warned they were unused); at c769558, with drivers/gpu/drm/asahi/.kunitconfig (no memory cgroups) 521 run, 517 passed, 4 skipped. Host tests at 9a9efbe (this head but for one KUnit assertion): 845 runs pass; the 29 that fail also fail at #3683's head (apple-dcp, test-m3-uapi, g15-pb-growth x2, g15-retention --mutations, g15-stats-drain x2, sep-suspend-errors x22).Risk: two-way door · M4 and M5 (G17). Every Mac running the driver: compute publication paths change. A compute owner that has commands keeps its backing, at least 23 MiB of pool pages on G17P, while they are pending. Idle owners hold what they held before.
Not yet:
Details
Where compute publication allocated (main), and where it moved:
Pages::for_compute), descriptor ringrearm_vacant,build_compute,try_add_compute_sibling,grow_computeBackend::provision_backing, charged to the clientcompute::Kit)PoolBacking::new,Backing::new,Queue::newQID split (commit 1). Only stores depend on the QID: the pool control header's buffer slot (
ComputePool::bind), the kick ring's id (kick::Queue::compute), and the owner's queue record (Graph::new).Kit::newallocates without a QID.Device memory. Objects that outlive the client that caused them (the kit, the shared aperture and scheduler) are allocated in the client's ioctl. So that their pages are not charged to that client's memory cgroup, they are allocated under
memacct::device_memory, which makes the root cgroup the active one. That matches what happened when the kworker allocated them.The reserve (commit 3).
Registry::compute_lackschecks whether the owner's spares, or the queues holding its backing (also while they give it back), have pages that fit the command and a ring. If not, SUBMIT allocates them. An owner without a queue of its own also gets a kit.backing_owner). When it gives the backing back, the pages and ring return to that owner's spares, reset and cleared, while the owner has compute commands. A backing comes back from:No deadlock: why a publication waiting for its backing always gets it.
Rebind::Done), or growth takes the queue's pages after draining its siblings;bind_reusablebinds it, or growth asks the queue to give it back (recall_backing);vacate_idle_poolsforces queues marked for fresh backing).keep_backing) and wakes channel and progress waiters.wait_channels,wait_release).So the wait does not depend on its own client: not on a fence the client waits for, nor on a later SUBMIT. A kill or a cancel ends it through the admission check. During recovery, the admission is extended as for every other publication wait. The 30 s admission deadline still bounds it, as it bounds every publication wait.
Evidence
asahi_g17_compute_reserve(4 cases): spares move without being freed; the smallest fitting pages are taken; a full owner frees only covered pages; each spare is freed once.asahi_memacct:device_memory_is_charged_to_the_root_cgroup.g17-compute-reserve-tests:keep_backing,stockandtrim_reservesfree every spare exactly once, also when the owner closes with its reserve unused.Mutations: 7.
g17-compute-constructor-tests: SUBMIT prepares the shared objects once as device memory, and a refusal fails it. Publication never builds them, and keeps them on cancel, a stale lease or a crash. Mutations: 3.g17-admission-order-tests: every valid compute SUBMIT provisions, and a refusal fails it.g17-queue-object-charge-tests: exactly QUEUE_CREATE's context and SUBMIT's compute backing name the client. Mutations: 2 new.